Subversion Repositories Scribus

Rev

Rev 3994 | Rev 4430 | 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"
3214 craig 5
#include "qwidget.h"
2972 craig 6
#include "qstring.h"
7
#include "qpixmap.h"
8
#include "qdatetime.h"
3193 craig 9
#include "qiconset.h"
2972 craig 10
#include "scribusapi.h"
11
 
12
class QWidget;
4026 craig 13
class ScribusMainWindow;
2972 craig 14
class DeferredTask;
3213 craig 15
class PrefsPanel;
2972 craig 16
 
3214 craig 17
/**
18
 * \brief Abstract super class for all Scribus plug-ins
2972 craig 19
 *
3214 craig 20
 * ScPlugin is a class that provides information about a plug-in, and a gateway
21
 * into any plugin-specific functionality.  It should not generally be
3486 craig 22
 * inherited directly by actual plugin implementations - use one of the
23
 * provided base classes instead. See below for info on the creation of
24
 * new types of plugin.
2972 craig 25
 *
3486 craig 26
 * Plugins create and return a instance of a subclass of ScPlugin when asked
27
 * for it by the plugin manager. The plugin manager is responsible for tracking
28
 * these instances. The ScPlugin instance is used by the plugin manager for
29
 * tasks such as hooking the plugin up to the UI where appropriate. It's also
30
 * used by other code that talks to plugins, usually by dynamic_cast<>ing it to
31
 * a more specific subclass and working with that. The file loader system will
32
 * provide an example of this.
3214 craig 33
 *
3486 craig 34
 * A given plugin implementation's ScPlugin subclass is *not* the best place
35
 * to implement the bulk of the plugin's functionality. It must be lightweight
36
 * and maintain as little state as possible. Most functionality should be
37
 * implemented in a private subclass that is not exposed in the header of the
38
 * scplugin subclass defined by the plugin. That helps keep the plugin's
39
 * implementation separate from its inteface to Scribus.
3214 craig 40
 *
3486 craig 41
 * Note that the two subclases ScActionPlugin and ScPersistentPlugin, defined
42
 * below, create two quite different kinds of plugins. One is for import/export
43
 * of data and for single function actions (like providing a particular dialog
44
 * or function) which generally do nothing when not in active use. A second for
45
 * persistent plugins that are loaded (generally when the app starts) and
46
 * remain resident, possibly active in the background or hooked into many
47
 * parts of the user interface.
48
 *
2972 craig 49
 * More plugin classes - such as plugins for text import and document import
50
 * - may be added in future.
2975 craig 51
 *
3486 craig 52
 * New types of plugin can be created by subclassing ScPlugin or one of its
53
 * subclasses and adding the additional interfaces required. The two existing
54
 * subclasses provide examples of how this can work. Of course, once you have
55
 * the plugin interface, you still need to actually use the plugins. You'll need
56
 * to extend pluginmanager.cpp a little to handle any low-level init required by
57
 * the new plugin type (please keep this minimal!). You'll then need to extend
58
 * the appropriate classes to make use of the new plugin type where required.
59
 * The new file loader mechanism will provide a good example of this.
60
 *
2975 craig 61
 * In addition to the API described here, a plugin is expected to implement
62
 * the following "extern C" functions, where 'pluginname' is the base name
3214 craig 63
 * of the plug-in (where the filename is libpluginname.so on UNIX):
2975 craig 64
 *
3207 craig 65
 * int pluginname_getPluginAPIVersion();
2975 craig 66
 *    Return an integer indicating the plug-in API version implemented.
67
 *    If the version does not exactly match the running plugin API version,
68
 *    the plugin will not be initialised.
69
 *
70
 * ScPlugin* pluginname_getPlugin();
71
 *    Create and return an instance of the plug-ins ScPlugin subclass.
72
 *    Nothing should be done here that's overly slow or uses much memory,
73
 *    and the plugin should not initialize its self here beyond what is
74
 *    required to return a ScPlugin instance.
75
 *
76
 * void pluginname_freePlugin(ScPlugin* plugin);
77
 *    Free the scplugin instance returned earlier.
78
 *
3193 craig 79
 * TODO: better discovery of what plug-ins can import / export what formats, file exts, names, etc.
80
 *
2972 craig 81
 */
