Unnecessary includes removed.
[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 <QtGui/QDialogButtonBox>
26 #include <QtGui/QTextEdit>
27 #include <QtCore/QDebug>
28 #include <QMaemo5InformationBox>
29 #include "mainwindow.h"
30 #include "settingsdialog.h"
31 #include "searchdialog.h"
32 #include "daemon.h"
33 #include "settings.h"
34 #include "db.h"
35
36 namespace
37 {
38     const QString START_ICON = ":/icons/start.png";
39     const QString CLOSE_ICON = ":/icons/stop.png";
40 }
41
42 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
43 searchResults_(0), settingsDialog_(0), running_(false),
44 toggleButton_(0), searchDialog_(0), aboutDialog_(0), warning_(0)
45 {
46     setWindowTitle(tr("Jenirok"));
47     setAttribute(Qt::WA_Maemo5StackedWindow);
48     QWidget* mainWidget = new QWidget(this);
49
50     if(Daemon::isRunning())
51     {
52         toggleButton_ = createButton(tr("Stop daemon"));
53         toggleButton_->setIcon(QIcon(CLOSE_ICON));
54         running_ = true;
55     }
56     else
57     {
58         toggleButton_ = createButton(tr("Start daemon"));
59         toggleButton_->setIcon(QIcon(START_ICON));
60         running_ = false;
61     }
62
63     QToolButton* searchButton = createButton(tr("Search"));
64     searchButton->setIcon(QIcon::fromTheme("general_search"));
65
66     QSize size(64, 64);
67     searchButton->setIconSize(size);
68     toggleButton_->setIconSize(size);
69
70     QHBoxLayout *buttonLayout = new QHBoxLayout;
71     buttonLayout->addWidget(toggleButton_, Qt::AlignLeft);
72     buttonLayout->addWidget(searchButton, Qt::AlignRight);
73
74     mainWidget->setLayout(buttonLayout);
75
76     connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
77     connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
78
79     setCentralWidget(mainWidget);
80     menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
81     menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
82 }
83
84 MainWindow::~MainWindow()
85 {
86     DB::removeDatabase();
87 }
88
89 void MainWindow::showSettings()
90 {
91     if(warning_ && warning_->isVisible())
92     {
93         warning_->hide();
94     }
95
96     if(!settingsDialog_)
97     {
98         settingsDialog_ = new SettingsDialog(this);
99     }
100
101     settingsDialog_->show();
102 }
103
104 void MainWindow::toggleDaemon()
105 {
106     QString readyText;
107     QString failText;
108     QString buttonText;
109     bool ret = false;
110
111     if(running_)
112     {
113         readyText = tr("Daemon was successfully stopped.");
114         failText = tr("Unable to stop daemon.");
115         buttonText = tr("Start daemon");
116         ret = Daemon::stop();
117     }
118     else
119     {
120         if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
121         {
122             if(!warning_)
123             {
124                 warning_ = new QDialog(this);
125                 warning_->setWindowTitle(tr("Unable to start daemon"));
126                 QHBoxLayout* warningLayout = new QHBoxLayout;
127                 QTextEdit* text = new QTextEdit(tr("Daemon cannot be started because it's not allowed to connect to the Internet. You have to either allow automatic Internet connection in Jenirok settings or in global Maemo settings."));
128                 text->setReadOnly(true);
129                 QDialogButtonBox* buttons = new QDialogButtonBox;
130                 buttons->setOrientation(Qt::Vertical);
131                 QPushButton* settingsButton = new QPushButton(tr("Open settings"));
132                 connect(settingsButton, SIGNAL(pressed()), this, SLOT(showSettings()));
133                 QPushButton* okButton = new QPushButton(tr("Close"));
134                 connect(okButton, SIGNAL(pressed()), warning_, SLOT(hide()));
135                 buttons->addButton(settingsButton, QDialogButtonBox::YesRole);
136                 buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
137                 warningLayout->addWidget(text);
138                 warningLayout->addWidget(buttons);
139                 warning_->setLayout(warningLayout);
140             }
141
142             warning_->show();
143
144             return;
145         }
146
147         readyText = tr("Daemon was successfully started.");
148         failText = tr("Unable to start daemon.");
149         buttonText = tr("Stop daemon");
150         ret = Daemon::start();
151     }
152
153     if(!ret)
154     {
155         QMessageBox::critical(this, tr("Error"), failText);
156     }
157     else
158     {
159         QMaemo5InformationBox::information(this, readyText);
160         toggleButton_->setText(buttonText);
161         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
162         running_ = !running_;
163     }
164
165 }
166
167 void MainWindow::openSearch()
168 {
169     DB::connect();
170
171     QString username = Settings::instance()->get("eniro_username");
172     QString password = Settings::instance()->get("eniro_password");
173
174     DB::disconnect();
175
176     if(username.isEmpty() || password.isEmpty())
177     {
178         QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature."));
179         return;
180     }
181
182     if(!searchDialog_)
183     {
184         searchDialog_ = new SearchDialog(this);
185         connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
186                 this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
187     }
188
189     searchDialog_->show();
190 }
191
192 QToolButton* MainWindow::createButton(QString const& text)
193 {
194     QToolButton* button = new QToolButton();
195     button->setText(text);
196     button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
197     return button;
198 }
199
200 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
201 {
202     emit search(details);
203 }
204
205 void MainWindow::showAbout()
206 {
207     if(!aboutDialog_)
208     {
209         aboutDialog_ = new AboutDialog(this);
210     }
211
212     aboutDialog_->show();
213
214 }