4 #include <QStringListModel>
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)
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
72 QString dir = Platform::instance()->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)
93 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
94 const QString &fileName)
99 if (Search::Ok == status) {
100 Library::instance()->add(fileName);
101 int row = results.indexOf(result);
103 list->model()->removeRow(row);
105 Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
106 "library").arg(result.title), this);