Fix forward navigation control on Linux.
[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
46     // Central widget. Must be an intermediate, because the book view widget
47     // might be re-parented later
48     QFrame *central = new QFrame(this);
49     QVBoxLayout *layout = new QVBoxLayout(central);
50     layout->setMargin(0);
51     central->setLayout(layout);
52     setCentralWidget(central);
53
54     // Book view
55     view = new BookView(this);
56
57     // Tool bar actions
58
59     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
60                                       "chapters", tr("Chapters"), true);
61     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
62                                        "bookmarks", tr("Bookmarks"), true);
63     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
64                                      "library", tr("Library"), false);
65
66 #ifdef Q_WS_MAEMO_5
67     settingsAction = menuBar()->addAction(tr("Settings"));
68     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
69     devToolsAction = menuBar()->addAction(tr("Developer"));
70     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
71 #else
72     settingsAction = addToolBarAction(this, SLOT(showSettings()),
73                                       "preferences-system", tr("Settings"));
74     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
75                                       "developer", tr("Developer"));
76 #endif
77
78     rotateAction = addToolBarAction(this, SLOT(rotate()),
79                                     "rotate", tr("Rotate"), true);
80     addToolBarSpace();
81     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
82         "view-fullscreen", tr("Full screen"), true);
83
84 #if defined(Q_WS_MAEMO_5)
85     QAction *aboutAction = menuBar()->addAction(tr("About"));
86     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
87 #else
88     addToolBarAction(this, SLOT(about()), "about", tr("About"));
89 #endif // defined(Q_WS_MAEMO_5)
90
91 #if defined(Q_OS_SYMBIAN)
92     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"), false);
93 #endif // defined(Q_OS_SYMBIAN)
94
95     // Decorations
96     prev = new TranslucentButton("back", this);
97     next = new TranslucentButton("forward", this);
98     prog = new Progress(this);
99
100     // Handle model changes
101     connect(Library::instance(), SIGNAL(nowReadingChanged()),
102             this, SLOT(onCurrentBookChanged()));
103
104     // Load library, upgrade it if needed
105     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
106     Library *library = Library::instance();
107     connect(library, SIGNAL(beginUpgrade(int)),
108             this, SLOT(onBeginUpgrade(int)));
109     connect(library, SIGNAL(upgrading(const QString &)),
110             this, SLOT(onUpgrading(const QString &)));
111     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
112
113     // Handle loading book parts
114     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
115     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
116
117     // Handle progress
118     connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
119
120     // Shadow window for full screen reading
121     fullScreenWindow = new FullScreenWindow();
122     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
123
124     // Handle settings changes
125     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
126             this, SLOT(onSettingsChanged(const QString &)));
127
128     // Handle book view buttons
129     connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
130     connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
131
132 #ifdef DORIAN_TEST_MODEL
133     (void)new ModelTest(Library::instance(), this);
134 #endif
135 }
136
137 MainWindow::~MainWindow()
138 {
139     delete fullScreenWindow;
140 }
141
142 void MainWindow::initialize()
143 {
144     TRACE;
145     Library *library = Library::instance();
146
147     // Show in regular (non full-screen) mode
148     showRegular();
149
150     // Upgrade library if needed, then load it
151     library->upgrade();
152     library->load();
153
154     // Load book on command line, or load last read book, or load default book
155     if (QCoreApplication::arguments().size() == 2) {
156         QString path = QCoreApplication::arguments()[1];
157         library->add(path);
158         QModelIndex index = library->find(path);
159         if (index.isValid()) {
160             library->setNowReading(index);
161         }
162     } else {
163         QModelIndex index = library->nowReading();
164         if (index.isValid()) {
165             library->setNowReading(index);
166         } else {
167             if (!library->rowCount()) {
168                 library->add(":/books/2BR02B.epub");
169             }
170             SortedLibrary sorted;
171             library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
172         }
173     }
174 }
175
176 void MainWindow::onCurrentBookChanged()
177 {
178     TRACE;
179     setCurrentBook(Library::instance()->nowReading());
180 }
181
182 void MainWindow::showRegular()
183 {
184     TRACE;
185
186     view->setLastBookmark();
187     fullScreenWindow->leaveBookView();
188     takeBookView(view, prog, prev, next);
189
190     fullScreenWindow->hide();
191     show();
192     view->scheduleRestoreLastBookmark();
193 }
194
195 void MainWindow::showBig()
196 {
197     TRACE;
198
199     view->setLastBookmark();
200     leaveBookView();
201     fullScreenWindow->takeBookView(view, prog, prev, next);
202
203 #if defined(Q_WS_MAEMO_5)
204     fullScreenWindow->raise();
205 #else
206     hide();
207 #endif
208     fullScreenWindow->showFullScreen();
209     view->scheduleRestoreLastBookmark();
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::rotate()
231 {
232     QString current = Settings::instance()->value("orientation",
233         Platform::instance()->defaultOrientation()).toString();
234     QString target = (current == "landscape")? "portrait": "landscape";
235     view->setLastBookmark();
236     Settings::instance()->setValue("orientation", target);
237 }
238
239 void MainWindow::showDevTools()
240 {
241     (new DevTools(this))->exec();
242 }
243
244 void MainWindow::showBookmarks()
245 {
246     Book *book = Library::instance()->book(mCurrent);
247     if (book) {
248         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
249         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
250                 this, SLOT(onAddBookmark(const QString &)));
251         connect(bookmarks, SIGNAL(goToBookmark(int)),
252                 this, SLOT(onGoToBookmark(int)));
253         bookmarks->show();
254     }
255 }
256
257 void MainWindow::onSettingsChanged(const QString &key)
258 {
259     TRACE;
260     qDebug() << "Key" << key;
261
262     if (key == "orientation") {
263         QString value = Settings::instance()->value(key,
264             Platform::instance()->defaultOrientation()).toString();
265         qDebug() << "Value: orientation" << value;
266         Platform::instance()->setOrientation(this, value);
267         Platform::instance()->setOrientation(fullScreenWindow, value);
268     }
269
270 #if defined(Q_WS_MAEMO_5)
271     else if (key == "lightson") {
272         bool enable = Settings::instance()->value(key, false).toBool();
273         killTimer(preventBlankingTimer);
274         if (enable) {
275             preventBlankingTimer = startTimer(29 * 1000);
276         }
277     }
278 #endif // defined(Q_WS_MAEMO_5)
279 }
280
281 void MainWindow::onPartLoadStart()
282 {
283     TRACE;
284     Platform::instance()->showBusy(this, true);
285 }
286
287 void MainWindow::onPartLoadEnd(int index)
288 {
289     TRACE;
290     bool enablePrevious = false;
291     bool enableNext = false;
292     Book *book = Library::instance()->book(mCurrent);
293     if (book) {
294         if (index > 0) {
295             enablePrevious = true;
296         }
297         if (index < (book->parts.size() - 1)) {
298             enableNext = true;
299         }
300     }
301     Platform::instance()->showBusy(this, false);
302 }
303
304 void MainWindow::onAddBookmark(const QString &note)
305 {
306     TRACE;
307     view->addBookmark(note);
308     Platform::instance()->information(tr("Bookmarked current position"), this);
309 }
310
311 void MainWindow::onGoToBookmark(int index)
312 {
313     TRACE;
314     Book *book = Library::instance()->book(mCurrent);
315     view->goToBookmark(book->bookmarks()[index]);
316 }
317
318 void MainWindow::showChapters()
319 {
320     Book *book = Library::instance()->book(mCurrent);
321     if (book) {
322         ChaptersDialog *chapters = new ChaptersDialog(book, this);
323         connect(chapters, SIGNAL(goToChapter(int)),
324                 this, SLOT(onGoToChapter(int)));
325         chapters->show();
326     }
327 }
328
329 void MainWindow::onGoToChapter(int index)
330 {
331     TRACE;
332
333     Book *book = Library::instance()->book(mCurrent);
334     if (book) {
335         QString fragment;
336         int partIndex = book->partFromChapter(index, fragment);
337         if (partIndex != -1) {
338             view->goToPart(partIndex, fragment);
339         }
340     }
341 }
342
343 void MainWindow::timerEvent(QTimerEvent *event)
344 {
345     if (event->timerId() == preventBlankingTimer) {
346 #ifdef Q_WS_MAEMO_5
347         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
348                            MCE_REQUEST_IF, QDBusConnection::systemBus());
349         mce.call(MCE_PREVENT_BLANK_REQ);
350 #endif // Q_WS_MAEMO_5
351         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
352     }
353     AdopterWindow::timerEvent(event);
354 }
355
356 void MainWindow::about()
357 {
358     Dyalog *aboutDialog = new Dyalog(this, false);
359     aboutDialog->setWindowTitle(tr("About Dorian"));
360     QString version = Platform::instance()->version();
361 #if !defined(Q_WS_MAEMO_5) && !defined(Q_OS_SYMBIAN)
362     QLabel *icon = new QLabel(aboutDialog);
363     QPixmap pixmap(Platform::instance()->icon("dorian"));
364     icon->setPixmap(pixmap);
365     aboutDialog->addWidget(icon);
366 #endif
367     QLabel *label = new QLabel(aboutDialog);
368     label->setTextFormat(Qt::RichText);
369     label->setOpenExternalLinks(true);
370     label->setWordWrap(true);
371     label->setText(tr("<b>Dorian %1</b><br><br>"
372         "Copyright &copy; 2010-2011 by "
373         "Akos Polster &lt;akos@pipacs.com&gt;<br><br>"
374         "Licensed under GNU General Public License, Version 3<br><br>"
375         "<a href='http://dorian.garage.maemo.org/'>"
376         "dorian.garage.maemo.org</a><br><br>"
377         ).arg(version));
378     aboutDialog->addWidget(label);
379     aboutDialog->addStretch();
380     aboutDialog->show();
381 }
382
383 void MainWindow::goToNextPage()
384 {
385     next->flash();
386     prev->flash();
387     view->goNextPage();
388 }
389
390 void MainWindow::goToPreviousPage()
391 {
392     next->flash();
393     prev->flash();
394     view->goPreviousPage();
395 }
396
397 void MainWindow::onBeginUpgrade(int total)
398 {
399     libraryProgress->setVisible(total > 0);
400     libraryProgress->setWindowTitle(tr("Upgrading library"));
401     libraryProgress->setMaximum(total);
402 }
403
404 void MainWindow::onUpgrading(const QString &path)
405 {
406     libraryProgress->setLabelText(tr("Upgrading %1").
407                                   arg(QFileInfo(path).fileName()));
408     libraryProgress->setValue(libraryProgress->value() + 1);
409 }
410
411 void MainWindow::onEndUpgrade()
412 {
413     libraryProgress->hide();
414     libraryProgress->reset();
415 }
416
417 void MainWindow::onBeginLoad(int total)
418 {
419     libraryProgress->setVisible(total > 0);
420     libraryProgress->setWindowTitle(tr("Loading library"));
421     libraryProgress->setMaximum(total);
422 }
423
424 void MainWindow::onLoading(const QString &path)
425 {
426     libraryProgress->setLabelText(tr("Loading %1").
427                                   arg(QFileInfo(path).fileName()));
428     libraryProgress->setValue(libraryProgress->value() + 1);
429 }
430
431 void MainWindow::onEndLoad()
432 {
433     libraryProgress->hide();
434     libraryProgress->reset();
435 }