KNewStuff
staticxmlprovider.cpp
Go to the documentation of this file.
00001 /* 00002 knewstuff3/provider.cpp 00003 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> 00004 Copyright (c) 2003 - 2007 Josef Spillner <spillner@kde.org> 00005 Copyright (c) 2009 Jeremy Whiting <jpwhiting@kde.org> 00006 Copyright (C) 2009-2010 Frederik Gladhorn <gladhorn@kde.org> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #include "staticxmlprovider.h" 00023 00024 #include "core/xmlloader.h" 00025 00026 #include <kdebug.h> 00027 #include <klocale.h> 00028 00029 #include <QtCore/QTimer> 00030 00031 namespace KNS3 00032 { 00033 00034 StaticXmlProvider::StaticXmlProvider( ) 00035 : mInitialized(false) 00036 { 00037 } 00038 00039 QString StaticXmlProvider::id() const 00040 { 00041 return mId; 00042 } 00043 00044 bool StaticXmlProvider::setProviderXML(const QDomElement & xmldata) 00045 { 00046 kDebug(550) << "setting provider xml"; 00047 00048 if (xmldata.tagName() != "provider") 00049 return false; 00050 00051 mUploadUrl = xmldata.attribute("uploadurl"); 00052 mNoUploadUrl = xmldata.attribute("nouploadurl"); 00053 00054 QString url = xmldata.attribute("downloadurl"); 00055 if (!url.isEmpty()) { 00056 mDownloadUrls.insert(QString(), KUrl(url)); 00057 } 00058 00059 url = xmldata.attribute("downloadurl-latest"); 00060 if (!url.isEmpty()) { 00061 mDownloadUrls.insert("latest", KUrl(url)); 00062 } 00063 00064 url = xmldata.attribute("downloadurl-score"); 00065 if (!url.isEmpty()) { 00066 mDownloadUrls.insert("score", KUrl(url)); 00067 } 00068 00069 url = xmldata.attribute("downloadurl-downloads"); 00070 if (!url.isEmpty()) { 00071 mDownloadUrls.insert("downloads", KUrl(url)); 00072 } 00073 00074 // FIXME: what exactly is the following condition supposed to do? 00075 // FIXME: make sure new KUrl in KDE 4 handles this right 00076 // FIXME: this depends on freedesktop.org icon naming... introduce 'desktopicon'? 00077 KUrl iconurl(xmldata.attribute("icon")); 00078 if (!iconurl.isValid()) 00079 iconurl.setPath(xmldata.attribute("icon")); 00080 mIcon = iconurl; 00081 00082 QDomNode n; 00083 for (n = xmldata.firstChild(); !n.isNull(); n = n.nextSibling()) { 00084 QDomElement e = n.toElement(); 00085 if (e.tagName() == "title") { 00086 //QString lang = e.attribute("lang"); 00087 mName = e.text().trimmed(); 00088 kDebug() << "add name for provider ("<< this << "): " << e.text(); 00089 } 00090 } 00091 00092 // Validation 00093 if ((mNoUploadUrl.isValid()) && (mUploadUrl.isValid())) { 00094 kWarning(550) << "StaticXmlProvider: both uploadurl and nouploadurl given"; 00095 return false; 00096 } 00097 00098 if ((!mNoUploadUrl.isValid()) && (!mUploadUrl.isValid())) { 00099 kWarning(550) << "StaticXmlProvider: neither uploadurl nor nouploadurl given"; 00100 return false; 00101 } 00102 00103 mId = mDownloadUrls[QString()].url(); 00104 if (mId.isEmpty()) { 00105 mId = mDownloadUrls[mDownloadUrls.keys().first()].url(); 00106 } 00107 00108 QTimer::singleShot(0, this, SLOT(slotEmitProviderInitialized())); 00109 00110 return true; 00111 } 00112 00113 void StaticXmlProvider::slotEmitProviderInitialized() 00114 { 00115 mInitialized = true; 00116 emit providerInitialized(this); 00117 } 00118 00119 bool StaticXmlProvider::isInitialized() const 00120 { 00121 return mInitialized; 00122 } 00123 00124 void StaticXmlProvider::setCachedEntries(const KNS3::EntryInternal::List& cachedEntries) 00125 { 00126 kDebug() << "Set cached entries " << cachedEntries.size(); 00127 mCachedEntries.append(cachedEntries); 00128 } 00129 00130 void StaticXmlProvider::loadEntries(const KNS3::Provider::SearchRequest& request) 00131 { 00132 mCurrentRequest = request; 00133 00134 // static providers only have on page containing everything 00135 if (request.page > 0) { 00136 emit loadingFinished(request, EntryInternal::List()); 00137 return; 00138 } 00139 00140 if (request.sortMode == Installed) { 00141 kDebug() << "Installed entries: " << mId << installedEntries().size(); 00142 emit loadingFinished(request, installedEntries()); 00143 return; 00144 } 00145 00146 KUrl url = downloadUrl(request.sortMode); 00147 if (!url.isEmpty()) { 00148 // TODO first get the entries, then filter with searchString, finally emit the finished signal... 00149 // FIXME: don't creat an endless number of xmlloaders! 00150 XmlLoader * loader = new XmlLoader(this); 00151 connect(loader, SIGNAL(signalLoaded(const QDomDocument&)), SLOT(slotFeedFileLoaded(const QDomDocument&))); 00152 connect(loader, SIGNAL(signalFailed()), SLOT(slotFeedFailed())); 00153 00154 mFeedLoaders.insert(request.sortMode, loader); 00155 00156 loader->load(url); 00157 } else { 00158 emit loadingFailed(request); 00159 } 00160 } 00161 00162 KUrl StaticXmlProvider::downloadUrl(SortMode mode) const 00163 { 00164 KUrl url; 00165 switch (mode) { 00166 case Installed: // should just query the registry and not end up here 00167 case Rating: 00168 url = mDownloadUrls.value("score"); 00169 break; 00170 case Alphabetical: 00171 url = mDownloadUrls.value(QString()); 00172 break; 00173 case Updates: 00174 case Newest: 00175 url = mDownloadUrls.value("latest"); 00176 break; 00177 case Downloads: 00178 url = mDownloadUrls.value("downloads"); 00179 break; 00180 } 00181 if (url.isEmpty()) { 00182 url = mDownloadUrls.value(QString()); 00183 } 00184 return url; 00185 } 00186 00187 void StaticXmlProvider::slotFeedFileLoaded(const QDomDocument& doc) 00188 { 00189 XmlLoader * loader = qobject_cast<KNS3::XmlLoader*>(sender()); 00190 if (!loader) 00191 { 00192 kWarning() << "Loader not found!"; 00193 emit loadingFailed(mCurrentRequest); 00194 return; 00195 } 00196 00197 // load all the entries from the domdocument given 00198 EntryInternal::List entries; 00199 QDomElement element; 00200 00201 element = doc.documentElement(); 00202 QDomElement n; 00203 for (n = element.firstChildElement(); !n.isNull(); n = n.nextSiblingElement()) { 00204 EntryInternal entry; 00205 entry.setEntryXML(n.toElement()); 00206 entry.setStatus(Entry::Downloadable); 00207 entry.setProviderId(mId); 00208 00209 int index = mCachedEntries.indexOf(entry); 00210 if (index >= 0) { 00211 00212 EntryInternal cacheEntry = mCachedEntries.takeAt(index); 00213 // check if updateable 00214 if ((cacheEntry.status() == Entry::Installed) && 00215 ((cacheEntry.version() != entry.version()) || (cacheEntry.releaseDate() != entry.releaseDate()))) { 00216 entry.setStatus(Entry::Updateable); 00217 entry.setUpdateVersion(entry.version()); 00218 entry.setVersion(cacheEntry.version()); 00219 entry.setUpdateReleaseDate(entry.releaseDate()); 00220 entry.setReleaseDate(cacheEntry.releaseDate()); 00221 } else { 00222 entry.setStatus(cacheEntry.status()); 00223 } 00224 cacheEntry = entry; 00225 } 00226 mCachedEntries.append(entry); 00227 00228 if (searchIncludesEntry(entry)) { 00229 entries << entry; 00230 } 00231 } 00232 emit loadingFinished(mCurrentRequest, entries); 00233 } 00234 00235 void StaticXmlProvider::slotFeedFailed() 00236 { 00237 emit loadingFailed(mCurrentRequest); 00238 } 00239 00240 bool StaticXmlProvider::searchIncludesEntry(const KNS3::EntryInternal& entry) const 00241 { 00242 if (mCurrentRequest.sortMode == Updates) { 00243 if (entry.status() != Entry::Updateable) { 00244 return false; 00245 } 00246 } 00247 00248 if (mCurrentRequest.searchTerm.isEmpty()) { 00249 return true; 00250 } 00251 QString search = mCurrentRequest.searchTerm; 00252 if (entry.name().contains(search, Qt::CaseInsensitive) || 00253 entry.summary().contains(search, Qt::CaseInsensitive) || 00254 entry.author().name().contains(search, Qt::CaseInsensitive) 00255 ) { 00256 return true; 00257 } 00258 return false; 00259 } 00260 00261 void StaticXmlProvider::loadPayloadLink(const KNS3::EntryInternal& entry, int) 00262 { 00263 kDebug() << "Payload: " << entry.payload(); 00264 emit payloadLinkLoaded(entry); 00265 } 00266 00267 00268 EntryInternal::List StaticXmlProvider::installedEntries() const 00269 { 00270 EntryInternal::List entries; 00271 foreach (const EntryInternal& entry, mCachedEntries) { 00272 if (entry.status() == Entry::Installed || entry.status() == Entry::Updateable) { 00273 entries.append(entry); 00274 } 00275 } 00276 return entries; 00277 } 00278 00279 00280 } 00281 00282 #include "staticxmlprovider.moc"
KDE 4.7 API Reference