From eeee4736820954b6053e7c33233607f0dd60abf4 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 23 Nov 2017 11:04:35 +0100 Subject: [PATCH] Moved IMDN xml creation to imdn.cpp instead of chat-message --- src/chat/chat-message/chat-message.cpp | 103 +------------------------ src/chat/notification/imdn.cpp | 100 ++++++++++++++++++++++++ src/chat/notification/imdn.h | 1 + 3 files changed, 103 insertions(+), 101 deletions(-) diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 220602ff8..fbed81566 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -36,6 +36,7 @@ #include "content/content.h" #include "core/core.h" #include "logger/logger.h" +#include "chat/notification/imdn.h" #include "ortp/b64.h" @@ -310,106 +311,6 @@ bool ChatMessagePrivate::downloadFile () { // ----------------------------------------------------------------------------- -string ChatMessagePrivate::createImdnXml (Imdn::Type imdnType, LinphoneReason reason) { - xmlBufferPtr buf; - xmlTextWriterPtr writer; - int err; - string content; - char *datetime = nullptr; - - // Check that the chat message has a message id. - if (id.empty()) return nullptr; - - buf = xmlBufferCreate(); - if (buf == nullptr) { - lError() << "Error creating the XML buffer"; - return content; - } - writer = xmlNewTextWriterMemory(buf, 0); - if (writer == nullptr) { - lError() << "Error creating the XML writer"; - return content; - } - - datetime = linphone_timestamp_to_rfc3339_string(time); - err = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", nullptr); - if (err >= 0) { - err = xmlTextWriterStartElementNS(writer, nullptr, (const xmlChar *)"imdn", - (const xmlChar *)"urn:ietf:params:xml:ns:imdn"); - } - if ((err >= 0) && (reason != LinphoneReasonNone)) { - err = xmlTextWriterWriteAttributeNS(writer, (const xmlChar *)"xmlns", (const xmlChar *)"linphoneimdn", nullptr, (const xmlChar *)"http://www.linphone.org/xsds/imdn.xsd"); - } - if (err >= 0) { - err = xmlTextWriterWriteElement(writer, (const xmlChar *)"message-id", (const xmlChar *)id.c_str()); - } - if (err >= 0) { - err = xmlTextWriterWriteElement(writer, (const xmlChar *)"datetime", (const xmlChar *)datetime); - } - if (err >= 0) { - if (imdnType == Imdn::Type::Delivery) { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivery-notification"); - } else { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"display-notification"); - } - } - if (err >= 0) { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"status"); - } - if (err >= 0) { - if (reason == LinphoneReasonNone) { - if (imdnType == Imdn::Type::Delivery) { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivered"); - } else { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"displayed"); - } - } else { - err = xmlTextWriterStartElement(writer, (const xmlChar *)"error"); - } - } - if (err >= 0) { - // Close the "delivered", "displayed" or "error" element. - err = xmlTextWriterEndElement(writer); - } - if ((err >= 0) && (reason != LinphoneReasonNone)) { - err = xmlTextWriterStartElementNS(writer, (const xmlChar *)"linphoneimdn", (const xmlChar *)"reason", nullptr); - if (err >= 0) { - char codestr[16]; - snprintf(codestr, 16, "%d", linphone_reason_to_error_code(reason)); - err = xmlTextWriterWriteAttribute(writer, (const xmlChar *)"code", (const xmlChar *)codestr); - } - if (err >= 0) { - err = xmlTextWriterWriteString(writer, (const xmlChar *)linphone_reason_to_string(reason)); - } - if (err >= 0) { - err = xmlTextWriterEndElement(writer); - } - } - if (err >= 0) { - // Close the "status" element. - err = xmlTextWriterEndElement(writer); - } - if (err >= 0) { - // Close the "delivery-notification" or "display-notification" element. - err = xmlTextWriterEndElement(writer); - } - if (err >= 0) { - // Close the "imdn" element. - err = xmlTextWriterEndElement(writer); - } - if (err >= 0) { - err = xmlTextWriterEndDocument(writer); - } - if (err > 0) { - // xmlTextWriterEndDocument returns the size of the content. - content = string((char *)buf->content); - } - xmlFreeTextWriter(writer); - xmlBufferFree(buf); - ms_free(datetime); - return content; -} - void ChatMessagePrivate::sendImdn (Imdn::Type imdnType, LinphoneReason reason) { L_Q(); @@ -417,7 +318,7 @@ void ChatMessagePrivate::sendImdn (Imdn::Type imdnType, LinphoneReason reason) { Content *content = new Content(); content->setContentType("message/imdn+xml"); - content->setBody(createImdnXml(imdnType, reason)); + content->setBody(Imdn::createXml(id, time, imdnType, reason)); msg->addContent(*content); msg->getPrivate()->send(); diff --git a/src/chat/notification/imdn.cpp b/src/chat/notification/imdn.cpp index 4fc01dbc6..72ed7eb20 100644 --- a/src/chat/notification/imdn.cpp +++ b/src/chat/notification/imdn.cpp @@ -32,6 +32,106 @@ LINPHONE_BEGIN_NAMESPACE const string Imdn::imdnPrefix = "/imdn:imdn"; +string Imdn::createXml (string id, time_t time, Imdn::Type imdnType, LinphoneReason reason) { + xmlBufferPtr buf; + xmlTextWriterPtr writer; + int err; + string content; + char *datetime = nullptr; + + // Check that the chat message has a message id. + if (id.empty()) return nullptr; + + buf = xmlBufferCreate(); + if (buf == nullptr) { + lError() << "Error creating the XML buffer"; + return content; + } + writer = xmlNewTextWriterMemory(buf, 0); + if (writer == nullptr) { + lError() << "Error creating the XML writer"; + return content; + } + + datetime = linphone_timestamp_to_rfc3339_string(time); + err = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", nullptr); + if (err >= 0) { + err = xmlTextWriterStartElementNS(writer, nullptr, (const xmlChar *)"imdn", + (const xmlChar *)"urn:ietf:params:xml:ns:imdn"); + } + if ((err >= 0) && (reason != LinphoneReasonNone)) { + err = xmlTextWriterWriteAttributeNS(writer, (const xmlChar *)"xmlns", (const xmlChar *)"linphoneimdn", nullptr, (const xmlChar *)"http://www.linphone.org/xsds/imdn.xsd"); + } + if (err >= 0) { + err = xmlTextWriterWriteElement(writer, (const xmlChar *)"message-id", (const xmlChar *)id.c_str()); + } + if (err >= 0) { + err = xmlTextWriterWriteElement(writer, (const xmlChar *)"datetime", (const xmlChar *)datetime); + } + if (err >= 0) { + if (imdnType == Imdn::Type::Delivery) { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivery-notification"); + } else { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"display-notification"); + } + } + if (err >= 0) { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"status"); + } + if (err >= 0) { + if (reason == LinphoneReasonNone) { + if (imdnType == Imdn::Type::Delivery) { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivered"); + } else { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"displayed"); + } + } else { + err = xmlTextWriterStartElement(writer, (const xmlChar *)"error"); + } + } + if (err >= 0) { + // Close the "delivered", "displayed" or "error" element. + err = xmlTextWriterEndElement(writer); + } + if ((err >= 0) && (reason != LinphoneReasonNone)) { + err = xmlTextWriterStartElementNS(writer, (const xmlChar *)"linphoneimdn", (const xmlChar *)"reason", nullptr); + if (err >= 0) { + char codestr[16]; + snprintf(codestr, 16, "%d", linphone_reason_to_error_code(reason)); + err = xmlTextWriterWriteAttribute(writer, (const xmlChar *)"code", (const xmlChar *)codestr); + } + if (err >= 0) { + err = xmlTextWriterWriteString(writer, (const xmlChar *)linphone_reason_to_string(reason)); + } + if (err >= 0) { + err = xmlTextWriterEndElement(writer); + } + } + if (err >= 0) { + // Close the "status" element. + err = xmlTextWriterEndElement(writer); + } + if (err >= 0) { + // Close the "delivery-notification" or "display-notification" element. + err = xmlTextWriterEndElement(writer); + } + if (err >= 0) { + // Close the "imdn" element. + err = xmlTextWriterEndElement(writer); + } + if (err >= 0) { + err = xmlTextWriterEndDocument(writer); + } + if (err > 0) { + // xmlTextWriterEndDocument returns the size of the content. + content = string((char *)buf->content); + } + xmlFreeTextWriter(writer); + xmlBufferFree(buf); + ms_free(datetime); + return content; +} + void Imdn::parse (ChatRoom &cr, const string &text) { xmlparsing_context_t *xmlCtx = linphone_xmlparsing_context_new(); xmlSetGenericErrorFunc(xmlCtx, linphone_xmlparsing_genericxml_error); diff --git a/src/chat/notification/imdn.h b/src/chat/notification/imdn.h index 8bb57486a..15e41905a 100644 --- a/src/chat/notification/imdn.h +++ b/src/chat/notification/imdn.h @@ -37,6 +37,7 @@ public: Display }; + static std::string createXml(std::string id, time_t time, Imdn::Type imdnType, LinphoneReason reason); static void parse (ChatRoom &cr, const std::string &content); private: