1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
 
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.
 
 
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.
 
 
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
 
 
30
import bzrlib.errors as errors
 
 
31
from bzrlib.workingtree import WorkingTree
 
 
33
from bzrlib.plugins.gtk import _i18n
 
 
34
from bzrlib.plugins.gtk.dialog import error_dialog, info_dialog, question_dialog, warning_dialog
 
 
35
from bzrlib.plugins.gtk.errors import show_bzr_error
 
 
36
from bzrlib.plugins.gtk.annotate.gannotate import GAnnotateWindow
 
 
37
from bzrlib.plugins.gtk.annotate.config import GAnnotateConfig
 
 
38
from bzrlib.plugins.gtk.diff import DiffWindow
 
 
39
from launch import launch
 
 
40
from bzrlib.plugins.gtk.olive import Preferences
 
 
43
    """ This class is responsible for building the context menus. """
 
 
44
    def __init__(self, path, selected, app=None):
 
 
46
        from guifiles import UIFILENAME
 
 
48
        self.uifile = UIFILENAME
 
 
51
        self.pref = Preferences()
 
 
55
        self.selected = selected
 
 
58
        # Create the file list context menu
 
 
59
        self.ui = gtk.UIManager()
 
 
61
        self.actiongroup = gtk.ActionGroup('context')
 
 
