/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2007-07-15 18:12:57 UTC
  • Revision ID: jelmer@samba.org-20070715181257-g264qus2zyi3v39z
Add RevisionSelectionBox widget, use in TagDialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import os
 
18
import sys
 
19
import time
 
20
 
 
21
# gettext support
 
22
import gettext
 
23
gettext.install('olive-gtk')
 
24
 
 
25
try:
 
26
    import pygtk
 
27
    pygtk.require("2.0")
 
28
except:
 
29
    pass
 
30
 
 
31
import gobject
 
32
import gtk
 
33
import gtk.gdk
 
34
import gtk.glade
 
35
 
 
36
from bzrlib.branch import Branch
 
37
import bzrlib.errors as bzrerrors
 
38
from bzrlib.lazy_import import lazy_import
 
39
from bzrlib.ui import ui_factory
 
40
from bzrlib.workingtree import WorkingTree
 
41
 
 
42
from bzrlib.plugins.gtk.dialog import error_dialog, info_dialog, warning_dialog
 
43
from bzrlib.plugins.gtk.errors import show_bzr_error
 
44
from guifiles import GLADEFILENAME
 
45
 
 
46
from bzrlib.plugins.gtk.diff import DiffWindow
 
47
lazy_import(globals(), """
 
48
from bzrlib.plugins.gtk.viz import branchwin
 
49
""")
 
50
from bzrlib.plugins.gtk.annotate.gannotate import GAnnotateWindow
 
51
from bzrlib.plugins.gtk.annotate.config import GAnnotateConfig
 
52
from bzrlib.plugins.gtk.commit import CommitDialog
 
53
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
54
from bzrlib.plugins.gtk.initialize import InitDialog
 
55
from bzrlib.plugins.gtk.push import PushDialog
 
56
from bzrlib.plugins.gtk.revbrowser import RevisionBrowser
 
57
 
 
58
def about():
 
59
    """ Display the AboutDialog. """
 
60
    from bzrlib.plugins.gtk import __version__
 
61
    from bzrlib.plugins.gtk.olive.guifiles import GLADEFILENAME
 
62
 
 
63
    # Load AboutDialog description
 
64
    dglade = gtk.glade.XML(GLADEFILENAME, 'aboutdialog')
 
65
    dialog = dglade.get_widget('aboutdialog')
 
66
 
 
67
    # Set version
 
68
    dialog.set_version(__version__)
 
69
 
 
70
    dialog.run()
 
71
    # Destroy the dialog
 
72
    dialog.destroy()
 
73
 
 
74
class OliveGtk:
 
75
    """ The main Olive GTK frontend class. This is called when launching the
 
76
    program. """
 
77
    
 
78
    def __init__(self):
 
79
        self.toplevel = gtk.glade.XML(GLADEFILENAME, 'window_main', 'olive-gtk')
 
80
        self.window = self.toplevel.get_widget('window_main')
 
81
        self.pref = Preferences()
 
82
        self.path = None
 
83
 
 
84
        # Initialize the statusbar
 
85
        self.statusbar = self.toplevel.get_widget('statusbar')
 
86
        self.context_id = self.statusbar.get_context_id('olive')
 
87
        
 
88
        # Get the main window
 
89
        self.window_main = self.toplevel.get_widget('window_main')
 
90
        # Get the HPaned
 
91
        self.hpaned_main = self.toplevel.get_widget('hpaned_main')
 
92
        # Get the TreeViews
 
93
        self.treeview_left = self.toplevel.get_widget('treeview_left')
 
94
        self.treeview_right = self.toplevel.get_widget('treeview_right')
 
95
        # Get some important menu items
 
96
        self.menuitem_add_files = self.toplevel.get_widget('menuitem_add_files')
 
97
        self.menuitem_remove_files = self.toplevel.get_widget('menuitem_remove_file')
 
98
        self.menuitem_file_make_directory = self.toplevel.get_widget('menuitem_file_make_directory')
 
99
        self.menuitem_file_rename = self.toplevel.get_widget('menuitem_file_rename')
 
100
        self.menuitem_file_move = self.toplevel.get_widget('menuitem_file_move')
 
101
        self.menuitem_file_annotate = self.toplevel.get_widget('menuitem_file_annotate')
 
102
        self.menuitem_view_show_hidden_files = self.toplevel.get_widget('menuitem_view_show_hidden_files')
 
103
        self.menuitem_branch = self.toplevel.get_widget('menuitem_branch')
 
104
        self.menuitem_branch_init = self.toplevel.get_widget('menuitem_branch_initialize')
 
105
        self.menuitem_branch_get = self.toplevel.get_widget('menuitem_branch_get')
 
106
        self.menuitem_branch_checkout = self.toplevel.get_widget('menuitem_branch_checkout')
 
107
        self.menuitem_branch_pull = self.toplevel.get_widget('menuitem_branch_pull')
 
108
        self.menuitem_branch_push = self.toplevel.get_widget('menuitem_branch_push')
 
109
        self.menuitem_branch_revert = self.toplevel.get_widget('menuitem_branch_revert')
 
110
        self.menuitem_branch_merge = self.toplevel.get_widget('menuitem_branch_merge')
 
111
        self.menuitem_branch_commit = self.toplevel.get_widget('menuitem_branch_commit')
 
112
        self.menuitem_branch_tags = self.toplevel.get_widget('menuitem_branch_tags')
 
113
        self.menuitem_branch_status = self.toplevel.get_widget('menuitem_branch_status')
 
114
        self.menuitem_branch_missing = self.toplevel.get_widget('menuitem_branch_missing_revisions')
 
115
        self.menuitem_branch_conflicts = self.toplevel.get_widget('menuitem_branch_conflicts')
 
116
        self.menuitem_stats = self.toplevel.get_widget('menuitem_stats')
 
117
        self.menuitem_stats_diff = self.toplevel.get_widget('menuitem_stats_diff')
 
118
        self.menuitem_stats_log = self.toplevel.get_widget('menuitem_stats_log')
 
119
        # Get some toolbuttons
 
120
        #self.menutoolbutton_diff = self.toplevel.get_widget('menutoolbutton_diff')
 
121
        self.toolbutton_diff = self.toplevel.get_widget('toolbutton_diff')
 
122
        self.toolbutton_log = self.toplevel.get_widget('toolbutton_log')
 
123
        self.toolbutton_commit = self.toplevel.get_widget('toolbutton_commit')
 
124
        self.toolbutton_pull = self.toplevel.get_widget('toolbutton_pull')
 
125
        self.toolbutton_push = self.toplevel.get_widget('toolbutton_push')
 
126
        # Get the drive selector
 
127
        self.combobox_drive = gtk.combo_box_new_text()
 
128
        self.combobox_drive.connect("changed", self._refresh_drives)
 
129
        
 
130
        # Get the navigation widgets
 
131
        self.hbox_location = self.toplevel.get_widget('hbox_location')
 
132
        self.button_location_up = self.toplevel.get_widget('button_location_up')
 
133
        self.button_location_jump = self.toplevel.get_widget('button_location_jump')
 
134
        self.entry_location = self.toplevel.get_widget('entry_location')
 
135
        self.image_location_error = self.toplevel.get_widget('image_location_error')
 
136
        
 
137
        # Get the History widgets
 
138
        self.check_history = self.toplevel.get_widget('checkbutton_history')
 
139
        self.entry_history = self.toplevel.get_widget('entry_history_revno')
 
140
        self.button_history = self.toplevel.get_widget('button_history_browse')
 
141
        
 
142
        self.vbox_main_right = self.toplevel.get_widget('vbox_main_right')
 
143
        
 
144
        # Dictionary for signal_autoconnect
 
