PageRenderTime 110ms CodeModel.GetById 33ms app.highlight 14ms RepoModel.GetById 33ms app.codeStats 0ms

/src/sip/jabber/xmlconsole.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 186 lines | 148 code | 20 blank | 18 comment | 16 complexity | 2d597484b99ce42334b3c3234dfb5db3 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2011, Ruslan Nigmatullin <euroelessar@ya.ru>
  4 *   Copyright 2011, Dominik Schmidt <dev@dominik-schmidt.de>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU 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 *   Tomahawk 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 General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#ifndef XMLCONSOLE_H
 21#define XMLCONSOLE_H
 22
 23#include <jreen/client.h>
 24#include <jreen/jid.h>
 25
 26#include <QWidget>
 27#include <QXmlStreamReader>
 28#include <QDateTime>
 29#include <QTextBlock>
 30
 31namespace Ui {
 32class XmlConsole;
 33}
 34
 35#include "../sipdllmacro.h"
 36
 37class SIPDLLEXPORT XmlConsole : public QWidget, public Jreen::XmlStreamHandler
 38{
 39    Q_OBJECT
 40
 41public:
 42	XmlConsole(Jreen::Client* client, QWidget *parent = 0);
 43	~XmlConsole();
 44
 45	virtual void handleStreamBegin();
 46	virtual void handleStreamEnd();
 47	virtual void handleIncomingData(const char *data, qint64 size);
 48	virtual void handleOutgoingData(const char *data, qint64 size);
 49
 50protected:
 51	void changeEvent(QEvent *e);
 52private:
 53	void stackProcess(const QByteArray &data, bool incoming);
 54
 55	struct XmlNode
 56	{
 57		enum Type
 58		{
 59			Iq = 1,
 60			Presence = 2,
 61			Message = 4,
 62			Custom = 8
 63		};
 64		QDateTime time;
 65		Type type;
 66		bool incoming;
 67		QSet<QString> xmlns;
 68		Jreen::JID jid;
 69		QSet<QString> attributes;
 70		QTextBlock block;
 71		int lineCount;
 72	};
 73	enum FilterType
 74	{
 75		Disabled = 0x10,
 76		ByJid = 0x20,
 77		ByXmlns = 0x30,
 78		ByAllAttributes = 0x40
 79	};
 80
 81	enum State
 82	{
 83		WaitingForStanza,
 84		ReadFeatures,
 85		ReadStanza,
 86		ReadCustom
 87	};
 88
 89	struct StackToken
 90	{
 91		StackToken(QXmlStreamReader &reader)
 92		{
 93			type = reader.tokenType();
 94			if (type == QXmlStreamReader::StartElement) {
 95				QStringRef tmp = reader.name();
 96				startTag.namePointer = new QString(*tmp.string());
 97				startTag.name = new QStringRef(startTag.namePointer, tmp.position(), tmp.length());
 98				tmp = reader.namespaceUri();
 99				startTag.xmlnsPointer = new QString(*tmp.string());
100				startTag.xmlns = new QStringRef(startTag.xmlnsPointer, tmp.position(), tmp.length());
101				startTag.attributes = new QXmlStreamAttributes(reader.attributes());
102				startTag.empty = false;
103			} else if (type == QXmlStreamReader::Characters) {
104				QStringRef tmp = reader.text();
105				charachters.textPointer = new QString(*tmp.string());
106				charachters.text = new QStringRef(charachters.textPointer, tmp.position(), tmp.length());
107			} else if (type == QXmlStreamReader::EndElement) {
108				QStringRef tmp = reader.name();
109				endTag.namePointer = new QString(*tmp.string());
110				endTag.name = new QStringRef(endTag.namePointer, tmp.position(), tmp.length());
111			}
112		}
113
114		StackToken(const QString &name)
115		{
116			type = QXmlStreamReader::Characters;
117			charachters.textPointer = new QString(name);
118			charachters.text = new QStringRef(charachters.textPointer);
119		}
120
121		~StackToken()
122		{
123			if (type == QXmlStreamReader::StartElement) {
124				delete startTag.namePointer;
125				delete startTag.name;
126				delete startTag.xmlnsPointer;
127				delete startTag.xmlns;
128				delete startTag.attributes;
129			} else if (type == QXmlStreamReader::Characters) {
130				delete charachters.textPointer;
131				delete charachters.text;
132			} else if (type == QXmlStreamReader::EndElement) {
133				delete endTag.namePointer;
134				delete endTag.name;
135			}
136		}
137
138		QXmlStreamReader::TokenType type;
139		union {
140			struct {
141				QString *namePointer;
142				QStringRef *name;
143				QString *xmlnsPointer;
144				QStringRef *xmlns;
145				QXmlStreamAttributes *attributes;
146				bool empty;
147			} startTag;
148			struct {
149				QString *textPointer;
150				QStringRef *text;
151			} charachters;
152			struct {
153				QString *namePointer;
154				QStringRef *name;
155			} endTag;
156		};
157	};
158
159	struct StackEnvironment
160	{
161		QXmlStreamReader reader;
162		State state;
163		int depth;
164		QList<StackToken*> tokens;
165		QColor bodyColor;
166		QColor tagColor;
167		QColor attributeColor;
168		QColor paramColor;
169	};
170
171	Ui::XmlConsole *m_ui;
172	Jreen::Client *m_client;
173	QList<XmlNode> m_nodes;
174	StackEnvironment m_stackIncoming;
175	StackEnvironment m_stackOutgoing;
176	QColor m_stackBracketsColor;
177	int m_filter;
178
179private slots:
180	void on_lineEdit_textChanged(const QString &);
181	void onActionGroupTriggered(QAction *action);
182	void on_saveButton_clicked();
183};
184
185
186#endif // XMLCONSOLE_H