/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 commit.py

  • Committer: Gary van der Merwe
  • Date: 2007-08-10 10:45:06 UTC
  • mto: This revision was merged to the branch mainline in revision 256.
  • Revision ID: garyvdm@gmail.com-20070810104506-wo2mp9zfkh338axe
Make icon locations consistant between source and installed version. Let glade nkow where to find the icons with a project file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    pass
22
22
 
23
23
import gtk
24
 
import gtk.glade
25
24
import gobject
26
25
import pango
27
26
 
28
 
import bzrlib.errors as errors
29
 
from bzrlib import osutils
 
27
import os.path
 
28
 
 
29
from bzrlib import errors, osutils
 
30
from bzrlib.trace import mutter
30
31
 
31
32
from dialog import error_dialog, question_dialog
32
 
from guifiles import GLADEFILENAME
33
 
 
34
 
 
35
 
class CommitDialog:
36
 
    """ Display Commit dialog and perform the needed actions. """
37
 
    def __init__(self, wt, wtpath, notbranch):
38
 
        """ Initialize the Commit dialog.
39
 
        :param  wt:         bzr working tree object
40
 
        :param  wtpath:     path to working tree root
41
 
        :param  notbranch:  flag that path is not a brach
42
 
        :type   notbranch:  bool
43
 
        """
44
 
        self.glade = gtk.glade.XML(GLADEFILENAME, 'window_commit', 'olive-gtk')
 
33
from errors import show_bzr_error
 
34
 
 
35
try:
 
36
    import dbus
 
37
    import dbus.glib
 
38
    have_dbus = True
 
39
except ImportError:
 
40
    have_dbus = False
 
41
 
 
42
class CommitDialog(gtk.Dialog):
 
43
    """ New implementation of the Commit dialog. """
 
44
    def __init__(self, wt, wtpath, notbranch, selected=None, parent=None):
 
45
        """ Initialize the Commit Dialog. """
 
46
        gtk.Dialog.__init__(self, title="Commit - Olive",
 
47
                                  parent=parent,
 
48
                                  flags=0,
 
49
                                  buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
45
50
        
 
51
        # Get arguments
46
52
        self.wt = wt
47
53
        self.wtpath = wtpath
48
54
        self.notbranch = notbranch
49
 
 
50
 
        # Get some important widgets
51
 
        self.window = self.glade.get_widget('window_commit')
52
 
        self.checkbutton_local = self.glade.get_widget('checkbutton_commit_local')
53
 
        self.textview = self.glade.get_widget('textview_commit')
54
 
        self.file_expander = self.glade.get_widget('expander_commit_select')
55
 
        self.file_view = self.glade.get_widget('treeview_commit_select')
56
 
        self.pending_expander = self.glade.get_widget('expander_commit_pending')
57
 
        self.pending_label = self.glade.get_widget('label_commit_pending')
58
 
        self.pending_view = self.glade.get_widget('treeview_commit_pending')
59
 
 
60
 
        if wt is None or notbranch:
61
 
            return
 
55
        self.selected = selected
62
56
        
63
57
        # Set the delta
64
58
        self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
67
61
        # Get pending merges
68
62
        self.pending = self._pending_merges(self.wt)
69
63
        
70
 
        # Dictionary for signal_autoconnect
71
 
        dic = { "on_button_commit_commit_clicked": self.commit,
72
 
                "on_button_commit_cancel_clicked": self.close }
73
 
 
74
 
        # Connect the signals to the handlers
75
 
        self.glade.signal_autoconnect(dic)
76
 
        
77
 
        # Create the file list
78
 
        self._create_file_view()
79
 
        # Create the pending merges
80
 
        self._create_pending_merges()
81
 
    
82
 
    def display(self):
83
 
        """ Display the Push dialog.
84
 
        @return:    True if dialog is shown.
85
 
        """
 
64
        # Do some preliminary checks
 
65
        self._is_checkout = False
 
66
        self._is_pending = False
86
67
        if self.wt is None and not self.notbranch:
87
68
            error_dialog(_('Directory does not have a working tree'),
88
69
                         _('Operation aborted.'))
89
70
            self.close()
90
 
            dialog_shown = False
 
71
            return
 
72
 
91
73
        if self.notbranch:
92
74
            error_dialog(_('Directory is not a branch'),
93
75
                         _('You can perform this action only in a branch.'))
94
76
            self.close()
95
 
            dialog_shown = False
 
77
            return
96
78
        else:
97
79
            if self.wt.branch.get_bound_location() is not None:
98
80
                # we have a checkout, so the local commit checkbox must appear
99
 
                self.checkbutton_local.show()
 
81
                self._is_checkout = True
100
82
            
101
83
            if self.pending:
102
84
                # There are pending merges, file selection not supported
103
 
                self.file_expander.set_expanded(False)
104
 
                self.file_view.set_sensitive(False)
105
 
            else:
106
 
                # No pending merges
107
 
                self.pending_expander.hide()
108
 
            
109
 
            self.textview.modify_font(pango.FontDescription("Monospace"))
110
 
            self.window.show()
111
 
            dialog_shown = True
112
 
        if dialog_shown:
113
 
            # Gives the focus to the commit message area
114
 
            self.textview.grab_focus()
115
 
        return dialog_shown
116
 
    
117
 
    def _create_file_view(self):
118
 
        self.file_store = gtk.ListStore(gobject.TYPE_BOOLEAN,   # [0] checkbox
119
 
                                        gobject.TYPE_STRING,    # [1] path to display
120
 
                                        gobject.TYPE_STRING,    # [2] changes type
121
 
                                        gobject.TYPE_STRING)    # [3] real path
122
 
        self.file_view.set_model(self.file_store)
123
 
        crt = gtk.CellRendererToggle()
124
 
        crt.set_property("activatable", True)
125
 
        crt.connect("toggled", self._toggle_commit, self.file_store)
126
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Commit'),
127
 
                                     crt, active=0))
