Architecture changed to allow easier addition of new phone books. Norwegian phonebook...
[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 #include "source.h"
31
32 class Eniro: public Source
33 {
34     Q_OBJECT
35
36 public:
37
38     enum Site {FI, SE, DK};
39
40     struct SiteDetails
41     {
42         QString name;
43         QString id;
44     };
45
46     static const int SITE_COUNT = 3;
47
48     Eniro(QObject *parent = 0);
49     ~Eniro();
50     void login(QString const& username, QString const& password);
51     void logout();
52     void setSite(Site);
53     virtual void search(Source::SearchDetails const& details);
54     virtual void abort();
55     static QMap<Site, SiteDetails> getSites();
56     static Site stringToSite(QString const& str);
57
58 signals:
59     void loginStatus(bool success);
60
61 private:
62
63     Q_DISABLE_COPY(Eniro);
64
65     struct NumberData
66     {
67         int searchId;
68         int index;
69     };
70
71     struct SearchData
72     {
73         Source::SearchDetails details;
74         QVector <Source::Result> results;
75         unsigned int foundNumbers;
76         unsigned int numbersTotal;
77     };
78
79     virtual void handleHttpData(int id, QString const& data);
80     virtual void handleHttpError(int id);
81     QUrl createUrl(QString const& query, QString const& location);
82     void loadResults(int id, QString const& data);
83     void loadNumber(int id, QString const& data);
84     void getNumberForResult(int id, int index, SearchDetails const& details);
85     void emitRequestFinished(int key, SearchData* data, bool error);
86     void timerEvent(QTimerEvent *t);
87
88     Site site_;
89     bool loggedIn_;
90     QString username_;
91     QString password_;
92     int timerId_;
93     typedef QMap <int, SearchData*> searchMap;
94     typedef QMap <int, NumberData*> numberMap;
95     searchMap pendingSearches_;
96     numberMap pendingNumberRequests_;
97     QSet <int> pendingLoginRequests_;
98     static QRegExp numberCleaner_;
99     static QRegExp tagStripper_;
100
101 };
102
103 #endif // ENIRO_H
104