KNewStuff
providerhandler.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KNewStuff2. 00003 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> 00004 Copyright (c) 2003 - 2007 Josef Spillner <spillner@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "providerhandler.h" 00021 00022 //#include <kconfig.h> 00023 #include <kdebug.h> 00024 //#include <kio/job.h> 00025 #include <kglobal.h> 00026 //#include <kmessagebox.h> 00027 #include <klocale.h> 00028 00029 #include "feed.h" 00030 00031 using namespace KNS; 00032 00033 ProviderHandler::ProviderHandler(const Provider& provider) 00034 { 00035 mValid = false; 00036 mProvider = provider; 00037 mProviderXML = serializeElement(provider); 00038 } 00039 00040 ProviderHandler::ProviderHandler(const QDomElement& providerxml) 00041 :mProviderXML(providerxml) 00042 ,mValid(false) 00043 { 00044 mProvider = deserializeElement(providerxml); 00045 } 00046 00047 bool ProviderHandler::isValid() 00048 { 00049 return mValid; 00050 } 00051 00052 QDomElement ProviderHandler::providerXML() 00053 { 00054 return mProviderXML; 00055 } 00056 00057 Provider *ProviderHandler::providerptr() 00058 { 00059 Provider *provider = new Provider(); 00060 provider->setName(mProvider.name()); 00061 //provider->setDownloadUrl(mProvider.downloadUrl()); 00062 provider->setUploadUrl(mProvider.uploadUrl()); 00063 provider->setNoUploadUrl(mProvider.noUploadUrl()); 00064 provider->setWebAccess(mProvider.webAccess()); 00065 provider->setWebService(mProvider.webService()); 00066 provider->setIcon(mProvider.icon()); 00067 00068 const QStringList feeds = mProvider.feeds(); 00069 for (QStringList::ConstIterator it = feeds.begin(); it != feeds.end(); ++it) { 00070 Feed *feed = mProvider.downloadUrlFeed((*it)); 00071 provider->addDownloadUrlFeed((*it), feed); 00072 } 00073 return provider; 00074 } 00075 00076 Provider ProviderHandler::provider() 00077 { 00078 return mProvider; 00079 } 00080 00081 QDomElement ProviderHandler::serializeElement(const Provider& provider) 00082 { 00083 QDomDocument doc; 00084 00085 QDomElement el = doc.createElement("provider"); 00086 00087 KTranslatable name = provider.name(); 00088 00089 QStringList::ConstIterator it; 00090 QDomElement e; 00091 const QStringList langs = name.languages(); 00092 for (it = langs.begin(); it != langs.end(); ++it) { 00093 e = addElement(doc, el, "title", name.translated(*it)); 00094 e.setAttribute("lang", *it); 00095 } 00096 00097 /*if(provider.downloadUrl().isValid()) 00098 { 00099 el.setAttribute("downloadurl", provider.downloadUrl().url()); 00100 }*/ 00101 if (provider.uploadUrl().isValid()) { 00102 el.setAttribute("uploadurl", provider.uploadUrl().url()); 00103 } 00104 if (provider.noUploadUrl().isValid()) { 00105 el.setAttribute("nouploadurl", provider.noUploadUrl().url()); 00106 } 00107 if (provider.webAccess().isValid()) { 00108 el.setAttribute("webaccess", provider.webAccess().url()); 00109 } 00110 if (provider.webService().isValid()) { 00111 el.setAttribute("webservice", provider.webService().url()); 00112 } 00113 if (provider.icon().isValid()) { 00114 el.setAttribute("icon", provider.icon().url()); 00115 } 00116 00117 const QStringList feeds = provider.feeds(); 00118 for (QStringList::ConstIterator it = feeds.begin(); it != feeds.end(); ++it) { 00119 Feed *feed = provider.downloadUrlFeed((*it)); 00120 if ((*it).isEmpty()) 00121 el.setAttribute("downloadurl", feed->feedUrl().url()); 00122 else 00123 el.setAttribute("downloadurl-" + (*it), feed->feedUrl().url()); 00124 } 00125 00126 mValid = true; 00127 return el; 00128 } 00129 00130 Provider ProviderHandler::deserializeElement(const QDomElement& providerxml) 00131 { 00132 Provider provider; 00133 KTranslatable name; 00134 00135 if (providerxml.tagName() != "provider") return provider; 00136 00137 QString uploadurl = providerxml.attribute("uploadurl"); 00138 QString nouploadurl = providerxml.attribute("nouploadurl"); 00139 QString webservice = providerxml.attribute("webservice"); 00140 QString webaccess = providerxml.attribute("webaccess"); 00141 //provider.setDownloadUrl(KUrl(downloadurl)); 00142 provider.setUploadUrl(KUrl(uploadurl)); 00143 provider.setNoUploadUrl(KUrl(nouploadurl)); 00144 provider.setWebService(KUrl(webservice)); 00145 provider.setWebAccess(KUrl(webaccess)); 00146 00147 QString downloadurl = providerxml.attribute("downloadurl"); 00148 QString downloadlatest = providerxml.attribute("downloadurl-latest"); 00149 QString downloadscore = providerxml.attribute("downloadurl-score"); 00150 QString downloaddownloads = providerxml.attribute("downloadurl-downloads"); 00151 00152 if (!downloadlatest.isEmpty()) { 00153 Feed *feedlatest = new Feed(); 00154 feedlatest->setName(i18nc("describes the feed of the latest posted entries", "Latest")); 00155 feedlatest->setFeedUrl(downloadlatest); 00156 provider.addDownloadUrlFeed("latest", feedlatest); 00157 } 00158 if (!downloadscore.isEmpty()) { 00159 Feed *feedscore = new Feed(); 00160 feedscore->setName(i18n("Highest Rated")); 00161 feedscore->setFeedUrl(downloadscore); 00162 provider.addDownloadUrlFeed("score", feedscore); 00163 } 00164 if (!downloaddownloads.isEmpty()) { 00165 Feed *feeddownloads = new Feed(); 00166 feeddownloads->setName(i18n("Most Downloads")); 00167 feeddownloads->setFeedUrl(downloaddownloads); 00168 provider.addDownloadUrlFeed("downloads", feeddownloads); 00169 } 00170 if (!downloadurl.isEmpty()) { 00171 Feed *feedgeneric = new Feed(); 00172 // feedgeneric->setName(i18n("Unsorted")); 00173 // Currently this is used for latest 00174 feedgeneric->setName(i18nc("describes the feed of the latest posted entries", "Latest")); 00175 feedgeneric->setFeedUrl(downloadurl); 00176 provider.addDownloadUrlFeed(QString(), feedgeneric); 00177 } 00178 00179 // FIXME: what exactly is the following condition supposed to do? 00180 // FIXME: make sure new KUrl in KDE 4 handles this right 00181 // FIXME: this depends on freedesktop.org icon naming... introduce 'desktopicon'? 00182 KUrl iconurl(providerxml.attribute("icon")); 00183 if (!iconurl.isValid()) iconurl.setPath(providerxml.attribute("icon")); 00184 provider.setIcon(iconurl); 00185 00186 QDomNode n; 00187 for (n = providerxml.firstChild(); !n.isNull(); n = n.nextSibling()) { 00188 QDomElement e = n.toElement(); 00189 if (e.tagName() == "title") { 00190 QString lang = e.attribute("lang"); 00191 name.addString(lang, e.text().trimmed()); 00192 } 00193 } 00194 00195 provider.setName(name); 00196 00197 // Validation 00198 00199 if ((provider.noUploadUrl().isValid()) && (provider.uploadUrl().isValid())) { 00200 //kWarning(550) << "ProviderHandler: both uploadurl and nouploadurl given"; 00201 return provider; 00202 } 00203 00204 if ((!provider.noUploadUrl().isValid()) && (!provider.uploadUrl().isValid())) { 00205 if (!provider.webService().isValid()) { 00206 //kWarning(550) << "ProviderHandler: neither uploadurl nor nouploadurl nor DXS given"; 00207 return provider; 00208 } 00209 } 00210 00211 // Provider is valid 00212 00213 mValid = true; 00214 return provider; 00215 } 00216 00217 // FIXME: also used in EntryHandler - make common method? 00218 QDomElement ProviderHandler::addElement(QDomDocument& doc, QDomElement& parent, 00219 const QString& tag, const QString& value) 00220 { 00221 QDomElement n = doc.createElement(tag); 00222 n.appendChild(doc.createTextNode(value)); 00223 parent.appendChild(n); 00224 00225 return n; 00226 }
KDE 4.6 API Reference