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), eniro_(0), list_(0)
31 setAttribute(Qt::WA_Maemo5StackedWindow);
32 setWindowTitle(tr("Search results"));
35 void ResultWindow::search(SearchDialog::SearchDetails& details)
39 list_ = new QListWidget(this);
40 setCentralWidget(list_);
41 connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*)));
50 Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
54 eniro_ = new Eniro(selectedSite, this);
56 connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
57 Eniro::SearchDetails const&)),
58 this, SLOT(resultAvailable(Eniro::Result const&,
59 Eniro::SearchDetails const&)));
61 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
62 Eniro::SearchDetails const&, bool)),
63 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
64 Eniro::SearchDetails const&, bool)));
67 QString username = Settings::instance()->get("eniro_username");
68 QString password = Settings::instance()->get("eniro_password");
70 if(!username.isEmpty() && !password.isEmpty())
72 eniro_->login(username, password);
75 eniro_->setSite(selectedSite);
79 Eniro::SearchType type;
84 type = Eniro::PERSONS;
87 type = Eniro::YELLOW_PAGES;
94 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
97 eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
101 void ResultWindow::resultAvailable(Eniro::Result const& result,
102 Eniro::SearchDetails const& details)
106 QString row = result.name;
108 if(!result.street.isEmpty())
110 row += ", " + result.street;
113 if(!result.city.isEmpty())
115 row += ", " + result.city;
118 QListWidgetItem* item = new QListWidgetItem(row, list_);
119 QMap <QString, QVariant> data;
120 data["name"] = QVariant(result.name);
121 data["street"] = QVariant(result.street);
122 data["city"] = QVariant(result.city);
123 data["number"] = QVariant(result.number);
125 item->setData(Qt::UserRole, QVariant(data));
127 list_->addItem(item);
130 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
131 Eniro::SearchDetails const& details,
138 QMessageBox::critical(this, tr("Error"), eniro_->errorString());
141 if(results.size() == 0)
143 QLabel* info = new QLabel(tr("No results found"));
144 info->setAlignment(Qt::AlignCenter);
145 setCentralWidget(info);
149 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
153 void ResultWindow::itemClicked(QListWidgetItem* item)
155 QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
156 Eniro::Result details;
157 details.name = data["name"].toString();
158 details.street = data["street"].toString();
159 details.city = data["city"].toString();
160 details.number = data["number"].toString();
162 emit itemSelected(details);