Sort library by author or title. Sort books by two levels.
[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     list = new ListView;
31     list->setSelectionMode(QAbstractItemView::SingleSelection);
32     list->setModel(model);
33     list->setUniformItemSizes(true);
34     addList(list);
35     addItemAction(tr("Download book"), this, SLOT(onDownload()));
36     connect(list, SIGNAL(activated(const QModelIndex &)),
37             this, SLOT(onItemActivated(const QModelIndex &)));
38     Search *search = Search::instance();
39     connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int)));
40     connect(search,
41             SIGNAL(endDownload(int, const Search::Result &, const QString &)),
42             this,
43             SLOT(onEndDownload(int, const Search::Result &, const QString &)));
44
45     progress = new ProgressDialog(tr("Downloading book"), this);
46 }
47
48 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
49 {
50     TRACE;
51     Search::Result result = results[index.row()];
52     qDebug() << "Book" << index.row() << ":" << result.title;
53     SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this);
54     d->setAttribute(Qt::WA_DeleteOnClose);
55     int ret = d->exec();
56     if (ret == QDialog::Accepted) {
57         qDebug() << "Accepted -> Start download";
58         QString fileName = downloadName();
59         qDebug() << "Downloading to" << fileName;
60         Search::instance()->download(result, fileName);
61     }
62 }
63
64 void SearchResultsDialog::onDownload()
65 {
66     onItemActivated(list->currentIndex());
67 }
68
69 QString SearchResultsDialog::downloadName() const
70 {
71     TRACE;
72     QString dir = Platform::instance()->downloadDir();
73     QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
74     unsigned i = 0;
75     QString fileName;
76     do {
77         char tmp[9];
78         snprintf(tmp, 8, "%8.8x", i++);
79         tmp[8] = '\0';
80         fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
81     } while (QFile(fileName).exists());
82     qDebug() << fileName;
83     return fileName;
84 }
85
86 void SearchResultsDialog::onBeginDownload(int size)
87 {
88     Q_UNUSED(size);
89     TRACE;
90     progress->showWait();
91 }
92
93 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
94                                         const QString &fileName)
95 {
96     Q_UNUSED(result);
97     TRACE;
98     progress->reset();
99     if (Search::Ok == status) {
100         Library::instance()->add(fileName);
101         int row = results.indexOf(result);
102         if (-1 != row) {
103             list->model()->removeRow(row);
104         }
105         Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
106                                              "library").arg(result.title), this);
107     }
108 }