Fixed problems with special html characters and entities in Norwegian version.
[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 {
48     setWindowTitle(tr("Jenirok"));
49     setAttribute(Qt::WA_Maemo5StackedWindow);
50     QWidget* mainWidget = new QWidget(this);
51
52     if(Daemon::isRunning())
53     {
54         toggleButton_ = createButton(tr("Stop daemon"));
55         toggleButton_->setIcon(QIcon(CLOSE_ICON));
56         running_ = true;
57     }
58     else
59     {
60         toggleButton_ = createButton(tr("Start daemon"));
61         toggleButton_->setIcon(QIcon(START_ICON));
62         running_ = false;
63     }
64
65     QToolButton* searchButton = createButton(tr("Search"));
66     searchButton->setIcon(QIcon::fromTheme("general_search"));
67
68     QSize size(64, 64);
69     searchButton->setIconSize(size);
70     toggleButton_->setIconSize(size);
71
72     QHBoxLayout *buttonLayout = new QHBoxLayout;
73     buttonLayout->addWidget(toggleButton_, Qt::AlignLeft);
74     buttonLayout->addWidget(searchButton, Qt::AlignRight);
75
76     mainWidget->setLayout(buttonLayout);
77
78     connect(toggleButton_, SIGNAL(pressed()), this, SLOT(toggleDaemon()));
79     connect(searchButton, SIGNAL(pressed()), this, SLOT(openSearch()));
80
81     setCentralWidget(mainWidget);
82     menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
83     menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
84 }
85
86 MainWindow::~MainWindow()
87 {
88     DB::removeDatabase();
89 }
90
91 void MainWindow::showSettings()
92 {
93     if(warning_ && warning_->isVisible())
94     {
95         warning_->hide();
96     }
97
98     if(!settingsDialog_)
99     {
100         settingsDialog_ = new SettingsDialog(this);
101     }
102
103     settingsDialog_->show();
104 }
105
106 void MainWindow::toggleDaemon()
107 {
108     QString readyText;
109     QString failText;
110     QString buttonText;
111     bool ret = false;
112
113     if(running_)
114     {
115         readyText = tr("Daemon was successfully stopped.");
116         failText = tr("Unable to stop daemon.");
117         buttonText = tr("Start daemon");
118         ret = Daemon::stop();
119     }
120     else
121     {
122         if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
123         {
124             if(!warning_)
125             {
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);
142             }
143
144             warning_->show();
145
146             return;
147         }
148
149         readyText = tr("Daemon was successfully started.");
150         failText = tr("Unable to start daemon.");
151         buttonText = tr("Stop daemon");
152         ret = Daemon::start();
153     }
154
155     if(!ret)
156     {
157         QMessageBox::critical(this, tr("Error"), failText);
158     }
159     else
160     {
161         QMaemo5InformationBox::information(this, readyText);
162         toggleButton_->setText(buttonText);
163         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
164         running_ = !running_;
165     }
166
167 }
168
169 void MainWindow::openSearch()
170 {
171     Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
172     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
173
174     Q_ASSERT(config != 0);
175
176     bool readyToSearch = config->readyToSearch();
177     delete config;
178
179     if(!readyToSearch)
180     {
181         QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
182         return;
183     }
184
185     if(!searchDialog_)
186     {
187         searchDialog_ = new SearchDialog(this);
188         connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
189                 this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
190     }
191
192     searchDialog_->show();
193 }
194
195 QToolButton* MainWindow::createButton(QString const& text)
196 {
197     QToolButton* button = new QToolButton();
198     button->setText(text);
199     button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
200     return button;
201 }
202
203 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
204 {
205     emit search(details);
206 }
207
208 void MainWindow::showAbout()
209 {
210     if(!aboutDialog_)
211     {
212         aboutDialog_ = new AboutDialog(this);
213     }
214
215     aboutDialog_->show();
216
217 }