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