Process and show search results.
authorAkos Polster <akos@pipacs.com>
Sun, 24 Oct 2010 23:09:41 +0000 (01:09 +0200)
committerAkos Polster <akos@pipacs.com>
Sun, 24 Oct 2010 23:09:41 +0000 (01:09 +0200)
dorian.pro
librarydialog.cpp
librarydialog.h
search.cpp
search.h
searchdialog.cpp
searchresultsdialog.cpp [new file with mode: 0644]
searchresultsdialog.h [new file with mode: 0644]

index 8bfda6f..af0dc0d 100644 (file)
-QT += webkit xml sql network\r
-\r
-INCLUDEPATH += $$PWD \\r
-    $$PWD/model \\r
-    $$PWD/widgets\r
-\r
-SOURCES += \\r
-    main.cpp \\r
-    mainwindow.cpp \\r
-    bookview.cpp \\r
-    model/unzip/unzip.c \\r
-    model/unzip/ioapi.c \\r
-    model/extractzip.cpp \\r
-    model/library.cpp \\r
-    model/book.cpp \\r
-    librarydialog.cpp \\r
-    devtools.cpp \\r
-    infodialog.cpp \\r
-    widgets/translucentbutton.cpp \\r
-    settingswindow.cpp \\r
-    model/settings.cpp \\r
-    bookmarksdialog.cpp \\r
-    model/sortedlibrary.cpp \\r
-    bookmarkinfodialog.cpp \\r
-    widgets/dyalog.cpp \\r
-    chaptersdialog.cpp \\r
-    widgets/fullscreenwindow.cpp \\r
-    trace.cpp \\r
-    widgets/toolbuttonbox.cpp \\r
-    model/bookfinder.cpp \\r
-    widgets/listwindow.cpp \\r
-    widgets/progress.cpp \\r
-    widgets/adopterwindow.cpp \\r
-    platform.cpp \\r
-    model/bookdb.cpp \\r
-    searchdialog.cpp \\r
-    search.cpp \\r
-    widgets/flickable.cpp \\r
-    widgets/listview.cpp\r
-\r
-HEADERS += \\r
-    mainwindow.h \\r
-    bookview.h \\r
-    model/opshandler.h \\r
-    model/unzip/unzip.h \\r
-    model/unzip/ioapi.h \\r
-    model/extractzip.h \\r
-    model/library.h \\r
-    model/book.h \\r
-    librarydialog.h \\r
-    devtools.h \\r
-    infodialog.h \\r
-    widgets/translucentbutton.h \\r
-    settingswindow.h \\r
-    model/settings.h \\r
-    bookmarksdialog.h \\r
-    model/xmlerrorhandler.h \\r
-    model/containerhandler.h \\r
-    model/sortedlibrary.h \\r
-    model/ncxhandler.h \\r
-    bookmarkinfodialog.h \\r
-    widgets/dyalog.h \\r
-    chaptersdialog.h \\r
-    widgets/fullscreenwindow.h \\r
-    trace.h \\r
-    widgets/toolbuttonbox.h \\r
-    model/bookfinder.h \\r
-    widgets/listwindow.h \\r
-    widgets/progress.h \\r
-    widgets/adopterwindow.h \\r
-    widgets/listview.h \\r
-    model/xmlhandler.h \\r
-    platform.h \\r
-    model/bookdb.h \\r
-    searchdialog.h \\r
-    search.h \\r
-    widgets/flickable.h\r
-\r
-RESOURCES += \\r
-    dorian.qrc\r
-\r
-OTHER_FILES += \\r
-    TODO.txt \\r
-    pkg/acknowledgements.txt \\r
-    pkg/maemo/postinst \\r
-    pkg/maemo/dorian.desktop \\r
-    pkg/maemo/control \\r
-    pkg/maemo/changelog \\r
-    pkg/maemo/build.sh \\r
-    styles/night.css \\r
-    pkg/changelog \\r
-    pkg/maemo/build-scratchbox.sh \\r
-    styles/sand.css \\r
-    styles/default.css \\r
-    pkg/version.txt \\r
-    styles/sand.js \\r
-    styles/night.js \\r
-    styles/default.js \\r
-    styles/day.js \\r
-    www/index.html \\r
-    pkg/maemo/autobuild.sh \\r
-    pkg/maemo/autobuild-scratchbox.sh \\r
-    LICENSE.txt \\r
-    pkg/symbian/book.svg\r
-\r
-DEFINES += \\r
-    USE_FILE32API\r
-\r
-!symbian {\r
-    DEFINES += DORIAN_TEST_MODEL\r
-    include(model/modeltest/modeltest.pri)\r
-}\r
-\r
-unix {\r
-    symbian {\r
-    } else {\r
-        LIBS += -lz\r
-    }\r
-}\r
-\r
-win32 {\r
-    DEFINES += ZLIB_WINAPI\r
-    INCLUDEPATH += $$PWD/model/zlib\r
-    LIBS += pkg/win32/zlibstat.lib\r
-}\r
-\r
-symbian {\r
-    ICON = $$PWD/pkg/symbian/book.svgt\r
-    TARGET.UID3 = 0xEA633557\r
-    TARGET.CAPABILITY = NetworkServices\r
-    # FIXME: Include path to OpenC ZLIB?\r
-    INCLUDEPATH += c:/Qt/4.7.0/src/3rdparty/zlib\r
-    HEADERS += widgets/flickcharm.h\r
-    SOURCES += widgets/flickcharm.cpp\r
-}\r
-\r
-maemo5 {\r
-    QT += maemo5 dbus\r
-    isEmpty(PREFIX) {\r
-        PREFIX = /usr\r
-    }\r
-    BINDIR = $$PREFIX/bin\r
-    DATADIR =$$PREFIX/share\r
-    DEFINES += DATADIR=\\\"$$DATADIR\\\" PKGDATADIR=\\\"$$PKGDATADIR\\\"\r
-\r
-    # For "make install"\r
-\r
-    INSTALLS += target desktop icon48 iconscalable\r
-\r
-    target.path = $$BINDIR\r
-\r
-    desktop.path = $$DATADIR/applications/hildon\r
-    desktop.files += pkg/maemo/dorian.desktop\r
-\r
-    icon48.path = $$DATADIR/icons/hicolor/48x48/hildon\r
-    icon48.files += pkg/maemo/icon-48/dorian.png\r
-\r
-    iconscalable.path = $$DATADIR/icons/hicolor/scalable/hildon\r
-    iconscalable.files += pkg/maemo/icon-scalable/dorian.png\r
-}\r
+QT += webkit xml sql network
+
+INCLUDEPATH += $$PWD \
+    $$PWD/model \
+    $$PWD/widgets
+
+SOURCES += \
+    main.cpp \
+    mainwindow.cpp \
+    bookview.cpp \
+    model/unzip/unzip.c \
+    model/unzip/ioapi.c \
+    model/extractzip.cpp \
+    model/library.cpp \
+    model/book.cpp \
+    librarydialog.cpp \
+    devtools.cpp \
+    infodialog.cpp \
+    widgets/translucentbutton.cpp \
+    settingswindow.cpp \
+    model/settings.cpp \
+    bookmarksdialog.cpp \
+    model/sortedlibrary.cpp \
+    bookmarkinfodialog.cpp \
+    widgets/dyalog.cpp \
+    chaptersdialog.cpp \
+    widgets/fullscreenwindow.cpp \
+    trace.cpp \
+    widgets/toolbuttonbox.cpp \
+    model/bookfinder.cpp \
+    widgets/listwindow.cpp \
+    widgets/progress.cpp \
+    widgets/adopterwindow.cpp \
+    platform.cpp \
+    model/bookdb.cpp \
+    searchdialog.cpp \
+    search.cpp \
+    widgets/flickable.cpp \
+    widgets/listview.cpp \
+    searchresultsdialog.cpp
+
+HEADERS += \
+    mainwindow.h \
+    bookview.h \
+    model/opshandler.h \
+    model/unzip/unzip.h \
+    model/unzip/ioapi.h \
+    model/extractzip.h \
+    model/library.h \
+    model/book.h \
+    librarydialog.h \
+    devtools.h \
+    infodialog.h \
+    widgets/translucentbutton.h \
+    settingswindow.h \
+    model/settings.h \
+    bookmarksdialog.h \
+    model/xmlerrorhandler.h \
+    model/containerhandler.h \
+    model/sortedlibrary.h \
+    model/ncxhandler.h \
+    bookmarkinfodialog.h \
+    widgets/dyalog.h \
+    chaptersdialog.h \
+    widgets/fullscreenwindow.h \
+    trace.h \
+    widgets/toolbuttonbox.h \
+    model/bookfinder.h \
+    widgets/listwindow.h \
+    widgets/progress.h \
+    widgets/adopterwindow.h \
+    widgets/listview.h \
+    model/xmlhandler.h \
+    platform.h \
+    model/bookdb.h \
+    searchdialog.h \
+    search.h \
+    widgets/flickable.h \
+    searchresultsdialog.h
+
+RESOURCES += \
+    dorian.qrc
+
+OTHER_FILES += \
+    TODO.txt \
+    pkg/acknowledgements.txt \
+    pkg/maemo/postinst \
+    pkg/maemo/dorian.desktop \
+    pkg/maemo/control \
+    pkg/maemo/changelog \
+    pkg/maemo/build.sh \
+    styles/night.css \
+    pkg/changelog \
+    pkg/maemo/build-scratchbox.sh \
+    styles/sand.css \
+    styles/default.css \
+    pkg/version.txt \
+    styles/sand.js \
+    styles/night.js \
+    styles/default.js \
+    styles/day.js \
+    www/index.html \
+    pkg/maemo/autobuild.sh \
+    pkg/maemo/autobuild-scratchbox.sh \
+    LICENSE.txt \
+    pkg/symbian/book.svg
+
+DEFINES += \
+    USE_FILE32API
+
+!symbian {
+    DEFINES += DORIAN_TEST_MODEL
+    include(model/modeltest/modeltest.pri)
+}
+
+unix {
+    symbian {
+    } else {
+        LIBS += -lz
+    }
+}
+
+win32 {
+    DEFINES += ZLIB_WINAPI
+    INCLUDEPATH += $$PWD/model/zlib
+    LIBS += pkg/win32/zlibstat.lib
+}
+
+symbian {
+    ICON = $$PWD/pkg/symbian/book.svgt
+    TARGET.UID3 = 0xEA633557
+    TARGET.CAPABILITY = NetworkServices
+    # FIXME: Include path to OpenC ZLIB?
+    INCLUDEPATH += c:/Qt/4.7.0/src/3rdparty/zlib
+    HEADERS += widgets/flickcharm.h
+    SOURCES += widgets/flickcharm.cpp
+}
+
+maemo5 {
+    QT += maemo5 dbus
+    isEmpty(PREFIX) {
+        PREFIX = /usr
+    }
+    BINDIR = $$PREFIX/bin
+    DATADIR =$$PREFIX/share
+    DEFINES += DATADIR=\\\"$$DATADIR\\\" PKGDATADIR=\\\"$$PKGDATADIR\\\"
+
+    # For "make install"
+
+    INSTALLS += target desktop icon48 iconscalable
+
+    target.path = $$BINDIR
+
+    desktop.path = $$DATADIR/applications/hildon
+    desktop.files += pkg/maemo/dorian.desktop
+
+    icon48.path = $$DATADIR/icons/hicolor/48x48/hildon
+    icon48.files += pkg/maemo/icon-48/dorian.png
+
+    iconscalable.path = $$DATADIR/icons/hicolor/scalable/hildon
+    iconscalable.files += pkg/maemo/icon-scalable/dorian.png
+}
index 0d7eeb4..bcedcf4 100644 (file)
@@ -20,6 +20,7 @@
 #include "bookfinder.h"
 #include "searchdialog.h"
 #include "platform.h"
