home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / include / scribus-ng / scplugin.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-12-09  |  19.7 KB  |  502 lines

  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. */
  7. #ifndef _SCPLUGIN_H
  8. #define _SCPLUGIN_H
  9.  
  10. #include <QObject>
  11. #include <QWidget>
  12. #include <QString>
  13. #include <QPixmap>
  14. #include <QDateTime>
  15. #include <QIcon>
  16. #include "scribusapi.h"
  17.  
  18. class ScribusDoc;
  19. class ScribusMainWindow;
  20. class DeferredTask;
  21. class PrefsPanel;
  22.  
  23. /**
  24.  * \brief Abstract super class for all Scribus plug-ins
  25.  *
  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
  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.
  31.  *
  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.
  39.  *
  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.
  46.  *
  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.  *
  55.  * More plugin classes - such as plugins for text import and document import
  56.  * - may be added in future.
  57.  *
  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.  *
  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
  69.  * of the plug-in (where the filename is libpluginname.so on UNIX):
  70.  *
  71.  * int pluginname_getPluginAPIVersion();
  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.  *
  85.  * TODO: better discovery of what plug-ins can import / export what formats, file exts, names, etc.
  86.  *
  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.  
  100.         /*! \brief A struct providing the information returned by getAboutData(), for
  101.         the "About Plug-ins" menu item. */
  102.         struct AboutData
  103.         {
  104.             //! Author name(s) and email address(es)
  105.             QString authors;
  106.             //! One-liner description of the function of the plugin
  107.             QString shortDescription;
  108.             //! Longer description of the plugin
  109.             QString description;
  110.             //! Text plug-in version string
  111.             QString version;
  112.             //! Date of the release of this version
  113.             QDateTime releaseDate;
  114.             //! Copyright string
  115.             QString copyright;
  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.
  120.             QString license;
  121.         };
  122.  
  123.         /**
  124.          * @brief ctor, returns a new ScPlugin instance
  125.          *
  126.          * Only pluginname_getPlugin(...) may call this. Calls by other code
  127.          * will have undefined results.
  128.          */
  129.         ScPlugin();
  130.  
  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.          */
  138.         virtual ~ScPlugin() = 0;
  139.  
  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.          */
  146.         virtual const QString fullTrName() const = 0;
  147.  
  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
  155.          * deleting it when it's parent is deleted. `parent' must be the dialog
  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);
  175.  
  176.         /*! @brief Return descriptive information about the plug-in
  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.          */
  187.         virtual const AboutData* getAboutData() const = 0;
  188.         virtual void deleteAboutData(const AboutData* about) const = 0;
  189.  
  190.         /*! @brief Text summary of last error encountered
  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.          */
  197.         const QString & lastError() const;
  198.  
  199.         //! \brief Returns if lastError message is not empty
  200.         bool hasLastError() const;
  201.  
  202.         //! \brief Clear last error message
  203.         void clearLastError();
  204.  
  205.         /*! @brief Update all user-visible text to reflect current UI language
  206.          *
  207.          * Updates the text on all widgets on the plug-in to reflect the new
  208.          * language. You should generally use this method to set all the widget
  209.          * text during construction. That ensures that text is not duplicated,
  210.          * and that when this method is called the text of all persistent
  211.          * widgets is correctly changed.
  212.          *
  213.          * This method only needs to affect text in widgets that currently
  214.          * exists (displayed or otherwise).
  215.          */
  216.         virtual void languageChange() = 0;
  217.  
  218.         //! \brief Returns human readable plugin type from plug-in's pluginType
  219.         const QString pluginTypeName() const;
  220.  
  221.         //! \brief Allow plugins to add to a main menu
  222.         virtual void addToMainWindowMenu(ScribusMainWindow *) = 0;
  223.  
  224.         //! \brief hooks that plugins can use to detect if the current document was closed etc..
  225.         virtual void setDoc(ScribusDoc* doc);
  226.         virtual void unsetDoc();
  227.         virtual void changedDoc(ScribusDoc* doc);
  228.     protected:
  229.         //! \brief Human readable, translated version of last error to occur.
  230.         QString m_lastError;
  231. };
  232.  
  233.  
  234. /*!
  235.  * @brief A plug-in that performs a single action
  236.  *
  237.  * ScActionPlugin describes a plug-in that is used to perform a single well
  238.  * defined action. Uses include data import/export, displaying a dialog, or
  239.  * doing a single batch task. It is automatically connected to the user
  240.  * interface and has simple methods to trigger the plug-in's supported action.
  241.  *
  242.  * If you need more complex user interface integration, persistent operation,
  243.  * etc, you may be better off with ScPersistentPlugin.
  244.  */
  245. class SCRIBUS_API ScActionPlugin : public ScPlugin
  246. {
  247.     Q_OBJECT
  248.  
  249.     public:
  250.  
  251.         /** @brief ctor
  252.          *
  253.          * @sa ScPlugin::ScPlugin()
  254.          */
  255.         ScActionPlugin();
  256.  
  257.         //! \brief Pure virtual dtor - abstract class
  258.         virtual ~ScActionPlugin() = 0;
  259.  
  260.         // Information about actions, to be returned by actionInfo()
  261.         struct ActionInfo {
  262.             QString name;  // name of action
  263.             QString text;  // text to display to user
  264.             QString keySequence;
  265.             QString menu;
  266.             QString menuAfterName;
  267.             QString parentMenu;
  268.             QString subMenuName;
  269.             QString toolbar;            // Name of the ToolBar the action is to be inserted, if that toolbar doesn't exits it will be created
  270.             QString toolBarName;        // translateable ToolBar title
  271.             QPixmap icon1;
  272.             QPixmap icon2;
  273.             QList<int> notSuitableFor;        // a list of PageItem type values which the plugin can *not* handle
  274.             QList<int> forAppMode;            // a list of AppMode values for which the plugin will be active, an empty list indicates that the plugin is always active
  275.             int needsNumObjects;            // plugin needs this number of selected Objects. -1 = needs no Object, num > 2 any number of Objects is allowed
  276.             QList<int> firstObjectType;        // when needsNumObjects is 2 this list contains the Object Types of the first object on the selection
  277.             QList<int> secondObjectType;    // when needsNumObjects is 2 this list contains the Object Types of the second object on the selection
  278.                                             // -1 indicates that any kind of object is possible. Otherwise the selection must contain the 2 Object Types
  279.                                             // for the Plugin Action to be enabled
  280.             bool enabledOnStartup;
  281.         };
  282.  
  283.         // Return an ActionInfo instance to the caller
  284.         const ActionInfo & actionInfo() const;
  285.         /**
  286.         * @brief handle the documents selection.
  287.         *
  288.         * This function is dedicated for handling the documents selection
  289.         * By default this uses the notSuitableFor, forAppMode, needsNumObjects,
  290.         * firstObjectType and secondObjectType variables.
  291.         * If you need more control over the selection reimplement this function.
  292.         *
  293.         * @returns bool true when the Action should be enabled.
  294.         *
  295.         */
  296.         virtual bool handleSelection(ScribusDoc* doc, int SelectedType = -1);
  297.  
  298.     public slots:
  299.         /**
  300.          * @brief Run the plug-in's main action.
  301.          *
  302.          * Run the plug-in's default action synchronously, blocking the rest of
  303.          * the application until this method returns.
  304.          *
  305.          * This method should generally call run(..., QIODevice* target) or
  306.          * run(..., QString target) to do the work. This is done by default,
  307.          * with the assumption that the target is a file. The argument is
  308.          * opened as QFile with IO_ReadOnly or IO_WriteOnly mode, depending on
  309.          * plugin type, and passed to run(..., QIODevice*). Override this if
  310.          * you need more flexibility.
  311.          *
  312.          * The optional target argument is a plugin-defined string indicating
  313.          * what the plugin should operate on. It will typically be a filename
  314.          * to import from / export to. Nothing stops a plugin doing funky
  315.          * things like accepting a WebDAV URI, etc, however.
  316.          *
  317.          * Plug-in authors might want to consider implementing this using the
  318.          * DeferredTask subclass used in runAsync, if the plug-in supports it.
  319.          *
  320.          * @param target Optional target to operate on (see
  321.          *                above)
  322.          * @returns bool True for success.
  323.          *
  324.          */
  325.         virtual bool run(ScribusDoc* doc, QString target = QString::null) = 0;
  326.  
  327.         /**
  328.          * @brief Run the plug-in's main action.
  329.          *
  330.          * This method behaves as the previous one. Except there is
  331.          * a parent widget reference. It's useful e.g. when you need to
  332.          * open a dialog on a specific parent one.
  333.          */
  334.         virtual bool run(QWidget* parent, ScribusDoc* doc, QString target = QString::null);
  335.  
  336.         /**
  337.          * @brief Run the plugin on a QIODevice
  338.          *
  339.          * This method is essentially the same as the above, except that it
  340.          * accepts a QIODevice* to work with. This will generally be used for
  341.          * file I/O without the plugin having to know or care where from.
  342.          *
  343.          * A plug-in MUST return false if it cannot support this method or upon
  344.          * failure. A plug-in MUST supply a working version of this method if
  345.          * it accepts runWith(..., QString) on a local file, and should
  346.          * generally implement the former using this method.
  347.          *
  348.          * Remember that not all QIODevice*s are seekable, and try to write
  349.          * your code to handle a non-seekable stream.
  350.          */
  351.         virtual bool run(ScribusDoc* doc, QIODevice* target);
  352.  
  353.         /**
  354.          * @brief Run the plugin asynchronously
  355.          *
  356.          * This call is similar to the one above, but instead of doing all the
  357.          * work in one big batch, it creates an instance of a DeferredTask
  358.          * subclass to keep track of the job, does some setup, and returns
  359.          * immediately. The DeferredTask, a pointer to which is returned, has
  360.          * already been started when this method returns.
  361.          *
  362.          * The caller of this method is responsible for handling the aborted()
  363.          * and finished() signals from the DeferredTask, and (if necessary) for
  364.          * cleaning it up using deleteLater(...) once it has finished. Remember
  365.          * that a DeferredTask is automatically deleted when its parent is, if
  366.          * it hasn't already been deleted. This approach is preferred where
  367.          * possible.
  368.          *
  369.          * See the DeferredTask documentation for details on how it works.
  370.          *
  371.          * If your plugin implementation does not support asynchronous
  372.          * operation, you must return null. This is the default implementation,
  373.          * so simply ignore this method unless you wish to support it. It is
  374.          * recommended that all new plugins support this interface where
  375.          * possible, even if they can only do part of their work progressively.
  376.          *
  377.          * If you implement this, you may want to implement run(...) using the
  378.          * same deferred operation and a modal dialog. That'll permit the event
  379.          * loop to continue running for redraws, permit you to update the
  380.          * progress bar, etc.
  381.          *
  382.          * NOTE: In current Scribus code (August 2005), callers of this method
  383.          * will generally create a modal dialog - possibly with progress meter
  384.          * - then attach signals from the DeferredTask, and exec() the dialog.
  385.          *   A caller is not required to do this, but with the current (August
  386.          *   2005) core code it's the only way to operate safely. This is
  387.          *   likely to change in future and you should avoid relying on it if
  388.          *   at all posssible.
  389.          *
  390.          * If this method is used, the plugin must not be unloaded until all
  391.          * DeferredTask instances have been deleted.
  392.          */
  393.         virtual DeferredTask* runAsync(QString target = QString::null);
  394.  
  395.         /**
  396.          * @brief Run the plugin asynchronously
  397.          *
  398.          * This method is much the same as the above, but takes a QIODevice* as
  399.          * the target. Please see the documentation for run(..., QIODevice*
  400.          * target) and runAsync(..., QString target) for details.
  401.          */
  402.         virtual DeferredTask* runAsync(QIODevice* target);
  403.  
  404.         // Compat kludge ... we store a QString result from any successful
  405.         // run(...) call in m_runResult and let callers retrive it here.
  406.         // DO NOT USE THIS INTERFACE FOR NEW PLUG-INS; you should
  407.         // dynamic_cast<> to the plugin type then call a plug-in specific
  408.         // method instead.
  409.         ///! Obsolete method - do not use this or rely on it in new code.
  410.         const QString & runResult() const;
  411.  
  412.     protected:
  413.         // Action info. To be set up by ctor.
  414.         ActionInfo m_actionInfo;
  415.         // Obsolete - see runResult()
  416.         QString m_runResult;
  417.         ScribusDoc* m_Doc;
  418. };
  419.  
  420. /**
  421.  * @brief A plug-in that is resident for the lifetime of the app
  422.  *
  423.  * ScPersistentPlugin describes a plugin that is to be kept resident for the
  424.  * lifetime of the app (or until unloaded by a request to the plug-in manager).
  425.  * Persistent plugins are useful where the plugin author needs to retain state,
  426.  * needs more control of how they integrate with the Scribus user interface,
  427.  * and/or wants to perform tasks in the background.
  428.  *
  429.  * Such plug-ins have an init method and a cleanup method; they have
  430.  * no "run" method as such. It is up to the plugin to determine how
  431.  * best to interfact with the user.
  432.  */
  433. class SCRIBUS_API ScPersistentPlugin : public ScPlugin
  434. {
  435.     Q_OBJECT
  436.  
  437.     public:
  438.  
  439.         /** @brief ctor
  440.          *
  441.          * @sa ScPlugin::ScPlugin()
  442.          */
  443.         ScPersistentPlugin();
  444.  
  445.         //! \brief Pure virtual dtor for abstract class
  446.         virtual ~ScPersistentPlugin() = 0;
  447.  
  448.         /**
  449.          * @brief Initialize the plugin
  450.          *
  451.          * This method must initialize the plugin. It is called at plug-in load
  452.          * time. This method will never be called twice without an intervening
  453.          * cleanupPlugin call, but there is no guarantee the plugin will actually
  454.          * be unloaded between initPlugin() and cleanupPlugin().
  455.          *
  456.          * It will usually instantiate a class and store a pointer to the
  457.          * instance in a static variable of the plugin or a member of its
  458.          * ScPersistentPlugin subclass, letting that class do the work of setup
  459.          * and integration into the app.
  460.          *
  461.          * Note that this is NOT the same as the init function of the plugin. That
  462.          * is always simple and lightweight, intended to just get an instance of this
  463.          * class back to the core app. This function is where any time-consuming or
  464.          * memory expensive init should be triggered, generally by the instantiation
  465.          * of the class that does the real work for the plugin.
  466.          *
  467.          * If this method returns failure, the plugin will be treated as dead and
  468.          * cleanupPlug() will not be called.
  469.          *
  470.          * @returns bool True for success.
  471.          */
  472.         virtual bool initPlugin() = 0;
  473.  
  474.         /**
  475.          * @brief Deactivates the plugin for unloading / program quit
  476.          *
  477.          * This method will be called when the plug-in is about to be unloaded,
  478.          * or if the plug-in manager has been asked to disable the plug-in.
  479.          * This method will never be called unless initPlugin has been called
  480.          * first, but there is no guarantee the plugin will actually be
  481.          * unloaded after this is called, or before initPlugin is called again.
  482.          *
  483.          * @returns bool True for success.
  484.          */
  485.         virtual bool cleanupPlugin() = 0;
  486. };
  487.  
  488. // Plug-in API version used to check if we can load the plug-in. This
  489. // does *NOT* ensure that the plug-in will be compatible with the internal
  490. // Scribus APIs, only that the ScPlugin class and its standard subclasses
  491. // will be compatible with what we expect, and that "extern C" functions
  492. // we need will be present and work as expected. It's a preprocessor directive
  493. // to make sure that it's compiled into each plugin rather than referenced
  494. // from the main code.
  495. //
  496. // The API version is currently simply incremented with each incompatible
  497. // change. Future versions may introduce a minor/major scheme if necessary.
  498. #define PLUGIN_API_VERSION 0x00000009
  499.  
  500.  
  501. #endif
  502.