Warning, file /office/calligra/libs/store/KoStore.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 David Faure <faure@kde.org>
0003    Copyright (C) 2010 C. Boemann <cbo@boemann.dk>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef __koStore_h_
0022 #define __koStore_h_
0023 
0024 #include <QByteArray>
0025 #include <QIODevice>
0026 #include "kostore_export.h"
0027 
0028 class QWidget;
0029 class QUrl;
0030 class KoStorePrivate;
0031 
0032 /**
0033  * Saves and loads Calligra documents using various backends. Currently supported
0034  * backends are ZIP, tar and directory.
0035  * We call a "store" the file on the hard disk (the one the users sees)
0036  * and call a "file" a file inside the store.
0037  */
0038 class KOSTORE_EXPORT KoStore
0039 {
0040 public:
0041 
0042     enum Mode { Read, Write };
0043     enum Backend { Auto, Tar, Zip, Directory, Encrypted };
0044 
0045     /**
0046      * Open a store (i.e. the representation on disk of a Calligra document).
0047      *
0048      * @param fileName the name of the file to open
0049      * @param mode if KoStore::Read, open an existing store to read it.
0050      *             if KoStore::Write, create or replace a store.
0051      * @param backend the backend to use for the data storage.
0052      * Auto means automatically-determined for reading,
0053      * and the current format (now Zip) for writing.
0054      *
0055      * @param appIdentification the application's mimetype,
0056      * to be written in the file for "mime-magic" identification.
0057      * Only meaningful if mode is Write, and if backend!=Directory.
0058      *
0059      * @param writeMimetype If true, some backends (notably the Zip
0060      * store) will write a file called 'mimetype' automatically and
0061      * fill it with data from the appIdentification. This is only
0062      * applicable if Mode is set to Write.
0063      */
0064     static KoStore *createStore(const QString &fileName, Mode mode,
0065                                 const QByteArray &appIdentification = QByteArray(),
0066                                 Backend backend = Auto, bool writeMimetype = true);
0067 
0068     /**
0069      * Create a store for any kind of QIODevice: file, memory buffer...
0070      * KoStore will take care of opening the QIODevice.
0071      * This method doesn't support the Directory store!
0072      */
0073     static KoStore *createStore(QIODevice *device, Mode mode,
0074                                 const QByteArray &appIdentification = QByteArray(),
0075                                 Backend backend = Auto, bool writeMimetype = true);
0076 
0077     /**
0078      * Open a store (i.e. the representation on disk of a Calligra document).
0079      *
0080      * @param window associated window (for the progress bar dialog and authentication)
0081      * @param url URL of the file to open
0082      * @param mode if KoStore::Read, open an existing store to read it.
0083      *             if KoStore::Write, create or replace a store.
0084      * @param backend the backend to use for the data storage.
0085      * Auto means automatically-determined for reading,
0086      * and the current format (now Zip) for writing.
0087      *
0088      * @param appIdentification the application's mimetype,
0089      * to be written in the file for "mime-magic" identification.
0090      * Only meaningful if mode is Write, and if backend!=Directory.
0091      *
0092      * If the file is remote, the backend Directory cannot be used!
0093      *
0094      * @param writeMimetype If true, some backends (notably the Zip
0095      * store) will write a file called 'mimetype' automatically and
0096      * fill it with data from the appIdentification. This is only
0097      * applicable if Mode is set to Write.
0098      *
0099      * @bug saving not completely implemented (fixed temporary file)
0100      */
0101     static KoStore *createStore(QWidget *window, const QUrl &url, Mode mode,
0102                                 const QByteArray &appIdentification = QByteArray(), Backend backend = Auto, bool writeMimetype = true);
0103 
0104     /**
0105      * Destroys the store (i.e. closes the file on the hard disk)
0106      */
0107     virtual ~KoStore();
0108 
0109     /**
0110      * Returns the url of the store. It can be a filename or a remote url.
0111      * it can also be empty, if the store is a bytearray
0112      * @return the url of the store as supplied in the createStore calls
0113      */
0114     QUrl urlOfStore() const;
0115 
0116     /**
0117      * Open a new file inside the store
0118      * @param name The filename, internal representation ("root", "tar:/0"... ).
0119      *        If the tar:/ prefix is missing it's assumed to be a relative URI.
0120      * @return true on success.
0121      */
0122     bool open(const QString &name);
0123 
0124     /**
0125      * Check whether a file inside the store is currently opened with open(),
0126      * ready to be read or written.
0127      * @return true if a file is currently opened.
0128      */
0129     bool isOpen() const;
0130 
0131     /**
0132      * Close the file inside the store
0133      * @return true on success.
0134      */
0135     bool close();
0136 
0137     /**
0138      * Get a device for reading a file from the store directly
0139      * (slightly faster than read() calls)
0140      * You need to call @ref open first, and @ref close afterwards.
0141      */
0142     QIODevice *device() const;
0143 
0144     /**
0145      * Read data from the currently opened file. You can also use the streams
0146      * for this.
0147      */
0148     QByteArray read(qint64 max);
0149 
0150     /**
0151      * Write data into the currently opened file. You can also use the streams
0152      * for this.
0153      */
0154     qint64 write(const QByteArray &data);
0155 
0156     /**
0157      * Read data from the currently opened file. You can also use the streams
0158      * for this.
0159      * @return size of data read, -1 on error
0160      */
0161     qint64 read(char *buffer, qint64 length);
0162 
0163     /**
0164      * Write data into the currently opened file. You can also use the streams
0165      * for this.
0166      */
0167     virtual qint64 write(const char* data, qint64 length);
0168 
0169     /**
0170      * @return the size of the currently opened file, -1 on error.
0171      * Can be used as an argument for the read methods, for instance
0172      */
0173     qint64 size() const;
0174 
0175     /**
0176      * @return true if an error occurred
0177      */
0178     bool bad() const;
0179 
0180     /**
0181      * @return the mode used when opening, read or write
0182      */
0183     Mode mode() const;
0184 
0185     /**
0186      * If an store is opened for reading, then the directories
0187      * of the store can be accessed via this function.
0188      *
0189      * @return a stringlist with all directories found
0190      */
0191     virtual QStringList directoryList() const;
0192 
0193     /**
0194      * Enters one or multiple directories. In Read mode this actually
0195      * checks whether the specified directories exist and returns false
0196      * if they don't. In Write mode we don't create the directory, we
0197      * just use the "current directory" to generate the absolute path
0198      * if you pass a relative path (one not starting with tar:/) when
0199      * opening a stream.
0200      * Note: Operates on internal names
0201      */
0202     bool enterDirectory(const QString &directory);
0203 
0204     /**
0205      * Leaves a directory. Equivalent to "cd .."
0206      * @return true on success, false if we were at the root already to
0207      * make it possible to "loop to the root"
0208      */
0209     bool leaveDirectory();
0210 
0211     /**
0212      * Returns the current path including a trailing slash.
0213      * Note: Returns a path in "internal name" style
0214      */
0215     QString currentPath() const;
0216 
0217     /**
0218      * Stacks the current directory. Restore the current path using
0219      * @ref popDirectory .
0220      */
0221     void pushDirectory();
0222 
0223     /**
0224      * Restores the previously pushed directory. No-op if the stack is
0225      * empty.
0226      */
0227     void popDirectory();
0228 
0229     /**
0230      * @return true if the given file exists in the current directory,
0231      * i.e. if open(fileName) will work.
0232      */
0233     bool hasFile(const QString &fileName) const;
0234 
0235     /**
0236      * Imports a local file into a store
0237      * @param fileName file on hard disk
0238      * @param destName file in the store
0239      */
0240     bool addLocalFile(const QString &fileName, const QString &destName);
0241 
0242     /**
0243      * Imports data into a store
0244      * @param buffer data
0245      * @param destName file in the store
0246      */
0247     bool addDataToFile(QByteArray &buffer, const QString &destName);
0248 
0249     /**
0250      * Extracts a file out of the store
0251      * @param sourceName file in the store
0252      * @param fileName file on a disk
0253      */
0254     bool extractFile(const QString &sourceName, const QString &fileName);
0255 
0256     /**
0257      * Extracts a file out of the store to a buffer
0258      * @param sourceName file in the store
0259      * @param data memory buffer
0260      */
0261     bool extractFile(const QString &sourceName, QByteArray &data);
0262 
0263     //@{
0264     /// See QIODevice
0265     bool seek(qint64 pos);
0266     qint64 pos() const;
0267     bool atEnd() const;
0268     //@}
0269 
0270     /**
0271      * Call this before destroying the store, to be able to catch errors
0272      * (e.g. from ksavefile)
0273      */
0274     bool finalize();
0275 
0276     /**
0277      * Sets the password to be used for decryption or encryption of the store.
0278      * Use of this function is optional: an encryptable store should make
0279      * a best effort in obtaining a password if it wasn't supplied.
0280      *
0281      * This method only works before opening a file. It might fail when a file
0282      * has already been opened before calling this method.
0283      *
0284      * This method will not function for any store that is not encrypted or
0285      * can't be encrypted when saving.
0286      *
0287      * @param   password    A non-empty password.
0288      *
0289      * @return  True if the password was set.
0290      */
0291     virtual bool setPassword(const QString &password);
0292 
0293     /**
0294      * Retrieves the password used to encrypt or decrypt the store. Note that
0295      * QString() will returned if no password has been given or the store is
0296      * not encrypted.
0297      *
0298      * @return  The password this store is encrypted with.
0299      */
0300     virtual QString password();
0301 
0302     /**
0303      * Returns whether a store opened for reading is encrypted or a store opened
0304      * for saving will be encrypted.
0305      *
0306      * @return  True if the store is encrypted.
0307      */
0308     virtual bool isEncrypted();
0309 
0310     /**
0311      * Allow to enable or disable compression of the files. Only supported by the
0312      * ZIP backend.
0313      */
0314     virtual void setCompressionEnabled(bool e);
0315 
0316 protected:
0317     KoStore(Mode mode, bool writeMimetype = true);
0318 
0319     /**
0320      * Finalize store - called by finalize.
0321      * @return true on success
0322      */
0323     virtual bool doFinalize() {
0324         return true;
0325     }
0326 
0327     /**
0328      * Open the file @p name in the store, for writing
0329      * On success, this method must set m_stream to a stream in which we can write.
0330      * @param name "absolute path" (in the archive) to the file to open
0331      * @return true on success
0332      */
0333     virtual bool openWrite(const QString &name) = 0;
0334     /**
0335      * Open the file @p name in the store, for reading.
0336      * On success, this method must set m_stream to a stream from which we can read,
0337      * as well as setting m_iSize to the size of the file.
0338      * @param name "absolute path" (in the archive) to the file to open
0339      * @return true on success
0340      */
0341     virtual bool openRead(const QString &name) = 0;
0342 
0343     /**
0344      * @return true on success
0345      */
0346     virtual bool closeRead() = 0;
0347     /**
0348      * @return true on success
0349      */
0350     virtual bool closeWrite() = 0;
0351 
0352     /**
0353      * Enter a subdirectory of the current directory.
0354      * The directory might not exist yet in Write mode.
0355      */
0356     virtual bool enterRelativeDirectory(const QString &dirName) = 0;
0357     /**
0358      * Enter a directory where we've been before.
0359      * It is guaranteed to always exist.
0360      */
0361     virtual bool enterAbsoluteDirectory(const QString &path) = 0;
0362 
0363     /**
0364      * Check if a file exists inside the store.
0365      * @param absPath the absolute path inside the store, i.e. not relative to the current directory
0366      */
0367     virtual bool fileExists(const QString &absPath) const = 0;
0368 
0369 protected:
0370     KoStorePrivate *d_ptr;
0371 
0372 private:
0373     Q_DECLARE_PRIVATE(KoStore)
0374 
0375 private:
0376     KoStore(const KoStore& store);    ///< don't copy
0377     KoStore& operator=(const KoStore& store);    ///< don't assign
0378 };
0379 
0380 #endif