PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 0ms

/src/sip/jabber/tomahawksipmessagefactory.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 146 lines | 100 code | 18 blank | 28 comment | 19 complexity | c520d7da322d71f48731e43eae3aaa1c MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU 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 *   Tomahawk 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 Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "tomahawksipmessagefactory.h"
 20
 21#include <QStringList>
 22#include <QXmlStreamWriter>
 23#include <QVariant>
 24
 25#include "utils/logger.h"
 26
 27using namespace Jreen;
 28
 29TomahawkSipMessageFactory::TomahawkSipMessageFactory()
 30{
 31    m_depth = 0;
 32    m_state = AtNowhere;
 33}
 34
 35TomahawkSipMessageFactory::~TomahawkSipMessageFactory()
 36{
 37}
 38
 39QStringList TomahawkSipMessageFactory::features() const
 40{
 41    return QStringList(TOMAHAWK_SIP_MESSAGE_NS);
 42}
 43
 44bool TomahawkSipMessageFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 45{
 46    Q_UNUSED(uri);
 47    Q_UNUSED(attributes);
 48    return name == QLatin1String("tomahawk") && uri == TOMAHAWK_SIP_MESSAGE_NS;
 49}
 50
 51void TomahawkSipMessageFactory::handleStartElement(const QStringRef &name, const QStringRef &uri,
 52                                            const QXmlStreamAttributes &attributes)
 53{
 54    m_depth++;
 55    if (m_depth == 1) {
 56        m_state = AtNowhere;
 57        m_ip = QString();
 58        m_port = -1;
 59        m_uniqname = QString();
 60        m_key = QString();
 61        m_visible = false;
 62    } else if (m_depth == 2) {
 63        if (name == QLatin1String("transport"))
 64        {
 65//            qDebug() << "Found Transport";
 66            m_state = AtTransport;
 67
 68            m_uniqname = attributes.value(QLatin1String("uniqname")).toString();
 69            m_key = attributes.value(QLatin1String("pwd")).toString();
 70        }
 71    } else if(m_depth == 3) {
 72        if (name == QLatin1String("candidate"))
 73        {
 74            m_state = AtCandidate;
 75//            qDebug() << "Found candidate";
 76            m_ip = attributes.value(QLatin1String("ip")).toString();
 77            m_port = attributes.value(QLatin1String("port")).toString().toInt();
 78
 79            m_visible = true;
 80        }
 81    }
 82    Q_UNUSED(uri);
 83    Q_UNUSED(attributes);
 84}
 85
 86void TomahawkSipMessageFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
 87{
 88    if (m_depth == 3)
 89        m_state = AtNowhere;
 90    Q_UNUSED(name);
 91    Q_UNUSED(uri);
 92    m_depth--;
 93}
 94
 95void TomahawkSipMessageFactory::handleCharacterData(const QStringRef &text)
 96{
 97    /*if (m_state == AtUtc) {
 98        //m_utc = Util::fromStamp(text.toString());
 99    } else if (m_state == AtTzo) {
100        QString str = text.toString();
101        int multiple = str.startsWith('-') ? -1 : 1;
102        //QTime delta = QTime::fromString(str.mid(1), QLatin1String("hh:mm"));
103        //m_tzo = multiple * (delta.hour() * 60 + delta.minute());
104    }*/
105    Q_UNUSED(text);
106}
107
108void TomahawkSipMessageFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
109{
110    TomahawkSipMessage *sipMessage = se_cast<TomahawkSipMessage*>(extension);
111
112        writer->writeStartElement(QLatin1String("tomahawk"));
113        writer->writeDefaultNamespace(TOMAHAWK_SIP_MESSAGE_NS);
114
115        if(sipMessage->visible())
116        {
117            // add transport tag
118            writer->writeStartElement(QLatin1String("transport"));
119            writer->writeAttribute(QLatin1String("pwd"), sipMessage->key());
120            writer->writeAttribute(QLatin1String("uniqname"), sipMessage->uniqname());
121
122            writer->writeEmptyElement(QLatin1String("candidate"));
123            writer->writeAttribute(QLatin1String("component"), "1");
124            writer->writeAttribute(QLatin1String("id"), "el0747fg11"); // FIXME
125            writer->writeAttribute(QLatin1String("ip"), sipMessage->ip());
126            writer->writeAttribute(QLatin1String("network"), "1");
127            writer->writeAttribute(QLatin1String("port"), QVariant(sipMessage->port()).toString());
128            writer->writeAttribute(QLatin1String("priority"), "1"); //TODO
129            writer->writeAttribute(QLatin1String("protocol"), "tcp");
130            writer->writeAttribute(QLatin1String("type"), "host"); //FIXME: correct?!
131            writer->writeEndElement();
132        }
133        else
134        {
135            writer->writeEmptyElement(QLatin1String("transport"));
136        }
137        writer->writeEndElement();
138}
139
140Payload::Ptr TomahawkSipMessageFactory::createPayload()
141{
142    if(m_visible)
143        return Payload::Ptr(new TomahawkSipMessage(m_ip, m_port, m_uniqname, m_key));
144    else
145        return Payload::Ptr(new TomahawkSipMessage());
146}