Subversion Repositories Scribus

Rev

Rev 3193 | Rev 3213 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2972 craig 1
#ifndef _SCPLUGIN_H
2
#define _SCPLUGIN_H
3
 
4
#include "qobject.h"
5
#include "qstring.h"
6
#include "qpixmap.h"
7
#include "qdatetime.h"
3193 craig 8
#include "qiconset.h"
2972 craig 9
#include "scribusapi.h"
10
 
11
class QWidget;
12
class ScribusApp;
13
class DeferredTask;
14
 
15
/*
16
 * The idea here is that a ScPlugin is an information class stored by the
17
 * plug-in and returned to callers. It is a singleton that is always owned
18
 * by the plugin implementation.
19
 *
20
 * Note that the implementation below really creates two quite different
21
 * kinds of plugins. One for import/export of data which is generally
22
 * loaded, run, and unloaded. A second for persistent plugins that are loaded
23
 * (generally when the app starts) and remain resident.
24
 *
25
 * More plugin classes - such as plugins for text import and document import
26
 * - may be added in future.
2975 craig 27
 *
28
 * In addition to the API described here, a plugin is expected to implement
29
 * the following "extern C" functions, where 'pluginname' is the base name
30
 * of the plug-in:
31
 *
3207 craig 32
 * int pluginname_getPluginAPIVersion();
2975 craig 33
 *    Return an integer indicating the plug-in API version implemented.
34
 *    If the version does not exactly match the running plugin API version,
35
 *    the plugin will not be initialised.
36
 *
37
 * ScPlugin* pluginname_getPlugin();
38
 *    Create and return an instance of the plug-ins ScPlugin subclass.
39
 *    Nothing should be done here that's overly slow or uses much memory,
40
 *    and the plugin should not initialize its self here beyond what is
41
 *    required to return a ScPlugin instance.
42
 *
43
 * void pluginname_freePlugin(ScPlugin* plugin);
44
 *    Free the scplugin instance returned earlier.
45
 *
3193 craig 46
 * TODO: define "standard" plug-ins better
47
 * TODO: better discovery of what plug-ins can import / export what formats, file exts, names, etc.
48
 *
2972 craig 49
 */
50
 
