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