~ubuntu-branches/ubuntu/maverick/backintime/maverick

« back to all changes in this revision

Viewing changes to gnomesnapshotsdialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Wiltshire
  • Date: 2009-05-16 23:04:32 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090516230432-orrutvtufbtuxsc6
Tags: 0.9.24-1
* New upstream version (closes: #527447):
  - backintime is no longer aware of 'backintime-gnome' and 'backintime-kde4'
    (you need run 'backintime-gnome' for GNOME version and 'backintime-kde4'
    for KDE4 version)
  - fix a bug that crashes the program after taking a snapshot
* Update homepage field in debian/control (closes: #527595)
* Refactor packaging to fit new upstream build system (an almost entire 
  re-write of debian/rules)
* Make configure scripts use /bin/sh instead of /bin/bash (they don't use
  bash features)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Back In Time
2
 
#    Copyright (C) 2008-2009 Oprea Dan
3
 
#
4
 
#    This program is free software; you can redistribute it and/or modify
5
 
#    it under the terms of the GNU General Public License as published by
6
 
#    the Free Software Foundation; either version 2 of the License, or
7
 
#    (at your option) any later version.
8
 
#
9
 
#    This program is distributed in the hope that it will be useful,
10
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#    GNU General Public License for more details.
13
 
#
14
 
#    You should have received a copy of the GNU General Public License along
15
 
#    with this program; if not, write to the Free Software Foundation, Inc.,
16
 
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 
 
18
 
 
19
 
import os
20
 
import os.path
21
 
import sys
22
 
import pygtk
23
 
pygtk.require("2.0")
24
 
import gtk
25
 
import gnomevfs
26
 
import gobject
27
 
import gtk.glade
28
 
import datetime
29
 
import gettext
30
 
 
31
 
import config
32
 
import tools
33
 
import gnomeclipboardtools 
34
 
import gnomemessagebox
35
 
import gnomesnapshotstools
36
 
 
37
 
 
38
 
_=gettext.gettext
39
 
 
40
 
 
41
 
class SnapshotsDialog:
42
 
        def __init__( self, snapshots, glade ):
43
 
                self.snapshots = snapshots
44
 
                self.config = snapshots.config
45
 
                self.glade = glade
46
 
 
47
 
                self.path = None
48
 
                self.icon_name = None
49
 
 
50
 
                self.dialog = self.glade.get_widget( 'SnapshotsDialog' )
51
 
 
52
 
                signals = { 
53
 
                        'on_list_snapshots_cursor_changed' : self.on_list_snapshots_cursor_changed,
54
 
                        'on_list_snapshots_row_activated' : self.on_list_snapshots_row_activated,
55
 
                        'on_list_snapshots_popup_menu' : self.on_list_snapshots_popup_menu,
56
 
                        'on_list_snapshots_button_press_event': self.on_list_snapshots_button_press_event,
57
 
                        'on_list_snapshots_drag_data_get': self.on_list_snapshots_drag_data_get,
58
 
                        'on_btn_diff_with_clicked' : self.on_btn_diff_with_clicked,
59
 
                        'on_btn_copy_snapshot_clicked' : self.on_btn_copy_snapshot_clicked,
60
 
                        'on_btn_restore_snapshot_clicked' : self.on_btn_restore_snapshot_clicked
61
 
                        }
62
 
 
63
 
                #path
64
 
                self.edit_path = self.glade.get_widget( 'edit_path' )
65
 
 
66
 
                #diff
67
 
                self.edit_diff_cmd = self.glade.get_widget( 'edit_diff_cmd' )
68
 
                self.edit_diff_cmd_params = self.glade.get_widget( 'edit_diff_cmd_params' )
69
 
 
70
 
                diff_cmd = self.config.get_str_value( 'gnome.diff.cmd', 'meld' )
71
 
                diff_cmd_params = self.config.get_str_value( 'gnome.diff.params', '%1 %2' )
72
 
 
73
 
                self.edit_diff_cmd.set_text( diff_cmd )
74
 
                self.edit_diff_cmd_params.set_text( diff_cmd_params )
75
 
 
76
 
                #setup backup folders
77
 
                self.list_snapshots = self.glade.get_widget( 'list_snapshots' )
78
 
                self.list_snapshots.drag_source_set( gtk.gdk.BUTTON1_MASK | gtk.gdk.BUTTON3_MASK, gtk.target_list_add_uri_targets(), gtk.gdk.ACTION_COPY )
79
 
 
80
 
                self.glade.signal_autoconnect( signals )
81
 
                
82
 
                text_renderer = gtk.CellRendererText()
83
 
                column = gtk.TreeViewColumn( _('Snapshots') )
84
 
                column.pack_end( text_renderer, True )
85
 
                column.add_attribute( text_renderer, 'markup', 0 )
86
 
                column.set_sort_column_id( 0 )
87
 
                self.list_snapshots.append_column( column )
88
 
 
89
 
                #display name, snapshot_id
90
 
                self.store_snapshots = gtk.ListStore( str, str )
91
 
                self.list_snapshots.set_model( self.store_snapshots )
92
 
 
93
 
                self.store_snapshots.set_sort_column_id( 0, gtk.SORT_DESCENDING )
94
 
 
95
 
                #setup diff with combo
96
 
                self.combo_diff_with = self.glade.get_widget( 'combo_diff_with' )
97
 
                text_renderer = gtk.CellRendererText()
98
 
                self.combo_diff_with.pack_start( text_renderer, True )
99
 
                self.combo_diff_with.add_attribute( text_renderer, 'markup', 0 )
100
 
                self.combo_diff_with.set_model( self.store_snapshots ) #use the same store
101
 
 
102
 
        def update_toolbar( self ):
103
 
                if len( self.store_snapshots ) <= 0:
104
 
                        self.glade.get_widget( 'btn_copy_snapshot' ).set_sensitive( False )
105
 
                        self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( False )
106
 
                else:
107
 
                        self.glade.get_widget( 'btn_copy_snapshot' ).set_sensitive( True )
108
 
 
109
 
                        iter = self.list_snapshots.get_selection().get_selected()[1]
110
 
                        if iter is None:
111
 
                                self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( False )
112
 
                        else:
113
 
                                path = self.store_snapshots.get_value( iter, 1 )
114
 
                                self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( len( path ) > 1 )
115
 
 
116
 
        def on_btn_restore_snapshot_clicked( self, button ):
117
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
118
 
                if not iter is None:
119
 
                        self.glade.get_widget('btn_restore_snapshot').set_sensitive( False )
120
 
                        gobject.timeout_add( 100, self.restore_ )
121
 
 
122
 
        def restore_( self ):
123
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
124
 
                if not iter is None:
125
 
                        self.snapshots.restore( self.store_snapshots.get_value( iter, 1 ), self.path )
126
 
 
127
 
                self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( True )
128
 
                return False
129
 
 
130
 
        def on_btn_copy_snapshot_clicked( self, button ):
131
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
132
 
                if not iter is None:
133
 
                        path = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
134
 
                        gnomeclipboardtools.clipboard_copy_path( path )
135
 
 
136
 
        def on_list_snapshots_drag_data_get( self, widget, drag_context, selection_data, info, timestamp, user_param1 = None ):
137
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
138
 
                if not iter is None:
139
 
                        path = self.store_snapshots.get_value( iter, 2 )
140
 
                        path = gnomevfs.escape_path_string(path)
141
 
                        selection_data.set_uris( [ 'file://' + path ] )
142
 
 
143
 
        def on_list_snapshots_cursor_changed( self, list ):
144
 
                self.update_toolbar()
145
 
 
146
 
        def on_list_snapshots_button_press_event( self, list, event ):
147
 
                if event.button != 3:
148
 
                        return
149
 
 
150
 
                if len( self.store_snapshots ) <= 0:
151
 
                        return
152
 
 
153
 
                path = self.list_snapshots.get_path_at_pos( int( event.x ), int( event.y ) )
154
 
                if path is None:
155
 
                        return
156
 
                path = path[0]
157
 
        
158
 
                self.list_snapshots.get_selection().select_path( path )
159
 
                self.update_toolbar()
160
 
                self.show_popup_menu( self.list_snapshots, event.button, event.time )
161
 
 
162
 
        def on_list_snapshots_popup_menu( self, list ):
163
 
                self.showPopupMenu( list, 1, gtk.get_current_event_time() )
164
 
 
165
 
        def show_popup_menu( self, list, button, time ):
166
 
                iter = list.get_selection().get_selected()[1]
167
 
                if iter is None:
168
 
                        return
169
 
 
170
 
                #print "popup-menu"
171
 
                self.popup_menu = gtk.Menu()
172
 
 
173
 
                menu_item = gtk.ImageMenuItem( 'backintime.open' )
174
 
                menu_item.set_image( gtk.image_new_from_icon_name( self.icon_name, gtk.ICON_SIZE_MENU ) )
175
 
                menu_item.connect( 'activate', self.on_list_snapshots_open_item )
176
 
                self.popup_menu.append( menu_item )
177
 
 
178
 
                self.popup_menu.append( gtk.SeparatorMenuItem() )
179
 
 
180
 
                menu_item = gtk.ImageMenuItem( 'backintime.copy' )
181
 
                menu_item.set_image( gtk.image_new_from_stock( gtk.STOCK_COPY, gtk.ICON_SIZE_MENU ) )
182
 
                menu_item.connect( 'activate', self.on_list_snapshots_copy_item )
183
 
                self.popup_menu.append( menu_item )
184
 
 
185
 
                menu_item = gtk.ImageMenuItem( gtk.STOCK_JUMP_TO )
186
 
                menu_item.connect( 'activate', self.on_list_snapshots_jumpto_item )
187
 
                self.popup_menu.append( menu_item )
188
 
 
189
 
                path = self.store_snapshots.get_value( iter, 1 )
190
 
                if len( path ) > 1:
191
 
                        menu_item = gtk.ImageMenuItem( 'backintime.restore' )
192
 
                        menu_item.set_image( gtk.image_new_from_stock( gtk.STOCK_UNDELETE, gtk.ICON_SIZE_MENU ) )
193
 
                        menu_item.connect( 'activate', self.on_list_snapshots_restore_item )
194
 
                        self.popup_menu.append( menu_item )
195
 
 
196
 
                self.popup_menu.append( gtk.SeparatorMenuItem() )
197
 
 
198
 
                menu_item = gtk.ImageMenuItem( 'backintime.diff' )
199
 
                #menu_item.set_image( gtk.image_new_from_stock( gtk.STOCK_COPY, gtk.ICON_SIZE_MENU ) )
200
 
                menu_item.connect( 'activate', self.on_list_snapshots_diff_item )
201
 
                self.popup_menu.append( menu_item )
202
 
 
203
 
                self.popup_menu.show_all()
204
 
                self.popup_menu.popup( None, None, None, button, time )
205
 
 
206
 
        def on_list_snapshots_diff_item( self, widget, data = None ):
207
 
                self.on_btn_diff_with_clicked( self.glade.get_widget( 'btn_diff_with' ) )
208
 
 
209
 
        def on_list_snapshots_jumpto_item( self, widget, data = None ):
210
 
                self.dialog.response( gtk.RESPONSE_OK )
211
 
 
212
 
        def on_list_snapshots_open_item( self, widget, data = None ):
213
 
                self.open_item()
214
 
 
215
 
        def on_list_snapshots_restore_item( self, widget, data = None ):
216
 
                self.on_btn_restore_snapshot_clicked( self.glade.get_widget( 'btn_restore_snapshot' ) )
217
 
 
218
 
        def on_list_snapshots_copy_item( self, widget, data = None ):
219
 
                self.on_btn_copy_snapshot_clicked( self.glade.get_widget( 'btn_copy_snapshot' ) )
220
 
 
221
 
        def on_btn_diff_with_clicked( self, button ):
222
 
                if len( self.store_snapshots ) < 1:
223
 
                        return
224
 
 
225
 
                #get path from the list
226
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
227
 
                if iter is None:
228
 
                        return
229
 
                path1 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
230
 
 
231
 
                #get path from the combo
232
 
                path2 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( self.combo_diff_with.get_active_iter(), 1 ), self.path )
233
 
 
234
 
                #check if the 2 paths are different
235
 
                if path1 == path2:
236
 
                        gnomemessagebox.show_error( self.dialog, self.config, _('You can\'t compare a snapshot to itself') )
237
 
                        return
238
 
 
239
 
                diff_cmd = self.edit_diff_cmd.get_text()
240
 
                diff_cmd_params = self.edit_diff_cmd_params.get_text()
241
 
 
242
 
                if not tools.check_command( diff_cmd ):
243
 
                        gnomemessagebox.show_error( self.dialog, self.config, _('Command not found: %s') % diff_cmd )
244
 
                        return
245
 
 
246
 
                params = diff_cmd_params
247
 
                params = params.replace( '%1', "\"%s\"" % path1 )
248
 
                params = params.replace( '%2', "\"%s\"" % path2 )
249
 
 
250
 
                cmd = diff_cmd + ' ' + params + ' &'
251
 
                os.system( cmd  )
252
 
 
253
 
                #check if the command changed
254
 
                old_diff_cmd = self.config.get_str_value( 'gnome.diff.cmd', 'meld' )
255
 
                old_diff_cmd_params = self.config.get_str_value( 'gnome.diff.params', '%1 %2' )
256
 
                if diff_cmd != old_diff_cmd or diff_cmd_params != old_diff_cmd_params:
257
 
                        self.config.set_str_value( 'gnome.diff.cmd', diff_cmd )
258
 
                        self.config.set_str_value( 'gnome.diff.params', diff_cmd_params )
259
 
                        self.config.save()
260
 
 
261
 
        def update_snapshots( self, current_snapshot_id, snapshots_list ):
262
 
                self.edit_path.set_text( self.path )
263
 
 
264
 
                #fill snapshots
265
 
                self.store_snapshots.clear()
266
 
        
267
 
                path = self.snapshots.get_snapshot_path_to( current_snapshot_id, self.path )    
268
 
                isdir = os.path.isdir( path )
269
 
 
270
 
                counter = 0
271
 
                index_combo_diff_with = 0
272
 
                
273
 
                #add now
274
 
                path = self.path
275
 
                if os.path.exists( path ):
276
 
                        if os.path.isdir( path ) == isdir:
277
 
                                self.store_snapshots.append( [ gnomesnapshotstools.get_snapshot_display_markup( self.snapshots, '/' ), '/' ] )
278
 
                                if '/' == current_snapshot_id:
279
 
                                        indexComboDiffWith = counter
280
 
                                counter += 1
281
 
                                
282
 
                #add snapshots
283
 
                for snapshot in snapshots_list:
284
 
                        path = self.snapshots.get_snapshot_path_to( snapshot, self.path )
285
 
                        if os.path.exists( path ):
286
 
                                if os.path.isdir( path ) == isdir:
287
 
                                        self.store_snapshots.append( [ gnomesnapshotstools.get_snapshot_display_markup( self.snapshots, snapshot ), snapshot ] )
288
 
                                        if snapshot == current_snapshot_id:
289
 
                                                index_combo_diff_with = counter
290
 
                                        counter += 1
291
 
 
292
 
                #select first item
293
 
                if len( self.store_snapshots ) > 0:
294
 
                        iter = self.store_snapshots.get_iter_first()
295
 
                        if not iter is None:
296
 
                                self.list_snapshots.get_selection().select_iter( iter )
297
 
                        self.combo_diff_with.set_active( index_combo_diff_with )
298
 
        
299
 
                        self.glade.get_widget( 'btn_diff_with' ).set_sensitive( True )
300
 
                        self.combo_diff_with.set_sensitive( True )
301
 
                else:
302
 
                        self.glade.get_widget( 'btn_diff_with' ).set_sensitive( False )
303
 
                        self.combo_diff_with.set_sensitive( False )
304
 
 
305
 
                self.list_snapshots.grab_focus()
306
 
                self.update_toolbar()
307
 
 
308
 
        def on_list_snapshots_row_activated( self, list, path, column ):
309
 
                self.open_item()
310
 
 
311
 
        def open_item( self ):
312
 
                iter = self.list_snapshots.get_selection().get_selected()[1]
313
 
                if iter is None:
314
 
                        return
315
 
                path = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
316
 
                cmd = "gnome-open \"%s\" &" % path
317
 
                os.system( cmd )
318
 
 
319
 
        def run( self, path, snapshots_list, current_snapshot_id, icon_name ):
320
 
                self.path = path
321
 
                self.icon_name = icon_name
322
 
                self.update_snapshots( current_snapshot_id, snapshots_list )
323
 
 
324
 
                snapshot_id = None
325
 
                while True:
326
 
                        ret_val = self.dialog.run()
327
 
                        
328
 
                        if gtk.RESPONSE_OK == ret_val: #go to
329
 
                                iter = self.list_snapshots.get_selection().get_selected()[1]
330
 
                                if not iter is None:
331
 
                                        snapshot_id = self.store_snapshots.get_value( iter, 1 )
332
 
                                break
333
 
                        else:
334
 
                                #cancel, close ...
335
 
                                break
336
 
 
337
 
                self.dialog.hide()
338
 
                return snapshot_id
339