86
86
properties and calls a hook function when any of them are
89
def __init__(self, proxy_object=None, properties=None, **kwargs):
89
def __init__(self, proxy_object=None, *args, **kwargs):
90
90
self.proxy = proxy_object # Mandos Client proxy object
91
self.properties = dict() if properties is None else properties
92
self.properties = dict()
92
93
self.property_changed_match = (
93
94
self.proxy.connect_to_signal("PropertyChanged",
94
self._property_changed,
95
self.property_changed,
98
if properties is None:
99
self.properties.update(
100
self.proxy.GetAll(client_interface,
102
= dbus.PROPERTIES_IFACE))
104
super(MandosClientPropertyCache, self).__init__(**kwargs)
106
def _property_changed(self, property, value):
107
"""Helper which takes positional arguments"""
108
return self.property_changed(property=property, value=value)
99
self.properties.update(
100
self.proxy.GetAll(client_interface,
101
dbus_interface = dbus.PROPERTIES_IFACE))
103
#XXX This breaks good super behaviour
104
# super(MandosClientPropertyCache, self).__init__(
110
107
def property_changed(self, property=None, value=None):
111
108
"""This is called whenever we get a PropertyChanged signal
114
111
# Update properties dict with new value
115
112
self.properties[property] = value
114
def delete(self, *args, **kwargs):
118
115
self.property_changed_match.remove()
116
super(MandosClientPropertyCache, self).__init__(
121
120
class MandosClientWidget(urwid.FlowWidget, MandosClientPropertyCache):
125
124
def __init__(self, server_proxy_object=None, update_hook=None,
126
delete_hook=None, logger=None, **kwargs):
125
delete_hook=None, logger=None, *args, **kwargs):
127
126
# Called on update
128
127
self.update_hook = update_hook
129
128
# Called on delete
134
133
self.logger = logger
136
135
self._update_timer_callback_tag = None
136
self._update_timer_callback_lock = 0
138
138
# The widget shown normally
139
139
self._text_widget = urwid.Text("")
140
140
# The widget shown when we have focus
141
141
self._focus_text_widget = urwid.Text("")
142
super(MandosClientWidget, self).__init__(**kwargs)
142
super(MandosClientWidget, self).__init__(
143
update_hook=update_hook, delete_hook=delete_hook,
144
146
self.opened = False
148
last_checked_ok = isoformat_to_datetime(self.properties
151
if self.properties ["LastCheckerStatus"] != 0:
152
self.using_timer(True)
154
if self.need_approval:
155
self.using_timer(True)
146
157
self.match_objects = (
147
158
self.proxy.connect_to_signal("CheckerCompleted",
148
159
self.checker_completed,
167
178
#self.logger('Created client {0}'
168
179
# .format(self.properties["Name"]))
181
def property_changed(self, property=None, value=None):
182
super(self, MandosClientWidget).property_changed(property,
184
if property == "ApprovalPending":
185
using_timer(bool(value))
186
if property == "LastCheckerStatus":
187
using_timer(value != 0)
188
#self.logger('Checker for client {0} (command "{1}") was '
189
# ' successful'.format(self.properties["Name"],
170
192
def using_timer(self, flag):
171
193
"""Call this method with True or False when timer should be
172
194
activated or deactivated.
174
if flag and self._update_timer_callback_tag is None:
196
old = self._update_timer_callback_lock
198
self._update_timer_callback_lock += 1
200
self._update_timer_callback_lock -= 1
201
if old == 0 and self._update_timer_callback_lock:
175
202
# Will update the shown timer value every second
176
203
self._update_timer_callback_tag = (gobject.timeout_add
178
205
self.update_timer))
179
elif not (flag or self._update_timer_callback_tag is None):
206
elif old and self._update_timer_callback_lock == 0:
180
207
gobject.source_remove(self._update_timer_callback_tag)
181
208
self._update_timer_callback_tag = None
224
251
message = 'Client {0} will get its secret in {1} seconds'
225
252
self.logger(message.format(self.properties["Name"],
254
self.using_timer(True)
228
256
def rejected(self, reason):
229
257
self.logger('Client {0} was rejected; reason: {1}'
255
283
"bold-underline-blink":
256
284
"bold-underline-blink-standout",
259
287
# Rebuild focus and non-focus widgets using current properties
261
289
# Base part of a client. Name!
262
290
base = '{name}: '.format(name=self.properties["Name"])
263
291
if not self.properties["Enabled"]:
264
292
message = "DISABLED"
265
self.using_timer(False)
266
293
elif self.properties["ApprovalPending"]:
267
294
timeout = datetime.timedelta(milliseconds
268
295
= self.properties
270
297
last_approval_request = isoformat_to_datetime(
271
298
self.properties["LastApprovalRequest"])
272
299
if last_approval_request is not None:
273
timer = max(timeout - (datetime.datetime.utcnow()
274
- last_approval_request),
275
datetime.timedelta())
300
timer = timeout - (datetime.datetime.utcnow()
301
- last_approval_request)
277
303
timer = datetime.timedelta()
278
304
if self.properties["ApprovedByDefault"]:
281
307
message = "Denial in {0}. (a)pprove?"
282
308
message = message.format(unicode(timer).rsplit(".", 1)[0])
283
self.using_timer(True)
284
309
elif self.properties["LastCheckerStatus"] != 0:
285
310
# When checker has failed, show timer until client expires
286
311
expires = self.properties["Expires"]
290
315
expires = (datetime.datetime.strptime
291
316
(expires, '%Y-%m-%dT%H:%M:%S.%f'))
292
timer = max(expires - datetime.datetime.utcnow(),
293
datetime.timedelta())
317
timer = expires - datetime.datetime.utcnow()
294
318
message = ('A checker has failed! Time until client'
295
319
' gets disabled: {0}'
296
320
.format(unicode(timer).rsplit(".", 1)[0]))
297
self.using_timer(True)
299
322
message = "enabled"
300
self.using_timer(False)
301
323
self._text = "{0}{1}".format(base, message)
303
325
if not urwid.supports_unicode():
304
326
self._text = self._text.encode("ascii", "replace")
305
327
textlist = [("normal", self._text)]
323
345
return True # Keep calling this
325
def delete(self, **kwargs):
347
def delete(self, *args, **kwargs):
326
348
if self._update_timer_callback_tag is not None:
327
349
gobject.source_remove(self._update_timer_callback_tag)
328
350
self._update_timer_callback_tag = None
331
353
self.match_objects = ()
332
354
if self.delete_hook is not None:
333
355
self.delete_hook(self)
334
return super(MandosClientWidget, self).delete(**kwargs)
356
return super(MandosClientWidget, self).delete(*args, **kwargs)
336
358
def render(self, maxcolrow, focus=False):
337
359
"""Render differently if we have focus.
382
def property_changed(self, property=None, **kwargs):
404
def property_changed(self, property=None, value=None,
383
406
"""Call self.update() if old value is not new value.
384
407
This overrides the method from MandosClientPropertyCache"""
385
408
property_name = unicode(property)
386
409
old_value = self.properties.get(property_name)
387
410
super(MandosClientWidget, self).property_changed(
388
property=property, **kwargs)
411
property=property, value=value, *args, **kwargs)
389
412
if self.properties.get(property_name) != old_value:
395
418
"down" key presses, thus not allowing any containing widgets to
396
419
use them as an excuse to shift focus away from this widget.
398
def keypress(self, *args, **kwargs):
399
ret = super(ConstrainedListBox, self).keypress(*args, **kwargs)
421
def keypress(self, maxcolrow, key):
422
ret = super(ConstrainedListBox, self).keypress(maxcolrow, key)
400
423
if ret in ("up", "down"):