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