21 #include <QtCore/QFile> 22 #include <QtCore/QDir> 23 #include <QtCore/QFileInfo> 24 #include <QtCore/QXmlStreamReader> 33 Cache::Cache(
const QString &appName):
QObject(0)
35 m_kns2ComponentName = appName;
38 kDebug() <<
"Using registry file: " << registryFile;
41 QSharedPointer<Cache> Cache::getCache(
const QString &appName)
43 CacheHash::const_iterator it = s_caches->constFind(appName);
44 if ((it != s_caches->constEnd()) && !(*it).isNull()) {
45 return QSharedPointer<Cache>(*it);
48 QSharedPointer<Cache> p(
new Cache(appName));
49 s_caches->insert(appName, QWeakPointer<Cache>(p));
63 QFile f(registryFile);
64 if (!f.open(QIODevice::ReadOnly)) {
65 kWarning() <<
"The file " << registryFile <<
" could not be opened.";
70 if (!doc.setContent(&f)) {
71 kWarning() <<
"The file could not be parsed.";
75 QDomElement root = doc.documentElement();
76 if (root.tagName() !=
"hotnewstuffregistry") {
77 kWarning() <<
"The file doesn't seem to be of interest.";
81 QDomElement stuff = root.firstChildElement(
"stuff");
82 while (!stuff.isNull()) {
87 stuff = stuff.nextSiblingElement(
"stuff");
90 kDebug() <<
"Cache read... entries: " << cache.size();
93 void Cache::readKns2MetaFiles()
96 kDebug() <<
"Loading KNS2 registry of files for the component: " << m_kns2ComponentName;
98 QString realAppName = m_kns2ComponentName.split(
':')[0];
100 const QStringList
dirs = d.
findDirs(
"data",
"knewstuff2-entries.registry");
101 for (QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++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);
108 kDebug() <<
" Load from file '" + filepath +
"'.";
110 QFileInfo info(filepath);
116 QString thisAppName = QString::fromUtf8(QByteArray::fromBase64(info.baseName().toUtf8()));
120 thisAppName = thisAppName.split(
':')[0];
122 if (thisAppName != realAppName) {
126 if (!f.open(QIODevice::ReadOnly)) {
127 kWarning() <<
"The file: " << filepath <<
" could not be opened.";
132 if (!doc.setContent(&f)) {
133 kWarning() <<
"The file could not be parsed.";
136 kDebug() <<
"found entry: " << doc.toString();
138 QDomElement root = doc.documentElement();
139 if (root.tagName() !=
"ghnsinstall") {
140 kWarning() <<
"The file doesn't seem to be of interest.";
145 QDomElement stuff = root.firstChildElement(
"stuff");
150 if (e.
payload().startsWith(QLatin1String(
"http://download.kde.org/khotnewstuff"))) {
152 e.
setProviderId(QLatin1String(
"https://api.opendesktop.org/v1/"));
153 e.
setHomepage(QString(QLatin1String(
"http://opendesktop.org/content/show.php?content=") + e.
uniqueId()));
155 }
else if (e.
payload().startsWith(QLatin1String(
"http://edu.kde.org/contrib/kvtml/"))) {
157 e.
setProviderId(
"http://edu.kde.org/contrib/kvtml/kvtml.xml");
158 }
else if (e.
payload().startsWith(QLatin1String(
"http://edu.kde.org/contrib/kvtml2/"))) {
160 e.
setProviderId(
"http://edu.kde.org/contrib/kvtml2/provider41.xml");
163 kWarning() <<
"Could not load entry: " << filepath;
170 QDomDocument tmp(
"yay");
172 kDebug() <<
"new entry: " << tmp.toString();
177 if (!dir.remove(filepath)) {
178 kWarning() <<
"could not delete old kns2 .meta file: " << filepath;
180 kDebug() <<
"Migrated KNS2 entry to KNS3.";
201 kDebug() <<
"Write registry";
203 QFile f(registryFile);
204 if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
205 kWarning() <<
"Cannot write meta information to '" << registryFile <<
"'." << endl;
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);
217 QDomElement exml = entry.
entryXML();
218 root.appendChild(exml);
222 QTextStream metastream(&f);
223 metastream << doc.toByteArray();
237 kDebug() << request.
hashForRequest() <<
" add: " << entries.size() <<
" keys: " << requestCache.keys();
EntryInternal::List requestFromCache(const KNS3::Provider::SearchRequest &)
KNewStuff data entry container.
void setProviderId(const QString &id)
EntryInternal::List registryForProvider(const QString &providerId)
All entries that have been installed by a certain provider.
#define K_GLOBAL_STATIC(TYPE, NAME)
bool setEntryXML(const QDomElement &xmldata)
set the xml for the entry parses the xml and sets the private members accordingly used to deserialize...
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QDomElement entryXML() const
get the xml string for the entry
QStringList findDirs(const char *type, const QString &reldir) const
used to keep track of a search
void setSource(Source source)
The source of this entry can be Cache, Registry or Online -.
void readRegistry()
Read the installed entries (on startup)
void setHomepage(const KUrl &page)
QString providerId() const
The id of the provider this entry belongs to.
QString payload() const
Retrieve the file name of the object.
void writeRegistry()
Save the list of installed entries.
Entry::Status status() const
Retrieves the entry's status.
QString dir(const QString &fileClass)
void registerChangedEntry(const KNS3::EntryInternal &entry)
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
void setStatus(Entry::Status status)
Returns the checksum for the entry.
QHash< QString, QWeakPointer< Cache > > CacheHash
void insertRequest(const KNS3::Provider::SearchRequest &, const KNS3::EntryInternal::List &entries)
QString hashForRequest() const