4
# Copyright (C) 2010 Krasimir Stefanov <lokiisyourmaster@gmail.com>
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
# import basic requisites
23
# for the texts in the UI elements we use gettext (do we realy?)
25
#from gettext import gettext as _
30
APP_NAME = "gedit-plugin-su-edit"
31
LOC_PATH = os.path.join(os.path.expanduser("/usr/share/locale"))
33
gettext.find(APP_NAME, LOC_PATH)
34
gettext.install(APP_NAME, LOC_PATH, True)
35
gettext.bindtextdomain(APP_NAME, LOC_PATH)
36
gettext.textdomain(APP_NAME)
38
# a common ui definition for menu and toolbar additions
40
<toolbar name="ToolBar">
42
<toolitem name="SUEdit" action="SUEdit" />
47
# define the plugin helper class
50
def __init__(self, plugin, window):
54
# Add "Toggle Text Wrap" to the View menu and to the Toolbar
55
self._insert_ui_items()
58
self._remove_ui_items()
62
def _insert_ui_items(self):
63
# Get the GtkUIManager
64
self._manager = self._window.get_ui_manager()
65
# Create a new action group
66
self._action_group = gtk.ActionGroup("PluginActions")
67
self._action_group.add_actions([(
72
_("Edit the current document as root"),
74
# Insert the action group
75
self._manager.insert_action_group(self._action_group, -1)
76
# Add my item to the "Views" menu and to the Toolbar
77
self._ui_id = self._manager.add_ui_from_string(ui_str)
79
def _remove_ui_items(self):
81
self._manager.remove_ui(self._ui_id)
84
self._manager.remove_action_group(self._action_group)
85
self._action_group = None
86
# ensure that manager updates
87
self._manager.ensure_update()
90
self._action_group.set_sensitive(self._window.get_active_document() != None)
92
# Get initial state from word wrapping in this view (if any)
93
_active_view = self._window.get_active_view()
94
# Get our action and set state according to current wrap mode
95
_current_action = self._action_group.get_action("SUEdit")
99
def on_su_edit(self, action):
100
_active_view = self._window.get_active_view()
101
path = self._window.get_active_document().get_uri().replace("file://", "")
102
path = path.replace("%20", " ")
103
subprocess.Popen('gksu gedit "%s"' % path, shell=True)
105
# define the plugin derivate class
106
class SUEditPlugin(gedit.Plugin):
108
gedit.Plugin.__init__(self)
111
def activate(self, window):
112
self._instances[window] = SUEditHelper(self, window)
114
def deactivate(self, window):
115
self._instances[window].deactivate()
116
del self._instances[window]
118
def update_ui(self, window):
119
self._instances[window].update_ui()