9b0f45994917ec8ae15f693aea8980449851fd91
[jenirok] / src / gui / mainwindow.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtGui/QMenuBar>
20 #include <QtGui/QLabel>
21 #include <QtGui/QPushButton>
22 #include <QtGui/QWidget>
23 #include <QtGui/QHBoxLayout>
24 #include <QtGui/QMessageBox>
25 #include <QMaemo5InformationBox>
26 #include <QDebug>
27 #include "mainwindow.h"
28 #include "settingsdialog.h"
29 #include "searchdialog.h"
30 #include "daemon.h"
31 #include "settings.h"
32 #include "db.h"
33
34 namespace
35 {
36     const QString START_ICON = ":/icons/start.png";
37     const QString CLOSE_ICON = ":/icons/stop.png";
38 }
39
40 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
41 searchResults_(0), settingsDialog_(0), running_(false),
42 toggleButton_(0), searchDialog_(0), aboutDialog_(0)
43 {
44     setWindowTitle(tr("Jenirok"));
45     setAttribute(Qt::WA_Maemo5StackedWindow);
46     QWidget* mainWidget = new QWidget(this);
47
48     if(Daemon::isRunning())
49     {
50         toggleButton_ = createButton(tr("Stop daemon"));
51         toggleButton_->setIcon(QIcon(CLOSE_ICON));
52         running_ = true;
53     }
54     else
55     {
56         toggleButton_ = createButton(tr("Start daemon"));
57         toggleButton_->setIcon(QIcon(START_ICON));
58         running_ = false;
59     }
60
61     QToolButton* searchButton = createButton(tr("Search"));
62     searchButton->setIcon(QIcon::fromTheme("general_search"));
63
64     QSize size(64, 64);
65     searchButton->setIconSize(size);
66     toggleButton_->setIconSize(size);
67
68     QHBoxLayout *buttonLayout = new QHBoxLayout;
69     buttonLayout->addWidget(toggleButton_, Qt::AlignLeft);
70     buttonLayout->addWidget(searchButton, Qt::AlignRight);
71
72     mainWidget->setLayout(buttonLayout);
73
74     connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
75     connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
76
77     setCentralWidget(mainWidget);
78     menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
79     menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
80 }
81
82 MainWindow::~MainWindow()
83 {
84     DB::removeDatabase();
85 }
86
87 void MainWindow::showSettings()
88 {
89     if(!settingsDialog_)
90     {
91         settingsDialog_ = new SettingsDialog(this);
92     }
93
94     settingsDialog_->show();
95 }
96
97 void MainWindow::toggleDaemon()
98 {
99     QString readyText;
100     QString failText;
101     QString buttonText;
102     bool ret = false;
103
104     if(running_)
105     {
106         readyText = tr("Daemon was successfully stopped.");
107         failText = tr("Unable to stop daemon.");
108         buttonText = tr("Start daemon");
109         ret = Daemon::stop();
110     }
111     else
112     {
113         readyText = tr("Daemon was successfully started.");
114         failText = tr("Unable to start daemon.");
115         buttonText = tr("Stop daemon");
116         ret = Daemon::start();
117     }
118
119     if(!ret)
120     {
121         QMessageBox::critical(this, tr("Error"), failText);
122     }
123     else
124     {
125         QMaemo5InformationBox::information(this, readyText);
126         toggleButton_->setText(buttonText);
127         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
128         running_ = !running_;
129     }
130
131 }
132
133 void MainWindow::openSearch()
134 {
135     DB::connect();
136
137     QString username = Settings::instance()->get("eniro_username");
138     QString password = Settings::instance()->get("eniro_password");
139
140     DB::disconnect();
141
142     if(username.isEmpty() || password.isEmpty())
143     {
144         QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature."));
145         return;
146     }
147
148     if(!searchDialog_)
149     {
150         searchDialog_ = new SearchDialog(this);
151         connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)), this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
152     }
153
154     searchDialog_->show();
155 }
156
157 QToolButton* MainWindow::createButton(QString const& text)
158 {
159     QToolButton* button = new QToolButton();
160     button->setText(text);
161     button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
162     return button;
163 }
164
165 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
166 {
167     emit search(details);
168 }
169
170 void MainWindow::showAbout()
171 {
172         if(!aboutDialog_)
173         {
174                 aboutDialog_ = new AboutDialog(this);
175         }
176
177         aboutDialog_->show();
178
179 }