File indexing completed on 2024-05-19 04:52:44
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 0012 /** 0013 * This class implements a QIODevice around KoStore, so that 0014 * it can be used to create a QDomDocument from it, to be written or read 0015 * using QDataStream or to be written using QTextStream 0016 */ 0017 class KoStoreDevice : public QIODevice 0018 { 0019 public: 0020 /// Note: KoStore::open() should be called before calling this. 0021 explicit KoStoreDevice( KoStore * store ) : m_store(store) { 0022 // koffice-1.x behavior compat: a KoStoreDevice is automatically open 0023 setOpenMode( m_store->mode() == KoStore::Read ? QIODevice::ReadOnly : QIODevice::WriteOnly ); 0024 } 0025 ~KoStoreDevice() override {} 0026 0027 bool isSequential() const override { return true; } 0028 0029 bool open( OpenMode m ) override { 0030 setOpenMode(m); 0031 if ( m & QIODevice::ReadOnly ) 0032 return ( m_store->mode() == KoStore::Read ); 0033 if ( m & QIODevice::WriteOnly ) 0034 return ( m_store->mode() == KoStore::Write ); 0035 return false; 0036 } 0037 void close() override {} 0038 0039 qint64 size() const override { 0040 if ( m_store->mode() == KoStore::Read ) 0041 return m_store->size(); 0042 else 0043 return 0xffffffff; 0044 } 0045 0046 qint64 readData( char *data, qint64 maxlen ) override { return m_store->read(data, maxlen); } 0047 qint64 writeData( const char *data, qint64 len ) override { return m_store->write( data, len ); } 0048 0049 #if 0 0050 int getch() { 0051 char c[2]; 0052 if ( m_store->read(c, 1) == -1) 0053 return -1; 0054 else 0055 return c[0]; 0056 } 0057 int putch( int _c ) { 0058 char c[2]; 0059 c[0] = _c; 0060 c[1] = 0; 0061 if (m_store->write( c, 1 ) == 1) 0062 return _c; 0063 else 0064 return -1; 0065 } 0066 int ungetch( int ) { return -1; } // unsupported 0067 #endif 0068 0069 // See QIODevice 0070 qint64 pos() const override { return m_store->pos(); } 0071 bool seek( qint64 pos ) override { return m_store->seek(pos); } 0072 bool atEnd() const override { return m_store->atEnd(); } 0073 0074 protected: 0075 KoStore * m_store; 0076 }; 0077 0078 #endif