7c556c61e423d1a752bfe2b0b57da6c19dbf6ea9
[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         eniro_->setTimeout(REQUEST_TIMEOUT);
59
60         connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
61                                                Eniro::SearchDetails const&)),
62                                                this, SLOT(resultAvailable(Eniro::Result const&,
63                                                                           Eniro::SearchDetails const&)));
64
65         connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
66                                                Eniro::SearchDetails const&, bool)),
67                                                this, SLOT(requestFinished(QVector <Eniro::Result> const&,
68                                                                           Eniro::SearchDetails const&, bool)));
69     }
70
71     QString username = Settings::instance()->get("eniro_username");
72     QString password = Settings::instance()->get("eniro_password");
73
74     if(!username.isEmpty() && !password.isEmpty())
75     {
76         eniro_->login(username, password);
77     }
78
79     eniro_->setSite(selectedSite);
80
81     DB::disconnect();
82
83     Eniro::SearchType type;
84
85     switch(details.type)
86     {
87     case 0:
88         type = Eniro::PERSONS;
89         break;
90     case 1:
91         type = Eniro::YELLOW_PAGES;
92         break;
93     default:
94         return;
95     }
96
97     show();
98     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
99
100     eniro_->abort();
101     eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
102
103 }
104
105 void ResultWindow::resultAvailable(Eniro::Result const& result,
106                                    Eniro::SearchDetails const& details)
107 {
108     Q_UNUSED(details);
109
110     if(!result.number.isEmpty())
111     {
112         Cache::instance().addItem(result);
113     }
114
115     QString row = result.name;
116
117     if(!result.street.isEmpty())
118     {
119         row += ", " + result.street;
120     }
121
122     if(!result.city.isEmpty())
123     {
124         row += ", " + result.city;
125     }
126
127     QListWidgetItem* item = new QListWidgetItem(row, list_);
128     QMap <QString, QVariant> data;
129     data["name"] = QVariant(result.name);
130     data["street"] = QVariant(result.street);
131     data["city"] = QVariant(result.city);
132     data["number"] = QVariant(result.number);
133
134     item->setData(Qt::UserRole, QVariant(data));
135
136     list_->addItem(item);
137 }
138
139 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
140                                    Eniro::SearchDetails const& details,
141                                    bool error)
142 {
143     Q_UNUSED(details);
144
145     if(error)
146     {
147         QString errorString = eniro_->errorString();
148
149         if(errorString.isEmpty())
150         {
151             errorString = tr("Unknown error");
152         }
153
154         QMessageBox::critical(this, tr("Error"), errorString);
155     }
156
157     if(results.size() == 0)
158     {
159         QLabel* info = new QLabel(tr("No results found"));
160         info->setAlignment(Qt::AlignCenter);
161         setCentralWidget(info);
162         list_ = 0;
163     }
164
165     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
166
167 }
168
169 void ResultWindow::itemClicked(QListWidgetItem* item)
170 {
171     QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
172     Eniro::Result details;
173     details.name = data["name"].toString();
174     details.street = data["street"].toString();
175     details.city = data["city"].toString();
176     details.number = data["number"].toString();
177
178     emit itemSelected(details);
179 }
180
181