1
# nautilus-compare: An context menu extension for Nautilus file manager
2
# Copyright (C) 2011 Guido Tabbernuk <boamaod@gmail.com>
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
import xdg.BaseDirectory
24
from gi.repository import Nautilus, GObject, Gio
26
APP = 'nautilus-compare'
27
SETTINGS_MAIN = 'Settings'
28
DIFF_PATH = 'diff_engine_path'
29
DIFF_PATH_3WAY = 'diff_engine_path_3way'
30
DEFAULT_DIFF_ENGINE = 'meld'
32
class NautilusCompareExtension(GObject.GObject, Nautilus.MenuProvider):
35
diff_engine = DEFAULT_DIFF_ENGINE
36
diff_engine_3way = DEFAULT_DIFF_ENGINE
39
config_dir = xdg.BaseDirectory.xdg_config_home
41
config_dir = os.path.join(os.getenv("HOME"), ".config")
42
config_file = os.path.join(config_dir, APP + ".conf")
43
config = ConfigParser.ConfigParser()
44
sections = config.read(config_file)
47
self.diff_engine = config.get(SETTINGS_MAIN, DIFF_PATH)
48
self.diff_engine_3way = config.get(SETTINGS_MAIN, DIFF_PATH_3WAY)
50
config.add_section(SETTINGS_MAIN)
51
config.set(SETTINGS_MAIN, DIFF_PATH, self.diff_engine)
52
config.set(SETTINGS_MAIN, DIFF_PATH_3WAY, self.diff_engine_3way)
53
with open(config_file, 'wb') as f:
56
def _open_comparator(self, paths):
58
self.for_later = paths[0]
63
args += "\"%s\" " % path
65
cmd = (self.diff_engine + " " + args + "&")
67
cmd = (self.diff_engine_3way + " " + args + "&")
70
def menu_activate_cb(self, menu, paths):
71
self._open_comparator(paths)
73
def valid_file(self, file):
74
if file.get_uri_scheme() == 'file' and file.get_file_type() in (Gio.FileType.DIRECTORY, Gio.FileType.REGULAR, Gio.FileType.SYMBOLIC_LINK):
79
def get_file_items(self, window, files):
82
if self.valid_file(file):
83
path = urllib.unquote(file.get_uri()[7:])
91
locale.setlocale(locale.LC_ALL, '')
92
gettext.bindtextdomain(APP)
93
gettext.textdomain(APP)
100
# for paths with remembered items
101
new_paths = list(paths)
103
# exactly one file selected
106
# and one was already selected for later comparison
107
if self.for_later is not None:
109
# we don't want to compare file to itself
110
if self.for_later not in paths:
111
item1 = Nautilus.MenuItem(
112
name="NautilusCompareExtension::CompareTo",
113
label=_('Compare to ') + self.for_later,
114
tip=_("Compare to the file remembered before")
117
# compare the one saved for later to the one selected now
118
new_paths.insert(0, self.for_later)
120
# if only one file selected, we offer to remember it for later anyway
121
item3 = Nautilus.MenuItem(
122
name="NautilusCompareExtension::CompareLater",
123
label=_('Compare Later'),
124
tip=_("Remember file for later comparison")
127
# can always compare, if more than one selected
129
# if we have already saved one file and add some more, we can do n-way compare
130
if self.for_later is not None:
131
if self.for_later not in paths:
132
item1 = Nautilus.MenuItem(
133
name="NautilusCompareExtension::MultiCompare",
134
label=_('Three-Way Compare to ') + self.for_later,
135
tip=_("Compare selected files to the file remembered before")
137
# compare the one saved for later to the ones selected now
138
new_paths.insert(0, self.for_later)
140
item2 = Nautilus.MenuItem(
141
name="NautilusCompareExtension::CompareWithin",
143
tip=_("Compare selected files")
146
if item1: item1.connect('activate', self.menu_activate_cb, new_paths)
147
if item2: item2.connect('activate', self.menu_activate_cb, paths)
148
if item3: item3.connect('activate', self.menu_activate_cb, paths)
150
items = [item1, item2, item3]