/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: Szilveszter Farkas (Phanatic)
  • Date: 2006-09-30 13:04:15 UTC
  • mto: (0.14.3 main)
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060930130415-aba4100709e11f0a
Loads of fixes. Pyflakes cleanup.

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
 
 
20
try:
 
21
    import pygtk
 
22
    pygtk.require("2.0")
 
23
except:
 
24
    pass
 
25
 
 
26
import gtk
 
27
import gtk.gdk
 
28
import gtk.glade
 
29
 
 
30
from bzrlib.branch import Branch
 
31
import bzrlib.errors as errors
 
32
from bzrlib.workingtree import WorkingTree
 
33
 
 
34
# Olive GTK UI version
 
35
__version__ = '0.11.0'
 
36
 
 
37
# Load the glade file
 
38
if sys.platform == 'win32':
 
39
    gladefile = os.path.dirname(sys.executable) + "/share/olive/olive.glade"
 
40
else:
 
41
    gladefile = "/usr/share/olive/olive.glade"
 
42
 
 
43
if not os.path.exists(gladefile):
 
44
    # Load from current directory if not installed
 
45
    gladefile = "olive.glade"
 
46
    # Check again
 
47
    if not os.path.exists(gladefile):
 
48
        # Fail
 
49
        print _('Glade file cannot be found.')
 
50
        sys.exit(1)
 
51
 
 
52
from dialog import error_dialog, info_dialog
 
53
 
 
54
class OliveGtk:
 
55
    """ The main Olive GTK frontend class. This is called when launching the
 
56
    program. """
 
57
    
 
58
    def __init__(self):
 
59
        self.toplevel = gtk.glade.XML(gladefile, 'window_main', 'olive-gtk')
 
60
        
 
61
        self.window = self.toplevel.get_widget('window_main')
 
62
        
 
63
        self.pref = OlivePreferences()
 
64
 
 
65
        # Initialize the statusbar
 
66
        self.statusbar = self.toplevel.get_widget('statusbar')
 
67
        self.context_id = self.statusbar.get_context_id('olive')
 
68
        
 
69
        # Get the main window
 
70
        self.window_main = self.toplevel.get_widget('window_main')
 
71
        # Get the HPaned
 
72
        self.hpaned_main = self.toplevel.get_widget('hpaned_main')
 
73
        # Get the TreeViews
 
74
        self.treeview_left = self.toplevel.get_widget('treeview_left')
 
75
        self.treeview_right = self.toplevel.get_widget('treeview_right')
 
76
        # Get some important menu items
 
77
        self.menuitem_add_files = self.toplevel.get_widget('menuitem_add_files')
 
78
        self.menuitem_remove_files = self.toplevel.get_widget('menuitem_remove_file')
 
79
        self.menuitem_file_make_directory = self.toplevel.get_widget('menuitem_file_make_directory')
 
80
        self.menuitem_file_rename = self.toplevel.get_widget('menuitem_file_rename')
 
81
        self.menuitem_file_move = self.toplevel.get_widget('menuitem_file_move')
 
82
        self.menuitem_view_show_hidden_files = self.toplevel.get_widget('menuitem_view_show_hidden_files')
 
83
        self.menuitem_branch = self.toplevel.get_widget('menuitem_branch')
 
84
        self.menuitem_branch_init = self.toplevel.get_widget('menuitem_branch_initialize')
 
85
        self.menuitem_branch_get = self.toplevel.get_widget('menuitem_branch_get')
 
86
        self.menuitem_branch_checkout = self.toplevel.get_widget('menuitem_branch_checkout')
 
87
        self.menuitem_branch_pull = self.toplevel.get_widget('menuitem_branch_pull')
 
88
        self.menuitem_branch_push = self.toplevel.get_widget('menuitem_branch_push')
 
89
        self.menuitem_branch_commit = self.toplevel.get_widget('menuitem_branch_commit')
 
90
        self.menuitem_branch_status = self.toplevel.get_widget('menuitem_branch_status')
 
91
        self.menuitem_branch_missing = self.toplevel.get_widget('menuitem_branch_missing_revisions')
 
92
        self.menuitem_stats = self.toplevel.get_widget('menuitem_stats')
 
93
        self.menuitem_stats_diff = self.toplevel.get_widget('menuitem_stats_diff')
 
94
        self.menuitem_stats_log = self.toplevel.get_widget('menuitem_stats_log')
 
95
        # Get some toolbuttons
 
96
        #self.menutoolbutton_diff = self.toplevel.get_widget('menutoolbutton_diff')
 
97
        self.toolbutton_diff = self.toplevel.get_widget('toolbutton_diff')
 
98
        self.toolbutton_log = self.toplevel.get_widget('toolbutton_log')
 
99
        self.toolbutton_commit = self.toplevel.get_widget('toolbutton_commit')
 
100
        self.toolbutton_pull = self.toplevel.get_widget('toolbutton_pull')
 
101
        self.toolbutton_push = self.toplevel.get_widget('toolbutton_push')
 
102
        # Get the drive selector
 
103
        self.combobox_drive = gtk.combo_box_new_text()
 
