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/>.
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 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
33 setAttribute(Qt::WA_Maemo5StackedWindow);
34 setWindowTitle(tr("Search results"));
37 void ResultWindow::search(SearchDialog::SearchDetails& details)
41 list_ = new QListWidget(this);
42 setCentralWidget(list_);
43 connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
44 SLOT(itemClicked(QListWidgetItem*)));
53 Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
57 eniro_ = new Eniro(selectedSite, this);
58 eniro_->setTimeout(REQUEST_TIMEOUT);
60 connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
61 Eniro::SearchDetails const&)),
62 this, SLOT(resultAvailable(Eniro::Result const&,
63 Eniro::SearchDetails const&)));
65 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
66 Eniro::SearchDetails const&, bool)),
67 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
68 Eniro::SearchDetails const&, bool)));
71 QString username = Settings::instance()->get("eniro_username");
72 QString password = Settings::instance()->get("eniro_password");
74 if(!username.isEmpty() && !password.isEmpty())
76 eniro_->login(username, password);
79 eniro_->setSite(selectedSite);
83 Eniro::SearchType type;
88 type = Eniro::PERSONS;
91 type = Eniro::YELLOW_PAGES;
98 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
101 eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
105 void ResultWindow::resultAvailable(Eniro::Result const& result,
106 Eniro::SearchDetails const& details)
110 if(!result.number.isEmpty())
112 Cache::instance().addItem(result);
115 QString row = result.name;
117 if(!result.street.isEmpty())
119 row += ", " + result.street;
122 if(!result.city.isEmpty())
124 row += ", " + result.city;
127 QListWidgetItem* item = new QListWidgetItem(row, list_);
128 QMap <QString, QVariant> data;
129 data["name"] = QVariant(result.name);
130 data["street"] = QVariant(result.street);
131 data["city"] = QVariant(result.city);
132 data["number"] = QVariant(result.number);
134 item->setData(Qt::UserRole, QVariant(data));
136 list_->addItem(item);
139 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
140 Eniro::SearchDetails const& details,
147 QString errorString = eniro_->errorString();
149 if(errorString.isEmpty())
151 errorString = tr("Unknown error");
154 QMessageBox::critical(this, tr("Error"), errorString);
157 if(results.size() == 0)
159 QLabel* info = new QLabel(tr("No results found"));
160 info->setAlignment(Qt::AlignCenter);
161 setCentralWidget(info);
165 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
169 void ResultWindow::itemClicked(QListWidgetItem* item)
171 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
172 Eniro::Result details;
173 details.name = data["name"].toString();
174 details.street = data["street"].toString();
175 details.city = data["city"].toString();
176 details.number = data["number"].toString();
178 emit itemSelected(details);