X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=searchresultsdialog.cpp;h=db0192b8fc0e4da6912b5c539cab09c76a0fcd20;hb=7c53414daec98fecb63ea234d54c428aefd587ac;hp=02748228ad03c3701869cbe5b7ed91b109e79c87;hpb=4259b7276912249c515aae70995aa606ddf344a3;p=dorian diff --git a/searchresultsdialog.cpp b/searchresultsdialog.cpp index 0274822..db0192b 100644 --- a/searchresultsdialog.cpp +++ b/searchresultsdialog.cpp @@ -1,12 +1,19 @@ +#include + #include +#include +#include +#include -#include "listview.h" #include "searchresultsdialog.h" #include "searchresultinfodialog.h" #include "trace.h" +#include "progressdialog.h" +#include "library.h" +#include "platform.h" SearchResultsDialog::SearchResultsDialog(const QList results_, - QWidget *parent): ListWindow(parent), results(results_) + QWidget *parent): ListWindow(tr("(No results)"), parent), results(results_) { setWindowTitle(tr("Search results")); @@ -19,19 +26,22 @@ SearchResultsDialog::SearchResultsDialog(const QList results_, } QStringListModel *model = new QStringListModel(data, this); - list = new ListView; - list->setSelectionMode(QAbstractItemView::SingleSelection); - list->setModel(model); - list->setUniformItemSizes(true); - addList(list); - addItemAction(tr("Download book"), this, SLOT(onDownload())); - connect(list, SIGNAL(activated(const QModelIndex &)), + setModel(model); + connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(onItemActivated(const QModelIndex &))); + Search *search = Search::instance(); + connect(search, SIGNAL(beginDownload(int)), this, SLOT(onBeginDownload(int))); + connect(search, + SIGNAL(endDownload(int, const Search::Result &, const QString &)), + this, + SLOT(onEndDownload(int, const Search::Result &, const QString &))); + + progress = new ProgressDialog(tr("Downloading book"), this); } void SearchResultsDialog::onItemActivated(const QModelIndex &index) { - Trace t("SearchResultsDialog::onItemActivated"); + TRACE; Search::Result result = results[index.row()]; qDebug() << "Book" << index.row() << ":" << result.title; SearchResultInfoDialog *d = new SearchResultInfoDialog(result, this); @@ -39,10 +49,51 @@ void SearchResultsDialog::onItemActivated(const QModelIndex &index) int ret = d->exec(); if (ret == QDialog::Accepted) { qDebug() << "Accepted -> Start download"; + QString fileName = downloadName(); + qDebug() << "Downloading to" << fileName; + Search::instance()->download(result, fileName); } } -void SearchResultsDialog::onDownload() +QString SearchResultsDialog::downloadName() const +{ + TRACE; + QString dir = Platform::instance()->downloadDir(); + QDir().mkpath(dir); // Not sure if this works. QDir API is quiet lame. + unsigned i = 0; + QString fileName; + do { + char tmp[9]; + snprintf(tmp, 8, "%8.8x", i++); + tmp[8] = '\0'; + fileName = QDir(dir).absoluteFilePath(QString(tmp) + ".epub"); + } while (QFile(fileName).exists()); + qDebug() << fileName; + return fileName; +} + +void SearchResultsDialog::onBeginDownload(int size) { - onItemActivated(list->currentIndex()); + Q_UNUSED(size); + TRACE; + progress->showWait(); +} + +void SearchResultsDialog::onEndDownload(int status, const Search::Result &result, + const QString &fileName) +{ + Q_UNUSED(result); + TRACE; + progress->reset(); + if (Search::Ok == status) { + Library::instance()->add(fileName); + int row = results.indexOf(result); + if (-1 != row) { + results.removeAt(row); + model()->removeRow(row); + } + Platform::instance()-> + information(tr("Downloaded \"%1\"\nand added to the " + "library").arg(result.title), this); + } }