korganizer Library API Documentation

downloaddialog.cpp

00001 /*
00002     This file is part of KNewStuff.
00003     Copyright (c) 2003 Josef Spillner <spillner@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "downloaddialog.h"
00022 #include "downloaddialog.moc"
00023 
00024 #include <klocale.h>
00025 #include <ktabctl.h>
00026 #include <klistview.h>
00027 #include <kdebug.h>
00028 #include <kio/job.h>
00029 #include <kio/netaccess.h>
00030 #include <kmessagebox.h>
00031 #include <kurl.h>
00032 #include <kconfig.h>
00033 #include <kapplication.h>
00034 #include <kiconloader.h>
00035 
00036 #include <knewstuff/entry.h>
00037 #include <knewstuff/knewstuffgeneric.h>
00038 #include <knewstuff/engine.h>
00039 
00040 #include <qlayout.h>
00041 #include <qpushbutton.h>
00042 #include <qdom.h>
00043 #include <qlabel.h>
00044 #include <qtextbrowser.h>
00045 #include <qtimer.h> // hack
00046 
00047 using namespace KNS;
00048 
00049 DownloadDialog::DownloadDialog(Engine *engine, QWidget *)
00050 : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
00051   KDialogBase::Close, KDialogBase::Close)
00052 {
00053   init(engine);
00054 }
00055 
00056 DownloadDialog::DownloadDialog(QWidget *)
00057 : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
00058   KDialogBase::Close, KDialogBase::Close)
00059 {
00060   init(0);
00061 }
00062 
00063 void DownloadDialog::init(Engine *engine)
00064 {
00065   resize(700, 400);
00066 
00067   m_engine = engine;
00068   m_page = NULL;
00069 
00070   connect(this, SIGNAL(aboutToShowPage(QWidget*)), SLOT(slotPage(QWidget*)));
00071 
00072   if(!engine)
00073   {
00074     m_loader = new ProviderLoader(this);
00075     connect(m_loader, SIGNAL(providersLoaded(Provider::List*)), SLOT(slotProviders(Provider::List*)));
00076   }
00077 }
00078 
00079 DownloadDialog::~DownloadDialog()
00080 {
00081 }
00082 
00083 void DownloadDialog::load()
00084 {
00085   m_loader->load(m_filter);
00086 }
00087 
00088 void DownloadDialog::clear()
00089 {
00090   QMap<QWidget*, QValueList<KListView*>* >::Iterator it;
00091   for(it = m_map.begin(); it != m_map.end(); it++)
00092   {
00093     QValueList<KListView*> *v = it.data();
00094     kdDebug() << "clear listviews in " << v << endl;
00095     if(v)
00096     {
00097       (*(v->at(0)))->clear();
00098       (*(v->at(1)))->clear();
00099       (*(v->at(2)))->clear();
00100 
00101       //delete (*it);
00102     }
00103 
00104     delete it.key();
00105   }
00106   m_map.clear();
00107 }
00108 
00109 void DownloadDialog::slotProviders(Provider::List *list)
00110 {
00111   Provider *p;
00112   /*QFrame *frame;*/
00113 
00114   for(p = list->first(); p; p = list->next())
00115   {
00116     kdDebug() << "++ provider ++ " << p->name() << endl;
00117 
00118     if(!m_filter.isEmpty())
00119       loadProvider(p);
00120     else
00121       addProvider(p);
00122     /*if(p == list->getFirst())
00123       slotPage(m_frame);*/ // only if !qtbug
00124   }
00125 }
00126 
00127 void DownloadDialog::addProvider(Provider *p)
00128 {
00129   QFrame *frame;
00130   KTabCtl *ctl;
00131   QWidget *w_d, *w_r, *w_l;
00132   QWidget *w2;
00133   KListView *lvtmp_r, *lvtmp_d, *lvtmp_l;
00134   QTextBrowser *rt;
00135   QString tmp;
00136 
00137   if(m_map.count() == 0)
00138   {
00139     frame = addPage(i18n("Welcome"), i18n("Welcome"), QPixmap(""));
00140     delete frame;
00141   }
00142 
00143   kdDebug() << "addProvider()/begin" << endl;
00144 
00145   KIO::NetAccess::download(p->icon(), tmp, this);
00146   frame = addPage(p->name(), p->name(), QPixmap(tmp));
00147   m_frame = frame;
00148 
00149   w2 = new QWidget(frame);
00150   w_d = new QWidget(frame);
00151   w_r = new QWidget(frame);
00152   w_l = new QWidget(frame);
00153 
00154   ctl = new KTabCtl(frame);
00155   ctl->addTab(w_r, i18n("Highest Rated"));
00156   ctl->addTab(w_d, i18n("Most Downloads"));
00157   ctl->addTab(w_l, i18n("Latest"));
00158 
00159   m_curtab = 0;
00160   connect(ctl, SIGNAL(tabSelected(int)), SLOT(slotTab(int)));
00161 
00162   QHBoxLayout *box = new QHBoxLayout(frame);
00163   box->add(ctl);
00164 
00165   lvtmp_r = new KListView(w_r);
00166   lvtmp_r->addColumn(i18n("Name"));
00167   lvtmp_r->addColumn(i18n("Version"));
00168   lvtmp_r->addColumn(i18n("Rating"));
00169   lvtmp_r->setSorting(2, false);
00170 
00171   lvtmp_d = new KListView(w_d);
00172   lvtmp_d->addColumn(i18n("Name"));
00173   lvtmp_d->addColumn(i18n("Version"));
00174   lvtmp_d->addColumn(i18n("Downloads"));
00175   lvtmp_d->setSorting(2, false);
00176 
00177   lvtmp_l = new KListView(w_l);
00178   lvtmp_l->addColumn(i18n("Name"));
00179   lvtmp_l->addColumn(i18n("Version"));
00180   lvtmp_l->addColumn(i18n("Release Date"));
00181   lvtmp_l->setSorting(2, false);
00182 
00183   connect(lvtmp_r, SIGNAL(selectionChanged()), SLOT(slotSelected()));
00184   connect(lvtmp_d, SIGNAL(selectionChanged()), SLOT(slotSelected()));
00185   connect(lvtmp_l, SIGNAL(selectionChanged()), SLOT(slotSelected()));
00186 
00187   rt = new QTextBrowser(frame);
00188   rt->setMinimumWidth(150);
00189 
00190   QPushButton *in = new QPushButton(i18n("Install"), frame);
00191   QPushButton *de = new QPushButton(i18n("Details"), frame);
00192   in->setEnabled(false);
00193   de->setEnabled(false);
00194 
00195   box->addSpacing(spacingHint());
00196   QVBoxLayout *vbox = new QVBoxLayout(box);
00197   vbox->add(rt);
00198   vbox->addSpacing(spacingHint());
00199   vbox->add(de);
00200   vbox->add(in);
00201 
00202   connect(in, SIGNAL(clicked()), SLOT(slotInstall()));
00203   connect(de, SIGNAL(clicked()), SLOT(slotDetails()));
00204 
00205   QVBoxLayout *box2 = new QVBoxLayout(w_r);
00206   box2->add(lvtmp_r);
00207   QVBoxLayout *box3 = new QVBoxLayout(w_d);
00208   box3->add(lvtmp_d);
00209   QVBoxLayout *box4 = new QVBoxLayout(w_l);
00210   box4->add(lvtmp_l);
00211 
00212   QValueList<KListView*> *v = new QValueList<KListView*>;
00213   *v << lvtmp_r << lvtmp_d << lvtmp_l;
00214   m_map[frame] = v;
00215   m_rts[frame] = rt;
00216   QValueList<QPushButton*> *vb = new QValueList<QPushButton*>;
00217   *vb << in << de;
00218   m_buttons[frame] = vb;
00219   m_providers[frame] = p;
00220 
00221   kdDebug() << "addProvider()/end; lvtmp_r = " << lvtmp_r << endl;
00222 
00223   if(m_engine) slotPage(frame);
00224 
00225   QTimer::singleShot(100, this, SLOT(slotFinish()));
00226 }
00227 
00228 void DownloadDialog::slotResult(KIO::Job *job)
00229 {
00230   QDomDocument dom;
00231   QDomElement knewstuff;
00232 
00233   kdDebug() << "got data: " << m_data[job] << endl;
00234 
00235   kapp->config()->setGroup("KNewStuffStatus");
00236 
00237   dom.setContent(m_data[job]);
00238   knewstuff = dom.documentElement();
00239 
00240   for(QDomNode pn = knewstuff.firstChild(); !pn.isNull(); pn = pn.nextSibling())
00241   {
00242     QDomElement stuff = pn.toElement();
00243 
00244     kdDebug() << "element: " << stuff.tagName() << endl;
00245 
00246     if(stuff.tagName() == "stuff")
00247     {
00248       Entry *entry = new Entry(stuff);
00249       kdDebug() << "TYPE::" << entry->type() << " FILTER::" << m_filter << endl;
00250       if(!entry->type().isEmpty())
00251       {
00252         if((!m_filter.isEmpty()) && (entry->type() != m_filter)) continue;
00253       }
00254 
00255       if((!m_filter.isEmpty()) && (m_jobs[job]))
00256       {
00257         Provider *p = m_jobs[job];
00258         addProvider(p);
00259         slotPage(m_frame);
00260         m_jobs[job] = 0;
00261       }
00262       addEntry(entry);
00263     }
00264   }
00265 
00266   m_data[job] = "";
00267 }
00268 
00269 void DownloadDialog::addEntry(Entry *entry)
00270 {
00271   QPixmap pix;
00272   QDate date;
00273   int installed;
00274 
00275   /*if(m_engine)
00276   {
00277     if(m_map.count() == 0)
00278     {
00279       m_frame = addPage(i18n("Welcome"), i18n("Welcome"), QPixmap(""));
00280       Provider *p = new Provider();
00281       p->setName(i18n("Generic"));
00282       addProvider(p);
00283       slotPage(m_frame);
00284     }
00285   }*/
00286   kapp->config()->setGroup("KNewStuffStatus");
00287   date = QDate::fromString(kapp->config()->readEntry(entry->name()));
00288   if(!date.isValid()) installed = 0;
00289   else if(date < entry->releaseDate()) installed = -1;
00290   else installed = 1;
00291 
00292   if(installed > 0) pix = KGlobal::iconLoader()->loadIcon("ok", KIcon::Small);
00293   else if(installed < 0) pix = KGlobal::iconLoader()->loadIcon("history", KIcon::Small);
00294   else pix = QPixmap();
00295 
00296   KListViewItem *tmp_r = new KListViewItem(lv_r,
00297     entry->name(), entry->version(), QString("%1").arg(entry->rating()));
00298   KListViewItem *tmp_d = new KListViewItem(lv_d,
00299     entry->name(), entry->version(), QString("%1").arg(entry->downloads()));
00300   KListViewItem *tmp_l = new KListViewItem(lv_l,
00301     entry->name(), entry->version(), entry->releaseDate().toString());
00302 
00303   tmp_r->setPixmap(0, pix);
00304   tmp_d->setPixmap(0, pix);
00305   tmp_l->setPixmap(0, pix);
00306 
00307   m_entries.append(entry);
00308 
00309   kdDebug() << "added entry " << entry->name() << endl;
00310 }
00311 
00312 void DownloadDialog::slotData(KIO::Job *job, const QByteArray &a)
00313 {
00314   QCString tmp(a, a.size() + 1);
00315   m_data[job].append(QString::fromUtf8(tmp));
00316 }
00317 
00318 void DownloadDialog::slotDetails()
00319 {
00320   Entry *e = getEntry();
00321   if(!e) return;
00322 
00323   QString info = i18n
00324   (
00325     "Name: %1\n"
00326     "Author: %2\n"
00327     "License: %3\n"
00328     "Version: %4\n"
00329     "Release: %5\n"
00330     "Rating: %6\n"
00331     "Downloads: %7\n"
00332     "Release date: %8\n"
00333     "Summary: %9\n"
00334     ).arg(e->name()
00335     ).arg(e->author()
00336     ).arg(e->license()
00337     ).arg(e->version()
00338     ).arg(e->release()
00339     ).arg(e->rating()
00340     ).arg(e->downloads()
00341     ).arg(e->releaseDate().toString()
00342     ).arg(e->summary()
00343   );
00344 
00345   info.append(i18n
00346   (
00347     "Preview: %1\n"
00348     "Payload: %2\n"
00349     ).arg(e->preview().url()
00350     ).arg(e->payload().url()
00351   ));
00352 
00353   KMessageBox::information(this, info, i18n("Details"));
00354 }
00355 
00356 void DownloadDialog::slotInstall()
00357 {
00358   Entry *e = getEntry();
00359   if(!e) return;
00360 
00361   kdDebug() << "download entry now" << endl;
00362 
00363   if(m_engine)
00364   {
00365     install(e);
00366     m_engine->download(e);
00367   }
00368   else
00369   {
00370     m_s = new KNewStuffGeneric(e->type(), this);
00371 
00372     m_entry = e;
00373 
00374     KURL source = e->payload();
00375     KURL dest = KURL(m_s->downloadDestination(e));
00376 
00377     KIO::FileCopyJob *job = KIO::file_copy(source, dest, -1, true);
00378     connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotInstalled(KIO::Job*)));
00379   }
00380 }
00381 
00382 void DownloadDialog::install(Entry *e)
00383 {
00384   kapp->config()->setGroup("KNewStuffStatus");
00385   kapp->config()->writeEntry(m_entryname, e->releaseDate().toString());
00386   kapp->config()->sync();
00387 
00388   QPixmap pix = KGlobal::iconLoader()->loadIcon("ok", KIcon::Small);
00389   m_entryitem = lv_r->findItem(m_entryname, 0);
00390   if(m_entryitem) m_entryitem->setPixmap(0, pix);
00391   m_entryitem = lv_d->findItem(m_entryname, 0);
00392   if(m_entryitem) m_entryitem->setPixmap(0, pix);
00393   m_entryitem = lv_l->findItem(m_entryname, 0);
00394   if(m_entryitem) m_entryitem->setPixmap(0, pix);
00395 }
00396 
00397 void DownloadDialog::slotInstalled(KIO::Job *job)
00398 {
00399   bool ret = (job->error() == 0);
00400   KIO::FileCopyJob *cjob;
00401 
00402   if(ret)
00403   {
00404     cjob = static_cast<KIO::FileCopyJob*>(job);
00405     if(cjob)
00406     {
00407       ret = m_s->install(cjob->destURL().path());
00408     }
00409     else ret = false;
00410   }
00411 
00412   if(ret)
00413   {
00414     install(m_entry);
00415 
00416     KMessageBox::information(this, i18n("Installation successful."), i18n("Installation"));
00417   }
00418   else KMessageBox::error(this, i18n("Installation failed."), i18n("Installation"));
00419 
00420   delete m_s;
00421 }
00422 
00423 void DownloadDialog::slotTab(int tab)
00424 {
00425   kdDebug() << "switch tab to: " << tab << endl;
00426   m_curtab = tab;
00427 }
00428 
00429 void DownloadDialog::slotSelected()
00430 {
00431   QString tmp;
00432   Entry *e = getEntry();
00433   if(e)
00434   {
00435     if(!e->preview().isValid())
00436     {
00437       m_rt->setText(QString("<b>%1</b><br>%2<br>%3<br><br><i>%4</i><br>(%5)").arg(
00438         e->name()).arg(e->author()).arg(e->releaseDate().toString()).arg(e->summary()).arg(e->license()));
00439     }
00440     else
00441     {
00442       KIO::NetAccess::download(e->preview(), tmp, this);
00443       m_rt->setText(QString("<b>%1</b><br>%2<br>%3<br><br><img src='%4'><br><i>%5</i><br>(%6)").arg(
00444         e->name()).arg(e->author()).arg(e->releaseDate().toString()).arg(tmp).arg(e->summary()).arg(e->license()));
00445     }
00446   }
00447 }
00448 
00449 Entry *DownloadDialog::getEntry()
00450 {
00451   if(m_curtab == 0) m_entryitem = lv_r->currentItem();
00452   else if(m_curtab == 1) m_entryitem = lv_d->currentItem();
00453   else if(m_curtab == 2) m_entryitem = lv_l->currentItem();
00454   else return 0;
00455 
00456   QPushButton *de, *in;
00457   in = *(m_buttons[m_page]->at(0));
00458   de = *(m_buttons[m_page]->at(1));
00459   if(in) in->setEnabled(true);
00460   if(de) de->setEnabled(true);
00461 
00462   m_entryname = m_entryitem->text(0);
00463 
00464   for(Entry *e = m_entries.first(); e; e = m_entries.next())
00465   {
00466     if(e->name() == m_entryname) return e;
00467   }
00468 
00469   return 0;
00470 }
00471 
00472 void DownloadDialog::slotPage(QWidget *w)
00473 {
00474   Provider *p;
00475 
00476   kdDebug() << "changed widget!!!" << endl;
00477 
00478   if(m_map.find(w) == m_map.end()) return;
00479 
00480   m_page = w;
00481 
00482   lv_r = *(m_map[w]->at(0));
00483   lv_d = *(m_map[w]->at(1));
00484   lv_l = *(m_map[w]->at(2));
00485   p = m_providers[w];
00486   m_rt = m_rts[w];
00487 
00488   kdDebug() << "valid change!!!; lv_r = " << lv_r << endl;
00489 
00490   if(m_engine) return;
00491 
00492   if(!m_filter.isEmpty()) return;
00493 
00494   lv_r->clear();
00495   lv_d->clear();
00496   lv_l->clear();
00497 
00498   kdDebug() << "-- fetch -- " << p->downloadUrl() << endl;
00499 
00500   loadProvider(p);
00501 }
00502 
00503 void DownloadDialog::loadProvider(Provider *p)
00504 {
00505   KIO::TransferJob *job = KIO::get(p->downloadUrl());
00506 
00507   m_jobs[job] = p;
00508 
00509   connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)));
00510   connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
00511     SLOT(slotData(KIO::Job*, const QByteArray&)));
00512 }
00513 
00514 void DownloadDialog::setType(QString type)
00515 {
00516   m_filter = type;
00517 }
00518 
00519 void DownloadDialog::slotOk()
00520 {
00521 }
00522 
00523 void DownloadDialog::slotApply()
00524 {
00525 }
00526 
00527 void DownloadDialog::open(QString type)
00528 {
00529   DownloadDialog d;
00530   d.setType(type);
00531   d.load();
00532   d.exec();
00533 }
00534 
00535 void DownloadDialog::slotFinish()
00536 {
00537   showPage(1);
00538   //updateBackground();
00539 }
00540 
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:49:26 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003