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

KNewStuff

  • 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::defaultProvidersLoaded()
43 {
44  QStringList providers;
45  foreach(Attica::Provider p, providerManager.providers()) {
46  if(p.isEnabled()) {
47  providers.append(p.name());
48  }
49  }
50  emit providersLoaded(providers);
51 }
52 
53 void AtticaHelper::setCurrentProvider(const QString &provider)
54 {
55  foreach(Attica::Provider p, providerManager.providers()) {
56  if (p.name() == provider) {
57  currentProvider = p;
58  break;
59  }
60  }
61 }
62 
63 Attica::Provider AtticaHelper::provider()
64 {
65  return currentProvider;
66 }
67 
68 void AtticaHelper::checkLogin(const QString &name, const QString &password)
69 {
70  Attica::PostJob* checkLoginJob = currentProvider.checkLogin(name, password);
71  connect(checkLoginJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(checkLoginFinished(Attica::BaseJob*)));
72  checkLoginJob->start();
73 }
74 
75 void AtticaHelper::checkLoginFinished(Attica::BaseJob* baseJob)
76 {
77  emit loginChecked(baseJob->metadata().error() == Attica::Metadata::NoError);
78 }
79 
80 bool AtticaHelper::loadCredentials(QString &name, QString &password)
81 {
82  if (currentProvider.isValid() && currentProvider.hasCredentials()) {
83  if (currentProvider.loadCredentials(name, password)) {
84  m_username = name;
85  return true;
86  }
87  }
88  return false;
89 }
90 
91 bool AtticaHelper::saveCredentials(const QString& name, const QString& password)
92 {
93  return currentProvider.saveCredentials(name, password);
94 }
95 
96 void AtticaHelper::loadCategories(const QStringList &configuredCategories)
97 {
98  m_configuredCategories = configuredCategories;
99  Attica::ListJob<Attica::Category>* job = currentProvider.requestCategories();
100  connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(categoriesLoaded(Attica::BaseJob*)));
101  job->start();
102 }
103 
104 void AtticaHelper::categoriesLoaded(Attica::BaseJob *baseJob)
105 {
106  Attica::ListJob<Attica::Category>* listJob = static_cast<Attica::ListJob<Attica::Category>*>(baseJob);
107  Attica::Category::List newCategories = listJob->itemList();
108 
109  if (m_configuredCategories.isEmpty()) {
110  kWarning() << "No category was set in knsrc file. Adding all categories.";
111  Q_FOREACH(const Attica::Category& category, newCategories) {
112  m_validCategories.append(category);
113  }
114  } else {
115  Q_FOREACH(const Attica::Category& category, newCategories) {
116  if (m_configuredCategories.contains(category.name())) {
117  m_validCategories.append(category);
118  }
119  }
120  }
121  emit categoriesLoaded(m_validCategories);
122 }
123 
124 void AtticaHelper::loadContentByCurrentUser()
125 {
126  // in case of updates we need the list of stuff that has been uploaded by the user before
127  Attica::ListJob<Attica::Content>* userContent = currentProvider.searchContentsByPerson(m_validCategories, m_username);
128  connect(userContent, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentByCurrentUserLoaded(Attica::BaseJob*)));
129  userContent->start();
130 }
131 
132 void AtticaHelper::contentByCurrentUserLoaded(Attica::BaseJob *baseJob)
133 {
134  Attica::ListJob<Attica::Content>* contentList = static_cast<Attica::ListJob<Attica::Content>*>(baseJob);
135  m_userCreatedContent = contentList->itemList();
136  emit contentByCurrentUserLoaded(m_userCreatedContent);
137 }
138 
139 void AtticaHelper::loadLicenses()
140 {
141  Attica::ListJob<Attica::License> *licenseJob = currentProvider.requestLicenses();
142  connect(licenseJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(licensesLoaded(Attica::BaseJob*)));
143  licenseJob->start();
144 }
145 
146 void AtticaHelper::licensesLoaded(Attica::BaseJob* baseJob)
147 {
148  Attica::ListJob<Attica::License>* licenseList = static_cast<Attica::ListJob<Attica::License>*>(baseJob);
149  emit licensesLoaded(licenseList->itemList());
150 }
151 
152 
153 void AtticaHelper::loadDetailsLink(const QString& contentId)
154 {
155  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
156  connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(detailsLinkLoaded(Attica::BaseJob*)));
157  contentJob->start();
158 }
159 
160 void AtticaHelper::detailsLinkLoaded(Attica::BaseJob* baseJob)
161 {
162  Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
163  Attica::Content content = contentItemJob->result();
164 
165  emit detailsLinkLoaded(content.detailpage());
166 }
167 
168 void AtticaHelper::loadContent(const QString& contentId)
169 {
170  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
171  connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentLoaded(Attica::BaseJob*)));
172  contentJob->start();
173 }
174 
175 
176 void AtticaHelper::loadCurrency()
177 {
178  Attica::ItemJob<Attica::AccountBalance> *job = currentProvider.requestAccountBalance();
179  connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(currencyLoaded(Attica::BaseJob*)));
180  job->start();
181 }
182 
183 void AtticaHelper::currencyLoaded(Attica::BaseJob *baseJob)
184 {
185  Attica::ItemJob<Attica::AccountBalance>* balanceJob = static_cast<Attica::ItemJob<Attica::AccountBalance>* >(baseJob);
186  Attica::AccountBalance balance = balanceJob->result();
187  emit currencyLoaded(balance.currency());
188 }
189 
190 void AtticaHelper::contentLoaded(Attica::BaseJob* baseJob)
191 {
192  Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
193 
194  const Attica::Content content(contentItemJob->result());
195  emit contentLoaded(content);
196 
197  for (int previewNum = 1; previewNum <=3; ++previewNum) {
198  KUrl url = content.smallPreviewPicture(QString::number(previewNum));
199  if (! url.isEmpty()) {
200  m_previewJob[previewNum-1] = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
201  connect(m_previewJob[previewNum-1], SIGNAL(result(KJob*)), SLOT(slotPreviewDownload(KJob*)));
202  connect(m_previewJob[previewNum-1], SIGNAL(data(KIO::Job*,QByteArray)), SLOT(slotPreviewData(KIO::Job*,QByteArray)));
203  KIO::Scheduler::setJobPriority(m_previewJob[previewNum-1], 1);
204  }
205  }
206 }
207 
208 void AtticaHelper::slotPreviewData(KIO::Job *job, const QByteArray& buf)
209 {
210  if (job == m_previewJob[0]) {
211  m_previewBuffer[0].append(buf);
212  } else if (job == m_previewJob[1]) {
213  m_previewBuffer[1].append(buf);
214  } else if (job == m_previewJob[2]) {
215  m_previewBuffer[2].append(buf);
216  }
217 }
218 
219 void AtticaHelper::slotPreviewDownload(KJob *job)
220 {
221  int previewNum = -1;
222  if (job == m_previewJob[0]) {
223  previewNum = 1;
224  } else if (job == m_previewJob[1]) {
225  previewNum = 2;
226  } else if (job == m_previewJob[2]) {
227  previewNum = 3;
228  }
229  Q_ASSERT(previewNum != -1);
230  if (job->error()) {
231  m_previewBuffer[previewNum-1].clear();
232  return;
233  }
234  QImage image;
235  image.loadFromData(m_previewBuffer[previewNum-1]);
236  m_previewBuffer[previewNum-1].clear();
237 
238  emit previewLoaded(previewNum, image);
239 }
240 
241 #include "atticahelper.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Mar 20 2013 07:21:17 by doxygen 1.8.3.1 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-4.10.1 API Reference

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