f45b950571feb0423c42f24e41642347eeca2b43
[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     // can 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)), 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
189     fullScreenWindow->hide();
190     show();
191
192 #if defined(Q_OS_SYMBIAN)
193     activateWindow();
194 #endif
195 }
196
197 void MainWindow::showBig()
198 {
199     TRACE;
200
201     // Re-parent children
202     leaveBookView();
203     fullScreenWindow->takeBookView(view, prog, prev, next);
204
205     fullScreenWindow->showFullScreen();
206     hide();
207
208 #ifdef Q_OS_SYMBIAN
209     fullScreenWindow->activateWindow();
210 #endif
211 }
212
213 void MainWindow::setCurrentBook(const QModelIndex &current)
214 {
215     mCurrent = current;
216     Book *book = Library::instance()->book(current);
217     view->setBook(book);
218     setWindowTitle(book? book->shortName(): tr("Dorian"));
219 }
220
221 void MainWindow::showLibrary()
222 {
223     (new LibraryDialog(this))->show();
224 }
225
226 void MainWindow::showSettings()
227 {
228     (new SettingsWindow(this))->show();
229 }
230
231 void MainWindow::showInfo()
232 {
233     if (mCurrent.isValid()) {
234         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
235                 exec();
236     }
237 }
238
239 void MainWindow::showDevTools()
240 {
241     (new DevTools())->exec();
242 }
243
244 void MainWindow::showBookmarks()
245 {
246     Book *book = Library::instance()->book(mCurrent);
247     if (book) {
248         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
249         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
250                 this, SLOT(onAddBookmark(const QString &)));
251         connect(bookmarks, SIGNAL(goToBookmark(int)),
252                 this, SLOT(onGoToBookmark(int)));
253         bookmarks->show();
254     }
255 }
256
257 void MainWindow::closeEvent(QCloseEvent *event)
258 {
259     TRACE;
260     view->setLastBookmark();
261     AdopterWindow::closeEvent(event);
262 }
263
264 void MainWindow::onSettingsChanged(const QString &key)
265 {
266 #if defined(Q_WS_MAEMO_5)
267     if (key == "orientation") {
268         QString value = Settings::instance()->value(key,
269             Platform::instance()->defaultOrientation()).toString();
270         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
271         if (value == "portrait") {
272             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
273             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
274             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
275                                            false);
276             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
277         } else {
278             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
279             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
280             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
281                                            false);
282             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
283                                            true);
284         }
285     } else if (key == "lightson") {
286         bool enable = Settings::instance()->value(key, false).toBool();
287         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
288         killTimer(preventBlankingTimer);
289         if (enable) {
290             preventBlankingTimer = startTimer(29 * 1000);
291         }
292     }
293 #else
294     Q_UNUSED(key);
295 #endif // Q_WS_MAEMO_5
296 }
297
298 void MainWindow::onPartLoadStart()
299 {
300     TRACE;
301     Platform::instance()->showBusy(this, true);
302 }
303
304 void MainWindow::onPartLoadEnd(int index)
305 {
306     TRACE;
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     Platform::instance()->showBusy(this, false);
319 }
320
321 void MainWindow::onAddBookmark(const QString &note)
322 {
323     TRACE;
324     view->addBookmark(note);
325     Platform::instance()->information(tr("Bookmarked current position"), this);
326 }
327
328 void MainWindow::onGoToBookmark(int index)
329 {
330     TRACE;
331     Book *book = Library::instance()->book(mCurrent);
332     view->goToBookmark(book->bookmarks()[index]);
333 }
334
335 void MainWindow::showChapters()
336 {
337     Book *book = Library::instance()->book(mCurrent);
338     if (book) {
339         ChaptersDialog *chapters = new ChaptersDialog(book, this);
340         connect(chapters, SIGNAL(goToChapter(int)),
341                 this, SLOT(onGoToChapter(int)));
342         chapters->show();
343     }
344 }
345
346 void MainWindow::onGoToChapter(int index)
347 {
348     TRACE;
349
350     Book *book = Library::instance()->book(mCurrent);
351     if (book) {
352         QString fragment;
353         int partIndex = book->partFromChapter(index, fragment);
354         if (partIndex != -1) {
355             view->goToPart(partIndex, fragment);
356         }
357     }
358 }
359
360 void MainWindow::timerEvent(QTimerEvent *event)
361 {
362     if (event->timerId() == preventBlankingTimer) {
363 #ifdef Q_WS_MAEMO_5
364         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
365                            MCE_REQUEST_IF, QDBusConnection::systemBus());
366         mce.call(MCE_PREVENT_BLANK_REQ);
367 #endif // Q_WS_MAEMO_5
368         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
369     }
370     AdopterWindow::timerEvent(event);
371 }
372
373 void MainWindow::about()
374 {
375     Dyalog *aboutDialog = new Dyalog(this, false);
376     aboutDialog->setWindowTitle(tr("About Dorian"));
377     QString version = Platform::instance()->version();
378     QLabel *label = new QLabel(aboutDialog);
379     label->setTextFormat(Qt::RichText);
380     label->setOpenExternalLinks(true);
381     label->setWordWrap(true);
382     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
383         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
384         "Licensed under GNU General Public License, Version 3<br>"
385         "Source code:<br><a href='http://dorian.garage.maemo.org/'>"
386         "dorian.garage.maemo.org</a>").arg(version));
387     aboutDialog->addWidget(label);
388     aboutDialog->addStretch();
389     aboutDialog->show();
390 }
391
392 void MainWindow::goToNextPage()
393 {
394     next->flash();
395     prev->flash();
396     view->goNextPage();
397 }
398
399 void MainWindow::goToPreviousPage()
400 {
401     next->flash();
402     prev->flash();
403     view->goPreviousPage();
404 }
405
406 void MainWindow::onBeginUpgrade(int total)
407 {
408     libraryProgress->setVisible(total > 0);
409     libraryProgress->setWindowTitle(tr("Upgrading library"));
410     libraryProgress->setMaximum(total);
411 }
412
413 void MainWindow::onUpgrading(const QString &path)
414 {
415     libraryProgress->setLabelText(tr("Upgrading %1").
416                                   arg(QFileInfo(path).fileName()));
417     libraryProgress->setValue(libraryProgress->value() + 1);
418 }
419
420 void MainWindow::onEndUpgrade()
421 {
422     libraryProgress->hide();
423     libraryProgress->reset();
424 }
425
426 void MainWindow::onBeginLoad(int total)
427 {
428     libraryProgress->setVisible(total > 0);
429     libraryProgress->setWindowTitle(tr("Loading library"));
430     libraryProgress->setMaximum(total);
431 }
432
433 void MainWindow::onLoading(const QString &path)
434 {
435     libraryProgress->setLabelText(tr("Loading %1").
436                                   arg(QFileInfo(path).fileName()));
437     libraryProgress->setValue(libraryProgress->value() + 1);
438 }
439
440 void MainWindow::onEndLoad()
441 {
442     libraryProgress->hide();
443     libraryProgress->reset();
444 }