diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2012-07-21 21:08:04 +0200 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2012-07-21 21:08:04 +0200 |
| commit | fbd08d4872f7d6820c247621465716fd0be11eda (patch) | |
| tree | 9f86a2b634715f8ef7cb6e70b960b9b76309c110 /src/base/QXmppMessage.cpp | |
| parent | dc73e98aa4fed98e735a5c1ff24424e37d2a06b1 (diff) | |
| download | qxmpp-fbd08d4872f7d6820c247621465716fd0be11eda.tar.gz | |
refactor message type parsing
Diffstat (limited to 'src/base/QXmppMessage.cpp')
| -rw-r--r-- | src/base/QXmppMessage.cpp | 82 |
1 files changed, 18 insertions, 64 deletions
diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index d48e8948..4dd0246e 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -39,6 +39,14 @@ static const char* chat_states[] = { "paused", }; +static const char* message_types[] = { + "error", + "normal", + "chat", + "groupchat", + "headline" +}; + static const char *ns_xhtml = "http://www.w3.org/1999/xhtml"; /// Constructs a QXmppMessage. @@ -145,26 +153,6 @@ QXmppMessage::Type QXmppMessage::type() const return m_type; } -QString QXmppMessage::getTypeStr() const -{ - switch(m_type) - { - case QXmppMessage::Error: - return "error"; - case QXmppMessage::Normal: - return "normal"; - case QXmppMessage::Chat: - return "chat"; - case QXmppMessage::GroupChat: - return "groupchat"; - case QXmppMessage::Headline: - return "headline"; - default: - qWarning("QXmppMessage::getTypeStr() invalid type %d", (int)m_type); - return ""; - } -} - /// Sets the message's type. /// /// \param type @@ -174,47 +162,6 @@ void QXmppMessage::setType(QXmppMessage::Type type) m_type = type; } -void QXmppMessage::setTypeFromStr(const QString& str) -{ - if(str == "error") - { - setType(QXmppMessage::Error); - return; - } - else if(str == "") // if no type is specified - { - setType(QXmppMessage::Normal); - return; - } - else if(str == "normal") - { - setType(QXmppMessage::Normal); - return; - } - else if(str == "chat") - { - setType(QXmppMessage::Chat); - return; - } - else if(str == "groupchat") - { - setType(QXmppMessage::GroupChat); - return; - } - else if(str == "headline") - { - setType(QXmppMessage::Headline); - return; - } - else - { - setType(static_cast<QXmppMessage::Type>(-1)); - qWarning("QXmppMessage::setTypeFromStr() invalid input string type: %s", - qPrintable(str)); - return; - } -} - /// Returns the message's timestamp (if any). QDateTime QXmppMessage::stamp() const @@ -302,7 +249,15 @@ void QXmppMessage::parse(const QDomElement &element) { QXmppStanza::parse(element); - setTypeFromStr(element.attribute("type")); + const QString type = element.attribute("type"); + m_type = Normal; + for (int i = Error; i <= Headline; i++) { + if (type == message_types[i]) { + m_type = static_cast<Type>(i); + break; + } + } + m_body = element.firstChildElement("body").text(); m_subject = element.firstChildElement("subject").text(); m_thread = element.firstChildElement("thread").text(); @@ -381,13 +336,12 @@ void QXmppMessage::parse(const QDomElement &element) void QXmppMessage::toXml(QXmlStreamWriter *xmlWriter) const { - xmlWriter->writeStartElement("message"); helperToXmlAddAttribute(xmlWriter, "xml:lang", lang()); helperToXmlAddAttribute(xmlWriter, "id", id()); helperToXmlAddAttribute(xmlWriter, "to", to()); helperToXmlAddAttribute(xmlWriter, "from", from()); - helperToXmlAddAttribute(xmlWriter, "type", getTypeStr()); + helperToXmlAddAttribute(xmlWriter, "type", message_types[m_type]); if (!m_subject.isEmpty()) helperToXmlAddTextElement(xmlWriter, "subject", m_subject); if (!m_body.isEmpty()) |
