Preferences/Shortcuts.py

changeset 595
7d2c8346021c
parent 537
72b32daeb8d6
child 791
9ec2ac20e54e
equal deleted inserted replaced
594:cded89ee3e30 595:7d2c8346021c
5 5
6 """ 6 """
7 Module implementing functions dealing with keyboard shortcuts. 7 Module implementing functions dealing with keyboard shortcuts.
8 """ 8 """
9 9
10 import io
11
12 from PyQt4.QtCore import * 10 from PyQt4.QtCore import *
13 from PyQt4.QtGui import * 11 from PyQt4.QtGui import *
14 12
15 from E5Gui.E5Application import e5App 13 from E5Gui.E5Application import e5App
16 from E5Gui import E5MessageBox 14 from E5Gui import E5MessageBox
17 15
18 from Preferences import Prefs, syncPreferences 16 from Preferences import Prefs, syncPreferences
19 17
20 from E5XML.XMLUtilities import make_parser 18 from E5XML.ShortcutsReader import ShortcutsReader
21 from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError
22 from E5XML.ShortcutsHandler import ShortcutsHandler
23 from E5XML.ShortcutsWriter import ShortcutsWriter 19 from E5XML.ShortcutsWriter import ShortcutsWriter
24 from E5XML.XMLEntityResolver import XMLEntityResolver
25 20
26 def __readShortcut(act, category, prefClass): 21 def __readShortcut(act, category, prefClass):
27 """ 22 """
28 Private function to read a single keyboard shortcut from the settings. 23 Private function to read a single keyboard shortcut from the settings.
29 24
187 def exportShortcuts(fn): 182 def exportShortcuts(fn):
188 """ 183 """
189 Module function to export the keyboard shortcuts for the defined QActions. 184 Module function to export the keyboard shortcuts for the defined QActions.
190 185
191 @param fn filename of the export file (string) 186 @param fn filename of the export file (string)
192 @return flag indicating success 187 """
193 """ 188 # let the plugin manager create on demand plugin objects
194 try: 189 pm = e5App().getObject("PluginManager")
195 if fn.lower().endswith("e4kz"): 190 pm.initOnDemandPlugins()
196 try: 191
197 import gzip 192 f = QFile(fn)
198 except ImportError: 193 if f.open(QIODevice.WriteOnly):
199 E5MessageBox.critical(None,
200 QApplication.translate("Shortcuts", "Export Keyboard Shortcuts"),
201 QApplication.translate("Shortcuts",
202 """Compressed keyboard shortcut files"""
203 """ not supported. The compression library is missing."""))
204 return 0
205 f = io.StringIO()
206 else:
207 f = open(fn, "w", encoding = "utf-8")
208
209 ShortcutsWriter(f).writeXML() 194 ShortcutsWriter(f).writeXML()
210
211 if fn.lower().endswith("e4kz"):
212 g = gzip.open(fn, "wb")
213 g.write(f.getvalue().encode("utf-8"))
214 g.close()
215 f.close() 195 f.close()
216 return True 196 else:
217 except IOError: 197 E5MessageBox.critical(None,
218 return False 198 QApplication.translate("Shortcuts", "Export Keyboard Shortcuts"),
199 QApplication.translate("Shortcuts",
200 "<p>The keyboard shortcuts could not be written to file <b>{0}</b>.</p>")
201 .format(fn))
219 202
220 def importShortcuts(fn): 203 def importShortcuts(fn):
221 """ 204 """
222 Module function to import the keyboard shortcuts for the defined E5Actions. 205 Module function to import the keyboard shortcuts for the defined E5Actions.
223 206
224 @param fn filename of the import file (string) 207 @param fn filename of the import file (string)
225 @return flag indicating success 208 """
226 """ 209 # let the plugin manager create on demand plugin objects
227 try: 210 pm = e5App().getObject("PluginManager")
228 if fn.lower().endswith("kz"): 211 pm.initOnDemandPlugins()
229 try: 212
230 import gzip 213 f = QFile(fn)
231 except ImportError: 214 if f.open(QIODevice.ReadOnly):
232 E5MessageBox.critical(None, 215 reader = ShortcutsReader(f)
233 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"), 216 reader.readXML()
234 QApplication.translate("Shortcuts", 217 f.close()
235 """Compressed keyboard shortcut files""" 218 if not reader.hasError():
236 """ not supported. The compression library is missing.""")) 219 shortcuts = reader.getShortcuts()
237 return False 220 setActions(shortcuts)
238 g = gzip.open(fn, "rb") 221 saveShortcuts()
239 f = io.StringIO(g.read().decode("utf-8")) 222 syncPreferences()
240 g.close() 223 else:
241 else:
242 f = open(fn, "r", encoding = "utf-8")
243 try:
244 f.readline()
245 dtdLine = f.readline()
246 finally:
247 f.close()
248 except IOError:
249 E5MessageBox.critical(None, 224 E5MessageBox.critical(None,
250 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"), 225 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
251 QApplication.translate("Shortcuts", 226 QApplication.translate("Shortcuts",
252 "<p>The keyboard shortcuts could not be read from file <b>{0}</b>.</p>") 227 "<p>The keyboard shortcuts could not be read from file <b>{0}</b>.</p>")
253 .format(fn)) 228 .format(fn))
254 return False 229 return
255
256 if fn.lower().endswith("kz"):
257 # work around for a bug in xmlproc
258 validating = False
259 else:
260 validating = dtdLine.startswith("<!DOCTYPE")
261 parser = make_parser(validating)
262 handler = ShortcutsHandler()
263 er = XMLEntityResolver()
264 eh = XMLErrorHandler()
265
266 parser.setContentHandler(handler)
267 parser.setEntityResolver(er)
268 parser.setErrorHandler(eh)
269
270 try:
271 if fn.lower().endswith("kz"):
272 try:
273 import gzip
274 except ImportError:
275 E5MessageBox.critical(None,
276 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
277 QApplication.translate("Shortcuts",
278 """Compressed keyboard shortcut files"""
279 """ not supported. The compression library is missing."""))
280 return False
281 g = gzip.open(fn, "rb")
282 f = io.StringIO(g.read().decode("utf-8"))
283 g.close()
284 else:
285 f = open(fn, "r", encoding = "utf-8")
286 try:
287 try:
288 parser.parse(f)
289 except UnicodeEncodeError:
290 f.seek(0)
291 buf = io.StringIO(f.read())
292 parser.parse(buf)
293 finally:
294 f.close()
295 except IOError:
296 E5MessageBox.critical(None,
297 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
298 QApplication.translate("Shortcuts",
299 "<p>The keyboard shortcuts could not be read from file <b>{0}</b>.</p>")
300 .format(fn))
301 return False
302
303 except XMLFatalParseError:
304 E5MessageBox.critical(None,
305 QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
306 QApplication.translate("Shortcuts",
307 "<p>The keyboard shortcuts file <b>{0}</b> has invalid contents.</p>")
308 .format(fn))
309 eh.showParseMessages()
310 return False
311
312 eh.showParseMessages()
313
314 shortcuts = handler.getShortcuts()
315
316 setActions(shortcuts)
317
318 saveShortcuts()
319 syncPreferences()
320
321 return True
322 230
323 def __setAction(actions, sdict): 231 def __setAction(actions, sdict):
324 """ 232 """
325 Private function to write a single keyboard shortcut to the settings. 233 Private function to write a single keyboard shortcut to the settings.
326 234

eric ide

mercurial