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