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"
29 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
32 setAttribute(Qt::WA_Maemo5StackedWindow);
33 setWindowTitle(tr("Search results"));
36 void ResultWindow::search(SearchDialog::SearchDetails& details)
40 list_ = new QListWidget(this);
41 setCentralWidget(list_);
42 connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
43 SLOT(itemClicked(QListWidgetItem*)));
52 Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
56 eniro_ = new Eniro(selectedSite, this);
58 connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
59 Eniro::SearchDetails const&)),
60 this, SLOT(resultAvailable(Eniro::Result const&,
61 Eniro::SearchDetails const&)));
63 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
64 Eniro::SearchDetails const&, bool)),
65 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
66 Eniro::SearchDetails const&, bool)));
69 QString username = Settings::instance()->get("eniro_username");
70 QString password = Settings::instance()->get("eniro_password");
72 if(!username.isEmpty() && !password.isEmpty())
74 eniro_->login(username, password);
77 eniro_->setSite(selectedSite);
81 Eniro::SearchType type;
86 type = Eniro::PERSONS;
89 type = Eniro::YELLOW_PAGES;
96 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
99 eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
103 void ResultWindow::resultAvailable(Eniro::Result const& result,
104 Eniro::SearchDetails const& details)
108 QString row = result.name;
110 if(!result.street.isEmpty())
112 row += ", " + result.street;
115 if(!result.city.isEmpty())
117 row += ", " + result.city;
120 QListWidgetItem* item = new QListWidgetItem(row, list_);
121 QMap <QString, QVariant> data;
122 data["name"] = QVariant(result.name);
123 data["street"] = QVariant(result.street);
124 data["city"] = QVariant(result.city);
125 data["number"] = QVariant(result.number);
127 item->setData(Qt::UserRole, QVariant(data));
129 list_->addItem(item);
132 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
133 Eniro::SearchDetails const& details,
140 QMessageBox::critical(this, tr("Error"), eniro_->errorString());
143 if(results.size() == 0)
145 QLabel* info = new QLabel(tr("No results found"));
146 info->setAlignment(Qt::AlignCenter);
147 setCentralWidget(info);
151 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
155 void ResultWindow::itemClicked(QListWidgetItem* item)
157 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
158 Eniro::Result details;
159 details.name = data["name"].toString();
160 details.street = data["street"].toString();
161 details.city = data["city"].toString();
162 details.number = data["number"].toString();
164 emit itemSelected(details);