KNewStuff
atticahelper.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2010 Frederik Gladhorn <gladhorn@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2.1 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "atticahelper.h" 00019 00020 #include <kdebug.h> 00021 00022 #include <kio/job.h> 00023 #include <kio/scheduler.h> 00024 00025 #include <attica/listjob.h> 00026 #include <attica/postjob.h> 00027 #include <attica/accountbalance.h> 00028 00029 using namespace KNS3; 00030 00031 AtticaHelper::AtticaHelper(QObject *parent) : 00032 QObject(parent) 00033 { 00034 } 00035 00036 void AtticaHelper::init() 00037 { 00038 connect(&providerManager, SIGNAL(defaultProvidersLoaded()), this, SLOT(defaultProvidersLoaded())); 00039 providerManager.loadDefaultProviders(); 00040 } 00041 00042 void AtticaHelper::defaultProvidersLoaded() 00043 { 00044 QStringList providers; 00045 foreach(Attica::Provider p, providerManager.providers()) { 00046 if(p.isEnabled()) { 00047 providers.append(p.name()); 00048 } 00049 } 00050 emit providersLoaded(providers); 00051 } 00052 00053 void AtticaHelper::setCurrentProvider(const QString &provider) 00054 { 00055 foreach(Attica::Provider p, providerManager.providers()) { 00056 if (p.name() == provider) { 00057 currentProvider = p; 00058 break; 00059 } 00060 } 00061 } 00062 00063 Attica::Provider AtticaHelper::provider() 00064 { 00065 return currentProvider; 00066 } 00067 00068 void AtticaHelper::checkLogin(const QString &name, const QString &password) 00069 { 00070 Attica::PostJob* checkLoginJob = currentProvider.checkLogin(name, password); 00071 connect(checkLoginJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(checkLoginFinished(Attica::BaseJob*))); 00072 checkLoginJob->start(); 00073 } 00074 00075 void AtticaHelper::checkLoginFinished(Attica::BaseJob* baseJob) 00076 { 00077 emit loginChecked(baseJob->metadata().error() == Attica::Metadata::NoError); 00078 } 00079 00080 bool AtticaHelper::loadCredentials(QString &name, QString &password) 00081 { 00082 if (currentProvider.isValid() && currentProvider.hasCredentials()) { 00083 if (currentProvider.loadCredentials(name, password)) { 00084 m_username = name; 00085 return true; 00086 } 00087 } 00088 return false; 00089 } 00090 00091 bool AtticaHelper::saveCredentials(const QString& name, const QString& password) 00092 { 00093 return currentProvider.saveCredentials(name, password); 00094 } 00095 00096 void AtticaHelper::loadCategories(const QStringList &configuredCategories) 00097 { 00098 m_configuredCategories = configuredCategories; 00099 Attica::ListJob<Attica::Category>* job = currentProvider.requestCategories(); 00100 connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(categoriesLoaded(Attica::BaseJob*))); 00101 job->start(); 00102 } 00103 00104 void AtticaHelper::categoriesLoaded(Attica::BaseJob *baseJob) 00105 { 00106 Attica::ListJob<Attica::Category>* listJob = static_cast<Attica::ListJob<Attica::Category>*>(baseJob); 00107 Attica::Category::List newCategories = listJob->itemList(); 00108 00109 if (m_configuredCategories.isEmpty()) { 00110 kWarning() << "No category was set in knsrc file. Adding all categories."; 00111 Q_FOREACH(const Attica::Category& category, newCategories) { 00112 m_validCategories.append(category); 00113 } 00114 } else { 00115 Q_FOREACH(const Attica::Category& category, newCategories) { 00116 if (m_configuredCategories.contains(category.name())) { 00117 m_validCategories.append(category); 00118 } 00119 } 00120 } 00121 emit categoriesLoaded(m_validCategories); 00122 } 00123 00124 void AtticaHelper::loadContentByCurrentUser() 00125 { 00126 // in case of updates we need the list of stuff that has been uploaded by the user before 00127 Attica::ListJob<Attica::Content>* userContent = currentProvider.searchContentsByPerson(m_validCategories, m_username); 00128 connect(userContent, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentByCurrentUserLoaded(Attica::BaseJob*))); 00129 userContent->start(); 00130 } 00131 00132 void AtticaHelper::contentByCurrentUserLoaded(Attica::BaseJob *baseJob) 00133 { 00134 Attica::ListJob<Attica::Content>* contentList = static_cast<Attica::ListJob<Attica::Content>*>(baseJob); 00135 m_userCreatedContent = contentList->itemList(); 00136 emit contentByCurrentUserLoaded(m_userCreatedContent); 00137 } 00138 00139 void AtticaHelper::loadLicenses() 00140 { 00141 Attica::ListJob<Attica::License> *licenseJob = currentProvider.requestLicenses(); 00142 connect(licenseJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(licensesLoaded(Attica::BaseJob*))); 00143 licenseJob->start(); 00144 } 00145 00146 void AtticaHelper::licensesLoaded(Attica::BaseJob* baseJob) 00147 { 00148 Attica::ListJob<Attica::License>* licenseList = static_cast<Attica::ListJob<Attica::License>*>(baseJob); 00149 emit licensesLoaded(licenseList->itemList()); 00150 } 00151 00152 00153 void AtticaHelper::loadDetailsLink(const QString& contentId) 00154 { 00155 Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId); 00156 connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(detailsLinkLoaded(Attica::BaseJob*))); 00157 contentJob->start(); 00158 } 00159 00160 void AtticaHelper::detailsLinkLoaded(Attica::BaseJob* baseJob) 00161 { 00162 Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob); 00163 Attica::Content content = contentItemJob->result(); 00164 00165 emit detailsLinkLoaded(content.detailpage()); 00166 } 00167 00168 void AtticaHelper::loadContent(const QString& contentId) 00169 { 00170 Attica::ItemJob<Attica::Content> *contentJob = currentProvider.requestContent(contentId); 00171 connect(contentJob, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(contentLoaded(Attica::BaseJob*))); 00172 contentJob->start(); 00173 } 00174 00175 00176 void AtticaHelper::loadCurrency() 00177 { 00178 Attica::ItemJob<Attica::AccountBalance> *job = currentProvider.requestAccountBalance(); 00179 connect(job, SIGNAL(finished(Attica::BaseJob*)), this, SLOT(currencyLoaded(Attica::BaseJob*))); 00180 job->start(); 00181 } 00182 00183 void AtticaHelper::currencyLoaded(Attica::BaseJob *baseJob) 00184 { 00185 Attica::ItemJob<Attica::AccountBalance>* balanceJob = static_cast<Attica::ItemJob<Attica::AccountBalance>* >(baseJob); 00186 Attica::AccountBalance balance = balanceJob->result(); 00187 emit currencyLoaded(balance.currency()); 00188 } 00189 00190 void AtticaHelper::contentLoaded(Attica::BaseJob* baseJob) 00191 { 00192 Attica::ItemJob<Attica::Content>* contentItemJob = static_cast<Attica::ItemJob<Attica::Content>* >(baseJob); 00193 00194 const Attica::Content content(contentItemJob->result()); 00195 emit contentLoaded(content); 00196 00197 for (int previewNum = 1; previewNum <=3; ++previewNum) { 00198 KUrl url = content.smallPreviewPicture(QString::number(previewNum)); 00199 if (! url.isEmpty()) { 00200 m_previewJob[previewNum-1] = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); 00201 connect(m_previewJob[previewNum-1], SIGNAL(result(KJob*)), SLOT(slotPreviewDownload(KJob*))); 00202 connect(m_previewJob[previewNum-1], SIGNAL(data(KIO::Job*, const QByteArray&)), SLOT(slotPreviewData(KIO::Job*, const QByteArray&))); 00203 KIO::Scheduler::setJobPriority(m_previewJob[previewNum-1], 1); 00204 } 00205 } 00206 } 00207 00208 void AtticaHelper::slotPreviewData(KIO::Job *job, const QByteArray& buf) 00209 { 00210 if (job == m_previewJob[0]) { 00211 m_previewBuffer[0].append(buf); 00212 } else if (job == m_previewJob[1]) { 00213 m_previewBuffer[1].append(buf); 00214 } else if (job == m_previewJob[2]) { 00215 m_previewBuffer[2].append(buf); 00216 } 00217 } 00218 00219 void AtticaHelper::slotPreviewDownload(KJob *job) 00220 { 00221 int previewNum = -1; 00222 if (job == m_previewJob[0]) { 00223 previewNum = 1; 00224 } else if (job == m_previewJob[1]) { 00225 previewNum = 2; 00226 } else if (job == m_previewJob[2]) { 00227 previewNum = 3; 00228 } 00229 Q_ASSERT(previewNum != -1); 00230 if (job->error()) { 00231 m_previewBuffer[previewNum-1].clear(); 00232 return; 00233 } 00234 QImage image; 00235 image.loadFromData(m_previewBuffer[previewNum-1]); 00236 m_previewBuffer[previewNum-1].clear(); 00237 00238 emit previewLoaded(previewNum, image); 00239 } 00240 00241 #include "atticahelper.moc"
KDE 4.6 API Reference