2 * This file is part of Jenirok.
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.
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.
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/>.
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"
30 #include "sourcecoreconfig.h"
32 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
33 source_(0), list_(0), connectionManager_(0), timer_(0), searching_(false), retries_(0)
35 setAttribute(Qt::WA_Maemo5StackedWindow);
36 setWindowTitle(tr("Search results"));
39 ResultWindow::~ResultWindow()
41 delete connectionManager_;
42 connectionManager_ = 0;
45 void ResultWindow::search(SearchDialog::SearchDetails& details)
49 list_ = new QListWidget(this);
50 setCentralWidget(list_);
51 connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
52 SLOT(itemClicked(QListWidgetItem*)));
60 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
62 if(!connectionManager_)
64 connectionManager_ = new ConnectionManager();
67 connectionManager_->connect();
69 Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
71 if(!source_ || id != sourceId_)
81 source_ = Source::getSource(sourceId_);
82 Q_ASSERT(source_ != 0);
83 source_->setTimeout(REQUEST_TIMEOUT);
85 connect(source_, SIGNAL(resultAvailable(Source::Result const&,
86 Source::SearchDetails const&)),
87 this, SLOT(resultAvailable(Source::Result const&,
88 Source::SearchDetails const&)));
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)));
96 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
98 Q_ASSERT(config != 0);
100 config->apply(source_);
106 timer_ = startTimer(SEARCH_INTERVAL);
111 QApplication::processEvents(QEventLoop::WaitForMoreEvents);
117 currentSearch_ = Source::SearchDetails(details.name, details.location, details.type);
118 source_->search(currentSearch_);
122 void ResultWindow::resultAvailable(Source::Result const& result,
123 Source::SearchDetails const& details)
132 if(!result.number.isEmpty())
134 Cache::instance().addItem(result);
137 QString row = result.name;
139 if(!result.street.isEmpty())
141 row += ", " + result.street;
144 if(!result.city.isEmpty())
146 row += ", " + result.city;
149 QListWidgetItem* item = new QListWidgetItem(row, list_);
150 QMap <QString, QVariant> data;
151 data["name"] = QVariant(result.name);
152 data["street"] = QVariant(result.street);
153 data["city"] = QVariant(result.city);
154 data["number"] = QVariant(result.number);
155 data["country"] = QVariant(result.country);
157 item->setData(Qt::UserRole, QVariant(data));
159 list_->addItem(item);
162 void ResultWindow::requestFinished(QVector <Source::Result> const& results,
163 Source::SearchDetails const& details,
170 if(retries_ < RETRIES)
172 qDebug() << "Searching failed, retrying...";
175 source_->search(currentSearch_);
180 Source::Error error = source_->error();
184 case Source::CONNECTION_FAILURE:
185 errorString = tr("Connection to server failed");
187 case Source::INVALID_LOGIN:
188 errorString = tr("Invalid login details");
190 case Source::TIMEOUT:
191 errorString = tr("Request timed out");
194 errorString = tr("Searching failed:") + " " + source_->errorString();
198 QMessageBox::critical(this, tr("Error"), errorString);
201 if(results.size() == 0)
203 QLabel* info = new QLabel(tr("No results found"));
204 info->setAlignment(Qt::AlignCenter);
205 setCentralWidget(info);
209 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
215 void ResultWindow::itemClicked(QListWidgetItem* item)
217 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
218 Source::Result details;
219 details.name = data["name"].toString();
220 details.street = data["street"].toString();
221 details.city = data["city"].toString();
222 details.number = data["number"].toString();
223 details.country = data["country"].toString();
225 emit itemSelected(details);
228 void ResultWindow::setVisible(bool visible)
230 if(!visible && source_)
235 QMainWindow::setVisible(visible);
238 void ResultWindow::timerEvent(QTimerEvent* event)