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

0001 /***************************************************************************
0002     Copyright (C) 2008-2020 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_TELLICOIMPORTER_H
0026 #define TELLICO_IMPORT_TELLICOIMPORTER_H
0027 
0028 #include "dataimporter.h"
0029 #include "../datavectors.h"
0030 #include "../utils/stringset.h"
0031 
0032 #include <KZip>
0033 
0034 #include <memory>
0035 
0036 class QBuffer;
0037 class KZip;
0038 class KArchiveDirectory;
0039 
0040 namespace Tellico {
0041   namespace Import {
0042 
0043 /**
0044  * @author Robby Stephenson
0045  */
0046 class TellicoImporter : public DataImporter {
0047 Q_OBJECT
0048 
0049 public:
0050   enum Format { Unknown, Error, XML, Zip, Cancel };
0051 
0052   /**
0053    * @param url The tellico data file.
0054    */
0055   explicit TellicoImporter(const QUrl& url, bool loadAllImages=true);
0056   /**
0057    * Constructor used to convert arbitrary text to a @ref Collection
0058    *
0059    * @param text The text
0060    */
0061   explicit TellicoImporter(const QString& text);
0062   virtual ~TellicoImporter();
0063 
0064   /**
0065    * sometimes, a new document format might add data
0066    */
0067   bool modifiedOriginal() const { return m_modified; }
0068 
0069   /**
0070    */
0071   virtual Data::CollPtr collection() Q_DECL_OVERRIDE;
0072   /**
0073    * The TellicoImporter can import any type known to Tellico. Obviously.
0074    */
0075   virtual bool canImport(int type) const Q_DECL_OVERRIDE { Q_UNUSED(type); return true; }
0076   Format format() const { return m_format; }
0077 
0078   bool hasImages() const;
0079   bool loadImage(const QString& id_);
0080 
0081   void setBaseUrl(const QUrl& url) { m_baseUrl = url; }
0082 
0083   // take ownership of zip object with images
0084   std::unique_ptr<KZip> takeImages();
0085 
0086   static bool loadAllImages(const QUrl& url);
0087 
0088 public Q_SLOTS:
0089   void slotCancel() Q_DECL_OVERRIDE;
0090 
0091 private:
0092   void loadXMLData(const QByteArray& data, bool loadImages);
0093   void loadZipData();
0094 
0095   Data::CollPtr m_coll;
0096   bool m_loadAllImages;
0097   QString m_namespace;
0098   Format m_format;
0099   bool m_modified;
0100   bool m_cancelled;
0101   bool m_hasImages;
0102   StringSet m_images;
0103   QUrl m_baseUrl;
0104 
0105   std::unique_ptr<QBuffer> m_buffer;
0106   std::unique_ptr<KZip> m_zip;
0107   // no ownership of this pointer
0108   const KArchiveDirectory* m_imgDir;
0109 };
0110 
0111   } // end namespace
0112 } // end namespace
0113 #endif