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