1/* This file is part of the KDE libraries
2 * Copyright (C) 2000 Waldo Bastian <bastian@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License version 2 as published by the Free Software Foundation;
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public License
14 * along with this library; see the file COPYING.LIB. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17 **/
18
19#include "kctimefactory.h"
20#include <ksycoca.h>
21#include <ksycocatype.h>
22#include <kdebug.h>
23
24#include <assert.h>
25
26static inline QString key(const QString &path, const QByteArray& resource)
27{
28 return QString::fromLatin1(resource) + QLatin1Char('|') + path;
29}
30
31void KCTimeDict::addCTime(const QString &path, const QByteArray& resource, quint32 ctime)
32{
33 assert(!path.isEmpty());
34 m_hash.insert(key(path, resource), ctime );
35}
36
37quint32 KCTimeDict::ctime(const QString &path, const QByteArray& resource) const
38{
39 return m_hash.value(key(path, resource), 0);
40}
41
42void KCTimeDict::remove(const QString &path, const QByteArray &resource)
43{
44 m_hash.remove(key(path, resource));
45}
46
47void KCTimeDict::dump() const
48{
49 kDebug() << m_hash.keys();
50}
51
52QStringList KCTimeDict::resourceList() const
53{
54 QSet<QString> resources;
55 Hash::const_iterator it = m_hash.constBegin();
56 const Hash::const_iterator end = m_hash.constEnd();
57 for ( ; it != end; ++it ) {
58 const QString key = it.key();
59 const QString res = key.left(key.indexOf('|'));
60 resources.insert(res);
61 }
62 return resources.toList();
63}
64
65void KCTimeDict::load(QDataStream &str)
66{
67 QString key;
68 quint32 ctime;
69 while(true)
70 {
71 KSycocaEntry::read(str, key);
72 str >> ctime;
73 if (key.isEmpty()) break;
74 m_hash.insert(key, ctime);
75 }
76}
77
78void KCTimeDict::save(QDataStream &str) const
79{
80 Hash::const_iterator it = m_hash.constBegin();
81 const Hash::const_iterator end = m_hash.constEnd();
82 for ( ; it != end; ++it ) {
83 str << it.key() << it.value();
84 }
85 str << QString() << (quint32) 0;
86}
87
88///////////
89
90KCTimeInfo::KCTimeInfo()
91 : KSycocaFactory( KST_CTimeInfo ), m_ctimeDict()
92{
93 if (!KSycoca::self()->isBuilding()) {
94 QDataStream* str = stream();
95 (*str) >> m_dictOffset;
96 } else {
97 m_dictOffset = 0;
98 }
99}
100
101KCTimeInfo::~KCTimeInfo()
102{
103}
104
105void
106KCTimeInfo::saveHeader(QDataStream &str)
107{
108 KSycocaFactory::saveHeader(str);
109
110 str << m_dictOffset;
111}
112
113void KCTimeInfo::save(QDataStream &str)
114{
115 KSycocaFactory::save(str);
116
117 m_dictOffset = str.device()->pos();
118 m_ctimeDict.save(str);
119 const int endOfFactoryData = str.device()->pos();
120 saveHeader(str);
121 str.device()->seek(endOfFactoryData);
122}
123
124KCTimeDict KCTimeInfo::loadDict() const
125{
126 KCTimeDict dict;
127 QDataStream* str = stream();
128 assert(str);
129 str->device()->seek(m_dictOffset);
130 dict.load(*str);
131 return dict;
132}
133