Re-design ListWindow.
[dorian] / searchresultsdialog.cpp
1 #include <stdio.h>
2
3 #include <QtGui>
4 #include <QStringListModel>
5 #include <QDir>
6 #include <QFile>
7
8 #include "listview.h"
9 #include "searchresultsdialog.h"
10 #include "searchresultinfodialog.h"
11 #include "trace.h"
12 #include "progressdialog.h"
13 #include "library.h"
14 #include "platform.h"
15
16 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
17     QWidget *parent): ListWindow(parent), results(results_)
18 {
19     setWindowTitle(tr("Search results"));
20
21     foreach (Search::Result result, results) {
22         QString author;
23         if (result.authors.length()) {
24             author = result.authors[0];
25         }
26         data.append(result.title + "\n" + author);
27     }
28
29     QStringListModel *model = new QStringListModel(data, this);
30     setModel(model);
31     // FIXME
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;
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 QString SearchResultsDialog::downloadName() const
61 {
62     TRACE;
63     QString dir = Platform::instance()->downloadDir();
64     QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
65     unsigned i = 0;
66     QString fileName;
67     do {
68         char tmp[9];
69         snprintf(tmp, 8, "%8.8x", i++);
70         tmp[8] = '\0';
71         fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
72     } while (QFile(fileName).exists());
73     qDebug() << fileName;
74     return fileName;
75 }
76
77 void SearchResultsDialog::onBeginDownload(int size)
78 {
79     Q_UNUSED(size);
80     TRACE;
81     progress->showWait();
82 }
83
84 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
85                                         const QString &fileName)
86 {
87     Q_UNUSED(result);
88     TRACE;
89     progress->reset();
90     if (Search::Ok == status) {
91         Library::instance()->add(fileName);
92         int row = results.indexOf(result);
93         if (-1 != row) {
94             list->model()->removeRow(row);
95         }
96         Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
97                                              "library").arg(result.title), this);
98     }
99 }