4 #include <QStringListModel>
8 #include "searchresultsdialog.h"
9 #include "searchresultinfodialog.h"
11 #include "progressdialog.h"
15 SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
16 QWidget *parent): ListWindow(tr("(No results)"), parent), results(results_)
18 setWindowTitle(tr("Search results"));
20 foreach (Search::Result result, results) {
22 if (result.authors.length()) {
23 author = result.authors[0];
25 data.append(result.title + "\n" + author);
28 QStringListModel *model = new QStringListModel(data, this);
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)));
35 SIGNAL(endDownload(int, const Search::Result &, const QString &)),
37 SLOT(onEndDownload(int, const Search::Result &, const QString &)));
39 progress = new ProgressDialog(tr("Downloading book"), this);
42 void SearchResultsDialog::onItemActivated(const QModelIndex &index)
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);
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);
58 QString SearchResultsDialog::downloadName() const
61 QString dir = Platform::instance()->downloadDir();
62 QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame.
67 snprintf(tmp, 8, "%8.8x", i++);
69 fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub");
70 } while (QFile(fileName).exists());
75 void SearchResultsDialog::onBeginDownload(int size)
82 void SearchResultsDialog::onEndDownload(int status, const Search::Result &result,
83 const QString &fileName)
88 if (Search::Ok == status) {
89 Library::instance()->add(fileName);
90 int row = results.indexOf(result);
92 model()->removeRow(row);
94 Platform::instance()->information(tr("Downloaded \"%1\"\nand added to the "
95 "library").arg(result.title), this);