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)
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 sourceId = Source::stringToId(Settings::instance()->get("source"));
63 source_ = Source::getSource(sourceId);
64 Q_ASSERT(source_ != 0);
65 source_->setTimeout(REQUEST_TIMEOUT);
67 connect(source_, SIGNAL(resultAvailable(Source::Result const&,
68 Source::SearchDetails const&)),
69 this, SLOT(resultAvailable(Source::Result const&,
70 Source::SearchDetails const&)));
72 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
73 Source::SearchDetails const&, bool)),
74 this, SLOT(requestFinished(QVector <Source::Result> const&,
75 Source::SearchDetails const&, bool)));
78 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
80 Q_ASSERT(config != 0);
82 config->apply(source_);
85 Source::SearchType type;
90 type = Source::PERSONS;
93 type = Source::YELLOW_PAGES;
96 qDebug() << "Unknown search type: " << details.type;
101 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
103 if(!connectionManager_)
105 connectionManager_ = new ConnectionManager();
108 connectionManager_->connect();
111 source_->search(Source::SearchDetails(details.name, details.location, type));
115 void ResultWindow::resultAvailable(Source::Result const& result,
116 Source::SearchDetails const& details)
120 if(!result.number.isEmpty())
122 Cache::instance().addItem(result);
125 QString row = result.name;
127 if(!result.street.isEmpty())
129 row += ", " + result.street;
132 if(!result.city.isEmpty())
134 row += ", " + result.city;
137 QListWidgetItem* item = new QListWidgetItem(row, list_);
138 QMap <QString, QVariant> data;
139 data["name"] = QVariant(result.name);
140 data["street"] = QVariant(result.street);
141 data["city"] = QVariant(result.city);
142 data["number"] = QVariant(result.number);
144 item->setData(Qt::UserRole, QVariant(data));
146 list_->addItem(item);
149 void ResultWindow::requestFinished(QVector <Source::Result> const& results,
150 Source::SearchDetails const& details,
158 Source::Error error = source_->error();
162 case Source::CONNECTION_FAILURE:
163 errorString = tr("Connection to server failed");
165 case Source::INVALID_LOGIN:
166 errorString = tr("Invalid login details");
168 case Source::TIMEOUT:
169 errorString = tr("Request timed out");
172 errorString = tr("Searching failed:") + " " + source_->errorString();
176 QMessageBox::critical(this, tr("Error"), errorString);
179 if(results.size() == 0)
181 QLabel* info = new QLabel(tr("No results found"));
182 info->setAlignment(Qt::AlignCenter);
183 setCentralWidget(info);
187 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
191 void ResultWindow::itemClicked(QListWidgetItem* item)
193 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
194 Source::Result details;
195 details.name = data["name"].toString();
196 details.street = data["street"].toString();
197 details.city = data["city"].toString();
198 details.number = data["number"].toString();
200 emit itemSelected(details);
203 void ResultWindow::setVisible(bool visible)
205 QMainWindow::setVisible(visible);
207 if(!visible && source_)