KNewStuff

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

KDE's Doxygen guidelines are available online.