104
        self.combobox_drive.connect("changed", self._refresh_drives)
 
105
        
 
106
        self.vbox_main_right = self.toplevel.get_widget('vbox_main_right')
 
107
 
 
108
        
 
109
        # Dictionary for signal_autoconnect
 
110
        dic = { "on_window_main_destroy": gtk.main_quit,
 
111
                "on_window_main_delete_event": self.on_window_main_delete_event,
 
112
                "on_quit_activate": self.on_window_main_delete_event,
 
113
                "on_about_activate": self.on_about_activate,
 
114
                "on_menuitem_add_files_activate": self.on_menuitem_add_files_activate,
 
115
                "on_menuitem_remove_file_activate": self.on_menuitem_remove_file_activate,
 
116
                "on_menuitem_file_make_directory_activate": self.on_menuitem_file_make_directory_activate,
 
117
                "on_menuitem_file_move_activate": self.on_menuitem_file_move_activate,
 
118
                "on_menuitem_file_rename_activate": self.on_menuitem_file_rename_activate,
 
119
                "on_menuitem_view_show_hidden_files_activate": self.on_menuitem_view_show_hidden_files_activate,
 
120
                "on_menuitem_view_refresh_activate": self.on_menuitem_view_refresh_activate,
 
121
                "on_menuitem_branch_initialize_activate": self.on_menuitem_branch_initialize_activate,
 
122
                "on_menuitem_branch_get_activate": self.on_menuitem_branch_get_activate,
 
123
                "on_menuitem_branch_checkout_activate": self.on_menuitem_branch_checkout_activate,
 
124
                "on_menuitem_branch_commit_activate": self.on_menuitem_branch_commit_activate,
 
125
                "on_menuitem_branch_push_activate": self.on_menuitem_branch_push_activate,
 
126
                "on_menuitem_branch_pull_activate": self.on_menuitem_branch_pull_activate,
 
127
                "on_menuitem_branch_status_activate": self.on_menuitem_branch_status_activate,
 
128
                "on_menuitem_branch_missing_revisions_activate": self.on_menuitem_branch_missing_revisions_activate,
 
129
                "on_menuitem_stats_diff_activate": self.on_menuitem_stats_diff_activate,
 
130
                "on_menuitem_stats_log_activate": self.on_menuitem_stats_log_activate,
 
131
                "on_menuitem_stats_infos_activate": self.on_menuitem_stats_infos_activate,
 
132
                "on_toolbutton_refresh_clicked": self.on_menuitem_view_refresh_activate,
 
133
                "on_toolbutton_log_clicked": self.on_menuitem_stats_log_activate,
 
134
                #"on_menutoolbutton_diff_clicked": self.on_menuitem_stats_diff_activate,
 
135
                "on_toolbutton_diff_clicked": self.on_menuitem_stats_diff_activate,
 
136
                "on_toolbutton_commit_clicked": self.on_menuitem_branch_commit_activate,
 
137
                "on_toolbutton_pull_clicked": self.on_menuitem_branch_pull_activate,
 
138
                "on_toolbutton_push_clicked": self.on_menuitem_branch_push_activate,
 
139
                "on_treeview_right_button_press_event": self.on_treeview_right_button_press_event,
 
140
                "on_treeview_right_row_activated": self.on_treeview_right_row_activated,
 
141
                "on_treeview_left_button_press_event": self.on_treeview_left_button_press_event,
 
142
                "on_treeview_left_row_activated": self.on_treeview_left_row_activated }
 
143
        
 
144
        # Connect the signals to the handlers
 
145
        self.toplevel.signal_autoconnect(dic)
 
146
        
 
147
        # Apply window size and position
 
148
        width = self.pref.get_preference('window_width', 'int')
 
149
        height = self.pref.get_preference('window_height', 'int')
 
150
        self.window.resize(width, height)
 
151
        x = self.pref.get_preference('window_x', 'int')
 
152
        y = self.pref.get_preference('window_y', 'int')
 
153
        self.window.move(x, y)
 
154
        # Apply paned position
 
155
        pos = self.pref.get_preference('paned_position', 'int')
 
156
        self.hpaned_main.set_position(pos)
 
157
        
 
158
        # Apply menu to the toolbutton
 
159
        #menubutton = self.toplevel.get_widget('menutoolbutton_diff')
 
160
        #menubutton.set_menu(handler.menu.toolbar_diff)
 
161
        
 
162
        # Now we can show the window
 
163
        self.window.show()
 
164
        
 
165
        # Show drive selector if under Win32
 
166
        if sys.platform == 'win32':
 
167
            self.vbox_main_right.pack_start(self.combobox_drive, False, True, 0)
 
168
            self.vbox_main_right.reorder_child(self.combobox_drive, 0)
 
169
            self.combobox_drive.show()
 
170
            self.gen_hard_selector()
 
171
        
 
172
        self._load_left()
 
173
 
 
174
        # Apply menu state
 
175
        self.menuitem_view_show_hidden_files.set_active(self.pref.get_preference('dotted_files', 'bool'))
 
176
 
 
177
        self.set_path(os.getcwd())
 
