Added some missing Finnish translations to settings dialog. Added missing build deped...
[jenirok] / src / common / eniro.h
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 #ifndef ENIRO_H
20 #define ENIRO_H
21
22 #include <QtCore/QObject>
23 #include <QtCore/QString>
24 #include <QtCore/QVector>
25 #include <QtCore/QSet>
26 #include <QtCore/QRegExp>
27 #include <QtCore/QUrl>
28 #include <QtNetwork/QHttp>
29
30 class Eniro: public QObject
31 {
32     Q_OBJECT
33
34 public:
35
36     enum Site {FI, SE, DK};
37     static const int SITE_COUNT = 3;
38
39     enum SearchType {YELLOW_PAGES, PERSONS};
40
41     enum Error {NO_ERROR, CONNECTION_FAILURE, INVALID_LOGIN};
42
43     struct Result
44     {
45         QString name;
46         QString street;
47         QString city;
48         QString number;
49     };
50
51     struct SearchDetails
52     {
53         QString query;
54         QString location;
55         SearchType type;
56         SearchDetails(QString const& query = "",
57                       QString const& location = "",
58                       SearchType type = PERSONS);
59     };
60
61     struct SiteDetails
62     {
63         QString name;
64         QString id;
65     };
66
67     Eniro(Site site, QObject *parent = 0);
68
69     ~Eniro();
70
71     void login(QString const& username, QString const& password);
72     void logout();
73     void testLogin();
74     void setSite(Site);
75     void setMaxResults(unsigned int value);
76     void setFindNumber(bool value);
77     bool search(SearchDetails const& details);
78     void abort();
79     Error error() const;
80     const QString& errorString() const;
81     static QMap<Site, SiteDetails> getSites();
82     static Site stringToSite(QString const& str);
83
84 signals:
85     void resultAvailable(Eniro::Result const& result, Eniro::SearchDetails const& details);
86     void requestFinished(QVector <Eniro::Result> const& results, Eniro::SearchDetails const& details, bool error);
87     void loginStatus(bool success);
88
89 private slots:
90     void httpReady(int id, bool error);
91
92 private:
93
94     Q_DISABLE_COPY(Eniro);
95
96     struct NumberData
97     {
98         int searchId;
99         int index;
100     };
101
102     struct SearchData
103     {
104         SearchDetails details;
105         QVector <Result> results;
106         unsigned int foundNumbers;
107         unsigned int numbersTotal;
108     };
109
110     QUrl createUrl(QString const& query, QString const& location);
111     void loadResults(int id, QString const& data);
112     void loadNumber(int id, QString const& data);
113     void getNumberForResult(int id, int index, SearchDetails const& details);
114     void emitRequestFinished(int key, SearchData* data, bool error);
115     QString ucFirst(QString& string);
116     QString& cleanUpNumber(QString& number);
117     QString& stripTags(QString& string);
118
119     QHttp http_;
120     Site site_;
121     QString username_;
122     QString password_;
123     bool loggedIn_;
124     Error error_;
125     QString errorString_;
126     unsigned int maxResults_;
127     bool findNumber_;
128     typedef QMap <int, SearchData*> searchMap;
129     typedef QMap <int, NumberData*> numberMap;
130
131     searchMap pendingSearches_;
132     numberMap pendingNumberRequests_;
133     QSet <int> pendingLoginRequests_;
134
135     static QRegExp numberCleaner_;
136     static QRegExp tagStripper_;
137
138 };
139
140 #endif // ENIRO_H
141