auf-libreoffice-extension 1204.5
[macros-openoffice.git] / auf-libreoffice-extension / extension / DialogHandler.py
1 #!
2 # -*- coding: utf_8 -*-
3 """
4 Extension pour faciliter l'intégration du logiciel Coda.
5 Gestion de la fenêtre des options.
6
7 Copyright : Agence universitaire de la Francophonie
8 Licence : GNU General Public Licence, version 2
9 Auteur : Jean Christophe André
10 Date de création : juillet 2009
11 """
12 import uno
13 import unohelper
14
15 # interfaces
16 from com.sun.star.lang import XServiceInfo
17 from com.sun.star.awt import XContainerWindowEventHandler
18
19 # main class
20 class DialogHandler(unohelper.Base, XServiceInfo, XContainerWindowEventHandler):
21 def __init__(self, ctx):
22 self.ctx = ctx;
23 self.cp = self.ctx.ServiceManager.createInstanceWithContext(
24 "com.sun.star.configuration.ConfigurationProvider", self.ctx)
25 # options AUF
26 node = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
27 node.Name = "nodepath"
28 node.Value = "/org.auf.openoffice.macros/General"
29 self.node = node
30 self.cfg_names = ("MoinMoinOldStyle", "MoinMoinVertOnMulti", )
31 # options OpenOffice
32 node = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
33 node.Name = "nodepath"
34 node.Value = "/org.openoffice.VCL/Settings/Transfer"
35 self.nodeVCL = node
36 self.cfg_namesVCL = ("SelectionTimeout",)
37 return
38
39 # XServiceInfo
40 def supportsService(self, name):
41 return False
42
43 # XServiceInfo
44 def getImplementationName(self):
45 return "org.auf.openoffice.macros.DialogHandler"
46
47 # XServiceInfo
48 def getSupportedServiceNames(self):
49 return ()
50
51 # XContainerWindowEventHandler
52 def getSupportedMethodNames(self):
53 return ("external_event",)
54
55 # XContainerWindowEventHandler
56 def callHandlerMethod(self, window, eventObject, method):
57 if method == "external_event":
58 try:
59 self.handleExternalEvent(window, eventObject)
60 except:
61 pass
62 return True
63
64 def handleExternalEvent(self, window, eventName):
65 if eventName == "ok":
66 self.saveData(window)
67 elif eventName == "back":
68 self.loadData(window, "back")
69 elif eventName == "initialize":
70 self.loadData(window, "initialize")
71 return True
72
73 # load and set the data
74 def loadData(self, window, ev):
75 name = window.getModel().Name
76 if name != "MacrosDialog":
77 return
78 settings = self.configreader()
79 if not settings:
80 return
81 # options AUF
82 for name in self.cfg_names:
83 window.getControl(name).setState(int(settings.get(name, True)))
84 # options OpenOffice
85 for name in self.cfg_namesVCL:
86 window.getControl(name).setValue(settings.get(name, ''))
87 return
88
89 # making the save data
90 def saveData(self,window):
91 name = window.getModel().Name
92 if name != "MacrosDialog":
93 return
94 settings = {}
95 # options AUF
96 for name in self.cfg_names:
97 settings[name] = bool(window.getControl(name).State)
98 # options OpenOffice
99 for name in self.cfg_namesVCL:
100 settings[name] = str(int(window.getControl(name).Value))
101 self.configwriter(settings)
102 return
103
104 # read configuration
105 def configreader(self):
106 settings = {}
107 # options AUF
108 try:
109 ConfigReader = self.cp.createInstanceWithArguments(
110 "com.sun.star.configuration.ConfigurationAccess",
111 (self.node,))
112 for name in self.cfg_names:
113 settings[name] = ConfigReader.getByName(name)
114 except:
115 pass
116 # options OpenOffice
117 try:
118 ConfigReader = self.cp.createInstanceWithArguments(
119 "com.sun.star.configuration.ConfigurationAccess",
120 (self.nodeVCL,))
121 for name in self.cfg_namesVCL:
122 settings[name] = ConfigReader.getByName(name)
123 except:
124 pass
125 return settings
126
127 # write configuration
128 def configwriter(self, settings):
129 # options AUF
130 try:
131 ConfigWriter = self.cp.createInstanceWithArguments(
132 "com.sun.star.configuration.ConfigurationUpdateAccess",
133 (self.node,))
134 for name in self.cfg_names:
135 ConfigWriter.replaceByName(name, settings[name])
136 ConfigWriter.commitChanges()
137 except:
138 pass
139 # options OpenOffice
140 try:
141 ConfigWriter = self.cp.createInstanceWithArguments(
142 "com.sun.star.configuration.ConfigurationUpdateAccess",
143 (self.nodeVCL,))
144 for name in self.cfg_namesVCL:
145 ConfigWriter.replaceByName(name, settings[name])
146 ConfigWriter.commitChanges()
147 except:
148 pass
149
150 # uno implementation
151 g_ImplementationHelper = unohelper.ImplementationHelper()
152 g_ImplementationHelper.addImplementation( \
153 DialogHandler, "org.auf.openoffice.macros.DialogHandler", \
154 ("org.auf.openoffice.macros.DialogHandler",),)