178
        self._load_right()
 
179
 
 
180
    def set_path(self, path):
 
181
        self.path = path
 
182
        self.notbranch = False
 
183
        try:
 
184
            self.wt, self.wtpath = WorkingTree.open_containing(self.path)
 
185
        except errors.NotBranchError:
 
186
            self.notbranch = True
 
187
 
 
188
    def get_path(self):
 
189
        return self.path
 
190
   
 
191
    def on_about_activate(self, widget):
 
192
        from dialog import about
 
193
        about()
 
194
        
 
195
    def on_menuitem_add_files_activate(self, widget):
 
196
        """ Add file(s)... menu handler. """
 
197
        from add import OliveAdd
 
198
        add = OliveAdd(self.wt, self.wtpath, self.get_selected_right())
 
199
        add.display()
 
200
    
 
201
    def on_menuitem_branch_get_activate(self, widget):
 
202
        """ Branch/Get... menu handler. """
 
203
        from branch import OliveBranch
 
204
        branch = OliveBranch(self.get_path())
 
205
        branch.display()
 
206
    
 
207
    def on_menuitem_branch_checkout_activate(self, widget):
 
208
        """ Branch/Checkout... menu handler. """
 
209
        from checkout import OliveCheckout
 
210
        checkout = OliveCheckout(self.get_path())
 
211
        checkout.display()
 
212
    
 
213
    def on_menuitem_branch_commit_activate(self, widget):
 
214
        """ Branch/Commit... menu handler. """
 
215
        from commit import OliveCommit
 
216
        commit = OliveCommit(self.wt, self.wtpath)
 
217
        commit.display()
 
218
    
 
219
    def on_menuitem_branch_missing_revisions_activate(self, widget):
 
220
        """ Branch/Missing revisions menu handler. """
 
221
        local_branch = self.wt.branch
 
222
        
 
223
        other_branch = local_branch.get_parent()
 
224
        if other_branch is None:
 
225
            error_dialog(_('Parent location is unknown'),
 
226
                         _('Cannot determine missing revisions if no parent location is known.'))
 
227
            return
 
228
        
 
229
        remote_branch = Branch.open(other_branch)
 
230
        
 
231
        if remote_branch.base == local_branch.base:
 
232
            remote_branch = local_branch
 
233
 
 
234
        ret = len(local_branch.missing_revisions(remote_branch))
 
235
 
 
236
        if ret > 0:
 
237
            info_dialog(_('There are missing revisions'),
 
238
                        _('%d revision(s) missing.') % ret)
 
239
        else:
 
240
            info_dialog(_('Local branch up to date'),
 
241
                        _('There are no missing revisions.'))
 
242
 
 
243
    def on_menuitem_branch_pull_activate(self, widget):
 
244
        """ Branch/Pull menu handler. """
 
245
        branch_to = self.wt.branch
 
246
 
 
247
        location = branch_to.get_parent()
 
248
        if location is None:
 
249
            error_dialog(_('Parent location is unknown'),
 
250
                                     _('Pulling is not possible until there is a parent location.'))
 
251
            return
 
252
 
 
253
        try:
 
254
            branch_from = Branch.open(location)
 
255
        except errors.NotBranchError:
 
256
            error_dialog(_('Directory is not a branch'),
 
257
                                     _('You can perform this action only in a branch.'))
 
258
 
 
259
        if branch_to.get_parent() is None:
 
260
            branch_to.set_parent(branch_from.base)
 
261
 
 
262
        #old_rh = branch_to.revision_history()
 
263
        #if tree_to is not None:
 
264
        #    tree_to.pull(branch_from)
 
265
        #else:
 
266
        #    branch_to.pull(branch_from)
 
267
        branch_to.pull(branch_from)
 
268
        
 
269
        # TODO: get the number of pulled revisions
 
270
        ret = 0
 
271
        
 
272
        info_dialog(_('Pull successful'), _('%d revision(s) pulled.') % ret)
 
273
    
 
274
    def on_menuitem_branch_push_activate(self, widget):
 
275
        """ Branch/Push... menu handler. """
 
276
        from push import OlivePush
 
277
        push = OlivePush(self.wt.branch)
 
278
        push.display()
 
279
    
 
280
    def on_menuitem_branch_status_activate(self, widget):
 
281
        """ Branch/Status... menu handler. """
 
282
        from status import OliveStatus
 
283
        status = OliveStatus(self.wt, self.wtpath)
 
284
        status.display()
 
285
    
 
286
    def on_menuitem_branch_initialize_activate(self, widget):
 
287
        """ Initialize current directory. """
 
288
        import bzrlib.bzrdir as bzrdir
 
289
        
 
290
        try:
 
291
            if not os.path.exists(self.path):
 
292
                os.mkdir(self.path)
 
293
     
 
294
            try:
 
295
                existing_bzrdir = bzrdir.BzrDir.open(self.path)
 
296
            except errors.NotBranchError:
 
297
                bzrdir.BzrDir.create_branch_convenience(self.path)
 
298
            else:
 
299
                if existing_bzrdir.has_branch():
 
