Krita Source Code Documentation
Loading...
Searching...
No Matches
KoStore Class Referenceabstract

#include <KoStore.h>

+ Inheritance diagram for KoStore:

Public Types

enum  Backend { Auto , Zip , Directory }
 
enum  Mode { Read , Write }
 

Public Member Functions

bool atEnd () const
 
bool bad () const
 
bool close ()
 
QString currentPath () const
 
QIODevice * device () const
 
virtual QStringList directoryList () const
 
virtual bool enterDirectory (const QString &directory)
 
bool extractFile (const QString &sourceName, QByteArray &data)
 
bool finalize ()
 
bool hasDirectory (const QString &directoryName)
 
bool hasFile (const QString &fileName) const
 
bool isOpen () const
 
bool leaveDirectory ()
 
Mode mode () const
 
bool open (const QString &name)
 
void popDirectory ()
 
qint64 pos () const
 
void pushDirectory ()
 
qint64 read (char *buffer, qint64 length)
 
QByteArray read (qint64 max)
 
bool seek (qint64 pos)
 See QIODevice.
 
virtual void setCompressionEnabled (bool e)
 
void setSubstitution (const QString &name, const QString &substitution)
 When reading, in the paths in the store where name occurs, substitution is used.
 
qint64 size () const
 
virtual qint64 write (const char *data, qint64 length)
 
qint64 write (const QByteArray &data)
 
virtual ~KoStore ()
 

Static Public Member Functions