82
 
83
class SCRIBUS_API ScPlugin : public QObject
84
{
85
        Q_OBJECT
86
 
87
        /**
88
         * @brief ScPlugin provides an interface to ask plugins for information
89
         * about themselves.
90
         *
91
         */
92
        public:
93
 
94
                // A struct providing the information returned by getAboutData(), for
95
                // the "About Plug-ins" menu item.
96
                struct AboutData
97
                {
98
                        // Author name(s) and email address(es)
99
                        QString authors;
100
                        // One-liner description of the function of the plugin
101
                        QString shortDescription;
102
                        // Longer description of the plugin
103
                        QString description;
104
                        // Text plug-in version string
105
                        QString version;
106
                        // Date of the release of this version
107
                        QDateTime releaseDate;
108
                        // Copyright string
109
                        QString copyright;
110
                        // License text / name / URL / whatever. Please don't include
111
                        // the GPL here, just reference it. Remember that Scribus is
112
                        // licensed under the GPL, so plug-in authors must take that
113
                        // into account.
114
                        QString license;
115
                };
116
 
2975 craig 117
                /**
118
                 * @brief ctor, returns a new ScPlugin instance
119
                 *
3486 craig 120
                 * Only pluginname_getPlugin(...) may call this. Calls by other code
121
                 * will have undefined results.
2975 craig 122
                 */
3487 craig 123
                ScPlugin();
2975 craig 124
 
3486 craig 125
                /** @brief Destroy the ScPlugin instance
126
                 *
127
                 * Pure virtual destructor - this is an abstract class.
128
                 *
129
                 * Only pluginname_freePlugin(...) may call this. Calls by other code
130
                 * will have undefined results.
131
                 */
2975 craig 132
                virtual ~ScPlugin() = 0;
133
 
3486 craig 134
                /** @brief Plug-in's human-readable, translated name.
135
                 *
136
                 * Please don't use this for anything except display to the user.
137
                 * The results of testing the value of this can not be guaranteed,
138
                 * as its value may change depending on locale and change at runtime.
139
                 */
3207 craig 140
                virtual const QString fullTrName() const = 0;
2972 craig 141
 
3214 craig 142
                /**
143
                 * \brief Create and return a prefs UI panel for the plugin.
144
                 *
145
                 * Override this method if you want to provide a panel for the
146
                 * preferences dialog.
147
                 *
148
                 * The caller takes ownership of the panel. Qt is responsible for
3486 craig 149
                 * deleting it when it's parent is deleted. `parent' must be the dialog
3214 craig 150
                 * the widget will be added to or a child of it, otherwise the panel
151
                 * won't be deleted correctly when the dialog is.
152
                 *
153
                 * See prefspanel.h for info on implementing the panel.
154
                 *
155
                 * This method must return false (the default) if the plugin does
156
                 * not provide a prefs panel. If true is returned, caption, panel,
157
                 * and icon MUST have been assigned.
158
                 *
159
                 * \param parent Parent widget, to be passed to panel ctor
160
                 * \param panel (out) the panel created
161
                 * \param caption (out) caption for icon in panel list
162
                 * \param icon (out) icon for use in panel list
163
                 * \return true if panel was created, false if not.
164
                 *
165
                 * By default, returns 0 to indicate no prefs UI.
166
                */
167
                virtual bool newPrefsPanelWidget(QWidget* parent, PrefsPanel*& panel,
168
                                                                                 QString& caption, QPixmap& icon);
2972 craig 169
 
3994 subik 170
                /*! @brief Return descriptive information about the plug-in
3486 craig 171
                 *
172
                 * Returns a structure containing descriptive information about the
173
                 * plug-in. This information is used in places like the Help->About
174
                 * Plug-ins menu item. The stucture MUST be deleted using
175
                 * deleteAboutData(AboutData* about) when finished with.
176
                 *
177
                 * Every plugin MUST reimplement getAboutData(...) and deleteAboutData(...).
178
                 *
179
                 * @sa ScPlugin::deleteAboutData
180
                 */
3189 craig 181
                virtual const AboutData* getAboutData() const = 0;
182
                virtual void deleteAboutData(const AboutData* about) const = 0;
2972 craig 183
 
3994 subik 184
                /*! @brief Text summary of last error encountered
3486 craig 185
                 *
186
                 * Return the human readable, translated text of last error to be
187
                 * encountered by this plugin. DO NOT ATTEMPT TO USE THIS VALUE TO MAKE
188
                 * DECISIONS IN PROGRAM CODE. The value returned for a given error
189
                 * may change depending on locale, and may change at runtime.
190
                 */
2975 craig 191
                const QString & lastError() const;
192
 
3994 subik 193
                /*! @brief Update all user-visible text to reflect current UI language
3486 craig 194
                 *
195
                 * Updates the text on all widgets on the plug-in to reflect the new
196
                 * language. You should generally use this method to set all the widget
197
                 * text during construction. That ensures that text is not duplicated,
198
                 * and that when this method is called the text of all persistent
199
                 * widgets is correctly changed.
200
                 *
201
                 * This method only needs to affect text in widgets that currently
202
                 * exists (displayed or otherwise).
203
                 */
3193 craig 204
                virtual void languageChange() = 0;
205
 
3994 subik 206
                //! \brief Returns human readable plugin type from plug-in's pluginType
3193 craig 207
                const QString pluginTypeName() const;
208
 
2972 craig 209
        protected:
3994 subik 210
                //! \brief Human readable, translated version of last error to occur.
2975 craig 211
                QString m_lastError;
2972 craig 212
};
213
 
