Speed up paging through the book. Clean up code for saving/restoring
[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 #ifndef Q_OS_SYMBIAN
47     setWindowTitle("Dorian");
48 #endif
49
50     // Central widget. Must be an intermediate, because the book view widget
51     // can be re-parented later
52     QFrame *central = new QFrame(this);
53     QVBoxLayout *layout = new QVBoxLayout(central);
54     layout->setMargin(0);
55     central->setLayout(layout);
56     setCentralWidget(central);
57
58     // Book view
59     view = new BookView(this);
60
61     // Tool bar actions
62
63 #ifdef Q_OS_SYMBIAN
64     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
65                                         "view-fullscreen", tr("Full screen"));
66 #endif
67
68     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
69                                       "chapters", tr("Chapters"), true);
70     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
71                                        "bookmarks", tr("Bookmarks"), true);
72     infoAction = addToolBarAction(this, SLOT(showInfo()),
73                                   "info", tr("Book info"), true);
74     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
75                                      "library", tr("Library"), true);
76
77 #ifdef Q_WS_MAEMO_5
78     settingsAction = menuBar()->addAction(tr("Settings"));
79     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
80     devToolsAction = menuBar()->addAction(tr("Developer"));
81     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
82     QAction *aboutAction = menuBar()->addAction(tr("About"));
83     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
84 #else
85     settingsAction = addToolBarAction(this, SLOT(showSettings()),
86                                       "preferences-system", tr("Settings"));
87     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
88                                       "developer", tr("Developer"));
89     addToolBarAction(this, SLOT(about()), "about", tr("About"));
90 #endif // Q_WS_MAEMO_5
91
92 #ifndef Q_OS_SYMBIAN
93     addToolBarSpace();
94     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
95                                         "view-fullscreen", tr("Full screen"));
96 #else
97     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
98 #endif
99
100     // Decorations
101     prev = new TranslucentButton("back", this);
102     next = new TranslucentButton("forward", this);
103     prog = new Progress(this);
104
105     // Handle model changes
106     connect(Library::instance(), SIGNAL(nowReadingChanged()),
107             this, SLOT(onCurrentBookChanged()));
108
109     // Load library, upgrade it if needed
110     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
111     Library *library = Library::instance();
112     connect(library, SIGNAL(beginUpgrade(int)),
113             this, SLOT(onBeginUpgrade(int)));
114     connect(library, SIGNAL(upgrading(const QString &)),
115             this, SLOT(onUpgrading(const QString &)));
116     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
117
118     // Handle loading book parts
119     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
120     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
121
122     // Handle progress
123     connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
124
125     // Shadow window for full screen reading
126     fullScreenWindow = new FullScreenWindow(this);
127     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
128
129     // Handle settings changes
130     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
131             this, SLOT(onSettingsChanged(const QString &)));
132
133     // Handle book view buttons
134     connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
135     connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
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     // Show in regular (non full-screen) mode
173     showRegular();
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     // Re-parent children
187     fullScreenWindow->leaveBookView();
188     takeBookView(view, prog, prev, next);
189
190     fullScreenWindow->hide();
191     show();
192
193 #if defined(Q_OS_SYMBIAN)
194     activateWindow();
195 #endif
196 }
197
198 void MainWindow::showBig()
199 {
200     TRACE;
201
202     // Re-parent children
203     leaveBookView();
204     fullScreenWindow->takeBookView(view, prog, prev, next);
205
206     fullScreenWindow->showFullScreen();
207     hide();
208
209 #ifdef Q_OS_SYMBIAN
210     fullScreenWindow->activateWindow();
211 #endif
212 }
213
214 void MainWindow::setCurrentBook(const QModelIndex &current)
215 {
216     mCurrent = current;
217     Book *book = Library::instance()->book(current);
218     view->setBook(book);
219     setWindowTitle(book? book->shortName(): tr("Dorian"));
220 }
221
222 void MainWindow::showLibrary()
223 {
224     (new LibraryDialog(this))->show();
225 }
226
227 void MainWindow::showSettings()
228 {
229     (new SettingsWindow(this))->show();
230 }
231
232 void MainWindow::showInfo()
233 {
234     if (mCurrent.isValid()) {
235         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
236                 exec();
237     }
238 }
239
240 void MainWindow::showDevTools()
241 {
242     (new DevTools())->exec();
243 }
244
245 void MainWindow::showBookmarks()
246 {
247     Book *book = Library::instance()->book(mCurrent);
248     if (book) {
249         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
250         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
251                 this, SLOT(onAddBookmark(const QString &)));
252         connect(bookmarks, SIGNAL(goToBookmark(int)),
253                 this, SLOT(onGoToBookmark(int)));
254         bookmarks->show();
255     }
256 }
257
258 void MainWindow::onSettingsChanged(const QString &key)
259 {
260 #if defined(Q_WS_MAEMO_5)
261     if (key == "orientation") {
262         QString value = Settings::instance()->value(key,
263             Platform::instance()->defaultOrientation()).toString();
264         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
265         if (value == "portrait") {
266             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
267             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
268             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
269                                            false);
270             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
271         } else {
272             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
273             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
274             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
275                                            false);
276             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
277                                            true);
278         }
279     } else if (key == "lightson") {
280         bool enable = Settings::instance()->value(key, false).toBool();
281         qDebug() << "MainWindow::onSettingsChanged: lightson" << 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;
295     Platform::instance()->showBusy(this, true);
296 }
297
298 void MainWindow::onPartLoadEnd(int index)
299 {
300     TRACE;
301     bool enablePrevious = false;
302     bool enableNext = false;
303     Book *book = Library::instance()->book(mCurrent);
304     if (book) {
305         if (index > 0) {
306             enablePrevious = true;
307         }
308         if (index < (book->parts.size() - 1)) {
309             enableNext = true;
310         }
311     }
312     Platform::instance()->showBusy(this, false);
313 }
314
315 void MainWindow::onAddBookmark(const QString &note)
316 {
317     TRACE;
318     view->addBookmark(note);
319     Platform::instance()->information(tr("Bookmarked current position"), this);
320 }
321
322 void MainWindow::onGoToBookmark(int index)
323 {
324     TRACE;
325     Book *book = Library::instance()->book(mCurrent);
326     view->goToBookmark(book->bookmarks()[index]);
327 }
328
329 void MainWindow::showChapters()
330 {
331     Book *book = Library::instance()->book(mCurrent);
332     if (book) {
333         ChaptersDialog *chapters = new ChaptersDialog(book, this);
334         connect(chapters, SIGNAL(goToChapter(int)),
335                 this, SLOT(onGoToChapter(int)));
336         chapters->show();
337     }
338 }
339
340 void MainWindow::onGoToChapter(int index)
341 {
342     TRACE;
343
344     Book *book = Library::instance()->book(mCurrent);
345     if (book) {
346         QString fragment;
347         int partIndex = book->partFromChapter(index, fragment);
348         if (partIndex != -1) {
349             view->goToPart(partIndex, fragment);
350         }
351     }
352 }
353
354 void MainWindow::timerEvent(QTimerEvent *event)
355 {
356     if (event->timerId() == preventBlankingTimer) {
357 #ifdef Q_WS_MAEMO_5
358         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
359                            MCE_REQUEST_IF, QDBusConnection::systemBus());
360         mce.call(MCE_PREVENT_BLANK_REQ);
361 #endif // Q_WS_MAEMO_5
362         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
363     }
364     AdopterWindow::timerEvent(event);
365 }
366
367 void MainWindow::about()
368 {
369     Dyalog *aboutDialog = new Dyalog(this, false);
370     aboutDialog->setWindowTitle(tr("About Dorian"));
371     QString version = Platform::instance()->version();
372     QLabel *label = new QLabel(aboutDialog);
373     label->setTextFormat(Qt::RichText);
374     label->setOpenExternalLinks(true);
375     label->setWordWrap(true);
376     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
377         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
378         "Licensed under GNU General Public License, Version 3<br>"
379         "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
380         "dorian.garage.maemo.org</a>").arg(version));
381     aboutDialog->addWidget(label);
382     aboutDialog->addStretch();
383     aboutDialog->show();
384 }
385
386 void MainWindow::goToNextPage()
387 {
388     next->flash();
389     prev->flash();
390     view->goNextPage();
391 }
392
393 void MainWindow::goToPreviousPage()
394 {
395     next->flash();
396     prev->flash();
397     view->goPreviousPage();
398 }
399
400 void MainWindow::onBeginUpgrade(int total)
401 {
402     libraryProgress->setVisible(total > 0);
403     libraryProgress->setWindowTitle(tr("Upgrading library"));
404     libraryProgress->setMaximum(total);
405 }
406
407 void MainWindow::onUpgrading(const QString &path)
408 {
409     libraryProgress->setLabelText(tr("Upgrading %1").
410                                   arg(QFileInfo(path).fileName()));
411     libraryProgress->setValue(libraryProgress->value() + 1);
412 }
413
414 void MainWindow::onEndUpgrade()
415 {
416     libraryProgress->hide();
417     libraryProgress->reset();
418 }
419
420 void MainWindow::onBeginLoad(int total)
421 {
422     libraryProgress->setVisible(total > 0);
423     libraryProgress->setWindowTitle(tr("Loading library"));
424     libraryProgress->setMaximum(total);
425 }
426
427 void MainWindow::onLoading(const QString &path)
428 {
429     libraryProgress->setLabelText(tr("Loading %1").
430                                   arg(QFileInfo(path).fileName()));
431     libraryProgress->setValue(libraryProgress->value() + 1);
432 }
433
434 void MainWindow::onEndLoad()
435 {
436     libraryProgress->hide();
437     libraryProgress->reset();
438 }