Subversion Repositories Scribus

Rev

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

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