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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* 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 "QXmppUtils.h"
#include "QXmppLogger.h"
#include <QString>
#include <QXmlStreamWriter>
#include <QByteArray>
#include <QBuffer>
#include <QImage>
#include <QImageReader>
#include <QCryptographicHash>
#include <QDateTime>
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)
{
return dt.toUTC().toString("yyyy-MM-ddThh:mm:ss.zzzZ");
}
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);
}
void log(const QString& str)
{
QXmppLogger::getLogger()->log(str);
}
void log(const QByteArray& str)
{
QXmppLogger::getLogger()->log(str);
}
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;
}
QString getImageType(const QByteArray& image)
{
QBuffer buffer;
buffer.setData(image);
buffer.open(QIODevice::ReadOnly);
QString format = QImageReader::imageFormat(&buffer);
if(format.toUpper() == "PNG")
return "image/png";
else if(format.toUpper() == "MNG")
return "video/x-mng";
else if(format.toUpper() == "GIF")
return "image/gif";
else if(format.toUpper() == "BMP")
return "image/bmp";
else if(format.toUpper() == "XPM")
return "image/x-xpm";
else if(format.toUpper() == "SVG")
return "image/svg+xml";
else if(format.toUpper() == "JPEG")
return "image/jpeg";
return "image/unknown";
}
QString getImageHash(const QByteArray& image)
{
if(image.isEmpty())
return "";
else
return QString(QCryptographicHash::hash(image,
QCryptographicHash::Sha1).toHex());
}
QImage getImageFromByteArray(const QByteArray& image)
{
QBuffer buffer;
buffer.setData(image);
buffer.open(QIODevice::ReadOnly);
QImageReader imageReader(&buffer);
return imageReader.read();
}
|