3
* Copyright (C) 2006 Remko Troncon, Justin Karneges
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include "psioptions.h"
27
QString JIDUtil::defaultDomain()
29
return PsiOptions::instance()->getOption("options.account.domain").toString();
32
void JIDUtil::setDefaultDomain(QString domain)
34
PsiOptions::instance()->setOption("options.account.domain", domain);
37
QString JIDUtil::encode(const QString &jid)
41
for(int n = 0; n < jid.length(); ++n) {
42
if(jid.at(n) == '@') {
45
else if(jid.at(n) == '.') {
48
else if(!jid.at(n).isLetterOrNumber()) {
51
hex.sprintf("%%%02X", jid.at(n).latin1());
55
jid2.append(jid.at(n));
62
QString JIDUtil::decode(const QString &jid)
67
for(n = 0; n < (int)jid.length(); ++n) {
68
if(jid.at(n) == '%' && (jid.length() - n - 1) >= 2) {
69
QString str = jid.mid(n+1,2);
71
char c = str.toInt(&ok, 16);
80
jid2.append(jid.at(n));
84
// search for the _at_ backwards, just in case
85
for(n = (int)jid2.length(); n >= 3; --n) {
86
if(jid2.mid(n, 4) == "_at_") {
87
jid2.replace(n, 4, "@");
95
QString JIDUtil::nickOrJid(const QString &nick, const QString &jid)
103
QString JIDUtil::accountToString(const Jid& j, bool withResource)
105
QString s = j.user();
106
if (!defaultDomain().isEmpty()) {
107
return (withResource && !j.resource().isEmpty() ? j.user() + "/" + j.resource() : j.user());
110
return (withResource ? j.full() : j.bare());
114
Jid JIDUtil::accountFromString(const QString& s)
116
if (!defaultDomain().isEmpty()) {
118
j.set(defaultDomain(), s, "");
126
QString JIDUtil::toString(const Jid& j, bool withResource)
128
return (withResource ? j.full() : j.bare());
131
Jid JIDUtil::fromString(const QString& s)
136
QString JIDUtil::encode822(const QString &s)
139
for(int n = 0; n < (int)s.length(); ++n) {
140
if(s[n] == '\\' || s[n] == '<' || s[n] == '>') {
142
hex.sprintf("\\x%02X", (unsigned char )s[n].toLatin1());
151
QString JIDUtil::decode822(const QString &s)
154
for(int n = 0; n < (int)s.length(); ++n) {
155
if(s[n] == '\\' && n + 3 < (int)s.length()) {
161
val += QString(s[x+1]).toInt(NULL,16);
162
val += QString(s[x+2]).toInt(NULL,16);