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