First attempts towards monitoring folders with books.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6 #include <QStringList>
7
8 #ifdef Q_WS_MAEMO_5
9 #   include <QtMaemo5/QMaemo5InformationBox>
10 #   include <QtDBus>
11 #   include <mce/mode-names.h>
12 #   include <mce/dbus-names.h>
13 #endif // Q_WS_MAEMO_5
14
15 #include "bookview.h"
16 #include "book.h"
17 #include "library.h"
18 #include "infodialog.h"
19 #include "librarydialog.h"
20 #include "devtools.h"
21 #include "mainwindow.h"
22 #include "settingswindow.h"
23 #include "bookmarksdialog.h"
24 #include "settings.h"
25 #include "chaptersdialog.h"
26 #include "fullscreenwindow.h"
27 #include "trace.h"
28 #include "bookfinder.h"
29
30 #ifdef DORIAN_TEST_MODEL
31 #include "modeltest.h"
32 #endif
33
34 #ifdef Q_WS_MAC
35 #   define ICON_PREFIX ":/icons/mac/"
36 #else
37 #   define ICON_PREFIX ":/icons/"
38 #endif
39
40 MainWindow::MainWindow(QWidget *parent):
41     QMainWindow(parent), view(0), preventBlankingTimer(-1)
42 {
43     Trace t("MainWindow::MainWindow");
44 #ifdef Q_WS_MAEMO_5
45     setAttribute(Qt::WA_Maemo5StackedWindow, true);
46 #endif
47     setWindowTitle("Dorian");
48
49     // Central widget. Must be an intermediate, because the book view widget
50     // can be re-parented later
51     QFrame *central = new QFrame(this);
52     QVBoxLayout *layout = new QVBoxLayout(central);
53     layout->setMargin(0);
54     central->setLayout(layout);
55     setCentralWidget(central);
56
57     // Book view
58     view = new BookView(central);
59     view->show();
60     layout->addWidget(view);
61
62     // Tool bar
63     setUnifiedTitleAndToolBarOnMac(true);
64     settings = new QDialog(this);
65     toolBar = addToolBar("controls");
66     toolBar->setMovable(false);
67     toolBar->setFloatable(false);
68     toolBar->toggleViewAction()->setVisible(false);
69 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
70     toolBar->setIconSize(QSize(42, 42));
71 #endif
72
73     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
74     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
75     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
76     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
77
78 #ifdef Q_WS_MAEMO_5
79     infoAction = menuBar()->addAction(tr("Book details"));
80     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
81     libraryAction = menuBar()->addAction(tr("Library"));
82     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
83     settingsAction = menuBar()->addAction(tr("Settings"));
84     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
85     devToolsAction = menuBar()->addAction(tr("Developer"));
86     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
87 #else
88     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
89     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
90                                      "system-file-manager");
91     settingsAction = addToolBarAction(this, SLOT(showSettings()),
92                                       "preferences-system");
93     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
94 #endif // Q_WS_MAEMO_5
95
96     QFrame *frame = new QFrame(toolBar);
97     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
98     toolBar->addWidget(frame);
99
100     fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
101
102     // Handle model changes
103     connect(Library::instance(), SIGNAL(nowReadingChanged()),
104             this, SLOT(onCurrentBookChanged()));
105
106     // Load book on command line, or load last read book, or load default book
107     Library *library = Library::instance();
108     if (QCoreApplication::arguments().size() == 2) {
109         QString path = QCoreApplication::arguments()[1];
110         library->add(path);
111         QModelIndex index = library->find(path);
112         if (index.isValid()) {
113             library->setNowReading(index);
114         }
115     }
116     else {
117         QModelIndex index = library->nowReading();
118         if (index.isValid()) {
119             library->setNowReading(index);
120         }
121         else {
122             if (!library->rowCount()) {
123                 library->add(":/books/2 B R 0 2 B.epub");
124             }
125             library->setNowReading(library->index(0));
126         }
127     }
128
129     // Handle settings changes
130     Settings *settings = Settings::instance();
131     connect(settings, SIGNAL(valueChanged(const QString &)),
132             this, SLOT(onSettingsChanged(const QString &)));
133     settings->setValue("orientation", settings->value("orientation"));
134     settings->setValue("lightson", settings->value("lightson"));
135
136     // Handle loading chapters
137     connect(view, SIGNAL(chapterLoadStart(int)),
138             this, SLOT(onChapterLoadStart()));
139     connect(view, SIGNAL(chapterLoadEnd(int)),
140             this, SLOT(onChapterLoadEnd(int)));
141
142     // Shadow window for full screen
143     fullScreenWindow = new FullScreenWindow(this);
144     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
145
146     // Create thread for finding books in directories
147     bookFinder = new BookFinder();
148     connect(bookFinder, SIGNAL(add(const QString &)),
149             this, SLOT(onAddBook(const QString &)));
150     connect(bookFinder, SIGNAL(remove(const QString &)),
151             this, SLOT(onRemoveBook(const QString &)));
152     bookFinder->moveToThread(&bookFinderThread);
153     bookFinderThread.start();
154     qRegisterMetaType<QStringList>("QStringList");
155
156     bool ret = QMetaObject::invokeMethod(
157         bookFinder,
158         "find",
159         Q_ARG(QStringList, QStringList(QString("/home/polster/Books"))),
160         Q_ARG(QStringList, library->bookPaths()));
161     t.trace(QString("Invoking BookFinder::find ") + (ret?"succeeded":"failed"));
162
163 #ifdef DORIAN_TEST_MODEL
164     (void)new ModelTest(Library::instance(), this);
165 #endif
166 }
167
168 MainWindow::~MainWindow()
169 {
170     bookFinderThread.quit();
171     bookFinderThread.wait();
172     delete bookFinder;
173 }
174
175 void MainWindow::onCurrentBookChanged()
176 {
177     setCurrentBook(Library::instance()->nowReading());
178 }
179
180 void MainWindow::showRegular()
181 {
182     Trace t("MainWindow::showRegular");
183     fullScreenWindow->hide();
184     fullScreenWindow->leaveChild();
185     view->setParent(centralWidget());
186     centralWidget()->layout()->addWidget(view);
187 }
188
189 void MainWindow::showBig()
190 {
191     Trace t("MainWindow::showBig");
192     centralWidget()->layout()->removeWidget(view);
193     fullScreenWindow->takeChild(view);
194     fullScreenWindow->showFullScreen();
195 }
196
197 void MainWindow::setCurrentBook(const QModelIndex &current)
198 {
199     mCurrent = current;
200     Book *book = Library::instance()->book(current);
201     view->setBook(book);
202     setWindowTitle(book? book->name(): tr("Dorian"));
203 }
204
205 QAction *MainWindow::addToolBarAction(const QObject *receiver,
206                                       const char *member,
207                                       const QString &name)
208 {
209     return toolBar->
210         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
211 }
212
213 void MainWindow::showLibrary()
214 {
215     LibraryDialog *dialog = new LibraryDialog(this);
216     dialog->show();
217 }
218
219 void MainWindow::showSettings()
220 {
221     SettingsWindow *settings = new SettingsWindow(this);
222     settings->show();
223 }
224
225 void MainWindow::showInfo()
226 {
227     if (mCurrent.isValid()) {
228         InfoDialog *info =
229             new InfoDialog(Library::instance()->book(mCurrent), this);
230         info->exec();
231     }
232 }
233
234 void MainWindow::showDevTools()
235 {
236     DevTools *devTools = new DevTools();
237     devTools->exec();
238 }
239
240 void MainWindow::showBookmarks()
241 {
242     Book *book = Library::instance()->book(mCurrent);
243     if (book) {
244         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
245         bookmarks->setWindowModality(Qt::WindowModal);
246         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
247         connect(bookmarks, SIGNAL(goToBookmark(int)),
248                 this, SLOT(onGoToBookmark(int)));
249         bookmarks->show();
250     }
251 }
252
253 void MainWindow::closeEvent(QCloseEvent *event)
254 {
255     Trace t("MainWindow::closeEvent");
256     view->setLastBookmark();
257     event->accept();
258 }
259
260 void MainWindow::onSettingsChanged(const QString &key)
261 {
262 #ifdef Q_WS_MAEMO_5
263     if (key == "orientation") {
264         QString value = Settings::instance()->value(key).toString();
265         Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
266                      arg(value));
267         if (value == "portrait") {
268             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
269             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
270         }
271         else {
272             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
273             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
274         }
275     } else if (key == "lightson") {
276         bool enable = Settings::instance()->value(key, false).toBool();
277         Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
278                      arg(enable));
279         killTimer(preventBlankingTimer);
280         if (enable) {
281             preventBlankingTimer = startTimer(29 * 1000);
282         }
283     }
284 #else
285     Q_UNUSED(key);
286 #endif // Q_WS_MAEMO_5
287 }
288
289 void MainWindow::onChapterLoadStart()
290 {
291     Trace t("MainWindow::onChapterLoadStart");
292 #ifdef Q_WS_MAEMO_5
293     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
294 #endif
295 }
296
297 void MainWindow::onChapterLoadEnd(int index)
298 {
299     Trace t("MainWindow::onChapterLoadEnd");
300     bool enablePrevious = false;
301     bool enableNext = false;
302     Book *book = Library::instance()->book(mCurrent);
303     if (book) {
304         if (index > 0) {
305             enablePrevious = true;
306         }
307         if (index < (book->toc.size() - 1)) {
308             enableNext = true;
309         }
310     }
311 #ifdef Q_WS_MAEMO_5
312     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
313     previousAction->setIcon(QIcon(enablePrevious?
314         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
315     nextAction->setIcon(QIcon(enableNext?
316         ":/icons/next.png": ":/icons/next-disabled.png"));
317 #endif // Q_WS_MAEMO_5
318     previousAction->setEnabled(enablePrevious);
319     nextAction->setEnabled(enableNext);
320 }
321
322 void MainWindow::onAddBookmark()
323 {
324     Trace t("MainWindow:onAddBookmark");
325     view->addBookmark();
326 }
327
328 void MainWindow::onGoToBookmark(int index)
329 {
330     Trace t("MainWindow::onGoToBookmark");
331     Book *book = Library::instance()->book(mCurrent);
332     view->goToBookmark(book->bookmarks()[index]);
333 }
334
335 void MainWindow::showChapters()
336 {
337     Book *book = Library::instance()->book(mCurrent);
338     if (book) {
339         ChaptersDialog *chapters = new ChaptersDialog(book, this);
340         chapters->setWindowModality(Qt::WindowModal);
341         connect(chapters, SIGNAL(goToChapter(int)),
342                 this, SLOT(onGoToChapter(int)));
343         chapters->show();
344     }
345 }
346
347 void MainWindow::onGoToChapter(int index)
348 {
349     view->goToBookmark(Book::Bookmark(index, 0));
350 }
351
352 void MainWindow::timerEvent(QTimerEvent *event)
353 {
354     if (event->timerId() == preventBlankingTimer) {
355 #ifdef Q_WS_MAEMO_5
356         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
357                            MCE_REQUEST_IF, QDBusConnection::systemBus());
358         mce.call(MCE_PREVENT_BLANK_REQ);
359 #endif // Q_WS_MAEMO_5
360         Trace::trace("MainWindow::timerEvent: Prevent display blanking");
361     }
362 }