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