c8f5dfe93646f7059993750de18f110579809ca8
[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     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
68                                      "library", tr("Library"), false);
69
70 #ifdef Q_WS_MAEMO_5
71     settingsAction = menuBar()->addAction(tr("Settings"));
72     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
73     devToolsAction = menuBar()->addAction(tr("Developer"));
74     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
75 #else
76     settingsAction = addToolBarAction(this, SLOT(showSettings()),
77                                       "preferences-system", tr("Settings"));
78     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
79                                       "developer", tr("Developer"));
80 #endif
81
82     rotateAction = addToolBarAction(this, SLOT(rotate()),
83                                     "rotate", tr("Rotate"), true);
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     fullScreenWindow->hide();
189
190 #ifdef Q_OS_SYMBIAN
191     view->setFixedSize(Platform::availableSize().width(),
192         Platform::availableSize().height() - Platform::softKeyHeight());
193 #endif
194     show();
195 }
196
197 void MainWindow::showBig()
198 {
199     TRACE;
200
201     // Re-parent children
202     leaveBookView();
203     fullScreenWindow->takeBookView(view, prog, prev, next);
204     hide();
205
206 #ifdef Q_OS_SYMBIAN
207     view->setFixedSize(Platform::size());
208 #endif
209     fullScreenWindow->showFullScreen();
210 }
211
212 void MainWindow::setCurrentBook(const QModelIndex &current)
213 {
214     mCurrent = current;
215     Book *book = Library::instance()->book(current);
216     view->setBook(book);
217     setWindowTitle(book? book->shortName(): tr("Dorian"));
218 }
219
220 void MainWindow::showLibrary()
221 {
222     (new LibraryDialog(this))->show();
223 }
224
225 void MainWindow::showSettings()
226 {
227     (new SettingsWindow(this))->show();
228 }
229
230 void MainWindow::rotate()
231 {
232     QString current = Settings::instance()->value("orientation",
233         Platform::instance()->defaultOrientation()).toString();
234     QString target = (current == "landscape")? "portrait": "landscape";
235     Settings::instance()->setValue("orientation", target);
236 }
237
238 void MainWindow::showDevTools()
239 {
240     (new DevTools())->exec();
241 }
242
243 void MainWindow::showBookmarks()
244 {
245     Book *book = Library::instance()->book(mCurrent);
246     if (book) {
247         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
248         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
249                 this, SLOT(onAddBookmark(const QString &)));
250         connect(bookmarks, SIGNAL(goToBookmark(int)),
251                 this, SLOT(onGoToBookmark(int)));
252         bookmarks->show();
253     }
254 }
255
256 void MainWindow::onSettingsChanged(const QString &key)
257 {
258     TRACE;
259     qDebug() << "Key" << key;
260
261     if (key == "orientation") {
262         view->setLastBookmark();
263         QString value = Settings::instance()->value(key,
264             Platform::instance()->defaultOrientation()).toString();
265         qDebug() << "Value: orientation" << value;
266         Platform::instance()->setOrientation(this, value);
267         Platform::instance()->setOrientation(fullScreenWindow, value);
268     }
269
270 #if defined(Q_WS_MAEMO_5)
271     else if (key == "lightson") {
272         bool enable = Settings::instance()->value(key, false).toBool();
273         killTimer(preventBlankingTimer);
274         if (enable) {
275             preventBlankingTimer = startTimer(29 * 1000);
276         }
277     }
278 #endif // defined(Q_WS_MAEMO_5)
279 }
280
281 void MainWindow::onPartLoadStart()
282 {
283     TRACE;
284     Platform::instance()->showBusy(this, true);
285 }
286
287 void MainWindow::onPartLoadEnd(int index)
288 {
289     TRACE;
290     bool enablePrevious = false;
291     bool enableNext = false;
292     Book *book = Library::instance()->book(mCurrent);
293     if (book) {
294         if (index > 0) {
295             enablePrevious = true;
296         }
297         if (index < (book->parts.size() - 1)) {
298             enableNext = true;
299         }
300     }
301     Platform::instance()->showBusy(this, false);
302 }
303
304 void MainWindow::onAddBookmark(const QString &note)
305 {
306     TRACE;
307     view->addBookmark(note);
308     Platform::instance()->information(tr("Bookmarked current position"), this);
309 }
310
311 void MainWindow::onGoToBookmark(int index)
312 {
313     TRACE;
314     Book *book = Library::instance()->book(mCurrent);
315     view->goToBookmark(book->bookmarks()[index]);
316 }
317
318 void MainWindow::showChapters()
319 {
320     Book *book = Library::instance()->book(mCurrent);
321     if (book) {
322         ChaptersDialog *chapters = new ChaptersDialog(book, this);
323         connect(chapters, SIGNAL(goToChapter(int)),
324                 this, SLOT(onGoToChapter(int)));
325         chapters->show();
326     }
327 }
328
329 void MainWindow::onGoToChapter(int index)
330 {
331     TRACE;
332
333     Book *book = Library::instance()->book(mCurrent);
334     if (book) {
335         QString fragment;
336         int partIndex = book->partFromChapter(index, fragment);
337         if (partIndex != -1) {
338             view->goToPart(partIndex, fragment);
339         }
340     }
341 }
342
343 void MainWindow::timerEvent(QTimerEvent *event)
344 {
345     if (event->timerId() == preventBlankingTimer) {
346 #ifdef Q_WS_MAEMO_5
347         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
348                            MCE_REQUEST_IF, QDBusConnection::systemBus());
349         mce.call(MCE_PREVENT_BLANK_REQ);
350 #endif // Q_WS_MAEMO_5
351         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
352     }
353     AdopterWindow::timerEvent(event);
354 }
355
356 void MainWindow::about()
357 {
358     Dyalog *aboutDialog = new Dyalog(this, false);
359     aboutDialog->setWindowTitle(tr("About Dorian"));
360     QString version = Platform::instance()->version();
361     QLabel *label = new QLabel(aboutDialog);
362     label->setTextFormat(Qt::RichText);
363     label->setOpenExternalLinks(true);
364     label->setWordWrap(true);
365     label->setText(tr("<b>Dorian %1</b><br><br>"
366         "Copyright &copy; 2010-2011 by "
367         "Akos Polster &lt;akos@pipacs.com&gt;<br><br>"
368         "Licensed under GNU General Public License, Version 3<br><br>"
369         "<a href='http://dorian.garage.maemo.org/'>"
370         "dorian.garage.maemo.org</a><br><br>"
371         ).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 }