Danish Eniro search fixed.
[jenirok] / src / common / source.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 SOURCE_H
20 #define SOURCE_H
21
22 #include <QtCore/QObject>
23 #include <QtCore/QString>
24 #include <QtCore/QTimerEvent>
25 #include <QtCore/QUrl>
26 #include <QtCore/QByteArray>
27 #include <QtNetwork/QHttp>
28
29 class Source : public QObject
30 {
31
32     Q_OBJECT
33
34 public:
35
36     struct Result
37     {
38         QString name;
39         QString street;
40         QString city;
41         QString number;
42         QString country;
43     };
44
45     enum SearchType {YELLOW_PAGES, PERSONS, BOTH};
46
47     struct SearchDetails
48     {
49         QString query;
50         QString location;
51         SearchType type;
52         SearchDetails(QString const& query = "",
53                       QString const& location = "",
54                       SearchType type = PERSONS);
55     };
56
57
58     enum Error {NO_ERROR, CONNECTION_FAILURE, INVALID_LOGIN, TIMEOUT};
59     enum SourceId {ENIRO, MOBIL1881, DASTELEFONBUCH};
60     static int const SOURCE_COUNT = 3;
61
62     struct SourceDetails
63     {
64         SourceId type;
65         QString name;
66         QString id;
67     };
68
69     static unsigned int const DEFAULT_MAX_RESULTS = 30;
70
71     static Source* getSource(SourceId id, QObject* parent = 0);
72     static void getSources(QList<SourceDetails>& list);
73     static SourceId stringToId(QString const& str);
74     Source(QObject* parent = 0);
75     virtual ~Source();
76     static Source* getSource();
77     virtual void abort();
78     virtual void search(SearchDetails const& details) = 0;
79     virtual void getSearchTypes(QList<SearchType>& types) const;
80     void setMaxResults(unsigned int results);
81     unsigned int getMaxResults() const;
82     void setTimeout(unsigned int ms);
83     unsigned int getTimeout() const;
84     void resetTimeout();
85     void setFindNumber(bool value);
86     bool getFindNumber() const;
87     Error error() const;
88     const QString& errorString() const;
89
90 signals:
91     void resultAvailable(Source::Result const& result, Source::SearchDetails const& details);
92     void requestFinished(QVector <Source::Result> const& results, Source::SearchDetails const& details, bool error);
93
94 protected:
95     void setError(Error error, QString const& errorString = "");
96     virtual void timerEvent(QTimerEvent *te);
97     static QString ucFirst(QString& string);
98     static QString& cleanUpNumber(QString& number);
99     static QString& stripTags(QString& string);
100     static QString& htmlEntityDecode(QString& string);
101     static bool isPhoneNumber(QString const& string);
102     void fixUrl(QUrl& url);
103     QHttp http_;
104
105 private slots:
106     void httpReady(int id, bool error);
107
108 private:
109     Q_DISABLE_COPY(Source);
110     virtual void handleHttpData(int id, QByteArray const& data) = 0;
111     virtual void handleHttpError(int id) = 0;
112     unsigned int maxResults_;
113     unsigned int timeout_;
114     int timerId_;
115     bool findNumber_;
116     Error error_;
117     QString errorString_;
118     QString username_;
119     QString password_;
120     bool loggedIn_;
121
122     static QRegExp numberCleaner_;
123     static QRegExp tagStripper_;
124
125 };
126
127 #endif