145
        dic = { "on_window_main_destroy": gtk.main_quit,
 
146
                "on_window_main_delete_event": self.on_window_main_delete_event,
 
147
                "on_quit_activate": self.on_window_main_delete_event,
 
148
                "on_about_activate": self.on_about_activate,
 
149
                "on_menuitem_add_files_activate": self.on_menuitem_add_files_activate,
 
150
                "on_menuitem_remove_file_activate": self.on_menuitem_remove_file_activate,
 
151
                "on_menuitem_file_make_directory_activate": self.on_menuitem_file_make_directory_activate,
 
152
                "on_menuitem_file_move_activate": self.on_menuitem_file_move_activate,
 
153
                "on_menuitem_file_rename_activate": self.on_menuitem_file_rename_activate,
 
154
                "on_menuitem_file_annotate_activate": self.on_menuitem_file_annotate_activate,
 
155
                "on_menuitem_view_show_hidden_files_activate": self.on_menuitem_view_show_hidden_files_activate,
 
156
                "on_menuitem_view_refresh_activate": self.on_menuitem_view_refresh_activate,
 
157
                "on_menuitem_branch_initialize_activate": self.on_menuitem_branch_initialize_activate,
 
158
                "on_menuitem_branch_get_activate": self.on_menuitem_branch_get_activate,
 
159
                "on_menuitem_branch_checkout_activate": self.on_menuitem_branch_checkout_activate,
 
160
                "on_menuitem_branch_revert_activate": self.on_menuitem_branch_revert_activate,
 
161
                "on_menuitem_branch_merge_activate": self.on_menuitem_branch_merge_activate,
 
162
                "on_menuitem_branch_commit_activate": self.on_menuitem_branch_commit_activate,
 
163
                "on_menuitem_branch_push_activate": self.on_menuitem_branch_push_activate,
 
164
                "on_menuitem_branch_pull_activate": self.on_menuitem_branch_pull_activate,
 
165
                "on_menuitem_branch_tags_activate": self.on_menuitem_branch_tags_activate,
 
166
                "on_menuitem_branch_status_activate": self.on_menuitem_branch_status_activate,
 
167
                "on_menuitem_branch_missing_revisions_activate": self.on_menuitem_branch_missing_revisions_activate,
 
168
                "on_menuitem_branch_conflicts_activate": self.on_menuitem_branch_conflicts_activate,
 
169
                "on_menuitem_stats_diff_activate": self.on_menuitem_stats_diff_activate,
 
170
                "on_menuitem_stats_log_activate": self.on_menuitem_stats_log_activate,
 
171
                "on_menuitem_stats_infos_activate": self.on_menuitem_stats_infos_activate,
 
172
                "on_toolbutton_refresh_clicked": self.on_menuitem_view_refresh_activate,
 
173
                "on_toolbutton_log_clicked": self.on_menuitem_stats_log_activate,
 
174
                #"on_menutoolbutton_diff_clicked": self.on_menuitem_stats_diff_activate,
 
175
                "on_toolbutton_diff_clicked": self.on_menuitem_stats_diff_activate,
 
176
                "on_toolbutton_commit_clicked": self.on_menuitem_branch_commit_activate,
 
177
                "on_toolbutton_pull_clicked": self.on_menuitem_branch_pull_activate,
 
178
                "on_toolbutton_push_clicked": self.on_menuitem_branch_push_activate,
 
179
                "on_treeview_right_button_press_event": self.on_treeview_right_button_press_event,
 
180
                "on_treeview_right_row_activated": self.on_treeview_right_row_activated,
 
181
                "on_treeview_left_button_press_event": self.on_treeview_left_button_press_event,
 
182
                "on_treeview_left_row_activated": self.on_treeview_left_row_activated,
 
183
                "on_button_location_up_clicked": self.on_button_location_up_clicked,
 
184
                "on_button_location_jump_clicked": self.on_button_location_jump_clicked,
 
185
                "on_entry_location_key_press_event": self.on_entry_location_key_press_event,
 
186
                "on_checkbutton_history_toggled": self.on_checkbutton_history_toggled,
 
187
                "on_entry_history_revno_key_press_event": self.on_entry_history_revno_key_press_event,
 
188
                "on_button_history_browse_clicked": self.on_button_history_browse_clicked
 
189
            }
 
190
        
 
191
        # Connect the signals to the handlers
 
192
        self.toplevel.signal_autoconnect(dic)
 
193
        
 
194
        self._just_started = True
 
195
        
 
196
        # Apply window size and position
 
197
        width = self.pref.get_preference('window_width', 'int')
 
198
        height = self.pref.get_preference('window_height', 'int')
 
199
        self.window.resize(width, height)
 
200
        x = self.pref.get_preference('window_x', 'int')
 
201
        y = self.pref.get_preference('window_y', 'int')
 
202
        self.window.move(x, y)
 
203
        # Apply paned position
 
204
        pos = self.pref.get_preference('paned_position', 'int')
 
205
        self.hpaned_main.set_position(pos)
 
206
        
 
207
        # Apply menu to the toolbutton
 
208
        #menubutton = self.toplevel.get_widget('menutoolbutton_diff')
 
209
        #menubutton.set_menu(handler.menu.toolbar_diff)
 
210
        
 
211
        # Now we can show the window
 
212
        self.window.show()
 
213
        
 
214
        # Show drive selector if under Win32
 
215
        if sys.platform == 'win32':
 
216
            self.hbox_location.pack_start(self.combobox_drive, False, False, 0)
 
217
            self.hbox_location.reorder_child(self.combobox_drive, 1)
 
218
            self.combobox_drive.show()
 
219
            self.gen_hard_selector()
 
220
        
 
221
        self._load_left()
 
222
 
 
223
        # Apply menu state
 
224
        self.menuitem_view_show_hidden_files.set_active(self.pref.get_preference('dotted_files', 'bool'))
 
225
 
 
226
        # We're starting local
 
227
        self.remote = False
 
228
        self.remote_branch = None
 
229
        self.remote_path = None
 
230
        self.remote_revision = None
 
231
        
 
232
        self.set_path(os.getcwd())
 
233
        self._load_right()
 
234
        
 
235
        self._just_started = False
 
236
 
 
237
    def set_path(self, path, force_remote=False):
 
238
        self.notbranch = False
 
239
        
 
240
        if force_remote:
 
241
            # Forcing remote mode (reading data from inventory)
 
242
            self._show_stock_image(gtk.STOCK_DISCONNECT)
 
243
            try:
 
244
                br = Branch.open_containing(path)[0]
 
245
            except bzrerrors.NotBranchError:
 
246
                self._show_stock_image(gtk.STOCK_DIALOG_ERROR)
 
247
                self.check_history.set_active(False)
 
248
                self.check_history.set_sensitive(False)
 
249
                return False
 
250
            except bzrerrors.UnsupportedProtocol:
 
251
                self._show_stock_image(gtk.STOCK_DIALOG_ERROR)
 
252
                self.check_history.set_active(False)
 
253
                self.check_history.set_sensitive(False)
 
254
                return False
 
255
            
 
256
            self._show_stock_image(gtk.STOCK_CONNECT)
 
257
            
 
258
            self.remote = True
 
259
           
 
260
            # We're remote
 
261
            self.remote_branch, self.remote_path = Branch.open_containing(path)
 
262
            
 
263
            if self.remote_revision is None:
 
264
                self.remote_revision = self.remote_branch.last_revision()
 
265
            
 
266
            self.remote_entries = self.remote_branch.repository.get_inventory(self.remote_revision).entries()
 
267
            
 
268
            if len(self.remote_path) == 0:
 
269
                self.remote_parent = self.remote_branch.repository.get_inventory(self.remote_branch.last_revision()).iter_entries_by_dir().next()[1].file_id
 
270
            else:
 
271
                for (name, type) in self.remote_entries:
 
272
                    if name == self.remote_path:
 
273
                        self.remote_parent = type.file_id
 
274
                        break
 
275
            
 
276
            if not path.endswith('/'):
 
277
                path += '/'
 
278
            
 
279
            if self.remote_branch.base == path:
 
280
                self.button_location_up.set_sensitive(False)
 
281
            else:
 
282
                self.button_location_up.set_sensitive(True)
 
283
        else:
 
284
            if os.path.isdir(path):
 
285
                self.image_location_error.destroy()
 
286
                self.remote = False
 
287
                
 
288
                # We're local
 
289
                try:
 
290
                    self.wt, self.wtpath = WorkingTree.open_containing(path)
 
291
                except (bzrerrors.NotBranchError, bzrerrors.NoWorkingTree):
 
292
                    self.notbranch = True
 
293
                
 
294
                # If we're in the root, we cannot go up anymore
 
295
                if sys.platform == 'win32':
 
296
                    drive, tail = os.path.splitdrive(path)
 
297
                    if tail in ('', '/', '\\'):
 
298
                        self.button_location_up.set_sensitive(False)
 
299
                    else:
 
300
                        self.button_location_up.set_sensitive(True)
 
301
                else:
 
302
                    if self.path == '/':
 
303
                        self.button_location_up.set_sensitive(False)
 
304
                    else:
 
305
                        self.button_location_up.set_sensitive(True)
 
306
            elif not os.path.isfile(path):
 
307
                # Doesn't seem to be a file nor a directory, trying to open a
 
308
                # remote location
 
309
                self._show_stock_image(gtk.STOCK_DISCONNECT)
 
310
                try:
 
311
                    br = Branch.open_containing(path)[0]
 
312
                except bzrerrors.NotBranchError:
 
313
                    self._show_stock_image(gtk.STOCK_DIALOG_ERROR)
 
314
                    self.check_history.set_active(False)
 
315
                    self.check_history.set_sensitive(False)
 
316
                    return False
 
317
                except bzrerrors.UnsupportedProtocol:
 
318
                    self._show_stock_image(gtk.STOCK_DIALOG_ERROR)
 
319
                    self.check_history.set_active(False)
 
320
                    self.check_history.set_sensitive(False)
 
321
                    return False
 
322
                
 
323
                self._show_stock_image(gtk.STOCK_CONNECT)
 
324
                
 
325
                self.remote = True
 
326
               
 
327
                # We're remote
 
328
                self.remote_branch, self.remote_path = Branch.open_containing(path)
 
329
                
 
330
                if self.remote_revision is None:
 
331
                    self.remote_revision = self.remote_branch.last_revision()
 
