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

KNewStuff

  • knewstuff
  • knewstuff3
knewstuff3/uploaddialog.cpp
Go to the documentation of this file.
1 /*
2  knewstuff3/ui/uploaddialog.cpp.
3  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4  Copyright (c) 2009 Jeremy Whiting <jpwhiting@kde.org>
5  Copyright (C) 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "uploaddialog.h"
22 #include "uploaddialog_p.h"
23 
24 #include <QtGui/QLabel>
25 #include <QtGui/QLayout>
26 #include <QtGui/QDoubleSpinBox>
27 #include <QtCore/QString>
28 #include <QtCore/QSignalMapper>
29 
30 #include <kaboutdata.h>
31 #include <kcomponentdata.h>
32 #include <kfiledialog.h>
33 #include <kmessagebox.h>
34 #include <kstandarddirs.h>
35 #include <kpixmapsequence.h>
36 #include <kpixmapsequencewidget.h>
37 
38 #include <kdebug.h>
39 #include <kconfiggroup.h>
40 
41 using namespace KNS3;
42 
43 bool UploadDialog::Private::init(const QString& configfile)
44 {
45  QWidget* _mainWidget = new QWidget(q);
46  q->setMainWidget(_mainWidget);
47  ui.setupUi(_mainWidget);
48  atticaHelper = new AtticaHelper(q);
49 
50  bool success = true;
51  KConfig conf(configfile);
52  if (conf.accessMode() == KConfig::NoAccess) {
53  kError() << "No knsrc file named '" << configfile << "' was found." << endl;
54  success = false;
55  }
56  // FIXME: accessMode() doesn't return NoAccess for non-existing files
57  // - bug in kdecore?
58  // - this needs to be looked at again until KConfig backend changes for KDE 4
59  // the check below is a workaround
60  if (KStandardDirs::locate("config", configfile).isEmpty()) {
61  kError() << "No knsrc file named '" << configfile << "' was found." << endl;
62  success = false;
63  }
64 
65  KConfigGroup group;
66  if (conf.hasGroup("KNewStuff3")) {
67  kDebug() << "Loading KNewStuff3 config: " << configfile;
68  group = conf.group("KNewStuff3");
69  } else {
70  kError() << "A knsrc file was found but it doesn't contain a KNewStuff3 section." << endl;
71  success = false;
72  }
73 
74  if ( success ) {
75  const QString providersFileUrl = group.readEntry("ProvidersUrl", QString());
76 
77  categoryNames = group.readEntry("UploadCategories", QStringList());
78  // fall back to download categories
79  if (categoryNames.isEmpty()) {
80  categoryNames = group.readEntry("Categories", QStringList());
81  }
82 
83  atticaHelper->addProviderFile(QUrl(providersFileUrl));
84  }
85 
86  ui.mCategoryCombo->addItems(categoryNames);
87 
88  if (categoryNames.size() == 1) {
89  ui.mCategoryLabel->setVisible(false);
90  ui.mCategoryCombo->setVisible(false);
91  }
92 
93  kDebug() << "Categories: " << categoryNames;
94 
95  q->connect(atticaHelper, SIGNAL(providersLoaded(QStringList)), q, SLOT(_k_providersLoaded(QStringList)));
96  q->connect(atticaHelper, SIGNAL(loginChecked(bool)), q, SLOT(_k_checkCredentialsFinished(bool)));
97  q->connect(atticaHelper, SIGNAL(licensesLoaded(Attica::License::List)), q, SLOT(_k_licensesLoaded(Attica::License::List)));
98  q->connect(atticaHelper, SIGNAL(categoriesLoaded(Attica::Category::List)), q, SLOT(_k_categoriesLoaded(Attica::Category::List)));
99  q->connect(atticaHelper, SIGNAL(contentByCurrentUserLoaded(Attica::Content::List)), q, SLOT(_k_contentByCurrentUserLoaded(Attica::Content::List)));
100  q->connect(atticaHelper, SIGNAL(contentLoaded(Attica::Content)), q, SLOT(_k_updatedContentFetched(Attica::Content)));
101  q->connect(atticaHelper, SIGNAL(detailsLinkLoaded(QUrl)), q, SLOT(_k_detailsLinkLoaded(QUrl)));
102  q->connect(atticaHelper, SIGNAL(currencyLoaded(QString)), q, SLOT(_k_currencyLoaded(QString)));
103  q->connect(atticaHelper, SIGNAL(previewLoaded(int,QImage)), q, SLOT(_k_previewLoaded(int,QImage)));
104  atticaHelper->init();
105 
106  q->connect(ui.changePreview1Button, SIGNAL(clicked()), q, SLOT(_k_changePreview1()));
107  q->connect(ui.changePreview2Button, SIGNAL(clicked()), q, SLOT(_k_changePreview2()));
108  q->connect(ui.changePreview3Button, SIGNAL(clicked()), q, SLOT(_k_changePreview3()));
109 
110  q->connect(ui.providerComboBox, SIGNAL(currentIndexChanged(QString)), q, SLOT(_k_providerChanged(QString)));
111  q->connect(ui.radioUpdate, SIGNAL(toggled(bool)), q, SLOT(_k_updateContentsToggled(bool)));
112 
113  //Busy widget
114  busyWidget = new KPixmapSequenceWidget();
115  busyWidget->setSequence(KPixmapSequence("process-working", 22));
116  busyWidget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
117  ui.busyWidget->setLayout(new QHBoxLayout());
118  ui.busyWidget->layout()->addWidget(busyWidget);
119  busyWidget->setVisible(false);
120 
121  return success;
122 }
123 
124 void UploadDialog::Private::setBusy(const QString& message)
125 {
126  ui.busyLabel->setText(message);
127  busyWidget->setVisible(true);
128 }
129 
130 void UploadDialog::Private::setIdle(const QString& message)
131 {
132  ui.busyLabel->setText(message);
133  busyWidget->setVisible(false);
134 }
135 
136 void UploadDialog::Private::_k_showPage(int page)
137 {
138  ui.stackedWidget->setCurrentIndex(page);
139  setIdle(QString());
140 
141  switch (ui.stackedWidget->currentIndex()) {
142  case UserPasswordPage:
143  ui.username->setFocus();
144  // TODO 4.6 enable new string: setBusy(i18n("Fetching provider information..."));
145  break;
146 
147  case FileNewUpdatePage:
148  atticaHelper->loadLicenses();
149  atticaHelper->loadCurrency();
150  ui.uploadButton->setFocus();
151  setBusy(i18n("Fetching license data from server..."));
152  break;
153 
154  case Details1Page:
155  if (ui.radioUpdate->isChecked()) {
156  // Fetch
157  atticaHelper->loadContent(ui.userContentList->currentItem()->data(Qt::UserRole).toString());
158  setBusy(i18n("Fetching content data from server..."));
159  }
160 
161  ui.mNameEdit->setFocus();
162  break;
163 
164  case UploadFinalPage:
165  if (previewFile1.isEmpty()) {
166  ui.uploadPreview1ImageLabel->setVisible(false);
167  ui.uploadPreview1Label->setVisible(false);
168  }
169  if (previewFile2.isEmpty()) {
170  ui.uploadPreview2ImageLabel->setVisible(false);
171  ui.uploadPreview2Label->setVisible(false);
172  }
173  if (previewFile3.isEmpty()) {
174  ui.uploadPreview3ImageLabel->setVisible(false);
175  ui.uploadPreview3Label->setVisible(false);
176  }
177  break;
178  }
179 
180  _k_updatePage();
181 }
182 
183 void UploadDialog::Private::_k_updatePage()
184 {
185  bool firstPage = ui.stackedWidget->currentIndex() == 0;
186  q->enableButton(BackButton, !firstPage && !finished);
187 
188  bool nextEnabled = false;
189  switch (ui.stackedWidget->currentIndex()) {
190  case UserPasswordPage:
191  if (ui.providerComboBox->count() > 0 && !ui.username->text().isEmpty() && !ui.password->text().isEmpty()) {
192  nextEnabled = true;
193  }
194  break;
195 
196  case FileNewUpdatePage:
197  // FIXME: check if the file requester contains a valid file
198  if (!uploadFile.isEmpty() || ui.uploadFileRequester->url().isLocalFile()) {
199  if (ui.radioNewUpload->isChecked() || ui.userContentList->currentRow() >= 0) {
200  nextEnabled = true;
201  }
202  }
203  break;
204 
205  case Details1Page:
206  if (!ui.mNameEdit->text().isEmpty()) {
207  nextEnabled = true;
208  }
209  break;
210 
211  case Details2Page:
212  nextEnabled = true;
213  break;
214 
215  case UploadFinalPage:
216  break;
217  }
218 
219  q->enableButton(NextButton, nextEnabled);
220  q->enableButton(FinishButton, finished);
221 
222  q->setDefaultButton(nextEnabled ? NextButton : FinishButton);
223 
224  if (nextEnabled && q->button(KDialog::Cancel)->hasFocus()) {
225  q->button(NextButton)->setFocus();
226  }
227 }
228 
229 void UploadDialog::Private::_k_providersLoaded(const QStringList& providers)
230 {
231  if (providers.size() == 0) {
232  // TODO 4.6 enable new string: setIdle(i18n("Could not fetch provider information."));
233  ui.stackedWidget->setEnabled(false);
234  kWarning() << "Could not load providers.";
235  return;
236  }
237  setIdle(QString());
238  ui.providerComboBox->addItems(providers);
239  ui.providerComboBox->setCurrentIndex(0);
240  atticaHelper->setCurrentProvider(providers.at(0));
241 
242  QString user;
243  QString pass;
244  if (atticaHelper->loadCredentials(user, pass)) {
245  ui.username->setText(user);
246  ui.password->setText(pass);
247  }
248  _k_updatePage();
249 }
250 
251 void UploadDialog::Private::_k_providerChanged(const QString& providerName)
252 {
253  atticaHelper->setCurrentProvider(providerName);
254  ui.username->clear();
255  ui.password->clear();
256  QString user;
257  QString pass;
258  if (atticaHelper->loadCredentials(user, pass)) {
259  ui.username->setText(user);
260  ui.password->setText(pass);
261  }
262  _k_updatePage();
263 }
264 
265 void UploadDialog::Private::_k_backPage()
266 {
267  _k_showPage(ui.stackedWidget->currentIndex()-1);
268 }
269 
270 void UploadDialog::Private::_k_nextPage()
271 {
272  // TODO: validate credentials after user name/password have been entered
273  if (ui.stackedWidget->currentIndex() == UserPasswordPage) {
274  setBusy(i18n("Checking login..."));
275  q->button(NextButton)->setEnabled(false);
276  ui.providerComboBox->setEnabled(false);
277  ui.username->setEnabled(false);
278  ui.password->setEnabled(false);
279  atticaHelper->checkLogin(ui.username->text(), ui.password->text());
280  } else {
281  _k_showPage(ui.stackedWidget->currentIndex()+1);
282  }
283 }
284 
285 void UploadDialog::Private::_k_checkCredentialsFinished(bool success)
286 {
287  ui.providerComboBox->setEnabled(true);
288  ui.username->setEnabled(true);
289  ui.password->setEnabled(true);
290 
291  if (success) {
292  atticaHelper->saveCredentials(ui.username->text(), ui.password->text());
293  _k_showPage(FileNewUpdatePage);
294 
295  atticaHelper->loadCategories(categoryNames);
296  setBusy(i18n("Fetching your previously updated content..."));
297  } else {
298  // TODO check what the actual error is
299  setIdle(i18n("Could not verify login, please try again."));
300  }
301 }
302 
303 void UploadDialog::Private::_k_licensesLoaded(const Attica::License::List& licenses)
304 {
305  ui.mLicenseCombo->clear();
306  foreach (Attica::License license, licenses) {
307  ui.mLicenseCombo->addItem(license.name(), license.id());
308  }
309 }
310 
311 void UploadDialog::Private::_k_currencyLoaded(const QString& currency)
312 {
313  ui.priceCurrency->setText(currency);
314 }
315 
316 void UploadDialog::Private::_k_contentByCurrentUserLoaded(const Attica::Content::List& contentList)
317 {
318  setIdle(i18n("Fetching your previously updated content finished."));
319 
320  foreach(Attica::Content content, contentList) {
321  QListWidgetItem *contentItem = new QListWidgetItem(content.name());
322  contentItem->setData(Qt::UserRole, content.id());
323  ui.userContentList->addItem(contentItem);
324  }
325 
326  if (ui.userContentList->count() > 0) {
327  ui.userContentList->setCurrentRow(0);
328  ui.radioUpdate->setEnabled(true);
329  _k_updatePage();
330  }
331 
332 }
333 
334 void UploadDialog::Private::_k_updatedContentFetched(const Attica::Content& content)
335 {
336  setIdle(i18n("Fetching content data from server finished."));
337 
338  contentId = content.id();
339  // fill in ui
340  ui.mNameEdit->setText(content.name());
341  ui.mSummaryEdit->setText(content.description());
342  ui.mVersionEdit->setText(content.version());
343  ui.changelog->setText(content.changelog());
344  ui.priceCheckBox->setChecked(content.attribute("downloadbuy1") == "1");
345  ui.priceSpinBox->setValue(content.attribute("downloadbuyprice1").toDouble());
346  ui.priceReasonLineEdit->setText(content.attribute("downloadbuyreason1"));
347 
348  bool conversionOk = false;
349  int licenseNumber = content.license().toInt(&conversionOk);
350  if (conversionOk) {
351  // check if that int is in list
352  int row = ui.mLicenseCombo->findData(licenseNumber, Qt::UserRole);
353  ui.mLicenseCombo->setCurrentIndex(row);
354  } else {
355  ui.mLicenseCombo->setEditText(content.license());
356  }
357 
358  ui.contentWebsiteLink->setText(QLatin1String("<a href=\"") + content.detailpage().toString() + QLatin1String("\">")
359  + i18nc("A link to the website where the get hot new stuff upload can be seen", "Visit website") + QLatin1String("</a>"));
360  ui.fetchContentLinkImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
361 }
362 
363 void UploadDialog::Private::_k_previewLoaded(int index, const QImage& image)
364 {
365  switch (index) {
366  case 1:
367  ui.previewImage1->setPixmap(QPixmap::fromImage(image));
368  break;
369  case 2:
370  ui.previewImage2->setPixmap(QPixmap::fromImage(image));
371  break;
372  case 3:
373  ui.previewImage3->setPixmap(QPixmap::fromImage(image));
374  break;
375  }
376 }
377 
378 void UploadDialog::Private::_k_updateContentsToggled(bool update)
379 {
380  ui.userContentList->setEnabled(update);
381 }
382 
383 UploadDialog::UploadDialog(QWidget *parent)
384  : KDialog(parent), d(new Private(this))
385 {
386  KComponentData component = KGlobal::activeComponent();
387  QString name = component.componentName();
388  init(name + ".knsrc");
389 }
390 
391 UploadDialog::UploadDialog(const QString& configFile, QWidget *parent)
392  : KDialog(parent), d(new Private(this))
393 {
394  init(configFile);
395 }
396 
397 UploadDialog::~UploadDialog()
398 {
399  delete d;
400 }
401 
402 bool UploadDialog::init(const QString &configfile)
403 {
404  bool success = d->init(configfile);
405 
406  setCaption(i18n("Share Hot New Stuff"));
407 
408  setButtons(KDialog::Cancel | KDialog::User1 | KDialog::User2 | KDialog::User3 | KDialog::Help);
409  setButtonGuiItem( BackButton, KStandardGuiItem::back(KStandardGuiItem::UseRTL) );
410 
411  setButtonText( NextButton, i18nc("Opposite to Back", "Next") );
412  setButtonIcon( NextButton, KStandardGuiItem::forward(KStandardGuiItem::UseRTL).icon() );
413  setButtonText(FinishButton, i18n("Finish"));
414  setButtonIcon( FinishButton, KIcon("dialog-ok-apply") );
415  setDefaultButton(NextButton);
416  d->_k_updatePage();
417 
418  connect(d->ui.username, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
419 
420  connect(d->ui.password, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
421  connect(d->ui.mNameEdit, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
422  connect(d->ui.uploadFileRequester, SIGNAL(textChanged(QString)), this, SLOT(_k_updatePage()));
423  connect(d->ui.priceCheckBox, SIGNAL(toggled(bool)), this, SLOT(_k_priceToggled(bool)));
424 
425  connect(d->ui.uploadButton, SIGNAL(clicked()), this, SLOT(_k_startUpload()));
426 
427  connect(this, SIGNAL(user3Clicked()), this, SLOT(_k_backPage()));
428  connect(this, SIGNAL(user2Clicked()), this, SLOT(_k_nextPage()));
429  connect(this, SIGNAL(user1Clicked()), this, SLOT(accept()));
430 
431  d->ui.mTitleWidget->setText(i18nc("Program name followed by 'Add On Uploader'",
432  "%1 Add-On Uploader",
433  KGlobal::activeComponent().aboutData()->programName()));
434  d->ui.mTitleWidget->setPixmap(KIcon(KGlobal::activeComponent().aboutData()->programIconName()));
435 
436  if ( success ) {
437  d->_k_showPage(0);
438  }
439 
440  return success;
441 }
442 
443 void UploadDialog::setUploadFile(const KUrl& payloadFile)
444 {
445  d->uploadFile = payloadFile;
446 
447  d->ui.uploadFileLabel->setVisible(false);
448  d->ui.uploadFileRequester->setVisible(false);
449 
450  QFile file(d->uploadFile.toLocalFile());
451  if (!file.open(QIODevice::ReadOnly)) {
452  KMessageBox::error(this, i18n("File not found: %1", d->uploadFile.url()), i18n("Upload Failed"));
453  }
454 }
455 
456 void UploadDialog::setUploadName(const QString& name)
457 {
458  d->ui.mNameEdit->setText(name);
459 }
460 
461 void UploadDialog::selectCategory(const QString& category)
462 {
463  d->ui.mCategoryCombo->setCurrentIndex(d->ui.mCategoryCombo->findText(category, Qt::MatchFixedString));
464 }
465 
466 void UploadDialog::setChangelog(const QString& changelog)
467 {
468  d->ui.changelog->setText(changelog);
469 }
470 
471 void UploadDialog::setDescription(const QString& description)
472 {
473  d->ui.mSummaryEdit->setText(description);
474 }
475 
476 void UploadDialog::setPriceEnabled(bool enabled)
477 {
478  d->ui.priceCheckBox->setVisible(enabled);
479  d->ui.priceGroupBox->setVisible(enabled);
480 }
481 
482 void UploadDialog::setPrice(double price)
483 {
484  d->ui.priceCheckBox->setEnabled(true);
485  d->ui.priceSpinBox->setValue(price);
486 }
487 
488 void UploadDialog::setPriceReason(const QString& reason)
489 {
490  d->ui.priceReasonLineEdit->setText(reason);
491 }
492 
493 void UploadDialog::setVersion(const QString& version)
494 {
495  d->ui.mVersionEdit->setText(version);
496 }
497 
498 void UploadDialog::setPreviewImageFile(uint number, const KUrl& file)
499 {
500  QPixmap preview(file.toLocalFile());
501  switch(number) {
502  case 0 :
503  d->previewFile1 = file;
504  d->ui.previewImage1->setPixmap(preview.scaled(d->ui.previewImage1->size()));
505  break;
506  case 1 :
507  d->previewFile2 = file;
508  d->ui.previewImage2->setPixmap(preview.scaled(d->ui.previewImage2->size()));
509  break;
510  case 2 :
511  d->previewFile3 = file;
512  d->ui.previewImage3->setPixmap(preview.scaled(d->ui.previewImage3->size()));
513  break;
514  default :
515  kError() << "Wrong preview image file number";
516  break;
517  }
518 }
519 
520 void UploadDialog::Private::_k_priceToggled(bool priceEnabled)
521 {
522  ui.priceGroupBox->setEnabled(priceEnabled);
523 }
524 
525 void UploadDialog::Private::_k_categoriesLoaded(const Attica::Category::List& loadedCategories)
526 {
527  categories = loadedCategories;
528 
529  // at least one category is needed
530  if (categories.count() == 0) {
531  KMessageBox::error(q,
532  i18np("The server does not recognize the category %2 to which you are trying to upload.",
533  "The server does not recognize any of the categories to which you are trying to upload: %2",
534  categoryNames.size(), categoryNames.join(", ")),
535  i18n("Error"));
536  // close the dialog
537  q->reject();
538  return;
539  }
540  foreach(Attica::Category c, categories) {
541  ui.mCategoryCombo->addItem(c.name(), c.id());
542  }
543  atticaHelper->loadContentByCurrentUser();
544 }
545 
546 void UploadDialog::accept()
547 {
548  KDialog::accept();
549 }
550 
551 void UploadDialog::Private::_k_startUpload()
552 {
553  // FIXME: this only works if categories are set in the .knsrc file
554  // TODO: ask for confirmation when closing the dialog
555 
556  q->button(BackButton)->setEnabled(false);
557  q->button(KDialog::Cancel)->setEnabled(false);
558 
559  ui.uploadButton->setEnabled(false);
560 
561  // idle back and forth, we need a fix in attica to get at real progress values
562  ui.uploadProgressBar->setMinimum(0);
563  ui.uploadProgressBar->setMaximum(0);
564  ui.uploadProgressBar->setValue(0);
565 
566  // check the category
567  QString categoryName = ui.mCategoryCombo->currentText();
568  QList<Attica::Category>::const_iterator iter = categories.constBegin();
569  Attica::Category category;
570  while (iter != categories.constEnd()) {
571  if (iter->name() == categoryName) {
572  category = *iter;
573  break;
574  }
575  ++iter;
576  }
577  if (!category.isValid()) {
578  KMessageBox::error(q, i18n("The selected category \"%1\" is invalid.", categoryName), i18n("Upload Failed"));
579  return;
580  }
581 
582  // fill in the content object
583  Attica::Content content;
584  content.setName(ui.mNameEdit->text());
585  QString summary = ui.mSummaryEdit->toPlainText();
586  content.addAttribute("description", summary);
587  content.addAttribute("version", ui.mVersionEdit->text());
588 
589  // for the license, if one of the licenses coming from the server was used, pass its id, otherwise the string
590  QString licenseId = ui.mLicenseCombo->itemData(ui.mLicenseCombo->currentIndex()).toString();
591  if (licenseId.isEmpty()) {
592  // use other as type and add the string as text
593  content.addAttribute("licensetype", "0");
594  content.addAttribute("license", ui.mLicenseCombo->currentText());
595  } else {
596  content.addAttribute("licensetype", licenseId);
597  }
598 
599  content.addAttribute("changelog", ui.changelog->toPlainText());
600 
601  // TODO: add additional attributes
602  //content.addAttribute("downloadlink1", ui.link1->text());
603  //content.addAttribute("downloadlink2", ui.link2->text());
604  //content.addAttribute("homepage1", ui.homepage->text());
605  //content.addAttribute("blog1", ui.blog->text());
606 
607  content.addAttribute("downloadbuy1", ui.priceCheckBox->isChecked() ? "1" : "0");
608  content.addAttribute("downloadbuyprice1", QString::number(ui.priceSpinBox->value()));
609  content.addAttribute("downloadbuyreason1", ui.priceReasonLineEdit->text());
610 
611  if (ui.radioNewUpload->isChecked()) {
612  // upload a new content
613  Attica::ItemPostJob<Attica::Content>* job = currentProvider().addNewContent(category, content);
614  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_contentAdded(Attica::BaseJob*)));
615  job->start();
616  } else {
617  // update old content
618  Attica::ItemPostJob<Attica::Content>* job = currentProvider().editContent(category, ui.userContentList->currentItem()->data(Qt::UserRole).toString(), content);
619  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_contentAdded(Attica::BaseJob*)));
620  job->start();
621  }
622 }
623 
624 void UploadDialog::Private::_k_changePreview1()
625 {
626  KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
627  previewFile1 = url;
628  kDebug() << "preview is: " << url.url();
629  QPixmap preview(url.toLocalFile());
630  ui.previewImage1->setPixmap(preview.scaled(ui.previewImage1->size()));
631 }
632 
633 void UploadDialog::Private::_k_changePreview2()
634 {
635  KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
636  previewFile2 = url;
637  QPixmap preview(url.toLocalFile());
638  ui.previewImage2->setPixmap(preview.scaled(ui.previewImage1->size()));
639 }
640 
641 void UploadDialog::Private::_k_changePreview3()
642 {
643  KUrl url = KFileDialog::getImageOpenUrl(KUrl(), q, i18n("Select preview image"));
644  previewFile3 = url;
645  QPixmap preview(url.toLocalFile());
646  ui.previewImage3->setPixmap(preview.scaled(ui.previewImage1->size()));
647 }
648 
649 void UploadDialog::Private::_k_contentAdded(Attica::BaseJob* baseJob)
650 {
651  if (baseJob->metadata().error()) {
652  if (baseJob->metadata().error() == Attica::Metadata::NetworkError) {
653  KMessageBox::error(q, i18n("There was a network error."), i18n("Uploading Failed"));
654  return;
655  }
656  if (baseJob->metadata().error() == Attica::Metadata::OcsError) {
657  if (baseJob->metadata().statusCode() == 102)
658  KMessageBox::error(q, i18n("Authentication error."), i18n("Uploading Failed"));
659  }
660  return;
661  }
662 
663  ui.createContentImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
664 
665  Attica::ItemPostJob<Attica::Content> * job = static_cast<Attica::ItemPostJob<Attica::Content> *>(baseJob);
666  if (job->metadata().error() != Attica::Metadata::NoError) {
667  KMessageBox::error(q, i18n("Upload failed: %1", job->metadata().message()));
668  return;
669  }
670 
671  // only when adding new content we get an id returned, otherwise stick with the old one
672  QString id = job->result().id();
673  if (!id.isEmpty()) {
674  contentId = id;
675  }
676 
677  if (!uploadFile.isEmpty()) {
678  doUpload(QString(), uploadFile);
679  } else {
680  doUpload(QString(), ui.uploadFileRequester->url());
681  }
682 
683  // FIXME: status labels need to accomodate 3 previews
684  if (!previewFile1.isEmpty()) {
685  doUpload("1", previewFile1);
686  }
687  if (!previewFile2.isEmpty()) {
688  doUpload("2", previewFile2);
689  }
690  if (!previewFile3.isEmpty()) {
691  doUpload("3", previewFile3);
692  }
693 
694  if (ui.radioNewUpload->isChecked()) {
695  atticaHelper->loadDetailsLink(contentId);
696  }
697 }
698 
699 void UploadDialog::Private::doUpload(const QString& index, const KUrl& path)
700 {
701  QFile file(path.toLocalFile());
702  if (!file.open(QIODevice::ReadOnly)) {
703  KMessageBox::error(q, i18n("File not found: %1", uploadFile.url(), i18n("Upload Failed")));
704  q->reject();
705  return;
706  }
707 
708  QByteArray fileContents;
709  fileContents.append(file.readAll());
710  file.close();
711 
712  QString fileName = QFileInfo(path.toLocalFile()).fileName();
713 
714  Attica::PostJob* job = 0;
715  if (index.isEmpty()) {
716  job = currentProvider().setDownloadFile(contentId, fileName, fileContents);
717  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_fileUploadFinished(Attica::BaseJob*)));
718  } else if (index == QLatin1String("1")) {
719  job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
720  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview1UploadFinished(Attica::BaseJob*)));
721  } else if (index == QLatin1String("2")) {
722  job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
723  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview2UploadFinished(Attica::BaseJob*)));
724  } else if (index == QLatin1String("3")) {
725  job = currentProvider().setPreviewImage(contentId, index, fileName, fileContents);
726  q->connect(job, SIGNAL(finished(Attica::BaseJob*)), q, SLOT(_k_preview3UploadFinished(Attica::BaseJob*)));
727  }
728  if( job )
729  job->start();
730 }
731 
732 void UploadDialog::Private::_k_fileUploadFinished(Attica::BaseJob* )
733 {
734  ui.uploadContentImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
735  finishedContents = true;
736  uploadFileFinished();
737 }
738 
739 void UploadDialog::Private::_k_preview1UploadFinished(Attica::BaseJob* )
740 {
741  ui.uploadPreview1ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
742  finishedPreview1 = true;
743  uploadFileFinished();
744 }
745 
746 void UploadDialog::Private::_k_preview2UploadFinished(Attica::BaseJob* )
747 {
748  ui.uploadPreview2ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
749  finishedPreview2 = true;
750  uploadFileFinished();
751 }
752 
753 void UploadDialog::Private::_k_preview3UploadFinished(Attica::BaseJob* )
754 {
755  ui.uploadPreview3ImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
756  finishedPreview3 = true;
757  uploadFileFinished();
758 }
759 
760 void UploadDialog::Private::uploadFileFinished()
761 {
762  // FIXME multiple previews
763  if (finishedContents && (previewFile1.isEmpty() || finishedPreview1)
764  && (previewFile2.isEmpty() || finishedPreview2)
765  && (previewFile3.isEmpty() || finishedPreview3)) {
766  finished = true;
767  ui.uploadProgressBar->setMinimum(0);
768  ui.uploadProgressBar->setMaximum(100);
769  ui.uploadProgressBar->setValue(100);
770  _k_updatePage();
771  }
772 }
773 
774 void UploadDialog::Private::_k_detailsLinkLoaded(const QUrl& url)
775 {
776  ui.contentWebsiteLink->setText(QLatin1String("<a href=\"") + url.toString() + QLatin1String("\">")
777  + i18nc("A link to the website where the get hot new stuff upload can be seen", "Visit website") + QLatin1String("</a>"));
778  ui.fetchContentLinkImageLabel->setPixmap(KIcon("dialog-ok").pixmap(16));
779 }
780 
781 #include "uploaddialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 20:21:42 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

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

kdelibs-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal