4 #include <QStringListModel>
9 #include "searchresultsdialog.h"
10 #include "searchresultinfodialog.h"
12 #include "progressdialog.h"
16 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
17 QWidget *parent): ListWindow(parent), results(results_)
19 setWindowTitle(tr("Search results"));
21 foreach (Search::Result result, results) {
23 if (result.authors.length()) {
24 author = result.authors[0];
26 data.append(result.title + "\n" + author);
29 QStringListModel *model = new QStringListModel(data, this);
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)));
37 SIGNAL(endDownload(int, const Search::Result &, const QString &)),
39 SLOT(onEndDownload(int, const Search::Result &, const QString &)));
41 progress = new ProgressDialog(tr("Downloading book"), this);
44 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
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);
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);
60 QString SearchResultsDialog::downloadName() const
63 QString dir = Platform::instance()->downloadDir();
64 QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
69 snprintf(tmp, 8, "%8.8x", i++);
71 fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
72 } while (QFile(fileName).exists());
77 void SearchResultsDialog::onBeginDownload(int size)
84 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
85 const QString &fileName)
90 if (Search::Ok == status) {
91 Library::instance()->add(fileName);
92 int row = results.indexOf(result);
94 list->model()->removeRow(row);
96 Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
97 "library").arg(result.title), this);