179eac89ca1941edc9c6bbd130a738786772f557
[jenirok] / src / gui / resultwindow.cpp
1 /*
2  * This file is part of Jenirok.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <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"
26 #include "settings.h"
27 #include "db.h"
28
29 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
30 eniro_(0), list_(0)
31 {
32     setAttribute(Qt::WA_Maemo5StackedWindow);
33     setWindowTitle(tr("Search results"));
34 }
35
36 void ResultWindow::search(SearchDialog::SearchDetails& details)
37 {
38     if(!list_)
39     {
40         list_ = new QListWidget(this);
41         setCentralWidget(list_);
42         connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
43                 SLOT(itemClicked(QListWidgetItem*)));
44     }
45     else
46     {
47         list_->clear();
48     }
49
50     DB::connect();
51
52     Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
53
54     if(!eniro_)
55     {
56         eniro_ = new Eniro(selectedSite, this);
57
58         connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
59                                                Eniro::SearchDetails const&)),
60                                                this, SLOT(resultAvailable(Eniro::Result const&,
61                                                                           Eniro::SearchDetails const&)));
62
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)));
67     }
68
69     QString username = Settings::instance()->get("eniro_username");
70     QString password = Settings::instance()->get("eniro_password");
71
72     if(!username.isEmpty() && !password.isEmpty())
73     {
74         eniro_->login(username, password);
75     }
76
77     eniro_->setSite(selectedSite);
78
79     DB::disconnect();
80
81     Eniro::SearchType type;
82
83     switch(details.type)
84     {
85     case 0:
86         type = Eniro::PERSONS;
87         break;
88     case 1:
89         type = Eniro::YELLOW_PAGES;
90         break;
91     default:
92         return;
93     }
94
95     show();
96     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
97
98     eniro_->abort();
99     eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
100
101 }
102
103 void ResultWindow::resultAvailable(Eniro::Result const& result,
104                                    Eniro::SearchDetails const& details)
105 {
106     Q_UNUSED(details);
107
108     QString row = result.name;
109
110     if(!result.street.isEmpty())
111     {
112         row += ", " + result.street;
113     }
114
115     if(!result.city.isEmpty())
116     {
117         row += ", " + result.city;
118     }
119
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);
126
127     item->setData(Qt::UserRole, QVariant(data));
128
129     list_->addItem(item);
130 }
131
132 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
133                                    Eniro::SearchDetails const& details,
134                                    bool error)
135 {
136     Q_UNUSED(details);
137
138     if(error)
139     {
140         QMessageBox::critical(this, tr("Error"), eniro_->errorString());
141     }
142
143     if(results.size() == 0)
144     {
145         QLabel* info = new QLabel(tr("No results found"));
146         info->setAlignment(Qt::AlignCenter);
147         setCentralWidget(info);
148         list_ = 0;
149     }
150
151     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
152
153 }
154
155 void ResultWindow::itemClicked(QListWidgetItem* item)
156 {
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();
163
164     emit itemSelected(details);
165 }
166
167