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

KNewStuff

uploaddialog.cpp

Go to the documentation of this file.
00001 /*
00002     knewstuff3/ui/uploaddialog.cpp.
00003     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (c) 2009 Jeremy Whiting <jpwhiting@kde.org>
00005     Copyright (C) 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "uploaddialog.h"
00022 #include "uploaddialog_p.h"
00023 
00024 #include <QtGui/QLabel>
00025 #include <QtGui/QLayout>
00026 #include <QtGui/QDoubleSpinBox>
00027 #include <QtCore/QString>
00028 #include <QtCore/QSignalMapper>
00029 
00030 #include <kaboutdata.h>
00031 #include <kcomponentdata.h>
00032 #include <kfiledialog.h>
00033 #include <kmessagebox.h>
00034 #include <kstandarddirs.h>
00035 #include <kpixmapsequence.h>
00036 #include <kpixmapsequencewidget.h>
00037 
00038 #include <kdebug.h>
00039 #include <kconfiggroup.h>
00040 
00041 using namespace KNS3;
00042 
00043 void UploadDialog::Private::init()
00044 {
00045     QWidget* _mainWidget = new QWidget(q);
00046     q->setMainWidget(_mainWidget);
00047     ui.setupUi(_mainWidget);
00048 
00049     atticaHelper = new AtticaHelper(q);
00050     q->connect(atticaHelper, SIGNAL(providersLoaded(QStringList)), q, SLOT(_k_providersLoaded(QStringList)));
00051     q->connect(atticaHelper, SIGNAL(loginChecked(bool)), q, SLOT(_k_checkCredentialsFinished(bool)));
00052     q->connect(atticaHelper, SIGNAL(licensesLoaded(Attica::License::List)), q, SLOT(_k_licensesLoaded(Attica::License::List)));
00053     q->connect(atticaHelper, SIGNAL(categoriesLoaded(Attica::Category::List)), q, SLOT(_k_categoriesLoaded(Attica::Category::List)));
00054     q->connect(atticaHelper, SIGNAL(contentByCurrentUserLoaded(Attica::Content::List)), q, SLOT(_k_contentByCurrentUserLoaded(Attica::Content::List)));
00055     q->connect(atticaHelper, SIGNAL(contentLoaded(Attica::Content)), q, SLOT(_k_updatedContentFetched(Attica::Content)));
00056     q->connect(atticaHelper, SIGNAL(detailsLinkLoaded(QUrl)), q, SLOT(_k_detailsLinkLoaded(QUrl)));
00057     q->connect(atticaHelper, SIGNAL(currencyLoaded(QString)), q, SLOT(_k_currencyLoaded(QString)));
00058     q->connect(atticaHelper, SIGNAL(previewLoaded(int, QImage)), q, SLOT(_k_previewLoaded(int, QImage)));
00059     atticaHelper->init();
00060 
00061     q->connect(ui.changePreview1Button, SIGNAL(clicked()), q, SLOT(_k_changePreview1()));
00062     q->connect(ui.changePreview2Button, SIGNAL(clicked()), q, SLOT(_k_changePreview2()));
00063     q->connect(ui.changePreview3Button, SIGNAL(clicked()), q, SLOT(_k_changePreview3()));
00064 
00065     q->connect(ui.providerComboBox, SIGNAL(currentIndexChanged(QString)), q, SLOT(_k_providerChanged(QString)));
00066     q->connect(ui.radioUpdate, SIGNAL(toggled(bool)), q, SLOT(_k_updateContentsToggled(bool)));
00067 
00068     //Busy widget
00069     busyWidget = new KPixmapSequenceWidget();
00070     busyWidget->setSequence(KPixmapSequence("process-working", 22));
00071     busyWidget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00072     ui.busyWidget->setLayout(new QHBoxLayout());
00073     ui.busyWidget->layout()->addWidget(busyWidget);
00074     busyWidget->setVisible(false);
00075 
00076     //ui.previewImage1->showPreview(KUrl("invalid"));
00077     //ui.previewImage2->showPreview(KUrl("invalid"));
00078     //ui.previewImage3->showPreview(KUrl("invalid"));
00079 }
00080 
00081 void UploadDialog::Private::setBusy(const QString& message)
00082 {
00083     ui.busyLabel->setText(message);
00084     busyWidget->setVisible(true);
00085 }
00086 
00087 void UploadDialog::Private::setIdle(const QString& message)
00088 {
00089     ui.busyLabel->setText(message);
00090     busyWidget->setVisible(false);
00091 }
00092 
00093 void UploadDialog::Private::_k_showPage(int page)
00094 {
00095     ui.stackedWidget->setCurrentIndex(page);
00096     setIdle(QString());
00097 
00098     switch (ui.stackedWidget->currentIndex()) {
00099     case UserPasswordPage:
00100         ui.username->setFocus();
00101         // TODO 4.6 enable new string: setBusy(i18n("Fetching provider information..."));
00102         break;
00103 
00104     case FileNewUpdatePage:
00105         atticaHelper->loadLicenses();
00106         atticaHelper->loadCurrency();
00107         ui.uploadButton->setFocus();
00108         setBusy(i18n("Fetching license data from server..."));
00109         break;
00110 
00111     case Details1Page:
00112         if (ui.radioUpdate->isChecked()) {
00113             // Fetch
00114             atticaHelper->loadContent(ui.userContentList->currentItem()->data(Qt::UserRole).toString());
00115             setBusy(i18n("Fetching content data from server..."));
00116         }
00117 
00118         ui.mNameEdit->setFocus();
00119         break;
00120 
00121     case UploadFinalPage:
00122         if (previewFile1.isEmpty()) {
00123             ui.uploadPreview1ImageLabel->setVisible(false);
00124             ui.uploadPreview1Label->setVisible(false);
00125         }
00126         if (previewFile2.isEmpty()) {
00127             ui.uploadPreview2ImageLabel->setVisible(false);
00128             ui.uploadPreview2Label->setVisible(false);
00129         }
00130         if (previewFile3.isEmpty()) {
00131             ui.uploadPreview3ImageLabel->setVisible(false);
00132             ui.uploadPreview3Label->setVisible(false);
00133         }
00134         break;
00135     }
00136 
00137     _k_updatePage();
00138 }
00139 
00140 void UploadDialog::Private::_k_updatePage()
00141 {
00142     bool firstPage = ui.stackedWidget->currentIndex() == 0;
00143     q->enableButton(BackButton, !firstPage && !finished);
00144 
00145     bool nextEnabled = false;
00146     switch (ui.stackedWidget->currentIndex()) {
00147     case UserPasswordPage:
00148         if (ui.providerComboBox->count() > 0 && !ui.username->text().isEmpty() && !ui.password->text().isEmpty()) {
00149             nextEnabled = true;
00150         }
00151         break;
00152 
00153     case FileNewUpdatePage:
00154         // FIXME: check if the file requester contains a valid file
00155         if (!uploadFile.isEmpty() || ui.uploadFileRequester->url().isLocalFile()) {
00156             if (ui.radioNewUpload->isChecked() || ui.userContentList->currentRow() >= 0) {
00157                 nextEnabled = true;
00158             }
00159         }
00160         break;
00161 
00162     case Details1Page:
00163         if (!ui.mNameEdit->text().isEmpty()) {
00164             nextEnabled = true;
00165         }
00166         break;
00167 
00168     case Details2Page:
00169         nextEnabled = true;
00170         break;
00171 
00172     case UploadFinalPage:
00173         break;
00174     }
00175 
00176     q->enableButton(NextButton, nextEnabled);
00177     q->enableButton(FinishButton, finished);
00178 
00179     q->setDefaultButton(nextEnabled ? NextButton : FinishButton);
00180 
00181     if (nextEnabled && q->button(KDialog::Cancel)->hasFocus()) {
00182         q->button(NextButton)->setFocus();
00183     }
00184 }
00185 
00186 void UploadDialog::Private::_k_providersLoaded(const QStringList& providers)
00187 {
00188     if (providers.size() == 0) {
00189         // TODO 4.6 enable new string: setIdle(i18n("Could not fetch provider information."));
00190         ui.stackedWidget->setEnabled(false);
00191         kWarning() << "Could not load providers.";
00192         return;
00193     }
00194     setIdle(QString());
00195     ui.providerComboBox->addItems(providers);
00196     ui.providerComboBox->setCurrentIndex(0);
00197     atticaHelper->setCurrentProvider(providers.at(0));
00198 
00199     QString user;
00200     QString pass;
00201     if (atticaHelper->loadCredentials(user, pass)) {
00202         ui.username->setText(user);
00203         ui.password->setText(pass);
00204     }
00205     _k_updatePage();
00206 }
00207 
00208 void UploadDialog::Private::_k_providerChanged(const QString& providerName)
00209 {
00210     atticaHelper->setCurrentProvider(providerName);
00211     ui.username->clear();
00212     ui.password->clear();
00213     QString user;
00214     QString pass;
00215     if (atticaHelper->loadCredentials(user, pass)) {
00216         ui.username->setText(user);
00217         ui.password->setText(pass);
00218     }
00219     _k_updatePage();
00220 }
00221 
00222 void UploadDialog::Private::_k_backPage()
00223 {
00224     _k_showPage(ui.stackedWidget->currentIndex()-1);
00225 }
00226 
00227 void UploadDialog::Private::_k_nextPage()
00228 {
00229     // TODO: validate credentials after user name/password have been entered
00230     if (ui.stackedWidget->currentIndex() == UserPasswordPage) {
00231         setBusy(i18n("Checking login..."));
00232         q->button(NextButton)->setEnabled(false);
00233         ui.providerComboBox->setEnabled(false);
00234         ui.username->setEnabled(false);
00235         ui.password->setEnabled(false);
00236         atticaHelper->checkLogin(ui.username->text(), ui.password->text());
00237     } else {
00238         _k_showPage(ui.stackedWidget->currentIndex()+1);
00239     }
00240 }
00241 
00242 void UploadDialog::Private::_k_checkCredentialsFinished(bool success)
00243 {
00244     ui.providerComboBox->setEnabled(true);
00245     ui.username->setEnabled(true);
00246     ui.password->setEnabled(true);
00247 
00248     if (success) {
00249         atticaHelper->saveCredentials(ui.username->text(), ui.password->text());
00250         _k_showPage(FileNewUpdatePage);
00251 
00252         atticaHelper->loadCategories(categoryNames);
00253         setBusy(i18n("Fetching your previously updated content..."));
00254     } else {
00255         // TODO check what the actual error is
00256         setIdle(i18n("Could not verify login, please try again."));
00257     }
00258 }
00259 
00260 void UploadDialog::Private::_k_licensesLoaded(const Attica::License::List& licenses)
00261 {
00262     ui.mLicenseCombo->clear();
00263     foreach (Attica::License license, licenses) {
00264         ui.mLicenseCombo->addItem(license.name(), license.id());
00265     }
00266 }
00267 
00268 void UploadDialog::Private::_k_currencyLoaded(const QString& currency)
00269 {
00270     ui.priceCurrency->setText(currency);
00271 }
00272 
00273 void UploadDialog::Private::_k_contentByCurrentUserLoaded(const Attica::Content::List& contentList)
00274 {
00275     setIdle(i18n("Fetching your previously updated content finished."));
00276 
00277     foreach(Attica::Content content, contentList) {
00278         QListWidgetItem *contentItem = new QListWidgetItem(content.name());
00279         contentItem->setData(Qt::UserRole, content.id());
00280         ui.userContentList->addItem(contentItem);
00281     }
00282 
00283     if (ui.userContentList->count() > 0) {
00284         ui.userContentList->setCurrentRow(0);
00285         ui.radioUpdate->setEnabled(true);
00286         _k_updatePage();
00287     }
00288 
00289 }
00290 
00291 void UploadDialog::Private::_k_updatedContentFetched(const Attica::Content& content)
00292 {
00293     setIdle(i18n("Fetching content data from server finished."));
00294 
00295     contentId = content.id();
00296     // fill in ui
00297     ui.mNameEdit->setText(content.name());
00298     ui.mSummaryEdit->setText(content.description());
00299     ui.mVersionEdit->setText(content.version());
00300     ui.changelog->setText(content.changelog());
00301     ui.priceCheckBox->setChecked(content.attribute("downloadbuy1") == "1");
00302     ui.priceSpinBox->setValue(content.attribute("downloadbuyprice1").toDouble());
00303     ui.priceReasonLineEdit->setText(content.attribute("downloadbuyreason1"));
00304 
00305     bool conversionOk = false;
00306     int licenseNumber = content.license().toInt(&conversionOk);
00307     if (conversionOk) {
00308         // check if that int is in list
00309         int row = ui.mLicenseCombo->findData(licenseNumber, Qt::UserRole);
00310         ui.mLicenseCombo->setCurrentIndex(row);
00311     } else {
00312         ui.mLicenseCombo->setEditText(content.license());
00313     }
00314 
00315     ui.contentWebsiteLink->setText(QLatin1String("<a href=\"") + content.detailpage().toString() + QLatin1String("\">")
00316                                        + i18nc("A link to the website where the get hot new stuff upload can be seen", "Visit website") + QLatin1String("</a>"));
00317     ui.fetchContentLinkImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00318 }
00319 
00320 void UploadDialog::Private::_k_previewLoaded(int index, const QImage& image)
00321 {
00322     switch (index) {
00323     case 1:
00324         ui.previewImage1->setPixmap(QPixmap::fromImage(image));
00325         break;
00326     case 2:
00327         ui.previewImage2->setPixmap(QPixmap::fromImage(image));
00328         break;
00329     case 3:
00330         ui.previewImage3->setPixmap(QPixmap::fromImage(image));
00331         break;
00332     }
00333 }
00334 
00335 void UploadDialog::Private::_k_updateContentsToggled(bool update)
00336 {
00337     ui.userContentList->setEnabled(update);
00338 }
00339 
00340 UploadDialog::UploadDialog(QWidget *parent)
00341     : KDialog(parent), d(new Private(this))
00342 {
00343     KComponentData component = KGlobal::activeComponent();
00344     QString name = component.componentName();
00345     init(name + ".knsrc");
00346 }
00347 
00348 UploadDialog::UploadDialog(const QString& configFile, QWidget *parent)
00349     : KDialog(parent), d(new Private(this))
00350 {
00351     init(configFile);
00352 }
00353 
00354 UploadDialog::~UploadDialog()
00355 {
00356     delete d;
00357 }
00358 
00359 bool UploadDialog::init(const QString &configfile)
00360 {
00361     d->init();
00362 
00363     setCaption(i18n("Share Hot New Stuff"));
00364 
00365     setButtons(KDialog::Cancel | KDialog::User1 | KDialog::User2 | KDialog::User3 | KDialog::Help);
00366     setButtonGuiItem( BackButton, KStandardGuiItem::back(KStandardGuiItem::UseRTL) );
00367 
00368     setButtonText( NextButton, i18nc("Opposite to Back", "Next") );
00369     setButtonIcon( NextButton, KStandardGuiItem::forward(KStandardGuiItem::UseRTL).icon() );
00370     setButtonText(FinishButton, i18n("Finish"));
00371     setButtonIcon( FinishButton, KIcon("dialog-ok-apply") );
00372     setDefaultButton(NextButton);
00373     d->_k_updatePage();
00374 
00375     connect(d->ui.username, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
00376 
00377     connect(d->ui.password, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
00378     connect(d->ui.mNameEdit, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
00379     connect(d->ui.uploadFileRequester, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
00380     connect(d->ui.priceCheckBox, SIGNAL(toggled(bool)), this, SLOT(_k_priceToggled(bool)));
00381 
00382     connect(d->ui.uploadButton, SIGNAL(clicked()), this, SLOT(_k_startUpload()));
00383 
00384     connect(this, SIGNAL(user3Clicked()), this, SLOT(_k_backPage()));
00385     connect(this, SIGNAL(user2Clicked()), this, SLOT(_k_nextPage()));
00386     connect(this, SIGNAL(user1Clicked()), this, SLOT(accept()));
00387 
00388     d->ui.mTitleWidget->setText(i18nc("Program name followed by 'Add On Uploader'",
00389                                  "%1 Add-On Uploader",
00390                                  KGlobal::activeComponent().aboutData()->programName()));
00391     d->ui.mTitleWidget->setPixmap(KIcon(KGlobal::activeComponent().aboutData()->programIconName()));
00392 
00393     KConfig conf(configfile);
00394     if (conf.accessMode() == KConfig::NoAccess) {
00395         kError() << "No knsrc file named '" << configfile << "' was found." << endl;
00396         return false;
00397     }
00398     // FIXME: accessMode() doesn't return NoAccess for non-existing files
00399     // - bug in kdecore?
00400     // - this needs to be looked at again until KConfig backend changes for KDE 4
00401     // the check below is a workaround
00402     if (KStandardDirs::locate("config", configfile).isEmpty()) {
00403         kError() << "No knsrc file named '" << configfile << "' was found." << endl;
00404         return false;
00405     }
00406 
00407     KConfigGroup group;
00408     if (conf.hasGroup("KNewStuff3")) {
00409         kDebug() << "Loading KNewStuff3 config: " << configfile;
00410         group = conf.group("KNewStuff3");
00411     } else {
00412         kError() << "A knsrc file was found but it doesn't contain a KNewStuff3 section." << endl;
00413         return false;
00414     }
00415 
00416     d->categoryNames = group.readEntry("UploadCategories", QStringList());
00417     // fall back to download categories
00418     if (d->categoryNames.isEmpty()) {
00419         d->categoryNames = group.readEntry("Categories", QStringList());
00420     }
00421 
00422     d->ui.mCategoryCombo->addItems(d->categoryNames);
00423 
00424     if (d->categoryNames.size() == 1) {
00425         d->ui.mCategoryLabel->setVisible(false);
00426         d->ui.mCategoryCombo->setVisible(false);
00427     }
00428 
00429     kDebug() << "Categories: " << d->categoryNames;
00430 
00431     d->_k_showPage(0);
00432 
00433     return true;
00434 }
00435 
00436 void UploadDialog::setUploadFile(const KUrl& payloadFile)
00437 {
00438     d->uploadFile = payloadFile;
00439 
00440     d->ui.uploadFileLabel->setVisible(false);
00441     d->ui.uploadFileRequester->setVisible(false);
00442 
00443     QFile file(d->uploadFile.toLocalFile());
00444     if (!file.open(QIODevice::ReadOnly)) {
00445         KMessageBox::error(this, i18n("File not found: %1", d->uploadFile.url()), i18n("Upload Failed"));
00446     }
00447 }
00448 
00449 void UploadDialog::setUploadName(const QString& name)
00450 {
00451     d->ui.mNameEdit->setText(name);
00452 }
00453 
00454 void UploadDialog::selectCategory(const QString& category)
00455 {
00456     d->ui.mCategoryCombo->setCurrentIndex(d->ui.mCategoryCombo->findText(category, Qt::MatchFixedString));
00457 }
00458 
00459 void UploadDialog::setChangelog(const QString& changelog)
00460 {
00461     d->ui.changelog->setText(changelog);
00462 }
00463 
00464 void UploadDialog::setDescription(const QString& description)
00465 {
00466     d->ui.mSummaryEdit->setText(description);
00467 }
00468 
00469 void UploadDialog::setPriceEnabled(bool enabled)
00470 {
00471     d->ui.priceCheckBox->setVisible(enabled);
00472     d->ui.priceGroupBox->setVisible(enabled);
00473 }
00474 
00475 void UploadDialog::setPrice(double price)
00476 {
00477     d->ui.priceCheckBox->setEnabled(true);
00478     d->ui.priceSpinBox->setValue(price);
00479 }
00480 
00481 void UploadDialog::setPriceReason(const QString& reason)
00482 {
00483     d->ui.priceReasonLineEdit->setText(reason);
00484 }
00485 
00486 void UploadDialog::setVersion(const QString& version)
00487 {
00488     d->ui.mVersionEdit->setText(version);
00489 }
00490 
00491 void UploadDialog::setPreviewImageFile(uint number, const KUrl& file)
00492 {
00493     QPixmap preview(file.toLocalFile());
00494     switch(number) {
00495     case 0 :
00496         d->previewFile1 = file;
00497         d->ui.previewImage1->setPixmap(preview.scaled(d->ui.previewImage1->size()));
00498         break;
00499     case 1 :
00500         d->previewFile2 = file;
00501         d->ui.previewImage2->setPixmap(preview.scaled(d->ui.previewImage2->size()));
00502         break;
00503     case 2 :
00504         d->previewFile3 = file;
00505         d->ui.previewImage3->setPixmap(preview.scaled(d->ui.previewImage3->size()));
00506         break;
00507     default :
00508         kError() << "Wrong preview image file number";
00509         break;
00510     }
00511 }
00512 
00513 void UploadDialog::Private::_k_priceToggled(bool priceEnabled)
00514 {
00515     ui.priceGroupBox->setEnabled(priceEnabled);
00516 }
00517 
00518 void UploadDialog::Private::_k_categoriesLoaded(const Attica::Category::List& loadedCategories)
00519 {
00520     categories = loadedCategories;
00521 
00522     // at least one category is needed
00523     if (categories.count() == 0) {
00524             KMessageBox::error(q,
00525                      i18np("The server does not recognize the category %2 to which you are trying to upload.",
00526                            "The server does not recognize any of the categories to which you are trying to upload: %2",
00527                            categoryNames.size(), categoryNames.join(", ")),
00528                            i18n("Error"));
00529             // close the dialog
00530             q->reject();
00531             return;
00532     }
00533     foreach(Attica::Category c, categories) {
00534         ui.mCategoryCombo->addItem(c.name(), c.id());
00535     }
00536     atticaHelper->loadContentByCurrentUser();
00537 }
00538 
00539 void UploadDialog::accept()
00540 {
00541     KDialog::accept();
00542 }
00543 
00544 void UploadDialog::Private::_k_startUpload()
00545 {
00546     // FIXME: this only works if categories are set in the .knsrc file
00547     // TODO: ask for confirmation when closing the dialog
00548 
00549     q->button(BackButton)->setEnabled(false);
00550     q->button(KDialog::Cancel)->setEnabled(false);
00551 
00552     ui.uploadButton->setEnabled(false);
00553 
00554     // idle back and forth, we need a fix in attica to get at real progress values
00555     ui.uploadProgressBar->setMinimum(0);
00556     ui.uploadProgressBar->setMaximum(0);
00557     ui.uploadProgressBar->setValue(0);
00558 
00559     // check the category
00560     QString categoryName = ui.mCategoryCombo->currentText();
00561     QList<Attica::Category>::const_iterator iter = categories.constBegin();
00562     Attica::Category category;
00563     while (iter != categories.constEnd()) {
00564         if (iter->name() == categoryName) {
00565             category = *iter;
00566             break;
00567         }
00568         ++iter;
00569     }
00570     if (!category.isValid()) {
00571         KMessageBox::error(q, i18n("The selected category \"%1\" is invalid.", categoryName), i18n("Upload Failed"));
00572         return;
00573     }
00574 
00575     // fill in the content object
00576     Attica::Content content;
00577     content.setName(ui.mNameEdit->text());
00578     QString summary = ui.mSummaryEdit->toPlainText();
00579     content.addAttribute("description", summary);
00580     content.addAttribute("version", ui.mVersionEdit->text());
00581 
00582     // for the license, if one of the licenses coming from the server was used, pass its id, otherwise the string
00583     QString licenseId = ui.mLicenseCombo->itemData(ui.mLicenseCombo->currentIndex()).toString();
00584     if (licenseId.isEmpty()) {
00585         // use other as type and add the string as text
00586         content.addAttribute("licensetype", "0");
00587         content.addAttribute("license", ui.mLicenseCombo->currentText());
00588     } else {
00589         content.addAttribute("licensetype", licenseId);
00590     }
00591 
00592     content.addAttribute("changelog", ui.changelog->toPlainText());
00593 
00594     // TODO: add additional attributes
00595     //content.addAttribute("downloadlink1", ui.link1->text());
00596     //content.addAttribute("downloadlink2", ui.link2->text());
00597     //content.addAttribute("homepage1", ui.homepage->text());
00598     //content.addAttribute("blog1", ui.blog->text());
00599 
00600     content.addAttribute("downloadbuy1", ui.priceCheckBox->isChecked() ? "1" : "0");
00601     content.addAttribute("downloadbuyprice1", QString::number(ui.priceSpinBox->value()));
00602     content.addAttribute("downloadbuyreason1", ui.priceReasonLineEdit->text());
00603     
00604     if (ui.radioNewUpload->isChecked()) {
00605         // upload a new content
00606         Attica::ItemPostJob<Attica::Content>* job = currentProvider().addNewContent(category, content);
00607         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_contentAdded(Attica::BaseJob*)));
00608         job->start();
00609     } else {
00610         // update old content
00611         Attica::ItemPostJob<Attica::Content>* job = currentProvider().editContent(category, ui.userContentList->currentItem()->data(Qt::UserRole).toString(), content);
00612         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_contentAdded(Attica::BaseJob*)));
00613         job->start();
00614     }
00615 }
00616 
00617 void UploadDialog::Private::_k_changePreview1()
00618 {
00619     KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
00620     previewFile1 = url;
00621     kDebug() << "preview is: " << url.url();
00622     QPixmap preview(url.toLocalFile());
00623     ui.previewImage1->setPixmap(preview.scaled(ui.previewImage1->size()));
00624 }
00625 
00626 void UploadDialog::Private::_k_changePreview2()
00627 {
00628     KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
00629     previewFile2 = url;
00630     QPixmap preview(url.toLocalFile());
00631     ui.previewImage2->setPixmap(preview.scaled(ui.previewImage1->size()));
00632 }
00633 
00634 void UploadDialog::Private::_k_changePreview3()
00635 {
00636     KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
00637     previewFile3 = url;
00638     QPixmap preview(url.toLocalFile());
00639     ui.previewImage3->setPixmap(preview.scaled(ui.previewImage1->size()));
00640 }
00641 
00642 void UploadDialog::Private::_k_contentAdded(Attica::BaseJob* baseJob)
00643 {
00644     if (baseJob->metadata().error()) {
00645         if (baseJob->metadata().error() == Attica::Metadata::NetworkError) {
00646             KMessageBox::error(q, i18n("There was a network error."), i18n("Uploading Failed"));
00647             return;
00648         }
00649         if (baseJob->metadata().error() == Attica::Metadata::OcsError) {
00650             if (baseJob->metadata().statusCode() == 102)
00651             KMessageBox::error(q, i18n("Authentication error."), i18n("Uploading Failed"));
00652         }
00653         return;
00654     }
00655 
00656     ui.createContentImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00657 
00658     Attica::ItemPostJob<Attica::Content> * job = static_cast<Attica::ItemPostJob<Attica::Content> *>(baseJob);
00659     if (job->metadata().error() != Attica::Metadata::NoError) {
00660         KMessageBox::error(q, i18n("Upload failed: %1", job->metadata().message()));
00661         return;
00662     }
00663 
00664     // only when adding new content we get an id returned, otherwise stick with the old one
00665     QString id = job->result().id();
00666     if (!id.isEmpty()) {
00667         contentId = id;
00668     }
00669 
00670     if (!uploadFile.isEmpty()) {
00671         doUpload(QString(), uploadFile);
00672     } else {
00673         doUpload(QString(), ui.uploadFileRequester->url());
00674     }
00675 
00676     // FIXME: status labels need to accomodate 3 previews
00677     if (!previewFile1.isEmpty()) {
00678         doUpload("1", previewFile1);
00679     }
00680     if (!previewFile2.isEmpty()) {
00681         doUpload("2", previewFile2);
00682     }
00683     if (!previewFile3.isEmpty()) {
00684         doUpload("3", previewFile3);
00685     }
00686 
00687     if (ui.radioNewUpload->isChecked()) {
00688         atticaHelper->loadDetailsLink(contentId);
00689     }
00690 }
00691 
00692 void UploadDialog::Private::doUpload(const QString& index, const KUrl& path)
00693 {
00694     QFile file(path.toLocalFile());
00695     if (!file.open(QIODevice::ReadOnly)) {
00696         KMessageBox::error(q, i18n("File not found: %1", uploadFile.url(), i18n("Upload Failed")));
00697         q->reject();
00698         return;
00699     }
00700 
00701     QByteArray fileContents;
00702     fileContents.append(file.readAll());
00703     file.close();
00704 
00705     QString fileName = QFileInfo(path.toLocalFile()).fileName();
00706 
00707     Attica::PostJob* job = 0;
00708     if (index.isEmpty()) {
00709         job = currentProvider().setDownloadFile(contentId, fileName, fileContents);
00710         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_fileUploadFinished(Attica::BaseJob*)));
00711     } else if (index == QLatin1String("1")) {
00712         job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
00713         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview1UploadFinished(Attica::BaseJob*)));
00714     } else if (index == QLatin1String("2")) {
00715         job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
00716         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview2UploadFinished(Attica::BaseJob*)));
00717     } else if (index == QLatin1String("3")) {
00718         job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
00719         q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview3UploadFinished(Attica::BaseJob*)));
00720     }
00721     if( job ) 
00722       job->start();
00723 }
00724 
00725 void UploadDialog::Private::_k_fileUploadFinished(Attica::BaseJob* )
00726 {
00727     ui.uploadContentImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00728     finishedContents = true;
00729     uploadFileFinished();
00730 }
00731 
00732 void UploadDialog::Private::_k_preview1UploadFinished(Attica::BaseJob* )
00733 {
00734     ui.uploadPreview1ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00735     finishedPreview1 = true;
00736     uploadFileFinished();
00737 }
00738 
00739 void UploadDialog::Private::_k_preview2UploadFinished(Attica::BaseJob* )
00740 {
00741     ui.uploadPreview2ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00742     finishedPreview2 = true;
00743     uploadFileFinished();
00744 }
00745 
00746 void UploadDialog::Private::_k_preview3UploadFinished(Attica::BaseJob* )
00747 {
00748     ui.uploadPreview3ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00749     finishedPreview3 = true;
00750     uploadFileFinished();
00751 }
00752 
00753 void UploadDialog::Private::uploadFileFinished()
00754 {
00755     // FIXME multiple previews
00756     if (finishedContents && (previewFile1.isEmpty() || finishedPreview1)
00757             && (previewFile2.isEmpty() || finishedPreview2)
00758             && (previewFile3.isEmpty() || finishedPreview3)) {
00759         finished = true;
00760         ui.uploadProgressBar->setMinimum(0);
00761         ui.uploadProgressBar->setMaximum(100);
00762         ui.uploadProgressBar->setValue(100);
00763         _k_updatePage();
00764     }
00765 }
00766 
00767 void UploadDialog::Private::_k_detailsLinkLoaded(const QUrl& url)
00768 {
00769     ui.contentWebsiteLink->setText(QLatin1String("<a href=\"") + url.toString() + QLatin1String("\">")
00770                                        + i18nc("A link to the website where the get hot new stuff upload can be seen", "Visit website") + QLatin1String("</a>"));
00771     ui.fetchContentLinkImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
00772 }
00773 
00774 #include "uploaddialog.moc"

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal