forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configProfiles.py
485 lines (428 loc) · 18.3 KB
/
configProfiles.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
#gui/configProfiles.py
#A part of NonVisual Desktop Access (NVDA)
#Copyright (C) 2013-2018 NV Access Limited, Joseph Lee
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
import wx
import config
import api
import gui
from logHandler import log
import appModuleHandler
import globalVars
import guiHelper
class ProfilesDialog(wx.Dialog):
shouldSuspendConfigProfileTriggers = True
_instance = None
def __new__(cls, *args, **kwargs):
# Make this a singleton.
if ProfilesDialog._instance is None:
return super(ProfilesDialog, cls).__new__(cls, *args, **kwargs)
return ProfilesDialog._instance
def __init__(self, parent):
if ProfilesDialog._instance is not None:
return
ProfilesDialog._instance = self
# Translators: The title of the Configuration Profiles dialog.
super(ProfilesDialog, self).__init__(parent, title=_("Configuration Profiles"))
self.currentAppName = (gui.mainFrame.prevFocus or api.getFocusObject()).appModule.appName
self.profileNames = [None]
self.profileNames.extend(config.conf.listProfiles())
mainSizer = wx.BoxSizer(wx.VERTICAL)
sHelper = guiHelper.BoxSizerHelper(self,orientation=wx.VERTICAL)
profilesListGroupSizer = wx.StaticBoxSizer(wx.StaticBox(self), wx.HORIZONTAL)
profilesListGroupContents = wx.BoxSizer(wx.HORIZONTAL)
#contains the profile list and activation button in vertical arrangement.
changeProfilesSizer = wx.BoxSizer(wx.VERTICAL)
item = self.profileList = wx.ListBox(self,
choices=[self.getProfileDisplay(name, includeStates=True) for name in self.profileNames])
item.Bind(wx.EVT_LISTBOX, self.onProfileListChoice)
item.Selection = self.profileNames.index(config.conf.profiles[-1].name)
changeProfilesSizer.Add(item, proportion=1.0)
changeProfilesSizer.AddSpacer(guiHelper.SPACE_BETWEEN_BUTTONS_VERTICAL)
self.changeStateButton = wx.Button(self)
self.changeStateButton.Bind(wx.EVT_BUTTON, self.onChangeState)
self.AffirmativeId = self.changeStateButton.Id
self.changeStateButton.SetDefault()
changeProfilesSizer.Add(self.changeStateButton)
profilesListGroupContents.Add(changeProfilesSizer, flag = wx.EXPAND)
profilesListGroupContents.AddSpacer(guiHelper.SPACE_BETWEEN_ASSOCIATED_CONTROL_HORIZONTAL)
buttonHelper = guiHelper.ButtonHelper(wx.VERTICAL)
# Translators: The label of a button to create a new configuration profile.
newButton = buttonHelper.addButton(self, label=_("&New"))
newButton.Bind(wx.EVT_BUTTON, self.onNew)
# Translators: The label of a button to rename a configuration profile.
self.renameButton = buttonHelper.addButton(self, label=_("&Rename"))
self.renameButton.Bind(wx.EVT_BUTTON, self.onRename)
# Translators: The label of a button to delete a configuration profile.
self.deleteButton = buttonHelper.addButton(self, label=_("&Delete"))
self.deleteButton.Bind(wx.EVT_BUTTON, self.onDelete)
profilesListGroupContents.Add(buttonHelper.sizer)
profilesListGroupSizer.Add(profilesListGroupContents, border=guiHelper.BORDER_FOR_DIALOGS, flag=wx.ALL)
sHelper.addItem(profilesListGroupSizer)
# Translators: The label of a button to manage triggers
# in the Configuration Profiles dialog.
# See the Configuration Profiles section of the User Guide for details.
triggersButton = wx.Button(self, label=_("&Triggers..."))
triggersButton.Bind(wx.EVT_BUTTON, self.onTriggers)
# Translators: The label of a checkbox in the Configuration Profiles dialog.
self.disableTriggersToggle = wx.CheckBox(self, label=_("Temporarily d&isable all triggers"))
self.disableTriggersToggle.Value = not config.conf.profileTriggersEnabled
sHelper.addItem(guiHelper.associateElements(triggersButton,self.disableTriggersToggle))
# Translators: The label of a button to close a dialog.
closeButton = wx.Button(self, wx.ID_CLOSE, label=_("&Close"))
closeButton.Bind(wx.EVT_BUTTON, lambda evt: self.Close())
sHelper.addDialogDismissButtons(closeButton)
self.Bind(wx.EVT_CLOSE, self.onClose)
self.EscapeId = wx.ID_CLOSE
if globalVars.appArgs.secure:
for item in newButton, triggersButton, self.renameButton, self.deleteButton:
item.Disable()
self.onProfileListChoice(None)
mainSizer.Add(sHelper.sizer, flag=wx.ALL, border=guiHelper.BORDER_FOR_DIALOGS)
mainSizer.Fit(self)
self.Sizer = mainSizer
self.profileList.SetFocus()
self.CentreOnScreen()
def __del__(self):
ProfilesDialog._instance = None
def getProfileDisplay(self, name, includeStates=False):
# Translators: The item to select the user's normal configuration
# in the profile list in the Configuration Profiles dialog.
disp = name if name else _("(normal configuration)")
if includeStates:
disp += self.getProfileStates(name)
return disp
def getProfileStates(self, name):
try:
profile = config.conf.getProfile(name)
except KeyError:
return ""
states = []
editProfile = config.conf.profiles[-1]
if profile is editProfile:
# Translators: Reported for a profile which is being edited
# in the Configuration Profiles dialog.
states.append(_("editing"))
if name:
# This is a profile (not the normal configuration).
if profile.manual:
# Translators: Reported for a profile which has been manually activated
# in the Configuration Profiles dialog.
states.append(_("manual"))
if profile.triggered:
# Translators: Reported for a profile which is currently triggered
# in the Configuration Profiles dialog.
states.append(_("triggered"))
if states:
return " (%s)" % ", ".join(states)
return ""
def isProfileManual(self, name):
if not name:
return False
try:
profile = config.conf.getProfile(name)
except KeyError:
return False
return profile.manual
def onChangeState(self, evt):
sel = self.profileList.Selection
profile = self.profileNames[sel]
if self.isProfileManual(profile):
profile = None
try:
config.conf.manualActivateProfile(profile)
except:
# Translators: An error displayed when activating a configuration profile fails.
gui.messageBox(_("Error activating profile."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
return
self.Close()
def onNew(self, evt):
self.Disable()
NewProfileDialog(self).Show()
def onDelete(self, evt):
index = self.profileList.Selection
if gui.messageBox(
# Translators: The confirmation prompt displayed when the user requests to delete a configuration profile.
_("This profile will be permanently deleted. This action cannot be undone."),
# Translators: The title of the confirmation dialog for deletion of a configuration profile.
_("Confirm Deletion"),
wx.OK | wx.CANCEL | wx.ICON_QUESTION, self
) != wx.OK:
return
name = self.profileNames[index]
try:
config.conf.deleteProfile(name)
except:
log.debugWarning("", exc_info=True)
# Translators: An error displayed when deleting a configuration profile fails.
gui.messageBox(_("Error deleting profile."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
return
del self.profileNames[index]
self.profileList.Delete(index)
self.profileList.SetString(0, self.getProfileDisplay(None, includeStates=True))
self.profileList.Selection = 0
self.onProfileListChoice(None)
self.profileList.SetFocus()
def onProfileListChoice(self, evt):
sel = self.profileList.Selection
enable = sel > 0
name = self.profileNames[sel]
if self.isProfileManual(name):
# Translators: The label of the button to manually deactivate the selected profile
# in the Configuration Profiles dialog.
label = _("Manual deactivate")
else:
# Translators: The label of the button to manually activate the selected profile
# in the Configuration Profiles dialog.
label = _("Manual activate")
self.changeStateButton.Label = label
self.changeStateButton.Enabled = enable
if globalVars.appArgs.secure:
return
self.deleteButton.Enabled = enable
self.renameButton.Enabled = enable
def onRename(self, evt):
index = self.profileList.Selection
oldName = self.profileNames[index]
# Translators: The label of a field to enter a new name for a configuration profile.
with wx.TextEntryDialog(self, _("New name:"),
# Translators: The title of the dialog to rename a configuration profile.
_("Rename Profile"), value=oldName) as d:
if d.ShowModal() == wx.ID_CANCEL:
return
newName = api.filterFileName(d.Value)
try:
config.conf.renameProfile(oldName, newName)
except ValueError:
# Translators: An error displayed when renaming a configuration profile
# and a profile with the new name already exists.
gui.messageBox(_("That profile already exists. Please choose a different name."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
return
except:
log.debugWarning("", exc_info=True)
gui.messageBox(_("Error renaming profile."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
return
self.profileNames[index] = newName
self.profileList.SetString(index, self.getProfileDisplay(newName, includeStates=True))
self.profileList.Selection = index
self.profileList.SetFocus()
def onTriggers(self, evt):
self.Disable()
TriggersDialog(self).Show()
def getSimpleTriggers(self):
# Yields (spec, display, manualEdit)
yield ("app:%s" % self.currentAppName,
# Translators: Displayed for the configuration profile trigger for the current application.
# %s is replaced by the application executable name.
_("Current application (%s)") % self.currentAppName,
False)
# Translators: Displayed for the configuration profile trigger for say all.
yield "sayAll", _("Say all"), True
def onClose(self, evt):
if self.disableTriggersToggle.Value:
config.conf.disableProfileTriggers()
else:
config.conf.enableProfileTriggers()
self.Destroy()
# 7077: Nullify the instance flag, otherwise wxWidgets will think the dialog is active when it is gone.
ProfilesDialog._instance = None
def saveTriggers(self, parentWindow=None):
try:
config.conf.saveProfileTriggers()
except:
log.debugWarning("", exc_info=True)
# Translators: An error displayed when saving configuration profile triggers fails.
gui.messageBox(_("Error saving configuration profile triggers - probably read only file system."),
_("Error"), wx.OK | wx.ICON_ERROR, parent=parentWindow)
class TriggerInfo(object):
__slots__ = ("spec", "display", "profile")
def __init__(self, spec, display, profile):
self.spec = spec
self.display = display
self.profile = profile
class TriggersDialog(wx.Dialog):
def __init__(self, parent):
# Translators: The title of the configuration profile triggers dialog.
super(TriggersDialog, self).__init__(parent, title=_("Profile Triggers"))
mainSizer = wx.BoxSizer(wx.VERTICAL)
sHelper = guiHelper.BoxSizerHelper(self, orientation=wx.VERTICAL)
processed = set()
triggers = self.triggers = []
confTrigs = config.conf.triggersToProfiles
# Handle simple triggers.
for spec, disp, manualEdit in parent.getSimpleTriggers():
try:
profile = confTrigs[spec]
except KeyError:
profile = None
triggers.append(TriggerInfo(spec, disp, profile))
processed.add(spec)
# Handle all other triggers.
for spec, profile in confTrigs.iteritems():
if spec in processed:
continue
if spec.startswith("app:"):
# Translators: Displayed for a configuration profile trigger for an application.
# %s is replaced by the application executable name.
disp = _("%s application") % spec[4:]
else:
continue
triggers.append(TriggerInfo(spec, disp, profile))
# Translators: The label of the triggers list in the Configuration Profile Triggers dialog.
triggersText = _("Triggers")
triggerChoices = [trig.display for trig in triggers]
self.triggerList = sHelper.addLabeledControl(triggersText, wx.ListBox, choices=triggerChoices)
self.triggerList.Bind(wx.EVT_LISTBOX, self.onTriggerListChoice)
self.triggerList.Selection = 0
# Translators: The label of the profile list in the Configuration Profile Triggers dialog.
profileText = _("Profile")
profileChoices = [parent.getProfileDisplay(name) for name in parent.profileNames]
self.profileList = sHelper.addLabeledControl(profileText, wx.Choice, choices=profileChoices)
self.profileList.Bind(wx.EVT_CHOICE, self.onProfileListChoice)
closeButton = sHelper.addDialogDismissButtons(wx.Button(self, wx.ID_CLOSE, label=_("&Close")))
closeButton.Bind(wx.EVT_BUTTON, lambda evt: self.Close())
self.Bind(wx.EVT_CLOSE, self.onClose)
self.AffirmativeId = wx.ID_CLOSE
closeButton.SetDefault()
self.EscapeId = wx.ID_CLOSE
self.onTriggerListChoice(None)
mainSizer.Add(sHelper.sizer, border = guiHelper.BORDER_FOR_DIALOGS, flag=wx.ALL)
mainSizer.Fit(self)
self.Sizer = mainSizer
self.CentreOnScreen()
def onTriggerListChoice(self, evt):
trig = self.triggers[self.triggerList.Selection]
try:
self.profileList.Selection = self.Parent.profileNames.index(trig.profile)
except ValueError:
log.error("Trigger %s: invalid profile %s"
% (trig.spec, trig.profile))
self.profileList.Selection = 0
trig.profile = None
def onProfileListChoice(self, evt):
trig = self.triggers[self.triggerList.Selection]
trig.profile = self.Parent.profileNames[evt.Selection]
def onClose(self, evt):
confTrigs = config.conf.triggersToProfiles
for trig in self.triggers:
if trig.profile:
confTrigs[trig.spec] = trig.profile
else:
try:
del confTrigs[trig.spec]
except KeyError:
pass
self.Parent.saveTriggers(parentWindow=self)
self.Parent.Enable()
self.Destroy()
class NewProfileDialog(wx.Dialog):
def __init__(self, parent):
# Translators: The title of the dialog to create a new configuration profile.
super(NewProfileDialog, self).__init__(parent, title=_("New Profile"))
mainSizer = wx.BoxSizer(wx.VERTICAL)
sHelper = guiHelper.BoxSizerHelper(self, orientation=wx.VERTICAL)
sizer = wx.BoxSizer(wx.HORIZONTAL)
# Translators: The label of a field to enter the name of a new configuration profile.
profileNameText = _("Profile name:")
self.profileName = sHelper.addLabeledControl(profileNameText, wx.TextCtrl)
# Translators: The label of a radio button to specify that a profile will be used for manual activation
# in the new configuration profile dialog.
self.triggers = triggers = [(None, _("Manual activation"), True)]
triggers.extend(parent.getSimpleTriggers())
self.triggerChoice = sHelper.addItem(wx.RadioBox(self, label=_("Use this profile for:"),
choices=[trig[1] for trig in triggers]))
self.triggerChoice.Bind(wx.EVT_RADIOBOX, self.onTriggerChoice)
self.autoProfileName = ""
self.onTriggerChoice(None)
sHelper.addDialogDismissButtons(self.CreateButtonSizer(wx.OK | wx.CANCEL))
self.Bind(wx.EVT_BUTTON, self.onOk, id=wx.ID_OK)
self.Bind(wx.EVT_BUTTON, self.onCancel, id=wx.ID_CANCEL)
mainSizer.Add(sHelper.sizer, border = guiHelper.BORDER_FOR_DIALOGS, flag=wx.ALL)
mainSizer.Fit(self)
self.Sizer = mainSizer
self.profileName.SetFocus()
self.CentreOnScreen()
def onOk(self, evt):
confTrigs = config.conf.triggersToProfiles
spec, disp, manualEdit = self.triggers[self.triggerChoice.Selection]
if spec in confTrigs and gui.messageBox(
# Translators: The confirmation prompt presented when creating a new configuration profile
# and the selected trigger is already associated.
_("This trigger is already associated with another profile. "
"If you continue, it will be removed from that profile and associated with this one.\n"
"Are you sure you want to continue?"),
_("Warning"), wx.ICON_WARNING | wx.YES | wx.NO, self
) == wx.NO:
return
name = api.filterFileName(self.profileName.Value)
if not name:
return
try:
config.conf.createProfile(name)
except ValueError:
# Translators: An error displayed when the user attempts to create a configuration profile which already exists.
gui.messageBox(_("That profile already exists. Please choose a different name."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
return
except:
log.debugWarning("", exc_info=True)
# Translators: An error displayed when creating a configuration profile fails.
gui.messageBox(_("Error creating profile - probably read only file system."),
_("Error"), wx.OK | wx.ICON_ERROR, self)
self.onCancel(evt)
return
if spec:
confTrigs[spec] = name
self.Parent.saveTriggers(parentWindow=self)
parent = self.Parent
if manualEdit:
if gui.messageBox(
# Translators: The prompt asking the user whether they wish to
# manually activate a configuration profile that has just been created.
_("To edit this profile, you will need to manually activate it. "
"Once you have finished editing, you will need to manually deactivate it to resume normal usage.\n"
"Do you wish to manually activate it now?"),
# Translators: The title of the confirmation dialog for manual activation of a created profile.
_("Manual Activation"), wx.YES | wx.NO | wx.ICON_QUESTION, self
) == wx.YES:
config.conf.manualActivateProfile(name)
else:
# Return to the Profiles dialog.
parent.profileNames.append(name)
parent.profileList.Append(name)
parent.profileList.Selection = parent.profileList.Count - 1
parent.onProfileListChoice(None)
parent.profileList.SetFocus()
parent.Enable()
self.Destroy()
return
else:
# Ensure triggers are enabled so the user can edit the profile.
config.conf.enableProfileTriggers()
# The user is done with the Profiles dialog;
# let them get on with editing the profile.
parent.Destroy()
# Also nullify the instance flag as the profiles dialog itself is dead.
ProfilesDialog._instance = None
def onCancel(self, evt):
self.Parent.Enable()
self.Destroy()
def onTriggerChoice(self, evt):
spec, disp, manualEdit = self.triggers[self.triggerChoice.Selection]
if not spec:
# Manual activation shouldn't guess a name.
name = ""
elif spec.startswith("app:"):
name = spec[4:]
else:
name = disp
if self.profileName.Value == self.autoProfileName:
# The user hasn't changed the automatically filled value.
self.profileName.Value = name
self.profileName.SelectAll()
self.autoProfileName = name