-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaskpass-service
executable file
·115 lines (87 loc) · 3.61 KB
/
askpass-service
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gobject
import os
import dbus
import dbus.service
import dbus.mainloop.glib
import gtk
class PasswordAsker(dbus.service.Object):
@dbus.service.method("org.cups.sharp.Passwd",
in_signature='sa(sss)', out_signature='a{ss}')
def Ask(self, title, elements):
self.title = title
self.elements = elements
return self.show_dialog()
def check_entries(self, *args):
all_valid = True
for key, arguments, widget_type in self.elements:
valid = True
if widget_type in ['PASSWORD', 'STRING', 'PASSCODE']:
args = arguments.split(';')
min_length = 0
max_length = 9999
if len(args) == 3:
min_length = int(args[0])
max_length = int(args[1])
text = self.entries[key].get_text()
if (len(text) < min_length) or (len(text) > max_length):
valid = False
if widget_type == 'PASSCODE':
if not text.isdigit():
valid = False
if not valid:
# TODO: Show what entry is causing the troubles
all_valid = False
box = self.dialog.get_action_area()
box.get_children()[0].set_sensitive(all_valid)
def show_dialog(self):
self.dialog = gtk.Dialog(title=u"%s — Print Options" % self.title, buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK))
self.dialog.set_default_response(gtk.RESPONSE_OK)
sizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
self.entries = {}
for key, arguments, widget_type in self.elements:
# only support password and string types
assert widget_type in ['PASSWORD', 'STRING', 'PASSCODE']
entry_caps_lock_warning = False
entry_visibility = True
# The last element is the string
label_text = arguments.split(';')[-1]
if widget_type == 'PASSWORD':
entry_caps_lock_warning = True
entry_visibility = False
elif widget_type == 'PASSCODE':
entry_visibility = False
hbox = gtk.HBox()
if hbox.get_direction() == gtk.TEXT_DIR_LTR:
label = gtk.Label(label_text + ': ')
else:
label = gtk.Label(' :' + label_text)
label.props.xalign = 0.0
sizegroup.add_widget(label)
self.entries[key] = gtk.Entry()
self.entries[key].connect("changed", self.check_entries)
self.entries[key].props.activates_default = True
self.entries[key].props.caps_lock_warning = entry_caps_lock_warning
self.entries[key].props.visibility = entry_visibility
hbox.add(label)
hbox.add(self.entries[key])
self.dialog.vbox.add(hbox)
self.check_entries()
self.dialog.show_all()
self.dialog.present()
if self.dialog.run() != gtk.RESPONSE_OK:
self.dialog.destroy()
return {}
result = {}
for key, widget in self.entries.iteritems():
result[key] = widget.get_text()
self.dialog.destroy()
return result
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
name = dbus.service.BusName("org.cups.sharp.Passwd", bus=bus, allow_replacement=True, replace_existing=True)
object = PasswordAsker(bus, "/org/cups/sharp/Passwd")
mainloop = gobject.MainLoop()
mainloop.run()