Make orientation switch explicit 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 #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     TRACE;
261     qDebug() << "Key" << key;
262
263     if (key == "orientation") {
264         view->setLastBookmark();
265         QString value = Settings::instance()->value(key,
266             Platform::instance()->defaultOrientation()).toString();
267         qDebug() << "Value: orientation" << value;
268         Platform::instance()->setOrientation(this, value);
269         Platform::instance()->setOrientation(fullScreenWindow, value);
270     }
271
272 #if defined(Q_WS_MAEMO_5)
273     else if (key == "lightson") {
274         bool enable = Settings::instance()->value(key, false).toBool();
275         killTimer(preventBlankingTimer);
276         if (enable) {
277             preventBlankingTimer = startTimer(29 * 1000);
278         }
279     }
280 #endif // defined(Q_WS_MAEMO_5)
281 }
282
283 void MainWindow::onPartLoadStart()
284 {
285     TRACE;
286     Platform::instance()->showBusy(this, true);
287 }
288
289 void MainWindow::onPartLoadEnd(int index)
290 {
291     TRACE;
292     bool enablePrevious = false;
293     bool enableNext = false;
294     Book *book = Library::instance()->book(mCurrent);
295     if (book) {
296         if (index > 0) {
297             enablePrevious = true;
298         }
299         if (index < (book->parts.size() - 1)) {
300             enableNext = true;
301         }
302     }
303     Platform::instance()->showBusy(this, false);
304 }
305
306 void MainWindow::onAddBookmark(const QString &note)
307 {
308     TRACE;
309     view->addBookmark(note);
310     Platform::instance()->information(tr("Bookmarked current position"), this);
311 }
312
313 void MainWindow::onGoToBookmark(int index)
314 {
315     TRACE;
316     Book *book = Library::instance()->book(mCurrent);
317     view->goToBookmark(book->bookmarks()[index]);
318 }
319
320 void MainWindow::showChapters()
321 {
322     Book *book = Library::instance()->book(mCurrent);
323     if (book) {
324         ChaptersDialog *chapters = new ChaptersDialog(book, this);
325         connect(chapters, SIGNAL(goToChapter(int)),
326                 this, SLOT(onGoToChapter(int)));
327         chapters->show();
328     }
329 }
330
331 void MainWindow::onGoToChapter(int index)
332 {
333     TRACE;
334
335     Book *book = Library::instance()->book(mCurrent);
336     if (book) {
337         QString fragment;
338         int partIndex = book->partFromChapter(index, fragment);
339         if (partIndex != -1) {
340             view->goToPart(partIndex, fragment);
341         }
342     }
343 }
344
345 void MainWindow::timerEvent(QTimerEvent *event)
346 {
347     if (event->timerId() == preventBlankingTimer) {
348 #ifdef Q_WS_MAEMO_5
349         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
350                            MCE_REQUEST_IF, QDBusConnection::systemBus());
351         mce.call(MCE_PREVENT_BLANK_REQ);
352 #endif // Q_WS_MAEMO_5
353         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
354     }
355     AdopterWindow::timerEvent(event);
356 }
357
358 void MainWindow::about()
359 {
360     Dyalog *aboutDialog = new Dyalog(this, false);
361     aboutDialog->setWindowTitle(tr("About Dorian"));
362     QString version = Platform::instance()->version();
363     QLabel *label = new QLabel(aboutDialog);
364     label->setTextFormat(Qt::RichText);
365     label->setOpenExternalLinks(true);
366     label->setWordWrap(true);
367     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
368         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
369         "Licensed under GNU General Public License, Version 3<br>"
370         "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
371         "dorian.garage.maemo.org</a>").arg(version));
372     aboutDialog->addWidget(label);
373     aboutDialog->addStretch();
374     aboutDialog->show();
375 }
376
377 void MainWindow::goToNextPage()
378 {
379     next->flash();
380     prev->flash();
381     view->goNextPage();
382 }
383
384 void MainWindow::goToPreviousPage()
385 {
386     next->flash();
387     prev->flash();
388     view->goPreviousPage();
389 }
390
391 void MainWindow::onBeginUpgrade(int total)
392 {
393     libraryProgress->setVisible(total > 0);
394     libraryProgress->setWindowTitle(tr("Upgrading library"));
395     libraryProgress->setMaximum(total);
396 }
397
398 void MainWindow::onUpgrading(const QString &path)
399 {
400     libraryProgress->setLabelText(tr("Upgrading %1").
401                                   arg(QFileInfo(path).fileName()));
402     libraryProgress->setValue(libraryProgress->value() + 1);
403 }
404
405 void MainWindow::onEndUpgrade()
406 {
407     libraryProgress->hide();
408     libraryProgress->reset();
409 }
410
411 void MainWindow::onBeginLoad(int total)
412 {
413     libraryProgress->setVisible(total > 0);
414     libraryProgress->setWindowTitle(tr("Loading library"));
415     libraryProgress->setMaximum(total);
416 }
417
418 void MainWindow::onLoading(const QString &path)
419 {
420     libraryProgress->setLabelText(tr("Loading %1").
421                                   arg(QFileInfo(path).fileName()));
422     libraryProgress->setValue(libraryProgress->value() + 1);
423 }
424
425 void MainWindow::onEndLoad()
426 {
427     libraryProgress->hide();
428     libraryProgress->reset();
429 }