~skss/usu/6.0

« back to all changes in this revision

Viewing changes to gedit-plugin-su-edit/src/su_edit.py

  • Committer: Krasimir Stefanov
  • Date: 2011-05-05 22:03:50 UTC
  • Revision ID: lokiisyourmaster@gmail.com-20110505220350-qwqcx2ws19v0yuvp
moved the common sources to the "misc" branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# SU Edit Plugin
3
 
#
4
 
# Copyright (C) 2010 Krasimir Stefanov <lokiisyourmaster@gmail.com>
5
 
#
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.
10
 
#
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.
15
 
#
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/>.
18
 
 
19
 
# import basic requisites
20
 
import gedit
21
 
import gtk
22
 
 
23
 
# for the texts in the UI elements we use gettext (do we realy?)
24
 
import gettext
25
 
#from gettext import gettext as _
26
 
import os
27
 
import subprocess
28
 
_ = gettext.gettext
29
 
 
30
 
APP_NAME = "gedit-plugin-su-edit"
31
 
LOC_PATH = os.path.join(os.path.expanduser("/usr/share/locale"))
32
 
 
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)
37
 
 
38
 
# a common ui definition for menu and toolbar additions
39
 
ui_str = """<ui>
40
 
  <toolbar name="ToolBar">
41
 
        <separator />
42
 
        <toolitem name="SUEdit" action="SUEdit" />
43
 
  </toolbar>
44
 
</ui>
45
 
"""
46
 
 
47
 
# define the plugin helper class
48
 
class SUEditHelper:
49
 
 
50
 
        def __init__(self, plugin, window):
51
 
                self._window = window
52
 
                self._plugin = plugin
53
 
                
54
 
                # Add "Toggle Text Wrap" to the View menu and to the Toolbar
55
 
                self._insert_ui_items()
56
 
 
57
 
        def deactivate(self):
58
 
                self._remove_ui_items()
59
 
                self._window = None
60
 
                self._plugin = None
61
 
 
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([(
68
 
                                "SUEdit", 
69
 
                                gtk.STOCK_EDIT, 
70
 
                                _("Edit as root"), 
71
 
                                "<Ctrl><Shift>R", 
72
 
                                _("Edit the current document as root"), 
73
 
                                self.on_su_edit)])
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)
78
 
 
79
 
        def _remove_ui_items(self):
80
 
                # Remove the ui
81
 
                self._manager.remove_ui(self._ui_id)
82
 
                self._ui_id = None
83
 
                # Remove action group
84
 
                self._manager.remove_action_group(self._action_group)
85
 
                self._action_group = None
86
 
                # ensure that manager updates
87
 
                self._manager.ensure_update()
88
 
 
89
 
        def update_ui(self):
90
 
                self._action_group.set_sensitive(self._window.get_active_document() != None)
91
 
                try:
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")
96
 
                except:
97
 
                        return
98
 
 
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)
104
 
                
105
 
# define the plugin derivate class
106
 
class SUEditPlugin(gedit.Plugin):
107
 
        def __init__(self):
108
 
                gedit.Plugin.__init__(self)
109
 
                self._instances = {}
110
 
 
111
 
        def activate(self, window):
112
 
                self._instances[window] = SUEditHelper(self, window)
113
 
 
114
 
        def deactivate(self, window):
115
 
                self._instances[window].deactivate()
116
 
                del self._instances[window]
117
 
 
118
 
        def update_ui(self, window):
119
 
                self._instances[window].update_ui()
120