332
                
 
333
                self.remote_entries = self.remote_branch.repository.get_inventory(self.remote_revision).entries()
 
334
                
 
335
                if len(self.remote_path) == 0:
 
336
                    self.remote_parent = self.remote_branch.repository.get_inventory(self.remote_branch.last_revision()).iter_entries_by_dir().next()[1].file_id
 
337
                else:
 
338
                    for (name, type) in self.remote_entries:
 
339
                        if name == self.remote_path:
 
340
                            self.remote_parent = type.file_id
 
341
                            break
 
342
                
 
343
                if not path.endswith('/'):
 
344
                    path += '/'
 
345
                
 
346
                if self.remote_branch.base == path:
 
347
                    self.button_location_up.set_sensitive(False)
 
348
                else:
 
349
                    self.button_location_up.set_sensitive(True)
 
350
        
 
351
        if self.notbranch:
 
352
            self.check_history.set_active(False)
 
353
            self.check_history.set_sensitive(False)
 
354
        else:
 
355
            self.check_history.set_sensitive(True)
 
356
        
 
357
        self.statusbar.push(self.context_id, path)
 
358
        self.entry_location.set_text(path)
 
359
        self.path = path
 
360
        return True
 
361
 
 
362
    def get_path(self):
 
363
        if not self.remote:
 
364
            return self.path
 
365
        else:
 
366
            # Remote mode
 
367
            if len(self.remote_path) > 0:
 
368
                return self.remote_branch.base + self.remote_path + '/'
 
369
            else:
 
370
                return self.remote_branch.base
 
371
   
 
372
    def on_about_activate(self, widget):
 
373
        about()
 
374
    
 
375
    def on_button_history_browse_clicked(self, widget):
 
376
        """ Browse for revision button handler. """
 
377
        if self.remote:
 
378
            br = self.remote_branch
 
379
        else:
 
380
            br = self.wt.branch
 
381
            
 
382
        revb = RevisionBrowser(br, self.window)
 
383
        response = revb.run()
 
384
        if response != gtk.RESPONSE_NONE:
 
385
            revb.hide()
 
386
        
 
387
            if response == gtk.RESPONSE_OK:
 
388
                if revb.selected_revno is not None:
 
389
                    self.entry_history.set_text(revb.selected_revno)
 
390
            
 
391
            revb.destroy()
 
392
    
 
393
    def on_button_location_jump_clicked(self, widget):
 
394
        """ Location Jump button handler. """
 
395
        location = self.entry_location.get_text()
 
396
        
 
397
        if self.set_path(location):
 
398
            self.refresh_right()
 
399
    
 
400
    def on_button_location_up_clicked(self, widget):
 
401
        """ Location Up button handler. """
 
402
        if not self.remote:
 
403
            # Local mode
 
404
            self.set_path(os.path.split(self.get_path())[0])
 
405
        else:
 
406
            # Remote mode
 
407
            delim = '/'
 
408
            newpath = delim.join(self.get_path().split(delim)[:-2])
 
409
            newpath += '/'
 
410
            self.set_path(newpath)
 
411
 
 
412
        self.refresh_right()
 
413
    
 
414
    def on_checkbutton_history_toggled(self, widget):
 
415
        """ History Mode toggle handler. """
 
416
        if self.check_history.get_active():
 
417
            # History Mode activated
 
418
            self.entry_history.set_sensitive(True)
 
419
            self.button_history.set_sensitive(True)
 
420
        else:
 
421
            # History Mode deactivated
 
422
            self.entry_history.set_sensitive(False)
 
423
            self.button_history.set_sensitive(False)
 
424
    
 
425
    @show_bzr_error
 
426
    def on_entry_history_revno_key_press_event(self, widget, event):
 
427
        """ Key pressed handler for the history entry. """
 
428
        if event.keyval == 65293:
 
429
            # Return was hit, so we have to load that specific revision
 
430
            # Emulate being remote, so inventory should be used
 
431
            path = self.get_path()
 
432
            if not self.remote:
 
433
                self.remote = True
 
434
                self.remote_branch = self.wt.branch
 
435
            
 
436
            revno = int(self.entry_history.get_text())
 
437
            self.remote_revision = self.remote_branch.get_rev_id(revno)
 
438
            if self.set_path(path, True):
 
439
                self.refresh_right()
 
440
    
 
441
    def on_entry_location_key_press_event(self, widget, event):
 
442
        """ Key pressed handler for the location entry. """
 
443
        if event.keyval == 65293:
 
444
            # Return was hit, so we have to jump
 
445
            self.on_button_location_jump_clicked(widget)
 
446
    
 
447
    def on_menuitem_add_files_activate(self, widget):
 
448
        """ Add file(s)... menu handler. """
 
449
        from add import OliveAdd
 
450
        add = OliveAdd(self.wt, self.wtpath, self.get_selected_right())
 
451
        add.display()
 
452
    
 
453
    def on_menuitem_branch_get_activate(self, widget):
 
454
        """ Branch/Get... menu handler. """
 
455
        from bzrlib.plugins.gtk.branch import BranchDialog
 
456
        
 
457
        if self.remote:
 
458
            branch = BranchDialog(os.getcwd(), self.window, self.remote_branch.base)
 
459
        else:
 
460
            branch = BranchDialog(self.get_path(), self.window)
 
461
        response = branch.run()
 
462
        if response != gtk.RESPONSE_NONE:
 
463
            branch.hide()
 
464
            
 
465
            if response == gtk.RESPONSE_OK:
 
466
                self.refresh_right()
 
467
            
 
468
            branch.destroy()
 
469
    
 
470
    def on_menuitem_branch_checkout_activate(self, widget):
 
471
        """ Branch/Checkout... menu handler. """
 
472
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
473
        
 
474
        if self.remote:
 
475
            checkout = CheckoutDialog(os.getcwd(), self.window, self.remote_branch.base)
 
476
        else:
 
477
            checkout = CheckoutDialog(self.get_path(), self.window)
 
478
        response = checkout.run()
 
479
        if response != gtk.RESPONSE_NONE:
 
480
            checkout.hide()
 
481
        
 
482
            if response == gtk.RESPONSE_OK:
 
483
                self.refresh_right()
 
484
            
 
485
            checkout.destroy()
 
486
    
 
487
    @show_bzr_error
 
488
    def on_menuitem_branch_commit_activate(self, widget):
 
489
        """ Branch/Commit... menu handler. """
 
490
        commit = CommitDialog(self.wt, self.wtpath, self.notbranch, self.get_selected_right(), self.window)
 
491
        response = commit.run()
 
492
        if response != gtk.RESPONSE_NONE:
 
493
            commit.hide()
 
494
        
 
495
            if response == gtk.RESPONSE_OK:
 
496
                self.refresh_right()
 
497
            
 
498
            commit.destroy()
 
499
    
 
500
    def on_menuitem_branch_conflicts_activate(self, widget):
 
501
        """ Branch/Conflicts... menu handler. """
 
502
        conflicts = ConflictsDialog(self.wt, self.window)
 
503
        response = conflicts.run()
 
504
        if response != gtk.RESPONSE_NONE:
 
505
            conflicts.destroy()
 
506
    
 
507
    def on_menuitem_branch_merge_activate(self, widget):
 
508
        """ Branch/Merge... menu handler. """
 
509
        from bzrlib.plugins.gtk.merge import MergeDialog
 
510
        
 
511
        if self.check_for_changes():
 
512
            error_dialog(_('There are local changes in the branch'),
 
513
                         _('Please commit or revert the changes before merging.'))
 
514
        else:
 
515
            merge = MergeDialog(self.wt, self.wtpath)
 
516
            merge.display()
 
517
 
 
518
    @show_bzr_error
 
519
    def on_menuitem_branch_missing_revisions_activate(self, widget):
 
520
        """ Branch/Missing revisions menu handler. """
 
521
        local_branch = self.wt.branch
 
522
        
 
523
        other_branch = local_branch.get_parent()
 
524
        if other_branch is None:
 
525
            error_dialog(_('Parent location is unknown'),
 
526
                         _('Cannot determine missing revisions if no parent location is known.'))
 
527
            return
 
528
        
 
529
        remote_branch = Branch.open(other_branch)
 
530
        
 
531
        if remote_branch.base == local_branch.base:
 
532
            remote_branch = local_branch
 
533
 
 
534
        ret = len(local_branch.missing_revisions(remote_branch))
 
535
 
 
536
        if ret > 0:
 
537
            info_dialog(_('There are missing revisions'),
 
538
                        _('%d revision(s) missing.') % ret)
 
539
        else:
 
540
            info_dialog(_('Local branch up to date'),
 
541
                        _('There are no missing revisions.'))
 
542
 
 
543
    @show_bzr_error
 
544
    def on_menuitem_branch_pull_activate(self, widget):
 
545
        """ Branch/Pull menu handler. """
 
546
        branch_to = self.wt.branch
 
547
 
 
548
        location = branch_to.get_parent()
 
549
        if location is None:
 
