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

Skip menu "kdelibs-4.10.1 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