File: conflicts.py

package info (click to toggle)
bzr-gtk 0.94.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,528 kB
  • ctags: 902
  • sloc: python: 9,783; sh: 32; makefile: 8
file content (178 lines) | stat: -rw-r--r-- 7,500 bytes parent folder | download
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
# 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

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

import subprocess

import gtk
import gobject

from bzrlib.config import GlobalConfig

from dialog import error_dialog, warning_dialog
from errors import show_bzr_error

class ConflictsDialog(gtk.Dialog):
    """ This dialog displays the list of conflicts. """
    def __init__(self, wt, parent=None):
        """ Initialize the Conflicts dialog. """
        gtk.Dialog.__init__(self, title="Conflicts - Olive",
                                  parent=parent,
                                  flags=0,
                                  buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CANCEL))
        
        # Get arguments
        self.wt = wt
        
        # Create the widgets
        self._scrolledwindow = gtk.ScrolledWindow()
        self._treeview = gtk.TreeView()
        self._label_diff3 = gtk.Label(_("External utility:"))
        self._entry_diff3 = gtk.Entry()
        self._image_diff3 = gtk.Image()
        self._button_diff3 = gtk.Button()
        self._hbox_diff3 = gtk.HBox()
        
        # Set callbacks
        self._button_diff3.connect('clicked', self._on_diff3_clicked)
        
        # Set properties
        self._scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
                                        gtk.POLICY_AUTOMATIC)
        self._image_diff3.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_BUTTON)
        self._button_diff3.set_image(self._image_diff3)
        self._entry_diff3.set_text(self._get_diff3())
        self._hbox_diff3.set_spacing(3)
        self.vbox.set_spacing(3)
        self.set_default_size(400, 300)
        
        # Construct dialog
        self._hbox_diff3.pack_start(self._label_diff3, False, False)
        self._hbox_diff3.pack_start(self._entry_diff3, True, True)
        self._hbox_diff3.pack_start(self._button_diff3, False, False)
        self._scrolledwindow.add(self._treeview)
        self.vbox.pack_start(self._scrolledwindow, True, True)
        self.vbox.pack_start(self._hbox_diff3, False, False)
        
        # Create the conflict list
        self._create_conflicts()
        
        # Show the dialog
        self.vbox.show_all()
    
    def _get_diff3(self):
        """ Get the specified diff3 utility. Default is meld. """
        config = GlobalConfig()
        diff3 = config.get_user_option('gconflicts_diff3')
        if diff3 is None:
            diff3 = 'meld'
        return diff3
    
    def _set_diff3(self, cmd):
        """ Set the default diff3 utility to cmd. """
        config = GlobalConfig()
        config.set_user_option('gconflicts_diff3', cmd)
    
    def _create_conflicts(self):
        """ Construct the list of conflicts. """
        if len(self.wt.conflicts()) == 0:
            self.model = gtk.ListStore(gobject.TYPE_STRING)
            self._treeview.set_model(self.model)
            self._treeview.append_column(gtk.TreeViewColumn(_('Conflicts'),
                                         gtk.CellRendererText(), text=0))
            self._treeview.set_headers_visible(False)            
            self.model.append([ _("No conflicts in working tree.") ])
            self._button_diff3.set_sensitive(False)
        else:
            self.model = gtk.ListStore(gobject.TYPE_STRING,
                                       gobject.TYPE_STRING,
                                       gobject.TYPE_STRING)
            self._treeview.set_model(self.model)
            self._treeview.append_column(gtk.TreeViewColumn(_('Path'),
                                         gtk.CellRendererText(), text=0))
            self._treeview.append_column(gtk.TreeViewColumn(_('Type'),
                                         gtk.CellRendererText(), text=1))
            self._treeview.set_search_column(0)
            for conflict in self.wt.conflicts():
                if conflict.typestring == 'path conflict':
                    t = _("path conflict")
                elif conflict.typestring == 'contents conflict':
                    t = _("contents conflict")
                elif conflict.typestring == 'text conflict':
                    t = _("text conflict")
                elif conflict.typestring == 'duplicate id':
                    t = _("duplicate id")
                elif conflict.typestring == 'duplicate':
                    t = _("duplicate")
                elif conflict.typestring == 'parent loop':
                    t = _("parent loop")
                elif conflict.typestring == 'unversioned parent':
                    t = _("unversioned parent")
                elif conflict.typestring == 'missing parent':
                    t = _("missing parent")
                elif conflict.typestring == 'deleting parent':
                    t = _("deleting parent")
                else:
                    t = _("unknown type of conflict")
                
                self.model.append([ conflict.path, t, conflict.typestring ]) 
    
    def _get_selected_file(self):
        """ Return the selected conflict's filename. """
        treeselection = self._treeview.get_selection()
        (model, iter) = treeselection.get_selected()
        
        if iter is None:
            return None
        else:
            return model.get_value(iter, 0)
    
    def _get_selected_type(self):
        """ Return the type of the selected conflict. """
        treeselection = self._treeview.get_selection()
        (model, iter) = treeselection.get_selected()
        
        if iter is None:
            return None
        else:
            return model.get_value(iter, 2)
    
    def _on_diff3_clicked(self, widget):
        """ Launch external utility to resolve conflicts. """
        self._set_diff3(self._entry_diff3.get_text())
        selected = self._get_selected_file()
        if selected is None:
            error_dialog(_('No file was selected'),
                         _('Please select a file from the list.'))
            return
        elif self._get_selected_type() == 'text conflict':
            base = self.wt.abspath(selected) + '.BASE'
            this = self.wt.abspath(selected) + '.THIS'
            other = self.wt.abspath(selected) + '.OTHER'
            try:
                p = subprocess.Popen([ self._entry_diff3.get_text(), base, this, other ])
                p.wait()
            except OSError, e:
                warning_dialog(_('Call to external utility failed'), str(e))
        else:
            warning_dialog(_('Cannot resolve conflict'),
                           _('Only conflicts on the text of files can be resolved with Olive at the moment. Content conflicts, on the structure of the tree, need to be resolved using the command line.'))
            return