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),
49 setWindowTitle(tr("Jenirok"));
50 setAttribute(Qt::WA_Maemo5StackedWindow);
51 QWidget* mainWidget = new QWidget(this);
53 if(Daemon::isRunning())
55 toggleButton_ = createButton(tr("Stop daemon"));
56 toggleButton_->setIcon(QIcon(CLOSE_ICON));
61 toggleButton_ = createButton(tr("Start daemon"));
62 toggleButton_->setIcon(QIcon(START_ICON));
68 policy.setHorizontalPolicy(QSizePolicy::Preferred);
70 toggleButton_->setSizePolicy(policy);
72 QToolButton* searchButton = createButton(tr("Search"));
73 searchButton->setIcon(QIcon::fromTheme("general_search"));
74 searchButton->setSizePolicy(policy);
76 QToolButton* logButton = createButton(tr("Log"));
77 logButton->setIcon(QIcon::fromTheme("general_call"));
78 logButton->setSizePolicy(policy);
81 searchButton->setIconSize(size);
82 toggleButton_->setIconSize(size);
83 logButton->setIconSize(size);
85 QHBoxLayout *buttonLayout = new QHBoxLayout;
86 buttonLayout->addWidget(toggleButton_, Qt::AlignHCenter);
87 buttonLayout->addWidget(searchButton, Qt::AlignHCenter);
88 buttonLayout->addWidget(logButton, Qt::AlignHCenter);
90 mainWidget->setLayout(buttonLayout);
92 connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
93 connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
94 connect(logButton, SIGNAL(pressed()), this, SLOT(openLog()));
96 setCentralWidget(mainWidget);
97 menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
98 menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
101 MainWindow::~MainWindow()
103 DB::removeDatabase();
106 void MainWindow::showSettings()
108 if(warning_ && warning_->isVisible())
115 settingsDialog_ = new SettingsDialog(this);
119 connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
123 settingsDialog_->show();
126 void MainWindow::toggleDaemon()
135 readyText = tr("Daemon was successfully stopped.");
136 failText = tr("Unable to stop daemon.");
137 buttonText = tr("Start daemon");
138 ret = Daemon::stop();
142 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
146 warning_ = new QDialog(this);
147 warning_->setWindowTitle(tr("Unable to start daemon"));
148 QHBoxLayout* warningLayout = new QHBoxLayout;
149 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."));
150 text->setReadOnly(true);
151 QDialogButtonBox* buttons = new QDialogButtonBox;
152 buttons->setOrientation(Qt::Vertical);
153 QPushButton* settingsButton = new QPushButton(tr("Open settings"));
154 connect(settingsButton, SIGNAL(pressed()), this, SLOT(showSettings()));
155 QPushButton* okButton = new QPushButton(tr("Close"));
156 connect(okButton, SIGNAL(pressed()), warning_, SLOT(hide()));
157 buttons->addButton(settingsButton, QDialogButtonBox::YesRole);
158 buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
159 warningLayout->addWidget(text);
160 warningLayout->addWidget(buttons);
161 warning_->setLayout(warningLayout);
169 readyText = tr("Daemon was successfully started.");
170 failText = tr("Unable to start daemon.");
171 buttonText = tr("Stop daemon");
172 ret = Daemon::start();
177 QMessageBox::critical(this, tr("Error"), failText);
181 QMaemo5InformationBox::information(this, readyText, 800);
182 toggleButton_->setText(buttonText);
183 toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
184 running_ = !running_;
189 void MainWindow::openSearch(QString const& str)
191 Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
192 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
194 Q_ASSERT(config != 0);
196 bool readyToSearch = config->readyToSearch();
201 QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
207 searchDialog_ = new SearchDialog(this);
208 connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
209 this, SIGNAL(search(SearchDialog::SearchDetails&)));
213 connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
219 searchDialog_->setSearchString(str);
222 if(logWindow_ && logWindow_->isVisible())
227 searchDialog_->show();
230 void MainWindow::openLog()
234 logWindow_ = new LogWindow(this);
235 connect(logWindow_, SIGNAL(logItemSelected(Source::Result const&)), this, SIGNAL(logItemSelected(Source::Result const&)));
236 connect(logWindow_, SIGNAL(openSearch(QString const&)), this, SLOT(openSearch(QString const&)));
242 QToolButton* MainWindow::createButton(QString const& text)
244 QToolButton* button = new QToolButton();
245 button->setText(text);
246 button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
250 void MainWindow::showAbout()
254 aboutDialog_ = new AboutDialog(this);
257 aboutDialog_->show();