Mercurial > hg > easaier-soundaccess
view 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 |
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ /* Sound Access EASAIER client application. Silogic 2007. Laure Bajard. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING included with this distribution for more information. */ #include "HttpClient.h" #include <iostream> HttpClient::HttpClient(const QString& configFileName) : QHttp() { QFile *config = new QFile(configFileName); m_useProxy = false; m_proxy = ""; m_proxyPort = 0; m_userProxyId = ""; m_pwdProxyId = ""; m_host = ""; m_hostPort = 0; if (config->exists()) { fromXmlString(config); } if (useProxy()) setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId); setHost(m_host, m_hostPort); } HttpClient::~HttpClient() {} void HttpClient::valuesChanged(ConnectionSettings* dialogBox) { setUseProxy(dialogBox->useProxy()); if (useProxy()) { m_proxy = dialogBox->getProxy(); m_proxyPort = (dialogBox->getProxyPort()).toInt(); m_userProxyId = dialogBox->getUserProxyId(); m_pwdProxyId = dialogBox->getPwdProxyId(); setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId); } m_host = dialogBox->getHost(); m_hostPort = (dialogBox->getHostPort()).toUShort(); setHost(m_host, m_hostPort); } QString HttpClient::toXmlString(QString indent, QString extraAttributes) const { QString s; s += indent; s += QString("<connexionSettings>\n"); if (useProxy()) { s += indent; s += QString("\t"); s += QString("<proxy name=\"%1\" port=\"%2\" user=\"%3\" pwd=\"%4\" />\n") .arg(getProxy()) .arg(QString::number(getProxyPort())) .arg(getUserProxyId()) .arg(getPwdProxyId()); } s += indent; s += QString("\t"); s += QString("<host name=\"%1\" port=\"%2\" />\n") .arg(getHost()) .arg(QString::number(getHostPort())); s += indent; s += QString("</connexionSettings>\n"); return s; } bool HttpClient::fromXmlString(QIODevice* file) { HttpClientConfigXmlHandler handler(this); QXmlSimpleReader reader; reader.setContentHandler(&handler); reader.setErrorHandler(&handler); if (!file->open(QFile::ReadOnly | QFile::Text)) { return false; } QXmlInputSource xmlInputSource(file); if (reader.parse(xmlInputSource)) return true; return false; } HttpClientConfigXmlHandler::HttpClientConfigXmlHandler(HttpClient* httpClient) : QXmlDefaultHandler() { m_httpClient = httpClient; } bool HttpClientConfigXmlHandler::startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes) { QString name = qName.toLower(); bool ok = false; // Valid element names: // config // connexionSettings // proxy // host if (name == "config") { // nothing needed ok = true; } else if (name == "connexionsettings") { // nothing needed ok = true; } else if (name == "proxy") { m_httpClient->setProxyInfo(attributes.value("name")); if (attributes.value("name") != "") m_httpClient->setUseProxy(true); m_httpClient->setProxyPortInfo(attributes.value("port").toInt()); m_httpClient->setUserProxyId(attributes.value("user")); m_httpClient->setPwdProxyId(attributes.value("pwd")); ok = true; } else if (name == "host") { m_httpClient->setHostInfo(attributes.value("name")); m_httpClient->setHostPortInfo(attributes.value("port").toUShort()); ok = true; } if (!ok) { std::cerr << "WARNING: connexion config-XML: Failed to completely process element \"" << name.toLocal8Bit().data() << "\"" << std::endl; } return true; } bool HttpClientConfigXmlHandler::endElement(const QString &namespaceURI, const QString &localName, const QString &qName) { return true; } bool HttpClientConfigXmlHandler::characters(const QString &str) { return true; } bool HttpClientConfigXmlHandler::error(const QXmlParseException &exception) { QString errorString; errorString += QString("ERROR: connexion config-XML: %1 at line %2, column %3") .arg(exception.message()) .arg(exception.lineNumber()) .arg(exception.columnNumber()); std::cerr << errorString.toLocal8Bit().data() << std::endl; return QXmlDefaultHandler::error(exception); } bool HttpClientConfigXmlHandler::fatalError(const QXmlParseException &exception) { QString errorString; errorString += QString("FATAL ERROR: connexion config-XML: %1 at line %2, column %3") .arg(exception.message()) .arg(exception.lineNumber()) .arg(exception.columnNumber()); std::cerr << errorString.toLocal8Bit().data() << std::endl; return QXmlDefaultHandler::fatalError(exception); }