d340fa19dd72d0dcea559a85a06933c18cd438bc
[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)
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 }
80
81 MainWindow::~MainWindow()
82 {
83         DB::removeDatabase();
84 }
85
86 void MainWindow::showSettings()
87 {
88         if(!settingsDialog_)
89         {
90                 settingsDialog_ = new SettingsDialog(this);
91         }
92
93         settingsDialog_->show();
94 }
95
96 void MainWindow::toggleDaemon()
97 {
98     QString readyText;
99     QString failText;
100     QString buttonText;
101     bool ret = false;
102
103     if(running_)
104     {
105                 readyText = tr("Daemon was successfully stopped.");
106                 failText = tr("Unable to stop daemon.");
107                 buttonText = tr("Start daemon");
108                 ret = Daemon::stop();
109     }
110     else
111     {
112         readyText = tr("Daemon was successfully started.");
113         failText = tr("Unable to start daemon.");
114         buttonText = tr("Stop daemon");
115         ret = Daemon::start();
116     }
117
118     if(!ret)
119     {
120         QMessageBox::critical(this, tr("Error"), failText);
121     }
122     else
123     {
124         QMaemo5InformationBox::information(this, readyText);
125         toggleButton_->setText(buttonText);
126         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
127         running_ = !running_;
128     }
129
130 }
131
132 void MainWindow::openSearch()
133 {
134         DB::connect();
135
136         QString username = Settings::instance()->get("eniro_username");
137         QString password = Settings::instance()->get("eniro_password");
138
139         DB::disconnect();
140
141         if(username.isEmpty() || password.isEmpty())
142         {
143                 QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature."));
144                 return;
145         }
146
147         if(!searchDialog_)
148         {
149                 searchDialog_ = new SearchDialog(this);
150                 connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)), this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
151         }
152
153         searchDialog_->show();
154 }
155
156 QToolButton* MainWindow::createButton(QString const& text)
157 {
158         QToolButton* button = new QToolButton();
159         button->setText(text);
160         button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
161         return button;
162 }
163
164 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
165 {
166         emit search(details);
167 }