51
class SCRIBUS_API ScPlugin : public QObject
52
{
53
        Q_OBJECT
54
 
55
        /**
56
         * @brief ScPlugin provides an interface to ask plugins for information
57
         * about themselves.
58
         *
59
         * Plugins return a pointer to their ScPlugin instance when
60
         * asked for it by the plugin manager. This class provides general
61
         * information about the plugin. It should not generally be inherited
62
         * directly by plugins - use one of the provided base classes instead.
63
         */
64
        public:
65
 
3207 craig 66
                /** \brief Human readable enumertion of the plugin types
67
                 *
68
                 * This might get replaced with checking inheritance
69
                 * with QObject.
70
                 *
71
                 * PluginType_Persistent:
72
                 *    The plug-in is loaded and initialised on app startup. No
73
                 *    automatic connection to the GUI is made, and the plugin
74
                 *    essentially becomes part of the application, maintaining
75
                 *    its state as long as it's loaded.
76
                 *
77
                 * PluginType_Import:
78
                 *    The plug-in provides the facility to import file type(s).
79
                 *    It keeps no state.
80
                 *
81
                 * PluginType_Export:
82
                 *    The plugin provides a facility to export file type(s).
83
                 *    It keeps no state.
84
                 *
85
                 * PluginType_Action:
86
                 *    The plugin has a single specific action it can take that
87
                 *    is not import or export. It's automatically hooked into
88
                 *    the GUI and keeps no state.
89
                 */
2972 craig 90
                enum PluginType {
2975 craig 91
                        PluginType_Persistent = 4,
92
                        PluginType_Import = 7,
93
                        PluginType_Export = 8,
3207 craig 94
                        PluginType_Action = 9
2972 craig 95
                };
96
 
97
                // A struct providing the information returned by getAboutData(), for
98
                // the "About Plug-ins" menu item.
99
                struct AboutData
100
                {
101
                        // Author name(s) and email address(es)
102
                        QString authors;
103
                        // One-liner description of the function of the plugin
104
                        QString shortDescription;
105
                        // Longer description of the plugin
106
                        QString description;
107
                        // Text plug-in version string
108
                        QString version;
109
                        // Date of the release of this version
110
                        QDateTime releaseDate;
111
                        // Copyright string
112
                        QString copyright;
113
                        // License text / name / URL / whatever. Please don't include
114
                        // the GPL here, just reference it. Remember that Scribus is
115
                        // licensed under the GPL, so plug-in authors must take that
116
                        // into account.
117
                        QString license;
118
                };
119
 
2975 craig 120
                /**
121
                 * @brief ctor, returns a new ScPlugin instance
122
                 *
3193 craig 123
                 * @param pluginType      plugin type enum, used by plugin
124
                 *                        manager to identify plugin.
2975 craig 125
                 *
126
                 * Only the actual plugin implmementation should call this, from
127
                 * its setup function.
128
                 */
3207 craig 129
                ScPlugin(PluginType pluginType);
2975 craig 130
 
131
                /** @brief Pure virtual destructor - this is an abstract class */
132
                virtual ~ScPlugin() = 0;
133
 
2972 craig 134
                // Plug-in type, inited by ctor.
135
                const PluginType pluginType;
136
 
137
                // Plug-in's human-readable, translated name. Please don't use this for
3193 craig 138
                // anything except display to the user.
3207 craig 139
                virtual const QString fullTrName() const = 0;
2972 craig 140
 
141
                // Methods to create and destroy the UI pane for the plugin
3189 craig 142
                // A plugin MUST reimplment destroyPrefsPanelWidget if it
2975 craig 143
                // reimplements newPrefsPanelWidget .
3189 craig 144
                // By default, returns 0 to indicate no prefs UI.
2975 craig 145
                virtual QWidget* newPrefsPanelWidget();
3189 craig 146
                virtual void destroyPrefsPanelWidget(QWidget* prefsPanelWidget);
2972 craig 147
 
3189 craig 148
                // Return icon and caption to use for preferences panel in the prefs
149
                // dialog. Unless overridden, returns QString::null and an empty pixmap.
150
                virtual const QPixmap prefsPanelIcon() const;
151
                virtual const QString prefsPanelName() const;
152
 
2972 craig 153
                // Returns a structure containing descriptive information about the
154
                // plug-in. This information is used in places like the Help->About
155
                // Plug-ins menu item. The stucture MUST be deleted using
156
                // deleteAboutData(AboutData* about) when finished with.
157
                //
2975 craig 158
                // Every plug-in must re-implment these.
3189 craig 159
                virtual const AboutData* getAboutData() const = 0;
160
                virtual void deleteAboutData(const AboutData* about) const = 0;
2972 craig 161
 
2975 craig 162
                // Return human readable, translated text of last error to be
163
                // encountered. DO NOT ATTEMPT TO USE THIS VALUE TO MAKE
164
                // DECISIONS IN PROGRAM CODE.
165
                const QString & lastError() const;
166
 
3193 craig 167
                // Updates the text on all widgets on the plug-in to reflect the new
168
                // language. You should generally use this method to set all the widget
169
                // text during construction.
170
                virtual void languageChange() = 0;
171
 
172
                /// Returns human readable plugin type from plug-in's pluginType
173
                const QString pluginTypeName() const;
174
 
2972 craig 175
        protected:
2975 craig 176
                // Human readable, translated version of last error to occur.
177
                QString m_lastError;
2972 craig 178
};
179
 
180
 