550
            error_dialog(_('Parent location is unknown'),
 
551
                                     _('Pulling is not possible until there is a parent location.'))
 
552
            return
 
553
 
 
554
        branch_from = Branch.open(location)
 
555
 
 
556
        if branch_to.get_parent() is None:
 
557
            branch_to.set_parent(branch_from.base)
 
558
 
 
559
        ret = branch_to.pull(branch_from)
 
560
        
 
561
        info_dialog(_('Pull successful'), _('%d revision(s) pulled.') % ret)
 
562
    
 
563
    def on_menuitem_branch_push_activate(self, widget):
 
564
        """ Branch/Push... menu handler. """
 
565
        push = PushDialog(self.wt.branch, self.window)
 
566
        response = push.run()
 
567
        if response != gtk.RESPONSE_NONE:
 
568
            push.destroy()
 
569
    
 
570
    @show_bzr_error
 
571
    def on_menuitem_branch_revert_activate(self, widget):
 
572
        """ Branch/Revert all changes menu handler. """
 
573
        ret = self.wt.revert([])
 
574
        if ret:
 
575
            warning_dialog(_('Conflicts detected'),
 
576
                           _('Please have a look at the working tree before continuing.'))
 
577
        else:
 
578
            info_dialog(_('Revert successful'),
 
579
                        _('All files reverted to last revision.'))
 
580
        self.refresh_right()
 
581
    
 
582
    def on_menuitem_branch_status_activate(self, widget):
 
583
        """ Branch/Status... menu handler. """
 
584
        from bzrlib.plugins.gtk.status import StatusDialog
 
585
        status = StatusDialog(self.wt, self.wtpath)
 
586
        response = status.run()
 
587
        if response != gtk.RESPONSE_NONE:
 
588
            status.destroy()
 
589
    
 
590
    def on_menuitem_branch_initialize_activate(self, widget):
 
591
        """ Initialize current directory. """
 
592
        init = InitDialog(self.path, self.window)
 
593
        response = init.run()
 
594
        if response != gtk.RESPONSE_NONE:
 
595
            init.hide()
 
596
        
 
597
            if response == gtk.RESPONSE_OK:
 
598
                self.refresh_right()
 
599
            
 
600
            init.destroy()
 
601
        
 
602
    def on_menuitem_branch_tags_activate(self, widget):
 
603
        """ Branch/Tags... menu handler. """
 
604
        from bzrlib.plugins.gtk.tags import TagsWindow
 
605
        if not self.remote:
 
606
            window = TagsWindow(self.wt.branch, self.window)
 
607
        else:
 
608
            window = TagsWindow(self.remote_branch, self.window)
 
609
        window.show()
 
610
    
 
611
    def on_menuitem_file_annotate_activate(self, widget):
 
612
        """ File/Annotate... menu handler. """
 
613
        if self.get_selected_right() is None:
 
614
            error_dialog(_('No file was selected'),
 
615
                         _('Please select a file from the list.'))
 
616
            return
 
617
        
 
618
        branch = self.wt.branch
 
619
        file_id = self.wt.path2id(self.wt.relpath(os.path.join(self.path, self.get_selected_right())))
 
620
        
 
621
        window = GAnnotateWindow(all=False, plain=False)
 
622
        window.set_title(os.path.join(self.path, self.get_selected_right()) + " - Annotate")
 
623
        config = GAnnotateConfig(window)
 
624
        window.show()
 
625
        branch.lock_read()
 
626
        try:
 
627
            window.annotate(self.wt, branch, file_id)
 
628
        finally:
 
629
            branch.unlock()
 
630
    
 
631
    def on_menuitem_file_make_directory_activate(self, widget):
 
632
        """ File/Make directory... menu handler. """
 
633
        from mkdir import OliveMkdir
 
634
        mkdir = OliveMkdir(self.wt, self.wtpath)
 
635
        mkdir.display()
 
636
    
 
637
    def on_menuitem_file_move_activate(self, widget):
 
638
        """ File/Move... menu handler. """
 
639
        from move import OliveMove
 
640
        move = OliveMove(self.wt, self.wtpath, self.get_selected_right())
 
641
        move.display()
 
642
    
 
643
    def on_menuitem_file_rename_activate(self, widget):
 
644
        """ File/Rename... menu handler. """
 
645
        from rename import OliveRename
 
646
        rename = OliveRename(self.wt, self.wtpath, self.get_selected_right())
 
647
        rename.display()
 
648
 
 
649
    def on_menuitem_remove_file_activate(self, widget):
 
650
        """ Remove (unversion) selected file. """
 
651
        from remove import OliveRemoveDialog
 
652
        remove = OliveRemoveDialog(self.wt, self.wtpath,
 
653
                                   selected=self.get_selected_right(),
 
654
                                   parent=self.window)
 
655
        response = remove.run()
 
656
        
 
657
        if response != gtk.RESPONSE_NONE:
 
658
            remove.hide()
 
659
        
 
660
            if response == gtk.RESPONSE_OK:
 
661
                self.set_path(self.path)
 
662
                self.refresh_right()
 
663
            
 
664
            remove.destroy()
 
665
    
 
666
    def on_menuitem_stats_diff_activate(self, widget):
 
667
        """ Statistics/Differences... menu handler. """
 
668
        window = DiffWindow()
 
669
        parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
670
        window.set_diff(self.wt.branch.nick, self.wt, parent_tree)
 
671
        window.show()
 
672
    
 
673
    def on_menuitem_stats_infos_activate(self, widget):
 
674
        """ Statistics/Informations... menu handler. """
 
675
        from info import OliveInfo
 
676
        if self.remote:
 
677
            info = OliveInfo(self.remote_branch)
 
678
        else:
 
679
            info = OliveInfo(self.wt.branch)
 
680
        info.display()
 
681
    
 
682
    def on_menuitem_stats_log_activate(self, widget):
 
683
        """ Statistics/Log... menu handler. """
 
684
        window = branchwin.BranchWindow()
 
685
        if not self.remote:
 
686
            window.set_branch(self.wt.branch, self.wt.branch.last_revision(), None)
 
687
        else:
 
688
            window.set_branch(self.remote_branch, self.remote_branch.last_revision(), None)
 
689
        window.show()
 
690
    
 
691
    def on_menuitem_view_refresh_activate(self, widget):
 
692
        """ View/Refresh menu handler. """
 
693
        # Refresh the left pane
 
694
        self.refresh_left()
 
695
        # Refresh the right pane
 
696
        self.refresh_right()
 
697
   
 
698
    def on_menuitem_view_show_hidden_files_activate(self, widget):
 
699
        """ View/Show hidden files menu handler. """
 
700
        self.pref.set_preference('dotted_files', widget.get_active())
 
701
        if self.path is not None:
 
702
            self.refresh_right()
 
703
 
 
704
    def on_treeview_left_button_press_event(self, widget, event):
 
705
        """ Occurs when somebody right-clicks in the bookmark list. """
 
706
        if event.button == 3:
 
707
            # Don't show context with nothing selected
 
708
            if self.get_selected_left() == None:
 
709
                return
 
710
 
 
711
            # Create a menu
 
712
            from menu import OliveMenu
 
713
            menu = OliveMenu(path=self.get_path(),
 
714
                             selected=self.get_selected_left(),
 
715
                             app=self)
 
716
            
 
717
            menu.left_context_menu().popup(None, None, None, 0,
 
718
                                           event.time)
 
719
 
 
720
    def on_treeview_left_row_activated(self, treeview, path, view_column):
 
721
        """ Occurs when somebody double-clicks or enters an item in the
 
722
        bookmark list. """
 
723
 
 
724
        newdir = self.get_selected_left()
 
725
        if newdir == None:
 
726
            return
 
727
 
 
728
        if self.set_path(newdir):
 
729
            self.refresh_right()
 
730
 
 
731
    def on_treeview_right_button_press_event(self, widget, event):
 
732
        """ Occurs when somebody right-clicks in the file list. """
 
733
        if event.button == 3:
 
734
            # Create a menu
 
735
            from menu import OliveMenu
 
736
            menu = OliveMenu(path=self.get_path(),
 
737
                             selected=self.get_selected_right(),
 
738
                             app=self)
 
739
            # get the menu items
 
740
            m_open = menu.ui.get_widget('/context_right/open')
 
741
            m_add = menu.ui.get_widget('/context_right/add')
 
742
            m_remove = menu.ui.get_widget('/context_right/remove')
 
743
            m_rename = menu.ui.get_widget('/context_right/rename')
 
744
            m_revert = menu.ui.get_widget('/context_right/revert')
 
745
            m_commit = menu.ui.get_widget('/context_right/commit')
 
746
            m_annotate = menu.ui.get_widget('/context_right/annotate')
 
747
            m_diff = menu.ui.get_widget('/context_right/diff')
 
748
            # check if we're in a branch
 
749
            try:
 
750
                from bzrlib.branch import Branch
 
751
                Branch.open_containing(self.get_path())
 
752
                if self.remote:
 
753
                    m_open.set_sensitive(False)
 
754
                    m_add.set_sensitive(False)
 
755
                    m_remove.set_sensitive(False)
 
756
                    m_rename.set_sensitive(False)
 
757
                    m_revert.set_sensitive(False)
 
758
                    m_commit.set_sensitive(False)
 
759
                    m_annotate.set_sensitive(False)
 
760
                    m_diff.set_sensitive(False)
 
761
                else:
 
762
                    m_open.set_sensitive(True)
 
763
                    m_add.set_sensitive(True)
 
764
                    m_remove.set_sensitive(True)
 
765
                    m_rename.set_sensitive(True)
 
766
                    m_revert.set_sensitive(True)
 
767
                    m_commit.set_sensitive(True)
 
768
                    m_annotate.set_sensitive(True)
 
769
                    m_diff.set_sensitive(True)
 
770
            except bzrerrors.NotBranchError:
 
771
                m_open.set_sensitive(True)
 
772
                m_add.set_sensitive(False)
 
773
                m_remove.set_sensitive(False)
 
774
                m_rename.set_sensitive(False)
 
775
                m_revert.set_sensitive(False)
 
776
                m_commit.set_sensitive(False)
 
777
                m_annotate.set_sensitive(False)
 
778
                m_diff.set_sensitive(False)
 
779
 
 
780
            if not self.remote:
 
781
                menu.right_context_menu().popup(None, None, None, 0,
 
782
                                                event.time)
 
783
            else:
 
784
                menu.remote_context_menu().popup(None, None, None, 0,
 
785
                                                 event.time)
 
786
        
 
787
    def on_treeview_right_row_activated(self, treeview, path, view_column):
 
788
        """ Occurs when somebody double-clicks or enters an item in the
 
789
        file list. """
 
790
        from launch import launch
 
791
        
 
792
        newdir = self.get_selected_right()
 
793
        
 
794
        if not self.remote:
 
795
            # We're local
 
796
            if newdir == '..':
 
797
                self.set_path(os.path.split(self.get_path())[0])
 
798
            else:
 
799
                fullpath = os.path.join(self.get_path(), newdir)
 
800
                if os.path.isdir(fullpath):
 
801
                    # selected item is an existant directory
 
802
                    self.set_path(fullpath)
 
803
                else:
 
804
                    launch(fullpath)
 
805
        else:
 
806
            # We're remote
 
807
            if self._is_remote_dir(self.get_path() + newdir):
 
808
                self.set_path(self.get_path() + newdir)
 
809
        
 
810
        self.refresh_right()
 
811
    
 
812
    def on_window_main_delete_event(self, widget, event=None):
 
813
        """ Do some stuff before exiting. """
 
814
        width, height = self.window_main.get_size()
 
815
        self.pref.set_preference('window_width', width)
 
816
        self.pref.set_preference('window_height', height)
 
817
        x, y = self.window_main.get_position()
 
818
        self.pref.set_preference('window_x', x)
 
819
        self.pref.set_preference('window_y', y)
 
820
        self.pref.set_preference('paned_position',
 
821
                                 self.hpaned_main.get_position())
 
822
        
 
823
        self.pref.write()
 
824
        self.window_main.destroy()
 
825
        
 
826
    def _load_left(self):
 
827
        """ Load data into the left panel. (Bookmarks) """
 
828
        # Create TreeStore
 
829
        treestore = gtk.TreeStore(str, str)
 
830
        
 
831
        # Get bookmarks
 
832
        bookmarks = self.pref.get_bookmarks()
 
833
        
 
834
        # Add them to the TreeStore
 
835
        titer = treestore.append(None, [_('Bookmarks'), None])
 
836
        for item in bookmarks:
 
837
            title = self.pref.get_bookmark_title(item)
 
838
            treestore.append(titer, [title, item])
 
839
        
 
840
        # Create the column and add it to the TreeView
 
841
        self.treeview_left.set_model(treestore)
 
842
        tvcolumn_bookmark = gtk.TreeViewColumn(_('Bookmark'))
 
843
        self.treeview_left.append_column(tvcolumn_bookmark)
 
844
        
 
845
        # Set up the cells
 
846
        cell = gtk.CellRendererText()
 
847
        tvcolumn_bookmark.pack_start(cell, True)
 
848
        tvcolumn_bookmark.add_attribute(cell, 'text', 0)
 
849
        
 
850
        # Expand the tree
 
851
        self.treeview_left.expand_all()
 
852
 
 
853
    def _load_right(self):
 
854
        """ Load data into the right panel. (Filelist) """
 
855
        # Create ListStore
 
856
        # Model: [ icon, dir, name, status text, status, size (int), size (human), mtime (int), mtime (local), fileid]
 
857
        liststore = gtk.ListStore(gobject.TYPE_STRING,
 
858
                                  gobject.TYPE_BOOLEAN,
 
859
                                  gobject.TYPE_STRING,
 
860
                                  gobject.TYPE_STRING,
 
861
                                  gobject.TYPE_STRING,
 
862
                                  gobject.TYPE_INT,
 
863
                                  gobject.TYPE_STRING,
 
864
                                  gobject.TYPE_INT,
 
865
                                  gobject.TYPE_STRING,
 
866
                                  gobject.TYPE_STRING)
 
867
        
 
868
        dirs = []
 
869
        files = []
 
870
        
 
871
        # Fill the appropriate lists
 
872
        dotted_files = self.pref.get_preference('dotted_files', 'bool')
 
873
        for item in os.listdir(self.path):
 
874
            if not dotted_files and item[0] == '.':
 
875
                continue
 
876
            if os.path.isdir(self.path + os.sep + item):
 
877
                dirs.append(item)
 
878
            else:
 
879
                files.append(item)
 
880
        
 
881
        if not self.notbranch:
 
882
            branch = self.wt.branch
 
883
            tree2 = self.wt.branch.repository.revision_tree(branch.last_revision())
 
884
        
 
885
            delta = self.wt.changes_from(tree2, want_unchanged=True)
 
886
        
 
887
        # Add'em to the ListStore
 
888
        for item in dirs:
 
889
            statinfo = os.stat(self.path + os.sep + item)
 
890
            liststore.append([ gtk.STOCK_DIRECTORY,
 
891
                               True,
 
892
                               item,
 
893
                               '',
 
894
                               '',
 
895
                               statinfo.st_size,
 
896
                               self._format_size(statinfo.st_size),
 
897
                               statinfo.st_mtime,
 
898
                               self._format_date(statinfo.st_mtime),
 
899
                               ''])
 
900
        for item in files:
 
901
            status = 'unknown'
 
902
            fileid = ''
 
903
            if not self.notbranch:
 
904
                filename = self.wt.relpath(self.path + os.sep + item)
 
905
                
 
906
                try:
 
907
                    self.wt.lock_read()
 
908
                    
 
909
                    for rpath, rpathnew, id, kind, text_modified, meta_modified in delta.renamed:
 
910
                        if rpathnew == filename:
 
911
                            status = 'renamed'
 
912
                            fileid = id
 
913
                    for rpath, id, kind in delta.added:
 
914
                        if rpath == filename:
 
915
                            status = 'added'
 
916
                            fileid = id
 
917
                    for rpath, id, kind in delta.removed:
 
918
                        if rpath == filename:
 
919
                            status = 'removed'
 
920
                            fileid = id
 
921
                    for rpath, id, kind, text_modified, meta_modified in delta.modified:
 
922
                        if rpath == filename:
 
923
                            status = 'modified'
 
924
                            fileid = id
 
925
                    for rpath, id, kind in delta.unchanged:
 
926
                        if rpath == filename:
 
927
                            status = 'unchanged'
 
928
                            fileid = id
 
929
                    for rpath, file_class, kind, id, entry in self.wt.list_files():
 
930
                        if rpath == filename and file_class == 'I':
 
931
                            status = 'ignored'
 
932
                finally:
 
933
                    self.wt.unlock()
 
934
            
 
935
            if status == 'renamed':
 
936
                st = _('renamed')
 
937
            elif status == 'removed':
 
938
                st = _('removed')
 
939
            elif status == 'added':
 
940
                st = _('added')
 
941
            elif status == 'modified':
 
942
                st = _('modified')
 
943
            elif status == 'unchanged':
 
944
                st = _('unchanged')
 
945
            elif status == 'ignored':
 
946
                st = _('ignored')
 
947
            else:
 
948
                st = _('unknown')
 
949
            
 
950
            statinfo = os.stat(self.path + os.sep + item)
 
951
            liststore.append([gtk.STOCK_FILE,
 
952
                              False,
 
953
                              item,
 
954
                              st,
 
955
                              status,
 
956
                              statinfo.st_size,
 
957
                              self._format_size(statinfo.st_size),
 
958
                              statinfo.st_mtime,
 
959
                              self._format_date(statinfo.st_mtime),
 
960
                              fileid])
 
961
        
 
962
        # Create the columns and add them to the TreeView
 
963
        self.treeview_right.set_model(liststore)
 
964
        self._tvcolumn_filename = gtk.TreeViewColumn(_('Filename'))
 