300
                    if existing_bzrdir.has_workingtree():
 
301
                        raise errors.AlreadyBranchError(self.path)
 
302
                    else:
 
303
                        raise errors.BranchExistsWithoutWorkingTree(self.path)
 
304
                else:
 
305
                    existing_bzrdir.create_branch()
 
306
                    existing_bzrdir.create_workingtree()
 
307
        except errors.AlreadyBranchError, errmsg:
 
308
            error_dialog(_('Directory is already a branch'),
 
309
                                     _('The current directory (%s) is already a branch.\nYou can start using it, or initialize another directory.') % errmsg)
 
310
        except errors.BranchExistsWithoutWorkingTree, errmsg:
 
311
            error_dialog(_('Branch without a working tree'),
 
312
                                     _('The current directory (%s)\nis a branch without a working tree.') % errmsg)
 
313
        else:
 
314
            info_dialog(_('Initialize successful'),
 
315
                                    _('Directory successfully initialized.'))
 
316
            self.refresh_right()
 
317
        
 
318
    def on_menuitem_file_make_directory_activate(self, widget):
 
319
        """ File/Make directory... menu handler. """
 
320
        from mkdir import OliveMkdir
 
321
        mkdir = OliveMkdir(self.wt, self.wtpath)
 
322
        mkdir.display()
 
323
    
 
324
    def on_menuitem_file_move_activate(self, widget):
 
325
        """ File/Move... menu handler. """
 
326
        from move import OliveMove
 
327
        move = OliveMove(self.wt, self.wtpath, self.get_selected_right())
 
328
        move.display()
 
329
    
 
330
    def on_menuitem_file_rename_activate(self, widget):
 
331
        """ File/Rename... menu handler. """
 
332
        from rename import OliveRename
 
333
        rename = OliveRename(self.wt, self.wtpath, self.get_selected_right())
 
334
        rename.display()
 
335
 
 
336
    def on_menuitem_remove_file_activate(self, widget):
 
337
        """ Remove (unversion) selected file. """
 
338
        from remove import OliveRemove
 
339
        remove = OliveRemove(self.wt, self.wtpath, self.get_selected_right())
 
340
        remove.display()
 
341
    
 
342
    def on_menuitem_stats_diff_activate(self, widget):
 
343
        """ Statistics/Differences... menu handler. """
 
344
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
 
345
        window = DiffWindow()
 
346
        parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
347
        window.set_diff(self.wt.branch.nick, self.wt, parent_tree)
 
348
        window.show()
 
349
    
 
350
    def on_menuitem_stats_infos_activate(self, widget):
 
351
        """ Statistics/Informations... menu handler. """
 
352
        from info import OliveInfo
 
353
        info = OliveInfo(self.wt)
 
354
        info.display()
 
355
    
 
356
    def on_menuitem_stats_log_activate(self, widget):
 
357
        """ Statistics/Log... menu handler. """
 
358
        from bzrlib.plugins.gtk.viz.branchwin import BranchWindow
 
359
        window = BranchWindow()
 
360
        window.set_branch(self.wt.branch, self.wt.branch.last_revision(), None)
 
361
        window.show()
 
362
    
 
363
    def on_menuitem_view_refresh_activate(self, widget):
 
364
        """ View/Refresh menu handler. """
 
365
        # Refresh the left pane
 
366
        self.refresh_left()
 
367
        # Refresh the right pane
 
368
        self.refresh_right()
 
369
   
 
370
    def on_menuitem_view_show_hidden_files_activate(self, widget):
 
371
        """ View/Show hidden files menu handler. """
 
372
        self.pref.set_preference('dotted_files', widget.get_active())
 
373
 
 
374
    def on_treeview_left_button_press_event(self, widget, event):
 
375
        """ Occurs when somebody right-clicks in the bookmark list. """
 
376
        if event.button == 3:
 
377
            # Don't show context with nothing selected
 
378
            if self.get_selected_left() == None:
 
379
                return
 
380
 
 
381
            # Create a menu
 
382
            from menu import OliveMenu
 
383
            menu = OliveMenu(self.get_path(), self.get_selected_left())
 
384
            
 
385
            menu.left_context_menu().popup(None, None, None, 0,
 
386
                                           event.time)
 
387
 
 
388
    def on_treeview_left_row_activated(self, treeview, path, view_column):
 
389
        """ Occurs when somebody double-clicks or enters an item in the
 
390
        bookmark list. """
 
391
 
 
392
        newdir = self.get_selected_left()
 
393
        if newdir == None:
 
394
            return
 
395
 
 
396
        self.set_path(newdir)
 
397
        self.refresh_right()
 
398
 
 
399
    def on_treeview_right_button_press_event(self, widget, event):
 
400
        """ Occurs when somebody right-clicks in the file list. """
 
401
        if event.button == 3:
 
402
            # Create a menu
 
403
            from menu import OliveMenu
 
404
            menu = OliveMenu(self.get_path(), self.get_selected_right())
 
405
            # get the menu items
 
406
            m_add = menu.ui.get_widget('/context_right/add')
 
407
            m_remove = menu.ui.get_widget('/context_right/remove')
 
