summaryrefslogtreecommitdiff
path: root/plugins/quickopen/quickopen/windowhelper.py
blob: c23629cffbc61b55a304ff8b42d28b11d09da257 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-

#  Copyright (C) 2009 - Jesse van den Kieboom
#
#  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., 51 Franklin St, Fifth Floor,
#  Boston, MA 02110-1301, USA.

from popup import Popup
import os
from gi.repository import Gio, GLib, Gtk, Pluma
from virtualdirs import RecentDocumentsDirectory
from virtualdirs import CurrentDocumentsDirectory

ui_str = """<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
      <placeholder name="FileOps_2">
        <menuitem name="QuickOpen" action="QuickOpen"/>
      </placeholder>
    </menu>
  </menubar>
</ui>
"""

class WindowHelper:
        def __init__(self, window, plugin):
                self._window = window
                self._plugin = plugin

                self._popup = None
                self._install_menu()

        def deactivate(self):
                self._uninstall_menu()
                self._window = None
                self._plugin = None

        def update_ui(self):
                pass

        def _uninstall_menu(self):
                manager = self._window.get_ui_manager()

                manager.remove_ui(self._ui_id)
                manager.remove_action_group(self._action_group)

                manager.ensure_update()

        def _install_menu(self):
                manager = self._window.get_ui_manager()
                self._action_group = Gtk.ActionGroup("PlumaQuickOpenPluginActions")
                self._action_group.add_actions([
                        ("QuickOpen", Gtk.STOCK_OPEN, _("Quick open"),
                         '<Ctrl><Alt>O', _("Quickly open documents"),
                         self.on_quick_open_activate)
                ])

                manager.insert_action_group(self._action_group, -1)
                self._ui_id = manager.add_ui_from_string(ui_str)

        def _create_popup(self):
                paths = []

                # Open documents
                paths.append(CurrentDocumentsDirectory(self._window))

                doc = self._window.get_active_document()

                # Current document directory
                if doc and doc.is_local():
                        gfile = doc.get_location()
                        paths.append(gfile.get_parent())

                # File browser root directory
                bus = self._window.get_message_bus()

                try:
                        msg = bus.send_sync('/plugins/filebrowser', 'get_root')

                        if msg:
                                uri = msg.get_value('uri')

                                if uri:
                                        gfile = Gio.file_new_for_uri(uri)

                                        if gfile.is_native():
                                                paths.append(gfile)

                except StandardError:
                        pass

                # Recent documents
                paths.append(RecentDocumentsDirectory())

                # Local bookmarks
                for path in self._local_bookmarks():
                        paths.append(path)

                # Desktop directory
                desktopdir = self._desktop_dir()

                if desktopdir:
                        paths.append(Gio.file_new_for_path(desktopdir))

                # Home directory
                paths.append(Gio.file_new_for_path(os.path.expanduser('~')))

                self._popup = Popup(self._window, paths, self.on_activated)

                self._popup.set_default_size(*self._plugin.get_popup_size())
                self._popup.set_transient_for(self._window)
                self._popup.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)

                self._window.get_group().add_window(self._popup)

                self._popup.connect('destroy', self.on_popup_destroy)

        def _local_bookmarks(self):
                filename = os.path.expanduser('~/.gtk-bookmarks')

                if not os.path.isfile(filename):
                        return []

                paths = []

                for line in file(filename, 'r').xreadlines():
                        uri = line.strip().split(" ")[0]
                        f = Gio.file_new_for_uri(uri)

                        if f.is_native():
                                try:
                                        info = f.query_info(Gio.FILE_ATTRIBUTE_STANDARD_TYPE, Gio.FileQueryInfoFlags.NONE, None)

                                        if info and info.get_file_type() == Gio.FileType.DIRECTORY:
                                                paths.append(f)
                                except GLib.GError:
                                        pass

                return paths

        def _desktop_dir(self):
                config = os.getenv('XDG_CONFIG_HOME')

                if not config:
                        config = os.path.expanduser('~/.config')

                config = os.path.join(config, 'user-dirs.dirs')
                desktopdir = None

                if os.path.isfile(config):
                        for line in file(config, 'r').xreadlines():
                                line = line.strip()

                                if line.startswith('XDG_DESKTOP_DIR'):
                                        parts = line.split('=', 1)
                                        desktopdir = os.path.expandvars(parts[1].strip('"').strip("'"))
                                        break

                if not desktopdir:
                        desktopdir = os.path.expanduser('~/Desktop')

                return desktopdir

        # Callbacks
        def on_quick_open_activate(self, action):
                if not self._popup:
                        self._create_popup()

                self._popup.show()

        def on_popup_destroy(self, popup):
                self._plugin.set_popup_size(popup.get_final_size())
                self._popup = None

        def on_activated(self, gfile):
                Pluma.commands_load_uri(self._window, gfile.get_uri(), None, -1)
                return True

# ex:ts=8:et: