7facb52c3ae3215245cff6ff4c61d74489d9fd6e
[qtmads] / src / qtmadsservice.h
1 /*
2  * Copyright (c) 2009 Eetu Lehmusvuo.
3  *
4  * This file is part of QtMAds.
5  *
6  * QtMAds is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * QtMAds is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with QtMAds.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #ifndef QTMADSSERVICE_H_
22 #define QTMADSSERVICE_H_
23
24 #include "qtmadsadwidget.h"
25
26 #include <QObject>
27 #include <QHash>
28 #include <QVariant>
29 class QHttp;
30
31 class QFile;
32
33 static QString CONF_PATH = "plugins/confs/";
34 static QString CONF_FILE_POSTFIX = "_adconfigure.xml";
35
36 static QString TAG_SERVICE = "service";
37 static QString TAG_CAMPAIGN = "campaign";
38 static QString TAG_ACCOUNTID = "accountid";
39 static QString TAG_PASSWORD = "password";
40
41 static QString TAG_USERID = "userid";
42 static QString TAG_LANGUAGE = "language";
43 static QString TAG_TRANSACTIONID = "transactionid";
44
45 static QString TAG_ADTYPE = "adtype";
46 static QString TAG_TEXT = "text";
47 static QString TAG_CONTENT_URL = "content_url";
48 static QString TAG_URL = "url";
49 static QString TAG_HTML = "html";
50 static QString TAG_PREFERRED_SIZE = "preferredsize";
51
52 static QString TAG_KEY = "key";
53 static QString TAG_VALUE = "value";
54
55 class QtmadsService : public QObject
56 {
57     Q_OBJECT
58
59 public:
60         QtmadsService();
61         virtual ~QtmadsService();
62
63     void getAd(QHash<QString,QVariant> &adParameters);
64     void getRemoteImage(QString urlString);
65     bool isInitialized();
66     bool initializeService(QString service, quint32 campaingId = 0, AdType defaultAdType = anyAd);
67     void setDefaultAdType(AdType type);
68     AdType defaultAdType();
69
70     virtual AdType getAdTypeFromString(QString adType);
71     virtual QString getStringFromAdType(AdType adType);
72
73 private:
74     virtual bool parseUrlParameters(const QHash<QString, QVariant> &dynamicParameters, QList<QPair<QString,QString> > &parsedParameters) = 0;
75     virtual bool parseReceivedAd(const QByteArray &response, QHash<QString, QVariant> &parsedAd) = 0;
76     virtual bool parseXmlConfFile(QFile &file);
77
78 public slots:
79         void adRequestFinished(int, bool);
80     void imageRequestFinished(int, bool);
81
82 signals:
83         void adRequestReady(QHash<QString,QVariant> &ad);
84         void adRequestFailed();
85     void imageRequestReady(const QImage *image);
86     void imageRequestFailed();
87
88 protected:
89         QString serviceName;
90         quint32 adGroupId;
91         QString serviceUrl;
92         QString accountId;
93         QString password;
94
95         QString language;
96         QString transactionId;
97
98         AdType defAdType;
99
100         QSize preferredSize;
101
102         QHash<QString,QString> parameterTags;
103
104     QHttp *http;
105
106     qint32 adTrasactionId;
107     qint32 imageTrasactionId;
108
109     bool initialized;
110 };
111
112 #endif /* QTMADSSERVICE_H_ */