965
        self._tvcolumn_status = gtk.TreeViewColumn(_('Status'))
 
966
        self._tvcolumn_size = gtk.TreeViewColumn(_('Size'))
 
967
        self._tvcolumn_mtime = gtk.TreeViewColumn(_('Last modified'))
 
968
        self.treeview_right.append_column(self._tvcolumn_filename)
 
969
        self.treeview_right.append_column(self._tvcolumn_status)
 
970
        self.treeview_right.append_column(self._tvcolumn_size)
 
971
        self.treeview_right.append_column(self._tvcolumn_mtime)
 
972
        
 
973
        # Set up the cells
 
974
        cellpb = gtk.CellRendererPixbuf()
 
975
        cell = gtk.CellRendererText()
 
976
        self._tvcolumn_filename.pack_start(cellpb, False)
 
977
        self._tvcolumn_filename.pack_start(cell, True)
 
978
        self._tvcolumn_filename.set_attributes(cellpb, stock_id=0)
 
979
        self._tvcolumn_filename.add_attribute(cell, 'text', 2)
 
980
        self._tvcolumn_status.pack_start(cell, True)
 
981
        self._tvcolumn_status.add_attribute(cell, 'text', 3)
 
982
        self._tvcolumn_size.pack_start(cell, True)
 
983
        self._tvcolumn_size.add_attribute(cell, 'text', 6)
 
984
        self._tvcolumn_mtime.pack_start(cell, True)
 
985
        self._tvcolumn_mtime.add_attribute(cell, 'text', 8)
 
986
        
 
987
        # Set up the properties of the TreeView
 
988
        self.treeview_right.set_headers_visible(True)
 
989
        self.treeview_right.set_headers_clickable(True)
 
990
        self.treeview_right.set_search_column(1)
 
991
        self._tvcolumn_filename.set_resizable(True)
 
992
        self._tvcolumn_status.set_resizable(True)
 
993
        self._tvcolumn_size.set_resizable(True)
 
994
        self._tvcolumn_mtime.set_resizable(True)
 
995
        # Set up sorting
 
996
        liststore.set_sort_func(13, self._sort_filelist_callback, None)
 
997
        liststore.set_sort_column_id(13, gtk.SORT_ASCENDING)
 
998
        self._tvcolumn_filename.set_sort_column_id(13)
 
999
        self._tvcolumn_status.set_sort_column_id(3)
 
1000
        self._tvcolumn_size.set_sort_column_id(5)
 
1001
        self._tvcolumn_mtime.set_sort_column_id(7)
 
1002
        
 
1003
        # Set sensitivity
 
1004
        self.set_sensitivity()
 
1005
        
 
1006
    def get_selected_fileid(self):
 
1007
        """ Get the file_id of the selected file. """
 
1008
        treeselection = self.treeview_right.get_selection()
 
1009
        (model, iter) = treeselection.get_selected()
 
1010
        
 
1011
        if iter is None:
 
1012
            return None
 
1013
        else:
 
1014
            return model.get_value(iter, 9)
 
1015
    
 
1016
    def get_selected_right(self):
 
1017
        """ Get the selected filename. """
 
1018
        treeselection = self.treeview_right.get_selection()
 
1019
        (model, iter) = treeselection.get_selected()
 
1020
        
 
1021
        if iter is None:
 
1022
            return None
 
1023
        else:
 
1024
            return model.get_value(iter, 2)
 
1025
    
 
1026
    def get_selected_left(self):
 
1027
        """ Get the selected bookmark. """
 
1028
        treeselection = self.treeview_left.get_selection()
 
1029
        (model, iter) = treeselection.get_selected()
 
1030
        
 
1031
        if iter is None:
 
1032
            return None
 
1033
        else:
 
1034
            return model.get_value(iter, 1)
 
1035
 
 
1036
    def set_statusbar(self, message):
 
1037
        """ Set the statusbar message. """
 
1038
        self.statusbar.push(self.context_id, message)
 
1039
    
 
1040
    def clear_statusbar(self):
 
1041
        """ Clean the last message from the statusbar. """
 
1042
        self.statusbar.pop(self.context_id)
 
1043
    
 
1044
    def set_sensitivity(self):
 
1045
        """ Set menu and toolbar sensitivity. """
 
1046
        if not self.remote:
 
1047
            # We're local
 
1048
            self.menuitem_branch_init.set_sensitive(self.notbranch)
 
1049
            self.menuitem_branch_get.set_sensitive(self.notbranch)
 
1050
            self.menuitem_branch_checkout.set_sensitive(self.notbranch)
 
1051
            self.menuitem_branch_pull.set_sensitive(not self.notbranch)
 
1052
            self.menuitem_branch_push.set_sensitive(not self.notbranch)
 
1053
            self.menuitem_branch_revert.set_sensitive(not self.notbranch)
 
1054
            self.menuitem_branch_merge.set_sensitive(not self.notbranch)
 
1055
            self.menuitem_branch_commit.set_sensitive(not self.notbranch)
 
1056
            self.menuitem_branch_tags.set_sensitive(not self.notbranch)
 
1057
            self.menuitem_branch_status.set_sensitive(not self.notbranch)
 
1058
            self.menuitem_branch_missing.set_sensitive(not self.notbranch)
 
1059
            self.menuitem_branch_conflicts.set_sensitive(not self.notbranch)
 
1060
            self.menuitem_stats.set_sensitive(not self.notbranch)
 
1061
            self.menuitem_stats_diff.set_sensitive(not self.notbranch)
 
1062
            self.menuitem_add_files.set_sensitive(not self.notbranch)
 
1063
            self.menuitem_remove_files.set_sensitive(not self.notbranch)
 
1064
            self.menuitem_file_make_directory.set_sensitive(not self.notbranch)
 
1065
            self.menuitem_file_rename.set_sensitive(not self.notbranch)
 
1066
            self.menuitem_file_move.set_sensitive(not self.notbranch)
 
1067
            self.menuitem_file_annotate.set_sensitive(not self.notbranch)
 
1068
            #self.menutoolbutton_diff.set_sensitive(True)
 
1069
            self.toolbutton_diff.set_sensitive(not self.notbranch)
 
1070
            self.toolbutton_log.set_sensitive(not self.notbranch)
 
1071
            self.toolbutton_commit.set_sensitive(not self.notbranch)
 
1072
            self.toolbutton_pull.set_sensitive(not self.notbranch)
 
1073
            self.toolbutton_push.set_sensitive(not self.notbranch)
 
1074
        else:
 
1075
            # We're remote
 
1076
            self.menuitem_branch_init.set_sensitive(False)
 
1077
            self.menuitem_branch_get.set_sensitive(True)
 
1078
            self.menuitem_branch_checkout.set_sensitive(True)
 
1079
            self.menuitem_branch_pull.set_sensitive(False)
 
1080
            self.menuitem_branch_push.set_sensitive(False)
 
1081
            self.menuitem_branch_revert.set_sensitive(False)
 
1082
            self.menuitem_branch_merge.set_sensitive(False)
 
1083
            self.menuitem_branch_commit.set_sensitive(False)
 
1084
            self.menuitem_branch_tags.set_sensitive(True)
 
1085
            self.menuitem_branch_status.set_sensitive(False)
 
1086
            self.menuitem_branch_missing.set_sensitive(False)
 
1087
            self.menuitem_branch_conflicts.set_sensitive(False)
 
1088
            self.menuitem_stats.set_sensitive(True)
 
1089
            self.menuitem_stats_diff.set_sensitive(False)
 
1090
            self.menuitem_add_files.set_sensitive(False)
 
1091
            self.menuitem_remove_files.set_sensitive(False)
 
1092
            self.menuitem_file_make_directory.set_sensitive(False)
 
1093
            self.menuitem_file_rename.set_sensitive(False)
 
1094
            self.menuitem_file_move.set_sensitive(False)
 
1095
            self.menuitem_file_annotate.set_sensitive(False)
 
1096
            #self.menutoolbutton_diff.set_sensitive(True)
 
1097
            self.toolbutton_diff.set_sensitive(False)
 
1098
            self.toolbutton_log.set_sensitive(True)
 
1099
            self.toolbutton_commit.set_sensitive(False)
 
1100
            self.toolbutton_pull.set_sensitive(False)
 
1101
            self.toolbutton_push.set_sensitive(False)
 
1102
    
 
1103
    def refresh_left(self):
 
1104
        """ Refresh the bookmark list. """
 
1105
        
 
1106
        # Get TreeStore and clear it
 
1107
        treestore = self.treeview_left.get_model()
 
1108
        treestore.clear()
 
1109
 
 
1110
        # Re-read preferences
 
1111
        self.pref.read()
 
1112
        
 
1113
        # Get bookmarks
 
1114
        bookmarks = self.pref.get_bookmarks()
 
1115
 
 
1116
        # Add them to the TreeStore
 
1117
        titer = treestore.append(None, [_('Bookmarks'), None])
 
1118
        for item in bookmarks:
 
1119
            title = self.pref.get_bookmark_title(item)
 
1120
            treestore.append(titer, [title, item])
 
