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 // we have a loader, so see which sortmode it was used for 00198 QStringList::ConstIterator it; 00199 SortMode mode; 00200 00201 foreach(const SortMode &sortMode, mFeedLoaders.keys()){ 00202 if (loader == mFeedLoaders.value(sortMode)) 00203 { 00204 mode = sortMode; 00205 break; 00206 } 00207 } 00208 00209 // load all the entries from the domdocument given 00210 EntryInternal::List entries; 00211 QDomElement element; 00212 00213 element = doc.documentElement(); 00214 QDomElement n; 00215 for (n = element.firstChildElement(); !n.isNull(); n = n.nextSiblingElement()) { 00216 EntryInternal entry; 00217 entry.setEntryXML(n.toElement()); 00218 entry.setStatus(Entry::Downloadable); 00219 entry.setProviderId(mId); 00220 00221 int index = mCachedEntries.indexOf(entry); 00222 if (index >= 0) { 00223 00224 EntryInternal cacheEntry = mCachedEntries.takeAt(index); 00225 // check if updateable 00226 if ((cacheEntry.status() == Entry::Installed) && 00227 ((cacheEntry.version() != entry.version()) || (cacheEntry.releaseDate() != entry.releaseDate()))) { 00228 entry.setStatus(Entry::Updateable); 00229 entry.setUpdateVersion(entry.version()); 00230 entry.setVersion(cacheEntry.version()); 00231 entry.setUpdateReleaseDate(entry.releaseDate()); 00232 entry.setReleaseDate(cacheEntry.releaseDate()); 00233 } else { 00234 entry.setStatus(cacheEntry.status()); 00235 } 00236 cacheEntry = entry; 00237 } 00238 mCachedEntries.append(entry); 00239 00240 if (searchIncludesEntry(entry)) { 00241 entries << entry; 00242 } 00243 } 00244 emit loadingFinished(mCurrentRequest, entries); 00245 } 00246 00247 void StaticXmlProvider::slotFeedFailed() 00248 { 00249 emit loadingFailed(mCurrentRequest); 00250 } 00251 00252 bool StaticXmlProvider::searchIncludesEntry(const KNS3::EntryInternal& entry) const 00253 { 00254 if (mCurrentRequest.sortMode == Updates) { 00255 if (entry.status() != Entry::Updateable) { 00256 return false; 00257 } 00258 } 00259 00260 if (mCurrentRequest.searchTerm.isEmpty()) { 00261 return true; 00262 } 00263 QString search = mCurrentRequest.searchTerm; 00264 if (entry.name().contains(search, Qt::CaseInsensitive) || 00265 entry.summary().contains(search, Qt::CaseInsensitive) || 00266 entry.author().name().contains(search, Qt::CaseInsensitive) 00267 ) { 00268 return true; 00269 } 00270 return false; 00271 } 00272 00273 void StaticXmlProvider::loadPayloadLink(const KNS3::EntryInternal& entry, int) 00274 { 00275 kDebug() << "Payload: " << entry.payload(); 00276 emit payloadLinkLoaded(entry); 00277 } 00278 00279 00280 EntryInternal::List StaticXmlProvider::installedEntries() const 00281 { 00282 EntryInternal::List entries; 00283 foreach (const EntryInternal& entry, mCachedEntries) { 00284 if (entry.status() == Entry::Installed || entry.status() == Entry::Updateable) { 00285 entries.append(entry); 00286 } 00287 } 00288 return entries; 00289 } 00290 00291 00292 } 00293 00294 #include "staticxmlprovider.moc"
KDE 4.6 API Reference