Battle with include files (lost).
[dorian] / searchresultsdialog.cpp
1 #include <stdio.h>
2
3 #include <QtGui>
4 #include <QStringListModel>
5 #include <QDir>
6 #include <QFile>
7
8 #ifdef Q_WS_MAEMO_5
9 #include <QtMaemo5/QMaemo5InformationBox>
10 #endif
11
12 #include "listview.h"
13 #include "searchresultsdialog.h"
14 #include "searchresultinfodialog.h"
15 #include "trace.h"
16 #include "progressdialog.h"
17 #include "library.h"
18 #include "platform.h"
19
20 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
21     QWidget *parent): ListWindow(parent), results(results_)
22 {
23     setWindowTitle(tr("Search Results"));
24
25     foreach (Search::Result result, results) {
26         QString author;
27         if (result.authors.length()) {
28             author = result.authors[0];
29         }
30         data.append(result.title + "\n" + author);
31     }
32
33     QStringListModel *model = new QStringListModel(data, this);
34     list = new ListView;
35     list->setSelectionMode(QAbstractItemView::SingleSelection);
36     list->setModel(model);
37     list->setUniformItemSizes(true);
38     addList(list);
39     addItemAction(tr("Download book"), this, SLOT(onDownload()));
40     connect(list, SIGNAL(activated(const QModelIndex &)),
41             this, SLOT(onItemActivated(const QModelIndex &)));
42     Search *search = Search::instance();
43     connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
44     connect(search,
45             SIGNAL(endDownload(int, const Search::Result &, const QString &)),
46             this,
47             SLOT(onEndDownload(int, const Search::Result &, const QString &)));
48
49     progress = new ProgressDialog(tr("Downloading Book"), this);
50 }
51
52 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
53 {
54     TRACE;
55     Search::Result result = results[index.row()];
56     qDebug() << "Book" << index.row() << ":" << result.title;
57     SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
58     d->setAttribute(Qt::WA_DeleteOnClose);
59     int ret = d->exec();
60     if (ret == QDialog::Accepted) {
61         qDebug() << "Accepted -> Start download";
62         QString fileName = downloadName();
63         qDebug() << "Downloading to" << fileName;
64         Search::instance()->download(result, fileName);
65     }
66 }
67
68 void SearchResultsDialog::onDownload()
69 {
70     onItemActivated(list->currentIndex());
71 }
72
73 QString SearchResultsDialog::downloadName() const
74 {
75     TRACE;
76     QString dir = Platform::downloadDir();
77     QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
78     unsigned i = 0;
79     QString fileName;
80     do {
81         char tmp[9];
82         snprintf(tmp, 8, "%8.8x", i++);
83         tmp[8] = '\0';
84         fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
85     } while (QFile(fileName).exists());
86     qDebug() << fileName;
87     return fileName;
88 }
89
90 void SearchResultsDialog::onBeginDownload(int size)
91 {
92     Q_UNUSED(size);
93     TRACE;
94     progress->showWait();
95 }
96
97 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
98                                         const QString &fileName)
99 {
100     Q_UNUSED(result);
101     TRACE;
102     progress->reset();
103     if (Search::Ok == status) {
104         Library::instance()->add(fileName);
105         int row = results.indexOf(result);
106         if (-1 != row) {
107             list->model()->removeRow(row);
108         }
109 #ifdef Q_WS_MAEMO_5
110         QMaemo5InformationBox::information(this,
111             tr("Downloaded \"%1\"\nand added to the library").arg(result.title),
112             QMaemo5InformationBox::DefaultTimeout);
113 #endif
114     }
115 }