Rev 1550 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1549 | subik | 1 | #include "pluginmanager.h" |
2 | //include "pluginmanager.moc" |
||
3 | #include <dlfcn.h> |
||
4 | #include <iostream> |
||
5 | #include <qdir.h> |
||
6 | #include "scribus.h" |
||
7 | #include "menumanager.h" |
||
8 | #include "scraction.h" |
||
9 | #include "splash.h" |
||
10 | #include "mpalette.h" |
||
11 | #include "tree.h" |
||
12 | |||
13 | extern ScribusApp *ScApp; |
||
14 | |||
15 | |||
16 | PluginManager::PluginManager() |
||
17 | { |
||
18 | dllInput = ""; |
||
19 | dllReturn = ""; |
||
20 | } |
||
21 | |||
22 | PluginManager::~PluginManager() |
||
23 | { |
||
24 | } |
||
25 | |||
26 | void PluginManager::initPlugs() |
||
27 | { |
||
28 | QString nam = ""; |
||
29 | int id = 0; |
||
30 | int ty = 0; |
||
31 | int menid = 0; |
||
32 | struct PluginData pda; |
||
33 | #if defined(__hpux) |
||
34 | QDir d(PLUGINDIR, "*.sl*", QDir::Name, QDir::Files | QDir::Executable | QDir::NoSymLinks); |
||
35 | #else |
||
36 | QDir d(PLUGINDIR, "*.so*", QDir::Name, QDir::Files | QDir::Executable | QDir::NoSymLinks); |
||
37 | #endif |
||
38 | if ((d.exists()) && (d.count() != 0)) |
||
39 | { |
||
40 | ScApp->scrMenuMgr->addMenuSeparator("Extras"); |
||
41 | for (uint dc = 0; dc < d.count(); ++dc) |
||
42 | { |
||
43 | pda.Zeiger = 0; |
||
44 | pda.Datei = ""; |
||
45 | pda.Name = ""; |
||
46 | pda.Typ = 0; |
||
47 | pda.MenuID = 0; |
||
48 | if (DLLname(d[dc], &nam, &ty, &pda.Zeiger, &id, &pda.actName, &pda.actKeySequence, &pda.actMenu, &pda.actMenuAfterName, &pda.actEnabledOnStartup)) |
||
49 | { |
||
50 | if (ty == 1) |
||
51 | { |
||
52 | qDebug(QString("Type %1 plugins not supported anymore").arg(ty)); |
||
53 | break; |
||
54 | } |
||
55 | |||
56 | pda.Name = nam; |
||
57 | pda.Datei = d[dc]; |
||
58 | pda.Typ = ty; |
||
59 | if (ty < 5 && ty!=4) |
||
60 | pda.MenuID = menid; |
||
61 | if (ty==4 || ty==6 || ty==7) |
||
62 | { |
||
63 | //Add in ScrAction based plugin linkage |
||
64 | //Insert DLL Action into Dictionary with values from plugin interface |
||
65 | ScApp->scrActions.insert(pda.actName, new ScrAction(ScrAction::DLL, QIconSet(), pda.Name, QKeySequence(pda.actKeySequence), this, pda.actName, id)); |
||
66 | |||
67 | if (ScApp->scrActions[pda.actName]) |
||
68 | { |
||
69 | ScApp->scrActions[pda.actName]->setEnabled(pda.actEnabledOnStartup); |
||
70 | //Connect DLL Action's activated signal with ID to Scribus DLL loader |
||
71 | connect( ScApp->scrActions[pda.actName], SIGNAL(activatedData(int)) , ScApp->pluginManager, SLOT(callDLLBySlot(int)) ); |
||
72 | //Get the menu manager to add the DLL's menu item to the right menu, after the chosen existing item |
||
73 | if (QString(pda.actMenuAfterName).length()==0) |
||
74 | ScApp->scrMenuMgr->addMenuItem(ScApp->scrActions[pda.actName], pda.actMenu); |
||
75 | else |
||
76 | ScApp->scrMenuMgr->addMenuItemAfter(ScApp->scrActions[pda.actName], pda.actMenu, pda.actMenuAfterName); |
||
77 | } |
||
78 | } |
||
79 | pluginMap.insert(id, pda); |
||
80 | if (ScApp->splashScreen != NULL) |
||
81 | ScApp->splashScreen->setStatus( tr("Loading:")+" "+nam); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | void PluginManager::callDLLBySlot(int pluginID) |
||
88 | { |
||
89 | //Run old type 2 Import pre call code |
||
90 | if (pluginMap[pluginID].Typ==7) |
||
91 | { |
||
92 | if (ScApp->HaveDoc) |
||
93 | ScApp->doc->OpenNodes = ScApp->outlinePalette->buildReopenVals(); |
||
94 | } |
||
95 | |||
96 | callDLL(pluginID); |
||
97 | |||
98 | //Run old type 2 Import post call code |
||
99 | if (pluginMap[pluginID].Typ==7) |
||
100 | { |
||
101 | if (ScApp->HaveDoc) |
||
102 | { |
||
103 | ScApp->outlinePalette->BuildTree(ScApp->doc); |
||
104 | ScApp->outlinePalette->reopenTree(ScApp->doc->OpenNodes); |
||
105 | ScApp->propertiesPalette->updateCList(); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | void PluginManager::callDLL(int pluginID) |
||
111 | { |
||
112 | void *mo; |
||
113 | const char *error; |
||
114 | struct PluginData pda; |
||
115 | pda = pluginMap[pluginID]; |
||
116 | typedef void (*sdem)(QWidget *d, ScribusApp *plug); |
||
117 | sdem demo; |
||
118 | QString plugDir = PLUGINDIR; |
||
119 | if (pda.Typ != 4 && pda.Typ !=5) |
||
120 | { |
||
121 | plugDir += pda.Datei; |
||
122 | mo = dlopen(plugDir, RTLD_LAZY | RTLD_GLOBAL); |
||
123 | if (!mo) |
||
124 | { |
||
125 | std::cout << "Can't find Plug-in" << endl; |
||
126 | return; |
||
127 | } |
||
128 | } |
||
129 | else |
||
130 | mo = pda.Zeiger; |
||
131 | dlerror(); |
||
132 | demo = (sdem)dlsym(mo, "Run"); |
||
133 | if ((error = dlerror()) != NULL) |
||
134 | { |
||
135 | std::cout << "Can't find Symbol" << endl; |
||
136 | dlclose(mo); |
||
137 | return; |
||
138 | } |
||
139 | (*demo)(ScApp, ScApp); |
||
140 | // FIXME: how is the menu organized? (*demo)(ScApp->scrActions[pluginMap[pluginID].actName], ScApp); |
||
141 | if (pda.Typ != 4 && pda.Typ != 5) |
||
142 | dlclose(mo); |
||
143 | if (ScApp->HaveDoc) |
||
144 | ScApp->view->DrawNew(); |
||
145 | } |
||
146 | |||
147 | bool PluginManager::DLLexists(int pluginID) |
||
148 | { |
||
149 | return pluginMap.contains(pluginID); |
||
150 | } |
||
151 | |||
152 | // used anywhere? |
||
153 | void PluginManager::callDLLbyMenu(int pluginID) |
||
154 | { |
||
155 | QMap<int, PluginData>::Iterator it; |
||
156 | struct PluginData pda; |
||
157 | for (it = pluginMap.begin(); it != pluginMap.end(); ++it) |
||
158 | { |
||
159 | if (it.data().MenuID == pluginID) |
||
160 | { |
||
161 | callDLL(it.key()); |
||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | bool PluginManager::DLLname(QString name, QString *PName, int *typ, void **Zeig, int *idNr, QString *actName, QString *actKeySequence, QString *actMenu, QString *actMenuAfterName, bool *actEnabledOnStartup) |
||
168 | { |
||
169 | void *mo; |
||
170 | const char *error; |
||
171 | typedef QString (*sdem0)(); |
||
172 | typedef int (*sdem1)(); |
||
173 | typedef void (*sdem2)(QWidget *d, ScribusApp *plug); |
||
174 | typedef bool (*sdem3)(); |
||
175 | sdem0 demo; |
||
176 | sdem1 demo1; |
||
177 | sdem2 demo2; |
||
178 | sdem3 demo3; |
||
179 | QString plugName = PLUGINDIR; |
||
180 | plugName += name; |
||
181 | mo = dlopen(plugName, RTLD_LAZY | RTLD_GLOBAL); |
||
182 | if (!mo) |
||
183 | { |
||
184 | std::cout << dlerror() << endl; |
||
185 | return false; |
||
186 | } |
||
187 | dlerror(); |
||
188 | demo = (sdem0)dlsym(mo, "Name"); |
||
189 | if ((error = dlerror()) != NULL) |
||
190 | { |
||
191 | dlclose(mo); |
||
192 | return false; |
||
193 | } |
||
194 | *PName = (*demo)(); |
||
195 | dlerror(); |
||
196 | demo1 = (sdem1)dlsym(mo, "Type"); |
||
197 | if ((error = dlerror()) != NULL) |
||
198 | { |
||
199 | dlclose(mo); |
||
200 | return false; |
||
201 | } |
||
202 | *typ = (*demo1)(); |
||
203 | *Zeig = mo; |
||
204 | demo1 = (sdem1)dlsym(mo, "ID"); |
||
205 | if ((error = dlerror()) != NULL) |
||
206 | { |
||
207 | dlclose(mo); |
||
208 | return false; |
||
209 | } |
||
210 | *idNr = (*demo1)(); |
||
211 | //ScrAction based plugins |
||
212 | if (*typ==4 || *typ==6 || *typ==7) |
||
213 | { |
||
214 | demo = (sdem0)dlsym(mo, "actionName"); |
||
215 | if ((error = dlerror()) != NULL) |
||
216 | { |
||
217 | dlclose(mo); |
||
218 | return false; |
||
219 | } |
||
220 | *actName = (*demo)(); |
||
221 | dlerror(); |
||
222 | demo = (sdem0)dlsym(mo, "actionKeySequence"); |
||
223 | if ((error = dlerror()) != NULL) |
||
224 | { |
||
225 | dlclose(mo); |
||
226 | return false; |
||
227 | } |
||
228 | *actKeySequence = (*demo)(); |
||
229 | dlerror(); |
||
230 | demo = (sdem0)dlsym(mo, "actionMenu"); |
||
231 | if ((error = dlerror()) != NULL) |
||
232 | { |
||
233 | dlclose(mo); |
||
234 | return false; |
||
235 | } |
||
236 | *actMenu = (*demo)(); |
||
237 | dlerror(); |
||
238 | demo = (sdem0)dlsym(mo, "actionMenuAfterName"); |
||
239 | if ((error = dlerror()) != NULL) |
||
240 | { |
||
241 | dlclose(mo); |
||
242 | return false; |
||
243 | } |
||
244 | *actMenuAfterName = (*demo)(); |
||
245 | dlerror(); |
||
246 | demo3 = (sdem3)dlsym(mo, "actionEnabledOnStartup"); |
||
247 | if ((error = dlerror()) != NULL) |
||
248 | { |
||
249 | dlclose(mo); |
||
250 | return false; |
||
251 | } |
||
252 | *actEnabledOnStartup = (*demo3)(); |
||
253 | } |
||
254 | else |
||
255 | { |
||
256 | *actName = QString::null; |
||
257 | *actKeySequence = QString::null; |
||
258 | *actMenu = QString::null; |
||
259 | *actMenuAfterName = QString::null; |
||
260 | *actEnabledOnStartup = false; |
||
261 | } |
||
262 | if (*typ != 4 && *typ!=5) |
||
263 | dlclose(mo); |
||
264 | else |
||
265 | { |
||
266 | dlerror(); |
||
267 | demo2 = (sdem2)dlsym(mo, "InitPlug"); |
||
268 | if ((error = dlerror()) != NULL) |
||
269 | { |
||
270 | dlclose(mo); |
||
271 | return false; |
||
272 | } |
||
273 | (*demo2)(ScApp, ScApp); |
||
274 | // FIXME: how is the menu organized? (*demo2)(ScApp->scrActions[pluginMap[idNr]], ScApp); |
||
275 | } |
||
276 | |||
277 | return true; |
||
278 | } |
||
279 | |||
280 | void PluginManager::finalizePlugs() |
||
281 | { |
||
282 | const char *error; |
||
283 | struct PluginData pda; |
||
284 | typedef void (*sdem2)(); |
||
285 | sdem2 demo2; |
||
286 | for (QMap<int, PluginData>::Iterator it = pluginMap.begin(); it != pluginMap.end(); ++it) |
||
287 | { |
||
288 | if (it.data().Typ == 4 || it.data().Typ == 5) |
||
289 | { |
||
290 | dlerror(); |
||
291 | demo2 = (sdem2)dlsym(it.data().Zeiger, "CleanUpPlug"); |
||
292 | if ((error = dlerror()) != NULL) |
||
293 | { |
||
294 | dlclose(it.data().Zeiger); |
||
295 | continue; |
||
296 | } |
||
297 | else |
||
298 | { |
||
299 | (*demo2)(); |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | } |