forked from xapi-project/xsconsole
-
Notifications
You must be signed in to change notification settings - Fork 2
/
XSConsoleDialogueBases.py
769 lines (608 loc) · 25 KB
/
XSConsoleDialogueBases.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# Copyright (c) 2007-2009 Citrix Systems Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from XSConsoleBases import *
from XSConsoleCurses import *
from XSConsoleData import *
from XSConsoleDataUtils import *
from XSConsoleDialoguePane import *
from XSConsoleFields import *
from XSConsoleLayout import *
from XSConsoleMenus import *
from XSConsoleLang import *
from XSConsoleTask import *
from XSConsoleUtils import *
class Dialogue:
def __init__(self, inLayout = None, inParent = None):
self.layout = FirstValue(inLayout, Layout.Inst())
self.parent = FirstValue(inParent, self.layout.Parent())
self.panes = {}
def Pane(self, inName = None):
return self.panes[FirstValue(inName, 0)]
def NewPane(self,inPane, inName = None):
self.panes[FirstValue(inName, 0)] = inPane
return inPane
def Title(self):
return self.title
def Destroy(self):
for pane in self.panes.values():
pane.Delete()
def Render(self):
for pane in self.panes.values():
pane.Render()
def UpdateFields(self):
pass
def NeedsCursor(self):
retVal = False
for pane in self.panes.values():
if pane.NeedsCursor():
retVal = True
return retVal
def CursorOff(self):
for pane in self.panes.values():
pane.CursorOff()
def Reset(self):
# Reset to known state, e.g. first menu item selected
pass
def Snapshot(self):
retVal = []
for key in sorted(self.panes.keys()):
retVal.append(self.panes[key].Snapshot())
return retVal
class InfoDialogue(Dialogue):
def __init__(self, inText, inInfo = None):
Dialogue.__init__(self)
self.text = inText
self.info = inInfo
pane = self.NewPane(DialoguePane(self.parent))
pane.AddBox()
self.UpdateFields()
def UpdateFields(self):
pane = self.Pane()
pane.ResetFields()
pane.AddWrappedCentredBoldTextField(self.text)
if self.info is not None:
pane.NewLine()
pane.AddWrappedTextField(self.info)
helpKeys = { Lang("<Enter>") : Lang("OK") }
if pane.NeedsScroll():
helpKeys.update({
Lang("<Page Up/Down>") : Lang("Scroll"),
Lang("<F5>") : Lang("Refresh"),
})
pane.AddKeyHelpField( helpKeys )
def HandleKey(self, inKey):
handled = True
if inKey == 'KEY_ESCAPE' or inKey == 'KEY_ENTER':
Layout.Inst().PopDialogue()
elif inKey == 'KEY_PPAGE':
self.Pane().ScrollPageUp()
elif inKey == 'KEY_NPAGE':
self.Pane().ScrollPageDown()
else:
handled = False
return handled
class BannerDialogue(Dialogue):
def __init__(self, inText):
Dialogue.__init__(self)
self.text = inText
pane = self.NewPane(DialoguePane(self.parent))
pane.AddBox()
self.UpdateFields()
def UpdateFields(self):
pane = self.Pane()
pane.ResetFields()
pane.AddWrappedCentredBoldTextField(self.text)
class QuestionDialogue(Dialogue):
def __init__(self, inText, inHandler):
Dialogue.__init__(self,)
self.text = inText
self.handler = inHandler
pane = self.NewPane(DialoguePane(self.parent))
pane.AddBox()
self.UpdateFields()
def UpdateFields(self):
pane = self.Pane()
pane.ResetFields()
pane.ResetPosition()
pane.AddWrappedCentredBoldTextField(self.text)
pane.AddKeyHelpField( { Lang("<F8>") : Lang("Yes"), Lang("<Esc>") : Lang("No") } )
def HandleKey(self, inKey):
handled = True
if inKey == 'y' or inKey == 'Y' or inKey == 'KEY_F(8)':
Layout.Inst().PopDialogue()
self.handler('y')
elif inKey == 'n' or inKey == 'N' or inKey == 'KEY_ESCAPE':
Layout.Inst().PopDialogue()
self.handler('n')
else:
handled = False
return handled
class LoginDialogue(Dialogue):
def __init__(self, inText = None, inSuccessFunc = None):
Dialogue.__init__(self)
self.text = inText
self.successFunc = inSuccessFunc
pane = self.NewPane(DialoguePane(self.parent))
pane.TitleSet("Login")
pane.AddBox()
self.UpdateFields()
pane.InputIndexSet(0)
def UpdateFields(self):
pane = self.Pane()
pane.ResetFields()
if self.text is not None:
pane.AddTitleField(self.text)
pane.AddInputField(Lang("Username", 14), "root", 'username')
pane.AddPasswordField(Lang("Password", 14), Auth.Inst().DefaultPassword(), 'password')
pane.AddKeyHelpField( {
Lang("<Esc>") : Lang("Cancel"),
Lang("<Enter>") : Lang("Next/OK"),
Lang("<Tab>") : Lang("Next")
})
def HandleKey(self, inKey):
handled = True
pane = self.Pane()
if inKey == 'KEY_ESCAPE':
Layout.Inst().PopDialogue()
elif inKey == 'KEY_ENTER':
if not pane.IsLastInput():
pane.ActivateNextInput()
else:
inputValues = pane.GetFieldValues()
Layout.Inst().PopDialogue()
Layout.Inst().DoUpdate() # Redraw now as login can take a while
try:
Auth.Inst().ProcessLogin(inputValues['username'], inputValues['password'])
if self.successFunc is not None:
self.successFunc()
else:
Layout.Inst().PushDialogue(InfoDialogue( Lang('Login Successful')))
except Exception as e:
Layout.Inst().PushDialogue(InfoDialogue( Lang('Login Failed: ')+Lang(e)))
Data.Inst().Update()
elif inKey == 'KEY_TAB':
pane.ActivateNextInput()
elif inKey == 'KEY_BTAB':
pane.ActivatePreviousInput()
elif pane.CurrentInput().HandleKey(inKey):
pass # Leave handled as True
else:
handled = False
return handled
class FileDialogue(Dialogue):
def __init__(self):
Dialogue.__init__(self)
self.vdiMount = None
self.ChangeState('INITIAL')
def Custom(self, inKey):
return self.custom.get(inKey, None)
def BuildPaneBase(self):
pane = self.NewPane(DialoguePane(self.parent))
pane.TitleSet(self.Custom('title'))
pane.AddBox()
def BuildPaneINITIAL(self):
if self.Custom('mode') == 'rw':
self.deviceList = FileUtils.DeviceList(True) # Writable devices only
else:
self.deviceList = FileUtils.DeviceList(False) # Writable and read-only devices
choiceDefs = []
for device in self.deviceList:
choiceDefs.append(ChoiceDef(device.name, lambda: self.HandleDeviceChoice(self.deviceMenu.ChoiceIndex()) ) )
if len(choiceDefs) == 0:
choiceDefs.append(ChoiceDef('<No devices available>', lambda: None)) # Avoid empty menu
self.deviceMenu = Menu(self, None, Lang("Select Device"), choiceDefs)
self.BuildPaneBase()
self.UpdateFields()
def BuildPaneUSBNOTFORMATTED(self):
self.BuildPaneBase()
self.UpdateFields()
def BuildPaneUSBNOTMOUNTABLE(self):
self.BuildPaneBase()
self.UpdateFields()
def BuildPaneFILES(self):
self.BuildPaneBase()
choiceDefs = []
offset = 0
if self.Custom('mode') == 'rw':
choiceDefs.append(ChoiceDef(Lang('Enter New Filename'), lambda: self.HandleFileChoice(None)))
offset=1
for filename in self.fileList:
displayName = "%-60.60s%10.10s" % (filename, self.vdiMount.SizeString(filename))
choiceDefs.append(ChoiceDef(displayName, lambda: self.HandleFileChoice(self.fileMenu.ChoiceIndex() - offset) ) )
if self.Custom('mode') != 'rw': # Read-only
choiceDefs.append(ChoiceDef(Lang('Enter Custom Filename'), lambda: self.HandleFileChoice(None)))
self.fileMenu = Menu(self, None, Lang("Select File"), choiceDefs)
self.UpdateFields()
def BuildPaneCONFIRM(self):
self.BuildPaneBase()
self.UpdateFields()
def BuildPaneCUSTOM(self):
self.BuildPaneBase()
self.UpdateFields()
def ChangeState(self, inState):
self.state = inState
getattr(self, 'BuildPane'+self.state)() # Despatch method named 'BuildPane'+self.state
def UpdateFields(self):
self.Pane().ResetPosition()
getattr(self, 'UpdateFields'+self.state)() # Despatch method named 'UpdateFields'+self.state
def UpdateFieldsINITIAL(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.Custom('deviceprompt'))
pane.AddMenuField(self.deviceMenu)
pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Cancel"),
"<F5>" : Lang("Rescan") } )
def UpdateFieldsUSBNOTFORMATTED(self):
pane = self.Pane()
pane.ResetFields()
pane.AddWrappedBoldTextField(Lang("This USB media is not formatted. Would you like to format it now?"))
pane.AddKeyHelpField( { Lang("<F8>") : Lang("Format media"), Lang("<Esc>") : Lang("Exit") } )
def UpdateFieldsUSBNOTMOUNTABLE(self):
pane = self.Pane()
pane.ResetFields()
pane.AddWrappedBoldTextField(Lang("This USB media contains data but this application cannot mount it. Would you like to format the media? This will erase all data on the media."))
pane.AddKeyHelpField( { Lang("<F8>") : Lang("Format Media"), Lang("<Esc>") : Lang("Exit") } )
def UpdateFieldsFILES(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.Custom('fileprompt'))
pane.AddMenuField(self.fileMenu)
pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Cancel") } )
def UpdateFieldsCUSTOM(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(Lang("Enter Filename"))
pane.AddInputField(Lang("Filename", 16), FirstValue(self.Custom('filename'), ''), 'filename')
pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Exit") } )
if pane.CurrentInput() is None:
pane.InputIndexSet(0)
def UpdateFieldsCONFIRM(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.Custom('confirmprompt'))
pane.AddWrappedBoldTextField(Lang("Device"))
pane.AddWrappedTextField(self.deviceName)
pane.NewLine()
if self.Custom('mode') == 'rw':
fileSize = ' ('+self.vdiMount.SizeString(self.filename, Lang('New file'))+')'
else:
if self.filename == '':
fileSize = Lang(' (No file specified)')
elif os.path.isdir(self.filename):
fileSize = Lang(' (Directory)')
else:
fileSize = ' ('+self.vdiMount.SizeString(self.filename, Lang('File not found'))+')'
pane.AddWrappedBoldTextField(Lang("File"))
pane.AddWrappedTextField(self.filename+fileSize)
pane.NewLine()
pane.AddKeyHelpField( { Lang("<F8>") : Lang("OK"), Lang("<Esc>") : Lang("Exit") } )
def HandleKey(self, inKey):
handled = False
if hasattr(self, 'HandleKey'+self.state):
handled = getattr(self, 'HandleKey'+self.state)(inKey)
if not handled and inKey == 'KEY_ESCAPE':
Layout.Inst().PopDialogue()
self.PreExitActions()
handled = True
return handled
def HandleKeyINITIAL(self, inKey):
handled = self.deviceMenu.HandleKey(inKey)
if not handled and inKey == 'KEY_F(5)':
Layout.Inst().PushDialogue(BannerDialogue( Lang("Rescanning...")))
Layout.Inst().Refresh()
Layout.Inst().DoUpdate()
Layout.Inst().PopDialogue()
self.BuildPaneINITIAL() # Updates self.deviceList
time.sleep(0.5) # Display rescanning box for a reasonable time
Layout.Inst().Refresh()
handled = True
return handled
def HandleKeyUSBNOTFORMATTED(self, inKey):
handled = False
if inKey == 'KEY_F(8)':
Layout.Inst().PushDialogue(BannerDialogue( Lang("Formatting...")))
Layout.Inst().Refresh()
Layout.Inst().DoUpdate()
Layout.Inst().PopDialogue()
try:
FileUtils.USBFormat(self.vdi)
self.HandleDevice()
except Exception as e:
Layout.Inst().PushDialogue(InfoDialogue( Lang("Formatting Failed"), Lang(e)))
handled = True
return handled
def HandleKeyUSBNOTMOUNTABLE(self, inKey):
return self.HandleKeyUSBNOTFORMATTED(inKey)
def HandleKeyFILES(self, inKey):
return self.fileMenu.HandleKey(inKey)
def HandleKeyCUSTOM(self, inKey):
handled = True
pane = self.Pane()
if pane.CurrentInput() is None:
pane.InputIndexSet(0)
if inKey == 'KEY_ENTER':
inputValues = pane.GetFieldValues()
try:
FileUtils.AssertSafeLeafname(inputValues['filename'])
self.filename = inputValues['filename']
self.ChangeState('CONFIRM')
except Exception as e:
Layout.Inst().PopDialogue()
Layout.Inst().PushDialogue(InfoDialogue(Lang(e)))
elif pane.CurrentInput().HandleKey(inKey):
pass # Leave handled as True
else:
handled = False
return handled
def HandleKeyCONFIRM(self, inKey):
handled = False
if inKey == 'KEY_F(8)':
self.DoAction()
handled = True
return handled
def HandleDeviceChoice(self, inChoice):
self.deviceName = self.deviceList[inChoice].name
self.vdi = self.deviceList[inChoice].vdi
self.HandleDevice()
def HandleDevice(self):
try:
self.vdiMount = None
Layout.Inst().PushDialogue(BannerDialogue( Lang("Mounting device...")))
Layout.Inst().Refresh()
Layout.Inst().DoUpdate()
self.vdiMount = MountVDIDirectly(self.vdi, self.Custom('mode'))
Layout.Inst().PopDialogue()
Layout.Inst().PushDialogue(BannerDialogue( Lang("Scanning device...")))
Layout.Inst().Refresh()
Layout.Inst().DoUpdate()
self.fileList = self.vdiMount.Scan(self.Custom('searchregexp'), 500) # Limit number of files to avoid colossal menu
Layout.Inst().PopDialogue()
self.ChangeState('FILES')
except USBNotFormatted:
Layout.Inst().PopDialogue()
self.ChangeState('USBNOTFORMATTED')
except USBNotMountable:
Layout.Inst().PopDialogue()
self.ChangeState('USBNOTMOUNTABLE')
except Exception as e:
try:
self.PreExitActions()
except Exception:
pass # Ignore failue
Layout.Inst().PopDialogue()
Layout.Inst().PushDialogue(InfoDialogue( Lang("Operation Failed"), Lang(e)))
def HandleFileChoice(self, inChoice):
if inChoice is None:
self.ChangeState('CUSTOM')
else:
FileUtils.AssertSafeLeafname(self.fileList[inChoice])
self.filename = self.fileList[inChoice]
self.ChangeState('CONFIRM')
def PreExitActions(self):
if self.vdiMount is not None:
self.vdiMount.Unmount()
self.vdiMount = None
class InputDialogue(Dialogue):
def __init__(self, inLayout = None, inParent = None):
Dialogue.__init__(self, inLayout, inParent)
pane = self.NewPane(DialoguePane(self.parent))
pane.TitleSet(self.Custom('title'))
pane.AddBox()
self.UpdateFields()
pane.InputIndexSet(0)
def Custom(self, inKey):
return self.custom.get(inKey, None)
def UpdateFields(self):
pane = self.Pane()
pane.ResetFields()
if self.Custom('info') is not None:
pane.AddWrappedTextField(self.Custom('info'))
pane.NewLine()
for field in self.Custom('fields'):
pane.AddInputField(*field)
pane.AddKeyHelpField( {
Lang("<Enter>") : Lang("OK"),
Lang("<Esc>") : Lang("Cancel")
})
def HandleCommit(self, inValues): # Override this
Layout.Inst().PopDialogue()
def HandleKey(self, inKey):
handled = True
pane = self.Pane()
if inKey == 'KEY_ESCAPE':
Layout.Inst().PopDialogue()
elif inKey == 'KEY_ENTER':
if not pane.IsLastInput():
pane.ActivateNextInput()
else:
try:
Layout.Inst().PopDialogue()
Layout.Inst().DoUpdate()
# The next line will work only in subclasses which override HandleCommit
# accordingly to return a tuple. The local self.HandleCommit returns None.
# FIXME: Update the local self.HandleCommit here accordingly to reflect this:
# pylint: disable-next=assignment-from-no-return,unpacking-non-sequence
title, info = self.HandleCommit(self.Pane().GetFieldValues())
Layout.Inst().PushDialogue(InfoDialogue( title, info))
except Exception as e:
Layout.Inst().PushDialogue(InfoDialogue( Lang('Failed: ')+Lang(e)))
elif inKey == 'KEY_TAB':
pane.ActivateNextInput()
elif inKey == 'KEY_BTAB': # BTAB not available on all platforms
pane.ActivatePreviousInput()
elif pane.CurrentInput().HandleKey(inKey):
pass # Leave handled as True
else:
handled = False
return handled
class SRDialogue(Dialogue):
def __init__(self):
Dialogue.__init__(self)
self.ChangeState('INITIAL')
def Custom(self, inKey):
return self.custom.get(inKey, None)
def BuildPaneBase(self):
pane = self.NewPane(DialoguePane(self.parent))
pane.TitleSet(self.Custom('title'))
pane.AddBox()
def BuildPaneINITIAL(self):
data = Data.Inst()
data.Update() # Make sure SR list is up to date
self.choices = SRDataUtils.SRList(self.Custom('mode'), self.Custom('capabilities'))
choiceDefs = []
for choice in self.choices:
choiceDefs.append(ChoiceDef(choice.name, lambda: self.HandleSRChoice(self.srMenu.ChoiceIndex()) ) )
if len(choiceDefs) == 0:
choiceDefs.append(ChoiceDef(Lang('<No suitable SRs available>'), lambda: None)) # Avoid empty menu
self.srMenu = Menu(self, None, Lang("Select SR"), choiceDefs)
self.BuildPaneBase()
self.UpdateFields()
def ChangeState(self, inState):
self.state = inState
getattr(self, 'BuildPane'+self.state)() # Despatch method named 'BuildPane'+self.state
def UpdateFields(self):
self.Pane().ResetPosition()
getattr(self, 'UpdateFields'+self.state)() # Despatch method named 'UpdateFields'+self.state
def UpdateFieldsINITIAL(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.Custom('prompt'))
pane.AddMenuField(self.srMenu)
pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Cancel"), Lang("<F5>") : Lang("Refresh") } )
def HandleKey(self, inKey):
handled = False
if hasattr(self, 'HandleKey'+self.state):
handled = getattr(self, 'HandleKey'+self.state)(inKey)
if not handled and inKey == 'KEY_ESCAPE':
Layout.Inst().PopDialogue()
handled = True
return handled
def HandleKeyINITIAL(self, inKey):
handled = self.srMenu.HandleKey(inKey)
if not handled and inKey == 'KEY_F(5)':
Data.Inst().Update()
self.BuildPaneINITIAL() # Updates menu
Layout.Inst().Refresh()
handled = True
return handled
def HandleSRChoice(self, inChoice):
self.DoAction(self.choices[inChoice].sr)
class ProgressDialogue(Dialogue):
def __init__(self, inTask, inText, *args, **kwargs):
Dialogue.__init__(self)
self.task = inTask
self.text = inText
# OnComplete is a function to call when the task completes
# Used for getting task result and do something after the task completes
self.OnComplete = kwargs.get('OnComplete', None)
self.args = args # Arguments to pass to OnComplete
self.ChangeState('INITIAL')
def BuildPane(self):
pane = self.NewPane(DialoguePane(self.parent))
pane.AddBox()
def UpdateFieldsINITIAL(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.text)
try:
progressVal = self.task.ProgressValue()
progressStr = str(int(100*progressVal))+'%'
durationSecs = self.task.DurationSecs()
elapsedStr = TimeUtils.DurationString(durationSecs)
except Exception as e:
progressStr = Lang('<Unavailable>')
elapsedStr = Lang('<Unavailable>')
pane.AddWrappedTextField(Lang('Time', 16) + elapsedStr)
pane.AddWrappedTextField(Lang('Progress', 16) + progressStr)
helpKeys = { Lang("<Enter>") : Lang("Hide This Window") }
if self.task.CanCancel():
helpKeys[ Lang('<Esc>') ]= Lang('Cancel Operation')
pane.AddKeyHelpField( helpKeys )
def UpdateFieldsCANCEL(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.text)
pane.AddWrappedBoldTextField(Lang('Attempting to cancel operation...'))
pane.NewLine()
try:
progressVal = self.task.ProgressValue()
progressStr = str(int(100*progressVal))+'%'
durationSecs = self.task.DurationSecs()
elapsedStr = TimeUtils.DurationString(durationSecs)
except Exception as e:
progressStr = Lang('<Unavailable>')
elapsedStr = Lang('<Unavailable>')
pane.AddWrappedTextField(Lang('Time', 16) + elapsedStr)
pane.AddWrappedTextField(Lang('Progress', 16) + progressStr)
helpKeys = { Lang("<Enter>") : Lang("Hide This Window") }
pane.AddKeyHelpField( helpKeys )
def UpdateFieldsCOMPLETE(self):
pane = self.Pane()
pane.ResetFields()
pane.AddTitleField(self.text)
message = self.task.Message()
pane.AddWrappedBoldTextField(message)
pane.NewLine()
progressStr = '100%'
try:
durationSecs = self.task.DurationSecs()
elapsedStr = TimeUtils.DurationString(durationSecs)
except Exception as e:
elapsedStr = Lang(e)
pane.AddWrappedTextField(Lang('Time', 16) + elapsedStr)
pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK") } )
def UpdateFields(self):
if self.state != 'COMPLETE' and not self.task.IsPending():
self.HandleCompletion()
self.Pane().ResetPosition()
getattr(self, 'UpdateFields'+self.state)() # Despatch method named 'UpdateFields'+self.state
def LiveUpdateFields(self):
self.UpdateFields()
def ChangeState(self, inState):
self.state = inState
self.BuildPane()
self.UpdateFields()
def HandleKey(self, inKey):
handled = True
if inKey == 'KEY_ESCAPE':
if self.task.CanCancel():
self.task.Cancel()
self.ChangeState('CANCEL')
else:
Layout.Inst().PopDialogue()
elif inKey == 'KEY_ENTER':
Layout.Inst().PopDialogue()
else:
handled = False
return True
def HandleCompletion(self):
# This method is called from UpdateFields, so shouldn't pop the dialogue, etc.
self.ChangeState('COMPLETE')
if self.OnComplete:
self.OnComplete(self.task, *self.args)
class DialogueUtils:
# Helper for activate
@classmethod
def AuthenticatedOnly(cls, inFunc):
if not Auth.Inst().IsAuthenticated():
Layout.Inst().PushDialogue(LoginDialogue(Lang('Please log in to perform this function'), inFunc))
else:
inFunc()
@classmethod
def AuthenticatedOrPasswordUnsetOnly(cls, inFunc):
if Auth.Inst().IsPasswordSet():
cls.AuthenticatedOnly(inFunc)
else:
inFunc()