Fix sorting of library. Add basic search framework.
authorAkos Polster <akos@pipacs.com>
Sun, 10 Oct 2010 18:58:13 +0000 (20:58 +0200)
committerAkos Polster <akos@pipacs.com>
Sun, 10 Oct 2010 18:58:13 +0000 (20:58 +0200)
15 files changed:
dorian.pro
librarydialog.cpp
librarydialog.h
main.cpp
model/settings.cpp
model/settings.h
model/sortedlibrary.cpp
pkg/changelog
platform.cpp
platform.h
search.cpp [new file with mode: 0644]
search.h [new file with mode: 0644]
searchdialog.cpp [new file with mode: 0644]
searchdialog.h [new file with mode: 0644]
widgets/listwindow.cpp

index cab9378..2bdb4b7 100644 (file)
@@ -32,7 +32,9 @@ SOURCES += \
     widgets/progress.cpp \
     widgets/adopterwindow.cpp \
     platform.cpp \
-    model/bookdb.cpp
+    model/bookdb.cpp \
+    searchdialog.cpp \
+    search.cpp
 
 HEADERS += \
     mainwindow.h \
@@ -67,7 +69,9 @@ HEADERS += \
     widgets/listview.h \
     model/xmlhandler.h \
     platform.h \
-    model/bookdb.h
+    model/bookdb.h \
+    searchdialog.h \
+    search.h
 
 RESOURCES += \
     dorian.qrc
index 378f48e..9ae3123 100644 (file)
@@ -18,6 +18,7 @@
 #include "listview.h"
 #include "trace.h"
 #include "bookfinder.h"
+#include "searchdialog.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
@@ -31,10 +32,9 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     addItemAction(tr("Delete"), this, SLOT(onRemove()));
 #endif // ! Q_WS_MAEMO_5
 
-    addAction(tr("Add book"), this, SLOT(onAdd()), ":/icons/add.png");
-    addAction(tr("Add books from folder"), this, SLOT(onAddFolder()),
-              ":/icons/folder.png");
-    addAction(tr("Search the Web"), this, SLOT(onSearch()), ":/icons/search.png");
+    addAction(tr("Add book"), this, SLOT(onAdd()), "add");
+    addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
+    addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
 
     // Create and add list view
     list = new ListView(this);
@@ -61,6 +61,9 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
             SLOT(onBookAdded()));
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
+
+    // Create search dialog
+    searchDialog = new SearchDialog(this);
 }
 
 void LibraryDialog::onAdd()
@@ -240,3 +243,12 @@ void LibraryDialog::onAddFromFolder(const QString &path)
     progress->setLabelText(QFileInfo(path).fileName());
     progress->setValue(progress->value() + 1);
 }
+
+void LibraryDialog::onSearch()
+{
+    int ret = searchDialog->exec();
+    if (ret != QDialog::Accepted) {
+        return;
+    }
+    Search::instance()->start(searchDialog->query());
+}
index ccbe43f..4a08318 100644 (file)
@@ -15,6 +15,7 @@ class QProgressDialog;
 class Book;
 class InfoWindow;
 class SortedLibrary;
+class SearchDialog;
 
 /** Manage library. */
 class LibraryDialog: public ListWindow
@@ -37,6 +38,8 @@ public slots:
     void onCurrentBookChanged();
     void onAddFromFolder(const QString &path);
     void onAddFromFolderDone(int added);
+    void onSearch();
+
 private:
     QString createItemText(const Book *book);
     void setSelected(const QModelIndex &index);
@@ -44,6 +47,7 @@ private:
     ListView *list;
     SortedLibrary *sortedLibrary;
     QProgressDialog *progress;
+    SearchDialog *searchDialog;
 };
 
 #endif // LIBRARYDIALOG_H
index c1b25ce..5977925 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,12 +1,13 @@
-#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
-#include <unistd.h>
-#endif
-
 #include <QtGui/QApplication>
 
 #include "mainwindow.h"
 #include "trace.h"
 #include "settings.h"
+#include "library.h"
+#include "settings.h"
+#include "bookdb.h"
+#include "search.h"
+#include "platform.h"
 
 static const char *DORIAN_VERSION =
 #include "pkg/version.txt"
@@ -23,6 +24,7 @@ static const QtMsgType DORIAN_DEFAULT_TRACE_LEVEL =
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
+    int ret;
 
     a.setApplicationName("Dorian");
     a.setApplicationVersion(DORIAN_VERSION);
@@ -33,22 +35,21 @@ int main(int argc, char *argv[])
         value("tracelevel", (int)DORIAN_DEFAULT_TRACE_LEVEL).toInt();
     qInstallMsgHandler(Trace::messageHandler);
 
-    MainWindow w;
-    w.show();
-
-#if 0 // FIXME #ifdef Q_OS_SYMBIAN
-    // Remove context menu from all widgets
-    foreach (QWidget *w, QApplication::allWidgets()) {
-        w->setContextMenuPolicy(Qt::NoContextMenu);
+    {
+        MainWindow w;
+        w.show();
+        ret = a.exec();
     }
-#endif // Q_OS_SYMBIAN
 
-    int ret = a.exec();
+    // Release singletons
+    Library::close();
+    BookDb::close();
+    Settings::close();
+    Search::close();
+
+    // Re-start application if event loop exit code was 1000
     if (ret == 1000) {
-#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
-        extern char **environ;
-        execve(argv[0], argv, environ);
-#endif
+        Platform::restart(argv);
     }
     return ret;
 }
index ea2f8c2..5e853ac 100644 (file)
@@ -17,6 +17,12 @@ Settings *Settings::instance()
     return inst;
 }
 
+void Settings::close()
+{
+    delete inst;
+    inst = 0;
+}
+
 void Settings::setValue(const QString &key, const QVariant &value)
 {
     QSettings s;
index cb4df46..ad09efe 100644 (file)
@@ -17,6 +17,7 @@ class Settings: public QObject
 public:
     explicit Settings(QObject *parent = 0);
     static Settings *instance();
+    static void close();
     QVariant value(const QString &key, const QVariant &defaultValue = QVariant())
         const;
     void setValue(const QString &key, const QVariant &value);
index 9091263..3a0fb54 100644 (file)
@@ -1,9 +1,12 @@
 #include "sortedlibrary.h"
 #include "book.h"
+#include "trace.h"
 
-SortedLibrary::SortedLibrary(QObject *parent): QSortFilterProxyModel(parent)
+SortedLibrary::SortedLibrary(QObject *parent):
+        QSortFilterProxyModel(parent), mSortBy(SortByTitle)
 {
     setSourceModel(Library::instance());
+    sort(0);
 }
 
 void SortedLibrary::sortBy(SortBy key)
@@ -31,5 +34,5 @@ bool SortedLibrary::lessThan(const QModelIndex &left,
         break;
     }
 
-    return QString::localeAwareCompare(leftString, rightString);
+    return QString::localeAwareCompare(leftString, rightString) < 0;
 }
index 9efba85..4311462 100644 (file)
@@ -1,6 +1,6 @@
 dorian (0.3.1-1) unstable; urgency=low
 
-  *
+  * Fix sorting of library
 
  -- Akos Polster <akos@pipacs.com>  Sat, 10 Oct 2010 02:00:00 +0200
 
index d93c63d..b86a359 100644 (file)
@@ -1,3 +1,7 @@
+#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
+#include <unistd.h>
+#endif
+
 #include <QDir>
 
 #include "platform.h"
@@ -24,3 +28,13 @@ QString Platform::icon(const QString &name)
 {
     return QString(DORIAN_ICON_PREFIX) + name + ".png";
 }
+
+void Platform::restart(char *argv[])
+{
+#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
+    extern char **environ;
+    execve(argv[0], argv, environ);
+#else
+    Q_UNUSED(argv);
+#endif
+}
index b10d17e..f4baaad 100644 (file)
@@ -8,6 +8,7 @@ class Platform
 public:
     static QString dbPath();
     static QString icon(const QString &name);
+    static void restart(char *argv[]);
 };
 
 #endif // PLATFORM_H
