Warn if selected book is already in the library. Make book info working
[dorian] / mainwindow.cpp
index 36a88fd..56ef2e2 100755 (executable)
 #include "bookmarksdialog.h"
 #include "settings.h"
 
+#ifdef DORIAN_TEST_MODEL
+#include "modeltest.h"
+#endif
+
 #ifdef Q_WS_MAC
 #   define ICON_PREFIX ":/icons/mac/"
 #else
@@ -30,7 +34,7 @@ const Qt::WindowFlags WIN_BIG_FLAGS =
 const int WIN_BIG_TIMER = 3000;
 
 MainWindow::MainWindow(QWidget *parent):
-        QMainWindow(parent), view(0), book(0), isFullscreen(false)
+        QMainWindow(parent), view(0), isFullscreen(false)
 {
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
@@ -76,7 +80,7 @@ MainWindow::MainWindow(QWidget *parent):
                                         "view-fullscreen");
 
     // Handle model changes
-    connect(Library::instance(), SIGNAL(currentBookChanged()),
+    connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
 
     normalFlags = windowFlags();
@@ -87,21 +91,21 @@ MainWindow::MainWindow(QWidget *parent):
     if (QCoreApplication::arguments().size() == 2) {
         QString path = QCoreApplication::arguments()[1];
         library->add(path);
-        int index = library->find(path);
-        if (index != -1) {
-            library->setCurrent(index);
+        QModelIndex index = library->find(path);
+        if (index.isValid()) {
+            library->setNowReading(index);
         }
     }
     else {
-        Book *current = library->current();
-        if (current) {
-            setCurrentBook(current);
+        QModelIndex index = library->nowReading();
+        if (index.isValid()) {
+            library->setNowReading(index);
         }
         else {
-            if (!library->size()) {
+            if (!library->rowCount()) {
                 library->add(":/books/2 B R 0 2 B.epub");
             }
-            library->setCurrent(0);
+            library->setNowReading(library->index(0));
         }
     }
 
@@ -113,11 +117,15 @@ MainWindow::MainWindow(QWidget *parent):
 
     // Handle loading chapters
     connect(view, SIGNAL(chapterLoaded(int)), this, SLOT(onChapterLoaded(int)));
+
+#ifdef DORIAN_TEST_MODEL
+    (void)new ModelTest(Library::instance(), this);
+#endif
 }
 
 void MainWindow::onCurrentBookChanged()
 {
-    setCurrentBook(Library::instance()->current());
+    setCurrentBook(Library::instance()->nowReading());
 }
 
 void MainWindow::showNormal()
@@ -143,14 +151,21 @@ void MainWindow::showFullScreen()
     restoreButton->flash();
 }
 
-void MainWindow::setCurrentBook(Book *current)
+void MainWindow::setCurrentBook(const QModelIndex &current)
 {
-    book = current;
-    view->setBook(current);
-    setWindowTitle(current? current->title: "Dorian");
+    mCurrent = current;
+    if (current.isValid()) {
+        Book *book = Library::instance()->book(current);
+        view->setBook(book);
+        setWindowTitle(book->name());
+    } else {
+        view->setBook(0);
+        setWindowTitle("Dorian");
+    }
 }
 
-QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
+QAction *MainWindow::addToolBarAction(const QObject *receiver,
+                                      const char *member,
                                       const QString &name)
 {
     return toolBar->
@@ -171,8 +186,9 @@ void MainWindow::showSettings()
 
 void MainWindow::showInfo()
 {
-    if (book) {
-        InfoDialog *info = new InfoDialog(book, this);
+    if (mCurrent.isValid()) {
+        InfoDialog *info =
+            new InfoDialog(Library::instance()->book(mCurrent), this);
         info->exec();
     }
 }
@@ -185,6 +201,7 @@ void MainWindow::showDevTools()
 
 void MainWindow::showBookmarks()
 {
+    Book *book = Library::instance()->book(mCurrent);
     if (book) {
         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
         int ret = bookmarks->exec();
@@ -250,6 +267,7 @@ void MainWindow::onChapterLoaded(int index)
 {
     bool enablePrevious = false;
     bool enableNext = false;
+    Book *book = Library::instance()->book(mCurrent);
     if (book) {
         if (index > 0) {
             enablePrevious = true;