view jid.cpp @ 11:98485ebbdb86 default tip

LayoutView: WidgetViewBox fix
author Goffi <goffi@goffi.org>
date Sat, 27 Aug 2011 00:31:20 +0200
parents 48045176d1c6
children
line wrap: on
line source

/*
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; 
}

const bool& Jid::isValid() const
{
    return m_valid;
}

Jid& Jid::operator=(const Jid& other)
{
    m_user = other.getUser();
    m_domain = other.getDomain();
    m_resource = other.getResource();

    return *this;
}