• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • sources
  • kde-4.12
  • kdelibs
  • knewstuff
  • knewstuff3
  • core
cache.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2009 Frederik Gladhorn <gladhorn@kde.org>
3  Copyright (c) 2010 Matthias Fuchs <mat69@gmx.net>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 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  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "cache.h"
20 
21 #include <QtCore/QFile>
22 #include <QtCore/QDir>
23 #include <QtCore/QFileInfo>
24 #include <QtCore/QXmlStreamReader>
25 #include <kstandarddirs.h>
26 #include <kdebug.h>
27 
28 using namespace KNS3;
29 
30 typedef QHash<QString, QWeakPointer<Cache> > CacheHash;
31 K_GLOBAL_STATIC(CacheHash, s_caches)
32 
33 Cache::Cache(const QString &appName): QObject(0)
34 {
35  m_kns2ComponentName = appName;
36 
37  registryFile = KStandardDirs::locateLocal("data", "knewstuff3/" + appName + ".knsregistry");
38  kDebug() << "Using registry file: " << registryFile;
39 }
40 
41 QSharedPointer<Cache> Cache::getCache(const QString &appName)
42 {
43  CacheHash::const_iterator it = s_caches->constFind(appName);
44  if ((it != s_caches->constEnd()) && !(*it).isNull()) {
45  return QSharedPointer<Cache>(*it);
46  }
47 
48  QSharedPointer<Cache> p(new Cache(appName));
49  s_caches->insert(appName, QWeakPointer<Cache>(p));
50 
51  return p;
52 }
53 
54 Cache::~Cache()
55 {
56 }
57 
58 void Cache::readRegistry()
59 {
60  // read KNS2 registry first to migrate it
61  readKns2MetaFiles();
62 
63  QFile f(registryFile);
64  if (!f.open(QIODevice::ReadOnly)) {
65  kWarning() << "The file " << registryFile << " could not be opened.";
66  return;
67  }
68 
69  QDomDocument doc;
70  if (!doc.setContent(&f)) {
71  kWarning() << "The file could not be parsed.";
72  return;
73  }
74 
75  QDomElement root = doc.documentElement();
76  if (root.tagName() != "hotnewstuffregistry") {
77  kWarning() << "The file doesn't seem to be of interest.";
78  return;
79  }
80 
81  QDomElement stuff = root.firstChildElement("stuff");
82  while (!stuff.isNull()) {
83  EntryInternal e;
84  e.setEntryXML(stuff);
85  e.setSource(EntryInternal::Cache);
86  cache.insert(e);
87  stuff = stuff.nextSiblingElement("stuff");
88  }
89 
90  kDebug() << "Cache read... entries: " << cache.size();
91 }
92 
93 void Cache::readKns2MetaFiles()
94 {
95  KStandardDirs d;
96  kDebug() << "Loading KNS2 registry of files for the component: " << m_kns2ComponentName;
97 
98  QString realAppName = m_kns2ComponentName.split(':')[0];
99 
100  const QStringList dirs = d.findDirs("data", "knewstuff2-entries.registry");
101  for (QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it) {
102  //kDebug() << " + Load from directory '" + (*it) + "'.";
103  QDir dir((*it));
104  const QStringList files = dir.entryList(QDir::Files | QDir::Readable);
105  for (QStringList::const_iterator fit = files.begin(); fit != files.end(); ++fit) {
106  QString filepath = (*it) + '/' + (*fit);
107 
108  kDebug() << " Load from file '" + filepath + "'.";
109 
110  QFileInfo info(filepath);
111  QFile f(filepath);
112 
113  // first see if this file is even for this app
114  // because the registry contains entries for all apps
115  // FIXMEE: should be able to do this with a filter on the entryList above probably
116  QString thisAppName = QString::fromUtf8(QByteArray::fromBase64(info.baseName().toUtf8()));
117 
118  // NOTE: the ":" needs to always coincide with the separator character used in
119  // the id(Entry*) method
120  thisAppName = thisAppName.split(':')[0];
121 
122  if (thisAppName != realAppName) {
123  continue;
124  }
125 
126  if (!f.open(QIODevice::ReadOnly)) {
127  kWarning() << "The file: " << filepath << " could not be opened.";
128  continue;
129  }
130 
131  QDomDocument doc;
132  if (!doc.setContent(&f)) {
133  kWarning() << "The file could not be parsed.";
134  return;
135  }
136  kDebug() << "found entry: " << doc.toString();
137 
138  QDomElement root = doc.documentElement();
139  if (root.tagName() != "ghnsinstall") {
140  kWarning() << "The file doesn't seem to be of interest.";
141  return;
142  }
143 
144  // The .meta files only contain one entry
145  QDomElement stuff = root.firstChildElement("stuff");
146  EntryInternal e;
147  e.setEntryXML(stuff);
148  e.setSource(EntryInternal::Cache);
149 
150  if (e.payload().startsWith(QLatin1String("http://download.kde.org/khotnewstuff"))) {
151  // This is 99% sure a opendesktop file, make it a real one.
152  e.setProviderId(QLatin1String("https://api.opendesktop.org/v1/"));
153  e.setHomepage(QString(QLatin1String("http://opendesktop.org/content/show.php?content=") + e.uniqueId()));
154 
155  } else if (e.payload().startsWith(QLatin1String("http://edu.kde.org/contrib/kvtml/"))) {
156  // kvmtl-1
157  e.setProviderId("http://edu.kde.org/contrib/kvtml/kvtml.xml");
158  } else if (e.payload().startsWith(QLatin1String("http://edu.kde.org/contrib/kvtml2/"))) {
159  // kvmtl-2
160  e.setProviderId("http://edu.kde.org/contrib/kvtml2/provider41.xml");
161  } else {
162  // we failed, skip
163  kWarning() << "Could not load entry: " << filepath;
164  continue;
165  }
166 
167  e.setStatus(Entry::Installed);
168 
169  cache.insert(e);
170  QDomDocument tmp("yay");
171  tmp.appendChild(e.entryXML());
172  kDebug() << "new entry: " << tmp.toString();
173 
174  f.close();
175 
176  QDir dir;
177  if (!dir.remove(filepath)) {
178  kWarning() << "could not delete old kns2 .meta file: " << filepath;
179  } else {
180  kDebug() << "Migrated KNS2 entry to KNS3.";
181  }
182 
183  }
184  }
185 }
186 
187 EntryInternal::List Cache::registryForProvider(const QString& providerId)
188 {
189  EntryInternal::List entries;
190  foreach (const EntryInternal& e, cache) {
191  if (e.providerId() == providerId) {
192  entries.append(e);
193  }
194  }
195  return entries;
196 }
197 
198 
199 void Cache::writeRegistry()
200 {
201  kDebug() << "Write registry";
202 
203  QFile f(registryFile);
204  if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
205  kWarning() << "Cannot write meta information to '" << registryFile << "'." << endl;
206  return;
207  }
208 
209  QDomDocument doc("khotnewstuff3");
210  doc.appendChild(doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ));
211  QDomElement root = doc.createElement("hotnewstuffregistry");
212  doc.appendChild(root);
213 
214  foreach (const EntryInternal& entry, cache) {
215  // Write the entry, unless the policy is CacheNever and the entry is not installed.
216  if (entry.status() == Entry::Installed || entry.status() == Entry::Updateable) {
217  QDomElement exml = entry.entryXML();
218  root.appendChild(exml);
219  }
220  }
221 
222  QTextStream metastream(&f);
223  metastream << doc.toByteArray();
224 
225  f.close();
226 }
227 
228 void Cache::registerChangedEntry(const KNS3::EntryInternal& entry)
229 {
230  cache.insert(entry);
231 }
232 
233 void Cache::insertRequest(const KNS3::Provider::SearchRequest& request, const KNS3::EntryInternal::List& entries)
234 {
235  // append new entries
236  requestCache[request.hashForRequest()].append(entries);
237  kDebug() << request.hashForRequest() << " add: " << entries.size() << " keys: " << requestCache.keys();
238 }
239 
240 EntryInternal::List Cache::requestFromCache(const KNS3::Provider::SearchRequest& request)
241 {
242  kDebug() << request.hashForRequest();
243  return requestCache.value(request.hashForRequest());
244 }
245 
246 #include "cache.moc"
KNS3::Cache::requestFromCache
EntryInternal::List requestFromCache(const KNS3::Provider::SearchRequest &)
Definition: cache.cpp:240
KNS3::Cache
Definition: cache.h:30
KNS3::EntryInternal::providerId
QString providerId() const
The id of the provider this entry belongs to.
Definition: entryinternal.cpp:144
kdebug.h
KNS3::EntryInternal
KNewStuff data entry container.
Definition: entryinternal.h:54
KNS3::EntryInternal::setProviderId
void setProviderId(const QString &id)
Definition: entryinternal.cpp:149
KNS3::Cache::registryForProvider
EntryInternal::List registryForProvider(const QString &providerId)
All entries that have been installed by a certain provider.
Definition: cache.cpp:187
KNS3::EntryInternal::payload
QString payload() const
Retrieve the file name of the object.
Definition: entryinternal.cpp:234
KStandardDirs::findDirs
QStringList findDirs(const char *type, const QString &reldir) const
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
dirs
KStandardDirs * dirs()
QString
QHash
KNS3::EntryInternal::setEntryXML
bool setEntryXML(const QDomElement &xmldata)
set the xml for the entry parses the xml and sets the private members accordingly used to deserialize...
Definition: entryinternal.cpp:417
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS3::Provider::SearchRequest
used to keep track of a search
Definition: knewstuff3/core/provider.h:64
KNS3::EntryInternal::setSource
void setSource(Source source)
The source of this entry can be Cache, Registry or Online -.
Definition: entryinternal.cpp:362
KNS3::Entry::Installed
Definition: knewstuff3/entry.h:61
KNS3::EntryInternal::entryXML
QDomElement entryXML() const
get the xml string for the entry
Definition: entryinternal.cpp:510
KNS3::Cache::readRegistry
void readRegistry()
Read the installed entries (on startup)
Definition: cache.cpp:58
KStandardDirs
QStringList
KNS3::EntryInternal::status
Entry::Status status() const
Retrieves the entry's status.
Definition: entryinternal.cpp:367
KNS3::Cache::getCache
static QSharedPointer< Cache > getCache(const QString &appName)
Returns an instance of a shared cache for appName That way it is made sure, that there do not exist d...
Definition: cache.cpp:41
cache.h
KNS3::EntryInternal::setHomepage
void setHomepage(const KUrl &page)
Definition: entryinternal.cpp:169
KNS3::Cache::writeRegistry
void writeRegistry()
Save the list of installed entries.
Definition: cache.cpp:199
dir
QString dir(const QString &fileClass)
KNS3::Cache::registerChangedEntry
void registerChangedEntry(const KNS3::EntryInternal &entry)
Definition: cache.cpp:228
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
kstandarddirs.h
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS3::EntryInternal::uniqueId
QString uniqueId() const
Definition: entryinternal.cpp:134
KNS3::EntryInternal::Cache
Definition: entryinternal.h:65
KNS3::EntryInternal::setStatus
void setStatus(Entry::Status status)
Returns the checksum for the entry.
Definition: entryinternal.cpp:372
KNS3::Provider::SearchRequest::hashForRequest
QString hashForRequest() const
Definition: knewstuff3/core/provider.cpp:33
CacheHash
QHash< QString, QWeakPointer< Cache > > CacheHash
Definition: cache.cpp:30
KNS3::Cache::insertRequest
void insertRequest(const KNS3::Provider::SearchRequest &, const KNS3::EntryInternal::List &entries)
Definition: cache.cpp:233
KNS3::Entry::Updateable
Definition: knewstuff3/entry.h:62
QList< EntryInternal >
KNS3::Cache::~Cache
~Cache()
Definition: cache.cpp:54
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal