Show progress while searching and downloading.
[dorian] / searchresultsdialog.cpp
1 #include <QtGui>
2
3 #include "listview.h"
4 #include "searchresultsdialog.h"
5 #include "searchresultinfodialog.h"
6 #include "trace.h"
7 #include "progressdialog.h"
8
9 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
10     QWidget *parent): ListWindow(parent), results(results_)
11 {
12     setWindowTitle(tr("Search Results"));
13
14     foreach (Search::Result result, results) {
15         QString author;
16         if (result.authors.length()) {
17             author = result.authors[0];
18         }
19         data.append(result.title + "\n" + author);
20     }
21
22     QStringListModel *model = new QStringListModel(data, this);
23     list = new ListView;
24     list->setSelectionMode(QAbstractItemView::SingleSelection);
25     list->setModel(model);
26     list->setUniformItemSizes(true);
27     addList(list);
28     addItemAction(tr("Download book"), this, SLOT(onDownload()));
29     connect(list, SIGNAL(activated(const QModelIndex &)),
30             this, SLOT(onItemActivated(const QModelIndex &)));
31     Search *search = Search::instance();
32     connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
33     connect(search, SIGNAL(endDownload()), this, SLOT(onEndDownload()));
34
35     progress = new ProgressDialog(tr("Downloading Book"), this);
36 }
37
38 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
39 {
40     Trace t("SearchResultsDialog::onItemActivated");
41     Search::Result result = results[index.row()];
42     qDebug() << "Book" << index.row() << ":" << result.title;
43     SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
44     d->setAttribute(Qt::WA_DeleteOnClose);
45     int ret = d->exec();
46     if (ret == QDialog::Accepted) {
47         qDebug() << "Accepted -> Start download";
48         QString fileName = downloadName();
49         qDebug() << "Downloading to" << fileName;
50         Search::instance()->download(result, fileName);
51     }
52 }
53
54 void SearchResultsDialog::onDownload()
55 {
56     onItemActivated(list->currentIndex());
57 }
58
59 QString SearchResultsDialog::downloadName() const
60 {
61     // FIXME
62     return QString("/tmp/book.epub");
63 }
64
65 void SearchResultsDialog::onBeginDownload(int size)
66 {
67     Trace t("SearchResultsDialog::onBeginDownload");
68     progress->setMinimum(0);
69     progress->setMaximum(0);
70     progress->setValue(0);
71     progress->show();
72 }
73
74 void SearchResultsDialog::onEndDownload()
75 {
76     Trace t("SearchResultsDialog::onEndDownload");
77     progress->reset();
78 }