Project Gutenberg downloads work end-to-end.
[dorian] / searchresultsdialog.cpp
1 #include <stdio.h>
2 #include <QtGui>
3
4 #include "listview.h"
5 #include "searchresultsdialog.h"
6 #include "searchresultinfodialog.h"
7 #include "trace.h"
8 #include "progressdialog.h"
9 #include "library.h"
10 #include "platform.h"
11
12 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
13     QWidget *parent): ListWindow(parent), results(results_)
14 {
15     setWindowTitle(tr("Search Results"));
16
17     foreach (Search::Result result, results) {
18         QString author;
19         if (result.authors.length()) {
20             author = result.authors[0];
21         }
22         data.append(result.title + "\n" + author);
23     }
24
25     QStringListModel *model = new QStringListModel(data, this);
26     list = new ListView;
27     list->setSelectionMode(QAbstractItemView::SingleSelection);
28     list->setModel(model);
29     list->setUniformItemSizes(true);
30     addList(list);
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)));
36     connect(search,
37             SIGNAL(endDownload(int, const Search::Result &, const QString &)),
38             this,
39             SLOT(onEndDownload(int, const Search::Result &, const QString &)));
40
41     progress = new ProgressDialog(tr("Downloading Book"), this);
42 }
43
44 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
45 {
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);
51     int ret = d->exec();
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);
57     }
58 }
59
60 void SearchResultsDialog::onDownload()
61 {
62     onItemActivated(list->currentIndex());
63 }
64
65 QString SearchResultsDialog::downloadName() const
66 {
67     Trace t("SearchResultsDialog::downloadName");
68     QString dir = Platform::downloadDir();
69     QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
70     unsigned i = 0;
71     QString fileName;
72     do {
73         char tmp[9];
74         snprintf(tmp, 8, "%8.8x", i++);
75         tmp[8] = '\0';
76         fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
77     } while (QFile(fileName).exists());
78     qDebug() << fileName;
79     return fileName;
80 }
81
82 void SearchResultsDialog::onBeginDownload(int size)
83 {
84     Q_UNUSED(size);
85     Trace t("SearchResultsDialog::onBeginDownload");
86     progress->setMinimum(0);
87     progress->setMaximum(0);
88     progress->setValue(0);
89     progress->show();
90 }
91
92 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
93                                         const QString &fileName)
94 {
95     Q_UNUSED(result);
96     Trace t("SearchResultsDialog::onEndDownload");
97     progress->reset();
98     if (Search::Ok == status) {
99         Library::instance()->add(fileName);
100         int row = results.indexOf(result);
101         if (-1 != row) {
102             list->model()->removeRow(row);
103         }
104     }
105 }