File indexing completed on 2024-05-12 05:09:50

0001 /***************************************************************************
0002     Copyright (C) 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_IMAGEDIRECTORY_H
0026 #define TELLICO_IMAGEDIRECTORY_H
0027 
0028 #include "../utils/stringset.h"
0029 
0030 #include <QString>
0031 
0032 #include <memory>
0033 
0034 class QTemporaryDir;
0035 
0036 class KZip;
0037 class KArchiveDirectory;
0038 
0039 namespace Tellico {
0040   namespace Data {
0041     class Image;
0042   }
0043 
0044 class ImageStorage {
0045 public:
0046   ImageStorage() {}
0047   virtual ~ImageStorage() {}
0048 
0049   virtual bool hasImage(const QString& id) = 0;
0050   virtual Data::Image* imageById(const QString& id) = 0;
0051 
0052 private:
0053   Q_DISABLE_COPY(ImageStorage)
0054 };
0055 
0056 class ImageDirectory : public ImageStorage {
0057 public:
0058   ImageDirectory();
0059   ImageDirectory(const QString& path);
0060   virtual ~ImageDirectory();
0061 
0062   virtual QString path();
0063   virtual void setPath(const QString& path);
0064 
0065   bool hasImage(const QString& id) Q_DECL_OVERRIDE;
0066   Data::Image* imageById(const QString& id) Q_DECL_OVERRIDE;
0067   bool writeImage(const Data::Image& image);
0068   bool removeImage(const QString& id);
0069 
0070 private:
0071   Q_DISABLE_COPY(ImageDirectory)
0072   QString m_path;
0073   bool m_pathExists;
0074   // until the file gets saved, the local directory is temporary
0075   QTemporaryDir* m_dir;
0076 };
0077 
0078 class TemporaryImageDirectory : public ImageDirectory {
0079 public:
0080   TemporaryImageDirectory();
0081   virtual ~TemporaryImageDirectory();
0082 
0083   virtual QString path() Q_DECL_OVERRIDE;
0084   void purge();
0085 
0086 private:
0087   Q_DISABLE_COPY(TemporaryImageDirectory)
0088   void setPath(const QString& path) Q_DECL_OVERRIDE;
0089 
0090   QTemporaryDir* m_dir;
0091 };
0092 
0093 class ImageZipArchive : public ImageStorage {
0094 public:
0095   ImageZipArchive();
0096   virtual ~ImageZipArchive();
0097 
0098   void setZip(std::unique_ptr<KZip> zip);
0099 
0100   bool hasImage(const QString& id) Q_DECL_OVERRIDE;
0101   Data::Image* imageById(const QString& id) Q_DECL_OVERRIDE;
0102 
0103 private:
0104   Q_DISABLE_COPY(ImageZipArchive)
0105   std::unique_ptr<KZip> m_zip;
0106   const KArchiveDirectory* m_imgDir;
0107   StringSet m_images;
0108 };
0109 
0110 } // end namespace
0111 #endif