+#include "searchresultsdialog.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
@@ -69,6 +70,8 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 
     // Create search dialog
     searchDialog = new SearchDialog(this);
+    connect(Search::instance(), SIGNAL(endSearch()),
+            this, SLOT(showSearchResults()));
 }
 
 void LibraryDialog::onAdd()
@@ -257,3 +260,17 @@ void LibraryDialog::onSearch()
     }
     Search::instance()->start(searchDialog->query());
 }
+
+void LibraryDialog::showSearchResults()
+{
+    QList<Search::Result> results = Search::instance()->results();
+    if (results.count() == 0) {
+        QMessageBox::information(this, tr("Search results"), tr("No books found"));
+        return;
+    }
+
+    SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
+    connect(dialog, SIGNAL(add(const Search::Result &)),
+            this, SLOT(onAddSearchResult(const Search::Result &)));
+    dialog->show();
+}
index 4a08318..6af37b3 100644 (file)
@@ -39,6 +39,7 @@ public slots:
     void onAddFromFolder(const QString &path);
     void onAddFromFolderDone(int added);
     void onSearch();
+    void showSearchResults();
 
 private:
     QString createItemText(const Book *book);
index 2696b80..27e4ed2 100644 (file)
@@ -1,6 +1,10 @@
 #include <QNetworkAccessManager>
 #include <QNetworkReply>
 #include <QNetworkRequest>