128
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Path'),
129
 
                                     gtk.CellRendererText(), text=1))
130
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Type'),
131
 
                                     gtk.CellRendererText(), text=2))
132
 
 
133
 
        for path, id, kind in self.delta.added:
134
 
            marker = osutils.kind_marker(kind)
135
 
            self.file_store.append([ True, path+marker, _('added'), path ])
136
 
 
137
 
        for path, id, kind in self.delta.removed:
138
 
            marker = osutils.kind_marker(kind)
139
 
            self.file_store.append([ True, path+marker, _('removed'), path ])
140
 
 
141
 
        for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
142
 
            marker = osutils.kind_marker(kind)
143
 
            if text_modified or meta_modified:
144
 
                changes = _('renamed and modified')
145
 
            else:
146
 
                changes = _('renamed')
147
 
            self.file_store.append([ True,
148
 
                                     oldpath+marker + '  =>  ' + newpath+marker,
149
 
                                     changes,
150
 
                                     newpath
151
 
                                   ])
152
 
 
153
 
        for path, id, kind, text_modified, meta_modified in self.delta.modified:
154
 
            marker = osutils.kind_marker(kind)
155
 
            self.file_store.append([ True, path+marker, _('modified'), path ])
156
 
    
157
 
    def _create_pending_merges(self):
158
 
        liststore = gtk.ListStore(gobject.TYPE_STRING,
159
 
                                  gobject.TYPE_STRING,
160
 
                                  gobject.TYPE_STRING)
161
 
        self.pending_view.set_model(liststore)
162
 
        
163
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Date'),
164
 
                                        gtk.CellRendererText(), text=0))
165
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Committer'),
166
 
                                        gtk.CellRendererText(), text=1))
167
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Summary'),
168
 
                                        gtk.CellRendererText(), text=2))
 
85
                self._is_pending = True
 
86
        
 
87
        # Create the widgets
 
88
        self._button_commit = gtk.Button(_("Comm_it"), use_underline=True)
 