214
 
3994 subik 215
/*!
3486 craig 216
 * @brief A plug-in that performs a single action
217
 *
218
 * ScActionPlugin describes a plug-in that is used to perform a single well
219
 * defined action. Uses include data import/export, displaying a dialog, or
220
 * doing a single batch task. It is automatically connected to the user
221
 * interface and has simple methods to trigger the plug-in's supported action.
222
 *
223
 * If you need more complex user interface integration, persistent operation,
224
 * etc, you may be better off with ScPersistentPlugin.
225
 */
3207 craig 226
class SCRIBUS_API ScActionPlugin : public ScPlugin
2972 craig 227
{
228
        Q_OBJECT
229
 
230
        public:
231
 
2975 craig 232
                /** @brief ctor
233
                 *
234
                 * @sa ScPlugin::ScPlugin()
235
                 */
3487 craig 236
                ScActionPlugin();
3207 craig 237
 
3994 subik 238
                //! \brief Pure virtual dtor - abstract class
3207 craig 239
                virtual ~ScActionPlugin() = 0;
2975 craig 240
 
3193 craig 241
                // Information about actions, to be returned by actionInfo()
242
                struct ActionInfo {
243
                        QString name;  // name of action
244
                        QString text;  // text to display to user
245
                        QString keySequence;
246
                        QString menu;
247
                        QString menuAfterName;
248
                        QIconSet iconSet;
249
                        bool enabledOnStartup;
250
                };
251
 
252
                // Return an ActionInfo instance to the caller
3207 craig 253
                const ActionInfo & actionInfo() const;
3193 craig 254
 
255
        public slots:
2972 craig 256
                /**
257
                 * @brief Run the plug-in's main action.
258
                 *
3486 craig 259
                 * Run the plug-in's default action synchronously, blocking the rest of
260
                 * the application until this method returns.
2972 craig 261
                 *
3486 craig 262
                 * This method should generally call run(..., QIODevice* target) or
263
                 * run(..., QString target) to do the work. This is done by default,
264
                 * with the assumption that the target is a file. The argument is
265
                 * opened as QFile with IO_ReadOnly or IO_WriteOnly mode, depending on
266
                 * plugin type, and passed to run(..., QIODevice*). Override this if
267
                 * you need more flexibility.
2972 craig 268
                 *
269
                 * The optional target argument is a plugin-defined string indicating
270
                 * what the plugin should operate on. It will typically be a filename
271
                 * to import from / export to. Nothing stops a plugin doing funky
272
                 * things like accepting a WebDAV URI, etc, however.
273
                 *
274
                 * Plug-in authors might want to consider implementing this using the
275
                 * DeferredTask subclass used in runAsync, if the plug-in supports it.
276
                 *
3193 craig 277
                 * @param QString target Optional target to operate on (see
278
                 *                above)
279
                 * @returns bool True for success.
2972 craig 280
                 *
281
                 */
3487 craig 282
                virtual bool run(QString target = QString::null) = 0;
2972 craig 283
 
284
                /**
285
                 * @brief Run the plugin on a QIODevice
286
                 *
287
                 * This method is essentially the same as the above, except that it
288
                 * accepts a QIODevice* to work with. This will generally be used for
289
                 * file I/O without the plugin having to know or care where from.
290
                 *
291
                 * A plug-in MUST return false if it cannot support this method or upon
292
                 * failure. A plug-in MUST supply a working version of this method if
293
                 * it accepts runWith(..., QString) on a local file, and should
294
                 * generally implement the former using this method.
295
                 *
296
                 * Remember that not all QIODevice*s are seekable, and try to write
297
                 * your code to handle a non-seekable stream.
298
                 */
3193 craig 299
                virtual bool run(QIODevice* target);
2972 craig 300
 
301
                /**
302
                 * @brief Run the plugin asynchronously
303
                 *
304
                 * This call is similar to the one above, but instead of doing all the
305
                 * work in one big batch, it creates an instance of a DeferredTask
306
                 * subclass to keep track of the job, does some setup, and returns
307
                 * immediately. The DeferredTask, a pointer to which is returned, has
308
                 * already been started when this method returns.
309
                 *
310
                 * The caller of this method is responsible for handling the aborted()
311
                 * and finished() signals from the DeferredTask, and (if necessary) for
3486 craig 312
                 * cleaning it up using deleteLater(...) once it has finished. Remember
313
                 * that a DeferredTask is automatically deleted when its parent is, if
314
                 * it hasn't already been deleted. This approach is preferred where
315
                 * possible.
2972 craig 316
                 *
317
                 * See the DeferredTask documentation for details on how it works.
318
                 *
319
                 * If your plugin implementation does not support asynchronous
320
                 * operation, you must return null. This is the default implementation,
321
                 * so simply ignore this method unless you wish to support it. It is
322
                 * recommended that all new plugins support this interface where
323
                 * possible, even if they can only do part of their work progressively.
324
                 *
325
                 * If you implement this, you may want to implement run(...) using the
326
                 * same deferred operation and a modal dialog. That'll permit the event
327
                 * loop to continue running for redraws, permit you to update the
328
                 * progress bar, etc.
329
                 *
330
                 * NOTE: In current Scribus code (August 2005), callers of this method
331
                 * will generally create a modal dialog - possibly with progress meter
332
                 * - then attach signals from the DeferredTask, and exec() the dialog.
333
                 *   A caller is not required to do this, but with the current (August
334
                 *   2005) core code it's the only way to operate safely. This is
335
                 *   likely to change in future and you should avoid relying on it if
336
                 *   at all posssible.
337
                 *
338
                 * If this method is used, the plugin must not be unloaded until all
339
                 * DeferredTask instances have been deleted.
340
                 */
3193 craig 341
                virtual DeferredTask* runAsync(QString target = QString::null);
2972 craig 342
 
343
                /**
344
                 * @brief Run the plugin asynchronously
345
                 *
346
                 * This method is much the same as the above, but takes a QIODevice* as
347
                 * the target. Please see the documentation for run(..., QIODevice*
348
                 * target) and runAsync(..., QString target) for details.
349
                 */
3193 craig 350
                virtual DeferredTask* runAsync(QIODevice* target);
2972 craig 351
 
3193 craig 352
                // Compat kludge ... we store a QString result from any successful
353
                // run(...) call in m_runResult and let callers retrive it here.
354
                // DO NOT USE THIS INTERFACE FOR NEW PLUG-INS; you should
355
                // dynamic_cast<> to the plugin type then call a plug-in specific
356
                // method instead.
3994 subik 357
                ///! Obsolete method - do not use this or rely on it in new code.
3193 craig 358
                const QString & runResult() const;
2972 craig 359
 
3189 craig 360
        protected:
3207 craig 361
                // Action info. To be set up by ctor.
3189 craig 362
                ActionInfo m_actionInfo;
3486 craig 363
                // Obsolete - see runResult()
3193 craig 364
                QString m_runResult;
2972 craig 365
};
366
 
