1/*
2 This file is part of the KDE libraries
3 Copyright (C) 2008 Christian Ehrlicher <ch.ehrlicher@gmx.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KMEMFILE_H
22#define KMEMFILE_H
23
24#ifndef QT_NO_SHAREDMEMORY
25
26#include <QtCore/QIODevice>
27#include <kdecore_export.h>
28
29/**
30 * @internal
31 * Simple QIODevice for QSharedMemory to keep ksycoca cache in memory only once
32 * The first call to open() loads the file into a shm segment. Every
33 * subsequent call only attaches to this segment. When the file content changed,
34 * you have to execute KMemFile::fileContentsChanged() to update the internal
35 * structures. The next call to open() creates a new shm segment. The old one
36 * is automatically destroyed when the last process closed KMemFile.
37 */
38
39class KDECORE_EXPORT KMemFile : public QIODevice
40{
41public:
42 /**
43 * ctor
44 *
45 * @param filename the file to load into memory
46 * @param parent our parent
47 */
48 explicit KMemFile ( const QString &filename, QObject *parent = 0 );
49 /**
50 * dtor
51 */
52 virtual ~KMemFile();
53 /**
54 * closes the KMemFile
55 *
56 * @reimp
57 */
58 virtual void close ();
59 /**
60 * As KMemFile is a random access device, it returns false
61 *
62 * @reimp
63 */
64 virtual bool isSequential () const;
65 /**
66 * @reimp
67 * @param mode only QIODevice::ReadOnly is accepted
68 */
69 virtual bool open ( OpenMode mode );
70 /**
71 * Sets the current read/write position to pos
72 * @reimp
73 * @param pos the new read/write position
74 */
75 virtual bool seek ( qint64 pos );
76 /**
77 * Returns the size of the file
78 * @reimp
79 */
80 virtual qint64 size () const;
81 /**
82 * This static function updates the internal information about the file
83 * loaded into shared memory. The next time the file is opened, the file is
84 * reread from the file system.
85 */
86 static void fileContentsChanged ( const QString &filename );
87protected:
88 /** @reimp */
89 virtual qint64 readData ( char * data, qint64 maxSize );
90 /** @reimp */
91 virtual qint64 writeData ( const char * data, qint64 maxSize );
92private:
93 class Private;
94 friend class Private;
95 Private * const d;
96};
97
98#endif //QT_NO_SHAREDMEMORY
99
100#endif // KMEMFILE_H
101