File indexing completed on 2024-05-12 15:59:59

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef koStoreDevice_h
0008 #define koStoreDevice_h
0009 
0010 #include "KoStore.h"
0011 #include <kritastore_export.h>
0012 
0013 
0014 /**
0015  * This class implements a QIODevice around KoStore, so that
0016  * it can be used to create a QDomDocument from it, to be written or read
0017  * using QDataStream or to be written using QTextStream
0018  */
0019 class KRITASTORE_EXPORT KoStoreDevice : public QIODevice
0020 {
0021     Q_OBJECT
0022 public:
0023     /// Note: KoStore::open() should be called before calling this.
0024     explicit KoStoreDevice(KoStore * store) : m_store(store) {
0025         // calligra-1.x behavior compat: a KoStoreDevice is automatically open
0026         setOpenMode(m_store->mode() == KoStore::Read ? QIODevice::ReadOnly : QIODevice::WriteOnly);
0027     }
0028     ~KoStoreDevice() override;
0029 
0030     bool isSequential() const override {
0031         return true;
0032     }
0033 
0034     bool open(OpenMode m) override {
0035         setOpenMode(m);
0036         if (m & QIODevice::ReadOnly)
0037             return (m_store->mode() == KoStore::Read);
0038         if (m & QIODevice::WriteOnly)
0039             return (m_store->mode() == KoStore::Write);
0040         return false;
0041     }
0042     void close() override {}
0043 
0044     qint64 size() const override {
0045         if (m_store->mode() == KoStore::Read)
0046             return m_store->size();
0047         else
0048             return 0xffffffff;
0049     }
0050 
0051     // See QIODevice
0052     qint64 pos() const override {
0053         return m_store->pos();
0054     }
0055     bool seek(qint64 pos) override {
0056         return m_store->seek(pos);
0057     }
0058     bool atEnd() const override {
0059         return m_store->atEnd();
0060     }
0061 
0062 protected:
0063     KoStore *m_store;
0064 
0065     qint64 readData(char *data, qint64 maxlen) override {
0066         return m_store->read(data, maxlen);
0067     }
0068 
0069     qint64 writeData(const char *data, qint64 len) override {
0070         return m_store->write(data, len);
0071     }
0072 
0073 };
0074 
0075 #endif