+#include <QWebFrame>
+#include <QWebPage>
+#include <QWebElementCollection>
+#include <QWebElement>
 
 #include "search.h"
 #include "platform.h"
@@ -33,21 +37,25 @@ void Search::start(const Query &query)
 
     emit beginSearch();
 
+    searchResults.clear();
     QNetworkRequest request;
     request.setUrl(QUrl("http://www.gutenberg.org/catalog/world/results"));
     // request.setRawHeader("User-Agent", "Dorian " + Platform::version());
+    QString title = query.title;
+    if (title.isEmpty()) {
+        title = ".";
+    }
     QByteArray data;
-    data = "title=" + QUrl::toPercentEncoding(query.title) + "&author=" +
+    data = "title=" + QUrl::toPercentEncoding(title) + "&author=" +
            QUrl::toPercentEncoding(query.author);
-    qDebug() << "Request:" << data;
+    qDebug() << "Request:" << (request.url().toString() + "?" + data);
     reply = manager->post(request, data);
     connect(reply, SIGNAL(finished()), this, SLOT(finished()));
 }
 
 QList<Search::Result> Search::results()
 {
-    QList<Search::Result> ret;
-    return ret;
+    return searchResults;
 }
 
 bool Search::download(const Search::Result &result, const QString &fileName)
