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., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "downloaddialog.h"
00022 #include "downloaddialog.moc"
00023 
00024 #include <klocale.h>
00025 #include <klistview.h>
00026 #include <kdebug.h>
00027 #include <kio/job.h>
00028 #include <kio/netaccess.h>
00029 #include <kmessagebox.h>
00030 #include <kurl.h>
00031 #include <kconfig.h>
00032 #include <kapplication.h>
00033 #include <kiconloader.h>
00034 
00035 #include <knewstuff/entry.h>
00036 #include <knewstuff/knewstuffgeneric.h>
00037 #include <knewstuff/engine.h>
00038 
00039 #include <qlayout.h>
00040 #include <qpushbutton.h>
00041 #include <qdom.h>
00042 #include <qlabel.h>
00043 #include <qtextbrowser.h>
00044 #include <qtabwidget.h>
00045 #include <qtimer.h> // hack
00046 
00047 using namespace KNS;
00048 
00049 struct DownloadDialog::Private
00050 {
00051     QString m_providerlist;
00052     QWidget *m_page;
00053     KListView *m_lvtmp_r, *m_lvtmp_d, *m_lvtmp_l;
00054     QPtrList<Entry> m_installlist;
00055     QMap<KIO::Job*, Provider*> m_variantjobs;
00056     QMap<KIO::Job*, QStringList> m_variants;
00057     QMap<Provider*, Provider*> m_newproviders;
00058 };
00059 
00060 class NumSortListViewItem : public KListViewItem
00061 {
00062   public:
00063   NumSortListViewItem( QListView * parent, QString label1, QString label2 = QString::null, QString label3 = QString::null, QString label4 = QString::null, QString label5 = QString::null, QString label6 = QString::null, QString label7 = QString::null, QString label8 = QString::null )  :
00064   KListViewItem( parent, label1, label2, label3, label4, label5, label6, label7, label8 )
00065   {
00066   }
00067 
00068   QString key(int col, bool asc) const {
00069     if (col == 2)
00070     {
00071       QString s;
00072       s.sprintf("%08d", text(col).toInt());
00073       return s;
00074     }
00075     return KListViewItem::key( col, asc );
00076   }
00077 };
00078 
00079 class DateSortListViewItem : public KListViewItem
00080 {
00081   public:
00082   DateSortListViewItem( QListView * parent, QString label1, QString label2 = QString::null, QString label3 = QString::null, QString label4 = QString::null, QString label5 = QString::null, QString label6 = QString::null, QString label7 = QString::null, QString label8 = QString::null )  :
00083   KListViewItem( parent, label1, label2, label3, label4, label5, label6, label7, label8 )
00084   {
00085   }
00086 
00087   QString key(int col, bool asc) const {
00088     if (col == 2)
00089     {
00090       QString s;
00091       QDate date = KGlobal::locale()->readDate(text(col));
00092       s.sprintf("%08d", date.year() * 366 + date.dayOfYear());
00093       return s;
00094     }
00095     return KListViewItem::key( col, asc );
00096   }
00097 };
00098 
00099 // BEGIN deprecated for KDE 4
00100 DownloadDialog::DownloadDialog(Engine *engine, QWidget *)
00101 : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
00102   KDialogBase::Close, KDialogBase::Close)
00103 {
00104   init(engine);
00105 }
00106 
00107 DownloadDialog::DownloadDialog(QWidget *)
00108 : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
00109   KDialogBase::Close, KDialogBase::Close)
00110 {
00111   init(0);
00112 }
00113 
00114 void DownloadDialog::open(QString type)
00115 {
00116   DownloadDialog d;
00117   d.setType(type);
00118   d.load();
00119   d.exec();
00120 }
00121 // END deprecated for KDE 4
00122 
00123 DownloadDialog::DownloadDialog(Engine *engine, QWidget *, const QString& caption)
00124 : KDialogBase(KDialogBase::IconList, (caption.isNull() ? i18n("Get Hot New Stuff") : caption),
00125   KDialogBase::Close, KDialogBase::Close)
00126 {
00127   init(engine);
00128 }
00129 
00130 DownloadDialog::DownloadDialog(QWidget *, const QString& caption)
00131 : KDialogBase(KDialogBase::IconList, (caption.isNull() ? i18n("Get Hot New Stuff") : caption),
00132   KDialogBase::Close, KDialogBase::Close)
00133 {
00134   init(0);
00135 }
00136 
00137 void DownloadDialog::init(Engine *engine)
00138 {
00139   resize(700, 400);
00140   d = new Private();
00141 
00142   m_engine = engine;
00143   d->m_page = NULL;
00144 
00145   connect(this, SIGNAL(aboutToShowPage(QWidget*)), SLOT(slotPage(QWidget*)));
00146 
00147   if(!engine)
00148   {
00149     m_loader = new ProviderLoader(this);
00150     connect(m_loader, SIGNAL(providersLoaded(Provider::List*)), SLOT(slotProviders(Provider::List*)));
00151   }
00152 }
00153 
00154 DownloadDialog::~DownloadDialog()
00155 {
00156     delete d;
00157 }
00158 
00159 void DownloadDialog::load()
00160 {
00161   m_loader->load(m_filter, d->m_providerlist);
00162 }
00163 
00164 void DownloadDialog::load(QString providerList)
00165 {
00166   m_loader->load(m_filter, providerList);
00167 }
00168 
00169 void DownloadDialog::clear()
00170 {
00171   QMap<QWidget*, QValueList<KListView*>* >::Iterator it;
00172   QMap<QWidget*, QValueList<KListView*>* >::Iterator end(m_map.end());
00173   for(it = m_map.begin(); it != end; ++it)
00174   {
00175     QValueList<KListView*> *v = it.data();
00176     kdDebug() << "clear listviews in " << v << endl;
00177     if(v)
00178     {
00179       (*(v->at(0)))->clear();
00180       (*(v->at(1)))->clear();
00181       (*(v->at(2)))->clear();
00182 
00183       //delete (*it);
00184     }
00185 
00186     delete it.key();
00187   }
00188   m_map.clear();
00189 }
00190 
00191 void DownloadDialog::slotProviders(Provider::List *list)
00192 {
00193   Provider *p;
00194   /*QFrame *frame;*/
00195 
00196   for(p = list->first(); p; p = list->next())
00197   {
00198     kdDebug() << "++ provider ++ " << p->name() << endl;
00199 
00200     if(!m_filter.isEmpty())
00201       loadProvider(p);
00202     else
00203       addProvider(p);
00204     /*if(p == list->getFirst())
00205       slotPage(m_frame);*/ // only if !qtbug
00206   }
00207 }
00208 
00209 void DownloadDialog::addProvider(Provider *p)
00210 {
00211   QFrame *frame;
00212   QTabWidget *ctl;
00213   QWidget *w_d, *w_r, *w_l;
00214   QWidget *w2;
00215   QTextBrowser *rt;
00216   QString tmp;
00217   int ret;
00218   QPixmap pix;
00219 
00220   if(m_map.count() == 0)
00221   {
00222     frame = addPage(i18n("Welcome"), i18n("Welcome"), QPixmap(""));
00223     delete frame;
00224   }
00225 
00226   kdDebug() << "addProvider()/begin" << endl;
00227 
00228   ret = true;
00229   if(p->icon().path().isEmpty()) ret = false;
00230   else
00231   {
00232     if(!p->icon().protocol().isEmpty())
00233     {
00234       ret = KIO::NetAccess::download(p->icon(), tmp, this);
00235       if(ret) pix = QPixmap(tmp);
00236     }
00237     else
00238     {
00239       pix = KGlobal::iconLoader()->loadIcon(p->icon().path(), KIcon::Panel);
00240       ret = true;
00241     }
00242   }
00243   if(!ret) pix = KGlobal::iconLoader()->loadIcon("knewstuff", KIcon::Panel);
00244   frame = addPage(p->name(), p->name(), pix);
00245   m_frame = frame;
00246 
00247   w2 = new QWidget(frame);
00248   w_d = new QWidget(frame);
00249   w_r = new QWidget(frame);
00250   w_l = new QWidget(frame);
00251 
00252   ctl = new QTabWidget(frame);
00253   ctl->addTab(w_r, i18n("Highest Rated"));
00254   ctl->addTab(w_d, i18n("Most Downloads"));
00255   ctl->addTab(w_l, i18n("Latest"));
00256 
00257   m_curtab = 0;
00258   connect(ctl, SIGNAL(currentChanged(QWidget *)), SLOT(slotTab()));
00259 
00260   QHBoxLayout *box = new QHBoxLayout(frame);
00261   box->add(ctl);
00262 
00263   d->m_lvtmp_r = new KListView(w_r);
00264   d->m_lvtmp_r->addColumn(i18n("Name"));
00265   d->m_lvtmp_r->addColumn(i18n("Version"));
00266   d->m_lvtmp_r->addColumn(i18n("Rating"));
00267   d->m_lvtmp_r->setSorting(2, false);
00268 
00269   d->m_lvtmp_d = new KListView(w_d);
00270   d->m_lvtmp_d->addColumn(i18n("Name"));
00271   d->m_lvtmp_d->addColumn(i18n("Version"));
00272   d->m_lvtmp_d->addColumn(i18n("Downloads"));
00273   d->m_lvtmp_d->setSorting(2, false);
00274 
00275   d->m_lvtmp_l = new KListView(w_l);
00276   d->m_lvtmp_l->addColumn(i18n("Name"));
00277   d->m_lvtmp_l->addColumn(i18n("Version"));
00278   d->m_lvtmp_l->addColumn(i18n("Release Date"));
00279   d->m_lvtmp_l->setSorting(2, false);
00280 
00281   connect(d->m_lvtmp_r, SIGNAL(clicked(QListViewItem*)), SLOT(slotSelected()));
00282   connect(d->m_lvtmp_d, SIGNAL(clicked(QListViewItem*)), SLOT(slotSelected()));
00283   connect(d->m_lvtmp_l, SIGNAL(clicked(QListViewItem*)), SLOT(slotSelected()));
00284 
00285   rt = new QTextBrowser(frame);
00286   rt->setMinimumWidth(150);
00287 
00288   QPushButton *in = new QPushButton(i18n("Install"), frame);
00289   QPushButton *de = new QPushButton(i18n("Details"), frame);
00290   in->setEnabled(false);
00291   de->setEnabled(false);
00292 
00293   box->addSpacing(spacingHint());
00294   QVBoxLayout *vbox = new QVBoxLayout(box);
00295   vbox->add(rt);
00296   vbox->addSpacing(spacingHint());
00297   vbox->add(de);
00298   vbox->add(in);
00299 
00300   connect(rt, SIGNAL(linkClicked(const QString&)), SLOT(slotEmail(const QString&)));
00301 
00302   connect(in, SIGNAL(clicked()), SLOT(slotInstall()));
00303   connect(de, SIGNAL(clicked()), SLOT(slotDetails()));
00304 
00305   QVBoxLayout *box2 = new QVBoxLayout(w_r);
00306   box2->add(d->m_lvtmp_r);
00307   QVBoxLayout *box3 = new QVBoxLayout(w_d);
00308   box3->add(d->m_lvtmp_d);
00309   QVBoxLayout *box4 = new QVBoxLayout(w_l);
00310   box4->add(d->m_lvtmp_l);
00311 
00312   QValueList<KListView*> *v = new QValueList<KListView*>;
00313   *v << d->m_lvtmp_r << d->m_lvtmp_d << d->m_lvtmp_l;
00314   m_map[frame] = v;
00315   m_rts[frame] = rt;
00316   QValueList<QPushButton*> *vb = new QValueList<QPushButton*>;
00317   *vb << in << de;
00318   m_buttons[frame] = vb;
00319   m_providers[frame] = p;
00320 
00321   kdDebug() << "addProvider()/end; d->m_lvtmp_r = " << d->m_lvtmp_r << endl;
00322 
00323   if(m_engine) slotPage(frame);
00324 
00325   QTimer::singleShot(100, this, SLOT(slotFinish()));
00326 }
00327 
00328 void DownloadDialog::slotResult(KIO::Job *job)
00329 {
00330   QDomDocument dom;
00331   QDomElement knewstuff;
00332 
00333   kdDebug() << "got data: " << m_data[job] << endl;
00334 
00335   kapp->config()->setGroup("KNewStuffStatus");
00336 
00337   dom.setContent(m_data[job]);
00338   knewstuff = dom.documentElement();
00339 
00340   for(QDomNode pn = knewstuff.firstChild(); !pn.isNull(); pn = pn.nextSibling())
00341   {
00342     QDomElement stuff = pn.toElement();
00343 
00344     kdDebug() << "element: " << stuff.tagName() << endl;
00345 
00346     if(stuff.tagName() == "stuff")
00347     {
00348       Entry *entry = new Entry(stuff);
00349       kdDebug() << "TYPE::" << entry->type() << " FILTER::" << m_filter << endl;
00350       if(!entry->type().isEmpty())
00351       {
00352         if((!m_filter.isEmpty()) && (entry->type() != m_filter)) continue;
00353       }
00354 
00355       /*if((!m_filter.isEmpty()) && (m_jobs[job]))
00356       {
00357         Provider *p = m_jobs[job];
00358         if(d->m_newproviders[p])
00359         {
00360           addProvider(p);
00361           slotPage(m_frame);
00362           d->m_newproviders[p] = 0;
00363         }
00364       }*/
00365       if((!m_filter.isEmpty()) && (d->m_variantjobs[job]))
00366       {
00367         Provider *p = d->m_variantjobs[job];
00368         if(d->m_newproviders[p])
00369         {
00370           addProvider(p);
00371           slotPage(m_frame);
00372           d->m_newproviders[p] = 0;
00373         }
00374       }
00375 
00376       /*if(m_jobs[job]) addEntry(entry);
00377       else*/
00378       if(d->m_variantjobs[job]) addEntry(entry, d->m_variants[job]);
00379     }
00380   }
00381 }
00382 
00383 int DownloadDialog::installStatus(Entry *entry)
00384 {
00385   QDate date;
00386   QString datestring;
00387   int installed;
00388 
00389   QString lang = KGlobal::locale()->language();
00390 
00391   kapp->config()->setGroup("KNewStuffStatus");
00392   datestring = kapp->config()->readEntry(entry->name(lang));
00393   if(datestring.isNull()) installed = 0;
00394   else
00395   {
00396     date = QDate::fromString(datestring, Qt::ISODate);
00397     if(!date.isValid()) installed = 0;
00398     else if(date < entry->releaseDate()) installed = -1;
00399     else installed = 1;
00400   }
00401 
00402   return installed;
00403 }
00404 
00405 void DownloadDialog::addEntry(Entry *entry, const QStringList& variants)
00406 {
00407   QPixmap pix;
00408   int installed;
00409 
00410   installed = installStatus(entry);
00411 
00412   if(installed > 0) pix = KGlobal::iconLoader()->loadIcon("ok", KIcon::Small);
00413   else if(installed < 0) pix = KGlobal::iconLoader()->loadIcon("history", KIcon::Small);
00414   else pix = QPixmap();
00415 
00416   QString lang = KGlobal::locale()->language();
00417 
00418   if(variants.contains("score"))
00419   {
00420     KListViewItem *tmp_r = new NumSortListViewItem(lv_r,
00421       entry->name(lang), entry->version(), QString("%1").arg(entry->rating()));
00422     tmp_r->setPixmap(0, pix);
00423   }
00424   if(variants.contains("downloads"))
00425   {
00426     KListViewItem *tmp_d = new NumSortListViewItem(lv_d,
00427       entry->name(lang), entry->version(), QString("%1").arg(entry->downloads()));
00428     tmp_d->setPixmap(0, pix);
00429   }
00430   if(variants.contains("latest"))
00431   {
00432     KListViewItem *tmp_l = new DateSortListViewItem(lv_l,
00433       entry->name(lang), entry->version(), KGlobal::locale()->formatDate(entry->releaseDate()));
00434     tmp_l->setPixmap(0, pix);
00435   }
00436 
00437   m_entries.append(entry);
00438 
00439   kdDebug() << "added entry " << entry->name() << " for variants " << variants << endl;
00440 }
00441 
00442 void DownloadDialog::addEntry(Entry *entry)
00443 {
00444   QStringList variants;
00445 
00446   variants << "score";
00447   variants << "downloads";
00448   variants << "latest";
00449 
00450   addEntry(entry, variants);
00451 
00452   // not used anymore due to variants (but still used by engine)
00453   kdDebug() << "added entry " << entry->name() << endl;
00454 }
00455 
00456 void DownloadDialog::slotData(KIO::Job *job, const QByteArray &a)
00457 {
00458   QCString tmp(a, a.size() + 1);
00459   m_data[job].append(QString::fromUtf8(tmp));
00460 }
00461 
00462 void DownloadDialog::slotDetails()
00463 {
00464   Entry *e = getEntry();
00465   if(!e) return;
00466 
00467   QString lang = KGlobal::locale()->language();
00468 
00469   QString info = i18n
00470   (
00471     "Name: %1\n"
00472     "Author: %2\n"
00473     "License: %3\n"
00474     "Version: %4\n"
00475     "Release: %5\n"
00476     "Rating: %6\n"
00477     "Downloads: %7\n"
00478     "Release date: %8\n"
00479     "Summary: %9\n"
00480     ).arg(e->name(lang)
00481     ).arg(e->author()
00482     ).arg(e->license()
00483     ).arg(e->version()
00484     ).arg(e->release()
00485     ).arg(e->rating()
00486     ).arg(e->downloads()
00487     ).arg(KGlobal::locale()->formatDate(e->releaseDate())
00488     ).arg(e->summary(lang)
00489   );
00490 
00491   info.append(i18n
00492   (
00493     "Preview: %1\n"
00494     "Payload: %2\n"
00495     ).arg(e->preview().url()
00496     ).arg(e->payload().url()
00497   ));
00498 
00499   KMessageBox::information(this, info, i18n("Details"));
00500 }
00501 
00502 QListViewItem *DownloadDialog::currentEntryItem()
00503 {
00504   if((m_curtab == 0) && (lv_r->selectedItem())) return lv_r->selectedItem();
00505   if((m_curtab == 1) && (lv_d->selectedItem())) return lv_d->selectedItem();
00506   if((m_curtab == 2) && (lv_l->selectedItem())) return lv_l->selectedItem();
00507 
00508   return 0;
00509 }
00510 
00511 void DownloadDialog::slotInstall()
00512 {
00513   Entry *e = getEntry();
00514   if(!e) return;
00515 
00516   d->m_lvtmp_r->setEnabled( false );
00517   d->m_lvtmp_l->setEnabled( false );
00518   d->m_lvtmp_d->setEnabled( false );
00519   m_entryitem = currentEntryItem();
00520   m_entryname = m_entryitem->text(0);
00521 
00522   kdDebug() << "download entry now" << endl;
00523 
00524   if(m_engine)
00525   {
00526     m_engine->download(e);
00527     install(e);
00528   }
00529   else
00530   {
00531     m_s = new KNewStuffGeneric(e->type(), this);
00532     m_entry = e;
00533     KURL source = e->payload();
00534     KURL dest = KURL(m_s->downloadDestination(e));
00535 
00536     KIO::FileCopyJob *job = KIO::file_copy(source, dest, -1, true);
00537     connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotInstalled(KIO::Job*)));
00538   }
00539 }
00540 
00541 void DownloadDialog::install(Entry *e)
00542 {
00543   kapp->config()->setGroup("KNewStuffStatus");
00544   kapp->config()->writeEntry(m_entryname, e->releaseDate().toString(Qt::ISODate));
00545   kapp->config()->sync();
00546 
00547   QPixmap pix = KGlobal::iconLoader()->loadIcon("ok", KIcon::Small);
00548 
00549   QString lang = KGlobal::locale()->language();
00550   
00551   if(m_entryitem)
00552   {
00553     m_entryitem->setPixmap(0, pix);
00554 
00555     QListViewItem *item;
00556     item = lv_r->findItem(e->name(lang), 0);
00557     if(item) item->setPixmap(0, pix);
00558     item = lv_d->findItem(e->name(lang), 0);
00559     if(item) item->setPixmap(0, pix);
00560     item = lv_l->findItem(e->name(lang), 0);
00561     if(item) item->setPixmap(0, pix);
00562   }
00563 
00564   if(currentEntryItem() == m_entryitem)
00565   {
00566     QPushButton *in;
00567     in = *(m_buttons[d->m_page]->at(0));
00568     if(in) in->setEnabled(false);
00569   }
00570 
00571   d->m_installlist.append(e);
00572   d->m_lvtmp_r->setEnabled( true );
00573   d->m_lvtmp_l->setEnabled( true );
00574   d->m_lvtmp_d->setEnabled( true );
00575 }
00576 
00577 void DownloadDialog::slotInstalled(KIO::Job *job)
00578 {
00579   bool ret = job && (job->error() == 0);
00580   if(ret)
00581   {
00582     KIO::FileCopyJob *cjob = ::qt_cast<KIO::FileCopyJob*>(job);
00583     if(cjob)
00584     {
00585       ret = m_s->install(cjob->destURL().path());
00586     }
00587     else ret = false;
00588   }
00589 
00590   if(ret)
00591   {
00592     install(m_entry);
00593 
00594     KMessageBox::information(this, i18n("Installation successful."), i18n("Installation"));
00595   }
00596   else KMessageBox::error(this, i18n("Installation failed."), i18n("Installation"));
00597   d->m_lvtmp_r->setEnabled( true );
00598   d->m_lvtmp_l->setEnabled( true );
00599   d->m_lvtmp_d->setEnabled( true );
00600 
00601   delete m_s;
00602 }
00603 
00604 void DownloadDialog::slotTab()
00605 {
00606   int tab = static_cast<const QTabWidget *>(sender())->currentPageIndex();
00607   kdDebug() << "switch tab to: " << tab << endl;
00608 
00609   Entry *eold = getEntry();
00610   m_curtab = tab;
00611   Entry *e = getEntry();
00612 
00613   if(e == eold) return;
00614 
00615   if(e)
00616   {
00617     slotSelected();
00618   }
00619   else
00620   {
00621     QPushButton *de, *in;
00622     in = *(m_buttons[d->m_page]->at(0));
00623     de = *(m_buttons[d->m_page]->at(1));
00624 
00625     if(in) in->setEnabled(false);
00626     if(de) de->setEnabled(false);
00627 
00628     m_rt->clear();
00629   }
00630 }
00631 
00632 void DownloadDialog::slotSelected()
00633 {
00634   QString tmp;
00635   bool enabled;
00636   Entry *e = getEntry();
00637   QString lang = KGlobal::locale()->language();
00638   bool ret;
00639 
00640   if(e)
00641   {
00642     QString lang = KGlobal::locale()->language();
00643 
00644     QListViewItem *item;
00645     if(m_curtab != 0)
00646     {
00647       lv_r->clearSelection();
00648       item = lv_r->findItem(e->name(lang), 0);
00649       if(item) lv_r->setSelected(item, true);
00650     }
00651     if(m_curtab != 1)
00652     {
00653       lv_d->clearSelection();
00654       item = lv_d->findItem(e->name(lang), 0);
00655       if(item) lv_d->setSelected(item, true);
00656     }
00657     if(m_curtab != 2)
00658     {
00659       lv_l->clearSelection();
00660       item = lv_l->findItem(e->name(lang), 0);
00661       if(item) lv_l->setSelected(item, true);
00662     }
00663 
00664     if(!e->preview(lang).isValid())
00665     {
00666       ret = 0;
00667     }
00668     else
00669     {
00670       ret = KIO::NetAccess::download(e->preview(lang), tmp, this);
00671     }
00672 
00673     QString desc = QString("<b>%1</b><br>").arg(e->name(lang));
00674     if(!e->authorEmail().isNull())
00675     {
00676       desc += QString("<a href='mailto:" + e->authorEmail() + "'>" + e->author() + "</a>");
00677     }
00678     else
00679     {
00680       desc += QString("%1").arg(e->author());
00681     }
00682     desc += QString("<br>%1").arg(KGlobal::locale()->formatDate(e->releaseDate()));
00683     desc += QString("<br><br>");
00684     if(ret)
00685     {
00686       desc += QString("<img src='%1'>").arg(tmp);
00687     }
00688     else
00689     {
00690       desc += i18n("Preview not available.");
00691     }
00692     desc += QString("<br><i>%1</i>").arg(e->summary(lang));
00693     desc += QString("<br>(%1)").arg(e->license());
00694 
00695     m_rt->clear();
00696     m_rt->setText(desc);
00697 
00698     if(installStatus(e) == 1) enabled = false;
00699     else enabled = true;
00700 
00701     QPushButton *de, *in;
00702     in = *(m_buttons[d->m_page]->at(0));
00703     de = *(m_buttons[d->m_page]->at(1));
00704     if(in) in->setEnabled(enabled);
00705     if(de) de->setEnabled(true);
00706   }
00707 }
00708 
00709 void DownloadDialog::slotEmail(const QString& link)
00710 {
00711   kdDebug() << "EMAIL: " << link << endl;
00712   kapp->invokeMailer(link);
00713   slotSelected(); // QTextBrowser oddity workaround as it cannot handle mailto: URLs
00714 }
00715 
00716 Entry *DownloadDialog::getEntry()
00717 {
00718   QListViewItem *entryItem = currentEntryItem();
00719 
00720   if(!entryItem)
00721     return 0;
00722 
00723   QString entryName = entryItem->text(0);
00724 
00725   QString lang = KGlobal::locale()->language();
00726 
00727   for(Entry *e = m_entries.first(); e; e = m_entries.next())
00728     if(e->name(lang) == entryName)
00729       return e;
00730 
00731   return 0;
00732 }
00733 
00734 void DownloadDialog::slotPage(QWidget *w)
00735 {
00736   Provider *p;
00737 
00738   kdDebug() << "changed widget!!!" << endl;
00739 
00740   if(m_map.find(w) == m_map.end()) return;
00741 
00742   d->m_page = w;
00743 
00744   lv_r = *(m_map[w]->at(0));
00745   lv_d = *(m_map[w]->at(1));
00746   lv_l = *(m_map[w]->at(2));
00747   p = m_providers[w];
00748   m_rt = m_rts[w];
00749 
00750   kdDebug() << "valid change!!!; lv_r = " << lv_r << endl;
00751 
00752   if(m_engine) return;
00753 
00754   if(!m_filter.isEmpty()) return;
00755 
00756   lv_r->clear();
00757   lv_d->clear();
00758   lv_l->clear();
00759 
00760   kdDebug() << "-- fetch -- " << p->downloadUrl() << endl;
00761 
00762   loadProvider(p);
00763 }
00764 
00765 void DownloadDialog::loadProvider(Provider *p)
00766 {
00767   QMap<KIO::Job*, Provider*>::Iterator it;
00768 
00769   for(it = d->m_variantjobs.begin(); it != d->m_variantjobs.end(); it++)
00770   {
00771     if(it.data() == p)
00772     {
00773       kdDebug() << "-- found provider data in cache" << endl;
00774       slotResult(it.key());
00775       return;
00776     }
00777   }
00778 
00779   QStringList variants;
00780   variants << "score";
00781   variants << "downloads";
00782   variants << "latest";
00783 
00784   // Optimise URLs so each unique URL only gets fetched once
00785 
00786   QMap<QString, QStringList> urls;
00787 
00788   for(QStringList::Iterator it = variants.begin(); it != variants.end(); it++)
00789   {
00790     QString url = p->downloadUrlVariant((*it)).url();
00791     if(!urls.contains(url))
00792     {
00793       urls[url] = QStringList();
00794     }
00795     urls[url] << (*it);
00796 
00797     it = variants.remove(it);
00798   }
00799 
00800   // Now fetch the URLs while keeping the variant list for each attached
00801 
00802   for(QMap<QString, QStringList>::Iterator it = urls.begin(); it != urls.end(); it++)
00803   {
00804     QString url = it.key();
00805     QStringList urlvariants = it.data();
00806 
00807     KIO::TransferJob *variantjob = KIO::get(url);
00808     d->m_newproviders[p] = p;
00809     d->m_variantjobs[variantjob] = p;
00810     d->m_variants[variantjob] = urlvariants;
00811     m_data[variantjob] = "";
00812 
00813     connect(variantjob, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)));
00814     connect(variantjob, SIGNAL(data(KIO::Job*, const QByteArray&)),
00815       SLOT(slotData(KIO::Job*, const QByteArray&)));
00816   }
00817 
00818   if(variants.count() == 0) return;
00819 
00820   // If not all variants are given, use default URL for those
00821 
00822   kdDebug() << "-- reached old downloadurl section; variants left: " << variants.count() << endl;
00823 
00824   KIO::TransferJob *job = KIO::get(p->downloadUrl());
00825 
00826   d->m_newproviders[p] = p;
00827   d->m_variantjobs[job] = p;
00828   d->m_variants[job] = variants;
00829   //m_jobs[job] = p; // not used anymore due to variants
00830   m_data[job] = "";
00831 
00832   connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)));
00833   connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
00834     SLOT(slotData(KIO::Job*, const QByteArray&)));
00835 }
00836 
00837 void DownloadDialog::setType(QString type)
00838 {
00839   m_filter = type;
00840 }
00841 
00842 void DownloadDialog::setProviderList(const QString& providerList)
00843 {
00844   d->m_providerlist = providerList;
00845 }
00846 
00847 void DownloadDialog::slotOk()
00848 {
00849 }
00850 
00851 void DownloadDialog::slotApply()
00852 {
00853 }
00854 
00855 void DownloadDialog::open(const QString& type, const QString& caption)
00856 {
00857   DownloadDialog d(0, caption);
00858   d.setType(type);
00859   d.load();
00860   d.exec();
00861 }
00862 
00863 void DownloadDialog::slotFinish()
00864 {
00865   showPage(1);
00866   //updateBackground();
00867 }
00868 
00869 QPtrList<Entry> DownloadDialog::installedEntries()
00870 {
00871   return d->m_installlist;
00872 }
KDE Home | KDE Accessibility Home | Description of Access Keys