89
        self._expander_files = gtk.Expander(_("File(s) to commit"))
 
90
        self._vpaned_main = gtk.VPaned()
 
91
        self._scrolledwindow_files = gtk.ScrolledWindow()
 
92
        self._scrolledwindow_message = gtk.ScrolledWindow()
 
93
        self._treeview_files = gtk.TreeView()
 
94
        self._vbox_message = gtk.VBox()
 
95
        self._label_message = gtk.Label(_("Commit message:"))
 
96
        self._textview_message = gtk.TextView()
 
97
        
 
98
        if self._is_pending:
 
99
            self._expander_merges = gtk.Expander(_("Pending merges"))
 
100
            self._vpaned_list = gtk.VPaned()
 
101
            self._scrolledwindow_merges = gtk.ScrolledWindow()
 
102
            self._treeview_merges = gtk.TreeView()
 
103
 
 
104
        # Set callbacks
 
105
        self._button_commit.connect('clicked', self._on_commit_clicked)
 
106
        self._treeview_files.connect('row_activated', self._on_treeview_files_row_activated)
 
107
        
 
108
        # Set properties
 
109
        self._scrolledwindow_files.set_policy(gtk.POLICY_AUTOMATIC,
 
110
                                              gtk.POLICY_AUTOMATIC)
 
111
        self._scrolledwindow_message.set_policy(gtk.POLICY_AUTOMATIC,
 
112
                                                gtk.POLICY_AUTOMATIC)
 
113
        self._textview_message.modify_font(pango.FontDescription("Monospace"))
 
114
        self.set_default_size(500, 500)
 
115
        self._vpaned_main.set_position(200)
 
116
        self._button_commit.set_flags(gtk.CAN_DEFAULT)
 
117
 
 
118
        if self._is_pending:
 
119
            self._scrolledwindow_merges.set_policy(gtk.POLICY_AUTOMATIC,
 
120
                                                   gtk.POLICY_AUTOMATIC)
 
121
            self._treeview_files.set_sensitive(False)
 
122
        
 
123
        # Construct the dialog
 
124
        self.action_area.pack_end(self._button_commit)
 
125
        
 
126
        self._scrolledwindow_files.add(self._treeview_files)
 
127
        self._scrolledwindow_message.add(self._textview_message)
 
128
        
 
129
        self._expander_files.add(self._scrolledwindow_files)
 
130
        
 
131
        self._vbox_message.pack_start(self._label_message, False, False)
 
132
        self._vbox_message.pack_start(self._scrolledwindow_message, True, True)
 
133
        
 
134
        if self._is_pending:        
 
135
            self._expander_merges.add(self._scrolledwindow_merges)
 
136
            self._scrolledwindow_merges.add(self._treeview_merges)
 
137
            self._vpaned_list.add1(self._expander_files)
 
138
            self._vpaned_list.add2(self._expander_merges)
 
139
            self._vpaned_main.add1(self._vpaned_list)
 
140
        else:
 
141
            self._vpaned_main.add1(self._expander_files)
 
142
 
 
143
        self._vpaned_main.add2(self._vbox_message)
 
144
        
 
145
        self.vbox.pack_start(self._vpaned_main, True, True)
 
146
        if self._is_checkout: 
 
147
            self._check_local = gtk.CheckButton(_("_Only commit locally"),
 
148
                                                use_underline=True)
 
149
            self.vbox.pack_start(self._check_local, False, False)
 
150
            if have_dbus:
 
151
                bus = dbus.SystemBus()
 
152
                proxy_obj = bus.get_object('org.freedesktop.NetworkManager', 
 
153
                              '/org/freedesktop/NetworkManager')
 
154
                dbus_iface = dbus.Interface(
 
155
                        proxy_obj, 'org.freedesktop.NetworkManager')
 
156
                try:
 
157
                    # 3 is the enum value for STATE_CONNECTED
 
158
                    self._check_local.set_active(dbus_iface.state() != 3)
 
159
                except dbus.DBusException, e:
 
