1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
3
# Some parts of the code:
4
# Copyright (C) 2005 by Canonical Ltd.
5
# Author: Scott James Remnant <scott@ubuntu.com>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from cStringIO import StringIO
40
have_gtksourceview = True
42
have_gtksourceview = False
46
from bzrlib.diff import show_diff_trees
47
import bzrlib.errors as errors
48
from bzrlib.workingtree import WorkingTree
51
""" Display Diff window and perform the needed actions. """
52
def __init__(self, gladefile, comm):
53
""" Initialize the Diff window. """
54
self.gladefile = gladefile
55
self.glade = gtk.glade.XML(self.gladefile, 'window_diff', 'olive-gtk')
57
# Communication object
60
# Get some important widgets
61
self.window = self.glade.get_widget('window_diff')
62
self.treeview = self.glade.get_widget('treeview_diff_files')
64
# Check if current location is a branch
66
(self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
67
branch = self.wt.branch
68
except errors.NotBranchError:
74
file_id = self.wt.path2id(path)
76
self.notbranch = False
81
# Set the old working tree
82
self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
84
# Dictionary for signal_autoconnect
85
dic = { "on_button_diff_close_clicked": self.close,
86
"on_treeview_diff_files_cursor_changed": self.cursor_changed }
88
# Connect the signals to the handlers
89
self.glade.signal_autoconnect(dic)
91
# Create the file list
92
self._create_file_view()
94
# Generate initial diff
98
""" Display the Diff window. """
100
error_dialog(_('Directory is not a branch'),
101
_('You can perform this action only in a branch.'))
104
self.window.show_all()
106
def _create_file_view(self):
107
""" Create the list of files. """
108
self.model = gtk.TreeStore(str, str)
109
self.treeview.set_model(self.model)
111
cell = gtk.CellRendererText()
112
cell.set_property("width-chars", 20)
113
column = gtk.TreeViewColumn()
114
column.pack_start(cell, expand=True)
115
column.add_attribute(cell, "text", 0)
116
self.treeview.append_column(column)
118
if have_gtksourceview:
119
self.buffer = gtksourceview.SourceBuffer()
120
slm = gtksourceview.SourceLanguagesManager()
121
gsl = slm.get_language_from_mime_type("text/x-patch")
122
self.buffer.set_language(gsl)
123
self.buffer.set_highlight(True)
125
sourceview = gtksourceview.SourceView(self.buffer)
127
self.buffer = gtk.TextBuffer()
128
sourceview = gtk.TextView(self.buffer)
130
sourceview.set_editable(False)
131
sourceview.modify_font(pango.FontDescription("Monospace"))
132
scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
133
scrollwin_diff.add(sourceview)
135
def _init_diff(self):
136
""" Generate initial diff. """
138
delta = self.wt.changes_from(self.old_tree)
140
self.model.append(None, [ _('Complete Diff'), "" ])
143
titer = self.model.append(None, [ _('Added'), None ])
144
for path, id, kind in delta.added:
145
self.model.append(titer, [ path, path ])
147
if len(delta.removed):
148
titer = self.model.append(None, [ _('Removed'), None ])
149
for path, id, kind in delta.removed:
150
self.model.append(titer, [ path, path ])
152
if len(delta.renamed):
153
titer = self.model.append(None, [ _('Renamed'), None ])
154
for oldpath, newpath, id, kind, text_modified, meta_modified \
156
self.model.append(titer, [ oldpath, newpath ])
158
if len(delta.modified):
159
titer = self.model.append(None, [ _('Modified'), None ])
160
for path, id, kind, text_modified, meta_modified in delta.modified:
161
self.model.append(titer, [ path, path ])
163
self.treeview.expand_all()
165
def cursor_changed(self, *args):
166
""" Callback when the TreeView cursor changes. """
167
(path, col) = self.treeview.get_cursor()
168
specific_files = [ self.model[path][1] ]
169
if specific_files == [ None ]:
171
elif specific_files == [ "" ]:
175
show_diff_trees(self.old_tree, self.wt, s, specific_files)
176
self.buffer.set_text(s.getvalue())
178
def close(self, widget=None):
179
self.window.destroy()