5 #include <QtMaemo5/QMaemo5InformationBox>
9 #include "searchresultsdialog.h"
10 #include "searchresultinfodialog.h"
12 #include "progressdialog.h"
16 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
17 QWidget *parent): ListWindow(parent), results(results_)
19 setWindowTitle(tr("Search Results"));
21 foreach (Search::Result result, results) {
23 if (result.authors.length()) {
24 author = result.authors[0];
26 data.append(result.title + "\n" + author);
29 QStringListModel *model = new QStringListModel(data, this);
31 list->setSelectionMode(QAbstractItemView::SingleSelection);
32 list->setModel(model);
33 list->setUniformItemSizes(true);
35 addItemAction(tr("Download book"), this, SLOT(onDownload()));
36 connect(list, SIGNAL(activated(const QModelIndex &)),
37 this, SLOT(onItemActivated(const QModelIndex &)));
38 Search *search = Search::instance();
39 connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
41 SIGNAL(endDownload(int, const Search::Result &, const QString &)),
43 SLOT(onEndDownload(int, const Search::Result &, const QString &)));
45 progress = new ProgressDialog(tr("Downloading Book"), this);
48 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
50 Trace t("SearchResultsDialog::onItemActivated");
51 Search::Result result = results[index.row()];
52 qDebug() << "Book" << index.row() << ":" << result.title;
53 SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
54 d->setAttribute(Qt::WA_DeleteOnClose);
56 if (ret == QDialog::Accepted) {
57 qDebug() << "Accepted -> Start download";
58 QString fileName = downloadName();
59 qDebug() << "Downloading to" << fileName;
60 Search::instance()->download(result, fileName);
64 void SearchResultsDialog::onDownload()
66 onItemActivated(list->currentIndex());
69 QString SearchResultsDialog::downloadName() const
71 Trace t("SearchResultsDialog::downloadName");
72 QString dir = Platform::downloadDir();
73 QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
78 snprintf(tmp, 8, "%8.8x", i++);
80 fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
81 } while (QFile(fileName).exists());
86 void SearchResultsDialog::onBeginDownload(int size)
89 Trace t("SearchResultsDialog::onBeginDownload");
90 progress->setMinimum(0);
91 progress->setMaximum(0);
92 progress->setValue(0);
96 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
97 const QString &fileName)
100 Trace t("SearchResultsDialog::onEndDownload");
102 if (Search::Ok == status) {
103 Library::instance()->add(fileName);
104 int row = results.indexOf(result);
106 list->model()->removeRow(row);
109 QMaemo5InformationBox::information(this,
110 tr("Downloaded \"%1\"\nand added to the library").arg(result.title),
111 QMaemo5InformationBox::DefaultTimeout);