dc1509f02f731b0634aea595ca7fbf61b91c5929
[jenirok] / src / gui / resultwindow.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 <QtCore/QDebug>
20 #include <QtCore/QVariant>
21 #include <QtCore/QString>
22 #include <QtGui/QLabel>
23 #include <QtGui/QListWidgetItem>
24 #include <QtGui/QMessageBox>
25 #include "resultwindow.h"
26 #include "settings.h"
27 #include "db.h"
28 #include "cache.h"
29 #include "source.h"
30 #include "sourcecoreconfig.h"
31
32 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
33 source_(0), list_(0), connectionManager_(0), timer_(0), searching_(false)
34 {
35     setAttribute(Qt::WA_Maemo5StackedWindow);
36     setWindowTitle(tr("Search results"));
37 }
38
39 ResultWindow::~ResultWindow()
40 {
41     delete connectionManager_;
42     connectionManager_ = 0;
43 }
44
45 void ResultWindow::search(SearchDialog::SearchDetails& details)
46 {
47     if(!list_)
48     {
49         list_ = new QListWidget(this);
50         setCentralWidget(list_);
51         connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
52                 SLOT(itemClicked(QListWidgetItem*)));
53     }
54     else
55     {
56         list_->clear();
57     }
58
59     show();
60     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
61
62     if(!connectionManager_)
63     {
64         connectionManager_ = new ConnectionManager();
65     }
66
67     connectionManager_->connect();
68
69     Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
70
71     if(!source_ || id != sourceId_)
72     {
73         sourceId_ = id;
74
75         if(source_)
76         {
77             delete source_;
78             source_ = 0;
79         }
80
81         source_ = Source::getSource(sourceId_);
82         Q_ASSERT(source_ != 0);
83         source_->setTimeout(REQUEST_TIMEOUT);
84
85         connect(source_, SIGNAL(resultAvailable(Source::Result const&,
86                                                Source::SearchDetails const&)),
87                                                this, SLOT(resultAvailable(Source::Result const&,
88                                                                           Source::SearchDetails const&)));
89
90         connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
91                                                Source::SearchDetails const&, bool)),
92                                                this, SLOT(requestFinished(QVector <Source::Result> const&,
93                                                                           Source::SearchDetails const&, bool)));
94     }
95
96     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
97
98     Q_ASSERT(config != 0);
99
100     config->apply(source_);
101     delete config;
102
103     if(searching_)
104     {
105         source_->abort();
106         timer_ = startTimer(SEARCH_INTERVAL);
107     }
108
109     while(timer_)
110     {
111         QApplication::processEvents(QEventLoop::WaitForMoreEvents);
112     }
113
114     list_->clear();
115     searching_ = true;
116     source_->search(Source::SearchDetails(details.name, details.location, details.type));
117
118 }
119
120 void ResultWindow::resultAvailable(Source::Result const& result,
121                                    Source::SearchDetails const& details)
122 {
123     if(!list_)
124     {
125         return;
126     }
127
128     Q_UNUSED(details);
129
130     if(!result.number.isEmpty())
131     {
132         Cache::instance().addItem(result);
133     }
134
135     QString row = result.name;
136
137     if(!result.street.isEmpty())
138     {
139         row += ", " + result.street;
140     }
141
142     if(!result.city.isEmpty())
143     {
144         row += ", " + result.city;
145     }
146
147     QListWidgetItem* item = new QListWidgetItem(row, list_);
148     QMap <QString, QVariant> data;
149     data["name"] = QVariant(result.name);
150     data["street"] = QVariant(result.street);
151     data["city"] = QVariant(result.city);
152     data["number"] = QVariant(result.number);
153     data["country"] = QVariant(result.country);
154
155     item->setData(Qt::UserRole, QVariant(data));
156
157     list_->addItem(item);
158 }
159
160 void ResultWindow::requestFinished(QVector <Source::Result> const& results,
161                                    Source::SearchDetails const& details,
162                                    bool error)
163 {
164     Q_UNUSED(details);
165
166     if(error)
167     {
168         QString errorString;
169         Source::Error error = source_->error();
170
171         switch(error)
172         {
173         case Source::CONNECTION_FAILURE:
174             errorString = tr("Connection to server failed");
175             break;
176         case Source::INVALID_LOGIN:
177             errorString = tr("Invalid login details");
178             break;
179         case Source::TIMEOUT:
180             errorString = tr("Request timed out");
181             break;
182         default:
183             errorString = tr("Searching failed:") + " " + source_->errorString();
184             break;
185         }
186
187         QMessageBox::critical(this, tr("Error"), errorString);
188     }
189
190     if(results.size() == 0)
191     {
192         QLabel* info = new QLabel(tr("No results found"));
193         info->setAlignment(Qt::AlignCenter);
194         setCentralWidget(info);
195         list_ = 0;
196     }
197
198     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
199
200     if(timer_)
201     {
202         killTimer(timer_);
203     }
204
205     timer_ = startTimer(SEARCH_INTERVAL);
206     searching_ = false;
207
208 }
209
210 void ResultWindow::itemClicked(QListWidgetItem* item)
211 {
212     QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
213     Source::Result details;
214     details.name = data["name"].toString();
215     details.street = data["street"].toString();
216     details.city = data["city"].toString();
217     details.number = data["number"].toString();
218     details.country = data["country"].toString();
219
220     emit itemSelected(details);
221 }
222
223 void ResultWindow::setVisible(bool visible)
224 {
225     if(!visible && source_)
226     {
227         source_->abort();
228     }
229
230     QMainWindow::setVisible(visible);
231 }
232
233 void ResultWindow::timerEvent(QTimerEvent* event)
234 {
235     Q_UNUSED(event);
236
237     killTimer(timer_);
238     timer_ = 0;
239 }
240
241