2 * This file is part of Jenirok.
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.
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.
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/>.
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 <QMaemo5InformationBox>
29 #include "mainwindow.h"
30 #include "settingsdialog.h"
31 #include "searchdialog.h"
38 const QString START_ICON = ":/icons/start.png";
39 const QString CLOSE_ICON = ":/icons/stop.png";
42 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
43 searchResults_(0), settingsDialog_(0), running_(false),
44 toggleButton_(0), searchDialog_(0), aboutDialog_(0), warning_(0)
46 setWindowTitle(tr("Jenirok"));
47 setAttribute(Qt::WA_Maemo5StackedWindow);
48 QWidget* mainWidget = new QWidget(this);
50 if(Daemon::isRunning())
52 toggleButton_ = createButton(tr("Stop daemon"));
53 toggleButton_->setIcon(QIcon(CLOSE_ICON));
58 toggleButton_ = createButton(tr("Start daemon"));
59 toggleButton_->setIcon(QIcon(START_ICON));
63 QToolButton* searchButton = createButton(tr("Search"));
64 searchButton->setIcon(QIcon::fromTheme("general_search"));
67 searchButton->setIconSize(size);
68 toggleButton_->setIconSize(size);
70 QHBoxLayout *buttonLayout = new QHBoxLayout;
71 buttonLayout->addWidget(toggleButton_, Qt::AlignLeft);
72 buttonLayout->addWidget(searchButton, Qt::AlignRight);
74 mainWidget->setLayout(buttonLayout);
76 connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
77 connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
79 setCentralWidget(mainWidget);
80 menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
81 menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
84 MainWindow::~MainWindow()
89 void MainWindow::showSettings()
91 if(warning_ && warning_->isVisible())
98 settingsDialog_ = new SettingsDialog(this);
101 settingsDialog_->show();
104 void MainWindow::toggleDaemon()
113 readyText = tr("Daemon was successfully stopped.");
114 failText = tr("Unable to stop daemon.");
115 buttonText = tr("Start daemon");
116 ret = Daemon::stop();
120 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
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);
147 readyText = tr("Daemon was successfully started.");
148 failText = tr("Unable to start daemon.");
149 buttonText = tr("Stop daemon");
150 ret = Daemon::start();
155 QMessageBox::critical(this, tr("Error"), failText);
159 QMaemo5InformationBox::information(this, readyText);
160 toggleButton_->setText(buttonText);
161 toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
162 running_ = !running_;
167 void MainWindow::openSearch()
171 QString username = Settings::instance()->get("eniro_username");
172 QString password = Settings::instance()->get("eniro_password");
176 if(username.isEmpty() || password.isEmpty())
178 QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature."));
184 searchDialog_ = new SearchDialog(this);
185 connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
186 this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
189 searchDialog_->show();
192 QToolButton* MainWindow::createButton(QString const& text)
194 QToolButton* button = new QToolButton();
195 button->setText(text);
196 button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
200 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
202 emit search(details);
205 void MainWindow::showAbout()
209 aboutDialog_ = new AboutDialog(this);
212 aboutDialog_->show();