diff --git a/search.cpp b/search.cpp
new file mode 100644 (file)
index 0000000..4ed8768
--- /dev/null
@@ -0,0 +1,42 @@
+#include "search.h"
+
+Search *inst = 0;
+
+Search *Search::instance()
+{
+    if (!inst) {
+        inst = new Search();
+    }
+    return inst;
+}
+
+void Search::close()
+{
+    delete inst;
+    inst = 0;
+}
+
+Search::Search(): QObject(0)
+{
+}
+
+void Search::start(const Query &query)
+{
+    emit beginSearch();
+    emit endSearch();
+}
+
+QList<Search::Result> Search::results()
+{
+    QList<Search::Result> ret;
+    return ret;
+}
+
+bool Search::download(const Search::Result &result, const QString &fileName)
+{
+    Q_UNUSED(result);
+    Q_UNUSED(fileName);
+    emit beginDownload(0);
+    emit endDownload();
+    return false;
+}
diff --git a/search.h b/search.h
new file mode 100644 (file)
index 0000000..47e5b1a
--- /dev/null
+++ b/search.h
@@ -0,0 +1,52 @@
+#ifndef SEARCH_H
+#define SEARCH_H
+
+#include <QObject>
+#include <QString>
+#include <QStringList>
+#include <QImage>
+#include <QList>
+
+class Search: public QObject
+{
+    Q_OBJECT
+
+public:
+    struct Query
+    {
+        QString title;
+        QString author;
+        QStringList languages;
+    };
+
+    struct Result
+    {
+        QString id;
+        QString source;
+        QString title;
+        QStringList authors;
+        QString language;
+        QImage cover;
+    };
+
+    static Search *instance();
+    static void close();
+
+signals:
+    void beginSearch();
+    void searching();
+    void endSearch();
+    void beginDownload(int totalBlocks);
+    void downloading(int blocks);
+    void endDownload();
+
+public slots:
+    void start(const Query &query);
+    QList<Result> results();
+    bool download(const Result &result, const QString &fileName);
+
+protected:
+    explicit Search();
+};
+
+#endif // SEARCH_H
diff --git a/searchdialog.cpp b/searchdialog.cpp
new file mode 100644 (file)
index 0000000..da5182a
--- /dev/null
@@ -0,0 +1,29 @@
+#include <QtGui>
+
+#include "searchdialog.h"
+#include "search.h"
+
+SearchDialog::SearchDialog(QWidget *parent): Dyalog(parent)
+{
+    setWindowTitle(tr("Search"));
+
+    QLabel *titleLabel = new QLabel(tr("Title:"), this);
+    title = new QLineEdit(this);
+    QLabel *authorLabel = new QLabel(tr("Author"), this);
+    author = new QLineEdit(this);
+
+    addWidget(titleLabel);
+    addWidget(title);
+    addWidget(authorLabel);
+    addWidget(author);
+    addStretch();
+    addButton(tr("Search"), this, SLOT(accept()));
+}
+
+Search::Query SearchDialog::query()
+{
+    Search::Query ret;
+    ret.title = title->text();
+    ret.author = author->text();
+    return ret;
+}
diff --git a/searchdialog.h b/searchdialog.h
new file mode 100644 (file)
index 0000000..7f8a19b
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef SEARCHDIALOG_H
+#define SEARCHDIALOG_H
+
+#include <QVariantHash>
+
+#include "dyalog.h"
+#include "search.h"
+
+class QLineEdit;
+
+class SearchDialog : public Dyalog
+{
+    Q_OBJECT
+
+public:
+    explicit SearchDialog(QWidget *parent = 0);
+    Search::Query query();
+
+signals:
+
+public slots:
+
+protected:
+    QLineEdit *author;
+    QLineEdit *title;
+};
+
+#endif // SEARCHDIALOG_H
index 98ad89c..48ef599 100644 (file)
@@ -3,6 +3,7 @@
 #include "listwindow.h"
 #include "trace.h"
 #include "listview.h"
+#include "platform.h"
 
 ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
 {
@@ -72,23 +73,25 @@ void ListWindow::addList(ListView *listView)
 }
 
 void ListWindow::addAction(const QString &title, QObject *receiver,
-                           const char *slot, const QString &iconPath,
+                           const char *slot, const QString &iconName,
                            QDialogButtonBox::ButtonRole role)
 {
     Trace t("ListWindow::addAction");
 #ifdef Q_WS_MAEMO_5
     Q_UNUSED(role);
-    QPushButton *button = new QPushButton(QIcon(iconPath), title, this);
+    QPushButton *button =
+            new QPushButton(QIcon(Platform::icon(iconName)), title, this);
     contentLayout->addWidget(button);
     connect(button, SIGNAL(clicked()), receiver, slot);
 #elif defined(Q_OS_SYMBIAN)
     Q_UNUSED(role);
+    Q_UNUSED(iconName);
     QAction *action = new QAction(title, this);
     connect(action, SIGNAL(triggered()), receiver, slot);
     action->setSoftKeyRole(QAction::PositiveSoftKey);
     menuBar()->addAction(action);
 #else
-    Q_UNUSED(iconPath);
+    Q_UNUSED(iconName);
     QPushButton *button = buttonBox->addButton(title, role);
     connect(button, SIGNAL(clicked()), receiver, slot);
 #endif // Q_WS_MAEMO_5