Added Das Telefonbuch support.
[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         if(searchDialog_)
103         {
104             connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
105         }
106     }
107
108     settingsDialog_->show();
109 }
110
111 void MainWindow::toggleDaemon()
112 {
113     QString readyText;
114     QString failText;
115     QString buttonText;
116     bool ret = false;
117
118     if(running_)
119     {
120         readyText = tr("Daemon was successfully stopped.");
121         failText = tr("Unable to stop daemon.");
122         buttonText = tr("Start daemon");
123         ret = Daemon::stop();
124     }
125     else
126     {
127         if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
128         {
129             if(!warning_)
130             {
131                 warning_ = new QDialog(this);
132                 warning_->setWindowTitle(tr("Unable to start daemon"));
133                 QHBoxLayout* warningLayout = new QHBoxLayout;
134                 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."));
135                 text->setReadOnly(true);
136                 QDialogButtonBox* buttons = new QDialogButtonBox;
137                 buttons->setOrientation(Qt::Vertical);
138                 QPushButton* settingsButton = new QPushButton(tr("Open settings"));
139                 connect(settingsButton, SIGNAL(pressed()), this, SLOT(showSettings()));
140                 QPushButton* okButton = new QPushButton(tr("Close"));
141                 connect(okButton, SIGNAL(pressed()), warning_, SLOT(hide()));
142                 buttons->addButton(settingsButton, QDialogButtonBox::YesRole);
143                 buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
144                 warningLayout->addWidget(text);
145                 warningLayout->addWidget(buttons);
146                 warning_->setLayout(warningLayout);
147             }
148
149             warning_->show();
150
151             return;
152         }
153
154         readyText = tr("Daemon was successfully started.");
155         failText = tr("Unable to start daemon.");
156         buttonText = tr("Stop daemon");
157         ret = Daemon::start();
158     }
159
160     if(!ret)
161     {
162         QMessageBox::critical(this, tr("Error"), failText);
163     }
164     else
165     {
166         QMaemo5InformationBox::information(this, readyText, 800);
167         toggleButton_->setText(buttonText);
168         toggleButton_->setIcon(QIcon(running_ ? START_ICON : CLOSE_ICON));
169         running_ = !running_;
170     }
171
172 }
173
174 void MainWindow::openSearch()
175 {
176     Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
177     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
178
179     Q_ASSERT(config != 0);
180
181     bool readyToSearch = config->readyToSearch();
182     delete config;
183
184     if(!readyToSearch)
185     {
186         QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
187         return;
188     }
189
190     if(!searchDialog_)
191     {
192         searchDialog_ = new SearchDialog(this);
193         connect(searchDialog_, SIGNAL(search(SearchDialog::SearchDetails&)),
194                 this, SLOT(handleSearch(SearchDialog::SearchDetails&)));
195
196         if(settingsDialog_)
197         {
198             connect(settingsDialog_, SIGNAL(saved()), searchDialog_, SLOT(loadSearchTypes()));
199         }
200     }
201
202     searchDialog_->show();
203 }
204
205 QToolButton* MainWindow::createButton(QString const& text)
206 {
207     QToolButton* button = new QToolButton();
208     button->setText(text);
209     button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
210     return button;
211 }
212
213 void MainWindow::handleSearch(SearchDialog::SearchDetails& details)
214 {
215     emit search(details);
216 }
217
218 void MainWindow::showAbout()
219 {
220     if(!aboutDialog_)
221     {
222         aboutDialog_ = new AboutDialog(this);
223     }
224
225     aboutDialog_->show();
226
227 }