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

KNewStuff

  • sources
  • kde-4.12
  • kdelibs
  • knewstuff
  • knewstuff3
  • staticxml
staticxmlprovider.cpp
Go to the documentation of this file.
1 /*
2  knewstuff3/provider.cpp
3  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4  Copyright (c) 2003 - 2007 Josef Spillner <spillner@kde.org>
5  Copyright (c) 2009 Jeremy Whiting <jpwhiting@kde.org>
6  Copyright (C) 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "staticxmlprovider.h"
23 
24 #include "core/xmlloader.h"
25 
26 #include <kdebug.h>
27 #include <klocale.h>
28 
29 #include <QtCore/QTimer>
30 
31 namespace KNS3
32 {
33 
34 StaticXmlProvider::StaticXmlProvider( )
35  : mInitialized(false)
36 {
37 }
38 
39 QString StaticXmlProvider::id() const
40 {
41  return mId;
42 }
43 
44 bool StaticXmlProvider::setProviderXML(const QDomElement & xmldata)
45 {
46  kDebug(550) << "setting provider xml";
47 
48  if (xmldata.tagName() != "provider")
49  return false;
50 
51  mUploadUrl = xmldata.attribute("uploadurl");
52  mNoUploadUrl = xmldata.attribute("nouploadurl");
53 
54  QString url = xmldata.attribute("downloadurl");
55  if (!url.isEmpty()) {
56  mDownloadUrls.insert(QString(), KUrl(url));
57  }
58 
59  url = xmldata.attribute("downloadurl-latest");
60  if (!url.isEmpty()) {
61  mDownloadUrls.insert("latest", KUrl(url));
62  }
63 
64  url = xmldata.attribute("downloadurl-score");
65  if (!url.isEmpty()) {
66  mDownloadUrls.insert("score", KUrl(url));
67  }
68 
69  url = xmldata.attribute("downloadurl-downloads");
70  if (!url.isEmpty()) {
71  mDownloadUrls.insert("downloads", KUrl(url));
72  }
73 
74  // FIXME: what exactly is the following condition supposed to do?
75  // FIXME: make sure new KUrl in KDE 4 handles this right
76  // FIXME: this depends on freedesktop.org icon naming... introduce 'desktopicon'?
77  KUrl iconurl(xmldata.attribute("icon"));
78  if (!iconurl.isValid())
79  iconurl.setPath(xmldata.attribute("icon"));
80  mIcon = iconurl;
81 
82  QDomNode n;
83  for (n = xmldata.firstChild(); !n.isNull(); n = n.nextSibling()) {
84  QDomElement e = n.toElement();
85  if (e.tagName() == "title") {
86  //QString lang = e.attribute("lang");
87  mName = e.text().trimmed();
88  kDebug() << "add name for provider ("<< this << "): " << e.text();
89  }
90  }
91 
92  // Validation
93  if ((mNoUploadUrl.isValid()) && (mUploadUrl.isValid())) {
94  kWarning(550) << "StaticXmlProvider: both uploadurl and nouploadurl given";
95  return false;
96  }
97 
98  if ((!mNoUploadUrl.isValid()) && (!mUploadUrl.isValid())) {
99  kWarning(550) << "StaticXmlProvider: neither uploadurl nor nouploadurl given";
100  return false;
101  }
102 
103  mId = mDownloadUrls[QString()].url();
104  if (mId.isEmpty()) {
105  mId = mDownloadUrls[mDownloadUrls.keys().first()].url();
106  }
107 
108  QTimer::singleShot(0, this, SLOT(slotEmitProviderInitialized()));
109 
110  return true;
111 }
112 
113 void StaticXmlProvider::slotEmitProviderInitialized()
114 {
115  mInitialized = true;
116  emit providerInitialized(this);
117 }
118 
119 bool StaticXmlProvider::isInitialized() const
120 {
121  return mInitialized;
122 }
123 
124 void StaticXmlProvider::setCachedEntries(const KNS3::EntryInternal::List& cachedEntries)
125 {
126  kDebug() << "Set cached entries " << cachedEntries.size();
127  mCachedEntries.append(cachedEntries);
128 }
129 
130 void StaticXmlProvider::loadEntries(const KNS3::Provider::SearchRequest& request)
131 {
132  mCurrentRequest = request;
133 
134  // static providers only have on page containing everything
135  if (request.page > 0) {
136  emit loadingFinished(request, EntryInternal::List());
137  return;
138  }
139 
140  if (request.sortMode == Installed) {
141  kDebug() << "Installed entries: " << mId << installedEntries().size();
142  emit loadingFinished(request, installedEntries());
143  return;
144  }
145 
146  KUrl url = downloadUrl(request.sortMode);
147  if (!url.isEmpty()) {
148  // TODO first get the entries, then filter with searchString, finally emit the finished signal...
149  // FIXME: don't creat an endless number of xmlloaders!
150  XmlLoader * loader = new XmlLoader(this);
151  connect(loader, SIGNAL(signalLoaded(QDomDocument)), SLOT(slotFeedFileLoaded(QDomDocument)));
152  connect(loader, SIGNAL(signalFailed()), SLOT(slotFeedFailed()));
153 
154  mFeedLoaders.insert(request.sortMode, loader);
155 
156  loader->load(url);
157  } else {
158  emit loadingFailed(request);
159  }
160 }
161 
162 KUrl StaticXmlProvider::downloadUrl(SortMode mode) const
163 {
164  KUrl url;
165  switch (mode) {
166  case Installed: // should just query the registry and not end up here
167  case Rating:
168  url = mDownloadUrls.value("score");
169  break;
170  case Alphabetical:
171  url = mDownloadUrls.value(QString());
172  break;
173  case Updates:
174  case Newest:
175  url = mDownloadUrls.value("latest");
176  break;
177  case Downloads:
178  url = mDownloadUrls.value("downloads");
179  break;
180  }
181  if (url.isEmpty()) {
182  url = mDownloadUrls.value(QString());
183  }
184  return url;
185 }
186 
187 void StaticXmlProvider::slotFeedFileLoaded(const QDomDocument& doc)
188 {
189  XmlLoader * loader = qobject_cast<KNS3::XmlLoader*>(sender());
190  if (!loader)
191  {
192  kWarning() << "Loader not found!";
193  emit loadingFailed(mCurrentRequest);
194  return;
195  }
196 
197  // load all the entries from the domdocument given
198  EntryInternal::List entries;
199  QDomElement element;
200 
201  element = doc.documentElement();
202  QDomElement n;
203  for (n = element.firstChildElement(); !n.isNull(); n = n.nextSiblingElement()) {
204  EntryInternal entry;
205  entry.setEntryXML(n.toElement());
206  entry.setStatus(Entry::Downloadable);
207  entry.setProviderId(mId);
208 
209  int index = mCachedEntries.indexOf(entry);
210  if (index >= 0) {
211 
212  EntryInternal cacheEntry = mCachedEntries.takeAt(index);
213  // check if updateable
214  if ((cacheEntry.status() == Entry::Installed) &&
215  ((cacheEntry.version() != entry.version()) || (cacheEntry.releaseDate() != entry.releaseDate()))) {
216  entry.setStatus(Entry::Updateable);
217  entry.setUpdateVersion(entry.version());
218  entry.setVersion(cacheEntry.version());
219  entry.setUpdateReleaseDate(entry.releaseDate());
220  entry.setReleaseDate(cacheEntry.releaseDate());
221  } else {
222  entry.setStatus(cacheEntry.status());
223  }
224  cacheEntry = entry;
225  }
226  mCachedEntries.append(entry);
227 
228  if (searchIncludesEntry(entry)) {
229  entries << entry;
230  }
231  }
232  emit loadingFinished(mCurrentRequest, entries);
233 }
234 
235 void StaticXmlProvider::slotFeedFailed()
236 {
237  emit loadingFailed(mCurrentRequest);
238 }
239 
240 bool StaticXmlProvider::searchIncludesEntry(const KNS3::EntryInternal& entry) const
241 {
242  if (mCurrentRequest.sortMode == Updates) {
243  if (entry.status() != Entry::Updateable) {
244  return false;
245  }
246  }
247 
248  if (mCurrentRequest.searchTerm.isEmpty()) {
249  return true;
250  }
251  QString search = mCurrentRequest.searchTerm;
252  if (entry.name().contains(search, Qt::CaseInsensitive) ||
253  entry.summary().contains(search, Qt::CaseInsensitive) ||
254  entry.author().name().contains(search, Qt::CaseInsensitive)
255  ) {
256  return true;
257  }
258  return false;
259 }
260 
261 void StaticXmlProvider::loadPayloadLink(const KNS3::EntryInternal& entry, int)
262 {
263  kDebug() << "Payload: " << entry.payload();
264  emit payloadLinkLoaded(entry);
265 }
266 
267 
268 EntryInternal::List StaticXmlProvider::installedEntries() const
269 {
270  EntryInternal::List entries;
271  foreach (const EntryInternal& entry, mCachedEntries) {
272  if (entry.status() == Entry::Installed || entry.status() == Entry::Updateable) {
273  entries.append(entry);
274  }
275  }
276  return entries;
277 }
278 
279 
280 }
281 
282 #include "staticxmlprovider.moc"
KNS3::StaticXmlProvider::loadPayloadLink
virtual void loadPayloadLink(const KNS3::EntryInternal &entry, int)
Definition: staticxmlprovider.cpp:261
KNS3::EntryInternal::List
QList< EntryInternal > List
Definition: entryinternal.h:57
KNS3::XmlLoader
KNewStuff xml loader.
Definition: xmlloader.h:51
KNS3::StaticXmlProvider::setCachedEntries
virtual void setCachedEntries(const KNS3::EntryInternal::List &cachedEntries)
Definition: staticxmlprovider.cpp:124
kdebug.h
KNS3::EntryInternal
KNewStuff data entry container.
Definition: entryinternal.h:54
KNS3::EntryInternal::name
QString name() const
Retrieve the name of the data object.
Definition: entryinternal.cpp:124
KNS3::Provider::Installed
Definition: knewstuff3/core/provider.h:57
KNS3::EntryInternal::payload
QString payload() const
Retrieve the file name of the object.
Definition: entryinternal.cpp:234
KNS3::Provider::Downloads
Definition: knewstuff3/core/provider.h:56
KNS3::StaticXmlProvider::loadEntries
virtual void loadEntries(const KNS3::Provider::SearchRequest &request)
load the given search and return given page
Definition: staticxmlprovider.cpp:130
KNS3::XmlLoader::load
void load(const KUrl &url)
Starts asynchronously loading the xml document from the specified URL.
Definition: xmlloader.cpp:39
KNS3::Author::name
QString name() const
Retrieve the author's name.
Definition: knewstuff3/core/author.cpp:30
QString
KNS3::Provider::loadingFailed
void loadingFailed(const KNS3::Provider::SearchRequest &)
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
xmlloader.h
KUrl
KNS3::Provider::payloadLinkLoaded
void payloadLinkLoaded(const KNS3::EntryInternal &)
KUrl::setPath
void setPath(const QString &path)
KNS3::Provider::Updates
Definition: knewstuff3/core/provider.h:58
KNS3::Provider::SearchRequest
used to keep track of a search
Definition: knewstuff3/core/provider.h:64
KNS3::Provider::Newest
Definition: knewstuff3/core/provider.h:53
KNS3::Provider::Alphabetical
Definition: knewstuff3/core/provider.h:54
KNS3::Provider::providerInitialized
void providerInitialized(KNS3::Provider *)
KNS3::StaticXmlProvider::StaticXmlProvider
StaticXmlProvider()
Constructor.
Definition: staticxmlprovider.cpp:34
KNS3::StaticXmlProvider::isInitialized
virtual bool isInitialized() const
Definition: staticxmlprovider.cpp:119
KNS3::Provider::mIcon
KUrl mIcon
Definition: knewstuff3/core/provider.h:149
KNS3::Entry::Installed
Definition: knewstuff3/entry.h:61
KNS3::EntryInternal::status
Entry::Status status() const
Retrieves the entry's status.
Definition: entryinternal.cpp:367
KNS3::Provider::Rating
Definition: knewstuff3/core/provider.h:55
KNS3::Provider::SearchRequest::searchTerm
QString searchTerm
Definition: knewstuff3/core/provider.h:66
KNS3::Entry::Downloadable
Definition: knewstuff3/entry.h:60
KNS3::StaticXmlProvider::id
virtual QString id() const
A unique Id for this provider (the url in most cases)
Definition: staticxmlprovider.cpp:39
KNS3::Provider::SearchRequest::page
int page
Definition: knewstuff3/core/provider.h:68
KNS3::Provider::loadingFinished
void loadingFinished(const KNS3::Provider::SearchRequest &, const KNS3::EntryInternal::List &) const
KNS3::EntryInternal::author
Author author() const
Retrieve the author of the object.
Definition: entryinternal.cpp:174
staticxmlprovider.h
KNS3::StaticXmlProvider::setProviderXML
virtual bool setProviderXML(const QDomElement &xmldata)
set the provider data xml, to initialize the provider
Definition: staticxmlprovider.cpp:44
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS3::Provider::mName
QString mName
Definition: knewstuff3/core/provider.h:148
KNS3::EntryInternal::summary
QString summary() const
Retrieve a short description about the object.
Definition: entryinternal.cpp:194
KNS3::Entry::Updateable
Definition: knewstuff3/entry.h:62
QList< EntryInternal >
KNS3::Provider::SearchRequest::sortMode
SortMode sortMode
Definition: knewstuff3/core/provider.h:65
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:49 by doxygen 1.8.7 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 API Reference

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

Search



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