Back to index

scribus-ng  1.3.4.dfsg+svn20071115
loadsaveplugin.h
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #ifndef SCRIBUS_LOADSAVEPLUGIN_H
00008 #define SCRIBUS_LOADSAVEPLUGIN_H
00009 
00010 #include "scplugin.h"
00011 
00012 #include <qstring.h>
00013 #include <qregexp.h>
00014 #include <qiodevice.h>
00015 #include <qprogressbar.h>
00016 #include <qvaluelist.h>
00017 #include <qstringlist.h>
00018 
00019 class FileFormat;
00020 //TODO REmove includes one day
00021 //class ScribusDoc;
00022 //class ScribusView;
00023 #include "scfonts.h"
00024 #include "scribusdoc.h"
00025 #include "scribusview.h"
00026 
00027 /*
00028  * @brief Superclass for all file import/export/load/save plugins
00029  *
00030  * This class provides the interface common to all file load/save/import/export
00031  * plugins. It provides the facilities for discovering what format(s) a plugin
00032  * supports, how they should be identified, etc.
00033  */
00034 class SCRIBUS_API LoadSavePlugin : public ScPlugin
00035 {
00036        Q_OBJECT
00037 
00038        public:
00039               // Construct a plugin instance.
00040               LoadSavePlugin();
00041               ~LoadSavePlugin();
00042 
00043               enum loadFlags
00044               {
00045                      lfCreateDoc = 1,
00046                      lfUseCurrentPage = 2,
00047                      lfInsertPage = 4,
00048                      lfInteractive = 8,
00049                      lfScripted = 16
00050               };
00051 
00052               // Static functions:
00053 
00054               // Return a list of format descriptions suitable for use with
00055               // QFileDialog.  You can convert it to QString form with
00056               // fileDialogSaveFilter().join(";;")
00057               static const QStringList fileDialogLoadFilter();
00058 
00059               // Same deal but for save
00060               static const QStringList fileDialogSaveFilter();
00061 
00062               // Get the highest priority format of a given id, or 0 if
00063               // not found / not available.
00064               static const FileFormat * getFormatById(const int id);
00065 
00066               // Non-static members implemented by plugins:
00067               //
00068               // Load the requested format from the specified path.
00069               // Default implementation always reports failure.
00070               virtual bool loadFile(const QString & fileName, const FileFormat & fmt, int flags, int index = 0);
00071 
00072               // Save the requested format to the requested path.
00073               virtual bool saveFile(const QString & fileName, const FileFormat & fmt);
00074 
00075               // Examine the passed file and test to see whether it appears to be
00076               // loadable with this plugin. This test must be quick and simple.
00077               // It need not verify a file, just confirm that it looks like a supported
00078               // file type (eg "XML doc with root element SCRIBUSXML and version 1.3.1").
00079               // All plugins must implement this method.
00080               virtual bool fileSupported(QIODevice* file, const QString & fileName=QString::null) const = 0;
00081               
00082               // Return a list of all formats supported by all currently loaded and
00083               // active plugins. This list is sorted in a very specific order:
00084               // First, by descending order of `id', then descending order of priority.
00085               static const QValueList<FileFormat> & supportedFormats();
00086               
00087               virtual void setupTargets(ScribusDoc *targetDoc, ScribusView* targetView, ScribusMainWindow* targetMW, QProgressBar* targetMWPRogressBar, SCFonts* targetAvailableFonts);
00088               virtual void getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QValueList<ScFace> &getDummyScFaces);
00089               virtual bool loadPage(const QString & fileName, int pageNumber, bool Mpage, QString renamedPageName=QString::null);
00090               virtual bool readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles);
00091               virtual bool readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles);
00092               virtual bool readLineStyles(const QString& fileName, QMap<QString,multiLine> *Sty);
00093               virtual bool readColors(const QString& fileName, ColorList & colors);
00094               virtual bool readPageCount(const QString& fileName, int *num1, int *num2, QStringList & masterPageNames);
00095               
00096        protected:
00097 
00099               virtual bool checkFlags(int flags);
00100 
00102               void registerFormat(const FileFormat & fmt);
00103 
00105               void unregisterFormat(unsigned int id);
00106 
00108               void unregisterAll();
00109               
00110               ScribusDoc* m_Doc;
00111               ScribusView* m_View; //For 1.2.x loader at the moment
00112               ScribusMainWindow* m_ScMW; //For plugins when required
00113               QProgressBar* m_mwProgressBar;
00114               SCFonts* m_AvailableFonts;
00115 
00116        private:
00117               // A list of all supported formats. This is maintained by plugins
00118               // using the protected `registerFormat(...)', `unregisterFormat(...)'
00119               // and `unregisterAll(...)' methods. This is sorted in a very specific
00120               // order - ascending ID, then descending priority.
00121               static QValueList<FileFormat> formats;
00122 
00123               // Return an iterator referencing the first format structure named `name'.
00124               // If specified, only return formats implmented by `plug'.
00125               // If `start' is specified, start searching at this iterator rather than the
00126               // start of the list.
00127               // The end iterator is returned if no match was found.
00128               // Note that due to the sort order maintained in `formats', the first
00129               // iterator returned by this method will always be to the highest
00130               // priority format of the required ID, and each subsequent call will
00131               // return the next lowest priority format.
00132               static QValueList<FileFormat>::iterator findFormat(unsigned int id,
00133                             LoadSavePlugin* plug = 0,
00134                             QValueList<FileFormat>::iterator it = formats.begin());
00135                             
00136               static QValueList<FileFormat>::iterator findFormat(const QString& extension,
00137                             LoadSavePlugin* plug = 0,
00138                             QValueList<FileFormat>::iterator it = formats.begin());
00139 
00140               // Print out a format list for debugging purposes
00141               static void printFormatList();
00142 
00143               // Base implementation for fileDialogLoadFilter and fileDialogSaveFilter
00144               static const QStringList getDialogFilter(bool forLoad);
00145 };
00146 
00147 
00148 
00149 // Info on each supported format. A plugin must register a
00150 // FileFormat structure for every format it knows how to load or
00151 // save. If it both loads and saves a given format, one structure must
00152 // be registered for load and one for save.
00153 // Plugins must unregister formats when being unloaded to ensure that
00154 // no attempt is made to load a file using a plugin that's no longer
00155 // available.
00156 //
00157 // This class also provides methods to ask the implementation to load / save a
00158 // file in this format.
00159 class SCRIBUS_API FileFormat
00160 {
00161        public:
00162               // Default ctor to make QValueList happy
00163               FileFormat() : load(false), save(false), plug(0) {}
00164               // Standard ctor that sets up a valid FileFormat
00165               FileFormat(LoadSavePlugin * plug) : load(false), save(false), plug(plug) {}
00166               // Load a file with this format
00167               bool loadFile(const QString & fileName, int flags, int index = 0) const;
00168               // Save a file with this format
00169               bool saveFile(const QString & fileName) const;
00170               
00171               
00172               void setupTargets(ScribusDoc *targetDoc, ScribusView* targetView, ScribusMainWindow* targetMW, QProgressBar* targetMWPRogressBar, SCFonts* targetAvailableFonts) const;
00173               void getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QValueList<ScFace> &getDummyScFaces) const;
00174               bool loadPage(const QString & fileName, int pageNumber, bool Mpage, QString renamedPageName=QString::null) const;
00175               bool readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles) const;
00176               bool readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles) const;
00177               bool readLineStyles(const QString& fileName, QMap<QString,multiLine> *Sty) const;
00178               bool readColors(const QString& fileName, ColorList & colors) const;
00179               bool readPageCount(const QString& fileName, int *num1, int *num2, QStringList & masterPageNames) const;
00180               //
00181               // Data members
00182               //
00183               // An integer ID code used to idenfify formats. Should be unique
00184               // across all FileFormat structures except where they implement
00185               // support for the same file format, eg sla 1.2.x, sla 1.3.x and
00186               // "new format" SLA should all have equal IDs (with different
00187               // priorities to control what order they're tried in when a user
00188               // tries to open a file).
00189               // Note that dialog box options are sorted in descending `id' order.
00190               uint formatId;
00191               // The human-readable, translated name of this file format.
00192               QString trName;
00193               // A filter in the format used by QFileDialog that should be used to
00194               // select for this format.
00195               QString filter;
00196               // Regexp to match filenames for this format
00197               QRegExp nameMatch;
00198               // MIME type(s) that should be matched by this format.
00199               QStringList mimeTypes;
00200               // Can we load it?
00201               bool load;
00202               // Can we save it?
00203               bool save;
00204               // Priority of this format from 0 (lowest, tried last) to
00205               // 255 (highest, tried first). 64-128 recommended in general.
00206               // Priority controls the order options are displayed in when a file
00207               // of a given type is selected in a dialog, and controls the order
00208               // loaders are tried in when multiple plugins support the same file
00209               // type.
00210               unsigned short int priority;
00211               // For convenience, a pointer back to the plugin to use to open
00212               // this format.
00213               LoadSavePlugin * const plug;
00214 };
00215 
00216 
00217 
00218 #endif