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 <QtCore/QDebug>
28 #include <QMaemo5InformationBox>
29 #include "mainwindow.h"
30 #include "settingsdialog.h"
31 #include "searchdialog.h"
36 #include "sourcecoreconfig.h"
40 const QString START_ICON = ":/icons/start.png";
41 const QString CLOSE_ICON = ":/icons/stop.png";
44 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
45 searchResults_(0), settingsDialog_(0), running_(false),
46 toggleButton_(0), searchDialog_(0), aboutDialog_(0), warning_(0)
48 setWindowTitle(tr("Jenirok"));
49 setAttribute(Qt::WA_Maemo5StackedWindow);
50 QWidget* mainWidget = new QWidget(this);
52 if(Daemon::isRunning())
54 toggleButton_ = createButton(tr("Stop daemon"));
55 toggleButton_->setIcon(QIcon(CLOSE_ICON));
60 toggleButton_ = createButton(tr("Start daemon"));
61 toggleButton_->setIcon(QIcon(START_ICON));
65 QToolButton* searchButton = createButton(tr("Search"));
66 searchButton->setIcon(QIcon::fromTheme("general_search"));
69 searchButton->setIconSize(size);
70 toggleButton_->setIconSize(size);
72 QHBoxLayout *buttonLayout = new QHBoxLayout;
73 buttonLayout->addWidget(toggleButton_, Qt::AlignLeft);
74 buttonLayout->addWidget(searchButton, Qt::AlignRight);
76 mainWidget->setLayout(buttonLayout);
78 connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
79 connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
81 setCentralWidget(mainWidget);
82 menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
83 menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
86 MainWindow::~MainWindow()
91 void MainWindow::showSettings()
93 if(warning_ && warning_->isVisible())
100 settingsDialog_ = new SettingsDialog(this);
103 settingsDialog_->show();
106 void MainWindow::toggleDaemon()
115 readyText = tr("Daemon was successfully stopped.");
116 failText = tr("Unable to stop daemon.");
117 buttonText = tr("Start daemon");
118 ret = Daemon::stop();
122 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
126 warning_ = new QDialog(this);
127 warning_->setWindowTitle(tr("Unable to start daemon"));
128 QHBoxLayout* warningLayout = new QHBoxLayout;
129 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."));
130 text->setReadOnly(true);
131 QDialogButtonBox* buttons = new QDialogButtonBox;
132 buttons->setOrientation(Qt::Vertical);
133 QPushButton* settingsButton = new QPushButton(tr("Open settings"));
134 connect(settingsButton, SIGNAL(pressed()), this, SLOT(showSettings()));
135 QPushButton* okButton = new QPushButton(tr("Close"));
136 connect(okButton, SIGNAL(pressed()), warning_, SLOT(hide()));
137 buttons->addButton(settingsButton, QDialogButtonBox::YesRole);
138 buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
139 warningLayout->addWidget(text);
140 warningLayout->addWidget(buttons);
141 warning_->setLayout(warningLayout);
149 readyText = tr("Daemon was successfully started.");
150 failText = tr("Unable to start daemon.");
151 buttonText = tr("Stop daemon");
152 ret = Daemon::start();
157 QMessageBox::critical(this, tr("Error"), failText);
161 QMaemo5InformationBox::information(this, readyText);
162 toggleButton_->setText(buttonText);
163 toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
164 running_ = !running_;
169 void MainWindow::openSearch()
171 Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
172 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
174 Q_ASSERT(config != 0);
176 bool readyToSearch = config->readyToSearch();
181 QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
187 searchDialog_ = new SearchDialog(this);
188 connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
189 this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
192 searchDialog_->show();
195 QToolButton* MainWindow::createButton(QString const& text)
197 QToolButton* button = new QToolButton();
198 button->setText(text);
199 button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
203 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
205 emit search(details);
208 void MainWindow::showAbout()
212 aboutDialog_ = new AboutDialog(this);
215 aboutDialog_->show();