3207 craig 181
class SCRIBUS_API ScActionPlugin : public ScPlugin
2972 craig 182
{
183
        Q_OBJECT
184
 
185
        /*
186
         * @brief A plug-in that's loaded for data import/export duties
187
         *
3207 craig 188
         * ScActionPlugin describes a plug-in that is loaded on demand
2972 craig 189
         * to perform a data import/export task such as importing an SVG
190
         * image or exporting a page to EPS format. It'll generally by unloaded
191
         * after being queried, then loaded on demand when it needs to run.
192
         */
193
 
194
        public:
195
 
2975 craig 196
                /** @brief ctor
197
                 *
198
                 * @sa ScPlugin::ScPlugin()
199
                 */
3207 craig 200
                ScActionPlugin(PluginType pluginType);
201
 
2975 craig 202
                // Pure virtual dtor - abstract class
3207 craig 203
                virtual ~ScActionPlugin() = 0;
2975 craig 204
 
3193 craig 205
                // Information about actions, to be returned by actionInfo()
206
                struct ActionInfo {
207
                        QString name;  // name of action
208
                        QString text;  // text to display to user
209
                        QString keySequence;
210
                        QString menu;
211
                        QString menuAfterName;
212
                        QIconSet iconSet;
213
                        bool enabledOnStartup;
214
                };
215
 
216
                // Return an ActionInfo instance to the caller
3207 craig 217
                const ActionInfo & actionInfo() const;
3193 craig 218
 
219
        public slots:
2972 craig 220
                /**
221
                 * @brief Run the plug-in's main action.
222
                 *
223
                 * Run the plug-in's default action. That usually means prompting the
2975 craig 224
                 * user for a target (file to save as / file to import) then
225
                 * performing the import/export action the plug-in is written for.
2972 craig 226
                 *
2975 craig 227
                 * It should generally call run(..., QIODevice* target) or run(...,
228
                 * QString target) to do the work. This is done by default, with the
229
                 * assumption that the target is a file. An open QFile with
230
                 * IO_ReadOnly or IO_WriteOnly mode, depending on plugin type,
231
                 * is supplied. Override this if you need more flexibility.
2972 craig 232
                 *
233
                 * The optional target argument is a plugin-defined string indicating
234
                 * what the plugin should operate on. It will typically be a filename
235
                 * to import from / export to. Nothing stops a plugin doing funky
236
                 * things like accepting a WebDAV URI, etc, however.
237
                 *
238
                 * Plug-in authors might want to consider implementing this using the
239
                 * DeferredTask subclass used in runAsync, if the plug-in supports it.
240
                 *
3193 craig 241
                 * @param QString target Optional target to operate on (see
242
                 *                above)
243
                 * @returns bool True for success.
2972 craig 244
                 *
245
                 */
3193 craig 246
                virtual bool run(QString target = QString::null);
2972 craig 247
 
248
                /**
249
                 * @brief Run the plugin on a QIODevice
250
                 *
251
                 * This method is essentially the same as the above, except that it
252
                 * accepts a QIODevice* to work with. This will generally be used for
253
                 * file I/O without the plugin having to know or care where from.
254
                 *
255
                 * A plug-in MUST return false if it cannot support this method or upon
256
                 * failure. A plug-in MUST supply a working version of this method if
257
                 * it accepts runWith(..., QString) on a local file, and should
258
                 * generally implement the former using this method.
259
                 *
260
                 * Remember that not all QIODevice*s are seekable, and try to write
261
                 * your code to handle a non-seekable stream.
262
                 */
3193 craig 263
                virtual bool run(QIODevice* target);
2972 craig 264
 
265
                /**
266
                 * @brief Run the plugin asynchronously
267
                 *
268
                 * This call is similar to the one above, but instead of doing all the
269
                 * work in one big batch, it creates an instance of a DeferredTask
270
                 * subclass to keep track of the job, does some setup, and returns
271
                 * immediately. The DeferredTask, a pointer to which is returned, has
272
                 * already been started when this method returns.
273
                 *
274
                 * The caller of this method is responsible for handling the aborted()
275
                 * and finished() signals from the DeferredTask, and (if necessary) for
276
                 * cleaning it up once it has finished. Remember that a DeferredTask is
277
                 * automatically deleted when its parent is, if it hasn't already been
278
                 * deleted.
279
                 *
280
                 * FIXME: The above is probably utterly broken on win32.
281
                 *
282
                 * See the DeferredTask documentation for details on how it works.
283
                 *
284
                 * If your plugin implementation does not support asynchronous
285
                 * operation, you must return null. This is the default implementation,
286
                 * so simply ignore this method unless you wish to support it. It is
287
                 * recommended that all new plugins support this interface where
288
                 * possible, even if they can only do part of their work progressively.
289
                 *
290
                 * If you implement this, you may want to implement run(...) using the
291
                 * same deferred operation and a modal dialog. That'll permit the event
292
                 * loop to continue running for redraws, permit you to update the
293
                 * progress bar, etc.
294
                 *
295
                 * NOTE: In current Scribus code (August 2005), callers of this method
296
                 * will generally create a modal dialog - possibly with progress meter
297
                 * - then attach signals from the DeferredTask, and exec() the dialog.
298
                 *   A caller is not required to do this, but with the current (August
299
                 *   2005) core code it's the only way to operate safely. This is
300
                 *   likely to change in future and you should avoid relying on it if
301
                 *   at all posssible.
302
                 *
303
                 * If this method is used, the plugin must not be unloaded until all
304
                 * DeferredTask instances have been deleted.
305
                 */
3193 craig 306
                virtual DeferredTask* runAsync(QString target = QString::null);
2972 craig 307
 
308
                /**
309
                 * @brief Run the plugin asynchronously
310
                 *
311
                 * This method is much the same as the above, but takes a QIODevice* as
312
                 * the target. Please see the documentation for run(..., QIODevice*
313
                 * target) and runAsync(..., QString target) for details.
314
                 */
3193 craig 315
                virtual DeferredTask* runAsync(QIODevice* target);
2972 craig 316
 
3193 craig 317
                // Compat kludge ... we store a QString result from any successful
318
                // run(...) call in m_runResult and let callers retrive it here.
319
                // DO NOT USE THIS INTERFACE FOR NEW PLUG-INS; you should
320
                // dynamic_cast<> to the plugin type then call a plug-in specific
321
                // method instead.
322
                const QString & runResult() const;
2972 craig 323
 
3189 craig 324
        protected:
3207 craig 325
                // Action info. To be set up by ctor.
3189 craig 326
                ActionInfo m_actionInfo;
3193 craig 327
                QString m_runResult;
2972 craig 328
};
329
 
