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 // Show in regular (non full-screen) mode
149 // Upgrade library if needed, then load it
153 // Load book on command line, or load last read book, or load default book
154 if (QCoreApplication::arguments().size() == 2) {
155 QString path = QCoreApplication::arguments()[1];
157 QModelIndex index = library->find(path);
158 if (index.isValid()) {
159 library->setNowReading(index);
162 QModelIndex index = library->nowReading();
163 if (index.isValid()) {
164 library->setNowReading(index);
166 if (!library->rowCount()) {
167 library->add(":/books/2BR02B.epub");
169 SortedLibrary sorted;
170 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
175 void MainWindow::onCurrentBookChanged()
178 setCurrentBook(Library::instance()->nowReading());
181 void MainWindow::showRegular()
185 view->setLastBookmark();
186 fullScreenWindow->leaveBookView();
187 takeBookView(view, prog, prev, next);
189 fullScreenWindow->hide();
191 view->scheduleRestoreLastBookmark();
194 void MainWindow::showBig()
198 view->setLastBookmark();
200 fullScreenWindow->takeBookView(view, prog, prev, next);
203 fullScreenWindow->raise();
207 fullScreenWindow->showFullScreen();
208 view->scheduleRestoreLastBookmark();
211 void MainWindow::setCurrentBook(const QModelIndex ¤t)
214 Book *book = Library::instance()->book(current);
216 setWindowTitle(book? book->shortName(): tr("Dorian"));
219 void MainWindow::showLibrary()
221 (new LibraryDialog(this))->show();
224 void MainWindow::showSettings()
226 (new SettingsWindow(this))->show();
229 void MainWindow::rotate()
231 QString current = Settings::instance()->value("orientation",
232 Platform::instance()->defaultOrientation()).toString();
233 QString target = (current == "landscape")? "portrait": "landscape";
234 view->setLastBookmark();
235 Settings::instance()->setValue("orientation", target);
238 void MainWindow::showDevTools()
240 (new DevTools(this))->exec();
243 void MainWindow::showBookmarks()
245 Book *book = Library::instance()->book(mCurrent);
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)));
256 void MainWindow::onSettingsChanged(const QString &key)
259 qDebug() << "Key" << key;
261 if (key == "orientation") {
262 QString value = Settings::instance()->value(key,
263 Platform::instance()->defaultOrientation()).toString();
264 qDebug() << "Value: orientation" << value;
265 Platform::instance()->setOrientation(this, value);
266 Platform::instance()->setOrientation(fullScreenWindow, value);
269 #if defined(Q_WS_MAEMO_5)
270 else if (key == "lightson") {
271 bool enable = Settings::instance()->value(key, false).toBool();
272 killTimer(preventBlankingTimer);
274 preventBlankingTimer = startTimer(29 * 1000);
277 #endif // defined(Q_WS_MAEMO_5)
280 void MainWindow::onPartLoadStart()
283 Platform::instance()->showBusy(this, true);
286 void MainWindow::onPartLoadEnd(int index)
289 bool enablePrevious = false;
290 bool enableNext = false;
291 Book *book = Library::instance()->book(mCurrent);
294 enablePrevious = true;
296 if (index < (book->parts.size() - 1)) {
300 Platform::instance()->showBusy(this, false);
303 void MainWindow::onAddBookmark(const QString ¬e)
306 view->addBookmark(note);
307 Platform::instance()->information(tr("Bookmarked current position"), this);
310 void MainWindow::onGoToBookmark(int index)
313 Book *book = Library::instance()->book(mCurrent);
314 view->goToBookmark(book->bookmarks()[index]);
317 void MainWindow::showChapters()
319 Book *book = Library::instance()->book(mCurrent);
321 ChaptersDialog *chapters = new ChaptersDialog(book, this);
322 connect(chapters, SIGNAL(goToChapter(int)),
323 this, SLOT(onGoToChapter(int)));
328 void MainWindow::onGoToChapter(int index)
332 Book *book = Library::instance()->book(mCurrent);
335 int partIndex = book->partFromChapter(index, fragment);
336 if (partIndex != -1) {
337 view->goToPart(partIndex, fragment);
342 void MainWindow::timerEvent(QTimerEvent *event)
344 if (event->timerId() == preventBlankingTimer) {
346 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
347 MCE_REQUEST_IF, QDBusConnection::systemBus());
348 mce.call(MCE_PREVENT_BLANK_REQ);
349 #endif // Q_WS_MAEMO_5
350 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
352 AdopterWindow::timerEvent(event);
355 void MainWindow::about()
357 Dyalog *aboutDialog = new Dyalog(this, false);
358 aboutDialog->setWindowTitle(tr("About Dorian"));
359 QString version = Platform::instance()->version();
360 QLabel *label = new QLabel(aboutDialog);
361 label->setTextFormat(Qt::RichText);
362 label->setOpenExternalLinks(true);
363 label->setWordWrap(true);
364 label->setText(tr("<b>Dorian %1</b><br><br>"
365 "Copyright © 2010-2011 by "
366 "Akos Polster <akos@pipacs.com><br><br>"
367 "Licensed under GNU General Public License, Version 3<br><br>"
368 "<a href='http://dorian.garage.maemo.org/'>"
369 "dorian.garage.maemo.org</a><br><br>"
371 aboutDialog->addWidget(label);
372 aboutDialog->addStretch();
376 void MainWindow::goToNextPage()
383 void MainWindow::goToPreviousPage()
387 view->goPreviousPage();
390 void MainWindow::onBeginUpgrade(int total)
392 libraryProgress->setVisible(total > 0);
393 libraryProgress->setWindowTitle(tr("Upgrading library"));
394 libraryProgress->setMaximum(total);
397 void MainWindow::onUpgrading(const QString &path)
399 libraryProgress->setLabelText(tr("Upgrading %1").
400 arg(QFileInfo(path).fileName()));
401 libraryProgress->setValue(libraryProgress->value() + 1);
404 void MainWindow::onEndUpgrade()
406 libraryProgress->hide();
407 libraryProgress->reset();
410 void MainWindow::onBeginLoad(int total)
412 libraryProgress->setVisible(total > 0);
413 libraryProgress->setWindowTitle(tr("Loading library"));
414 libraryProgress->setMaximum(total);
417 void MainWindow::onLoading(const QString &path)
419 libraryProgress->setLabelText(tr("Loading %1").
420 arg(QFileInfo(path).fileName()));
421 libraryProgress->setValue(libraryProgress->value() + 1);
424 void MainWindow::onEndLoad()
426 libraryProgress->hide();
427 libraryProgress->reset();