160
                    # Silently drop errors. While DBus may be 
 
161
                    # available, NetworkManager doesn't necessarily have to be
 
162
                    mutter("unable to get networkmanager state: %r" % e)
 
163
                
 
164
        # Create the file list
 
165
        self._create_file_view()
 
166
        # Create the pending merges
 
167
        self._create_pending_merges()
 
168
        
 
169
        # Expand the corresponding expander
 
170
        if self._is_pending:
 
171
            self._expander_merges.set_expanded(True)
 
172
        else:
 
173
            self._expander_files.set_expanded(True)
 
174
        
 
175
        # Display dialog
 
176
        self.vbox.show_all()
 
177
        
 
178
        # Default to Commit button
 
179
        self._button_commit.grab_default()
 
180
    
 
181
    def _on_treeview_files_row_activated(self, treeview, path, view_column):
 
182
        # FIXME: the diff window freezes for some reason
 
183
        treeselection = treeview.get_selection()
 
184
        (model, iter) = treeselection.get_selected()
 
185
        
 
186
        if iter is not None:
 
187
            from diff import DiffWindow
 
188
            
 
189
            _selected = model.get_value(iter, 1)
 
190
            
 
191
            diff = DiffWindow()
 
192
            diff.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
 
193
            diff.set_modal(True)
 
194
            parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
195
            diff.set_diff(self.wt.branch.nick, self.wt, parent_tree)
 
196
            try:
 
197
                diff.set_file(_selected)
 
198
            except errors.NoSuchFile:
 
199
                pass
 
200
            diff.show()
 
201
    
 
202
    @show_bzr_error
 
203
    def _on_commit_clicked(self, button):
 
204
        """ Commit button clicked handler. """
 
205
        textbuffer = self._textview_message.get_buffer()
 
206
        start, end = textbuffer.get_bounds()
 
207
        message = textbuffer.get_text(start, end).decode('utf-8')
169
208
        
170
209
        if not self.pending:
171
 
            return
 
210
            specific_files = self._get_specific_files()
 
211
        else:
 
212
            specific_files = None
 
213
 
 
214
        if message == '':
 
215
            response = question_dialog(_('Commit with an empty message?'),
 
216
                                       _('You can describe your commit intent in the message.'))
 
217
            if response == gtk.RESPONSE_NO:
 
218
                # Kindly give focus to message area
 
219
                self._textview_message.grab_focus()
 
220
                return
 
221
 
 
222
        if self._is_checkout:
 
223
            local = self._check_local.get_active()
 
224
        else:
 
225
            local = False
 
226
 
 
227
        if list(self.wt.unknowns()) != []:
 
228
            response = question_dialog(_("Commit with unknowns?"),
 
229
               _("Unknown files exist in the working tree. Commit anyway?"))
 
230
            if response == gtk.RESPONSE_NO:
 
231
                return
172
232
        
173
 
        for item in self.pending:
174
 
            liststore.append([ item['date'],
175
 
                               item['committer'],
176
 
                               item['summary'] ])
177
 
    
178
 
    def _get_specific_files(self):
179
 
        ret = []
180
 
        it = self.file_store.get_iter_first()
181
 
        while it:
182
 
            if self.file_store.get_value(it, 0):
183
 
                # get real path from hidden column 3
184
 
                ret.append(self.file_store.get_value(it, 3))
185
 
            it = self.file_store.iter_next(it)
 
233
        try:
 
234
            self.wt.commit(message,
 
235
                       allow_pointless=False,
 
236
                       strict=False,
 
237
                       local=local,
 
238
                       specific_files=specific_files)
 
239
        except errors.PointlessCommit:
 
240
            response = question_dialog(_('Commit with no changes?'),
 
241
                                       _('There are no changes in the working tree.'))
 
242
            if response == gtk.RESPONSE_YES:
 
243
                self.wt.commit(message,
 
244
                               allow_pointless=True,
 
245
                               strict=False,
 
246
                               local=local,
 
247
                               specific_files=specific_files)
 
