5 #include "searchresultsdialog.h"
6 #include "searchresultinfodialog.h"
8 #include "progressdialog.h"
12 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
13 QWidget *parent): ListWindow(parent), results(results_)
15 setWindowTitle(tr("Search Results"));
17 foreach (Search::Result result, results) {
19 if (result.authors.length()) {
20 author = result.authors[0];
22 data.append(result.title + "\n" + author);
25 QStringListModel *model = new QStringListModel(data, this);
27 list->setSelectionMode(QAbstractItemView::SingleSelection);
28 list->setModel(model);
29 list->setUniformItemSizes(true);
31 addItemAction(tr("Download book"), this, SLOT(onDownload()));
32 connect(list, SIGNAL(activated(const QModelIndex &)),
33 this, SLOT(onItemActivated(const QModelIndex &)));
34 Search *search = Search::instance();
35 connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
37 SIGNAL(endDownload(int, const Search::Result &, const QString &)),
39 SLOT(onEndDownload(int, const Search::Result &, const QString &)));
41 progress = new ProgressDialog(tr("Downloading Book"), this);
44 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
46 Trace t("SearchResultsDialog::onItemActivated");
47 Search::Result result = results[index.row()];
48 qDebug() << "Book" << index.row() << ":" << result.title;
49 SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
50 d->setAttribute(Qt::WA_DeleteOnClose);
52 if (ret == QDialog::Accepted) {
53 qDebug() << "Accepted -> Start download";
54 QString fileName = downloadName();
55 qDebug() << "Downloading to" << fileName;
56 Search::instance()->download(result, fileName);
60 void SearchResultsDialog::onDownload()
62 onItemActivated(list->currentIndex());
65 QString SearchResultsDialog::downloadName() const
67 Trace t("SearchResultsDialog::downloadName");
68 QString dir = Platform::downloadDir();
69 QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
74 snprintf(tmp, 8, "%8.8x", i++);
76 fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
77 } while (QFile(fileName).exists());
82 void SearchResultsDialog::onBeginDownload(int size)
85 Trace t("SearchResultsDialog::onBeginDownload");
86 progress->setMinimum(0);
87 progress->setMaximum(0);
88 progress->setValue(0);
92 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
93 const QString &fileName)
96 Trace t("SearchResultsDialog::onEndDownload");
98 if (Search::Ok == status) {
99 Library::instance()->add(fileName);
100 int row = results.indexOf(result);
102 list->model()->removeRow(row);