summaryrefslogtreecommitdiffstats
path: root/plugins/externaltools/tools/windowactivatable.py
blob: 594959800a0400fd5fbe80b1c5094a0208ef6d49 (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
# -*- coding: UTF-8 -*-
#    Gedit External Tools plugin
#    Copyright (C) 2005-2006  Steve Frécinaux <steve@istique.net>
#
#    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

__all__ = ('ExternalToolsPlugin', 'OutputPanel', 'Capture', 'UniqueById')

from gi.repository import GLib, Gio, GObject, Gtk, Gedit
from .library import ToolLibrary
from .outputpanel import OutputPanel
from .capture import Capture
from .functions import *

try:
    import gettext
    gettext.bindtextdomain('gedit')
    gettext.textdomain('gedit')
    _ = gettext.gettext
except:
    _ = lambda s: s

class ToolActions(object):
    def __init__(self, library, window, panel):
        super(ToolActions, self).__init__()
        self._library = library
        self._window = window
        self._panel = panel
        self._action_tools = {}

        self.update()

    def deactivate(self):
        self.remove()

    def remove(self):
        for name, tool in self._action_tools.items():
            self._window.remove_action(name)
        self._action_tools = {}

    def _insert_directory(self, directory):
        for tool in sorted(directory.tools, key=lambda x: x.name.lower()):
            # FIXME: find a better way to share the action name
            action_name = 'external-tool-%X-%X' % (id(tool), id(tool.name))
            self._action_tools[action_name] = tool

            action = Gio.SimpleAction(name=action_name)
            action.connect('activate', capture_menu_action, self._window, self._panel, tool)
            self._window.add_action(action)

    def update(self):
        self.remove()
        self._insert_directory(self._library.tree)
        self.filter(self._window.get_active_document())

    def filter_language(self, language, item):
        if not item.languages:
            return True

        if not language and 'plain' in item.languages:
            return True

        if language and (language.get_id() in item.languages):
            return True
        else:
            return False

    def filter(self, document):
        if document is None:
            titled = False
            remote = False
            language = None
        else:
            titled = document.get_file().get_location() is not None
            remote = not document.get_file().is_local()
            language = document.get_language()

        states = {
            'always': True,
            'all': document is not None,
            'local': titled and not remote,
            'remote': titled and remote,
            'titled': titled,
            'untitled': not titled,
        }

        for name, tool in self._action_tools.items():
            action = self._window.lookup_action(name)
            if action:
                action.set_enabled(states[tool.applicability] and
                                   self.filter_language(language, tool))


class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "ExternalToolsWindowActivatable"

    window = GObject.Property(type=Gedit.Window)

    def __init__(self):
        GObject.Object.__init__(self)
        self.actions = None

    def do_activate(self):
        self.window.external_tools_window_activatable = self

        self._library = ToolLibrary()

        # Create output console
        self._output_buffer = OutputPanel(self.plugin_info.get_data_dir(), self.window)

        self.actions = ToolActions(self._library, self.window, self._output_buffer)

        bottom = self.window.get_bottom_panel()
        bottom.add_titled(self._output_buffer.panel, "GeditExternalToolsShellOutput", _("Tool Output"))

    def do_update_state(self):
        if self.actions is not None:
            self.actions.filter(self.window.get_active_document())

    def do_deactivate(self):
        self.actions.deactivate()
        bottom = self.window.get_bottom_panel()
        bottom.remove(self._output_buffer.panel)
        self.window.external_tools_window_activatable = None

    def update_actions(self):
        self.actions.update()

# ex:ts=4:et: