File indexing completed on 2024-05-12 05:10:12

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_IMPORT_IMPORTER_H
0026 #define TELLICO_IMPORT_IMPORTER_H
0027 
0028 #include "../datavectors.h"
0029 #include "../collection.h"
0030 
0031 #include <QObject>
0032 #include <QString>
0033 #include <QUrl>
0034 
0035 class QWidget;
0036 
0037 namespace Tellico {
0038   namespace Import {
0039     enum Options {
0040       ImportProgress        = 1 << 0,   // show progress bar
0041       ImportShowImageErrors = 1 << 1
0042     };
0043 
0044 /**
0045  * The top-level abstract class for importing other document formats into Tellico.
0046  *
0047  * The Importer classes import a file, and return a pointer to a newly created
0048  * @ref Data::Collection. Any errors or warnings are added to a status message queue.
0049  * The calling function owns the collection pointer.
0050  *
0051  * @author Robby Stephenson
0052  */
0053 class Importer : public QObject {
0054 Q_OBJECT
0055 
0056 public:
0057   Importer();
0058   /**
0059    * The constructor should immediately load the contents of the file to be imported.
0060    * Any warnings or errors should be added to the status message queue.
0061    *
0062    * @param url The URL of the file to import
0063    */
0064   Importer(const QUrl& url);
0065   Importer(const QList<QUrl>& urls);
0066   Importer(const QString& text);
0067   /**
0068    */
0069   virtual ~Importer() {}
0070 
0071   /**
0072    * Returns a pointer to a @ref Data::Collection containing the contents of the imported file.
0073    * This function should probably only be called once, but the subclasses may cache the
0074    * collection. The collection should not be created until this function is called.
0075    *
0076    * @return A pointer to a @ref Collection created on the stack, or 0 if none could be created.
0077    */
0078   virtual Data::CollPtr collection() = 0;
0079   /**
0080    * Returns a string containing all the messages added to the queue in the course of loading
0081    * and importing the file.
0082    *
0083    * @return The status message
0084    */
0085   const QString& statusMessage() const { return m_statusMsg; }
0086   /**
0087    * Returns a widget with the setting specific to this importer, or 0 if no
0088    * options are needed.
0089    *
0090    * @return A pointer to the setting widget
0091    */
0092   virtual QWidget* widget(QWidget*) { return nullptr; }
0093   /**
0094    * Checks to see if the importer can return a collection of this type
0095    *
0096    * @param type The collection type to check
0097    * @return Whether the importer could return a collection of that type
0098    */
0099   virtual bool canImport(int type) const = 0;
0100   /**
0101    * Validate the import settings
0102    */
0103   virtual bool validImport() const { return true; }
0104   virtual void setText(const QString& text) { m_text = text; }
0105   long options() const { return m_options; }
0106   void setOptions(long options) { m_options = options; }
0107   /**
0108    * Returns a string useful for the ProgressManager
0109    */
0110   QString progressLabel() const;
0111   /**
0112    * Sets a pointer to the existing collection in case importers need to use existing field information
0113    */
0114   void setCurrentCollection(Data::CollPtr coll) { m_currentCollection = coll; }
0115 
0116 public Q_SLOTS:
0117   /**
0118    * The import action was changed in the import dialog
0119    */
0120   virtual void slotActionChanged(int) {}
0121   virtual void slotCancel() = 0;
0122 
0123 Q_SIGNALS:
0124   void signalTotalSteps(QObject* obj, qulonglong steps);
0125   void signalProgress(QObject* obj, qulonglong progress);
0126 
0127 protected:
0128   /**
0129    * Return the URL of the imported file.
0130    *
0131    * @return the file URL
0132    */
0133   QUrl url() const { return m_urls.isEmpty() ? QUrl() : m_urls[0]; }
0134   QList<QUrl> urls() const { return m_urls; }
0135   QString text() const { return m_text; }
0136   Data::CollPtr currentCollection() const;
0137   /**
0138    * Adds a message to the status queue.
0139    *
0140    * @param msg A string containing a warning or error.
0141    */
0142   void setStatusMessage(const QString& msg) { if(!msg.isEmpty()) m_statusMsg += msg + QLatin1Char(' '); }
0143 
0144   static const uint s_stepSize;
0145 
0146 private:
0147   long m_options;
0148   QList<QUrl> m_urls;
0149   QString m_text;
0150   QString m_statusMsg;
0151   Data::CollPtr m_currentCollection;
0152 };
0153 
0154   } // end namespace
0155 } // end namespace
0156 
0157 #endif