/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
import os.path
import shutil
import sys

try:
    import pygtk
    pygtk.require("2.0")
except:
    pass

import gtk

import bzrlib.errors as errors
from bzrlib.workingtree import WorkingTree

from bzrlib.plugins.gtk import _i18n
from bzrlib.plugins.gtk.dialog import error_dialog, info_dialog, question_dialog, warning_dialog
from bzrlib.plugins.gtk.errors import show_bzr_error
from bzrlib.plugins.gtk.annotate.gannotate import GAnnotateWindow
from bzrlib.plugins.gtk.annotate.config import GAnnotateConfig
from bzrlib.plugins.gtk.diff import DiffWindow
from launch import launch
from bzrlib.plugins.gtk.olive import Preferences

class OliveMenu:
    """ This class is responsible for building the context menus. """
    def __init__(self, path, selected, app=None):
        # Load the UI file
        from guifiles import UIFILENAME

        self.uifile = UIFILENAME

        # Preferences handler
        self.pref = Preferences()
        
        # Set default values
        self.path = path
        self.selected = selected
        self.app = app
        
        # Create the file list context menu
        self.ui = gtk.UIManager()
        
        self.actiongroup = gtk.ActionGroup('context')
        self.actiongroup.add_actions([('add', gtk.STOCK_ADD,
                                       _i18n('Add'), None,
                                       _i18n('Add the selected file'),
                                       self.add_file),
                                      ('remove', gtk.STOCK_REMOVE,
                                       _i18n('Remove'), None,
                                       _i18n('Remove the selected file'),
                                       self.remove_file),
                                      ('remove_and_delete', gtk.STOCK_REMOVE,
                                       _i18n('Remove and delete'), None,
                                       _i18n('Remove the selected file/dir and delete from disk'),
                                       self.remove_and_delete_file),
                                      ('rename', None,
                                       _i18n('Rename'), None,
                                       _i18n('Rename the selected file'),
                                       self.rename_file),
                                      ('open', gtk.STOCK_OPEN,
                                       _i18n('Open'), None,
                                       _i18n('Open the selected file'),
                                       self.open_file),
                                      ('revert', None,
                                       _i18n('Revert'), None,
                                       _i18n('Revert the changes'),
                                       self.revert),
                                      ('commit', None,
                                       _i18n('Commit'), None,
                                       _i18n('Commit the changes'),
                                       self.commit),
                                      ('annotate', None,
                                       _i18n('Annotate'), None,
                                       _i18n('Annotate the selected file'),
                                       self.annotate),
                                      ('diff', None,
                                       _i18n('Diff'), None,
                                       _i18n('Show the diff of the file'),
                                       self.diff),
                                      ('bookmark', None,
                                       _i18n('Bookmark'), None,
                                       _i18n('Bookmark current location'),
                                       self.bookmark),
                                      ('edit_bookmark', gtk.STOCK_EDIT,
                                       _i18n('Edit'), None,
                                       _i18n('Edit the selected bookmark'),
                                       self.edit_bookmark),
                                      ('remove_bookmark', gtk.STOCK_REMOVE,
                                       _i18n('Remove'), None,
                                       _i18n('Remove the selected bookmark'),
                                       self.remove_bookmark),
                                      ('open_folder', gtk.STOCK_OPEN,
                                       _i18n('Open Folder'), None,
                                       _i18n('Open bookmark folder in Nautilus'),
                                       self.open_folder),
                                      ('diff_selected', None,
                                       _i18n('Selected...'), None,
                                       _i18n('Show the differences of the selected file'),
                                       self.diff_selected),
                                      ('diff_all', None,
                                       _i18n('All...'), None,
                                       _i18n('Show the differences of all files'),
                                       self.diff_all),
                                      ('view_remote', None,
                                       _i18n('View contents'), None,
                                       _i18n('View the contents of the file in a builtin viewer'),
                                       self.view_remote),
                                      ('diff_remote', None,
                                       _i18n('Show differences'), None,
                                       _i18n('Show the differences between two revisions of the file'),
                                       self.diff_remote),
                                      ('revert_remote', None,
                                       _i18n('Revert to this revision'), None,
                                       _i18n('Revert the selected file to the selected revision'),
                                       self.revert_remote)
                                     ])
        
        self.ui.insert_action_group(self.actiongroup, 0)
        self.ui.add_ui_from_file(self.uifile)
        
        self.cmenu_right = self.ui.get_widget('/context_right')
        self.cmenu_left = self.ui.get_widget('/context_left')
        self.toolbar_diff = self.ui.get_widget('/toolbar_diff')
        self.cmenu_remote = self.ui.get_widget('/context_remote')
        
        # Set icons
        # TODO: do it without using deprecated comm
        #commit_menu = self.ui.get_widget('/context_right/commit')
        #commit_image = self.comm.menuitem_branch_commit.get_image()
        #commit_pixbuf = commit_image.get_pixbuf()
        #commit_icon = gtk.Image()
        #commit_icon.set_from_pixbuf(commit_pixbuf)
        #commit_menu.set_image(commit_icon)
        #diff_menu = self.ui.get_widget('/context_right/diff')
        #diff_image = self.comm.menuitem_stats_diff.get_image()
        #diff_pixbuf = diff_image.get_pixbuf()
        #diff_icon = gtk.Image()
        #diff_icon.set_from_pixbuf(diff_pixbuf)
        #diff_menu.set_image(diff_icon)

    def right_context_menu(self):
        return self.cmenu_right
    
    def left_context_menu(self):
        return self.cmenu_left
    
    def remote_context_menu(self):
        return self.cmenu_remote
    
    @show_bzr_error
    def add_file(self, action):
        """ Right context menu -> Add """
        import bzrlib.add
        
        # Add only the selected file
        directory = self.path
        filename = self.selected
            
        if filename is None:
            error_dialog(_i18n('No file was selected'),
                         _i18n('Please select a file from the list,\nor choose the other option.'))
            return
        
        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
        wt.add([path])
    
    @show_bzr_error
    def annotate(self, action):
        """ Right context menu -> Annotate """
        directory = self.path
        filename = self.selected
        
        if filename is None:
            error_dialog(_i18n('No file was selected'),
                         _i18n('Please select a file from the list.'))
            return
        
        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
        
        branch = wt.branch
        file_id = wt.path2id(wt.relpath(os.path.join(directory, filename)))
        
        window = GAnnotateWindow(all=False, plain=False, parent=self.app)
        window.set_title(os.path.join(directory, filename) + " - Annotate")
        config = GAnnotateConfig(window)
        window.show()
        branch.lock_read()
        try:
            window.annotate(wt, branch, file_id)
        finally:
            branch.unlock()
    
    @show_bzr_error
    def remove_file(self, action,delete_on_disk=0):
        """ Right context menu -> Remove """
        # Remove only the selected file
        directory = self.path
        filename = self.selected
        
        if filename is None:
            error_dialog(_i18n('No file was selected'),
                         _i18n('Please select a file from the list,\nor choose the other option.'))
            return
        
        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
        wt.remove(path)
        
        if delete_on_disk:
            abs_filename = os.path.join(directory,filename)
            if os.path.isdir(abs_filename):
                response = question_dialog(_i18n('Delete directory with all directories below ?'), abs_filename )
                if response == gtk.RESPONSE_YES:
                    shutil.rmtree(abs_filename)
            else:
                os.remove(abs_filename)
                
        self.app.set_path(self.path)
        self.app.refresh_right()
        
    def remove_and_delete_file(self, action):
        """ Right context menu -> Remove and delete"""
        self.remove_file(action,delete_on_disk=1)

    def rename_file(self, action):
        """ Right context menu -> Rename """
        from rename import OliveRename
        wt = WorkingTree.open_containing(self.path + os.sep + self.selected)[0]
        rename = OliveRename(wt, wt.relpath(self.path), self.selected)
        rename.display()
    
    def open_file(self, action):
        """ Right context menu -> Open """
        # Open only the selected file
        filename = self.selected
        
        if filename is None:
            error_dialog(_i18n('No file was selected'),
                         _i18n('Please select a file from the list,\nor choose the other option.'))
            return

        if filename == '..':
            # TODO: how to enter a directory?
            return
        else:
            fullpath = self.path + os.sep + filename
            if os.path.isdir(fullpath):
                # selected item is an existant directory
                # TODO: how to enter a directory?
                return
            else:
                launch(fullpath) 

    def revert(self, action):
        """ Right context menu -> Revert """
        wt, path = WorkingTree.open_containing(self.path)
        ret = wt.revert([os.path.join(path, self.selected)])
        if ret:
            warning_dialog(_i18n('Conflicts detected'),
                           _i18n('Please have a look at the working tree before continuing.'))
        else:
            info_dialog(_i18n('Revert successful'),
                        _i18n('All files reverted to last revision.'))
        self.app.refresh_right()       
    
    def commit(self, action):
        """ Right context menu -> Commit """
        from commit import CommitDialog
        branch = None
        try:
            wt, path = WorkingTree.open_containing(self.path)
            branch = wt.branch
        except NotBranchError, e:
            path = e.path
        
        if self.selected:
            selected = os.path.join(path, self.selected)
        else:
            selected = None
        commit = CommitDialog(wt=wt, selected=selected, parent=None)
        response = commit.run()
        if response != gtk.RESPONSE_NONE:
            commit.hide()
        
            if response == gtk.RESPONSE_OK:
                self.app.refresh_right()
            
            commit.destroy()
    
    @show_bzr_error
    def diff(self, action):
        """ Right context menu -> Diff """
        wt = WorkingTree.open_containing(self.path)[0]
        window = DiffWindow(self.app)
        parent_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
        window.set_diff(wt.branch.nick, wt, parent_tree)
        window.set_file(wt.relpath(self.path + os.sep + self.selected))
        window.show()
    
    def bookmark(self, action):
        """ Right context menu -> Bookmark """
        if self.pref.add_bookmark(self.path):
            info_dialog(_i18n('Bookmark successfully added'),
                        _i18n('The current directory was bookmarked. You can reach\nit by selecting it from the left panel.'))
            self.pref.write()
        else:
            warning_dialog(_i18n('Location already bookmarked'),
                           _i18n('The current directory is already bookmarked.\nSee the left panel for reference.'))
        
        self.app.refresh_left()

    def edit_bookmark(self, action):
        """ Left context menu -> Edit """
        from bookmark import BookmarkDialog
        
        if self.selected != None:
            bookmark = BookmarkDialog(self.selected, self.app.window)
            response = bookmark.run()
            
            if response != gtk.RESPONSE_NONE:
                bookmark.hide()
        
                if response == gtk.RESPONSE_OK:
                    self.app.refresh_left()
            
                bookmark.destroy()

    def remove_bookmark(self, action):
        """ Left context menu -> Remove """
        
        if self.selected != None:
            self.pref.remove_bookmark(self.selected)
            self.pref.write()
        
        self.app.refresh_left()
    
    def open_folder(self, action):
        """ Left context menu -> Open Folder """
        path = self.selected

        if path != None:
            launch(path)
    
    def diff_selected(self, action):
        """ Diff toolbutton -> Selected... """
        print "DEBUG: not implemented."
    
    def diff_all(self, action):
        """ Diff toolbutton -> All... """
        from diff import OliveDiff
        diff = OliveDiff(self.comm)
        diff.display()
    
    def view_remote(self, action):
        """ Remote context menu -> View contents """
        print "DEBUG: view contents."
    
    def diff_remote(self, action):
        """ Remote context menu -> Show differences """
        print "DEBUG: show differences."
    
    def revert_remote(self, action):
        """ Remote context menu -> Revert to this revision """
        print "DEBUG: revert to this revision."