diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jid.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -0,0 +1,130 @@
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program 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 Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "jid.h"
+#include <QtDebug>
+
+Jid::Jid()
+    :m_valid(false)
+{
+}
+
+Jid::~Jid()
+{
+}
+
+Jid::Jid(const QString& str)
+{
+    fromString(str);
+}
+
+void Jid::fromString(const QString& str)
+//Replace jid with the one parsed in str
+//m_valid set to false is jid is not valid
+{
+    //We do a basic parsing of the string
+    //TODO: better parsing/validity check
+    m_valid = false;
+    m_user.clear();
+    m_domain.clear();
+    m_resource.clear();
+
+    QChar delimiter = '@';
+    QString* curr_dest = &m_user;
+
+    for (int i=0; i<str.length(); i++)
+    {
+        if (str[i]==delimiter) {
+            if (delimiter=='@'){
+                curr_dest = &m_domain;
+                delimiter='/';
+            }
+            else if (delimiter=='/') {
+                curr_dest = &m_resource;
+                delimiter=0;
+            }
+            else {
+                qWarning("This line should never be reached");
+            }
+        }
+        else {
+            if (str[i]=='@' or str[i]=='/')
+            {
+                qWarning() << "Invalid jid [" <<  str <<"]";
+                return;
+            }
+            curr_dest->append(str[i]);
+        }
+    }
+
+    if (!m_user.isEmpty() && !m_domain.isEmpty())
+        m_valid = true;
+}
+
+QString Jid::getString() const
+{
+    if (!m_valid) {
+        return m_user; //We return the user anyway if the jid is invalid
+    }
+    return m_resource.isEmpty() ? getBareString() : getFullString();
+}
+
+QString Jid::getBareString() const
+{
+    return QString("%1@%2").arg(m_user).arg(m_domain);
+}
+
+QString Jid::getFullString() const
+{
+    return QString("%1@%2/%3").arg(m_user).arg(m_domain).arg(m_resource);
+}
+
+const QString& Jid::getUser() const
+{
+    return m_user;
+}
+
+const QString& Jid::getDomain() const
+{
+    return m_domain;
+}
+
+const QString& Jid::getResource() const
+{
+    return m_resource;
+}
+
+void Jid::setUser(const QString& user)
+{
+   m_user = user; 
+}
+
+void Jid::setDomain(const QString& domain)
+{
+   m_domain = domain; 
+}
+
+void Jid::setResource(const QString& resource)
+{
+   m_resource = resource; 
+}
+
+bool Jid::isValid()
+{
+    return m_valid;
+}