Better cache handling. Added clear cache button to settings.
[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     Eniro(Site site, QObject *parent = 0);
69
70     ~Eniro();
71
72     void login(QString const& username, QString const& password);
73     void logout();
74     void testLogin();
75     void setSite(Site);
76     void setMaxResults(unsigned int value);
77     void setFindNumber(bool value);
78     void setTimeout(unsigned int ms);
79     bool search(SearchDetails const& details);
80     void abort();
81     Error error() const;
82     const QString& errorString() const;
83     static QMap<Site, SiteDetails> getSites();
84     static Site stringToSite(QString const& str);
85
86 signals:
87     void resultAvailable(Eniro::Result const& result, Eniro::SearchDetails const& details);
88     void requestFinished(QVector <Eniro::Result> const& results, Eniro::SearchDetails const& details, bool error);
89     void loginStatus(bool success);
90
91 private slots:
92     void httpReady(int id, bool error);
93
94 private:
95
96     Q_DISABLE_COPY(Eniro);
97
98     struct NumberData
99     {
100         int searchId;
101         int index;
102     };
103
104     struct SearchData
105     {
106         SearchDetails details;
107         QVector <Result> results;
108         unsigned int foundNumbers;
109         unsigned int numbersTotal;
110     };
111
112     QUrl createUrl(QString const& query, QString const& location);
113     void loadResults(int id, QString const& data);
114     void loadNumber(int id, QString const& data);
115     void getNumberForResult(int id, int index, SearchDetails const& details);
116     void emitRequestFinished(int key, SearchData* data, bool error);
117     void resetTimeout();
118     void timerEvent(QTimerEvent *te);
119     QString ucFirst(QString& string);
120     QString& cleanUpNumber(QString& number);
121     QString& stripTags(QString& string);
122
123     QHttp http_;
124     Site site_;
125     QString username_;
126     QString password_;
127     bool loggedIn_;
128     Error error_;
129     QString errorString_;
130     unsigned int maxResults_;
131     unsigned int timeout_;
132     int timerId_;
133     bool findNumber_;
134     typedef QMap <int, SearchData*> searchMap;
135     typedef QMap <int, NumberData*> numberMap;
136
137     searchMap pendingSearches_;
138     numberMap pendingNumberRequests_;
139     QSet <int> pendingLoginRequests_;
140
141     static QRegExp numberCleaner_;
142     static QRegExp tagStripper_;
143
144 };
145
146 #endif // ENIRO_H
147