e9da90a8d5d625ced028b89ec3dadab9f1f1f2ea
[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)
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     Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
60
61     if(!source_ || id != sourceId_)
62     {
63         sourceId_ = id;
64
65         if(source_)
66         {
67             delete source_;
68             source_ = 0;
69         }
70
71         source_ = Source::getSource(sourceId_);
72         Q_ASSERT(source_ != 0);
73         source_->setTimeout(REQUEST_TIMEOUT);
74
75         connect(source_, SIGNAL(resultAvailable(Source::Result const&,
76                                                Source::SearchDetails const&)),
77                                                this, SLOT(resultAvailable(Source::Result const&,
78                                                                           Source::SearchDetails const&)));
79
80         connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
81                                                Source::SearchDetails const&, bool)),
82                                                this, SLOT(requestFinished(QVector <Source::Result> const&,
83                                                                           Source::SearchDetails const&, bool)));
84     }
85
86     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
87
88     Q_ASSERT(config != 0);
89
90     config->apply(source_);
91     delete config;
92
93     show();
94     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
95
96     if(!connectionManager_)
97     {
98         connectionManager_ = new ConnectionManager();
99     }
100
101     connectionManager_->connect();
102
103     source_->abort();
104     source_->search(Source::SearchDetails(details.name, details.location, details.type));
105
106 }
107
108 void ResultWindow::resultAvailable(Source::Result const& result,
109                                    Source::SearchDetails const& details)
110 {
111     Q_UNUSED(details);
112
113     if(!result.number.isEmpty())
114     {
115         Cache::instance().addItem(result);
116     }
117
118     QString row = result.name;
119
120     if(!result.street.isEmpty())
121     {
122         row += ", " + result.street;
123     }
124
125     if(!result.city.isEmpty())
126     {
127         row += ", " + result.city;
128     }
129
130     QListWidgetItem* item = new QListWidgetItem(row, list_);
131     QMap <QString, QVariant> data;
132     data["name"] = QVariant(result.name);
133     data["street"] = QVariant(result.street);
134     data["city"] = QVariant(result.city);
135     data["number"] = QVariant(result.number);
136     data["country"] = QVariant(result.country);
137
138     item->setData(Qt::UserRole, QVariant(data));
139
140     list_->addItem(item);
141 }
142
143 void ResultWindow::requestFinished(QVector <Source::Result> const& results,
144                                    Source::SearchDetails const& details,
145                                    bool error)
146 {
147     Q_UNUSED(details);
148
149     if(error)
150     {
151         QString errorString;
152         Source::Error error = source_->error();
153
154         switch(error)
155         {
156         case Source::CONNECTION_FAILURE:
157             errorString = tr("Connection to server failed");
158             break;
159         case Source::INVALID_LOGIN:
160             errorString = tr("Invalid login details");
161             break;
162         case Source::TIMEOUT:
163             errorString = tr("Request timed out");
164             break;
165         default:
166             errorString = tr("Searching failed:") + " " + source_->errorString();
167             break;
168         }
169
170         QMessageBox::critical(this, tr("Error"), errorString);
171     }
172
173     if(results.size() == 0)
174     {
175         QLabel* info = new QLabel(tr("No results found"));
176         info->setAlignment(Qt::AlignCenter);
177         setCentralWidget(info);
178         list_ = 0;
179     }
180
181     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
182
183 }
184
185 void ResultWindow::itemClicked(QListWidgetItem* item)
186 {
187     QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
188     Source::Result details;
189     details.name = data["name"].toString();
190     details.street = data["street"].toString();
191     details.city = data["city"].toString();
192     details.number = data["number"].toString();
193     details.country = data["country"].toString();
194
195     emit itemSelected(details);
196 }
197
198 void ResultWindow::setVisible(bool visible)
199 {
200     QMainWindow::setVisible(visible);
201
202     if(!visible && source_)
203     {
204         source_->abort();
205
206
207     }
208 }
209
210
211