changeset 8:d32c0a79ba39

add connection setting information reading and saving
author lbajardsilogic
date Fri, 11 May 2007 15:39:08 +0000
parents a5175615d153
children 5370801117f2
files data/fileio/HttpClient.cpp data/fileio/HttpClient.h
diffstat 2 files changed, 284 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/fileio/HttpClient.cpp	Fri May 11 15:39:08 2007 +0000
@@ -0,0 +1,196 @@
+/* -*- 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);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/fileio/HttpClient.h	Fri May 11 15:39:08 2007 +0000
@@ -0,0 +1,88 @@
+/* -*- 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.
+*/
+
+#ifndef _HTTP_CLIENT_H_
+#define _HTTP_CLIENT_H_
+
+#include <QHttp>
+#include <QFile>
+#include <QXmlDefaultHandler>
+
+#include "widgets/ConnectionSettings.h"
+#include "base/XmlExportable.h"
+
+class ConnectionSettings;
+
+class HttpClient : public QHttp, public XmlExportable 
+{
+	Q_OBJECT
+
+public:
+	HttpClient(const QString& configFileName);
+	virtual ~HttpClient();
+
+	inline bool useProxy() const {return m_useProxy;}
+	inline QString getProxy() const {return m_proxy;}
+	inline int getProxyPort() const {return m_proxyPort;}
+	inline QString getUserProxyId() const {return m_userProxyId;}
+	inline QString getPwdProxyId() const {return m_pwdProxyId;}
+
+	inline QString getHost() const {return m_host;}
+	inline quint16 getHostPort() const {return m_hostPort;}
+
+	inline void setUseProxy(const bool& ok) { m_useProxy = ok;}
+	inline void setProxyInfo(const QString& proxy) { m_proxy = proxy;}
+	inline void setProxyPortInfo(const int& port) { m_proxyPort = port;}
+	inline void setUserProxyId(const QString& user) { m_userProxyId = user;}
+	inline void setPwdProxyId(const QString& pwd) { m_pwdProxyId = pwd;}
+
+	inline void setHostInfo(const QString& host) { m_host = host;}
+	inline void setHostPortInfo(const quint16 port) { m_hostPort = port;}
+
+	void valuesChanged(ConnectionSettings* dialogBox);
+
+	virtual QString toXmlString(QString indent = "", QString extraAttributes = "") const;
+	bool fromXmlString(QIODevice *file);
+
+private:
+
+	bool		m_useProxy;
+	QString		m_proxy;
+	int			m_proxyPort;
+	QString		m_userProxyId;
+	QString		m_pwdProxyId;
+
+	QString		m_host;
+	quint16		m_hostPort;
+};
+
+
+class HttpClientConfigXmlHandler : public QXmlDefaultHandler
+{
+public:
+	HttpClientConfigXmlHandler(HttpClient *httpClient);
+
+	bool startElement(const QString &namespaceURI, const QString &localName,
+						  const QString &qName, const QXmlAttributes &attributes);
+	bool endElement(const QString &namespaceURI, const QString &localName,
+						const QString &qName);
+	bool characters(const QString &str);
+	bool error(const QXmlParseException &exception);
+	bool fatalError(const QXmlParseException &exception);
+
+private:
+
+	HttpClient	*m_httpClient;
+
+};
+#endif
\ No newline at end of file