comparison jid.cpp @ 4:220e5619bf87

Profiles selection now fill contact list + new Jid class
author Goffi <goffi@goffi.org>
date Thu, 11 Aug 2011 00:02:25 +0200
parents
children 48045176d1c6
comparison
equal deleted inserted replaced
3:2195295a2058 4:220e5619bf87
1 /*
2 Bellaciao: a Salut à Toi frontend
3 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "jid.h"
20 #include <QtDebug>
21
22 Jid::Jid()
23 :m_valid(false)
24 {
25 }
26
27 Jid::~Jid()
28 {
29 }
30
31 Jid::Jid(const QString& str)
32 {
33 fromString(str);
34 }
35
36 void Jid::fromString(const QString& str)
37 //Replace jid with the one parsed in str
38 //m_valid set to false is jid is not valid
39 {
40 //We do a basic parsing of the string
41 //TODO: better parsing/validity check
42 m_valid = false;
43 m_user.clear();
44 m_domain.clear();
45 m_resource.clear();
46
47 QChar delimiter = '@';
48 QString* curr_dest = &m_user;
49
50 for (int i=0; i<str.length(); i++)
51 {
52 if (str[i]==delimiter) {
53 if (delimiter=='@'){
54 curr_dest = &m_domain;
55 delimiter='/';
56 }
57 else if (delimiter=='/') {
58 curr_dest = &m_resource;
59 delimiter=0;
60 }
61 else {
62 qWarning("This line should never be reached");
63 }
64 }
65 else {
66 if (str[i]=='@' or str[i]=='/')
67 {
68 qWarning() << "Invalid jid [" << str <<"]";
69 return;
70 }
71 curr_dest->append(str[i]);
72 }
73 }
74
75 if (!m_user.isEmpty() && !m_domain.isEmpty())
76 m_valid = true;
77 }
78
79 QString Jid::getString() const
80 {
81 if (!m_valid) {
82 return m_user; //We return the user anyway if the jid is invalid
83 }
84 return m_resource.isEmpty() ? getBareString() : getFullString();
85 }
86
87 QString Jid::getBareString() const
88 {
89 return QString("%1@%2").arg(m_user).arg(m_domain);
90 }
91
92 QString Jid::getFullString() const
93 {
94 return QString("%1@%2/%3").arg(m_user).arg(m_domain).arg(m_resource);
95 }
96
97 const QString& Jid::getUser() const
98 {
99 return m_user;
100 }
101
102 const QString& Jid::getDomain() const
103 {
104 return m_domain;
105 }
106
107 const QString& Jid::getResource() const
108 {
109 return m_resource;
110 }
111
112 void Jid::setUser(const QString& user)
113 {
114 m_user = user;
115 }
116
117 void Jid::setDomain(const QString& domain)
118 {
119 m_domain = domain;
120 }
121
122 void Jid::setResource(const QString& resource)
123 {
124 m_resource = resource;
125 }
126
127 bool Jid::isValid()
128 {
129 return m_valid;
130 }