Subversion Repositories Scribus

Rev

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