File indexing completed on 2023-10-03 03:21:58
0001 /* 0002 SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "atticahelper_p.h" 0008 #include "jobs/httpjob.h" 0009 0010 #include <QImage> 0011 0012 #include <attica/accountbalance.h> 0013 #include <attica/listjob.h> 0014 #include <attica/postjob.h> 0015 0016 using namespace KNSCore; 0017 0018 AtticaHelper::AtticaHelper(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 void AtticaHelper::init() 0024 { 0025 connect(&providerManager, &Attica::ProviderManager::defaultProvidersLoaded, this, &AtticaHelper::defaultProvidersLoaded); 0026 providerManager.loadDefaultProviders(); 0027 } 0028 0029 void AtticaHelper::addProviderFile(const QUrl &file) 0030 { 0031 if (!providerManager.providerFiles().contains(file)) { 0032 // If a custom provider file is added, remove all the default ones. 0033 const auto lstUrl = providerManager.defaultProviderFiles(); 0034 for (const QUrl &url : lstUrl) { 0035 providerManager.removeProviderFileFromDefaultProviders(url); 0036 } 0037 providerManager.addProviderFile(file); 0038 } 0039 } 0040 0041 void AtticaHelper::defaultProvidersLoaded() 0042 { 0043 QStringList providers; 0044 const auto lst = providerManager.providers(); 0045 for (const Attica::Provider &p : lst) { 0046 if (p.isEnabled()) { 0047 providers.append(p.name()); 0048 } 0049 } 0050 Q_EMIT providersLoaded(providers); 0051 } 0052 0053 void AtticaHelper::setCurrentProvider(const QString &provider) 0054 { 0055 const auto lst = providerManager.providers(); 0056 for (const Attica::Provider &p : lst) { 0057 if (p.name() == provider) { 0058 currentProvider = p; 0059 break; 0060 } 0061 } 0062 } 0063 0064 Attica::Provider AtticaHelper::provider() 0065 { 0066 return currentProvider; 0067 } 0068 0069 void AtticaHelper::checkLogin(const QString &name, const QString &password) 0070 { 0071 Attica::PostJob *checkLoginJob = currentProvider.checkLogin(name, password); 0072 connect(checkLoginJob, &Attica::BaseJob::finished, this, &AtticaHelper::checkLoginFinished); 0073 checkLoginJob->start(); 0074 } 0075 0076 void AtticaHelper::checkLoginFinished(Attica::BaseJob *baseJob) 0077 { 0078 Q_EMIT loginChecked(baseJob->metadata().error() == Attica::Metadata::NoError); 0079 } 0080 0081 bool AtticaHelper::loadCredentials(QString &name, QString &password) 0082 { 0083 if (currentProvider.isValid() && currentProvider.hasCredentials()) { 0084 if (currentProvider.loadCredentials(name, password)) { 0085 m_username = name; 0086 return true; 0087 } 0088 } 0089 return false; 0090 } 0091 0092 bool AtticaHelper::saveCredentials(const QString &name, const QString &password) 0093 { 0094 return currentProvider.saveCredentials(name, password); 0095 } 0096 0097 void AtticaHelper::loadCategories(const QStringList &configuredCategories) 0098 { 0099 m_configuredCategories = configuredCategories; 0100 Attica::ListJob<Attica::Category> *job = currentProvider.requestCategories(); 0101 connect(job, &Attica::ListJob<Attica::Category>::finished, this, static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::categoriesLoaded)); 0102 job->start(); 0103 } 0104 0105 void AtticaHelper::categoriesLoaded(Attica::BaseJob *baseJob) 0106 { 0107 Attica::ListJob<Attica::Category> *listJob = static_cast<Attica::ListJob<Attica::Category> *>(baseJob); 0108 const Attica::Category::List newCategories = listJob->itemList(); 0109 0110 if (m_configuredCategories.isEmpty()) { 0111 qWarning() << "No category was set in knsrc file. Adding all categories."; 0112 for (const Attica::Category &category : newCategories) { 0113 m_validCategories.append(category); 0114 } 0115 } else { 0116 for (const Attica::Category &category : newCategories) { 0117 if (m_configuredCategories.contains(category.name())) { 0118 m_validCategories.append(category); 0119 } 0120 } 0121 } 0122 Q_EMIT categoriesLoaded(m_validCategories); 0123 } 0124 0125 void AtticaHelper::loadContentByCurrentUser() 0126 { 0127 // in case of updates we need the list of stuff that has been uploaded by the user before 0128 Attica::ListJob<Attica::Content> *userContent = currentProvider.searchContentsByPerson(m_validCategories, m_username); 0129 connect(userContent, 0130 &Attica::ListJob<Attica::Content>::finished, 0131 this, 0132 static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::contentByCurrentUserLoaded)); 0133 0134 userContent->start(); 0135 } 0136 0137 void AtticaHelper::contentByCurrentUserLoaded(Attica::BaseJob *baseJob) 0138 { 0139 Attica::ListJob<Attica::Content> *contentList = static_cast<Attica::ListJob<Attica::Content> *>(baseJob); 0140 m_userCreatedContent = contentList->itemList(); 0141 Q_EMIT contentByCurrentUserLoaded(m_userCreatedContent); 0142 } 0143 0144 void AtticaHelper::loadLicenses() 0145 { 0146 Attica::ListJob<Attica::License> *licenseJob = currentProvider.requestLicenses(); 0147 connect(licenseJob, 0148 &Attica::ListJob<Attica::License>::finished, 0149 this, 0150 static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::licensesLoaded)); 0151 licenseJob->start(); 0152 } 0153 0154 void AtticaHelper::licensesLoaded(Attica::BaseJob *baseJob) 0155 { 0156 Attica::ListJob<Attica::License> *licenseList = static_cast<Attica::ListJob<Attica::License> *>(baseJob); 0157 Q_EMIT licensesLoaded(licenseList->itemList()); 0158 } 0159 0160 void AtticaHelper::loadDetailsLink(const QString &contentId) 0161 { 0162 Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId); 0163 connect(contentJob, 0164 &Attica::ItemJob<Attica::Content>::finished, 0165 this, 0166 static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::detailsLinkLoaded)); 0167 contentJob->start(); 0168 } 0169 0170 void AtticaHelper::detailsLinkLoaded(Attica::BaseJob *baseJob) 0171 { 0172 Attica::ItemJob<Attica::Content> *contentItemJob = static_cast<Attica::ItemJob<Attica::Content> *>(baseJob); 0173 Attica::Content content = contentItemJob->result(); 0174 0175 Q_EMIT detailsLinkLoaded(content.detailpage()); 0176 } 0177 0178 void AtticaHelper::loadContent(const QString &contentId) 0179 { 0180 Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId); 0181 connect(contentJob, 0182 &Attica::ItemJob<Attica::Content>::finished, 0183 this, 0184 static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::contentLoaded)); 0185 contentJob->start(); 0186 } 0187 0188 void AtticaHelper::loadCurrency() 0189 { 0190 Attica::ItemJob<Attica::AccountBalance> *job = currentProvider.requestAccountBalance(); 0191 connect(job, 0192 &Attica::ItemJob<Attica::AccountBalance>::finished, 0193 this, 0194 static_cast<void (AtticaHelper::*)(Attica::BaseJob *)>(&AtticaHelper::currencyLoaded)); 0195 job->start(); 0196 } 0197 0198 void AtticaHelper::currencyLoaded(Attica::BaseJob *baseJob) 0199 { 0200 Attica::ItemJob<Attica::AccountBalance> *balanceJob = static_cast<Attica::ItemJob<Attica::AccountBalance> *>(baseJob); 0201 Attica::AccountBalance balance = balanceJob->result(); 0202 Q_EMIT currencyLoaded(balance.currency()); 0203 } 0204 0205 void AtticaHelper::contentLoaded(Attica::BaseJob *baseJob) 0206 { 0207 Attica::ItemJob<Attica::Content> *contentItemJob = static_cast<Attica::ItemJob<Attica::Content> *>(baseJob); 0208 0209 const Attica::Content content(contentItemJob->result()); 0210 Q_EMIT contentLoaded(content); 0211 0212 for (int previewNum = 1; previewNum <= 3; ++previewNum) { 0213 QUrl url = QUrl::fromUserInput(content.smallPreviewPicture(QString::number(previewNum))); 0214 if (!url.isEmpty()) { 0215 m_previewJob[previewNum - 1] = HTTPJob::get(url, KNSCore::NoReload, KNSCore::HideProgressInfo); 0216 connect(m_previewJob[previewNum - 1], &KJob::result, this, &AtticaHelper::slotPreviewDownload); 0217 connect(m_previewJob[previewNum - 1], &HTTPJob::data, this, &AtticaHelper::slotPreviewData); 0218 } 0219 } 0220 } 0221 0222 void AtticaHelper::slotPreviewData(KJob *job, const QByteArray &buf) 0223 { 0224 if (job == m_previewJob[0]) { 0225 m_previewBuffer[0].append(buf); 0226 } else if (job == m_previewJob[1]) { 0227 m_previewBuffer[1].append(buf); 0228 } else if (job == m_previewJob[2]) { 0229 m_previewBuffer[2].append(buf); 0230 } 0231 } 0232 0233 void AtticaHelper::slotPreviewDownload(KJob *job) 0234 { 0235 int previewNum = -1; 0236 if (job == m_previewJob[0]) { 0237 previewNum = 1; 0238 } else if (job == m_previewJob[1]) { 0239 previewNum = 2; 0240 } else if (job == m_previewJob[2]) { 0241 previewNum = 3; 0242 } 0243 Q_ASSERT(previewNum != -1); 0244 if (job->error()) { 0245 m_previewBuffer[previewNum - 1].clear(); 0246 return; 0247 } 0248 QImage image; 0249 image.loadFromData(m_previewBuffer[previewNum - 1]); 0250 m_previewBuffer[previewNum - 1].clear(); 0251 0252 Q_EMIT previewLoaded(previewNum, image); 0253 } 0254 0255 #include "moc_atticahelper_p.cpp"