~ubuntu-branches/ubuntu/quantal/nautilus-compare/quantal

« back to all changes in this revision

Viewing changes to src/nautilus-compare.py

  • Committer: Package Import Robot
  • Author(s): Andrea Veri
  • Date: 2011-10-27 13:01:47 UTC
  • Revision ID: package-import@ubuntu.com-20111027130147-t7lk8124nbi7ru0j
Tags: upstream-0.0.3
ImportĀ upstreamĀ versionĀ 0.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    nautilus-compare: An context menu extension for Nautilus file manager
 
2
#    Copyright (C) 2011  Guido Tabbernuk <boamaod@gmail.com>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
import os
 
18
import urllib
 
19
import gettext
 
20
import locale
 
21
import xdg.BaseDirectory
 
22
import ConfigParser
 
23
 
 
24
from gi.repository import Nautilus, GObject, Gio
 
25
 
 
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'
 
31
 
 
32
class NautilusCompareExtension(GObject.GObject, Nautilus.MenuProvider):
 
33
 
 
34
        for_later = None
 
35
        diff_engine = DEFAULT_DIFF_ENGINE
 
36
        diff_engine_3way = DEFAULT_DIFF_ENGINE
 
37
 
 
38
        def __init__(self):
 
39
                config_dir = xdg.BaseDirectory.xdg_config_home
 
40
                if not config_dir:
 
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)
 
45
 
 
46
                try:
 
47
                        self.diff_engine = config.get(SETTINGS_MAIN, DIFF_PATH)
 
48
                        self.diff_engine_3way = config.get(SETTINGS_MAIN, DIFF_PATH_3WAY)
 
49
                except:
 
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:
 
54
                                config.write(f)
 
55
 
 
56
        def _open_comparator(self, paths):
 
57
                if len(paths) == 1:
 
58
                        self.for_later = paths[0]
 
59
                        return
 
60
 
 
61
                args = ""
 
62
                for path in paths:
 
63
                        args += "\"%s\" " % path
 
64
                if len(paths)==2:
 
65
                        cmd = (self.diff_engine + " " + args + "&")
 
66
                else:
 
67
                        cmd = (self.diff_engine_3way + " " + args + "&")
 
68
                os.system(cmd)
 
69
                
 
70
        def menu_activate_cb(self, menu, paths):
 
71
                self._open_comparator(paths)
 
72
 
 
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):
 
75
                        return True
 
76
                else:
 
77
                        return False
 
78
 
 
79
        def get_file_items(self, window, files):
 
80
                paths = []
 
81
                for file in files:
 
82
                        if self.valid_file(file):
 
83
                                path = urllib.unquote(file.get_uri()[7:])
 
84
                                paths.append(path)
 
85
 
 
86
                # no files selected
 
87
                if len(paths) < 1:
 
88
                        return
 
89
 
 
90
                # initialize i18n
 
91
                locale.setlocale(locale.LC_ALL, '')
 
92
                gettext.bindtextdomain(APP)
 
93
                gettext.textdomain(APP)
 
94
                _ = gettext.gettext
 
95
 
 
96
                item1 = None
 
97
                item2 = None
 
98
                item3 = None
 
99
 
 
100
                # for paths with remembered items
 
101
                new_paths = list(paths)
 
102
 
 
103
                # exactly one file selected
 
104
                if len(paths) == 1:
 
105
 
 
106
                        # and one was already selected for later comparison
 
107
                        if self.for_later is not None:
 
108
 
 
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")
 
115
                                        )
 
116
 
 
117
                                        # compare the one saved for later to the one selected now
 
118
                                        new_paths.insert(0, self.for_later)
 
119
 
 
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")
 
125
                        )
 
126
 
 
127
                # can always compare, if more than one selected
 
128
                else:
 
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")
 
136
                                        )
 
137
                                        # compare the one saved for later to the ones selected now
 
138
                                        new_paths.insert(0, self.for_later)
 
139
 
 
140
                        item2 = Nautilus.MenuItem(
 
141
                                name="NautilusCompareExtension::CompareWithin",
 
142
                                label=_('Compare'),
 
143
                                tip=_("Compare selected files")
 
144
                        )
 
145
 
 
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)
 
149
 
 
150
                items = [item1, item2, item3]
 
151
 
 
152
                while None in items:
 
153
                        items.remove(None)
 
154
 
 
155
                return items
 
156