More Symbian fixes.
[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))->
255                 exec();
256     }
257 }
258
259 void MainWindow::showDevTools()
260 {
261     (new DevTools())->exec();
262 }
263
264 void MainWindow::showBookmarks()
265 {
266     Book *book = Library::instance()->book(mCurrent);
267     if (book) {
268         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
269         bookmarks->setWindowModality(Qt::WindowModal);
270         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
271         connect(bookmarks, SIGNAL(goToBookmark(int)),
272                 this, SLOT(onGoToBookmark(int)));
273         bookmarks->show();
274     }
275 }
276
277 void MainWindow::closeEvent(QCloseEvent *event)
278 {
279     Trace t("MainWindow::closeEvent");
280     view->setLastBookmark();
281     event->accept();
282 }
283
284 void MainWindow::onSettingsChanged(const QString &key)
285 {
286 #ifdef Q_WS_MAEMO_5
287     if (key == "orientation") {
288         QString value = Settings::instance()->value(key).toString();
289         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
290         if (value == "portrait") {
291             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
292             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
293         }
294         else {
295             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
296             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
297         }
298     } else if (key == "lightson") {
299         bool enable = Settings::instance()->value(key, false).toBool();
300         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
301         killTimer(preventBlankingTimer);
302         if (enable) {
303             preventBlankingTimer = startTimer(29 * 1000);
304         }
305     } else if (key == "usevolumekeys") {
306         bool value = Settings::instance()->value(key).toBool();
307         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
308         grabZoomKeys(value);
309         fullScreenWindow->grabZoomKeys(value);
310     }
311 #else
312     Q_UNUSED(key);
313 #endif // Q_WS_MAEMO_5
314 }
315
316 void MainWindow::onPartLoadStart()
317 {
318     Trace t("MainWindow::onPartLoadStart");
319 #ifdef Q_WS_MAEMO_5
320     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
321 #endif
322 }
323
324 void MainWindow::onPartLoadEnd(int index)
325 {
326     Trace t("MainWindow::onPartLoadEnd");
327     bool enablePrevious = false;
328     bool enableNext = false;
329     Book *book = Library::instance()->book(mCurrent);
330     if (book) {
331         if (index > 0) {
332             enablePrevious = true;
333         }
334         if (index < (book->parts.size() - 1)) {
335             enableNext = true;
336         }
337     }
338 #ifdef Q_WS_MAEMO_5
339     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
340 #endif // Q_WS_MAEMO_5
341 }
342
343 void MainWindow::onAddBookmark()
344 {
345     Trace t("MainWindow:onAddBookmark");
346     view->addBookmark();
347 }
348
349 void MainWindow::onGoToBookmark(int index)
350 {
351     Trace t("MainWindow::onGoToBookmark");
352     Book *book = Library::instance()->book(mCurrent);
353     view->goToBookmark(book->bookmarks()[index]);
354 }
355
356 void MainWindow::showChapters()
357 {
358     Book *book = Library::instance()->book(mCurrent);
359     if (book) {
360         ChaptersDialog *chapters = new ChaptersDialog(book, this);
361         chapters->setWindowModality(Qt::WindowModal);
362         connect(chapters, SIGNAL(goToChapter(int)),
363                 this, SLOT(onGoToChapter(int)));
364         chapters->show();
365     }
366 }
367
368 void MainWindow::onGoToChapter(int index)
369 {
370     Trace t("MainWindow::onGoToChapter");
371
372     Book *book = Library::instance()->book(mCurrent);
373     if (book) {
374         int partIndex = book->partFromChapter(index);
375         if (partIndex != -1) {
376             view->goToBookmark(Book::Bookmark(partIndex, 0));
377         }
378     }
379 }
380
381 void MainWindow::timerEvent(QTimerEvent *event)
382 {
383     if (event->timerId() == preventBlankingTimer) {
384 #ifdef Q_WS_MAEMO_5
385         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
386                            MCE_REQUEST_IF, QDBusConnection::systemBus());
387         mce.call(MCE_PREVENT_BLANK_REQ);
388 #endif // Q_WS_MAEMO_5
389         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
390     }
391 }
392
393 void MainWindow::resizeEvent(QResizeEvent *e)
394 {
395     Trace t("MainWindow::resizeEvent");
396     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
397 #if defined(Q_WS_MAEMO_5)
398     previousButton->setGeometry(0,
399         e->size().height() - toolBar->height() - TranslucentButton::pixels,
400         TranslucentButton::pixels, TranslucentButton::pixels);
401     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
402         TranslucentButton::pixels, TranslucentButton::pixels);
403 #elif defined(Q_OS_SYMBIAN)
404     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
405         TranslucentButton::pixels, TranslucentButton::pixels);
406     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
407         0, TranslucentButton::pixels, TranslucentButton::pixels);
408 #else
409     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
410         TranslucentButton::pixels, TranslucentButton::pixels);
411     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
412         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
413 #endif // Q_WS_MAEMO_5
414     qDebug() << "previousButton geometry" << previousButton->geometry();
415     previousButton->flash(1500);
416     nextButton->flash(1500);
417     QMainWindow::resizeEvent(e);
418 }
419
420 void MainWindow::about()
421 {
422     Dyalog *aboutDialog = new Dyalog(this);
423     aboutDialog->setWindowTitle(tr("About Dorian"));
424     QLabel *label = new QLabel(aboutDialog);
425     label->setTextFormat(Qt::RichText);
426     label->setOpenExternalLinks(true);
427     label->setWordWrap(true);
428     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
429         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
430         "Licensed under GNU General Public License, Version 3<br>"
431         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
432         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
433     aboutDialog->addWidget(label);
434     aboutDialog->addStretch();
435     aboutDialog->show();
436 }
437
438
439 void MainWindow::goToNextPage()
440 {
441     nextButton->flash(1500);
442     previousButton->flash(1500);
443     view->goNextPage();
444 }
445
446 void MainWindow::goToPreviousPage()
447 {
448     nextButton->flash(1500);
449     previousButton->flash(1500);
450     view->goPreviousPage();
451 }