Changed search to retry automatically couple of times before failing.
[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 #include "source.h"
36 #include "sourcecoreconfig.h"
37
38 namespace
39 {
40     const QString START_ICON = ":/icons/start.png";
41     const QString CLOSE_ICON = ":/icons/stop.png";
42 }
43
44 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
45 searchResults_(0), settingsDialog_(0), running_(false),
46 toggleButton_(0), searchDialog_(0), aboutDialog_(0), warning_(0),
47 logWindow_(0)
48 {
49     setWindowTitle(tr("Jenirok"));
50     setAttribute(Qt::WA_Maemo5StackedWindow);
51     QWidget* mainWidget = new QWidget(this);
52
53     if(Daemon::isRunning())
54     {
55         toggleButton_ = createButton(tr("Stop daemon"));
56         toggleButton_->setIcon(QIcon(CLOSE_ICON));
57         running_ = true;
58     }
59     else
60     {
61         toggleButton_ = createButton(tr("Start daemon"));
62         toggleButton_->setIcon(QIcon(START_ICON));
63         running_ = false;
64     }
65
66     QSizePolicy policy;
67
68     policy.setHorizontalPolicy(QSizePolicy::Preferred);
69
70     toggleButton_->setSizePolicy(policy);
71
72     QToolButton* searchButton = createButton(tr("Search"));
73     searchButton->setIcon(QIcon::fromTheme("general_search"));
74     searchButton->setSizePolicy(policy);
75
76     QToolButton* logButton = createButton(tr("Log"));
77     logButton->setIcon(QIcon::fromTheme("general_call"));
78     logButton->setSizePolicy(policy);
79
80     QSize size(64, 64);
81     searchButton->setIconSize(size);
82     toggleButton_->setIconSize(size);
83     logButton->setIconSize(size);
84
85     QHBoxLayout *buttonLayout = new QHBoxLayout;
86     buttonLayout->addWidget(toggleButton_, Qt::AlignHCenter);
87     buttonLayout->addWidget(searchButton, Qt::AlignHCenter);
88     buttonLayout->addWidget(logButton, Qt::AlignHCenter);
89
90     mainWidget->setLayout(buttonLayout);
91
92     connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
93     connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
94     connect(logButton, SIGNAL(pressed()), this, SLOT(openLog()));
95
96     setCentralWidget(mainWidget);
97     menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
98     menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
99 }
100
101 MainWindow::~MainWindow()
102 {
103     DB::removeDatabase();
104 }
105
106 void MainWindow::showSettings()
107 {
108     if(warning_ && warning_->isVisible())
109     {
110         warning_->hide();
111     }
112
113     if(!settingsDialog_)
114     {
115         settingsDialog_ = new SettingsDialog(this);
116
117         if(searchDialog_)
118         {
119             connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
120         }
121     }
122
123     settingsDialog_->show();
124 }
125
126 void MainWindow::toggleDaemon()
127 {
128     QString readyText;
129     QString failText;
130     QString buttonText;
131     bool ret = false;
132
133     if(running_)
134     {
135         readyText = tr("Daemon was successfully stopped.");
136         failText = tr("Unable to stop daemon.");
137         buttonText = tr("Start daemon");
138         ret = Daemon::stop();
139     }
140     else
141     {
142         if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
143         {
144             if(!warning_)
145             {
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);
162             }
163
164             warning_->show();
165
166             return;
167         }
168
169         readyText = tr("Daemon was successfully started.");
170         failText = tr("Unable to start daemon.");
171         buttonText = tr("Stop daemon");
172         ret = Daemon::start();
173     }
174
175     if(!ret)
176     {
177         QMessageBox::critical(this, tr("Error"), failText);
178     }
179     else
180     {
181         QMaemo5InformationBox::information(this, readyText, 800);
182         toggleButton_->setText(buttonText);
183         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
184         running_ = !running_;
185     }
186
187 }
188
189 void MainWindow::openSearch(QString const& str)
190 {
191     Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
192     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
193
194     Q_ASSERT(config != 0);
195
196     bool readyToSearch = config->readyToSearch();
197     delete config;
198
199     if(!readyToSearch)
200     {
201         QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
202         return;
203     }
204
205     if(!searchDialog_)
206     {
207         searchDialog_ = new SearchDialog(this);
208         connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
209                 this, SIGNAL(search(SearchDialog::SearchDetails&)));
210
211         if(settingsDialog_)
212         {
213             connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
214         }
215     }
216
217     if(!str.isEmpty())
218     {
219         searchDialog_->setSearchString(str);
220     }
221
222     if(logWindow_ && logWindow_->isVisible())
223     {
224         logWindow_->hide();
225     }
226
227     searchDialog_->show();
228 }
229
230 void MainWindow::openLog()
231 {
232     if(!logWindow_)
233     {
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&)));
237     }
238
239     logWindow_->show();
240 }
241
242 QToolButton* MainWindow::createButton(QString const& text)
243 {
244     QToolButton* button = new QToolButton();
245     button->setText(text);
246     button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
247     return button;
248 }
249
250 void MainWindow::showAbout()
251 {
252     if(!aboutDialog_)
253     {
254         aboutDialog_ = new AboutDialog(this);
255     }
256
257     aboutDialog_->show();
258
259 }