Navigate with volume keys.
[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
34 #ifdef DORIAN_TEST_MODEL
35 #include "modeltest.h"
36 #endif
37
38 #ifdef Q_WS_MAC
39 #   define ICON_PREFIX ":/icons/mac/"
40 #else
41 #   define ICON_PREFIX ":/icons/"
42 #endif
43
44 const int PROGRESS_HEIGHT = 17;
45
46 MainWindow::MainWindow(QWidget *parent):
47     QMainWindow(parent), view(0), preventBlankingTimer(-1)
48 {
49     Trace t("MainWindow::MainWindow");
50 #ifdef Q_WS_MAEMO_5
51     setAttribute(Qt::WA_Maemo5StackedWindow, true);
52     grabZoomKeys();
53 #endif
54     setWindowTitle("Dorian");
55
56     // Central widget. Must be an intermediate, because the book view widget
57     // can be re-parented later
58     QFrame *central = new QFrame(this);
59     QVBoxLayout *layout = new QVBoxLayout(central);
60     layout->setMargin(0);
61     central->setLayout(layout);
62     setCentralWidget(central);
63
64     // Book view
65     view = new BookView(central);
66     view->show();
67     layout->addWidget(view);
68
69     // Progress
70     progress = new Progress(central);
71
72     // Tool bar
73     setUnifiedTitleAndToolBarOnMac(true);
74     settings = new QDialog(this);
75     toolBar = addToolBar("controls");
76     toolBar->setMovable(false);
77     toolBar->setFloatable(false);
78     toolBar->toggleViewAction()->setVisible(false);
79 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
80     toolBar->setIconSize(QSize(42, 42));
81 #endif
82
83     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
84     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
85     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
86     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
87
88 #ifdef Q_WS_MAEMO_5
89     infoAction = menuBar()->addAction(tr("Book details"));
90     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
91     libraryAction = menuBar()->addAction(tr("Library"));
92     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
93     settingsAction = menuBar()->addAction(tr("Settings"));
94     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
95     devToolsAction = menuBar()->addAction(tr("Developer"));
96     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
97 #else
98     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
99     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
100                                      "system-file-manager");
101     settingsAction = addToolBarAction(this, SLOT(showSettings()),
102                                       "preferences-system");
103     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
104 #endif // Q_WS_MAEMO_5
105
106     QFrame *frame = new QFrame(toolBar);
107     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
108     toolBar->addWidget(frame);
109
110     fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
111
112     // Handle model changes
113     connect(Library::instance(), SIGNAL(nowReadingChanged()),
114             this, SLOT(onCurrentBookChanged()));
115
116     // Load book on command line, or load last read book, or load default book
117     Library *library = Library::instance();
118     if (QCoreApplication::arguments().size() == 2) {
119         QString path = QCoreApplication::arguments()[1];
120         library->add(path);
121         QModelIndex index = library->find(path);
122         if (index.isValid()) {
123             library->setNowReading(index);
124         }
125     }
126     else {
127         QModelIndex index = library->nowReading();
128         if (index.isValid()) {
129             library->setNowReading(index);
130         }
131         else {
132             if (!library->rowCount()) {
133                 library->add(":/books/2 B R 0 2 B.epub");
134             }
135             library->setNowReading(library->index(0));
136         }
137     }
138
139     // Handle settings changes
140     Settings *settings = Settings::instance();
141     connect(settings, SIGNAL(valueChanged(const QString &)),
142             this, SLOT(onSettingsChanged(const QString &)));
143     settings->setValue("orientation", settings->value("orientation"));
144     settings->setValue("lightson", settings->value("lightson"));
145
146     // Handle loading chapters
147     connect(view, SIGNAL(chapterLoadStart(int)),
148             this, SLOT(onChapterLoadStart()));
149     connect(view, SIGNAL(chapterLoadEnd(int)),
150             this, SLOT(onChapterLoadEnd(int)));
151
152     // Handle progress
153     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
154
155     // Shadow window for full screen
156     fullScreenWindow = new FullScreenWindow(this);
157     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
158
159     // Create thread for finding books in directories
160     bookFinder = new BookFinder();
161     connect(bookFinder, SIGNAL(add(const QString &)),
162             library, SLOT(add(const QString &)));
163     connect(bookFinder, SIGNAL(remove(const QString &)),
164             library, SLOT(remove(const QString &)));
165     bookFinder->moveToThread(&bookFinderThread);
166     bookFinderThread.start();
167
168 #ifdef DORIAN_TEST_MODEL
169     (void)new ModelTest(Library::instance(), this);
170 #endif
171 }
172
173 MainWindow::~MainWindow()
174 {
175     bookFinderThread.quit();
176     bookFinderThread.wait();
177     delete bookFinder;
178 }
179
180 void MainWindow::onCurrentBookChanged()
181 {
182     setCurrentBook(Library::instance()->nowReading());
183 }
184
185 void MainWindow::showRegular()
186 {
187     Trace t("MainWindow::showRegular");
188     fullScreenWindow->hide();
189     fullScreenWindow->leaveChild();
190     view->setParent(centralWidget());
191     progress->setParent(centralWidget());
192     progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
193     centralWidget()->layout()->addWidget(view);
194     progress->flash();
195 }
196
197 void MainWindow::showBig()
198 {
199     Trace t("MainWindow::showBig");
200     centralWidget()->layout()->removeWidget(view);
201     progress->setParent(fullScreenWindow);
202     progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
203                           PROGRESS_HEIGHT);
204     fullScreenWindow->takeChild(view);
205     fullScreenWindow->showFullScreen();
206     progress->flash();
207 }
208
209 void MainWindow::setCurrentBook(const QModelIndex &current)
210 {
211     mCurrent = current;
212     Book *book = Library::instance()->book(current);
213     view->setBook(book);
214     setWindowTitle(book? book->shortName(): tr("Dorian"));
215 }
216
217 QAction *MainWindow::addToolBarAction(const QObject *receiver,
218                                       const char *member,
219                                       const QString &name)
220 {
221     return toolBar->
222         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
223 }
224
225 void MainWindow::showLibrary()
226 {
227     (new LibraryDialog(this))->show();
228 }
229
230 void MainWindow::showSettings()
231 {
232     (new SettingsWindow(this))->show();
233 }
234
235 void MainWindow::showInfo()
236 {
237     if (mCurrent.isValid()) {
238         (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
239     }
240 }
241
242 void MainWindow::showDevTools()
243 {
244     (new DevTools())->exec();
245 }
246
247 void MainWindow::showBookmarks()
248 {
249     Book *book = Library::instance()->book(mCurrent);
250     if (book) {
251         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
252         bookmarks->setWindowModality(Qt::WindowModal);
253         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
254         connect(bookmarks, SIGNAL(goToBookmark(int)),
255                 this, SLOT(onGoToBookmark(int)));
256         bookmarks->show();
257     }
258 }
259
260 void MainWindow::closeEvent(QCloseEvent *event)
261 {
262     Trace t("MainWindow::closeEvent");
263     view->setLastBookmark();
264     event->accept();
265 }
266
267 void MainWindow::onSettingsChanged(const QString &key)
268 {
269 #ifdef Q_WS_MAEMO_5
270     if (key == "orientation") {
271         QString value = Settings::instance()->value(key).toString();
272         Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
273                      arg(value));
274         if (value == "portrait") {
275             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
276             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
277         }
278         else {
279             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
280             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
281         }
282     } else if (key == "lightson") {
283         bool enable = Settings::instance()->value(key, false).toBool();
284         Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
285                      arg(enable));
286         killTimer(preventBlankingTimer);
287         if (enable) {
288             preventBlankingTimer = startTimer(29 * 1000);
289         }
290     }
291 #else
292     Q_UNUSED(key);
293 #endif // Q_WS_MAEMO_5
294 }
295
296 void MainWindow::onPartLoadStart()
297 {
298     Trace t("MainWindow::onPartLoadStart");
299 #ifdef Q_WS_MAEMO_5
300     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
301 #endif
302 }
303
304 void MainWindow::onPartLoadEnd(int index)
305 {
306     Trace t("MainWindow::onPartLoadEnd");
307     bool enablePrevious = false;
308     bool enableNext = false;
309     Book *book = Library::instance()->book(mCurrent);
310     if (book) {
311         if (index > 0) {
312             enablePrevious = true;
313         }
314         if (index < (book->parts.size() - 1)) {
315             enableNext = true;
316         }
317     }
318 #ifdef Q_WS_MAEMO_5
319     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
320     previousAction->setIcon(QIcon(enablePrevious?
321         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
322     nextAction->setIcon(QIcon(enableNext?
323         ":/icons/next.png": ":/icons/next-disabled.png"));
324 #endif // Q_WS_MAEMO_5
325     previousAction->setEnabled(enablePrevious);
326     nextAction->setEnabled(enableNext);
327 }
328
329 void MainWindow::onAddBookmark()
330 {
331     Trace t("MainWindow:onAddBookmark");
332     view->addBookmark();
333 }
334
335 void MainWindow::onGoToBookmark(int index)
336 {
337     Trace t("MainWindow::onGoToBookmark");
338     Book *book = Library::instance()->book(mCurrent);
339     view->goToBookmark(book->bookmarks()[index]);
340 }
341
342 void MainWindow::showChapters()
343 {
344     Book *book = Library::instance()->book(mCurrent);
345     if (book) {
346         ChaptersDialog *chapters = new ChaptersDialog(book, this);
347         chapters->setWindowModality(Qt::WindowModal);
348         connect(chapters, SIGNAL(goToChapter(int)),
349                 this, SLOT(onGoToChapter(int)));
350         chapters->show();
351     }
352 }
353
354 void MainWindow::onGoToChapter(int index)
355 {
356     Trace t("MainWindow::onGoToChapter");
357
358     Book *book = Library::instance()->book(mCurrent);
359     if (book) {
360         int partIndex = book->partFromChapter(index);
361         if (partIndex != -1) {
362             view->goToBookmark(Book::Bookmark(partIndex, 0));
363         }
364     }
365 }
366
367 void MainWindow::timerEvent(QTimerEvent *event)
368 {
369     if (event->timerId() == preventBlankingTimer) {
370 #ifdef Q_WS_MAEMO_5
371         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
372                            MCE_REQUEST_IF, QDBusConnection::systemBus());
373         mce.call(MCE_PREVENT_BLANK_REQ);
374 #endif // Q_WS_MAEMO_5
375         Trace::trace("MainWindow::timerEvent: Prevent display blanking");
376     }
377 }
378
379 void MainWindow::resizeEvent(QResizeEvent *e)
380 {
381     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
382     QMainWindow::resizeEvent(e);
383 }
384
385 void MainWindow::grabZoomKeys()
386 {
387 #ifdef Q_WS_MAEMO_5
388     if (!winId()) {
389         qCritical() << "Can't grab keys unless we have a window id";
390         return;
391     }
392     unsigned long val = 1;
393     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
394     if (!atom) {
395         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
396         return;
397     }
398     XChangeProperty(QX11Info::display(),
399         winId(),
400         atom,
401         XA_INTEGER,
402         32,
403         PropModeReplace,
404         reinterpret_cast<unsigned char *>(&val),
405         1);
406 #endif // Q_WS_MAEMO_5
407 }