TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

downloaddialog.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. This file is part of KNewStuff.
  3. Copyright (c) 2003 Josef Spillner <spillner@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "downloaddialog.h"
  18. #include "downloaddialog.moc"
  19. #include <tdelocale.h>
  20. #include <tdelistview.h>
  21. #include <kdebug.h>
  22. #include <tdeio/job.h>
  23. #include <tdeio/netaccess.h>
  24. #include <tdemessagebox.h>
  25. #include <kurl.h>
  26. #include <tdeconfig.h>
  27. #include <tdeapplication.h>
  28. #include <kiconloader.h>
  29. #include <knewstuff/entry.h>
  30. #include <knewstuff/knewstuffgeneric.h>
  31. #include <knewstuff/engine.h>
  32. #include <tqlayout.h>
  33. #include <tqpushbutton.h>
  34. #include <tqdom.h>
  35. #include <tqlabel.h>
  36. #include <tqtextbrowser.h>
  37. #include <tqtabwidget.h>
  38. #include <tqtimer.h> // hack
  39. using namespace KNS;
  40. struct DownloadDialog::Private
  41. {
  42. TQString m_providerlist;
  43. TQWidget *m_page;
  44. TDEListView *m_lvtmp_r, *m_lvtmp_d, *m_lvtmp_l;
  45. TQPtrList<Entry> m_installlist;
  46. TQMap<TDEIO::Job*, Provider*> m_variantjobs;
  47. TQMap<TDEIO::Job*, TQStringList> m_variants;
  48. TQMap<Provider*, Provider*> m_newproviders;
  49. };
  50. class NumSortListViewItem : public TDEListViewItem
  51. {
  52. public:
  53. NumSortListViewItem( TQListView * parent, TQString label1, TQString label2 = TQString::null, TQString label3 = TQString::null, TQString label4 = TQString::null, TQString label5 = TQString::null, TQString label6 = TQString::null, TQString label7 = TQString::null, TQString label8 = TQString::null ) :
  54. TDEListViewItem( parent, label1, label2, label3, label4, label5, label6, label7, label8 )
  55. {
  56. }
  57. TQString key(int col, bool asc) const {
  58. if (col == 2)
  59. {
  60. TQString s;
  61. s.sprintf("%08d", text(col).toInt());
  62. return s;
  63. }
  64. return TDEListViewItem::key( col, asc );
  65. }
  66. };
  67. class DateSortListViewItem : public TDEListViewItem
  68. {
  69. public:
  70. DateSortListViewItem( TQListView * parent, TQString label1, TQString label2 = TQString::null, TQString label3 = TQString::null, TQString label4 = TQString::null, TQString label5 = TQString::null, TQString label6 = TQString::null, TQString label7 = TQString::null, TQString label8 = TQString::null ) :
  71. TDEListViewItem( parent, label1, label2, label3, label4, label5, label6, label7, label8 )
  72. {
  73. }
  74. TQString key(int col, bool asc) const {
  75. if (col == 2)
  76. {
  77. TQString s;
  78. TQDate date = TDEGlobal::locale()->readDate(text(col));
  79. s.sprintf("%08d", date.year() * 366 + date.dayOfYear());
  80. return s;
  81. }
  82. return TDEListViewItem::key( col, asc );
  83. }
  84. };
  85. // BEGIN deprecated for KDE 4
  86. DownloadDialog::DownloadDialog(Engine *engine, TQWidget *)
  87. : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
  88. KDialogBase::Close, KDialogBase::Close)
  89. {
  90. init(engine);
  91. }
  92. DownloadDialog::DownloadDialog(TQWidget *)
  93. : KDialogBase(KDialogBase::IconList, i18n("Get Hot New Stuff"),
  94. KDialogBase::Close, KDialogBase::Close)
  95. {
  96. init(0);
  97. }
  98. void DownloadDialog::open(TQString type)
  99. {
  100. DownloadDialog d;
  101. d.setType(type);
  102. d.load();
  103. d.exec();
  104. }
  105. // END deprecated for KDE 4
  106. DownloadDialog::DownloadDialog(Engine *engine, TQWidget *, const TQString& caption)
  107. : KDialogBase(KDialogBase::IconList, (caption.isNull() ? i18n("Get Hot New Stuff") : caption),
  108. KDialogBase::Close, KDialogBase::Close)
  109. {
  110. init(engine);
  111. }
  112. DownloadDialog::DownloadDialog(TQWidget *, const TQString& caption)
  113. : KDialogBase(KDialogBase::IconList, (caption.isNull() ? i18n("Get Hot New Stuff") : caption),
  114. KDialogBase::Close, KDialogBase::Close)
  115. {
  116. init(0);
  117. }
  118. void DownloadDialog::init(Engine *engine)
  119. {
  120. resize(700, 400);
  121. d = new Private();
  122. m_engine = engine;
  123. d->m_page = NULL;
  124. connect(this, TQT_SIGNAL(aboutToShowPage(TQWidget*)), TQT_SLOT(slotPage(TQWidget*)));
  125. if(!engine)
  126. {
  127. m_loader = new ProviderLoader(this);
  128. connect(m_loader, TQT_SIGNAL(providersLoaded(Provider::List*)), TQT_SLOT(slotProviders(Provider::List*)));
  129. }
  130. m_entries.setAutoDelete(true);
  131. }
  132. DownloadDialog::~DownloadDialog()
  133. {
  134. for (TQMap<TQWidget *, TQValueList<TQPushButton *>* >::const_iterator it = m_buttons.constBegin(); it != m_buttons.constEnd(); ++it)
  135. delete it.data();
  136. for (TQMap<TQWidget *, TQValueList<TDEListView *>* >::const_iterator it = m_map.constBegin(); it != m_map.constEnd(); ++it)
  137. delete it.data();
  138. delete d;
  139. }
  140. void DownloadDialog::load()
  141. {
  142. m_loader->load(m_filter, d->m_providerlist);
  143. }
  144. void DownloadDialog::load(TQString providerList)
  145. {
  146. m_loader->load(m_filter, providerList);
  147. }
  148. void DownloadDialog::clear()
  149. {
  150. TQMap<TQWidget*, TQValueList<TDEListView*>* >::Iterator it;
  151. TQMap<TQWidget*, TQValueList<TDEListView*>* >::Iterator end(m_map.end());
  152. for(it = m_map.begin(); it != end; ++it)
  153. {
  154. TQValueList<TDEListView*> *v = it.data();
  155. kdDebug() << "clear listviews in " << v << endl;
  156. if(v)
  157. {
  158. (*(v->at(0)))->clear();
  159. (*(v->at(1)))->clear();
  160. (*(v->at(2)))->clear();
  161. //delete (*it);
  162. }
  163. delete it.key();
  164. }
  165. m_map.clear();
  166. }
  167. void DownloadDialog::slotProviders(Provider::List *list)
  168. {
  169. Provider *p;
  170. /*TQFrame *frame;*/
  171. for(p = list->first(); p; p = list->next())
  172. {
  173. kdDebug() << "++ provider ++ " << p->name() << endl;
  174. if(!m_filter.isEmpty())
  175. loadProvider(p);
  176. else
  177. addProvider(p);
  178. /*if(p == list->getFirst())
  179. slotPage(m_frame);*/ // only if !qtbug
  180. }
  181. }
  182. void DownloadDialog::addProvider(Provider *p)
  183. {
  184. TQFrame *frame;
  185. TQTabWidget *ctl;
  186. TQWidget *w_d, *w_r, *w_l;
  187. TQWidget *w2;
  188. TQTextBrowser *rt;
  189. TQString tmp;
  190. int ret;
  191. TQPixmap pix;
  192. if(m_map.count() == 0)
  193. {
  194. frame = addPage(i18n("Welcome"), i18n("Welcome"), TQPixmap(TQString("")));
  195. delete frame;
  196. }
  197. kdDebug() << "addProvider()/begin" << endl;
  198. ret = true;
  199. if(p->icon().path().isEmpty()) ret = false;
  200. else
  201. {
  202. if(!p->icon().protocol().isEmpty())
  203. {
  204. ret = TDEIO::NetAccess::download(p->icon(), tmp, this);
  205. if(ret) pix = TQPixmap(tmp);
  206. }
  207. else
  208. {
  209. pix = TDEGlobal::iconLoader()->loadIcon(p->icon().path(), TDEIcon::Panel);
  210. ret = true;
  211. }
  212. }
  213. if(!ret) pix = TDEGlobal::iconLoader()->loadIcon("knewstuff", TDEIcon::Panel);
  214. frame = addPage(p->name(), p->name(), pix);
  215. m_frame = frame;
  216. w2 = new TQWidget(frame);
  217. w_d = new TQWidget(frame);
  218. w_r = new TQWidget(frame);
  219. w_l = new TQWidget(frame);
  220. ctl = new TQTabWidget(frame);
  221. ctl->addTab(w_r, i18n("Highest Rated"));
  222. ctl->addTab(w_d, i18n("Most Downloads"));
  223. ctl->addTab(w_l, i18n("Latest"));
  224. m_curtab = 0;
  225. connect(ctl, TQT_SIGNAL(currentChanged(TQWidget *)), TQT_SLOT(slotTab()));
  226. TQHBoxLayout *box = new TQHBoxLayout(frame);
  227. box->add(ctl);
  228. d->m_lvtmp_r = new TDEListView(w_r);
  229. d->m_lvtmp_r->addColumn(i18n("Name"));
  230. d->m_lvtmp_r->addColumn(i18n("Version"));
  231. d->m_lvtmp_r->addColumn(i18n("Rating"));
  232. d->m_lvtmp_r->setSorting(2, false);
  233. d->m_lvtmp_d = new TDEListView(w_d);
  234. d->m_lvtmp_d->addColumn(i18n("Name"));
  235. d->m_lvtmp_d->addColumn(i18n("Version"));
  236. d->m_lvtmp_d->addColumn(i18n("Downloads"));
  237. d->m_lvtmp_d->setSorting(2, false);
  238. d->m_lvtmp_l = new TDEListView(w_l);
  239. d->m_lvtmp_l->addColumn(i18n("Name"));
  240. d->m_lvtmp_l->addColumn(i18n("Version"));
  241. d->m_lvtmp_l->addColumn(i18n("Release Date"));
  242. d->m_lvtmp_l->setSorting(2, false);
  243. connect(d->m_lvtmp_r, TQT_SIGNAL(clicked(TQListViewItem*)), TQT_SLOT(slotSelected()));
  244. connect(d->m_lvtmp_d, TQT_SIGNAL(clicked(TQListViewItem*)), TQT_SLOT(slotSelected()));
  245. connect(d->m_lvtmp_l, TQT_SIGNAL(clicked(TQListViewItem*)), TQT_SLOT(slotSelected()));
  246. rt = new TQTextBrowser(frame);
  247. rt->setMinimumWidth(150);
  248. TQPushButton *in = new TQPushButton(i18n("Install"), frame);
  249. TQPushButton *de = new TQPushButton(i18n("Details"), frame);
  250. in->setEnabled(false);
  251. de->setEnabled(false);
  252. box->addSpacing(spacingHint());
  253. TQVBoxLayout *vbox = new TQVBoxLayout(box);
  254. vbox->add(rt);
  255. vbox->addSpacing(spacingHint());
  256. vbox->add(de);
  257. vbox->add(in);
  258. connect(rt, TQT_SIGNAL(linkClicked(const TQString&)), TQT_SLOT(slotEmail(const TQString&)));
  259. connect(in, TQT_SIGNAL(clicked()), TQT_SLOT(slotInstall()));
  260. connect(de, TQT_SIGNAL(clicked()), TQT_SLOT(slotDetails()));
  261. TQVBoxLayout *box2 = new TQVBoxLayout(w_r);
  262. box2->add(d->m_lvtmp_r);
  263. TQVBoxLayout *box3 = new TQVBoxLayout(w_d);
  264. box3->add(d->m_lvtmp_d);
  265. TQVBoxLayout *box4 = new TQVBoxLayout(w_l);
  266. box4->add(d->m_lvtmp_l);
  267. TQValueList<TDEListView*> *v = new TQValueList<TDEListView*>;
  268. *v << d->m_lvtmp_r << d->m_lvtmp_d << d->m_lvtmp_l;
  269. m_map[frame] = v;
  270. m_rts[frame] = rt;
  271. TQValueList<TQPushButton*> *vb = new TQValueList<TQPushButton*>;
  272. *vb << in << de;
  273. m_buttons[frame] = vb;
  274. m_providers[frame] = p;
  275. kdDebug() << "addProvider()/end; d->m_lvtmp_r = " << d->m_lvtmp_r << endl;
  276. if(m_engine) slotPage(frame);
  277. TQTimer::singleShot(100, this, TQT_SLOT(slotFinish()));
  278. }
  279. void DownloadDialog::slotResult(TDEIO::Job *job)
  280. {
  281. TQDomDocument dom;
  282. TQDomElement knewstuff;
  283. kdDebug() << "got data: " << m_data[job] << endl;
  284. kapp->config()->setGroup("KNewStuffStatus");
  285. dom.setContent(m_data[job]);
  286. knewstuff = dom.documentElement();
  287. for(TQDomNode pn = knewstuff.firstChild(); !pn.isNull(); pn = pn.nextSibling())
  288. {
  289. TQDomElement stuff = pn.toElement();
  290. kdDebug() << "element: " << stuff.tagName() << endl;
  291. if(stuff.tagName() == "stuff")
  292. {
  293. Entry *entry = new Entry(stuff);
  294. kdDebug() << "TYPE::" << entry->type() << " FILTER::" << m_filter << endl;
  295. if(!entry->type().isEmpty())
  296. {
  297. if((!m_filter.isEmpty()) && (entry->type() != m_filter)) continue;
  298. }
  299. /*if((!m_filter.isEmpty()) && (m_jobs[job]))
  300. {
  301. Provider *p = m_jobs[job];
  302. if(d->m_newproviders[p])
  303. {
  304. addProvider(p);
  305. slotPage(m_frame);
  306. d->m_newproviders[p] = 0;
  307. }
  308. }*/
  309. if((!m_filter.isEmpty()) && (d->m_variantjobs[job]))
  310. {
  311. Provider *p = d->m_variantjobs[job];
  312. if(d->m_newproviders[p])
  313. {
  314. addProvider(p);
  315. slotPage(m_frame);
  316. d->m_newproviders[p] = 0;
  317. }
  318. }
  319. /*if(m_jobs[job]) addEntry(entry);
  320. else*/
  321. if(d->m_variantjobs[job]) addEntry(entry, d->m_variants[job]);
  322. }
  323. }
  324. }
  325. int DownloadDialog::installStatus(Entry *entry)
  326. {
  327. TQDate date;
  328. TQString datestring;
  329. int installed;
  330. TQString lang = TDEGlobal::locale()->language();
  331. kapp->config()->setGroup("KNewStuffStatus");
  332. datestring = kapp->config()->readEntry(entry->name(lang));
  333. if(datestring.isNull()) installed = 0;
  334. else
  335. {
  336. date = TQT_TQDATE_OBJECT(TQDate::fromString(datestring, Qt::ISODate));
  337. if(!date.isValid()) installed = 0;
  338. else if(date < entry->releaseDate()) installed = -1;
  339. else installed = 1;
  340. }
  341. return installed;
  342. }
  343. void DownloadDialog::addEntry(Entry *entry, const TQStringList& variants)
  344. {
  345. TQPixmap pix;
  346. int installed;
  347. installed = installStatus(entry);
  348. if(installed > 0) pix = TDEGlobal::iconLoader()->loadIcon("ok", TDEIcon::Small);
  349. else if(installed < 0) pix = TDEGlobal::iconLoader()->loadIcon("history", TDEIcon::Small);
  350. else pix = TQPixmap();
  351. TQString lang = TDEGlobal::locale()->language();
  352. if(variants.contains("score"))
  353. {
  354. TDEListViewItem *tmp_r = new NumSortListViewItem(lv_r,
  355. entry->name(lang), entry->version(), TQString("%1").arg(entry->rating()));
  356. tmp_r->setPixmap(0, pix);
  357. }
  358. if(variants.contains("downloads"))
  359. {
  360. TDEListViewItem *tmp_d = new NumSortListViewItem(lv_d,
  361. entry->name(lang), entry->version(), TQString("%1").arg(entry->downloads()));
  362. tmp_d->setPixmap(0, pix);
  363. }
  364. if(variants.contains("latest"))
  365. {
  366. TDEListViewItem *tmp_l = new DateSortListViewItem(lv_l,
  367. entry->name(lang), entry->version(), TDEGlobal::locale()->formatDate(entry->releaseDate()));
  368. tmp_l->setPixmap(0, pix);
  369. }
  370. m_entries.append(entry);
  371. kdDebug() << "added entry " << entry->name() << " for variants " << variants << endl;
  372. }
  373. void DownloadDialog::addEntry(Entry *entry)
  374. {
  375. TQStringList variants;
  376. variants << "score";
  377. variants << "downloads";
  378. variants << "latest";
  379. addEntry(entry, variants);
  380. // not used anymore due to variants (but still used by engine)
  381. kdDebug() << "added entry " << entry->name() << endl;
  382. }
  383. void DownloadDialog::slotData(TDEIO::Job *job, const TQByteArray &a)
  384. {
  385. TQCString tmp(a, a.size() + 1);
  386. m_data[job].append(TQString::fromUtf8(tmp));
  387. }
  388. void DownloadDialog::slotDetails()
  389. {
  390. Entry *e = getEntry();
  391. if(!e) return;
  392. TQString lang = TDEGlobal::locale()->language();
  393. TQString info = i18n
  394. (
  395. "Name: %1\n"
  396. "Author: %2\n"
  397. "License: %3\n"
  398. "Version: %4\n"
  399. "Release: %5\n"
  400. "Rating: %6\n"
  401. "Downloads: %7\n"
  402. "Release date: %8\n"
  403. "Summary: %9\n"
  404. ).arg(e->name(lang)
  405. ).arg(e->author()
  406. ).arg(e->license()
  407. ).arg(e->version()
  408. ).arg(e->release()
  409. ).arg(e->rating()
  410. ).arg(e->downloads()
  411. ).arg(TDEGlobal::locale()->formatDate(e->releaseDate())
  412. ).arg(e->summary(lang)
  413. );
  414. info.append(i18n
  415. (
  416. "Preview: %1\n"
  417. "Payload: %2\n"
  418. ).arg(e->preview().url()
  419. ).arg(e->payload().url()
  420. ));
  421. KMessageBox::information(this, info, i18n("Details"));
  422. }
  423. TQListViewItem *DownloadDialog::currentEntryItem()
  424. {
  425. if((m_curtab == 0) && (lv_r->selectedItem())) return lv_r->selectedItem();
  426. if((m_curtab == 1) && (lv_d->selectedItem())) return lv_d->selectedItem();
  427. if((m_curtab == 2) && (lv_l->selectedItem())) return lv_l->selectedItem();
  428. return 0;
  429. }
  430. void DownloadDialog::slotInstall()
  431. {
  432. Entry *e = getEntry();
  433. if(!e) return;
  434. d->m_lvtmp_r->setEnabled( false );
  435. d->m_lvtmp_l->setEnabled( false );
  436. d->m_lvtmp_d->setEnabled( false );
  437. m_entryitem = currentEntryItem();
  438. m_entryname = m_entryitem->text(0);
  439. kdDebug() << "download entry now" << endl;
  440. if(m_engine)
  441. {
  442. m_engine->download(e);
  443. install(e);
  444. }
  445. else
  446. {
  447. m_s = new KNewStuffGeneric(e->type(), this);
  448. m_entry = e;
  449. KURL source = e->payload();
  450. KURL dest = KURL(m_s->downloadDestination(e));
  451. TDEIO::FileCopyJob *job = TDEIO::file_copy(source, dest, -1, true);
  452. connect(job, TQT_SIGNAL(result(TDEIO::Job*)), TQT_SLOT(slotInstalled(TDEIO::Job*)));
  453. }
  454. }
  455. void DownloadDialog::install(Entry *e)
  456. {
  457. kapp->config()->setGroup("KNewStuffStatus");
  458. kapp->config()->writeEntry(m_entryname, TQString(e->releaseDate().toString(Qt::ISODate)));
  459. kapp->config()->sync();
  460. TQPixmap pix = TDEGlobal::iconLoader()->loadIcon("ok", TDEIcon::Small);
  461. TQString lang = TDEGlobal::locale()->language();
  462. if(m_entryitem)
  463. {
  464. m_entryitem->setPixmap(0, pix);
  465. TQListViewItem *item;
  466. item = lv_r->findItem(e->name(lang), 0);
  467. if(item) item->setPixmap(0, pix);
  468. item = lv_d->findItem(e->name(lang), 0);
  469. if(item) item->setPixmap(0, pix);
  470. item = lv_l->findItem(e->name(lang), 0);
  471. if(item) item->setPixmap(0, pix);
  472. }
  473. if(currentEntryItem() == m_entryitem)
  474. {
  475. TQPushButton *in;
  476. in = *(m_buttons[d->m_page]->at(0));
  477. if(in) in->setEnabled(false);
  478. }
  479. d->m_installlist.append(e);
  480. d->m_lvtmp_r->setEnabled( true );
  481. d->m_lvtmp_l->setEnabled( true );
  482. d->m_lvtmp_d->setEnabled( true );
  483. }
  484. void DownloadDialog::slotInstalled(TDEIO::Job *job)
  485. {
  486. bool ret = job && (job->error() == 0);
  487. if(ret)
  488. {
  489. TDEIO::FileCopyJob *cjob = ::tqqt_cast<TDEIO::FileCopyJob*>(job);
  490. if(cjob)
  491. {
  492. ret = m_s->install(cjob->destURL().path());
  493. }
  494. else ret = false;
  495. }
  496. if(ret)
  497. {
  498. install(m_entry);
  499. KMessageBox::information(this, i18n("Installation successful."), i18n("Installation"));
  500. }
  501. else KMessageBox::error(this, i18n("Installation failed."), i18n("Installation"));
  502. d->m_lvtmp_r->setEnabled( true );
  503. d->m_lvtmp_l->setEnabled( true );
  504. d->m_lvtmp_d->setEnabled( true );
  505. delete m_s;
  506. }
  507. void DownloadDialog::slotTab()
  508. {
  509. int tab = static_cast<const TQTabWidget *>(sender())->currentPageIndex();
  510. kdDebug() << "switch tab to: " << tab << endl;
  511. Entry *eold = getEntry();
  512. m_curtab = tab;
  513. Entry *e = getEntry();
  514. if(e == eold) return;
  515. if(e)
  516. {
  517. slotSelected();
  518. }
  519. else
  520. {
  521. TQPushButton *de, *in;
  522. in = *(m_buttons[d->m_page]->at(0));
  523. de = *(m_buttons[d->m_page]->at(1));
  524. if(in) in->setEnabled(false);
  525. if(de) de->setEnabled(false);
  526. m_rt->clear();
  527. }
  528. }
  529. void DownloadDialog::slotSelected()
  530. {
  531. TQString tmp;
  532. bool enabled;
  533. Entry *e = getEntry();
  534. TQString lang = TDEGlobal::locale()->language();
  535. bool ret;
  536. if(e)
  537. {
  538. TQString lang = TDEGlobal::locale()->language();
  539. TQListViewItem *item;
  540. if(m_curtab != 0)
  541. {
  542. lv_r->clearSelection();
  543. item = lv_r->findItem(e->name(lang), 0);
  544. if(item) lv_r->setSelected(item, true);
  545. }
  546. if(m_curtab != 1)
  547. {
  548. lv_d->clearSelection();
  549. item = lv_d->findItem(e->name(lang), 0);
  550. if(item) lv_d->setSelected(item, true);
  551. }
  552. if(m_curtab != 2)
  553. {
  554. lv_l->clearSelection();
  555. item = lv_l->findItem(e->name(lang), 0);
  556. if(item) lv_l->setSelected(item, true);
  557. }
  558. if(!e->preview(lang).isValid())
  559. {
  560. ret = 0;
  561. }
  562. else
  563. {
  564. ret = TDEIO::NetAccess::download(e->preview(lang), tmp, this);
  565. }
  566. TQString desc = TQString("<b>%1</b><br>").arg(e->name(lang));
  567. if(!e->authorEmail().isNull())
  568. {
  569. desc += TQString("<a href='mailto:" + e->authorEmail() + "'>" + e->author() + "</a>");
  570. }
  571. else
  572. {
  573. desc += TQString("%1").arg(e->author());
  574. }
  575. desc += TQString("<br>%1").arg(TDEGlobal::locale()->formatDate(e->releaseDate()));
  576. desc += TQString("<br><br>");
  577. if(ret)
  578. {
  579. desc += TQString("<img src='%1'>").arg(tmp);
  580. }
  581. else
  582. {
  583. desc += i18n("Preview not available.");
  584. }
  585. desc += TQString("<br><i>%1</i>").arg(e->summary(lang));
  586. desc += TQString("<br>(%1)").arg(e->license());
  587. m_rt->clear();
  588. m_rt->setText(desc);
  589. if(installStatus(e) == 1) enabled = false;
  590. else enabled = true;
  591. TQPushButton *de, *in;
  592. in = *(m_buttons[d->m_page]->at(0));
  593. de = *(m_buttons[d->m_page]->at(1));
  594. if(in) in->setEnabled(enabled);
  595. if(de) de->setEnabled(true);
  596. }
  597. }
  598. void DownloadDialog::slotEmail(const TQString& link)
  599. {
  600. kdDebug() << "EMAIL: " << link << endl;
  601. kapp->invokeMailer(link);
  602. slotSelected(); // TQTextBrowser oddity workaround as it cannot handle mailto: URLs
  603. }
  604. Entry *DownloadDialog::getEntry()
  605. {
  606. TQListViewItem *entryItem = currentEntryItem();
  607. if(!entryItem)
  608. return 0;
  609. TQString entryName = entryItem->text(0);
  610. TQString lang = TDEGlobal::locale()->language();
  611. for(Entry *e = m_entries.first(); e; e = m_entries.next())
  612. if(e->name(lang) == entryName)
  613. return e;
  614. return 0;
  615. }
  616. void DownloadDialog::slotPage(TQWidget *w)
  617. {
  618. Provider *p;
  619. kdDebug() << "changed widget!!!" << endl;
  620. if(m_map.find(w) == m_map.end()) return;
  621. d->m_page = w;
  622. lv_r = *(m_map[w]->at(0));
  623. lv_d = *(m_map[w]->at(1));
  624. lv_l = *(m_map[w]->at(2));
  625. p = m_providers[w];
  626. m_rt = m_rts[w];
  627. kdDebug() << "valid change!!!; lv_r = " << lv_r << endl;
  628. if(m_engine) return;
  629. if(!m_filter.isEmpty()) return;
  630. lv_r->clear();
  631. lv_d->clear();
  632. lv_l->clear();
  633. kdDebug() << "-- fetch -- " << p->downloadUrl() << endl;
  634. loadProvider(p);
  635. }
  636. void DownloadDialog::loadProvider(Provider *p)
  637. {
  638. TQMap<TDEIO::Job*, Provider*>::Iterator it;
  639. for(it = d->m_variantjobs.begin(); it != d->m_variantjobs.end(); it++)
  640. {
  641. if(it.data() == p)
  642. {
  643. kdDebug() << "-- found provider data in cache" << endl;
  644. slotResult(it.key());
  645. return;
  646. }
  647. }
  648. TQStringList variants;
  649. variants << "score";
  650. variants << "downloads";
  651. variants << "latest";
  652. // Optimise URLs so each unique URL only gets fetched once
  653. TQMap<TQString, TQStringList> urls;
  654. for(TQStringList::Iterator it = variants.begin(); it != variants.end(); it++)
  655. {
  656. TQString url = p->downloadUrlVariant((*it)).url();
  657. if(!urls.contains(url))
  658. {
  659. urls[url] = TQStringList();
  660. }
  661. urls[url] << (*it);
  662. it = variants.remove(it);
  663. }
  664. // Now fetch the URLs while keeping the variant list for each attached
  665. for(TQMap<TQString, TQStringList>::Iterator it = urls.begin(); it != urls.end(); it++)
  666. {
  667. TQString url = it.key();
  668. TQStringList urlvariants = it.data();
  669. TDEIO::TransferJob *variantjob = TDEIO::get(url);
  670. d->m_newproviders[p] = p;
  671. d->m_variantjobs[variantjob] = p;
  672. d->m_variants[variantjob] = urlvariants;
  673. m_data[variantjob] = "";
  674. connect(variantjob, TQT_SIGNAL(result(TDEIO::Job*)), TQT_SLOT(slotResult(TDEIO::Job*)));
  675. connect(variantjob, TQT_SIGNAL(data(TDEIO::Job*, const TQByteArray&)),
  676. TQT_SLOT(slotData(TDEIO::Job*, const TQByteArray&)));
  677. }
  678. if(variants.count() == 0) return;
  679. // If not all variants are given, use default URL for those
  680. kdDebug() << "-- reached old downloadurl section; variants left: " << variants.count() << endl;
  681. TDEIO::TransferJob *job = TDEIO::get(p->downloadUrl());
  682. d->m_newproviders[p] = p;
  683. d->m_variantjobs[job] = p;
  684. d->m_variants[job] = variants;
  685. //m_jobs[job] = p; // not used anymore due to variants
  686. m_data[job] = "";
  687. connect(job, TQT_SIGNAL(result(TDEIO::Job*)), TQT_SLOT(slotResult(TDEIO::Job*)));
  688. connect(job, TQT_SIGNAL(data(TDEIO::Job*, const TQByteArray&)),
  689. TQT_SLOT(slotData(TDEIO::Job*, const TQByteArray&)));
  690. }
  691. void DownloadDialog::setType(TQString type)
  692. {
  693. m_filter = type;
  694. }
  695. void DownloadDialog::setProviderList(const TQString& providerList)
  696. {
  697. d->m_providerlist = providerList;
  698. }
  699. void DownloadDialog::slotOk()
  700. {
  701. }
  702. void DownloadDialog::slotApply()
  703. {
  704. }
  705. void DownloadDialog::open(const TQString& type, const TQString& caption)
  706. {
  707. DownloadDialog d(0, caption);
  708. d.setType(type);
  709. d.load();
  710. d.exec();
  711. }
  712. void DownloadDialog::slotFinish()
  713. {
  714. showPage(1);
  715. //updateBackground();
  716. }
  717. TQPtrList<Entry> DownloadDialog::installedEntries()
  718. {
  719. return d->m_installlist;
  720. }