3486 craig 367
/**
368
 * @brief A plug-in that is resident for the lifetime of the app
369
 *
370
 * ScPersistentPlugin describes a plugin that is to be kept resident for the
371
 * lifetime of the app (or until unloaded by a request to the plug-in manager).
372
 * Persistent plugins are useful where the plugin author needs to retain state,
373
 * needs more control of how they integrate with the Scribus user interface,
374
 * and/or wants to perform tasks in the background.
375
 *
376
 * Such plug-ins have an init method and a cleanup method; they have
377
 * no "run" method as such. It is up to the plugin to determine how
378
 * best to interfact with the user.
379
 */
2972 craig 380
class SCRIBUS_API ScPersistentPlugin : public ScPlugin
381
{
382
        Q_OBJECT
383
 
384
        public:
385
 
2975 craig 386
                /** @brief ctor
387
                 *
388
                 * @sa ScPlugin::ScPlugin()
389
                 */
3207 craig 390
                ScPersistentPlugin();
2975 craig 391
 
3994 subik 392
                //! \brief Pure virtual dtor for abstract class
2975 craig 393
                virtual ~ScPersistentPlugin() = 0;
394
 
2972 craig 395
                /**
396
                 * @brief Initialize the plugin
397
                 *
398
                 * This method must initialize the plugin. It is called at plug-in load
399
                 * time. This method will never be called twice without an intervening
3486 craig 400
                 * cleanupPlugin call, but there is no guarantee the plugin will actually
401
                 * be unloaded between initPlugin() and cleanupPlugin().
2972 craig 402
                 *
403
                 * It will usually instantiate a class and store a pointer to the
404
                 * instance in a static variable of the plugin or a member of its
405
                 * ScPersistentPlugin subclass, letting that class do the work of setup
406
                 * and integration into the app.
407
                 *
2975 craig 408
                 * Note that this is NOT the same as the init function of the plugin. That
409
                 * is always simple and lightweight, intended to just get an instance of this
410
                 * class back to the core app. This function is where any time-consuming or
3486 craig 411
                 * memory expensive init should be triggered, generally by the instantiation
412
                 * of the class that does the real work for the plugin.
2975 craig 413
                 *
3193 craig 414
                 * If this method returns failure, the plugin will be treated as dead and
415
                 * cleanupPlug() will not be called.
416
                 *
2975 craig 417
                 * @returns bool True for success.
2972 craig 418
                 */
3193 craig 419
                virtual bool initPlugin() = 0;
2972 craig 420
 
421
                /**
3486 craig 422
                 * @brief Deactivates the plugin for unloading / program quit
2972 craig 423
                 *
424
                 * This method will be called when the plug-in is about to be unloaded,
425
                 * or if the plug-in manager has been asked to disable the plug-in.
3486 craig 426
                 * This method will never be called unless initPlugin has been called
427
                 * first, but there is no guarantee the plugin will actually be
428
                 * unloaded after this is called, or before initPlugin is called again.
2972 craig 429
                 *
430
                 * @returns bool True for success.
431
                 */
3207 craig 432
                virtual bool cleanupPlugin() = 0;
2972 craig 433
};
434
 
3256 craig 435
// Plug-in API version used to check if we can load the plug-in. This
436
// does *NOT* ensure that the plug-in will be compatible with the internal
437
// Scribus APIs, only that the ScPlugin class and its standard subclasses
438
// will be compatible with what we expect, and that "extern C" functions
439
// we need will be present and work as expected. It's a preprocessor directive
440
// to make sure that it's compiled into each plugin rather than referenced
441
// from the main code.
442
//
443
// The API version is currently simply incremented with each incompatible
444
// change. Future versions may introduce a minor/major scheme if necessary.
3487 craig 445
#define PLUGIN_API_VERSION 0x00000003
3256 craig 446
 
447
 
2972 craig 448
#endif