248
        self.response(gtk.RESPONSE_OK)
186
249
 
187
 
        return ret
188
 
    
189
 
    def _toggle_commit(self, cell, path, model):
190
 
        model[path][0] = not model[path][0]
191
 
        return
192
 
    
193
250
    def _pending_merges(self, wt):
194
251
        """ Return a list of pending merges or None if there are none of them. """
195
252
        parents = wt.get_parent_ids()
254
311
        
255
312
        return pm
256
313
 
257
 
    def commit(self, widget):
258
 
        textbuffer = self.textview.get_buffer()
259
 
        start, end = textbuffer.get_bounds()
260
 
        message = textbuffer.get_text(start, end).decode('utf-8')
261
 
        
262
 
        checkbutton_strict = self.glade.get_widget('checkbutton_commit_strict')
263
 
        checkbutton_force = self.glade.get_widget('checkbutton_commit_force')
264
 
        
 
314
    def _create_file_view(self):
 
315
        self._file_store = gtk.ListStore(gobject.TYPE_BOOLEAN,   # [0] checkbox
 
316
                                         gobject.TYPE_STRING,    # [1] path to display
 
317
                                         gobject.TYPE_STRING,    # [2] changes type
 
318
                                         gobject.TYPE_STRING)    # [3] real path
 
319
        self._treeview_files.set_model(self._file_store)
 
320
        crt = gtk.CellRendererToggle()
 
321
        crt.set_property("activatable", True)
 
322
        crt.connect("toggled", self._toggle_commit, self._file_store)
 
323
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Commit'),
 
324
                                     crt, active=0))
 
325
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Path'),
 
326
                                     gtk.CellRendererText(), text=1))
 
327
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Type'),
 
328
                                     gtk.CellRendererText(), text=2))
 
329
 
 
330
        for path, id, kind in self.delta.added:
 
331
            marker = osutils.kind_marker(kind)
 
332
            if self.selected is not None:
 
333
                if path == os.path.join(self.wtpath, self.selected):
 
334
                    self._file_store.append([ True, path+marker, _('added'), path ])
 
335
                else:
 
336
                    self._file_store.append([ False, path+marker, _('added'), path ])
 
337
            else:
 
338
                self._file_store.append([ True, path+marker, _('added'), path ])
 
339
 
 
340
        for path, id, kind in self.delta.removed:
 
341
            marker = osutils.kind_marker(kind)
 
342
            if self.selected is not None:
 
343
                if path == os.path.join(self.wtpath, self.selected):
 
344
                    self._file_store.append([ True, path+marker, _('removed'), path ])
 
345
                else:
 
346
                    self._file_store.append([ False, path+marker, _('removed'), path ])
 
347
            else:
 
348
                self._file_store.append([ True, path+marker, _('removed'), path ])
 
349
 
 
350
        for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
 
351
            marker = osutils.kind_marker(kind)
 
352
            if text_modified or meta_modified:
 
353
                changes = _('renamed and modified')
 
354
            else:
 
355
                changes = _('renamed')
 
356
            if self.selected is not None:
 
357
                if newpath == os.path.join(self.wtpath, self.selected):
 
358
                    self._file_store.append([ True,
 
359
                                              oldpath+marker + '  =>  ' + newpath+marker,
 
360
                                              changes,
 
361
                                              newpath
 
362
                                            ])
 
363
                else:
 
364
                    self._file_store.append([ False,
 
365
                                              oldpath+marker + '  =>  ' + newpath+marker,
 
366
                                              changes,
 
367
                                              newpath
 
368
                                            ])
 
369
            else:
 
370
                self._file_store.append([ True,
 
371
                                          oldpath+marker + '  =>  ' + newpath+marker,
 
372
                                          changes,
 
373
                                          newpath
 
374
                                        ])
 
375
 
 
376
        for path, id, kind, text_modified, meta_modified in self.delta.modified:
 
377
            marker = osutils.kind_marker(kind)
 
378
            if self.selected is not None:
 
