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