Clean up book view decorations code.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QEvent>
3
4 #ifdef Q_WS_MAEMO_5
5 #   include <QtDBus>
6 #   include <QtGui/QX11Info>
7 #   include <X11/Xlib.h>
8 #   include <X11/Xatom.h>
9 #   include <mce/mode-names.h>
10 #   include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
12
13 #include "bookview.h"
14 #include "book.h"
15 #include "library.h"
16 #include "infodialog.h"
17 #include "librarydialog.h"
18 #include "devtools.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
22 #include "settings.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
25 #include "trace.h"
26 #include "bookfinder.h"
27 #include "progress.h"
28 #include "dyalog.h"
29 #include "translucentbutton.h"
30 #include "platform.h"
31 #include "progressdialog.h"
32 #include "sortedlibrary.h"
33
34 #ifdef DORIAN_TEST_MODEL
35 #   include "modeltest.h"
36 #endif
37
38 MainWindow::MainWindow(QWidget *parent):
39     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
40 {
41     TRACE;
42 #ifdef Q_WS_MAEMO_5
43     setAttribute(Qt::WA_Maemo5StackedWindow, true);
44 #endif
45     setWindowTitle("Dorian");
46
47     // Central widget. Must be an intermediate, because the book view widget
48     // can be re-parented later
49     QFrame *central = new QFrame(this);
50     QVBoxLayout *layout = new QVBoxLayout(central);
51     layout->setMargin(0);
52     central->setLayout(layout);
53     setCentralWidget(central);
54
55     // Book view
56     view = new BookView(this);
57     view->show();
58
59     // Tool bar actions
60
61 #ifdef Q_OS_SYMBIAN
62     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
63                                         "view-fullscreen", tr("Full screen"));
64 #endif
65
66     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
67                                       "chapters", tr("Chapters"), true);
68     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
69                                        "bookmarks", tr("Bookmarks"), true);
70     infoAction = addToolBarAction(this, SLOT(showInfo()),
71                                   "info", tr("Book info"), true);
72     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
73                                      "library", tr("Library"), true);
74
75 #ifdef Q_WS_MAEMO_5
76     settingsAction = menuBar()->addAction(tr("Settings"));
77     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
78     devToolsAction = menuBar()->addAction(tr("Developer"));
79     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
80     QAction *aboutAction = menuBar()->addAction(tr("About"));
81     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
82 #else
83     settingsAction = addToolBarAction(this, SLOT(showSettings()),
84                                       "preferences-system", tr("Settings"));
85     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
86                                       "developer", tr("Developer"));
87     addToolBarAction(this, SLOT(about()), "about", tr("About"));
88 #endif // Q_WS_MAEMO_5
89
90 #ifndef Q_OS_SYMBIAN
91     addToolBarSpace();
92     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
93                                         "view-fullscreen", tr("Full screen"));
94 #else
95     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
96 #endif
97
98     // Decorations
99     prev = new TranslucentButton("back", this);
100     next = new TranslucentButton("forward", this);
101     prog = new Progress(this);
102
103     // Handle model changes
104     connect(Library::instance(), SIGNAL(nowReadingChanged()),
105             this, SLOT(onCurrentBookChanged()));
106
107     // Load library, upgrade it if needed
108     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
109     Library *library = Library::instance();
110     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
111     connect(library, SIGNAL(upgrading(const QString &)),
112             this, SLOT(onUpgrading(const QString &)));
113     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
114
115     // Handle loading book parts
116     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
117     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
118
119     // Handle progress
120     connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
121
122     // Shadow window for full screen reading
123     fullScreenWindow = new FullScreenWindow(this);
124     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
125
126     // Handle settings changes
127     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
128             this, SLOT(onSettingsChanged(const QString &)));
129
130     // Handle book view buttons
131     connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
132     connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
133
134     // Adopt view, show window
135     showRegular();
136
137 #ifdef DORIAN_TEST_MODEL
138     (void)new ModelTest(Library::instance(), this);
139 #endif
140 }
141
142 void MainWindow::initialize()
143 {
144     TRACE;
145     Library *library = Library::instance();
146
147     // Upgrade library if needed, then load it
148     library->upgrade();
149     library->load();
150
151     // Load book on command line, or load last read book, or load default book
152     if (QCoreApplication::arguments().size() == 2) {
153         QString path = QCoreApplication::arguments()[1];
154         library->add(path);
155         QModelIndex index = library->find(path);
156         if (index.isValid()) {
157             library->setNowReading(index);
158         }
159     } else {
160         QModelIndex index = library->nowReading();
161         if (index.isValid()) {
162             library->setNowReading(index);
163         } else {
164             if (!library->rowCount()) {
165                 library->add(":/books/2BR02B.epub");
166             }
167             SortedLibrary sorted;
168             library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
169         }
170     }
171 }
172
173 void MainWindow::onCurrentBookChanged()
174 {
175     TRACE;
176     setCurrentBook(Library::instance()->nowReading());
177 }
178
179 void MainWindow::showRegular()
180 {
181     TRACE;
182
183     // Re-parent children
184     fullScreenWindow->leaveBookView();
185     takeBookView(view, prog, prev, next);
186
187     fullScreenWindow->hide();
188     show();
189 #if defined(Q_OS_SYMBIAN)
190     activateWindow();
191 #endif
192 }
193
194 void MainWindow::showBig()
195 {
196     TRACE;
197
198     // Re-parent children
199     leaveBookView();
200     fullScreenWindow->takeBookView(view, prog, prev, next);
201
202 // #ifdef Q_OS_SYMBIAN
203     hide();
204 // #endif
205
206     fullScreenWindow->showFullScreen();
207 #ifdef Q_OS_SYMBIAN
208     fullScreenWindow->activateWindow();
209 #endif
210 }
211
212 void MainWindow::setCurrentBook(const QModelIndex &current)
213 {
214     mCurrent = current;
215     Book *book = Library::instance()->book(current);
216     view->setBook(book);
217     setWindowTitle(book? book->shortName(): tr("Dorian"));
218 }
219
220 void MainWindow::showLibrary()
221 {
222     (new LibraryDialog(this))->show();
223 }
224
225 void MainWindow::showSettings()
226 {
227     (new SettingsWindow(this))->show();
228 }
229
230 void MainWindow::showInfo()
231 {
232     if (mCurrent.isValid()) {
233         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
234                 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         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
249                 this, SLOT(onAddBookmark(const QString &)));
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;
259     view->setLastBookmark();
260     AdopterWindow::closeEvent(event);
261 }
262
263 void MainWindow::onSettingsChanged(const QString &key)
264 {
265 #if defined(Q_WS_MAEMO_5)
266     if (key == "orientation") {
267         QString value = Settings::instance()->value(key,
268             Platform::instance()->defaultOrientation()).toString();
269         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
270         if (value == "portrait") {
271             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
272             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
273             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
274                                            false);
275             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
276         } else {
277             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
278             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
279             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
280                                            false);
281             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
282                                            true);
283         }
284     } else if (key == "lightson") {
285         bool enable = Settings::instance()->value(key, false).toBool();
286         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
287         killTimer(preventBlankingTimer);
288         if (enable) {
289             preventBlankingTimer = startTimer(29 * 1000);
290         }
291     }
292 #else
293     Q_UNUSED(key);
294 #endif // Q_WS_MAEMO_5
295 }
296
297 void MainWindow::onPartLoadStart()
298 {
299     TRACE;
300     Platform::instance()->showBusy(this, true);
301 }
302
303 void MainWindow::onPartLoadEnd(int index)
304 {
305     TRACE;
306     bool enablePrevious = false;
307     bool enableNext = false;
308     Book *book = Library::instance()->book(mCurrent);
309     if (book) {
310         if (index > 0) {
311             enablePrevious = true;
312         }
313         if (index < (book->parts.size() - 1)) {
314             enableNext = true;
315         }
316     }
317     Platform::instance()->showBusy(this, false);
318 }
319
320 void MainWindow::onAddBookmark(const QString &note)
321 {
322     TRACE;
323     view->addBookmark(note);
324     Platform::instance()->information(tr("Bookmarked current position"), this);
325 }
326
327 void MainWindow::onGoToBookmark(int index)
328 {
329     TRACE;
330     Book *book = Library::instance()->book(mCurrent);
331     view->goToBookmark(book->bookmarks()[index]);
332 }
333
334 void MainWindow::showChapters()
335 {
336     Book *book = Library::instance()->book(mCurrent);
337     if (book) {
338         ChaptersDialog *chapters = new ChaptersDialog(book, this);
339         connect(chapters, SIGNAL(goToChapter(int)),
340                 this, SLOT(onGoToChapter(int)));
341         chapters->show();
342     }
343 }
344
345 void MainWindow::onGoToChapter(int index)
346 {
347     TRACE;
348
349     Book *book = Library::instance()->book(mCurrent);
350     if (book) {
351         QString fragment;
352         int partIndex = book->partFromChapter(index, fragment);
353         if (partIndex != -1) {
354             view->goToPart(partIndex, fragment);
355         }
356     }
357 }
358
359 void MainWindow::timerEvent(QTimerEvent *event)
360 {
361     if (event->timerId() == preventBlankingTimer) {
362 #ifdef Q_WS_MAEMO_5
363         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
364                            MCE_REQUEST_IF, QDBusConnection::systemBus());
365         mce.call(MCE_PREVENT_BLANK_REQ);
366 #endif // Q_WS_MAEMO_5
367         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
368     }
369     AdopterWindow::timerEvent(event);
370 }
371
372 void MainWindow::about()
373 {
374     Dyalog *aboutDialog = new Dyalog(this, false);
375     aboutDialog->setWindowTitle(tr("About Dorian"));
376     QString version = Platform::instance()->version();
377     QLabel *label = new QLabel(aboutDialog);
378     label->setTextFormat(Qt::RichText);
379     label->setOpenExternalLinks(true);
380     label->setWordWrap(true);
381     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
382         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
383         "Licensed under GNU General Public License, Version 3<br>"
384         "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
385         "dorian.garage.maemo.org</a>").arg(version));
386     aboutDialog->addWidget(label);
387     aboutDialog->addStretch();
388     aboutDialog->show();
389 }
390
391 void MainWindow::goToNextPage()
392 {
393     next->flash();
394     prev->flash();
395     view->goNextPage();
396 }
397
398 void MainWindow::goToPreviousPage()
399 {
400     next->flash();
401     prev->flash();
402     view->goPreviousPage();
403 }
404
405 void MainWindow::onBeginUpgrade(int total)
406 {
407     libraryProgress->setVisible(total > 0);
408     libraryProgress->setWindowTitle(tr("Upgrading library"));
409     libraryProgress->setMaximum(total);
410 }
411
412 void MainWindow::onUpgrading(const QString &path)
413 {
414     libraryProgress->setLabelText(tr("Upgrading %1").
415                                   arg(QFileInfo(path).fileName()));
416     libraryProgress->setValue(libraryProgress->value() + 1);
417 }
418
419 void MainWindow::onEndUpgrade()
420 {
421     libraryProgress->hide();
422     libraryProgress->reset();
423 }
424
425 void MainWindow::onBeginLoad(int total)
426 {
427     libraryProgress->setVisible(total > 0);
428     libraryProgress->setWindowTitle(tr("Loading library"));
429     libraryProgress->setMaximum(total);
430 }
431
432 void MainWindow::onLoading(const QString &path)
433 {
434     libraryProgress->setLabelText(tr("Loading %1").
435                                   arg(QFileInfo(path).fileName()));
436     libraryProgress->setValue(libraryProgress->value() + 1);
437 }
438
439 void MainWindow::onEndLoad()
440 {
441     libraryProgress->hide();
442     libraryProgress->reset();
443 }