379
                if path == os.path.join(self.wtpath, self.selected):
 
380
                    self._file_store.append([ True, path+marker, _('modified'), path ])
 
381
                else:
 
382
                    self._file_store.append([ False, path+marker, _('modified'), path ])
 
383
            else:
 
384
                self._file_store.append([ True, path+marker, _('modified'), path ])
 
385
    
 
386
    def _create_pending_merges(self):
265
387
        if not self.pending:
266
 
            specific_files = self._get_specific_files()
267
 
        else:
268
 
            specific_files = None
269
 
 
270
 
        if message == '':
271
 
            response = question_dialog('Commit with an empty message ?',
272
 
                                       'You can describe your commit intent'
273
 
                                       +' in the message')
274
 
            if response == gtk.RESPONSE_NO:
275
 
                # Kindly give focus to message area
276
 
                self.textview.grab_focus()
277
 
                return
278
 
 
279
 
        try:
280
 
            self.wt.commit(message,
281
 
                           allow_pointless=checkbutton_force.get_active(),
282
 
                           strict=checkbutton_strict.get_active(),
283
 
                           local=self.checkbutton_local.get_active(),
284
 
                           specific_files=specific_files)
285
 
        except errors.NotBranchError:
286
 
            error_dialog(_('Directory is not a branch'),
287
 
                         _('You can perform this action only in a branch.'))
288
 
            return
289
 
        except errors.LocalRequiresBoundBranch:
290
 
            error_dialog(_('Directory is not a checkout'),
291
 
                         _('You can perform local commit only on checkouts.'))
292
 
            return
293
 
        except errors.PointlessCommit:
294
 
            error_dialog(_('No changes to commit'),
295
 
                         _('Try force commit if you want to commit anyway.'))
296
 
            return
297
 
        except errors.ConflictsInTree:
298
 
            error_dialog(_('Conflicts in tree'),
299
 
                         _('You need to resolve the conflicts before committing.'))
300
 
            return
301
 
        except errors.StrictCommitFailed:
302
 
            error_dialog(_('Strict commit failed'),
303
 
                         _('There are unknown files in the working tree.\nPlease add or delete them.'))
304
 
            return
305
 
        except errors.BoundBranchOutOfDate, errmsg:
306
 
            error_dialog(_('Bound branch is out of date'),
307
 
                         _('%s') % errmsg)
308
 
            return
309
 
        except errors.BzrError, msg:
310
 
            error_dialog(_('Unknown bzr error'), str(msg))
311
 
            return
312
 
        except Exception, msg:
313
 
            error_dialog(_('Unknown error'), str(msg))
314
 
            return
315
 
 
316
 
        self.close()
317
 
 
318
 
    def close(self, widget=None):
319
 
        self.window.destroy()
 
388
            return
 
389
        
 
390
        liststore = gtk.ListStore(gobject.TYPE_STRING,
 
391
                                  gobject.TYPE_STRING,
 
392
                                  gobject.TYPE_STRING)
 
393
        self._treeview_merges.set_model(liststore)
 
394
        
 
395
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Date'),
 
396
                                            gtk.CellRendererText(), text=0))
 
397
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Committer'),
 
398
                                            gtk.CellRendererText(), text=1))
 
399
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Summary'),
 
400
                                            gtk.CellRendererText(), text=2))
 
401
        
 
402
        for item in self.pending:
 
403
            liststore.append([ item['date'],
 
404
                               item['committer'],
 
405
                               item['summary'] ])
 
406
    
 
407
    def _get_specific_files(self):
 
408
        ret = []
 
409
        it = self._file_store.get_iter_first()
 
410
        while it:
 
411
            if self._file_store.get_value(it, 0):
 
412
                # get real path from hidden column 3
 
413
                ret.append(self._file_store.get_value(it, 3))
 
414
            it = self._file_store.iter_next(it)
 
415
 
 
416
        return ret
 
417
    
 
418
    def _toggle_commit(self, cell, path, model):
 
419
        model[path][0] = not model[path][0]
 
420
        return