6 # include <QtGui/QX11Info>
8 # include <X11/Xatom.h>
9 # include <mce/mode-names.h>
10 # include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
16 #include "infodialog.h"
17 #include "librarydialog.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
26 #include "bookfinder.h"
29 #include "translucentbutton.h"
31 #include "progressdialog.h"
32 #include "sortedlibrary.h"
34 #ifdef DORIAN_TEST_MODEL
35 # include "modeltest.h"
38 MainWindow::MainWindow(QWidget *parent):
39 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
43 setAttribute(Qt::WA_Maemo5StackedWindow, true);
47 setWindowTitle("Dorian");
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);
55 central->setLayout(layout);
56 setCentralWidget(central);
59 view = new BookView(this);
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);
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()));
76 settingsAction = addToolBarAction(this, SLOT(showSettings()),
77 "preferences-system", tr("Settings"));
78 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
79 "developer", tr("Developer"));
82 rotateAction = addToolBarAction(this, SLOT(rotate()),
83 "rotate", tr("Rotate"), true);
85 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
86 "view-fullscreen", tr("Full screen"), true);
88 #if defined(Q_WS_MAEMO_5)
89 QAction *aboutAction = menuBar()->addAction(tr("About"));
90 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
92 addToolBarAction(this, SLOT(about()), "about", tr("About"));
93 #endif // defined(Q_WS_MAEMO_5)
95 #if defined(Q_OS_SYMBIAN)
96 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"), false);
97 #endif // defined(Q_OS_SYMBIAN)
100 prev = new TranslucentButton("back", this);
101 next = new TranslucentButton("forward", this);
102 prog = new Progress(this);
104 // Handle model changes
105 connect(Library::instance(), SIGNAL(nowReadingChanged()),
106 this, SLOT(onCurrentBookChanged()));
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()));
117 // Handle loading book parts
118 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
119 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
122 connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
124 // Shadow window for full screen reading
125 fullScreenWindow = new FullScreenWindow(this);
126 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
128 // Handle settings changes
129 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
130 this, SLOT(onSettingsChanged(const QString &)));
132 // Handle book view buttons
133 connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
134 connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
136 #ifdef DORIAN_TEST_MODEL
137 (void)new ModelTest(Library::instance(), this);
141 void MainWindow::initialize()
144 Library *library = Library::instance();
146 // Upgrade library if needed, then load it
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];
154 QModelIndex index = library->find(path);
155 if (index.isValid()) {
156 library->setNowReading(index);
159 QModelIndex index = library->nowReading();
160 if (index.isValid()) {
161 library->setNowReading(index);
163 if (!library->rowCount()) {
164 library->add(":/books/2BR02B.epub");
166 SortedLibrary sorted;
167 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
171 // Show in regular (non full-screen) mode
175 void MainWindow::onCurrentBookChanged()
178 setCurrentBook(Library::instance()->nowReading());
181 void MainWindow::showRegular()
185 // Re-parent children
186 fullScreenWindow->leaveBookView();
187 takeBookView(view, prog, prev, next);
189 fullScreenWindow->hide();
193 void MainWindow::showBig()
197 // Re-parent children
199 fullScreenWindow->takeBookView(view, prog, prev, next);
202 fullScreenWindow->showFullScreen();
205 void MainWindow::setCurrentBook(const QModelIndex ¤t)
208 Book *book = Library::instance()->book(current);
210 setWindowTitle(book? book->shortName(): tr("Dorian"));
213 void MainWindow::showLibrary()
215 (new LibraryDialog(this))->show();
218 void MainWindow::showSettings()
220 (new SettingsWindow(this))->show();
223 void MainWindow::rotate()
225 QString current = Settings::instance()->value("orientation",
226 Platform::instance()->defaultOrientation()).toString();
227 QString target = (current == "landscape")? "portrait": "landscape";
228 Settings::instance()->setValue("orientation", target);
231 void MainWindow::showDevTools()
233 (new DevTools())->exec();
236 void MainWindow::showBookmarks()
238 Book *book = Library::instance()->book(mCurrent);
240 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
241 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
242 this, SLOT(onAddBookmark(const QString &)));
243 connect(bookmarks, SIGNAL(goToBookmark(int)),
244 this, SLOT(onGoToBookmark(int)));
249 void MainWindow::onSettingsChanged(const QString &key)
252 qDebug() << "Key" << key;
254 if (key == "orientation") {
255 view->setLastBookmark();
256 QString value = Settings::instance()->value(key,
257 Platform::instance()->defaultOrientation()).toString();
258 qDebug() << "Value: orientation" << value;
259 Platform::instance()->setOrientation(this, value);
260 Platform::instance()->setOrientation(fullScreenWindow, value);
263 #if defined(Q_WS_MAEMO_5)
264 else if (key == "lightson") {
265 bool enable = Settings::instance()->value(key, false).toBool();
266 killTimer(preventBlankingTimer);
268 preventBlankingTimer = startTimer(29 * 1000);
271 #endif // defined(Q_WS_MAEMO_5)
274 void MainWindow::onPartLoadStart()
277 Platform::instance()->showBusy(this, true);
280 void MainWindow::onPartLoadEnd(int index)
283 bool enablePrevious = false;
284 bool enableNext = false;
285 Book *book = Library::instance()->book(mCurrent);
288 enablePrevious = true;
290 if (index < (book->parts.size() - 1)) {
294 Platform::instance()->showBusy(this, false);
297 void MainWindow::onAddBookmark(const QString ¬e)
300 view->addBookmark(note);
301 Platform::instance()->information(tr("Bookmarked current position"), this);
304 void MainWindow::onGoToBookmark(int index)
307 Book *book = Library::instance()->book(mCurrent);
308 view->goToBookmark(book->bookmarks()[index]);
311 void MainWindow::showChapters()
313 Book *book = Library::instance()->book(mCurrent);
315 ChaptersDialog *chapters = new ChaptersDialog(book, this);
316 connect(chapters, SIGNAL(goToChapter(int)),
317 this, SLOT(onGoToChapter(int)));
322 void MainWindow::onGoToChapter(int index)
326 Book *book = Library::instance()->book(mCurrent);
329 int partIndex = book->partFromChapter(index, fragment);
330 if (partIndex != -1) {
331 view->goToPart(partIndex, fragment);
336 void MainWindow::timerEvent(QTimerEvent *event)
338 if (event->timerId() == preventBlankingTimer) {
340 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
341 MCE_REQUEST_IF, QDBusConnection::systemBus());
342 mce.call(MCE_PREVENT_BLANK_REQ);
343 #endif // Q_WS_MAEMO_5
344 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
346 AdopterWindow::timerEvent(event);
349 void MainWindow::about()
351 Dyalog *aboutDialog = new Dyalog(this, false);
352 aboutDialog->setWindowTitle(tr("About Dorian"));
353 QString version = Platform::instance()->version();
354 QLabel *label = new QLabel(aboutDialog);
355 label->setTextFormat(Qt::RichText);
356 label->setOpenExternalLinks(true);
357 label->setWordWrap(true);
358 label->setText(tr("<b>Dorian %1</b><br><br>"
359 "Copyright © 2010-2011 by "
360 "Akos Polster <akos@pipacs.com><br><br>"
361 "Licensed under GNU General Public License, Version 3<br><br>"
362 "<a href='http://dorian.garage.maemo.org/'>"
363 "dorian.garage.maemo.org</a><br><br>"
365 aboutDialog->addWidget(label);
366 aboutDialog->addStretch();
370 void MainWindow::goToNextPage()
377 void MainWindow::goToPreviousPage()
381 view->goPreviousPage();
384 void MainWindow::onBeginUpgrade(int total)
386 libraryProgress->setVisible(total > 0);
387 libraryProgress->setWindowTitle(tr("Upgrading library"));
388 libraryProgress->setMaximum(total);
391 void MainWindow::onUpgrading(const QString &path)
393 libraryProgress->setLabelText(tr("Upgrading %1").
394 arg(QFileInfo(path).fileName()));
395 libraryProgress->setValue(libraryProgress->value() + 1);
398 void MainWindow::onEndUpgrade()
400 libraryProgress->hide();
401 libraryProgress->reset();
404 void MainWindow::onBeginLoad(int total)
406 libraryProgress->setVisible(total > 0);
407 libraryProgress->setWindowTitle(tr("Loading library"));
408 libraryProgress->setMaximum(total);
411 void MainWindow::onLoading(const QString &path)
413 libraryProgress->setLabelText(tr("Loading %1").
414 arg(QFileInfo(path).fileName()));
415 libraryProgress->setValue(libraryProgress->value() + 1);
418 void MainWindow::onEndLoad()
420 libraryProgress->hide();
421 libraryProgress->reset();