1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-only |
6 | */ |
7 | |
8 | #include "kctimefactory_p.h" |
9 | #include "sycocadebug.h" |
10 | #include <ksycoca.h> |
11 | #include <ksycocatype.h> |
12 | #include <ksycocautils_p.h> |
13 | |
14 | #include <assert.h> |
15 | |
16 | // NOTE: the storing of "resource" here is now completely useless (since everything is under GenericDataLocation) |
17 | |
18 | static inline QString key(const QString &path, const QByteArray &resource) |
19 | { |
20 | return QString::fromLatin1(ba: resource) + QLatin1Char('|') + path; |
21 | } |
22 | |
23 | void KCTimeDict::addCTime(const QString &path, const QByteArray &resource, quint32 ctime) |
24 | { |
25 | Q_ASSERT(ctime != 0); |
26 | assert(!path.isEmpty()); |
27 | m_hash.insert(key: key(path, resource), value: ctime); |
28 | } |
29 | |
30 | quint32 KCTimeDict::ctime(const QString &path, const QByteArray &resource) const |
31 | { |
32 | return m_hash.value(key: key(path, resource), defaultValue: 0); |
33 | } |
34 | |
35 | void KCTimeDict::remove(const QString &path, const QByteArray &resource) |
36 | { |
37 | m_hash.remove(key: key(path, resource)); |
38 | } |
39 | |
40 | void KCTimeDict::dump() const |
41 | { |
42 | qCDebug(SYCOCA) << m_hash.keys(); |
43 | } |
44 | |
45 | void KCTimeDict::load(QDataStream &str) |
46 | { |
47 | QString key; |
48 | quint32 ctime; |
49 | while (true) { |
50 | str >> key >> ctime; |
51 | if (key.isEmpty()) { |
52 | break; |
53 | } |
54 | m_hash.insert(key, value: ctime); |
55 | } |
56 | } |
57 | |
58 | void KCTimeDict::save(QDataStream &str) const |
59 | { |
60 | for (auto it = m_hash.cbegin(), endIt = m_hash.cend(); it != endIt; ++it) { |
61 | str << it.key() << it.value(); |
62 | } |
63 | str << QString() << quint32(0); |
64 | } |
65 | |
66 | /////////// |
67 | |
68 | KCTimeFactory::KCTimeFactory(KSycoca *db) |
69 | : KSycocaFactory(KST_CTimeInfo, db) |
70 | , m_ctimeDict() |
71 | { |
72 | if (!sycoca()->isBuilding()) { |
73 | QDataStream *str = stream(); |
74 | (*str) >> m_dictOffset; |
75 | } else { |
76 | m_dictOffset = 0; |
77 | } |
78 | } |
79 | |
80 | KCTimeFactory::~KCTimeFactory() |
81 | { |
82 | } |
83 | |
84 | void KCTimeFactory::(QDataStream &str) |
85 | { |
86 | KSycocaFactory::saveHeader(str); |
87 | |
88 | str << m_dictOffset; |
89 | } |
90 | |
91 | void KCTimeFactory::save(QDataStream &str) |
92 | { |
93 | KSycocaFactory::save(str); |
94 | |
95 | m_dictOffset = str.device()->pos(); |
96 | m_ctimeDict.save(str); |
97 | const qint64 endOfFactoryData = str.device()->pos(); |
98 | saveHeader(str); |
99 | str.device()->seek(pos: endOfFactoryData); |
100 | } |
101 | |
102 | KCTimeDict KCTimeFactory::loadDict() const |
103 | { |
104 | KCTimeDict dict; |
105 | QDataStream *str = stream(); |
106 | assert(str); |
107 | str->device()->seek(pos: m_dictOffset); |
108 | dict.load(str&: *str); |
109 | return dict; |
110 | } |
111 | |