4 #include <QStringListModel>
9 #include <QtMaemo5/QMaemo5InformationBox>
13 #include "searchresultsdialog.h"
14 #include "searchresultinfodialog.h"
16 #include "progressdialog.h"
20 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
21 QWidget *parent): ListWindow(parent), results(results_)
23 setWindowTitle(tr("Search Results"));
25 foreach (Search::Result result, results) {
27 if (result.authors.length()) {
28 author = result.authors[0];
30 data.append(result.title + "\n" + author);
33 QStringListModel *model = new QStringListModel(data, this);
35 list->setSelectionMode(QAbstractItemView::SingleSelection);
36 list->setModel(model);
37 list->setUniformItemSizes(true);
39 addItemAction(tr("Download book"), this, SLOT(onDownload()));
40 connect(list, SIGNAL(activated(const QModelIndex &)),
41 this, SLOT(onItemActivated(const QModelIndex &)));
42 Search *search = Search::instance();
43 connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
45 SIGNAL(endDownload(int, const Search::Result &, const QString &)),
47 SLOT(onEndDownload(int, const Search::Result &, const QString &)));
49 progress = new ProgressDialog(tr("Downloading Book"), this);
52 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
55 Search::Result result = results[index.row()];
56 qDebug() << "Book" << index.row() << ":" << result.title;
57 SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
58 d->setAttribute(Qt::WA_DeleteOnClose);
60 if (ret == QDialog::Accepted) {
61 qDebug() << "Accepted -> Start download";
62 QString fileName = downloadName();
63 qDebug() << "Downloading to" << fileName;
64 Search::instance()->download(result, fileName);
68 void SearchResultsDialog::onDownload()
70 onItemActivated(list->currentIndex());
73 QString SearchResultsDialog::downloadName() const
76 QString dir = Platform::downloadDir();
77 QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
82 snprintf(tmp, 8, "%8.8x", i++);
84 fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
85 } while (QFile(fileName).exists());
90 void SearchResultsDialog::onBeginDownload(int size)
97 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
98 const QString &fileName)
103 if (Search::Ok == status) {
104 Library::instance()->add(fileName);
105 int row = results.indexOf(result);
107 list->model()->removeRow(row);
110 QMaemo5InformationBox::information(this,
111 tr("Downloaded \"%1\"\nand added to the library").arg(result.title),
112 QMaemo5InformationBox::DefaultTimeout);