1121
 
 
1122
        # Add the TreeStore to the TreeView
 
1123
        self.treeview_left.set_model(treestore)
 
1124
 
 
1125
        # Expand the tree
 
1126
        self.treeview_left.expand_all()
 
1127
 
 
1128
    def refresh_right(self, path=None):
 
1129
        """ Refresh the file list. """
 
1130
        if not self.remote:
 
1131
            # We're local
 
1132
            from bzrlib.workingtree import WorkingTree
 
1133
    
 
1134
            if path is None:
 
1135
                path = self.get_path()
 
1136
    
 
1137
            # A workaround for double-clicking Bookmarks
 
1138
            if not os.path.exists(path):
 
1139
                return
 
1140
    
 
1141
            # Get ListStore and clear it
 
1142
            liststore = self.treeview_right.get_model()
 
1143
            liststore.clear()
 
1144
            
 
1145
            # Show Status column
 
1146
            self._tvcolumn_status.set_visible(True)
 
1147
    
 
1148
            dirs = []
 
1149
            files = []
 
1150
    
 
1151
            # Fill the appropriate lists
 
1152
            dotted_files = self.pref.get_preference('dotted_files', 'bool')
 
1153
            for item in os.listdir(path):
 
1154
                if not dotted_files and item[0] == '.':
 
1155
                    continue
 
1156
                if os.path.isdir(path + os.sep + item):
 
1157
                    dirs.append(item)
 
1158
                else:
 
1159
                    files.append(item)
 
1160
            
 
1161
            # Try to open the working tree
 
1162
            notbranch = False
 
1163
            try:
 
1164
                tree1 = WorkingTree.open_containing(path)[0]
 
1165
            except (bzrerrors.NotBranchError, bzrerrors.NoWorkingTree):
 
1166
                notbranch = True
 
1167
            
 
1168
            if not notbranch:
 
1169
                branch = tree1.branch
 
1170
                tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
 
1171
            
 
1172
                delta = tree1.changes_from(tree2, want_unchanged=True)
 
1173
                
 
1174
            # Add'em to the ListStore
 
1175
            for item in dirs:
 
1176
                statinfo = os.stat(self.path + os.sep + item)
 
1177
                liststore.append([gtk.STOCK_DIRECTORY,
 
1178
                                  True,
 
1179
                                  item,
 
1180
                                  '',
 
1181
                                  '',
 
1182
                                  statinfo.st_size,
 
1183
                                  self._format_size(statinfo.st_size),
 
1184
                                  statinfo.st_mtime,
 
1185
                                  self._format_date(statinfo.st_mtime),
 
1186
                                  ''])
 
1187
            for item in files:
 
1188
                status = 'unknown'
 
1189
                fileid = ''
 
1190
                if not notbranch:
 
1191
                    filename = tree1.relpath(path + os.sep + item)
 
1192
                    
 
1193
                    try:
 
1194
                        self.wt.lock_read()
 
1195
                        
 
1196
                        for rpath, rpathnew, id, kind, text_modified, meta_modified in delta.renamed:
 
1197
                            if rpathnew == filename:
 
1198
                                status = 'renamed'
 
1199
                                fileid = id
 
1200
                        for rpath, id, kind in delta.added:
 
1201
                            if rpath == filename:
 
1202
                                status = 'added'
 
1203
                                fileid = id
 
1204
                        for rpath, id, kind in delta.removed:
 
1205
                            if rpath == filename:
 
1206
                                status = 'removed'
 
1207
                                fileid = id
 
1208
                        for rpath, id, kind, text_modified, meta_modified in delta.modified:
 
1209
                            if rpath == filename:
 
1210
                                status = 'modified'
 
1211
                                fileid = id
 
1212
                        for rpath, id, kind in delta.unchanged:
 
1213
                            if rpath == filename:
 
1214
                                status = 'unchanged'
 
1215
                                fileid = id
 
1216
                        for rpath, file_class, kind, id, entry in self.wt.list_files():
 
1217
                            if rpath == filename and file_class == 'I':
 
1218
                                status = 'ignored'
 
1219
                    finally:
 
1220
                        self.wt.unlock()
 
1221
                
 
1222
                if status == 'renamed':
 
1223
                    st = _('renamed')
 
1224
                elif status == 'removed':
 
1225
                    st = _('removed')
 
1226
                elif status == 'added':
 
1227
                    st = _('added')
 
1228
                elif status == 'modified':
 
1229
                    st = _('modified')
 
1230
                elif status == 'unchanged':
 
1231
                    st = _('unchanged')
 
1232
                elif status == 'ignored':
 
1233
                    st = _('ignored')
 
1234
                else:
 
1235
                    st = _('unknown')
 
1236
                
 
1237
                statinfo = os.stat(self.path + os.sep + item)
 
1238
                liststore.append([gtk.STOCK_FILE,
 
1239
                                  False,
 
1240
                                  item,
 
1241
                                  st,
 
1242
                                  status,
 
1243
                                  statinfo.st_size,
 
1244
                                  self._format_size(statinfo.st_size),
 
1245
                                  statinfo.st_mtime,
 
1246
                                  self._format_date(statinfo.st_mtime),
 
1247
                                  fileid])
 
1248
        else:
 
1249
            # We're remote
 
1250
            
 
1251
            # Get ListStore and clear it
 
1252
            liststore = self.treeview_right.get_model()
 
1253
            liststore.clear()
 
1254
            
 
1255
            # Hide Status column
 
1256
            self._tvcolumn_status.set_visible(False)
 
1257
            
 
1258
            dirs = []
 
1259
            files = []
 
1260
            
 
1261
            self._show_stock_image(gtk.STOCK_REFRESH)
 
1262
            
 
1263
            for (name, type) in self.remote_entries:
 
1264
                if type.kind == 'directory':
 
1265
                    dirs.append(type)
 
1266
                elif type.kind == 'file':
 
1267
                    files.append(type)
 
1268
            
 
1269
            class HistoryCache:
 
1270
                """ Cache based on revision history. """
 
1271
                def __init__(self, history):
 
1272
                    self._history = history
 
1273
                
 
1274
                def _lookup_revision(self, revid):
 
1275
                    for r in self._history:
 
1276
                        if r.revision_id == revid:
 
1277
                            return r
 
1278
                    rev = repo.get_revision(revid)
 
1279
                    self._history.append(rev)
 
1280
                    return rev
 
1281
            
 
1282
            repo = self.remote_branch.repository
 
1283
            
 
1284
            revhistory = self.remote_branch.revision_history()
 
1285
            try:
 
1286
                revs = repo.get_revisions(revhistory)
 
1287
                cache = HistoryCache(revs)
 
1288
            except bzrerrors.InvalidHttpResponse:
 
1289
                # Fallback to dummy algorithm, because of LP: #115209
 
1290
                cache = HistoryCache([])
 
1291
            
 
1292
            for item in dirs:
 
1293
                if item.parent_id == self.remote_parent:
 
1294
                    rev = cache._lookup_revision(item.revision)
 
1295
                    liststore.append([ gtk.STOCK_DIRECTORY,
 
1296
                                       True,
 
1297
                                       item.name,
 
1298
                                       '',
 
1299
                                       '',
 
1300
                                       0,
 
1301
                                       self._format_size(0),
 
1302
                                       rev.timestamp,
 
1303
                                       self._format_date(rev.timestamp),
 
1304
                                       ''
 
1305
                                   ])
 
1306
                while gtk.events_pending():
 
1307
                    gtk.main_iteration()
 
1308
            
 
1309
            for item in files:
 
1310
                if item.parent_id == self.remote_parent:
 
1311
                    rev = cache._lookup_revision(item.revision)
 
1312
                    liststore.append([ gtk.STOCK_FILE,
 
1313
                                       False,
 
1314
                                       item.name,
 
1315
                                       '',
 
1316
                                       '',
 
1317
                                       item.text_size,
 
1318
                                       self._format_size(item.text_size),
 
1319
                                       rev.timestamp,
 
1320
                                       self._format_date(rev.timestamp),
 
1321
                                       item.file_id
 
1322
                                   ])
 
1323
                while gtk.events_pending():
 
1324
                    gtk.main_iteration()
 
1325
            
 
1326
            self.image_location_error.destroy()
 
1327
 
 
1328
        # Columns should auto-size
 
1329
        self.treeview_right.columns_autosize()
 
1330
        
 
1331
        # Set sensitivity
 
1332
        self.set_sensitivity()
 
1333
 
 
1334
    def _harddisks(self):
 
1335
        """ Returns hard drive letters under Win32. """
 
1336
        try:
 
1337
            import win32file
 
1338
            import string
 
1339
        except ImportError:
 
1340
            if sys.platform == 'win32':
 
1341
                print "pyWin32 modules needed to run Olive on Win32."
 
1342
                sys.exit(1)
 
1343
            else:
 
1344
                pass
 
1345
        
 
1346
        driveletters = []
 
1347
        for drive in string.ascii_uppercase:
 
1348
            if win32file.GetDriveType(drive+':') == win32file.DRIVE_FIXED:
 