408
            m_commit = menu.ui.get_widget('/context_right/commit')
 
409
            m_diff = menu.ui.get_widget('/context_right/diff')
 
410
            # check if we're in a branch
 
411
            try:
 
412
                from bzrlib.branch import Branch
 
413
                Branch.open_containing(self.get_path())
 
414
                m_add.set_sensitive(True)
 
415
                m_remove.set_sensitive(True)
 
416
                m_commit.set_sensitive(True)
 
417
                m_diff.set_sensitive(True)
 
418
            except errors.NotBranchError:
 
419
                m_add.set_sensitive(False)
 
420
                m_remove.set_sensitive(False)
 
421
                m_commit.set_sensitive(False)
 
422
                m_diff.set_sensitive(False)
 
423
            menu.right_context_menu().popup(None, None, None, 0,
 
424
                                            event.time)
 
425
        
 
426
    def on_treeview_right_row_activated(self, treeview, path, view_column):
 
427
        """ Occurs when somebody double-clicks or enters an item in the
 
428
        file list. """
 
429
        import os.path
 
430
        
 
431
        from launch import launch
 
432
        
 
433
        newdir = self.get_selected_right()
 
434
        
 
435
        if newdir == '..':
 
436
            self.set_path(os.path.split(self.get_path())[0])
 
437
        else:
 
438
            fullpath = self.get_path() + os.sep + newdir
 
439
            if os.path.isdir(fullpath):
 
440
                # selected item is an existant directory
 
441
                self.set_path(fullpath)
 
442
            else:
 
443
                launch(fullpath) 
 
444
        
 
445
        self.refresh_right()
 
446
    
 
447
    def on_window_main_delete_event(self, widget, event=None):
 
448
        """ Do some stuff before exiting. """
 
449
        width, height = self.window_main.get_size()
 
450
        self.pref.set_preference('window_width', width)
 
451
        self.pref.set_preference('window_height', height)
 
452
        x, y = self.window_main.get_position()
 
453
        self.pref.set_preference('window_x', x)
 
454
        self.pref.set_preference('window_y', y)
 
455
        self.pref.set_preference('paned_position',
 
456
                                      self.hpaned_main.get_position())
 
457
        
 
458
        self.pref.write()
 
459
        self.window_main.destroy()
 
460
        
 
461
    def _load_left(self):
 
462
        """ Load data into the left panel. (Bookmarks) """
 
463
        # Create TreeStore
 
464
        treestore = gtk.TreeStore(str, str)
 
465
        
 
466
        # Get bookmarks
 
467
        bookmarks = self.pref.get_bookmarks()
 
468
        
 
469
        # Add them to the TreeStore
 
470
        titer = treestore.append(None, [_('Bookmarks'), None])
 
471
        for item in bookmarks:
 
472
            title = self.pref.get_bookmark_title(item)
 
473
            treestore.append(titer, [title, item])
 
474
        
 
475
        # Create the column and add it to the TreeView
 
476
        self.treeview_left.set_model(treestore)
 
477
        tvcolumn_bookmark = gtk.TreeViewColumn(_('Bookmark'))
 
478
        self.treeview_left.append_column(tvcolumn_bookmark)
 
479
        
 
480
        # Set up the cells
 
481
        cell = gtk.CellRendererText()
 
482
        tvcolumn_bookmark.pack_start(cell, True)
 
483
        tvcolumn_bookmark.add_attribute(cell, 'text', 0)
 
484
        
 
485
        # Expand the tree
 
486
        self.treeview_left.expand_all()
 
487
 
 
488
    def _load_right(self):
 
489
        """ Load data into the right panel. (Filelist) """
 
490
        # Create ListStore
 
491
        liststore = gtk.ListStore(str, str, str)
 
492
        
 
493
        dirs = ['..']
 
494
        files = []
 
495
        
 
496
        # Fill the appropriate lists
 
497
        dotted_files = self.pref.get_preference('dotted_files', 'bool')
 
498
        for item in os.listdir(self.path):
 
499
            if not dotted_files and item[0] == '.':
 
500
                continue
 
501
            if os.path.isdir(self.path + os.sep + item):
 
502
                dirs.append(item)
 
503
            else:
 
504
                files.append(item)
 
505
            
 
506
        # Sort'em
 
507
        dirs.sort()
 
508
        files.sort()
 
509
        
 
510
        if not self.notbranch:
 
511
            branch = self.wt.branch
 
512
            tree2 = self.wt.branch.repository.revision_tree(branch.last_revision())
 
513
        
 
514
            delta = self.wt.changes_from(tree2, want_unchanged=True)
 
515
        
 
516
        # Add'em to the ListStore
 
517
        for item in dirs:    
 
518
            liststore.append([gtk.STOCK_DIRECTORY, item, ''])
 
519
        for item in files:
 
520
            status = 'unknown'
 
521
            if not self.notbranch:
 
522
                filename = self.wt.relpath(self.path + os.sep + item)
 
523
                
 
524
                for rpath, rpathnew, id, kind, text_modified, meta_modified in delta.renamed:
 
525
                    if rpathnew == filename:
 
