KNewStuff

atticahelper.cpp
1 /*
2  Copyright (C) 2010 Frederik Gladhorn <[email protected]>
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_p.h"
19 #include "jobs/httpjob.h"
20 
21 #include <QImage>
22 
23 #include <attica/listjob.h>
24 #include <attica/postjob.h>
25 #include <attica/accountbalance.h>
26 
27 using namespace KNSCore;
28 
29 AtticaHelper::AtticaHelper(QObject *parent) :
30  QObject(parent)
31 {
32 }
33 
34 void AtticaHelper::init()
35 {
36  connect(&providerManager, &Attica::ProviderManager::defaultProvidersLoaded, this, &AtticaHelper::defaultProvidersLoaded);
37  providerManager.loadDefaultProviders();
38 }
39 
40 void AtticaHelper::addProviderFile(const QUrl &file)
41 {
42  if (! providerManager.providerFiles().contains(file)) {
43  // If a custom provider file is added, remove all the default ones.
44  const auto lstUrl = providerManager.defaultProviderFiles();
45  for (const QUrl &url : lstUrl) {
46  providerManager.removeProviderFileFromDefaultProviders(url);
47  }
48  providerManager.addProviderFile(file);
49  }
50 }
51 
52 void AtticaHelper::defaultProvidersLoaded()
53 {
55  const auto lst = providerManager.providers();
56  for (const Attica::Provider &p : lst) {
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  const auto lst = providerManager.providers();
67  for (const Attica::Provider &p : lst) {
68  if (p.name() == provider) {
69  currentProvider = p;
70  break;
71  }
72  }
73 }
74 
75 Attica::Provider AtticaHelper::provider()
76 {
77  return currentProvider;
78 }
79 
80 void AtticaHelper::checkLogin(const QString &name, const QString &password)
81 {
82  Attica::PostJob *checkLoginJob = currentProvider.checkLogin(name, password);
83  connect(checkLoginJob, &Attica::BaseJob::finished, this, &AtticaHelper::checkLoginFinished);
84  checkLoginJob->start();
85 }
86 
87 void AtticaHelper::checkLoginFinished(Attica::BaseJob *baseJob)
88 {
89  emit loginChecked(baseJob->metadata().error() == Attica::Metadata::NoError);
90 }
91 
92 bool AtticaHelper::loadCredentials(QString &name, QString &password)
93 {
94  if (currentProvider.isValid() && currentProvider.hasCredentials()) {
95  if (currentProvider.loadCredentials(name, password)) {
96  m_username = name;
97  return true;
98  }
99  }
100  return false;
101 }
102 
103 bool AtticaHelper::saveCredentials(const QString &name, const QString &password)
104 {
105  return currentProvider.saveCredentials(name, password);
106 }
107 
108 void AtticaHelper::loadCategories(const QStringList &configuredCategories)
109 {
110  m_configuredCategories = configuredCategories;
111  Attica::ListJob<Attica::Category> *job = currentProvider.requestCategories();
112  connect(job, &Attica::ListJob<Attica::Category>::finished,
113  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::categoriesLoaded));
114  job->start();
115 }
116 
117 void AtticaHelper::categoriesLoaded(Attica::BaseJob *baseJob)
118 {
119  Attica::ListJob<Attica::Category> *listJob = static_cast<Attica::ListJob<Attica::Category>*>(baseJob);
120  const Attica::Category::List newCategories = listJob->itemList();
121 
122  if (m_configuredCategories.isEmpty()) {
123  qWarning() << "No category was set in knsrc file. Adding all categories.";
124  for (const Attica::Category &category : newCategories) {
125  m_validCategories.append(category);
126  }
127  } else {
128  for (const Attica::Category &category : newCategories) {
129  if (m_configuredCategories.contains(category.name())) {
130  m_validCategories.append(category);
131  }
132  }
133  }
134  emit categoriesLoaded(m_validCategories);
135 }
136 
137 void AtticaHelper::loadContentByCurrentUser()
138 {
139  // in case of updates we need the list of stuff that has been uploaded by the user before
140  Attica::ListJob<Attica::Content> *userContent = currentProvider.searchContentsByPerson(m_validCategories, m_username);
141  connect(userContent, &Attica::ListJob<Attica::Content>::finished,
142  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::contentByCurrentUserLoaded));
143 
144  userContent->start();
145 }
146 
147 void AtticaHelper::contentByCurrentUserLoaded(Attica::BaseJob *baseJob)
148 {
149  Attica::ListJob<Attica::Content> *contentList = static_cast<Attica::ListJob<Attica::Content>*>(baseJob);
150  m_userCreatedContent = contentList->itemList();
151  emit contentByCurrentUserLoaded(m_userCreatedContent);
152 }
153 
154 void AtticaHelper::loadLicenses()
155 {
156  Attica::ListJob<Attica::License> *licenseJob = currentProvider.requestLicenses();
157  connect(licenseJob, &Attica::ListJob<Attica::License>::finished,
158  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::licensesLoaded));
159  licenseJob->start();
160 }
161 
162 void AtticaHelper::licensesLoaded(Attica::BaseJob *baseJob)
163 {
164  Attica::ListJob<Attica::License> *licenseList = static_cast<Attica::ListJob<Attica::License>*>(baseJob);
165  emit licensesLoaded(licenseList->itemList());
166 }
167 
168 void AtticaHelper::loadDetailsLink(const QString &contentId)
169 {
170  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
171  connect(contentJob, &Attica::ItemJob<Attica::Content>::finished,
172  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::detailsLinkLoaded));
173  contentJob->start();
174 }
175 
176 void AtticaHelper::detailsLinkLoaded(Attica::BaseJob *baseJob)
177 {
178  Attica::ItemJob<Attica::Content> *contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
179  Attica::Content content = contentItemJob->result();
180 
181  emit detailsLinkLoaded(content.detailpage());
182 }
183 
184 void AtticaHelper::loadContent(const QString &contentId)
185 {
186  Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId);
187  connect(contentJob, &Attica::ItemJob<Attica::Content>::finished,
188  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::contentLoaded));
189  contentJob->start();
190 }
191 
192 void AtticaHelper::loadCurrency()
193 {
194  Attica::ItemJob<Attica::AccountBalance> *job = currentProvider.requestAccountBalance();
195  connect(job, &Attica::ItemJob<Attica::AccountBalance>::finished,
196  this, static_cast<void(AtticaHelper::*)(Attica::BaseJob*)>(&AtticaHelper::currencyLoaded));
197  job->start();
198 }
199 
200 void AtticaHelper::currencyLoaded(Attica::BaseJob *baseJob)
201 {
202  Attica::ItemJob<Attica::AccountBalance> *balanceJob = static_cast<Attica::ItemJob<Attica::AccountBalance>* >(baseJob);
203  Attica::AccountBalance balance = balanceJob->result();
204  emit currencyLoaded(balance.currency());
205 }
206 
207 void AtticaHelper::contentLoaded(Attica::BaseJob *baseJob)
208 {
209  Attica::ItemJob<Attica::Content> *contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob);
210 
211  const Attica::Content content(contentItemJob->result());
212  emit contentLoaded(content);
213 
214  for (int previewNum = 1; previewNum <= 3; ++previewNum) {
215  QUrl url = QUrl::fromUserInput(content.smallPreviewPicture(QString::number(previewNum)));
216  if (! url.isEmpty()) {
217  m_previewJob[previewNum - 1] = HTTPJob::get(url, KNSCore::NoReload, KNSCore::HideProgressInfo);
218  connect(m_previewJob[previewNum - 1], &KJob::result, this, &AtticaHelper::slotPreviewDownload);
219  connect(m_previewJob[previewNum - 1], &HTTPJob::data, this, &AtticaHelper::slotPreviewData);
220  }
221  }
222 }
223 
224 void AtticaHelper::slotPreviewData(KJob* job, const QByteArray& buf)
225 {
226  if (job == m_previewJob[0]) {
227  m_previewBuffer[0].append(buf);
228  } else if (job == m_previewJob[1]) {
229  m_previewBuffer[1].append(buf);
230  } else if (job == m_previewJob[2]) {
231  m_previewBuffer[2].append(buf);
232  }
233 }
234 
235 void AtticaHelper::slotPreviewDownload(KJob *job)
236 {
237  int previewNum = -1;
238  if (job == m_previewJob[0]) {
239  previewNum = 1;
240  } else if (job == m_previewJob[1]) {
241  previewNum = 2;
242  } else if (job == m_previewJob[2]) {
243  previewNum = 3;
244  }
245  Q_ASSERT(previewNum != -1);
246  if (job->error()) {
247  m_previewBuffer[previewNum - 1].clear();
248  return;
249  }
250  QImage image;
251  image.loadFromData(m_previewBuffer[previewNum - 1]);
252  m_previewBuffer[previewNum - 1].clear();
253 
254  emit previewLoaded(previewNum, image);
255 }
256 
bool loadFromData(const uchar *data, int len, const char *format)
Contains the core functionality for handling interaction with NewStuff providers. ...
QUrl fromUserInput(const QString &userInput)
bool isEmpty() const const
QCA_EXPORT ProviderList providers()
QString number(int n, int base)
void append(const T &value)
void result(KJob *job)
int error() const
PostJob * checkLogin(const QString &user, const QString &password)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.