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