1349
                driveletters.append(drive+':')
 
1350
        return driveletters
 
1351
    
 
1352
    def gen_hard_selector(self):
 
1353
        """ Generate the hard drive selector under Win32. """
 
1354
        drives = self._harddisks()
 
1355
        for drive in drives:
 
1356
            self.combobox_drive.append_text(drive)
 
1357
        self.combobox_drive.set_active(drives.index(os.getcwd()[0:2]))
 
1358
    
 
1359
    def _refresh_drives(self, combobox):
 
1360
        if self._just_started:
 
1361
            return
 
1362
        model = combobox.get_model()
 
1363
        active = combobox.get_active()
 
1364
        if active >= 0:
 
1365
            drive = model[active][0]
 
1366
            self.set_path(drive + '\\')
 
1367
            self.refresh_right(drive + '\\')
 
1368
    
 
1369
    def check_for_changes(self):
 
1370
        """ Check whether there were changes in the current working tree. """
 
1371
        old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
1372
        delta = self.wt.changes_from(old_tree)
 
1373
 
 
1374
        changes = False
 
1375
        
 
1376
        if len(delta.added) or len(delta.removed) or len(delta.renamed) or len(delta.modified):
 
1377
            changes = True
 
1378
        
 
1379
        return changes
 
1380
    
 
1381
    def _sort_filelist_callback(self, model, iter1, iter2, data):
 
1382
        """ The sort callback for the file list, return values:
 
1383
        -1: iter1 < iter2
 
1384
        0: iter1 = iter2
 
1385
        1: iter1 > iter2
 
1386
        """
 
1387
        name1 = model.get_value(iter1, 2)
 
1388
        name2 = model.get_value(iter2, 2)
 
1389
        
 
1390
        if model.get_value(iter1, 1):
 
1391
            # item1 is a directory
 
1392
            if not model.get_value(iter2, 1):
 
1393
                # item2 isn't
 
1394
                return -1
 
1395
            else:
 
1396
                # both of them are directories, we compare their names
 
1397
                if name1 < name2:
 
1398
                    return -1
 
1399
                elif name1 == name2:
 
1400
                    return 0
 
1401
                else:
 
1402
                    return 1
 
1403
        else:
 
1404
            # item1 is not a directory
 
1405
            if model.get_value(iter2, 1):
 
1406
                # item2 is
 
1407
                return 1
 
1408
            else:
 
1409
                # both of them are files, compare them
 
1410
                if name1 < name2:
 
1411
                    return -1
 
1412
                elif name1 == name2:
 
1413
                    return 0
 
1414
                else:
 
1415
                    return 1
 
1416
    
 
1417
    def _format_size(self, size):
 
1418
        """ Format size to a human readable format. """
 
1419
        return size
 
1420
    
 
1421
    def _format_date(self, timestamp):
 
1422
        """ Format the time (given in secs) to a human readable format. """
 
1423
        return time.ctime(timestamp)
 
1424
    
 
1425
    def _is_remote_dir(self, location):
 
1426
        """ Determine whether the given location is a directory or not. """
 
1427
        if not self.remote:
 
1428
            # We're in local mode
 
1429
            return False
 
1430
        else:
 
1431
            branch, path = Branch.open_containing(location)
 
1432
            for (name, type) in self.remote_entries:
 
1433
                if name == path and type.kind == 'directory':
 
1434
                    # We got it
 
1435
                    return True
 
1436
            # Either it's not a directory or not in the inventory
 
1437
            return False
 
1438
    
 
1439
    def _show_stock_image(self, stock_id):
 
1440
        """ Show a stock image next to the location entry. """
 
1441
        self.image_location_error.destroy()
 
1442
        self.image_location_error = gtk.image_new_from_stock(stock_id, gtk.ICON_SIZE_BUTTON)
 
1443
        self.hbox_location.pack_start(self.image_location_error, False, False, 0)
 
1444
        if sys.platform == 'win32':
 
1445
            self.hbox_location.reorder_child(self.image_location_error, 2)
 
1446
        else:
 
1447
            self.hbox_location.reorder_child(self.image_location_error, 1)
 
1448
        self.image_location_error.show()
 
1449
        while gtk.events_pending():
 
1450
            gtk.main_iteration()
 
1451
 
 
1452
import ConfigParser
 
1453
 
 
1454
class Preferences:
 
1455
    """ A class which handles Olive's preferences. """
 
1456
    def __init__(self, path=None):
 
1457
        """ Initialize the Preferences class. """
 
1458
        # Some default options
 
1459
        self.defaults = { 'strict_commit' : False,
 
1460
                          'dotted_files'  : False,
 
1461
                          'window_width'  : 700,
 
1462
                          'window_height' : 400,
 
1463
                          'window_x'      : 40,
 
1464
                          'window_y'      : 40,
 
1465
                          'paned_position': 200 }
 
1466
 
 
1467
        # Create a config parser object
 
1468
        self.config = ConfigParser.RawConfigParser()
 
1469
 
 
1470
        # Set filename
 
1471
        if path is None:
 
1472
            if sys.platform == 'win32':
 
1473
                # Windows - no dotted files
 
1474
                self._filename = os.path.expanduser('~/olive.conf')
 
1475
            else:
 
1476
                self._filename = os.path.expanduser('~/.olive.conf')
 
1477
        else:
 
1478
            self._filename = path
 
1479
        
 
1480
        # Load the configuration
 
1481
        self.read()
 
1482
        
 
1483
    def _get_default(self, option):
 
1484
        """ Get the default option for a preference. """
 
1485
        try:
 
1486
            ret = self.defaults[option]
 
1487
        except KeyError:
 
1488
            return None
 
1489
        else:
 
1490
            return ret
 
1491
 
 
1492
    def refresh(self):
 
1493
        """ Refresh the configuration. """
 
1494
        # First write out the changes
 
1495
        self.write()
 
1496
        # Then load the configuration again
 
1497
        self.read()
 
1498
 
 
1499
    def read(self):
 
1500
        """ Just read the configuration. """
 
1501
        # Re-initialize the config parser object to avoid some bugs
 
1502
        self.config = ConfigParser.RawConfigParser()
 
1503
        self.config.read([self._filename])
 
1504
    
 
1505
    def write(self):
 
1506
        """ Write the configuration to the appropriate files. """
 
1507
        fp = open(self._filename, 'w')
 
1508
        self.config.write(fp)
 
1509
        fp.close()
 
1510
 
 
1511
    def get_bookmarks(self):
 
1512
        """ Return the list of bookmarks. """
 
1513
        bookmarks = self.config.sections()
 
1514
        if self.config.has_section('preferences'):
 
1515
            bookmarks.remove('preferences')
 
1516
        return bookmarks
 
1517
 
 
1518
    def add_bookmark(self, path):
 
1519
        """ Add bookmark. """
 
1520
        try:
 
1521
            self.config.add_section(path)
 
1522
        except ConfigParser.DuplicateSectionError:
 
1523
            return False
 
1524
        else:
 
1525
            return True
 
1526
 
 
1527
    def get_bookmark_title(self, path):
 
1528
        """ Get bookmark title. """
 
1529
        try:
 
1530
            ret = self.config.get(path, 'title')
 
1531
        except ConfigParser.NoOptionError:
 
1532
            ret = path
 
1533
        
 
1534
        return ret
 
1535
    
 
1536
    def set_bookmark_title(self, path, title):
 
1537
        """ Set bookmark title. """
 
1538
        # FIXME: What if path isn't listed yet?
 
1539
        # FIXME: Canonicalize paths first?
 
1540
        self.config.set(path, 'title', title)
 
1541
    
 
1542
    def remove_bookmark(self, path):
 
1543
        """ Remove bookmark. """
 
1544
        return self.config.remove_section(path)
 
1545
 
 
1546
    def set_preference(self, option, value):
 
1547
        """ Set the value of the given option. """
 
1548
        if value is True:
 
1549
            value = 'yes'
 
1550
        elif value is False:
 
1551
            value = 'no'
 
1552
        
 
1553
        if self.config.has_section('preferences'):
 
1554
            self.config.set('preferences', option, value)
 
1555
        else:
 
1556
            self.config.add_section('preferences')
 
1557
            self.config.set('preferences', option, value)
 
1558
 
 
1559
    def get_preference(self, option, kind='str'):
 
1560
        """ Get the value of the given option.
 
1561
        
 
1562
        :param kind: str/bool/int/float. default: str
 
1563
        """
 
1564
        if self.config.has_option('preferences', option):
 
1565
            if kind == 'bool':
 
1566
                return self.config.getboolean('preferences', option)
 
1567
            elif kind == 'int':
 
1568
                return self.config.getint('preferences', option)
 
1569
            elif kind == 'float':
 
1570
                return self.config.getfloat('preferences', option)
 
1571
            else:
 
1572
                return self.config.get('preferences', option)
 
1573
        else:
 
1574
            try:
 
1575
                return self._get_default(option)
 
1576
            except KeyError:
 
1577
                return None
 
1578