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

KNewStuff

  • 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"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sun Apr 28 2013 14:28:50 by doxygen 1.8.1.1 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-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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