62
        self.actiongroup.add_actions([('add', gtk.STOCK_ADD,
 
 
64
                                       _i18n('Add the selected file'),
 
 
66
                                      ('remove', gtk.STOCK_REMOVE,
 
 
67
                                       _i18n('Remove'), None,
 
 
68
                                       _i18n('Remove the selected file'),
 
 
70
                                      ('remove_and_delete', gtk.STOCK_REMOVE,
 
 
71
                                       _i18n('Remove and delete'), None,
 
 
72
                                       _i18n('Remove the selected file/dir and delete from disk'),
 
 
73
                                       self.remove_and_delete_file),
 
 
75
                                       _i18n('Rename'), None,
 
 
76
                                       _i18n('Rename the selected file'),
 
 
78
                                      ('open', gtk.STOCK_OPEN,
 
 
80
                                       _i18n('Open the selected file'),
 
 
83
                                       _i18n('Revert'), None,
 
 
84
                                       _i18n('Revert the changes'),
 
 
87
                                       _i18n('Commit'), None,
 
 
88
                                       _i18n('Commit the changes'),
 
 
91
                                       _i18n('Annotate'), None,
 
 
92
                                       _i18n('Annotate the selected file'),
 
 
96
                                       _i18n('Show the diff of the file'),
 
 
99
                                       _i18n('Bookmark'), None,
 
 
100
                                       _i18n('Bookmark current location'),
 
 
102
                                      ('edit_bookmark', gtk.STOCK_EDIT,
 
 
104
                                       _i18n('Edit the selected bookmark'),
 
 
106
                                      ('remove_bookmark', gtk.STOCK_REMOVE,
 
 
107
                                       _i18n('Remove'), None,
 
 
108
                                       _i18n('Remove the selected bookmark'),
 
 
109
                                       self.remove_bookmark),
 
 
110
                                      ('open_folder', gtk.STOCK_OPEN,
 
 
111
                                       _i18n('Open Folder'), None,
 
 
112
                                       _i18n('Open bookmark folder in Nautilus'),
 
 
114
                                      ('diff_selected', None,
 
 
115
                                       _i18n('Selected...'), None,
 
 
116
                                       _i18n('Show the differences of the selected file'),
 
 
119
                                       _i18n('All...'), None,
 
 
120
                                       _i18n('Show the differences of all files'),
 
 
122
                                      ('view_remote', None,
 
 
123
                                       _i18n('View contents'), None,
 
 
124
                                       _i18n('View the contents of the file in a builtin viewer'),
 
 
126
                                      ('diff_remote', None,
 
 
127
                                       _i18n('Show differences'), None,
 
 
128
                                       _i18n('Show the differences between two revisions of the file'),
 
 
130
                                      ('revert_remote', None,
 
 
131
                                       _i18n('Revert to this revision'), None,
 
 
132
                                       _i18n('Revert the selected file to the selected revision'),
 
 
136
        self.ui.insert_action_group(self.actiongroup, 0)
 
 
137
        self.ui.add_ui_from_file(self.uifile)
 
 
139
        self.cmenu_right = self.ui.get_widget('/context_right')
 
 
140
        self.cmenu_left = self.ui.get_widget('/context_left')
 
 
141
        self.toolbar_diff = self.ui.get_widget('/toolbar_diff')
 
 
142
        self.cmenu_remote = self.ui.get_widget('/context_remote')
 
 
145
        # TODO: do it without using deprecated comm
 
 
146
        #commit_menu = self.ui.get_widget('/context_right/commit')
 
 
147
        #commit_image = self.comm.menuitem_branch_commit.get_image()
 
 
148
        #commit_pixbuf = commit_image.get_pixbuf()
 
 
149
        #commit_icon = gtk.Image()
 
 
150
        #commit_icon.set_from_pixbuf(commit_pixbuf)
 
 
151
        #commit_menu.set_image(commit_icon)
 
 
152
        #diff_menu = self.ui.get_widget('/context_right/diff')
 
 
153
        #diff_image = self.comm.menuitem_stats_diff.get_image()
 
 
154
        #diff_pixbuf = diff_image.get_pixbuf()
 
 
155
        #diff_icon = gtk.Image()
 
 
156
        #diff_icon.set_from_pixbuf(diff_pixbuf)
 
 
157
        #diff_menu.set_image(diff_icon)
 
 
159
    def right_context_menu(self):
 
 
160
        return self.cmenu_right
 
 
162
    def left_context_menu(self):
 
 
163
        return self.cmenu_left
 
 
165
    def remote_context_menu(self):
 
 
166
        return self.cmenu_remote
 
 
169
    def add_file(self, action):
 
 
170
        """ Right context menu -> Add """
 
 
173
        # Add only the selected file
 
 
174
        directory = self.path
 
 
175
        filename = self.selected
 
 
178
            error_dialog(_i18n('No file was selected'),
 
 
179
                         _i18n('Please select a file from the list,\nor choose the other option.'))
 
 
182
        bzrlib.add.smart_add([os.path.join(directory, filename)])
 
 
185
    def annotate(self, action):
 
 
186
        """ Right context menu -> Annotate """
 
 
187
        directory = self.path
 
 
188
        filename = self.selected
 
 
191
            error_dialog(_i18n('No file was selected'),
 
 
192
                         _i18n('Please select a file from the list.'))
 
 
195
        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
 
 
198
        file_id = wt.path2id(wt.relpath(os.path.join(directory, filename)))
 
 
200
        window = GAnnotateWindow(all=False, plain=False, parent=self.app)
 
 
201
        window.set_title(os.path.join(directory, filename) + " - Annotate")
 
 
202
        config = GAnnotateConfig(window)
 
 
206
            window.annotate(wt, branch, file_id)
 
 
211
    def remove_file(self, action,delete_on_disk=0):
 
 
212
        """ Right context menu -> Remove """
 
 
213
        # Remove only the selected file
 
 
214
        directory = self.path
 
 
215
        filename = self.selected
 
 
218
            error_dialog(_i18n('No file was selected'),
 
 
219
                         _i18n('Please select a file from the list,\nor choose the other option.'))
 
 
222
        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
 
 
226
            abs_filename = os.path.join(directory,filename)
 
 
227
            if os.path.isdir(abs_filename):
 
 
228
                response = question_dialog(_i18n('Delete directory with all directories below ?'), abs_filename )
 
 
229
                if response == gtk.RESPONSE_YES:
 
 
230
                    shutil.rmtree(abs_filename)
 
 
232
                os.remove(abs_filename)
 
 
234
        self.app.set_path(self.path)
 
 
235
        self.app.refresh_right()
 
 
237
    def remove_and_delete_file(self, action):
 
 
238
        """ Right context menu -> Remove and delete"""
 
 
239
        self.remove_file(action,delete_on_disk=1)
 
 
241
    def rename_file(self, action):
 
 
242
        """ Right context menu -> Rename """
 
 
243
        from rename import OliveRename
 
 
244
        wt = WorkingTree.open_containing(self.path + os.sep + self.selected)[0]
 
 
245
        rename = OliveRename(wt, wt.relpath(self.path), self.selected)
 
 
248
    def open_file(self, action):
 
 
249
        """ Right context menu -> Open """
 
 
250
        # Open only the selected file
 
 
251
        filename = self.selected
 
 
254
            error_dialog(_i18n('No file was selected'),
 
 
255
                         _i18n('Please select a file from the list,\nor choose the other option.'))
 
 
259
            # TODO: how to enter a directory?
 
 
262
            fullpath = self.path + os.sep + filename
 
 
263
            if os.path.isdir(fullpath):
 
 
264
                # selected item is an existant directory
 
 
265
                # TODO: how to enter a directory?
 
 
270
    def revert(self, action):
 
 
271
        """ Right context menu -> Revert """
 
 
272
        wt, path = WorkingTree.open_containing(self.path)
 
 
273
        ret = wt.revert([os.path.join(path, self.selected)])
 
 
275
            warning_dialog(_i18n('Conflicts detected'),
 
 
276
                           _i18n('Please have a look at the working tree before continuing.'))
 
 
278
            info_dialog(_i18n('Revert successful'),
 
 
279
                        _i18n('All files reverted to last revision.'))
 
 
280
        self.app.refresh_right()       
 
 
282
    def commit(self, action):
 
 
283
        """ Right context menu -> Commit """
 
 
284
        from commit import CommitDialog
 
 
287
            wt, path = WorkingTree.open_containing(self.path)
 
 
289
        except NotBranchError, e:
 
 
293
            selected = os.path.join(path, self.selected)
 
 
296
        commit = CommitDialog(wt=wt, selected=selected, parent=None)
 
 
297
        response = commit.run()
 
 
298
        if response != gtk.RESPONSE_NONE:
 
 
301
            if response == gtk.RESPONSE_OK:
 
 
302
                self.app.refresh_right()
 
 
307
    def diff(self, action):
 
 
308
        """ Right context menu -> Diff """
 
 
309
        wt = WorkingTree.open_containing(self.path)[0]
 
 
310
        window = DiffWindow(self.app)
 
 
311
        parent_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
 
 
312
        window.set_diff(wt.branch.nick, wt, parent_tree)
 
 
313
        window.set_file(wt.relpath(self.path + os.sep + self.selected))
 
 
316
    def bookmark(self, action):
 
 
317
        """ Right context menu -> Bookmark """
 
 
318
        if self.pref.add_bookmark(self.path):
 
 
319
            info_dialog(_i18n('Bookmark successfully added'),
 
 
320
                        _i18n('The current directory was bookmarked. You can reach\nit by selecting it from the left panel.'))
 
 
323
            warning_dialog(_i18n('Location already bookmarked'),
 
 
324
                           _i18n('The current directory is already bookmarked.\nSee the left panel for reference.'))
 
 
326
        self.app.refresh_left()
 
 
328
    def edit_bookmark(self, action):
 
 
329
        """ Left context menu -> Edit """
 
 
330
        from bookmark import BookmarkDialog
 
 
332
        if self.selected != None:
 
 
333
            bookmark = BookmarkDialog(self.selected, self.app.window)
 
 
334
            response = bookmark.run()
 
 
336
            if response != gtk.RESPONSE_NONE:
 
 
339
                if response == gtk.RESPONSE_OK:
 
 
340
                    self.app.refresh_left()
 
 
344
    def remove_bookmark(self, action):
 
 
345
        """ Left context menu -> Remove """
 
 
347
        if self.selected != None:
 
 
348
            self.pref.remove_bookmark(self.selected)
 
 
351
        self.app.refresh_left()
 
 
353
    def open_folder(self, action):
 
 
354
        """ Left context menu -> Open Folder """
 
 
360
    def diff_selected(self, action):
 
 
361
        """ Diff toolbutton -> Selected... """
 
 
362
        print "DEBUG: not implemented."
 
 
364
    def diff_all(self, action):
 
 
365
        """ Diff toolbutton -> All... """
 
 
366
        from diff import OliveDiff
 
 
367
        diff = OliveDiff(self.comm)
 
 
370
    def view_remote(self, action):
 
 
371
        """ Remote context menu -> View contents """
 
 
372
        print "DEBUG: view contents."
 
 
374
    def diff_remote(self, action):
 
 
375
        """ Remote context menu -> Show differences """
 
 
376
        print "DEBUG: show differences."
 
 
378
    def revert_remote(self, action):
 
 
379
        """ Remote context menu -> Revert to this revision """
 
 
380
        print "DEBUG: revert to this revision."