330
class SCRIBUS_API ScPersistentPlugin : public ScPlugin
331
{
332
        Q_OBJECT
333
 
334
        /**
335
         * @brief A plug-in that is resident for the lifetime of the app
336
         *
337
         * ScPersistentPlugin describes a plugin that is to be kept resident
338
         * for the lifetime of the app (or until unloaded by a request to
339
         * the plug-in manager).
340
         *
341
         * Such plug-ins have an init method and a cleanup method; they have
342
         * no "run" method as such.
343
         */
344
        public:
345
 
2975 craig 346
                /** @brief ctor
347
                 *
348
                 * @sa ScPlugin::ScPlugin()
349
                 */
3207 craig 350
                ScPersistentPlugin();
2975 craig 351
 
352
                // Pure virtual dtor for abstract class
353
                virtual ~ScPersistentPlugin() = 0;
354
 
2972 craig 355
                /**
356
                 * @brief Initialize the plugin
357
                 *
358
                 * This method must initialize the plugin. It is called at plug-in load
359
                 * time. This method will never be called twice without an intervening
360
                 * cleanupPlug call (but not necessarily unloading the plugin).
361
                 *
362
                 * It will usually instantiate a class and store a pointer to the
363
                 * instance in a static variable of the plugin or a member of its
364
                 * ScPersistentPlugin subclass, letting that class do the work of setup
365
                 * and integration into the app.
366
                 *
2975 craig 367
                 * Note that this is NOT the same as the init function of the plugin. That
368
                 * is always simple and lightweight, intended to just get an instance of this
369
                 * class back to the core app. This function is where any time-consuming or
370
                 * memory expensive init should be done.
371
                 *
3193 craig 372
                 * If this method returns failure, the plugin will be treated as dead and
373
                 * cleanupPlug() will not be called.
374
                 *
2975 craig 375
                 * @returns bool True for success.
2972 craig 376
                 */
3193 craig 377
                virtual bool initPlugin() = 0;
2972 craig 378
 
379
                /**
380
                 * @brief Cleans up the plugin so it can be safely unloaded.
381
                 *
382
                 * This method will be called when the plug-in is about to be unloaded,
383
                 * or if the plug-in manager has been asked to disable the plug-in.
384
                 * This method will never be called unless initPlug has been called
385
                 * first, but there is no guarantee the plugin will be unloaded after
386
                 * this is called, or before initPlug is called again.
387
                 *
388
                 * @returns bool True for success.
389
                 */
3207 craig 390
                virtual bool cleanupPlugin() = 0;
2972 craig 391
};
392
 
393
#endif