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 sourceId_(Source::SOURCE_NONE), source_(0), list_(0), connectionManager_(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*)));
59 Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
61 if(!source_ || id != sourceId_)
71 source_ = Source::getSource(sourceId_);
72 Q_ASSERT(source_ != 0);
73 source_->setTimeout(REQUEST_TIMEOUT);
75 connect(source_, SIGNAL(resultAvailable(Source::Result const&,
76 Source::SearchDetails const&)),
77 this, SLOT(resultAvailable(Source::Result const&,
78 Source::SearchDetails const&)));
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)));
86 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
88 Q_ASSERT(config != 0);
90 config->apply(source_);
93 Source::SearchType type;
98 type = Source::PERSONS;
101 type = Source::YELLOW_PAGES;
104 qDebug() << "Unknown search type: " << details.type;
109 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
111 if(!connectionManager_)
113 connectionManager_ = new ConnectionManager();
116 connectionManager_->connect();
119 source_->search(Source::SearchDetails(details.name, details.location, type));
123 void ResultWindow::resultAvailable(Source::Result const& result,
124 Source::SearchDetails const& details)
128 if(!result.number.isEmpty())
130 Cache::instance().addItem(result);
133 QString row = result.name;
135 if(!result.street.isEmpty())
137 row += ", " + result.street;
140 if(!result.city.isEmpty())
142 row += ", " + result.city;
145 QListWidgetItem* item = new QListWidgetItem(row, list_);
146 QMap <QString, QVariant> data;
147 data["name"] = QVariant(result.name);
148 data["street"] = QVariant(result.street);
149 data["city"] = QVariant(result.city);
150 data["number"] = QVariant(result.number);
152 item->setData(Qt::UserRole, QVariant(data));
154 list_->addItem(item);
157 void ResultWindow::requestFinished(QVector <Source::Result> const& results,
158 Source::SearchDetails const& details,
166 Source::Error error = source_->error();
170 case Source::CONNECTION_FAILURE:
171 errorString = tr("Connection to server failed");
173 case Source::INVALID_LOGIN:
174 errorString = tr("Invalid login details");
176 case Source::TIMEOUT:
177 errorString = tr("Request timed out");
180 errorString = tr("Searching failed:") + " " + source_->errorString();
184 QMessageBox::critical(this, tr("Error"), errorString);
187 if(results.size() == 0)
189 QLabel* info = new QLabel(tr("No results found"));
190 info->setAlignment(Qt::AlignCenter);
191 setCentralWidget(info);
195 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
199 void ResultWindow::itemClicked(QListWidgetItem* item)
201 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
202 Source::Result details;
203 details.name = data["name"].toString();
204 details.street = data["street"].toString();
205 details.city = data["city"].toString();
206 details.number = data["number"].toString();
208 emit itemSelected(details);
211 void ResultWindow::setVisible(bool visible)
213 QMainWindow::setVisible(visible);
215 if(!visible && source_)