1 /*
2  *  Kaidan - A user-friendly XMPP client for every device!
3  *
4  *  Copyright (C) 2016-2021 Kaidan developers and contributors
5  *  (see the LICENSE file for a full list of copyright authors)
6  *
7  *  Kaidan is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  In addition, as a special exception, the author of Kaidan gives
13  *  permission to link the code of its release with the OpenSSL
14  *  project's "OpenSSL" library (or with modified versions of it that
15  *  use the same license as the "OpenSSL" library), and distribute the
16  *  linked executables. You must obey the GNU General Public License in
17  *  all respects for all of the code used other than "OpenSSL". If you
18  *  modify this file, you may extend this exception to your version of
19  *  the file, but you are not obligated to do so.  If you do not wish to
20  *  do so, delete this exception statement from your version.
21  *
22  *  Kaidan is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License
28  *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "LogHandler.h"
32 
33 // Qt
34 #include <QDebug>
35 #include <QXmlStreamReader>
36 #include <QXmlStreamWriter>
37 // QXmpp
38 #include <QXmppClient.h>
39 #include <QXmppLogger.h>
40 
LogHandler(QXmppClient * client,bool enable,QObject * parent)41 LogHandler::LogHandler(QXmppClient *client, bool enable, QObject *parent) : QObject(parent), m_client(client)
42 {
43 	client->logger()->setLoggingType(QXmppLogger::SignalLogging);
44 	enableLogging(enable);
45 }
46 
enableLogging(bool enabled)47 void LogHandler::enableLogging(bool enabled)
48 {
49 	// check if we need to change something
50 	if (this->enabled == enabled)
51 		return;
52 	// update enabled status
53 	this->enabled = enabled;
54 
55 	// apply change: enable or disable
56 	if (enabled)
57 		connect(m_client->logger(), &QXmppLogger::message, this, &LogHandler::handleLog);
58 	else
59 		disconnect(m_client->logger(), &QXmppLogger::message, this, &LogHandler::handleLog);
60 }
61 
handleLog(QXmppLogger::MessageType type,const QString & text)62 void LogHandler::handleLog(QXmppLogger::MessageType type, const QString &text)
63 {
64 	if (type == QXmppLogger::ReceivedMessage)
65 		qDebug() << "[client] [incoming] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
66 	else if (type == QXmppLogger::SentMessage)
67 		qDebug() << "[client] [outgoing] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
68 	else
69 		return;
70 
71 	qDebug().noquote() << makeXmlPretty(text);
72 }
73 
makeXmlPretty(QString xmlIn)74 QString LogHandler::makeXmlPretty(QString xmlIn)
75 {
76 	QString xmlOut;
77 
78 	QXmlStreamReader reader(xmlIn);
79 	QXmlStreamWriter writer(&xmlOut);
80 	writer.setAutoFormatting(true);
81 
82 	while (!reader.atEnd()) {
83 		reader.readNext();
84 		if (!reader.isWhitespace() && !reader.hasError()) {
85 			writer.writeCurrentToken(reader);
86 		}
87 	}
88 
89 	// remove xml header
90 	xmlOut.replace("<?xml version=\"1.0\"?>", "");
91 
92 	// remove first & last char (\n)
93 	// first char is needed due to header replacement
94 	xmlOut = xmlOut.right(xmlOut.size() - 1);
95 	xmlOut = xmlOut.left(xmlOut.size() - 1);
96 
97 	return xmlOut;
98 }
99