1# -*- coding: UTF-8 -*- 2# Gedit External Tools plugin 3# Copyright (C) 2005-2006 Steve Frécinaux <steve@istique.net> 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program; if not, write to the Free Software 17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19__all__ = ('ExternalToolsPlugin', 'OutputPanel', 'Capture', 'UniqueById') 20 21from gi.repository import GLib, Gio, GObject, Gtk, Gedit 22from .library import ToolLibrary 23from .outputpanel import OutputPanel 24from .capture import Capture 25from .functions import * 26 27try: 28 import gettext 29 gettext.bindtextdomain('gedit') 30 gettext.textdomain('gedit') 31 _ = gettext.gettext 32except: 33 _ = lambda s: s 34 35class ToolActions(object): 36 def __init__(self, library, window, panel): 37 super(ToolActions, self).__init__() 38 self._library = library 39 self._window = window 40 self._panel = panel 41 self._action_tools = {} 42 43 self.update() 44 45 def deactivate(self): 46 self.remove() 47 48 def remove(self): 49 for name, tool in self._action_tools.items(): 50 self._window.remove_action(name) 51 self._action_tools = {} 52 53 def _insert_directory(self, directory): 54 for tool in sorted(directory.tools, key=lambda x: x.name.lower()): 55 # FIXME: find a better way to share the action name 56 action_name = 'external-tool-%X-%X' % (id(tool), id(tool.name)) 57 self._action_tools[action_name] = tool 58 59 action = Gio.SimpleAction(name=action_name) 60 action.connect('activate', capture_menu_action, self._window, self._panel, tool) 61 self._window.add_action(action) 62 63 def update(self): 64 self.remove() 65 self._insert_directory(self._library.tree) 66 self.filter(self._window.get_active_document()) 67 68 def filter_language(self, language, item): 69 if not item.languages: 70 return True 71 72 if not language and 'plain' in item.languages: 73 return True 74 75 if language and (language.get_id() in item.languages): 76 return True 77 else: 78 return False 79 80 def filter(self, document): 81 if document is None: 82 titled = False 83 remote = False 84 language = None 85 else: 86 titled = document.get_file().get_location() is not None 87 remote = not document.get_file().is_local() 88 language = document.get_language() 89 90 states = { 91 'always': True, 92 'all': document is not None, 93 'local': titled and not remote, 94 'remote': titled and remote, 95 'titled': titled, 96 'untitled': not titled, 97 } 98 99 for name, tool in self._action_tools.items(): 100 action = self._window.lookup_action(name) 101 if action: 102 action.set_enabled(states[tool.applicability] and 103 self.filter_language(language, tool)) 104 105 106class WindowActivatable(GObject.Object, Gedit.WindowActivatable): 107 __gtype_name__ = "ExternalToolsWindowActivatable" 108 109 window = GObject.Property(type=Gedit.Window) 110 111 def __init__(self): 112 GObject.Object.__init__(self) 113 self.actions = None 114 115 def do_activate(self): 116 self.window.external_tools_window_activatable = self 117 118 self._library = ToolLibrary() 119 120 # Create output console 121 self._output_buffer = OutputPanel(self.plugin_info.get_data_dir(), self.window) 122 123 self.actions = ToolActions(self._library, self.window, self._output_buffer) 124 125 bottom = self.window.get_bottom_panel() 126 bottom.add_titled(self._output_buffer.panel, "GeditExternalToolsShellOutput", _("Tool Output")) 127 128 def do_update_state(self): 129 if self.actions is not None: 130 self.actions.filter(self.window.get_active_document()) 131 132 def do_deactivate(self): 133 self.actions.deactivate() 134 bottom = self.window.get_bottom_panel() 135 bottom.remove(self._output_buffer.panel) 136 self.window.external_tools_window_activatable = None 137 138 def update_actions(self): 139 self.actions.update() 140 141# ex:ts=4:et: 142