526
                        status = 'renamed'
 
527
                for rpath, id, kind in delta.added:
 
528
                    if rpath == filename:
 
529
                        status = 'added'
 
530
                for rpath, id, kind in delta.removed:
 
531
                    if rpath == filename:
 
532
                        status = 'removed'
 
533
                for rpath, id, kind, text_modified, meta_modified in delta.modified:
 
534
                    if rpath == filename:
 
535
                        status = 'modified'
 
536
                for rpath, id, kind in delta.unchanged:
 
537
                    if rpath == filename:
 
538
                        status = 'unchanged'
 
539
            
 
540
            #try:
 
541
            #    status = fileops.status(path + os.sep + item)
 
542
            #except errors.PermissionDenied:
 
543
            #    continue
 
544
            
 
545
            if status == 'renamed':
 
546
                st = _('renamed')
 
547
            elif status == 'removed':
 
548
                st = _('removed')
 
549
            elif status == 'added':
 
550
                st = _('added')
 
551
            elif status == 'modified':
 
552
                st = _('modified')
 
553
            elif status == 'unchanged':
 
554
                st = _('unchanged')
 
555
            else:
 
556
                st = _('unknown')
 
557
            liststore.append([gtk.STOCK_FILE, item, st])
 
558
        
 
559
        # Create the columns and add them to the TreeView
 
560
        self.treeview_right.set_model(liststore)
 
561
        tvcolumn_filename = gtk.TreeViewColumn(_('Filename'))
 
562
        tvcolumn_status = gtk.TreeViewColumn(_('Status'))
 
563
        self.treeview_right.append_column(tvcolumn_filename)
 
564
        self.treeview_right.append_column(tvcolumn_status)
 
565
        
 
566
        # Set up the cells
 
567
        cellpb = gtk.CellRendererPixbuf()
 
568
        cell = gtk.CellRendererText()
 
569
        tvcolumn_filename.pack_start(cellpb, False)
 
570
        tvcolumn_filename.pack_start(cell, True)
 
571
        tvcolumn_filename.set_attributes(cellpb, stock_id=0)
 
572
        tvcolumn_filename.add_attribute(cell, 'text', 1)
 
573
        tvcolumn_status.pack_start(cell, True)
 
574
        tvcolumn_status.add_attribute(cell, 'text', 2)
 
575
        
 
576
        # Set sensitivity
 
577
        self.set_sensitivity()
 
578
        
 
579
    def get_selected_right(self):
 
580
        """ Get the selected filename. """
 
581
        treeselection = self.treeview_right.get_selection()
 
582
        (model, iter) = treeselection.get_selected()
 
583
        
 
584
        if iter is None:
 
585
            return None
 
586
        else:
 
587
            return model.get_value(iter, 1)
 
588
    
 
589
    def get_selected_left(self):
 
590
        """ Get the selected bookmark. """
 
591
        treeselection = self.treeview_left.get_selection()
 
592
        (model, iter) = treeselection.get_selected()
 
593
        
 
594
        if iter is None:
 
595
            return None
 
596
        else:
 
597
            return model.get_value(iter, 1)
 
598
 
 
599
    def set_statusbar(self, message):
 
600
        """ Set the statusbar message. """
 
601
        self.statusbar.push(self.context_id, message)
 
602
    
 
603
    def clear_statusbar(self):
 
604
        """ Clean the last message from the statusbar. """
 
605
        self.statusbar.pop(self.context_id)
 
606
    
 
607
    def set_sensitivity(self):
 
608
        """ Set menu and toolbar sensitivity. """
 
609
        self.menuitem_branch_init.set_sensitive(self.notbranch)
 
610
        self.menuitem_branch_get.set_sensitive(self.notbranch)
 
611
        self.menuitem_branch_checkout.set_sensitive(self.notbranch)
 
612
        self.menuitem_branch_pull.set_sensitive(not self.notbranch)
 
613
        self.menuitem_branch_push.set_sensitive(not self.notbranch)
 
614
        self.menuitem_branch_commit.set_sensitive(not self.notbranch)
 
615
        self.menuitem_branch_status.set_sensitive(not self.notbranch)
 
616
        self.menuitem_branch_missing.set_sensitive(not self.notbranch)
 
617
        self.menuitem_stats.set_sensitive(not self.notbranch)
 
618
        self.menuitem_add_files.set_sensitive(not self.notbranch)
 
619
        self.menuitem_remove_files.set_sensitive(not self.notbranch)
 
620
        self.menuitem_file_make_directory.set_sensitive(not self.notbranch)
 
621
        self.menuitem_file_rename.set_sensitive(not self.notbranch)
 
622
        self.menuitem_file_move.set_sensitive(not self.notbranch)
 
623
        #self.menutoolbutton_diff.set_sensitive(True)
 
624
        self.toolbutton_diff.set_sensitive(not self.notbranch)
 
625
        self.toolbutton_log.set_sensitive(not self.notbranch)
 
626
        self.toolbutton_commit.set_sensitive(not self.notbranch)
 
627
        self.toolbutton_pull.set_sensitive(not self.notbranch)
 
628
        self.toolbutton_push.set_sensitive(not self.notbranch)
 
629
    
 
630
    def refresh_left(self):
 
631
        """ Refresh the bookmark list. """
 
632
        
 
633
        # Get TreeStore and clear it
 
634
        treestore = self.treeview_left.get_model()
 
635
        treestore.clear()
 
636
 
 
637
        # Re-read preferences
 
638
        self.pref.read()
 
639
 
 
640
        # Get bookmarks
 
641
        bookmarks = self.pref.get_bookmarks()
 
642
 
 
643
        # Add them to the TreeStore
 
644
        titer = treestore.append(None, [_('Bookmarks'), None])
 
645
        for item in bookmarks:
 
646
            title = self.pref.get_bookmark_title(item)
 
647
            treestore.append(titer, [title, item])
 
648
 
 
649
        # Add the TreeStore to the TreeView
 
650
        self.treeview_left.set_model(treestore)
 
651
 
 
652
        # Expand the tree
 
653
        self.treeview_left.expand_all()
 
654
 
 
655
    def refresh_right(self, path=None):
 
656
        """ Refresh the file list. """
 
657
        from bzrlib.workingtree import WorkingTree
 
658
 
 
659
        if path is None:
 
660
            path = self.get_path()
 
661
 
 
662
        # A workaround for double-clicking Bookmarks
 
663
        if not os.path.exists(path):
 
664
            return
 
665
 
 
666
        # Get ListStore and clear it
 
667
        liststore = self.treeview_right.get_model()
 
668
        liststore.clear()
 
669
 
 
670
        dirs = ['..']
 
671
        files = []
 
672
 
 
673
        # Fill the appropriate lists
 
674
        dotted_files = self.pref.get_preference('dotted_files', 'bool')
 
675
        for item in os.listdir(path):
 
676
            if not dotted_files and item[0] == '.':
 
677
                continue
 
678
            if os.path.isdir(path + os.sep + item):
 
679
                dirs.append(item)
 
680
            else:
 
681
                files.append(item)
 
682
 
 
683
        # Sort'em
 
684
        dirs.sort()
 
685
        files.sort()
 
686
        
 
687
        # Try to open the working tree
 
688
        notbranch = False
 
689
        try:
 
690
            tree1 = WorkingTree.open_containing(path)[0]
 
691
        except errors.NotBranchError:
 
692
            notbranch = True
 
693
        except errors.PermissionDenied:
 
694
            print "DEBUG: permission denied."
 
695
        
 
696
        if not notbranch:
 
697
            branch = tree1.branch
 
698
            tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
 
699
        
 
700
            delta = tree1.changes_from(tree2, want_unchanged=True)
 
701
 
 
702
        # Add'em to the ListStore
 
703
        for item in dirs:
 
704
            liststore.append([gtk.STOCK_DIRECTORY, item, ''])
 
705
        for item in files:
 
706
            status = 'unknown'
 
707
            if not notbranch:
 
708
                filename = tree1.relpath(path + os.sep + item)
 
709
                
 
710
                for rpath, rpathnew, id, kind, text_modified, meta_modified in delta.renamed:
 
711
                    if rpathnew == filename:
 
712
                        status = 'renamed'
 
713
                for rpath, id, kind in delta.added:
 
714
                    if rpath == filename:
 
715
                        status = 'added'                
 
716
                for rpath, id, kind, text_modified, meta_modified in delta.removed:
 
717
                    if rpath == filename:
 
718
                        status = 'removed'
 
719
                for rpath, id, kind, text_modified, meta_modified in delta.modified:
 
720
                    if rpath == filename:
 
721
                        status = 'modified'
 
722
                for rpath, id, kind in delta.unchanged:
 
723
                    if rpath == filename:
 
724
                        status = 'unchanged'
 
725
            
 
726
            #try:
 
727
            #    status = fileops.status(path + os.sep + item)
 
728
            #except errors.PermissionDenied:
 
729
            #    continue
 
730
 
 
731
            if status == 'renamed':
 
732
                st = _('renamed')
 
733
            elif status == 'removed':
 
734
                st = _('removed')
 
735
            elif status == 'added':
 
736
                st = _('added')
 
737
            elif status == 'modified':
 
738
                st = _('modified')
 
739
            elif status == 'unchanged':
 
740
                st = _('unchanged')
 
741
            else:
 
742
                st = _('unknown')
 
743
            liststore.append([gtk.STOCK_FILE, item, st])
 
744
 
 
745
        # Add the ListStore to the TreeView
 
746
        self.treeview_right.set_model(liststore)
 
747
        
 
748
        # Set sensitivity
 
749
        self.set_sensitivity()
 
750
 
 
751
    def _harddisks(self):
 
752
        """ Returns hard drive letters under Win32. """
 
753
        try:
 
754
            import win32file
 
755
            import string
 
756
        except ImportError:
 
757
            if sys.platform == 'win32':
 
758
                print "pyWin32 modules needed to run Olive on Win32."
 
759
                sys.exit(1)
 
760
            else:
 
761
                pass
 
