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