-
Notifications
You must be signed in to change notification settings - Fork 3
/
open_as_root.py
executable file
·75 lines (58 loc) · 2.52 KB
/
open_as_root.py
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
# GPL v3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gtk, Gedit, Gio
class OpenAsRootAppActivatable(GObject.Object, Gedit.AppActivatable):
app = GObject.property(type=Gedit.App)
__gtype_name__ = 'OpenAsRootAppActivatable'
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
self.menu_ext = self.extend_menu('file-section')
self.menu_item = Gio.MenuItem.new(_("Edit as administrator…"), 'win.open_as_root')
self.menu_ext.append_menu_item(self.menu_item)
def do_deactivate(self):
self.menu_item = None
self.menu_ext = None
############################################################################
################################################################################
class OpenAsRootWindowActivatable(GObject.Object, Gedit.WindowActivatable):
window = GObject.property(type=Gedit.Window)
__gtype_name__ = 'OpenAsRootWindowActivatable'
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
# Defining the action which was set to the menu item earlier.
action = Gio.SimpleAction(name='open_as_root')
action.connect('activate', self.action_cb)
self.window.add_action(action)
self.window.connect('active-tab-changed', self.update_item_state)
self.window.connect('active-tab-state-changed', self.update_item_state)
def do_deactivate(self):
pass
def do_update_state(self):
pass
def update_item_state(self, *args):
state = self.get_item_state()
self.window.lookup_action('open_as_root').set_enabled(state)
def get_item_state(self, *args):
if self.window.get_active_document() is None:
# if there is no document, we disable the action, so we don't get NoneException
return False
file_location = self.window.get_active_document().get_file().get_location()
if file_location is None:
# if the document isn't saved, we disable the action, so we don't get NoneException
return False
elif 'admin' in file_location.get_uri_scheme():
# if the document is already opened as root, we disable the action too
return False
else:
return True
def action_cb(self, action, data):
doc = self.window.get_active_document()
normal_uri = doc.get_file().get_location().get_uri()
admin_uri = normal_uri.replace('file://', 'admin://', 1)
gfile = Gio.File.new_for_uri(admin_uri)
self.window.create_tab_from_location(gfile, None, 0, 0, False, True)
############################################################################
################################################################################