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);
59 connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
60 Eniro::SearchDetails const&)),
61 this, SLOT(resultAvailable(Eniro::Result const&,
62 Eniro::SearchDetails const&)));
64 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
65 Eniro::SearchDetails const&, bool)),
66 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
67 Eniro::SearchDetails const&, bool)));
70 QString username = Settings::instance()->get("eniro_username");
71 QString password = Settings::instance()->get("eniro_password");
73 if(!username.isEmpty() && !password.isEmpty())
75 eniro_->login(username, password);
78 eniro_->setSite(selectedSite);
82 Eniro::SearchType type;
87 type = Eniro::PERSONS;
90 type = Eniro::YELLOW_PAGES;
97 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
100 eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
104 void ResultWindow::resultAvailable(Eniro::Result const& result,
105 Eniro::SearchDetails const& details)
109 if(!result.number.isEmpty())
111 Cache::instance().addItem(result);
114 QString row = result.name;
116 if(!result.street.isEmpty())
118 row += ", " + result.street;
121 if(!result.city.isEmpty())
123 row += ", " + result.city;
126 QListWidgetItem* item = new QListWidgetItem(row, list_);
127 QMap <QString, QVariant> data;
128 data["name"] = QVariant(result.name);
129 data["street"] = QVariant(result.street);
130 data["city"] = QVariant(result.city);
131 data["number"] = QVariant(result.number);
133 item->setData(Qt::UserRole, QVariant(data));
135 list_->addItem(item);
138 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
139 Eniro::SearchDetails const& details,
146 QMessageBox::critical(this, tr("Error"), eniro_->errorString());
149 if(results.size() == 0)
151 QLabel* info = new QLabel(tr("No results found"));
152 info->setAlignment(Qt::AlignCenter);
153 setCentralWidget(info);
157 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
161 void ResultWindow::itemClicked(QListWidgetItem* item)
163 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
164 Eniro::Result details;
165 details.name = data["name"].toString();
166 details.street = data["street"].toString();
167 details.city = data["city"].toString();
168 details.number = data["number"].toString();
170 emit itemSelected(details);