11e6fc1580a3d8935ed10e1fa646bae6f78de27b
[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 #include "cache.h"
29
30 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
31 eniro_(0), list_(0)
32 {
33     setAttribute(Qt::WA_Maemo5StackedWindow);
34     setWindowTitle(tr("Search results"));
35 }
36
37 void ResultWindow::search(SearchDialog::SearchDetails& details)
38 {
39     if(!list_)
40     {
41         list_ = new QListWidget(this);
42         setCentralWidget(list_);
43         connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
44                 SLOT(itemClicked(QListWidgetItem*)));
45     }
46     else
47     {
48         list_->clear();
49     }
50
51     DB::connect();
52
53     Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
54
55     if(!eniro_)
56     {
57         eniro_ = new Eniro(selectedSite, this);
58
59         connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
60                                                Eniro::SearchDetails const&)),
61                                                this, SLOT(resultAvailable(Eniro::Result const&,
62                                                                           Eniro::SearchDetails const&)));
63
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)));
68     }
69
70     QString username = Settings::instance()->get("eniro_username");
71     QString password = Settings::instance()->get("eniro_password");
72
73     if(!username.isEmpty() && !password.isEmpty())
74     {
75         eniro_->login(username, password);
76     }
77
78     eniro_->setSite(selectedSite);
79
80     DB::disconnect();
81
82     Eniro::SearchType type;
83
84     switch(details.type)
85     {
86     case 0:
87         type = Eniro::PERSONS;
88         break;
89     case 1:
90         type = Eniro::YELLOW_PAGES;
91         break;
92     default:
93         return;
94     }
95
96     show();
97     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
98
99     eniro_->abort();
100     eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
101
102 }
103
104 void ResultWindow::resultAvailable(Eniro::Result const& result,
105                                    Eniro::SearchDetails const& details)
106 {
107     Q_UNUSED(details);
108
109     if(!result.number.isEmpty())
110     {
111         Cache::instance().addItem(result);
112     }
113
114     QString row = result.name;
115
116     if(!result.street.isEmpty())
117     {
118         row += ", " + result.street;
119     }
120
121     if(!result.city.isEmpty())
122     {
123         row += ", " + result.city;
124     }
125
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);
132
133     item->setData(Qt::UserRole, QVariant(data));
134
135     list_->addItem(item);
136 }
137
138 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
139                                    Eniro::SearchDetails const& details,
140                                    bool error)
141 {
142     Q_UNUSED(details);
143
144     if(error)
145     {
146         QMessageBox::critical(this, tr("Error"), eniro_->errorString());
147     }
148
149     if(results.size() == 0)
150     {
151         QLabel* info = new QLabel(tr("No results found"));
152         info->setAlignment(Qt::AlignCenter);
153         setCentralWidget(info);
154         list_ = 0;
155     }
156
157     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
158
159 }
160
161 void ResultWindow::itemClicked(QListWidgetItem* item)
162 {
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();
169
170     emit itemSelected(details);
171 }
172
173