static KoStorecreateStore (const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
 
static KoStorecreateStore (QIODevice *device, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
 

Protected Member Functions

virtual bool closeRead ()=0
 
virtual bool closeWrite ()=0
 
virtual bool doFinalize ()
 
virtual bool enterAbsoluteDirectory (const QString &path)=0
 
virtual bool enterRelativeDirectory (const QString &dirName)=0
 
virtual bool fileExists (const QString &absPath) const =0
 
 KoStore (Mode mode, bool writeMimetype=true)
 
virtual bool openRead (const QString &name)=0
 
virtual bool openWrite (const QString &name)=0
 

Protected Attributes

KoStorePrivated_ptr
 

Private Member Functions

 KoStore (const KoStore &store)
 don't copy
 
KoStoreoperator= (const KoStore &store)
 don't assign
 

Detailed Description

Saves and loads Krita documents using various backends. Currently supported backends are zip and directory. We call a "store" the file on the hard disk (the one the users sees) and call a "file" a file inside the store.

Definition at line 25 of file KoStore.h.

Member Enumeration Documentation

◆ Backend

Enumerator
Auto 
Zip 
Directory 

Definition at line 30 of file KoStore.h.

30{ Auto, Zip, Directory };
@ Auto
Definition KoStore.h:30
@ Zip
Definition KoStore.h:30
@ Directory
Definition KoStore.h:30

◆ Mode

Enumerator
Read 
Write 

Definition at line 29 of file KoStore.h.

29{ Read, Write };
@ Read
Definition KoStore.h:29
@ Write
Definition KoStore.h:29

Constructor & Destructor Documentation

◆ ~KoStore()

KoStore::~KoStore ( )
virtual

Destroys the store (i.e. closes the file on the hard disk)

Definition at line 102 of file KoStore.cpp.

103{
104 Q_D(KoStore);
105 delete d->stream;
106 delete d_ptr;
107}
KoStorePrivate * d_ptr
Definition KoStore.h:278

References d_ptr.

◆ KoStore() [1/2]

KoStore::KoStore ( Mode mode,
bool writeMimetype = true )
protected

Definition at line 98 of file KoStore.cpp.

99 : d_ptr(new KoStorePrivate(this, mode, writeMimetype))
100{}
Mode mode() const
Definition KoStore.cpp:420

◆ KoStore() [2/2]

KoStore::KoStore ( const KoStore & store)
private

don't copy

Member Function Documentation

◆ atEnd()

bool KoStore::atEnd ( ) const

Definition at line 353 of file KoStore.cpp.

354{
355 Q_D(const KoStore);
356 return d->stream->atEnd();
357}

◆ bad()

bool KoStore::bad ( ) const
Returns
true if an error occurred

Definition at line 414 of file KoStore.cpp.

415{
416 Q_D(const KoStore);
417 return !d->good;
418}

◆ close()

bool KoStore::close ( )

Close the file inside the store

Returns
true on success.

Definition at line 156 of file KoStore.cpp.

157{
158 Q_D(KoStore);
159 if (!d->isOpen) {
160 warnStore << "You must open before closing";
161 return false;
162 }
163
164 bool ret = d->mode == Write ? closeWrite() : closeRead();
165 delete d->stream;
166 d->stream = 0;
167 d->isOpen = false;
168 return ret;
169}
#define warnStore
Definition StoreDebug.h:16
virtual bool closeRead()=0
virtual bool closeWrite()=0

References closeRead(), closeWrite(), warnStore, and Write.

◆ closeRead()

virtual bool KoStore::closeRead ( )
protectedpure virtual
Returns
true on success

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ closeWrite()

virtual bool KoStore::closeWrite ( )
protectedpure virtual
Returns
true on success

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ createStore() [1/2]

KoStore * KoStore::createStore ( const QString & fileName,
Mode mode,
const QByteArray & appIdentification = QByteArray(),
Backend backend = Auto,
bool writeMimetype = true )
static

Open a store (i.e. the representation on disk of a Krita document).

Parameters
fileNamethe name of the file to open
modeif KoStore::Read, open an existing store to read it. if KoStore::Write, create or replace a store.
backendthe backend to use for the data storage. Auto means automatically-determined for reading, and the current format (now Zip) for writing.
appIdentificationthe application's mimetype, to be written in the file for "mime-magic" identification. Only meaningful if mode is Write, and if backend!=Directory.
writeMimetypeIf true, some backends (notably the Zip store) will write a file called 'mimetype' automatically and fill it with data from the appIdentification. This is only applicable if Mode is set to Write.

Definition at line 39 of file KoStore.cpp.

40{
41 if (backend == Auto) {
42 if (mode == KoStore::Write)
43 backend = DefaultFormat;
44 else {
45 QFileInfo inf(fileName);
46 if (inf.isDir())
47 backend = Directory;
48 else {
49 QFile file(fileName);
50 if (file.open(QIODevice::ReadOnly))
51 backend = determineBackend(&file);
52 else
53 backend = DefaultFormat; // will create a "bad" store (bad()==true)
54 }
55 }
56 }
57 switch (backend) {
58 case Zip:
59 return new KoQuaZipStore(fileName, mode, appIdentification, writeMimetype);
60 case Directory:
61 return new KoDirectoryStore(fileName /* should be a dir name.... */, mode, writeMimetype);
62 default:
63 warnStore << "Unsupported backend requested for KoStore : " << backend;
64 return 0;
65 }
66}
#define DefaultFormat
Definition KoStore.cpp:27
static KoStore::Backend determineBackend(QIODevice *dev)
Definition KoStore.cpp:29

References Auto, DefaultFormat, determineBackend(), Directory, mode(), warnStore, Write, and Zip.

◆ createStore() [2/2]

KoStore * KoStore::createStore ( QIODevice * device,
Mode mode,
const QByteArray & appIdentification = QByteArray(),
Backend backend = Auto,
bool writeMimetype = true )
static

Create a store for any kind of QIODevice: file, memory buffer... KoStore will take care of opening the QIODevice. This method doesn't support the Directory store!

Definition at line 68 of file KoStore.cpp.

69{
70 if (backend == Auto) {
71 if (mode == KoStore::Write)
72 backend = DefaultFormat;
73 else {
74 if (device->open(QIODevice::ReadOnly)) {
75 backend = determineBackend(device);
76 device->close();
77 }
78 }
79 }
80 switch (backend) {
81 case Directory:
82 errorStore << "Can't create a Directory store for a memory buffer!" << Qt::endl;
83 return 0;
84 case Zip:
85 return new KoQuaZipStore(device, mode, appIdentification, writeMimetype);
86 default:
87 warnStore << "Unsupported backend requested for KoStore : " << backend;
88 return 0;
89 }
90}
#define errorStore
Definition StoreDebug.h:17
QIODevice * device() const
Definition KoStore.cpp:171

References Auto, DefaultFormat, determineBackend(), device(), Directory, errorStore, mode(), warnStore, Write, and Zip.

◆ currentPath()

QString KoStore::currentPath ( ) const

Returns the current path including a trailing slash. Note: Returns a path in "internal name" style

Definition at line 281 of file KoStore.cpp.

282{
283 Q_D(const KoStore);
284 QString path;
285 QStringList::ConstIterator it = d->currentPath.begin();
286 QStringList::ConstIterator end = d->currentPath.end();
287 for (; it != end; ++it) {
288 path += *it;
289 path += '/';
290 }
291 return path;
292}

◆ device()

QIODevice * KoStore::device ( ) const

Get a device for reading a file from the store directly (slightly faster than read() calls) You need to call open first, and close afterwards.

Definition at line 171 of file KoStore.cpp.

172{
173 Q_D(const KoStore);
174 if (!d->isOpen)
175 warnStore << "You must open before asking for a device";
176 if (d->mode != Read)
177 warnStore << "Can not get device from store that is opened for writing";
178 return d->stream;
179}

References Read, and warnStore.

◆ directoryList()

QStringList KoStore::directoryList ( ) const
virtual

If an store is opened for reading, then the directories of the store can be accessed via this function.

Returns
a stringlist with all directories found

Reimplemented in KoQuaZipStore.

Definition at line 426 of file KoStore.cpp.

427{
428 return QStringList();
429}
QList< QString > QStringList

◆ doFinalize()

virtual bool KoStore::doFinalize ( )
inlineprotectedvirtual

Finalize store - called by finalize.

Returns
true on success

Reimplemented in KoQuaZipStore.

Definition at line 230 of file KoStore.h.

230 {
231 return true;
232 }

◆ enterAbsoluteDirectory()

virtual bool KoStore::enterAbsoluteDirectory ( const QString & path)
protectedpure virtual

Enter a directory where we've been before. It is guaranteed to always exist.

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ enterDirectory()

bool KoStore::enterDirectory ( const QString & directory)
virtual

Enters one or multiple directories. In Read mode this actually checks whether the specified directories exist and returns false if they don't. In Write mode we don't create the directory, we just use the "current directory" to generate the absolute path if you pass a relative path (one not starting with tar:/) when opening a stream. Note: Operates on internal names

Definition at line 253 of file KoStore.cpp.

254{
255 Q_D(KoStore);
256 //debugStore <<"enterDirectory" << directory;
257 int pos;
258 bool success = true;
259 QString tmp(directory);
260
261 while ((pos = tmp.indexOf('/')) != -1 &&
262 (success = d->enterDirectoryInternal(tmp.left(pos))))
263 tmp.remove(0, pos + 1);
264
265 if (success && !tmp.isEmpty())
266 return d->enterDirectoryInternal(tmp);
267 return success;
268}
qint64 pos() const
Definition KoStore.cpp:347

References pos().

◆ enterRelativeDirectory()

virtual bool KoStore::enterRelativeDirectory ( const QString & dirName)
protectedpure virtual

Enter a subdirectory of the current directory. The directory might not exist yet in Write mode.

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ extractFile()

bool KoStore::extractFile ( const QString & sourceName,
QByteArray & data )

Extracts a file out of the store to a buffer

Parameters
sourceNamefile in the store
datamemory buffer

Definition at line 308 of file KoStore.cpp.

309{
310 Q_D(KoStore);
311 QBuffer buffer(&data);
312 return d->extractFile(srcName, buffer);
313}

◆ fileExists()

virtual bool KoStore::fileExists ( const QString & absPath) const
protectedpure virtual

Check if a file exists inside the store.

Parameters
absPaththe absolute path inside the store, i.e. not relative to the current directory

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ finalize()

bool KoStore::finalize ( )

Call this before destroying the store, to be able to catch errors (e.g. from ksavefile)

Definition at line 395 of file KoStore.cpp.

396{
397 Q_D(KoStore);
398 Q_ASSERT(!d->finalized); // call this only once!
399 d->finalized = true;
400 return doFinalize();
401}
virtual bool doFinalize()
Definition KoStore.h:230

References doFinalize().

◆ hasDirectory()

bool KoStore::hasDirectory ( const QString & directoryName)
Returns
true if the given directory exists in the archive

Definition at line 390 of file KoStore.cpp.

391{
392 return enterAbsoluteDirectory(directoryName);
393}
virtual bool enterAbsoluteDirectory(const QString &path)=0

References enterAbsoluteDirectory().

◆ hasFile()

bool KoStore::hasFile ( const QString & fileName) const
Returns
true if the given file exists in the current directory, i.e. if open(fileName) will work.

Definition at line 384 of file KoStore.cpp.

385{
386 Q_D(const KoStore);
387 return fileExists(d->toExternalNaming(fileName));
388}
virtual bool fileExists(const QString &absPath) const =0

References fileExists().

◆ isOpen()

bool KoStore::isOpen ( ) const

Check whether a file inside the store is currently opened with open(), ready to be read or written.

Returns
true if a file is currently opened.

Definition at line 150 of file KoStore.cpp.

151{
152 Q_D(const KoStore);
153 return d->isOpen;
154}

◆ leaveDirectory()

bool KoStore::leaveDirectory ( )

Leaves a directory. Equivalent to "cd .."

Returns
true on success, false if we were at the root already to make it possible to "loop to the root"

Definition at line 270 of file KoStore.cpp.

271{
272 Q_D(KoStore);
273 if (d->currentPath.isEmpty())
274 return false;
275
276 d->currentPath.pop_back();
277
279}
QString currentPath() const
Definition KoStore.cpp:281

References currentPath(), and enterAbsoluteDirectory().

◆ mode()

KoStore::Mode KoStore::mode ( ) const
Returns
the mode used when opening, read or write

Definition at line 420 of file KoStore.cpp.

421{
422 Q_D(const KoStore);
423 return d->mode;
424}

◆ open()

bool KoStore::open ( const QString & name)

Open a new file inside the store

Parameters
nameThe filename, internal representation ("root", "tar:/0"... ). If the tar:/ prefix is missing it's assumed to be a relative URI.
Returns
true on success.

Definition at line 109 of file KoStore.cpp.

110{
111 Q_D(KoStore);
112 // This also converts from relative to absolute, i.e. merges the currentPath()
113 d->fileName = d->toExternalNaming(_name);
114
115 debugStore << "KOStore" << _name << d->fileName;
116
117 if (d->isOpen) {
118 warnStore << "Store is already opened, missing close";
119 return false;
120 }
121
122 if (d->fileName.length() > 512) {
123 errorStore << "KoStore: Filename " << d->fileName << " is too long" << Qt::endl;
124 return false;
125 }
126
127 if (d->mode == Write) {
128 debugStore << "opening for writing" << d->fileName;
129 if (d->filesList.contains(d->fileName)) {
130 warnStore << "KoStore: Duplicate filename" << d->fileName;
131 return false;
132 }
133
134 d->filesList.append(d->fileName);
135
136 d->size = 0;
137 if (!openWrite(d->fileName))
138 return false;
139 } else if (d->mode == Read) {
140 debugStore << "Opening for reading" << d->fileName;
141 if (!openRead(d->fileName))
142 return false;
143 } else
144 return false;
145
146 d->isOpen = true;
147 return true;
148}
#define debugStore
Definition StoreDebug.h:15
virtual bool openWrite(const QString &name)=0
virtual bool openRead(const QString &name)=0

References debugStore, errorStore, openRead(), openWrite(), Read, warnStore, and Write.

◆ openRead()

virtual bool KoStore::openRead ( const QString & name)
protectedpure virtual

Open the file name in the store, for reading. On success, this method must set m_stream to a stream from which we can read, as well as setting m_iSize to the size of the file.

Parameters
name"absolute path" (in the archive) to the file to open
Returns
true on success

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ openWrite()

virtual bool KoStore::openWrite ( const QString & name)
protectedpure virtual

Open the file name in the store, for writing On success, this method must set m_stream to a stream in which we can write.

Parameters
name"absolute path" (in the archive) to the file to open
Returns
true on success

Implemented in KoDirectoryStore, and KoQuaZipStore.

◆ operator=()

KoStore & KoStore::operator= ( const KoStore & store)
private

don't assign

◆ popDirectory()

void KoStore::popDirectory ( )

Restores the previously pushed directory. No-op if the stack is empty.

Definition at line 300 of file KoStore.cpp.

301{
302 Q_D(KoStore);
303 d->currentPath.clear();
304 enterAbsoluteDirectory(QString());
305 enterDirectory(d->directoryStack.pop());
306}
virtual bool enterDirectory(const QString &directory)
Definition KoStore.cpp:253

References enterAbsoluteDirectory(), and enterDirectory().

◆ pos()

qint64 KoStore::pos ( ) const

Definition at line 347 of file KoStore.cpp.

348{
349 Q_D(const KoStore);
350 return d->stream->pos();
351}

◆ pushDirectory()

void KoStore::pushDirectory ( )

Stacks the current directory. Restore the current path using popDirectory .

Definition at line 294 of file KoStore.cpp.

295{
296 Q_D(KoStore);
297 d->directoryStack.push(currentPath());
298}

References currentPath().

◆ read() [1/2]

qint64 KoStore::read ( char * buffer,
qint64 length )

Read data from the currently opened file. You can also use the streams for this.

Returns
size of data read, -1 on error

Definition at line 203 of file KoStore.cpp.

204{
205 Q_D(KoStore);
206 if (!d->isOpen) {
207 errorStore << "KoStore: You must open before reading" << Qt::endl;
208 return -1;
209 }
210 if (d->mode != Read) {
211 errorStore << "KoStore: Can not read from store that is opened for writing" << Qt::endl;
212 return -1;
213 }
214
215 return d->stream->read(_buffer, _len);
216}

References errorStore, and Read.

◆ read() [2/2]

QByteArray KoStore::read ( qint64 max)

Read data from the currently opened file. You can also use the streams for this.

Definition at line 181 of file KoStore.cpp.

182{
183 Q_D(KoStore);
184 QByteArray data;
185
186 if (!d->isOpen) {
187 warnStore << "You must open before reading";
188 return data;
189 }
190 if (d->mode != Read) {
191 errorStore << "KoStore: Can not read from store that is opened for writing" << Qt::endl;
192 return data;
193 }
194
195 return d->stream->read(max);
196}

References errorStore, Read, and warnStore.

◆ seek()

bool KoStore::seek ( qint64 pos)

See QIODevice.

Definition at line 341 of file KoStore.cpp.

342{
343 Q_D(KoStore);
344 return d->stream->seek(pos);
345}

References pos().

◆ setCompressionEnabled()

void KoStore::setCompressionEnabled ( bool e)
virtual

Allow to enable or disable compression of the files. Only supported by the ZIP backend.

Reimplemented in KoQuaZipStore.

Definition at line 403 of file KoStore.cpp.

404{
405}

◆ setSubstitution()

void KoStore::setSubstitution ( const QString & name,
const QString & substitution )

When reading, in the paths in the store where name occurs, substitution is used.

Definition at line 407 of file KoStore.cpp.

408{
409 Q_D(KoStore);
410 d->substituteThis = name;
411 d->substituteWith = substitution;
412}
const char * name(StandardAction id)

◆ size()

qint64 KoStore::size ( ) const
Returns
the size of the currently opened file, -1 on error. Can be used as an argument for the read methods, for instance

Definition at line 239 of file KoStore.cpp.

240{
241 Q_D(const KoStore);
242 if (!d->isOpen) {
243 warnStore << "You must open before asking for a size";
244 return static_cast<qint64>(-1);
245 }
246 if (d->mode != Read) {
247 warnStore << "Can not get size from store that is opened for writing";
248 return static_cast<qint64>(-1);
249 }
250 return d->size;
251}

References Read, and warnStore.

◆ write() [1/2]

qint64 KoStore::write ( const char * data,
qint64 length )
virtual

Write data into the currently opened file. You can also use the streams for this.

Reimplemented in KoQuaZipStore.

Definition at line 218 of file KoStore.cpp.

219{
220 Q_D(KoStore);
221 if (_len == 0) return 0;
222
223 if (!d->isOpen) {
224 errorStore << "KoStore: You must open before writing" << Qt::endl;
225 return 0;
226 }
227 if (d->mode != Write) {
228 errorStore << "KoStore: Can not write to store that is opened for reading" << Qt::endl;
229 return 0;
230 }
231
232 int nwritten = d->stream->write(_data, _len);
233 Q_ASSERT(nwritten == (int)_len);
234 d->size += nwritten;
235
236 return nwritten;
237}

References errorStore, and Write.

◆ write() [2/2]

qint64 KoStore::write ( const QByteArray & data)

Write data into the currently opened file. You can also use the streams for this.

Definition at line 198 of file KoStore.cpp.

199{
200 return write(data.constData(), data.size()); // see below
201}
qint64 write(const QByteArray &data)
Definition KoStore.cpp:198

References write().

Member Data Documentation

◆ d_ptr

KoStorePrivate* KoStore::d_ptr
protected

Definition at line 278 of file KoStore.h.


The documentation for this class was generated from the following files: