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