~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/jidutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * jidutil.h
 
3
 * Copyright (C) 2006  Remko Troncon, Justin Karneges
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "xmpp_jid.h"
 
22
#include "jidutil.h"
 
23
#include "psioptions.h"
 
24
 
 
25
using namespace XMPP;
 
26
 
 
27
QString JIDUtil::defaultDomain()
 
28
{
 
29
        return PsiOptions::instance()->getOption("options.account.domain").toString();
 
30
}
 
31
 
 
32
void JIDUtil::setDefaultDomain(QString domain)
 
33
{
 
34
        PsiOptions::instance()->setOption("options.account.domain", domain);
 
35
}
 
36
 
 
37
QString JIDUtil::encode(const QString &jid)
 
38
{
 
39
        QString jid2;
 
40
 
 
41
        for(int n = 0; n < jid.length(); ++n) {
 
42
                if(jid.at(n) == '@') {
 
43
                        jid2.append("_at_");
 
44
                }
 
45
                else if(jid.at(n) == '.') {
 
46
                        jid2.append('.');
 
47
                }
 
48
                else if(!jid.at(n).isLetterOrNumber()) {
 
49
                        // hex encode
 
50
                        QString hex;
 
51
                        hex.sprintf("%%%02X", jid.at(n).latin1());
 
52
                        jid2.append(hex);
 
53
                }
 
54
                else {
 
55
                        jid2.append(jid.at(n));
 
56
                }
 
57
        }
 
58
 
 
59
        return jid2;
 
60
}
 
61
 
 
62
QString JIDUtil::decode(const QString &jid)
 
63
{
 
64
        QString jid2;
 
65
        int n;
 
66
 
 
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);
 
70
                        bool ok;
 
71
                        char c = str.toInt(&ok, 16);
 
72
                        if(!ok)
 
73
                                continue;
 
74
 
 
75
                        QChar a(c);
 
76
                        jid2.append(a);
 
77
                        n += 2;
 
78
                }
 
79
                else {
 
80
                        jid2.append(jid.at(n));
 
81
                }
 
82
        }
 
83
 
 
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, "@");
 
88
                        break;
 
89
                }
 
90
        }
 
91
 
 
92
        return jid2;
 
93
}
 
94
 
 
95
QString JIDUtil::nickOrJid(const QString &nick, const QString &jid)
 
96
{
 
97
        if(nick.isEmpty())
 
98
                return jid;
 
99
        else
 
100
                return nick;
 
101
}
 
102
 
 
103
QString JIDUtil::accountToString(const Jid& j, bool withResource)
 
104
{
 
105
        QString s = j.user();
 
106
        if (!defaultDomain().isEmpty()) {
 
107
                return (withResource && !j.resource().isEmpty() ? j.user() + "/" + j.resource() : j.user());
 
108
        }
 
109
        else {
 
110
                return (withResource ? j.full() : j.bare());
 
111
        }
 
112
}
 
113
 
 
114
Jid JIDUtil::accountFromString(const QString& s)
 
115
{
 
116
        if (!defaultDomain().isEmpty()) {
 
117
                Jid j;
 
118
                j.set(defaultDomain(), s, "");
 
119
                return j;
 
120
        }
 
121
        else {
 
122
                return Jid(s);
 
123
        }
 
124
}
 
125
 
 
126
QString JIDUtil::toString(const Jid& j, bool withResource)
 
127
{
 
128
        return (withResource ? j.full() : j.bare());
 
129
}
 
130
 
 
131
Jid JIDUtil::fromString(const QString& s)
 
132
{
 
133
        return Jid(s);
 
134
}
 
135
 
 
136
QString JIDUtil::encode822(const QString &s)
 
137
{
 
138
        QString out;
 
139
        for(int n = 0; n < (int)s.length(); ++n) {
 
140
                if(s[n] == '\\' || s[n] == '<' || s[n] == '>') {
 
141
                        QString hex;
 
142
                        hex.sprintf("\\x%02X", (unsigned char )s[n].toLatin1());
 
143
                        out.append(hex);
 
144
                }
 
145
                else
 
146
                        out += s[n];
 
147
        }
 
148
        return out;
 
149
}
 
150
 
 
151
QString JIDUtil::decode822(const QString &s)
 
152
{
 
153
        QString out;
 
154
        for(int n = 0; n < (int)s.length(); ++n) {
 
155
                if(s[n] == '\\' && n + 3 < (int)s.length()) {
 
156
                        int x = n + 1;
 
157
                        n += 3;
 
158
                        if(s[x] != 'x')
 
159
                                continue;
 
160
                        ushort val = 0;
 
161
                        val += QString(s[x+1]).toInt(NULL,16);
 
162
                        val += QString(s[x+2]).toInt(NULL,16);
 
163
                        QChar c(val);
 
164
                        out += c;
 
165
                }
 
166
                else
 
167
                        out += s[n];
 
168
        }
 
169
        return out;
 
170
}
 
171