X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=search.cpp;h=df928c70bbbd6ea0ad8b3ca90c070fbb64865640;hb=c474162a11b6851413b67df35765ce727b89c278;hp=27e4ed2d2dc78859a151c5252bacc4089af605b4;hpb=19cf9a449b50a2b52e552f5ba26974c06d1601ef;p=dorian diff --git a/search.cpp b/search.cpp index 27e4ed2..df928c7 100644 --- a/search.cpp +++ b/search.cpp @@ -1,10 +1,11 @@ -#include -#include #include -#include +#include +#include +#include +#include #include #include -#include +#include #include "search.h" #include "platform.h" @@ -26,14 +27,15 @@ void Search::close() inst = 0; } -Search::Search(): QObject(0), reply(0) +Search::Search(): QObject(0), reply(0), downloadReply(0) { manager = new QNetworkAccessManager(this); + downloadManager = new QNetworkAccessManager(this); } void Search::start(const Query &query) { - Trace t("Search::start"); + TRACE; emit beginSearch(); @@ -58,30 +60,45 @@ QList Search::results() return searchResults; } -bool Search::download(const Search::Result &result, const QString &fileName) +void Search::download(const Search::Result &result, const QString &fileName) { - Q_UNUSED(result); + TRACE; + downloadResult = result; + downloadFileName = fileName; + qDebug() << "UID" << result.id; Q_UNUSED(fileName); emit beginDownload(0); - emit endDownload(); - return false; + QUrl url("http://www.gutenberg.org/ebooks/" + result.id + ".epub"); + qDebug() << "Requesting" << url; + QNetworkRequest request; + request.setUrl(url); + downloadReply = downloadManager->get(request); + connect(downloadReply, SIGNAL(finished()), this, SLOT(downloadFinished())); } void Search::finished() { - Trace t("Search::finished"); + TRACE; + + if (!reply) { + return; + } + QByteArray data = reply->readAll(); - qDebug() << data; + + // Parse search results QWebPage page(this); QWebFrame *frame = page.mainFrame(); frame->setHtml(QString(data)); QWebElementCollection tables = frame->findAllElements("table"); if (tables.count() == 1) { - qDebug() << "Found table"; QWebElement table = tables[0]; foreach (QWebElement row, table.findAll("tr")) { QWebElementCollection cols = row.findAll("td"); + if (cols.count() < 5) { + continue; + } QString id = cols[0].toPlainText().trimmed(); if (id.isEmpty()) { continue; @@ -96,6 +113,7 @@ void Search::finished() r.title = titles[0]; } r.language = cols[4].toPlainText().trimmed(); + r.source = "Project Gutenberg"; searchResults.append(r); qDebug() << id; qDebug() << " Authors:" << r.authors; @@ -105,5 +123,61 @@ void Search::finished() } reply->deleteLater(); + reply = 0; emit endSearch(); } + +void Search::downloadFinished() +{ + TRACE; + + if (!downloadReply) { + return; + } + + // Handle download errors + if (QNetworkReply::NoError != downloadReply->error()) { + qCritical() << "Search::downloadFinished: Network error" + << downloadReply->error(); + downloadReply->deleteLater(); + downloadReply = 0; + emit endDownload(Search::DownloadError, downloadResult, downloadFileName); + return; + } + + // Handle redirection + QVariant header = downloadReply->header(QNetworkRequest::LocationHeader); + if (header.isValid()) { + // Handle redirection: Download again with the new URL + qDebug() << "Redirected to" << header; + QNetworkRequest request; + request.setUrl(header.toUrl()); + downloadReply->deleteLater(); + downloadReply = downloadManager->get(request); + connect(downloadReply, SIGNAL(finished()), this, SLOT(downloadFinished())); + return; + } + + // Handle download success + QByteArray data = downloadReply->readAll(); + qint64 size = (qint64)data.size(); + qDebug() << "Got" << size << "bytes"; + downloadReply->deleteLater(); + downloadReply = 0; + QFile out(downloadFileName); + int status = Search::FileError; + if (out.open(QIODevice::WriteOnly)) { + if (size == out.write(data, size)) { + qDebug() << "Book saved to" << downloadFileName; + status = Search::Ok; + } else { + qCritical() << "Search::downloadFinished: Failed to write" << size + << "bytes to" << downloadFileName; + } + out.close(); + } else { + qCritical() << "Search::downloadFinished: Could not open" + << downloadFileName; + } + emit endDownload(status, downloadResult, downloadFileName); +}