Mercurial > hg > easaier-soundaccess
comparison data/fileio/HttpClient.cpp @ 8:d32c0a79ba39
add connection setting information reading and saving
author | lbajardsilogic |
---|---|
date | Fri, 11 May 2007 15:39:08 +0000 |
parents | |
children | ca3a5912fd78 |
comparison
equal
deleted
inserted
replaced
7:a5175615d153 | 8:d32c0a79ba39 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* Sound Access | |
4 EASAIER client application. | |
5 Silogic 2007. Laure Bajard. | |
6 | |
7 This program is free software; you can redistribute it and/or | |
8 modify it under the terms of the GNU General Public License as | |
9 published by the Free Software Foundation; either version 2 of the | |
10 License, or (at your option) any later version. See the file | |
11 COPYING included with this distribution for more information. | |
12 */ | |
13 | |
14 #include "HttpClient.h" | |
15 | |
16 #include <iostream> | |
17 | |
18 | |
19 HttpClient::HttpClient(const QString& configFileName) : QHttp() | |
20 { | |
21 QFile *config = new QFile(configFileName); | |
22 | |
23 m_useProxy = false; | |
24 m_proxy = ""; | |
25 m_proxyPort = 0; | |
26 m_userProxyId = ""; | |
27 m_pwdProxyId = ""; | |
28 | |
29 m_host = ""; | |
30 m_hostPort = 0; | |
31 | |
32 if (config->exists()) | |
33 { | |
34 fromXmlString(config); | |
35 } | |
36 | |
37 if (useProxy()) | |
38 setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId); | |
39 setHost(m_host, m_hostPort); | |
40 } | |
41 | |
42 | |
43 HttpClient::~HttpClient() | |
44 {} | |
45 | |
46 void HttpClient::valuesChanged(ConnectionSettings* dialogBox) | |
47 { | |
48 setUseProxy(dialogBox->useProxy()); | |
49 if (useProxy()) | |
50 { | |
51 m_proxy = dialogBox->getProxy(); | |
52 m_proxyPort = (dialogBox->getProxyPort()).toInt(); | |
53 m_userProxyId = dialogBox->getUserProxyId(); | |
54 m_pwdProxyId = dialogBox->getPwdProxyId(); | |
55 | |
56 setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId); | |
57 } | |
58 | |
59 m_host = dialogBox->getHost(); | |
60 m_hostPort = (dialogBox->getHostPort()).toUShort(); | |
61 | |
62 setHost(m_host, m_hostPort); | |
63 } | |
64 | |
65 QString HttpClient::toXmlString(QString indent, QString extraAttributes) const | |
66 { | |
67 QString s; | |
68 | |
69 s += indent; | |
70 | |
71 s += QString("<connexionSettings>\n"); | |
72 | |
73 if (useProxy()) | |
74 { | |
75 s += indent; | |
76 s += QString("\t"); | |
77 s += QString("<proxy name=\"%1\" port=\"%2\" user=\"%3\" pwd=\"%4\" />\n") | |
78 .arg(getProxy()) | |
79 .arg(QString::number(getProxyPort())) | |
80 .arg(getUserProxyId()) | |
81 .arg(getPwdProxyId()); | |
82 } | |
83 | |
84 s += indent; | |
85 s += QString("\t"); | |
86 s += QString("<host name=\"%1\" port=\"%2\" />\n") | |
87 .arg(getHost()) | |
88 .arg(QString::number(getHostPort())); | |
89 | |
90 s += indent; | |
91 s += QString("</connexionSettings>\n"); | |
92 | |
93 return s; | |
94 } | |
95 | |
96 bool HttpClient::fromXmlString(QIODevice* file) | |
97 { | |
98 HttpClientConfigXmlHandler handler(this); | |
99 QXmlSimpleReader reader; | |
100 reader.setContentHandler(&handler); | |
101 reader.setErrorHandler(&handler); | |
102 | |
103 if (!file->open(QFile::ReadOnly | QFile::Text)) { | |
104 return false; | |
105 } | |
106 | |
107 QXmlInputSource xmlInputSource(file); | |
108 if (reader.parse(xmlInputSource)) | |
109 return true; | |
110 | |
111 return false; | |
112 } | |
113 | |
114 HttpClientConfigXmlHandler::HttpClientConfigXmlHandler(HttpClient* httpClient) : QXmlDefaultHandler() | |
115 { | |
116 m_httpClient = httpClient; | |
117 } | |
118 | |
119 bool HttpClientConfigXmlHandler::startElement(const QString &namespaceURI, const QString &localName, | |
120 const QString &qName, const QXmlAttributes &attributes) | |
121 { | |
122 QString name = qName.toLower(); | |
123 bool ok = false; | |
124 | |
125 // Valid element names: | |
126 // config | |
127 // connexionSettings | |
128 // proxy | |
129 // host | |
130 | |
131 if (name == "config") { | |
132 // nothing needed | |
133 ok = true; | |
134 | |
135 } else if (name == "connexionsettings") { | |
136 // nothing needed | |
137 ok = true; | |
138 | |
139 } else if (name == "proxy") { | |
140 | |
141 m_httpClient->setProxyInfo(attributes.value("name")); | |
142 if (attributes.value("name") != "") | |
143 m_httpClient->setUseProxy(true); | |
144 m_httpClient->setProxyPortInfo(attributes.value("port").toInt()); | |
145 m_httpClient->setUserProxyId(attributes.value("user")); | |
146 m_httpClient->setPwdProxyId(attributes.value("pwd")); | |
147 ok = true; | |
148 | |
149 } else if (name == "host") { | |
150 | |
151 m_httpClient->setHostInfo(attributes.value("name")); | |
152 m_httpClient->setHostPortInfo(attributes.value("port").toUShort()); | |
153 ok = true; | |
154 | |
155 } | |
156 | |
157 if (!ok) { | |
158 std::cerr << "WARNING: connexion config-XML: Failed to completely process element \"" | |
159 << name.toLocal8Bit().data() << "\"" << std::endl; | |
160 } | |
161 | |
162 return true; | |
163 } | |
164 | |
165 bool HttpClientConfigXmlHandler::endElement(const QString &namespaceURI, const QString &localName, | |
166 const QString &qName) | |
167 { | |
168 return true; | |
169 } | |
170 | |
171 bool HttpClientConfigXmlHandler::characters(const QString &str) | |
172 { | |
173 return true; | |
174 } | |
175 | |
176 bool HttpClientConfigXmlHandler::error(const QXmlParseException &exception) | |
177 { | |
178 QString errorString; | |
179 errorString += QString("ERROR: connexion config-XML: %1 at line %2, column %3") | |
180 .arg(exception.message()) | |
181 .arg(exception.lineNumber()) | |
182 .arg(exception.columnNumber()); | |
183 std::cerr << errorString.toLocal8Bit().data() << std::endl; | |
184 return QXmlDefaultHandler::error(exception); | |
185 } | |
186 | |
187 bool HttpClientConfigXmlHandler::fatalError(const QXmlParseException &exception) | |
188 { | |
189 QString errorString; | |
190 errorString += QString("FATAL ERROR: connexion config-XML: %1 at line %2, column %3") | |
191 .arg(exception.message()) | |
192 .arg(exception.lineNumber()) | |
193 .arg(exception.columnNumber()); | |
194 std::cerr << errorString.toLocal8Bit().data() << std::endl; | |
195 return QXmlDefaultHandler::fatalError(exception); | |
196 } |