Added missing Finnish translations for several error messages.
[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), connectionManager_(0)
32 {
33     setAttribute(Qt::WA_Maemo5StackedWindow);
34     setWindowTitle(tr("Search results"));
35 }
36
37 ResultWindow::~ResultWindow()
38 {
39     delete connectionManager_;
40     connectionManager_ = 0;
41 }
42
43 void ResultWindow::search(SearchDialog::SearchDetails& details)
44 {
45     if(!list_)
46     {
47         list_ = new QListWidget(this);
48         setCentralWidget(list_);
49         connect(list_, SIGNAL(itemClicked(QListWidgetItem*)), this,
50                 SLOT(itemClicked(QListWidgetItem*)));
51     }
52     else
53     {
54         list_->clear();
55     }
56
57     DB::connect();
58
59     Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site"));
60
61     if(!eniro_)
62     {
63         eniro_ = new Eniro(selectedSite, this);
64         eniro_->setTimeout(REQUEST_TIMEOUT);
65
66         connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&,
67                                                Eniro::SearchDetails const&)),
68                                                this, SLOT(resultAvailable(Eniro::Result const&,
69                                                                           Eniro::SearchDetails const&)));
70
71         connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
72                                                Eniro::SearchDetails const&, bool)),
73                                                this, SLOT(requestFinished(QVector <Eniro::Result> const&,
74                                                                           Eniro::SearchDetails const&, bool)));
75     }
76
77     QString username = Settings::instance()->get("eniro_username");
78     QString password = Settings::instance()->get("eniro_password");
79
80     if(!username.isEmpty() && !password.isEmpty())
81     {
82         eniro_->login(username, password);
83     }
84
85     eniro_->setSite(selectedSite);
86
87     DB::disconnect();
88
89     Eniro::SearchType type;
90
91     switch(details.type)
92     {
93     case 0:
94         type = Eniro::PERSONS;
95         break;
96     case 1:
97         type = Eniro::YELLOW_PAGES;
98         break;
99     default:
100         return;
101     }
102
103     show();
104     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
105
106     if(!connectionManager_)
107     {
108         connectionManager_ = new ConnectionManager();
109     }
110
111     connectionManager_->connect();
112
113     eniro_->abort();
114     eniro_->search(Eniro::SearchDetails(details.name, details.location, type));
115
116 }
117
118 void ResultWindow::resultAvailable(Eniro::Result const& result,
119                                    Eniro::SearchDetails const& details)
120 {
121     Q_UNUSED(details);
122
123     if(!result.number.isEmpty())
124     {
125         Cache::instance().addItem(result);
126     }
127
128     QString row = result.name;
129
130     if(!result.street.isEmpty())
131     {
132         row += ", " + result.street;
133     }
134
135     if(!result.city.isEmpty())
136     {
137         row += ", " + result.city;
138     }
139
140     QListWidgetItem* item = new QListWidgetItem(row, list_);
141     QMap <QString, QVariant> data;
142     data["name"] = QVariant(result.name);
143     data["street"] = QVariant(result.street);
144     data["city"] = QVariant(result.city);
145     data["number"] = QVariant(result.number);
146
147     item->setData(Qt::UserRole, QVariant(data));
148
149     list_->addItem(item);
150 }
151
152 void ResultWindow::requestFinished(QVector <Eniro::Result> const& results,
153                                    Eniro::SearchDetails const& details,
154                                    bool error)
155 {
156     Q_UNUSED(details);
157
158     if(error)
159     {
160         QString errorString;
161         Eniro::Error error = eniro_->error();
162
163         switch(error)
164         {
165         case Eniro::CONNECTION_FAILURE:
166             errorString = tr("Connection to server failed");
167             break;
168         case Eniro::INVALID_LOGIN:
169             errorString = tr("Invalid login details");
170             break;
171         case Eniro::TIMEOUT:
172             errorString = tr("Request timed out");
173             break;
174         default:
175             errorString = tr("Searching failed:") + " " + eniro_->errorString();
176             break;
177         }
178
179         QMessageBox::critical(this, tr("Error"), errorString);
180     }
181
182     if(results.size() == 0)
183     {
184         QLabel* info = new QLabel(tr("No results found"));
185         info->setAlignment(Qt::AlignCenter);
186         setCentralWidget(info);
187         list_ = 0;
188     }
189
190     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
191
192 }
193
194 void ResultWindow::itemClicked(QListWidgetItem* item)
195 {
196     QMap <QString, QVariant> data = item->data(Qt::UserRole).toMap();
197     Eniro::Result details;
198     details.name = data["name"].toString();
199     details.street = data["street"].toString();
200     details.city = data["city"].toString();
201     details.number = data["number"].toString();
202
203     emit itemSelected(details);
204 }
205
206