Fixes. About dialog. Books properly removed from library when file is deleted.
[mebook:mebook.git] / ui / startwindow.cpp
1 #include "startwindow.h"
2 #include "mainwindow.h"
3
4 #include <QScrollArea>
5 #include <QFileDialog>
6 #include <QMenuBar>
7
8 #include <QMaemo5InformationBox>
9
10 #include "shared.h"
11
12
13 StartWindow::StartWindow(QWidget *parent) : QMainWindow(parent), readingUI(0), searcCatalogsView(0)
14 {
15     setWindowTitle("MeBook");
16     
17     scrollArea = new QScrollArea(this);
18     setCentralWidget(scrollArea);
19
20     //Scroller is used to detect if user is tapping an item or scrolling the QAbstractKineticScroller.
21     //TODO: There might be a better way to do this..
22     scroller = scrollArea->property("kineticScroller").value<QAbstractKineticScroller*>();
23     
24     setAttribute(Qt::WA_Maemo5StackedWindow);
25
26     bookList = new QWidget();
27     bookList->setContextMenuPolicy(Qt::NoContextMenu);
28     scrollArea->setWidget(bookList);
29     scrollArea->setWidgetResizable(true);
30
31     listLayout = new QVBoxLayout(bookList);
32     listLayout->setSpacing(14);
33
34     QPushButton *openFileButton = new QPushButton(tr("Open file"), this);
35     listLayout->addWidget(openFileButton);
36     listLayout->setAlignment(openFileButton, Qt::AlignTop);
37     listLayout->setAlignment(Qt::AlignTop);
38     connect(openFileButton, SIGNAL(pressed()), this, SLOT(openFile()));
39
40     QPushButton *searchCatalogsButton = new QPushButton(tr("Search catalogs"), this);
41     listLayout->addWidget(searchCatalogsButton);
42     listLayout->setAlignment(searchCatalogsButton, Qt::AlignTop);
43     connect(searchCatalogsButton, SIGNAL(clicked()), this, SLOT(searchCatalogs()));
44
45     QMenuBar *menuBar = new QMenuBar(this);
46     QAction *settings = menuBar->addAction(tr("Settings"));
47     connect(settings, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
48     QAction *about = menuBar->addAction(tr("About"));
49     connect(about, SIGNAL(triggered()), this, SLOT(openAboutDialog()));
50
51     setMenuBar(menuBar);
52
53     readSettings();
54
55     library = new Library("/home/user/MyDocs/books", "/home/user/MyDocs/books/bookdb.db", this);
56
57     connect(library, SIGNAL(bookAdded(Book*)), this, SLOT(addBook(Book*)));
58     connect(library, SIGNAL(bookRemoved(Book*)), this, SLOT(removeBook(Book*)));
59     library->loadLibrary();
60
61     feedBooksCatalog = new FeedBooks(this);
62     connect(feedBooksCatalog, SIGNAL(bookSaved(Book*)), library, SLOT(addToLibrary(Book*)));
63     connect(feedBooksCatalog, SIGNAL(message(const QString)), this, SLOT(showMessage(const QString)));
64     connect(feedBooksCatalog, SIGNAL(openBook(Book*)), this, SLOT(openBook(Book*)));
65
66     osso_context = osso_initialize(OSSO_NAME.toUtf8(), "0.0.1", TRUE, NULL);
67
68 }
69
70 StartWindow::~StartWindow()
71 {
72     if(osso_context)
73         osso_deinitialize(osso_context);
74 }
75
76
77 void StartWindow::readSettings()
78 {
79
80     settings = new QSettings("lue", "MeBook-0.0.8");
81
82     setAttribute(static_cast<Qt::WidgetAttribute>(settings->value("orientation").toInt()), true);
83
84
85 }
86
87 void StartWindow::openFile()
88 {
89     QString filename = QFileDialog::getOpenFileName(this, tr("Open book"), "/home/user/MyDocs", tr("eBook Files (*.epub)"));
90     
91     if(!filename.isEmpty()){
92         Book *book = new Book(filename);
93         library->addToLibrary(book);
94         openBook(book);
95     }
96 }
97
98 void StartWindow::openBook(Book *book)
99 {
100     setAttribute(static_cast<Qt::WidgetAttribute>(settings->value("orientation").toInt()), true);
101
102     if(!readingUI){
103         readingUI = new MainWindow(osso_context, this);
104         connect(readingUI, SIGNAL(closing()), this, SLOT(bookViewClosed()));
105     }
106
107     readingUI->setFont(settings->value("font-family", "Sans Serif").toString(), settings->value("font-size", 24).toInt());
108     readingUI->setColorMode(!settings->value("colors", true).toBool());
109     readingUI->enableBacklightForcing(settings->value("backlight", false).toBool());
110     readingUI->setViewMode(static_cast<BookViewMode>(settings->value("viewmode", 1).toInt()), static_cast<Qt::WidgetAttribute>(settings->value("orientation", static_cast<int>(Qt::WA_Maemo5PortraitOrientation)).toInt()));
111
112     readingUI->openBook(book);
113     readingUI->show();
114
115     //Hide other windows 
116     if(searcCatalogsView){
117         if(searcCatalogsView->isVisible())
118             searcCatalogsView->hide();
119     }
120
121 }
122
123 void StartWindow::openSettingsDialog()
124 {
125     SettingsDialog *settingsDialog = new SettingsDialog(this);    
126     settingsDialog->setOrientation(settings->value("orientation").toInt());
127     settingsDialog->setColors(settings->value("colors").toInt());
128     settingsDialog->setFontFamily(settings->value("font-family").toString());
129     settingsDialog->setFontSize(settings->value("font-size").toInt());
130     settingsDialog->setBacklightForce(settings->value("backlight").toBool());
131     settingsDialog->setViewMode(static_cast<BookViewMode>(settings->value("viewmode").toInt()));
132
133
134     if(settingsDialog->exec() == QDialog::Accepted){
135         int orientation = settingsDialog->getOrientation();
136         if(orientation != settings->value("orientation")){
137             settings->setValue("orientation", orientation); 
138             setAttribute(static_cast<Qt::WidgetAttribute>(orientation), true);
139         }
140
141         int colors = settingsDialog->getColors();
142         if(colors != settings->value("colors")){
143             settings->setValue("colors", colors);
144         }
145
146         settings->setValue("font-family", settingsDialog->getFontFamily());
147         settings->setValue("font-size", settingsDialog->getFontSize());
148         settings->setValue("backlight", settingsDialog->getBacklighForce());
149         settings->setValue("viewmode", settingsDialog->getViewMode());
150
151         settings->sync();
152     }
153 }
154
155 void StartWindow::openAboutDialog()
156 {
157     QDialog dialog(this);
158     dialog.setWindowTitle(tr("About MeBook"));
159     QHBoxLayout horizontalLayout;
160
161     QLabel icon;
162     QPixmap pixmap = QIcon::fromTheme("mebook").pixmap(48, 48);
163     icon.setPixmap(pixmap.scaled(QSize(72, 72)));
164     horizontalLayout.addWidget(&icon);
165     horizontalLayout.setAlignment(&icon, Qt::AlignTop);
166
167     QVBoxLayout verticalLayout;
168     horizontalLayout.addLayout(&verticalLayout);
169     horizontalLayout.setAlignment(&verticalLayout, Qt::AlignLeft);
170     horizontalLayout.setStretch(1, 2);
171
172     //TODO: Get version number automagically.
173     QLabel name("MeBook 0.1.7");
174     name.setFont(QFont("Sans", 22));
175     verticalLayout.addWidget(&name);
176     verticalLayout.setAlignment(&name, Qt::AlignLeft | Qt::AlignTop);
177
178     QLabel copyright("<qt>&copy; 2010 by Juhana Jauhiainen</qt>");
179     QFont copyrightFont("Sans", 12);
180     QPalette palette = QApplication::palette();
181     palette.setColor(QPalette::Normal, QPalette::WindowText, Qt::darkGray);
182     copyright.setPalette(palette);
183     copyright.setFont(copyrightFont);
184     verticalLayout.addWidget(&copyright);
185     verticalLayout.setStretch(2, 3);
186     verticalLayout.setAlignment(&copyright, Qt::AlignLeft | Qt::AlignBottom);
187
188     dialog.setLayout(&horizontalLayout);
189
190     QPushButton reportBug(tr("Report bugs"));
191     connect(&reportBug, SIGNAL(clicked()), this, SLOT(reportBugs()));
192     horizontalLayout.addWidget(&reportBug);
193     dialog.exec();
194 }
195
196 void StartWindow::searchCatalogs()
197 {
198     if(!searcCatalogsView){
199         searcCatalogsView = new SearchCatalogsView(feedBooksCatalog, this);
200         connect(searcCatalogsView, SIGNAL(closing()), this, SLOT(searchCatalogsClosed()));
201     }
202     setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
203     searcCatalogsView->show();
204 }
205
206 void StartWindow::searchCatalogsClosed()
207 {
208     setAttribute(static_cast<Qt::WidgetAttribute>(settings->value("orientation").toInt()), true);
209 }
210
211 void StartWindow::bookViewClosed()
212 {
213     delete readingUI;
214     readingUI = 0;
215 }
216
217 void StartWindow::showMessage(const QString message)
218 {
219     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout); 
220 }
221
222 void StartWindow::addBook(Book *book)
223 {
224
225     BookListItem *item;
226     item = new BookListItem(book, scroller, scrollArea);
227     listLayout->addWidget(item);
228     connect(item, SIGNAL(selected(Book*)), this, SLOT(openBook(Book*)));
229     listLayout->setAlignment(item, Qt::AlignTop);
230     //Call adjust size so widget's size is recalculated.
231     bookList->adjustSize();
232     bookListItems.push_back(item);
233 }
234
235 void StartWindow::removeBook(Book *book)
236 {
237     if(!book->getFilename().isEmpty()){
238         for(QList<BookListItem*>::iterator iter = bookListItems.begin(); iter != bookListItems.end(); ++iter){
239             BookListItem *item = *iter;
240             if(item->book()->getFilename() == book->getFilename()){
241                 bookListItems.removeOne(item);
242                 listLayout->removeWidget(item);
243                 delete item;
244                 bookList->adjustSize();
245                 return;
246             
247             }
248         }
249     }
250 }
251
252 void StartWindow::reportBugs()
253 {
254     QDesktopServices::openUrl(QUrl("http://code.google.com/p/mebook/issues/"));
255 }