762
        
 
763
        driveletters = []
 
764
        for drive in string.ascii_uppercase:
 
765
            if win32file.GetDriveType(drive+':') == win32file.DRIVE_FIXED:
 
766
                driveletters.append(drive+':')
 
767
        return driveletters
 
768
    
 
769
    def gen_hard_selector(self):
 
770
        """ Generate the hard drive selector under Win32. """
 
771
        drives = self._harddisks()
 
772
        for drive in drives:
 
773
            self.combobox_drive.append_text(drive)
 
774
    
 
775
    def _refresh_drives(self, combobox):
 
776
        model = combobox.get_model()
 
777
        active = combobox.get_active()
 
778
        if active >= 0:
 
779
            drive = model[active][0]
 
780
            self.refresh_right(drive + '\\')
 
781
 
 
782
import ConfigParser
 
783
 
 
784
class OlivePreferences:
 
785
    """ A class which handles Olive's preferences. """
 
786
    def __init__(self):
 
787
        """ Initialize the Preferences class. """
 
788
        # Some default options
 
789
        self.defaults = { 'strict_commit' : False,
 
790
                          'dotted_files'  : False,
 
791
                          'window_width'  : 700,
 
792
                          'window_height' : 400,
 
793
                          'window_x'      : 40,
 
794
                          'window_y'      : 40,
 
795
                          'paned_position': 200 }
 
796
 
 
797
        # Create a config parser object
 
798
        self.config = ConfigParser.RawConfigParser()
 
799
        
 
800
        # Load the configuration
 
801
        self.read()
 
802
        
 
803
    def _get_default(self, option):
 
804
        """ Get the default option for a preference. """
 
805
        try:
 
806
            ret = self.defaults[option]
 
807
        except KeyError:
 
808
            return None
 
809
        else:
 
810
            return ret
 
811
 
 
812
    def refresh(self):
 
813
        """ Refresh the configuration. """
 
814
        # First write out the changes
 
815
        self.write()
 
816
        # Then load the configuration again
 
817
        self.read()
 
818
 
 
819
    def read(self):
 
820
        """ Just read the configuration. """
 
821
        if sys.platform == 'win32':
 
822
            # Windows - no dotted files
 
823
            self.config.read([os.path.expanduser('~/olive.conf')])
 
824
        else:
 
825
            self.config.read([os.path.expanduser('~/.olive.conf')])
 
826
    
 
827
    def write(self):
 
828
        """ Write the configuration to the appropriate files. """
 
829
        if sys.platform == 'win32':
 
830
            # Windows - no dotted files
 
831
            fp = open(os.path.expanduser('~/olive.conf'), 'w')
 
832
            self.config.write(fp)
 
833
            fp.close()
 
834
        else:
 
835
            fp = open(os.path.expanduser('~/.olive.conf'), 'w')
 
836
            self.config.write(fp)
 
837
            fp.close()
 
838
 
 
839
    def get_bookmarks(self):
 
840
        """ Return the list of bookmarks. """
 
841
        bookmarks = self.config.sections()
 
842
        if self.config.has_section('preferences'):
 
843
            bookmarks.remove('preferences')
 
844
        return bookmarks
 
845
 
 
846
    def add_bookmark(self, path):
 
847
        """ Add bookmark. """
 
848
        try:
 
849
            self.config.add_section(path)
 
850
        except ConfigParser.DuplicateSectionError:
 
851
            return False
 
852
        else:
 
853
            return True
 
854
 
 
855
    def get_bookmark_title(self, path):
 
856
        """ Get bookmark title. """
 
857
        try:
 
858
            ret = self.config.get(path, 'title')
 
859
        except ConfigParser.NoOptionError:
 
860
            ret = path
 
861
        
 
862
        return ret
 
863
    
 
864
    def set_bookmark_title(self, path, title):
 
865
        """ Set bookmark title. """
 
866
        self.config.set(path, 'title', title)
 
867
    
 
868
    def remove_bookmark(self, path):
 
869
        """ Remove bookmark. """
 
870
        return self.config.remove_section(path)
 
871
 
 
872
    def set_preference(self, option, value):
 
873
        """ Set the value of the given option. """
 
874
        if self.config.has_section('preferences'):
 
875
            self.config.set('preferences', option, value)
 
876
        else:
 
877
            self.config.add_section('preferences')
 
878
            self.config.set('preferences', option, value)
 
879
 
 
880
    def get_preference(self, option, kind='str'):
 
881
        """ Get the value of the given option.
 
882
        
 
883
        :param kind: str/bool/int/float. default: str
 
884
        """
 
885
        if self.config.has_option('preferences', option):
 
886
            if kind == 'bool':
 
887
                #return self.config.getboolean('preferences', option)
 
888
                return True
 
889
            elif kind == 'int':
 
890
                return self.config.getint('preferences', option)
 
891
            elif kind == 'float':
 
892
                return self.config.getfloat('preferences', option)
 
893
            else:
 
894
                return self.config.get('preferences', option)
 
895
        else:
 
896
            try:
 
897
                return self._get_default(option)
 
898
            except KeyError:
 
899
                return None
 
900