1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
* Copyright (C) 2008-2009 Manjeet Dahiya
*
* Author:
* Manjeet Dahiya
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include <QBuffer>
#include <QByteArray>
#include <QDateTime>
#include <QDebug>
#include <QRegExp>
#include <QString>
#include <QXmlStreamWriter>
#include "QXmppUtils.h"
#include "QXmppLogger.h"
QDateTime datetimeFromString(const QString &str)
{
QRegExp tzRe("(Z|([+-])([0-9]{2}):([0-9]{2}))");
int tzPos = tzRe.indexIn(str, 19);
if (str.size() < 20 || tzPos < 0)
return QDateTime();
// process date and time
QDateTime dt = QDateTime::fromString(str.left(19), "yyyy-MM-ddThh:mm:ss");
dt.setTimeSpec(Qt::UTC);
// process milliseconds
if (tzPos > 20 && str.at(19) == '.')
{
QString millis = (str.mid(20, tzPos - 20) + "000").left(3);
dt = dt.addMSecs(millis.toInt());
}
// process time zone
if (tzRe.cap(1) != "Z")
{
int offset = tzRe.cap(3).toInt() * 3600 + tzRe.cap(4).toInt() * 60;
if (tzRe.cap(2) == "+")
dt = dt.addSecs(-offset);
else
dt = dt.addSecs(offset);
}
return dt;
}
QString datetimeToString(const QDateTime &dt)
{
QDateTime utc = dt.toUTC();
if (utc.time().msec())
return utc.toString("yyyy-MM-ddThh:mm:ss.zzzZ");
else
return utc.toString("yyyy-MM-ddThh:mm:ssZ");
}
QString jidToResource(const QString& jid)
{
const int pos = jid.indexOf(QChar('/'));
if (pos < 0)
return QString();
return jid.mid(pos+1);
}
QString jidToBareJid(const QString& jid)
{
const int pos = jid.indexOf(QChar('/'));
if (pos < 0)
return jid;
return jid.left(pos);
}
QString generateStanzaHash()
{
QString somechars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
QString hashResult;
for ( int idx = 0; idx < 32; ++idx )
{
hashResult += somechars[(qrand() % 61)];
}
return hashResult;
}
void helperToXmlAddAttribute(QXmlStreamWriter* stream, const QString& name,
const QString& value)
{
if(!value.isEmpty())
stream->writeAttribute(name,value);
}
void helperToXmlAddNumberElement(QXmlStreamWriter* stream, const QString& name, int value)
{
stream->writeTextElement( name, QString::number(value));
}
void helperToXmlAddTextElement(QXmlStreamWriter* stream, const QString& name,
const QString& value)
{
if(!value.isEmpty())
stream->writeTextElement( name, value);
else
stream->writeEmptyElement(name);
}
QString escapeString(const QString& str)
{
QString strOut = str;
strOut.replace(QChar('&'), "&");
strOut.replace(QChar('<'), "<");
strOut.replace(QChar('>'), ">");
strOut.replace(QChar('"'), """);
return strOut;
}
QString unescapeString(const QString& str)
{
QString strOut = str;
strOut.replace("<", QChar('<'));
strOut.replace(">", QChar('>'));
strOut.replace(""", QChar('"'));
strOut.replace("&", QChar('&'));
return strOut;
}
|