• 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
  • upload
atticahelper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Frederik Gladhorn <gladhorn@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "atticahelper.h"
19 
20 #include <kdebug.h>
21 
22 #include <kio/job.h>
23 #include <kio/scheduler.h>
24 
25 #include <attica/listjob.h>
26 #include <attica/postjob.h>
27 #include <attica/accountbalance.h>
28 
29 using namespace KNS3;
30 
31 AtticaHelper::AtticaHelper(QObject *parent) :
32  QObject(parent)
33 {
34 }
35 
36 void AtticaHelper::init()
37 {
38  connect(&providerManager, SIGNAL(defaultProvidersLoaded()), this, SLOT(defaultProvidersLoaded()));
39  providerManager.loadDefaultProviders();
40 }
41 
42 void AtticaHelper::addProviderFile(const QUrl& file)
43 {
44  if (! providerManager.providerFiles().contains(file)) {
45  // If a custom provider file is added, remove all the default ones.
46  foreach ( const QUrl& url, providerManager.defaultProviderFiles() ) {
47  providerManager.removeProviderFileFromDefaultProviders(url);
48  }
49  providerManager.addProviderFile(file);
50  }
51 }
52 
53 void AtticaHelper::defaultProvidersLoaded()
54 {
55  QStringList providers;
56  foreach(Attica::Provider p, providerManager.providers()) {
57  if(p.isEnabled()) {
58  providers.append(p.name());
59  }
60  }
61  emit providersLoaded(providers);
62 }
63 
64 void AtticaHelper::setCurrentProvider(const QString &provider)
65 {
66  foreach(Attica::Provider p, providerManager.providers()) {
67  if (p.name() == provider) {
68  currentProvider = p;
69  break;
70  }
71  }
72 }
73 
74 Attica::Provider AtticaHelper::provider()
75 {
76  return currentProvider;
77 }
78 
79 void AtticaHelper::checkLogin(const QString &name, const QString &password)
80 {
81  Attica::PostJob* checkLoginJob = currentProvider.checkLogin(name, password);
82  connect(checkLoginJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(checkLoginFinished(Attica::BaseJob*)));
83  checkLoginJob->start();
84 }
85 
86 void AtticaHelper::checkLoginFinished(Attica::BaseJob* baseJob)
87 {
88  emit loginChecked(baseJob->metadata().error() == Attica::Metadata::NoError);
89 }
90 
91 bool AtticaHelper::loadCredentials(QString &name, QString &password)
92 {
93  if (currentProvider.isValid() && currentProvider.hasCredentials()) {
94  if (currentProvider.loadCredentials(name, password)) {
95  m_username = name;
96  return true;
97  }
98  }
99  return false;
100 }
101 
102 bool AtticaHelper::saveCredentials(const QString& name, const QString& password)
103 {
104  return currentProvider.saveCredentials(name, password);
105 }
106 
107 void AtticaHelper::loadCategories(const QStringList &configuredCategories)
108 {
109  m_configuredCategories = configuredCategories;
110  Attica::ListJob<Attica::Category>* job = currentProvider.requestCategories();
111  connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(categoriesLoaded(Attica::BaseJob*)));
112  job->start();
113 }
114 
115 void AtticaHelper::categoriesLoaded(Attica::BaseJob *baseJob)
116 {
117  Attica::ListJob<Attica::Category>* listJob = static_cast<Attica::ListJob<Attica::Category>*>(baseJob);
118  Attica::Category::List newCategories = listJob->itemList();
119 
120  if (m_configuredCategories.isEmpty()) {
121  kWarning() << "No category was set in knsrc file. Adding all categories.";
122  Q_FOREACH(const Attica::Category& category, newCategories) {
123  m_validCategories.append(category);
124  }
125  } else {
126  Q_FOREACH(const Attica::Category& category, newCategories) {
127  if (m_configuredCategories.contains(category.name())) {
128  m_validCategories.append(category);
129  }
130  }
131  }
132  emit categoriesLoaded(m_validCategories);
133 }
134 
135 void AtticaHelper::loadContentByCurrentUser()
136 {
137  // in case of updates we need the list of stuff that has been uploaded by the user before
138  Attica::ListJob<Attica::Content>* userContent = currentProvider.searchContentsByPerson(m_validCategories, m_username);
139  connect(userContent, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentByCurrentUserLoaded(Attica::BaseJob*)));
140  userContent->start();
141 }
142 
143 void AtticaHelper::contentByCurrentUserLoaded(Attica::BaseJob *baseJob)
144 {
145  Attica::ListJob<Attica::Content>* contentList = static_cast<Attica::ListJob<Attica::Content>*>(baseJob);
146  m_userCreatedContent = contentList->itemList();
147  emit contentByCurrentUserLoaded(m_userCreatedContent);
148 }
149 
150 void AtticaHelper::loadLicenses()
151 {
152  Attica::ListJob<Attica::License> *licenseJob = currentProvider.requestLicenses();
153  connect(licenseJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(licensesLoaded(Attica::BaseJob*)));
154  licenseJob->start();
155 }
156 
157 void AtticaHelper::licensesLoaded(Attica::BaseJob* baseJob)
158 {
159  Attica::ListJob<Attica::License>* licenseList = static_cast<Attica::ListJob<Attica::License>*>(baseJob);
160  emit licensesLoaded(licenseList->itemList());
161 }
162 
163 
164 void AtticaHelper::loadDetailsLink(const QString& contentId)
165 {
166  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
167  connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(detailsLinkLoaded(Attica::BaseJob*)));
168  contentJob->start();
169 }
170 
171 void AtticaHelper::detailsLinkLoaded(Attica::BaseJob* baseJob)
172 {
173  Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
174  Attica::Content content = contentItemJob->result();
175 
176  emit detailsLinkLoaded(content.detailpage());
177 }
178 
179 void AtticaHelper::loadContent(const QString& contentId)
180 {
181  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
182  connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentLoaded(Attica::BaseJob*)));
183  contentJob->start();
184 }
185 
186 
187 void AtticaHelper::loadCurrency()
188 {
189  Attica::ItemJob<Attica::AccountBalance> *job = currentProvider.requestAccountBalance();
190  connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(currencyLoaded(Attica::BaseJob*)));
191  job->start();
192 }
193 
194 void AtticaHelper::currencyLoaded(Attica::BaseJob *baseJob)
195 {
196  Attica::ItemJob<Attica::AccountBalance>* balanceJob = static_cast<Attica::ItemJob<Attica::AccountBalance>* >(baseJob);
197  Attica::AccountBalance balance = balanceJob->result();
198  emit currencyLoaded(balance.currency());
199 }
200 
201 void AtticaHelper::contentLoaded(Attica::BaseJob* baseJob)
202 {
203  Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
204 
205  const Attica::Content content(contentItemJob->result());
206  emit contentLoaded(content);
207 
208  for (int previewNum = 1; previewNum <=3; ++previewNum) {
209  KUrl url = content.smallPreviewPicture(QString::number(previewNum));
210  if (! url.isEmpty()) {
211  m_previewJob[previewNum-1] = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
212  connect(m_previewJob[previewNum-1], SIGNAL(result(KJob*)), SLOT(slotPreviewDownload(KJob*)));
213  connect(m_previewJob[previewNum-1], SIGNAL(data(KIO::Job*,QByteArray)), SLOT(slotPreviewData(KIO::Job*,QByteArray)));
214  KIO::Scheduler::setJobPriority(m_previewJob[previewNum-1], 1);
215  }
216  }
217 }
218 
219 void AtticaHelper::slotPreviewData(KIO::Job *job, const QByteArray& buf)
220 {
221  if (job == m_previewJob[0]) {
222  m_previewBuffer[0].append(buf);
223  } else if (job == m_previewJob[1]) {
224  m_previewBuffer[1].append(buf);
225  } else if (job == m_previewJob[2]) {
226  m_previewBuffer[2].append(buf);
227  }
228 }
229 
230 void AtticaHelper::slotPreviewDownload(KJob *job)
231 {
232  int previewNum = -1;
233  if (job == m_previewJob[0]) {
234  previewNum = 1;
235  } else if (job == m_previewJob[1]) {
236  previewNum = 2;
237  } else if (job == m_previewJob[2]) {
238  previewNum = 3;
239  }
240  Q_ASSERT(previewNum != -1);
241  if (job->error()) {
242  m_previewBuffer[previewNum-1].clear();
243  return;
244  }
245  QImage image;
246  image.loadFromData(m_previewBuffer[previewNum-1]);
247  m_previewBuffer[previewNum-1].clear();
248 
249  emit previewLoaded(previewNum, image);
250 }
251 
252 #include "atticahelper.moc"
KNS3::AtticaHelper::loadCurrency
void loadCurrency()
Definition: atticahelper.cpp:187
KNS3::AtticaHelper::addProviderFile
void addProviderFile(const QUrl &file)
Definition: atticahelper.cpp:42
kdebug.h
KNS3::AtticaHelper::detailsLinkLoaded
void detailsLinkLoaded(const QUrl &)
KIO::Scheduler::setJobPriority
static void setJobPriority(SimpleJob *job, int priority)
KNS3::AtticaHelper::licensesLoaded
void licensesLoaded(const Attica::License::List &)
KIO::HideProgressInfo
atticahelper.h
KIO::get
TransferJob * get(const KUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KNS3::AtticaHelper::loadContent
void loadContent(const QString &contentId)
Definition: atticahelper.cpp:179
QUrl
QString
KNS3::AtticaHelper::AtticaHelper
AtticaHelper(QObject *parent=0)
Definition: atticahelper.cpp:31
QObject
KUrl
scheduler.h
QStringList
KNS3::AtticaHelper::saveCredentials
bool saveCredentials(const QString &name, const QString &password)
Definition: atticahelper.cpp:102
KNS3::AtticaHelper::loadDetailsLink
void loadDetailsLink(const QString &contentId)
Definition: atticahelper.cpp:164
KNS3::AtticaHelper::loadCredentials
bool loadCredentials(QString &name, QString &password)
Definition: atticahelper.cpp:91
KNS3::AtticaHelper::previewLoaded
void previewLoaded(int index, const QImage &image)
KNS3::AtticaHelper::loginChecked
void loginChecked(bool)
KNS3::AtticaHelper::init
void init()
Definition: atticahelper.cpp:36
KNS3::AtticaHelper::provider
Attica::Provider provider()
Definition: atticahelper.cpp:74
KNS3::AtticaHelper::loadContentByCurrentUser
void loadContentByCurrentUser()
Definition: atticahelper.cpp:135
KNS3::AtticaHelper::checkLogin
void checkLogin(const QString &name, const QString &password)
Definition: atticahelper.cpp:79
job.h
KNS3::AtticaHelper::categoriesLoaded
void categoriesLoaded(Attica::Category::List)
KNS3::AtticaHelper::setCurrentProvider
void setCurrentProvider(const QString &provider)
Definition: atticahelper.cpp:64
KNS3::AtticaHelper::currencyLoaded
void currencyLoaded(const QString &)
KIO::Job
KNS3::AtticaHelper::loadCategories
void loadCategories(const QStringList &configuredCategories)
Definition: atticahelper.cpp:107
KIO::NoReload
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS3::AtticaHelper::providersLoaded
void providersLoaded(const QStringList &)
KNS3::AtticaHelper::contentLoaded
void contentLoaded(const Attica::Content &)
KJob
KNS3::AtticaHelper::loadLicenses
void loadLicenses()
Definition: atticahelper.cpp:150
KNS3::AtticaHelper::contentByCurrentUserLoaded
void contentByCurrentUserLoaded(const Attica::Content::List &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:48 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