@@ -64,6 +72,38 @@ void Search::finished()
     Trace t("Search::finished");
     QByteArray data = reply->readAll();
     qDebug() << data;
+
+    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");
+            QString id = cols[0].toPlainText().trimmed();
+            if (id.isEmpty()) {
+                continue;
+            }
+            QString author = cols[2].toPlainText().trimmed();
+            QStringList titles = cols[3].toPlainText().trimmed().
+                                 split("\n", QString::SkipEmptyParts);
+            Result r;
+            r.authors = author.split("\n", QString::SkipEmptyParts);
+            r.id = id;
+            if (titles.count()) {
+                r.title = titles[0];
+            }
+            r.language = cols[4].toPlainText().trimmed();
+            searchResults.append(r);
+            qDebug() << id;
+            qDebug() << " Authors:" << r.authors;
+            qDebug() << " Title:" << r.title;
+            qDebug() << " Language:" << r.language;
+        }
+    }
+
     reply->deleteLater();
     emit endSearch();
 }
index d11b7e5..4c7ad0e 100644 (file)
--- a/search.h
+++ b/search.h
@@ -54,6 +54,7 @@ protected:
     explicit Search();
     QNetworkAccessManager *manager;
     QNetworkReply *reply;
+    QList<Result> searchResults;
 };
 
 #endif // SEARCH_H
index 9054619..0d9cd87 100644 (file)
@@ -6,11 +6,11 @@
 
 SearchDialog::SearchDialog(QWidget *parent): Dyalog(parent)
 {
-    setWindowTitle(tr("SearchDialog::SearchDialog"));
+    setWindowTitle(tr("SearchDialog"));
 
     QLabel *titleLabel = new QLabel(tr("Title:"), this);
     title = new QLineEdit(this);
-    QLabel *authorLabel = new QLabel(tr("Author"), this);
+    QLabel *authorLabel = new QLabel(tr("Author:"), this);
     author = new QLineEdit(this);
 
     addWidget(titleLabel);
diff --git a/searchresultsdialog.cpp b/searchresultsdialog.cpp
new file mode 100644 (file)
index 0000000..eb447a0
--- /dev/null
@@ -0,0 +1,28 @@
+#include <QtGui>
+
+#include "listview.h"
+#include "searchresultsdialog.h"
+
+SearchResultsDialog::SearchResultsDialog(const QList<Search::Result> results_,
+    QWidget *parent): ListWindow(parent), results(results_)
+{
+    setWindowTitle(tr("Search results"));
+
+    foreach (Search::Result result, results) {
+        QString author;
+        if (result.authors.length()) {
+            author = result.authors[0];
+        }
+        data.append(author + "\n" + result.title);
+    }
+
+    QStringListModel *model = new QStringListModel(data, this);
+    list = new ListView;
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setModel(model);
+    list->setUniformItemSizes(true);
+    addList(list);
+    connect(list, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
+}
+
diff --git a/searchresultsdialog.h b/searchresultsdialog.h
new file mode 100644 (file)
index 0000000..96feeca
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef SEARCHRESULTSDIALOG_H
+#define SEARCHRESULTSDIALOG_H
+
+#include <QList>
+#include <QStringList>
+
+#include "listwindow.h"
+#include "search.h"
+
+class ListView;
+
+class SearchResultsDialog: public ListWindow
+{
+public:
+    explicit SearchResultsDialog(const QList<Search::Result> searchResults,
+                                 QWidget *parent = 0);
+
+protected:
+    const QList<Search::Result> results;
+    ListView *list;
+    QStringList data;
+};
+
+#endif // SEARCHRESULTSDIALOG_H