2
# Copyright (C) 2008-2009 Oprea Dan
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.
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.
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.
33
import gnomeclipboardtools
34
import gnomemessagebox
35
import gnomesnapshotstools
41
class SnapshotsDialog:
42
def __init__( self, snapshots, glade ):
43
self.snapshots = snapshots
44
self.config = snapshots.config
50
self.dialog = self.glade.get_widget( 'SnapshotsDialog' )
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
64
self.edit_path = self.glade.get_widget( 'edit_path' )
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' )
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' )
73
self.edit_diff_cmd.set_text( diff_cmd )
74
self.edit_diff_cmd_params.set_text( diff_cmd_params )
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 )
80
self.glade.signal_autoconnect( signals )
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 )
89
#display name, snapshot_id
90
self.store_snapshots = gtk.ListStore( str, str )
91
self.list_snapshots.set_model( self.store_snapshots )
93
self.store_snapshots.set_sort_column_id( 0, gtk.SORT_DESCENDING )
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
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 )
107
self.glade.get_widget( 'btn_copy_snapshot' ).set_sensitive( True )
109
iter = self.list_snapshots.get_selection().get_selected()[1]
111
self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( False )
113
path = self.store_snapshots.get_value( iter, 1 )
114
self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( len( path ) > 1 )
116
def on_btn_restore_snapshot_clicked( self, button ):
117
iter = self.list_snapshots.get_selection().get_selected()[1]
119
self.glade.get_widget('btn_restore_snapshot').set_sensitive( False )
120
gobject.timeout_add( 100, self.restore_ )
122
def restore_( self ):
123
iter = self.list_snapshots.get_selection().get_selected()[1]
125
self.snapshots.restore( self.store_snapshots.get_value( iter, 1 ), self.path )
127
self.glade.get_widget( 'btn_restore_snapshot' ).set_sensitive( True )
130
def on_btn_copy_snapshot_clicked( self, button ):
131
iter = self.list_snapshots.get_selection().get_selected()[1]
133
path = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
134
gnomeclipboardtools.clipboard_copy_path( path )
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]
139
path = self.store_snapshots.get_value( iter, 2 )
140
path = gnomevfs.escape_path_string(path)
141
selection_data.set_uris( [ 'file://' + path ] )
143
def on_list_snapshots_cursor_changed( self, list ):
144
self.update_toolbar()
146
def on_list_snapshots_button_press_event( self, list, event ):
147
if event.button != 3:
150
if len( self.store_snapshots ) <= 0:
153
path = self.list_snapshots.get_path_at_pos( int( event.x ), int( event.y ) )
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 )
162
def on_list_snapshots_popup_menu( self, list ):
163
self.showPopupMenu( list, 1, gtk.get_current_event_time() )
165
def show_popup_menu( self, list, button, time ):
166
iter = list.get_selection().get_selected()[1]
171
self.popup_menu = gtk.Menu()
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 )
178
self.popup_menu.append( gtk.SeparatorMenuItem() )
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 )
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 )
189
path = self.store_snapshots.get_value( iter, 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 )
196
self.popup_menu.append( gtk.SeparatorMenuItem() )
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 )
203
self.popup_menu.show_all()
204
self.popup_menu.popup( None, None, None, button, time )
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' ) )
209
def on_list_snapshots_jumpto_item( self, widget, data = None ):
210
self.dialog.response( gtk.RESPONSE_OK )
212
def on_list_snapshots_open_item( self, widget, data = None ):
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' ) )
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' ) )
221
def on_btn_diff_with_clicked( self, button ):
222
if len( self.store_snapshots ) < 1:
225
#get path from the list
226
iter = self.list_snapshots.get_selection().get_selected()[1]
229
path1 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
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 )
234
#check if the 2 paths are different
236
gnomemessagebox.show_error( self.dialog, self.config, _('You can\'t compare a snapshot to itself') )
239
diff_cmd = self.edit_diff_cmd.get_text()
240
diff_cmd_params = self.edit_diff_cmd_params.get_text()
242
if not tools.check_command( diff_cmd ):
243
gnomemessagebox.show_error( self.dialog, self.config, _('Command not found: %s') % diff_cmd )
246
params = diff_cmd_params
247
params = params.replace( '%1', "\"%s\"" % path1 )
248
params = params.replace( '%2', "\"%s\"" % path2 )
250
cmd = diff_cmd + ' ' + params + ' &'
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 )
261
def update_snapshots( self, current_snapshot_id, snapshots_list ):
262
self.edit_path.set_text( self.path )
265
self.store_snapshots.clear()
267
path = self.snapshots.get_snapshot_path_to( current_snapshot_id, self.path )
268
isdir = os.path.isdir( path )
271
index_combo_diff_with = 0
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
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
293
if len( self.store_snapshots ) > 0:
294
iter = self.store_snapshots.get_iter_first()
296
self.list_snapshots.get_selection().select_iter( iter )
297
self.combo_diff_with.set_active( index_combo_diff_with )
299
self.glade.get_widget( 'btn_diff_with' ).set_sensitive( True )
300
self.combo_diff_with.set_sensitive( True )
302
self.glade.get_widget( 'btn_diff_with' ).set_sensitive( False )
303
self.combo_diff_with.set_sensitive( False )
305
self.list_snapshots.grab_focus()
306
self.update_toolbar()
308
def on_list_snapshots_row_activated( self, list, path, column ):
311
def open_item( self ):
312
iter = self.list_snapshots.get_selection().get_selected()[1]
315
path = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path )
316
cmd = "gnome-open \"%s\" &" % path
319
def run( self, path, snapshots_list, current_snapshot_id, icon_name ):
321
self.icon_name = icon_name
322
self.update_snapshots( current_snapshot_id, snapshots_list )
326
ret_val = self.dialog.run()
328
if gtk.RESPONSE_OK == ret_val: #go to
329
iter = self.list_snapshots.get_selection().get_selected()[1]
331
snapshot_id = self.store_snapshots.get_value( iter, 1 )