From c75eb544afb18c05be83d04626fc02e3ed2eb603 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Sat, 21 Jul 2012 21:15:44 +0200 Subject: make QXmppMessage use implicit data sharing --- src/base/QXmppMessage.cpp | 181 +++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 68 deletions(-) (limited to 'src/base/QXmppMessage.cpp') diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index 4dd0246e..d47233b9 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -49,6 +49,33 @@ static const char* message_types[] = { static const char *ns_xhtml = "http://www.w3.org/1999/xhtml"; +enum StampType +{ + LegacyDelayedDelivery, // XEP-0091: Legacy Delayed Delivery + DelayedDelivery, // XEP-0203: Delayed Delivery +}; + +class QXmppMessagePrivate : public QSharedData +{ +public: + QXmppMessage::Type type; + QDateTime stamp; + StampType stampType; + QXmppMessage::State state; + + bool attentionRequested; + QString body; + QString subject; + QString thread; + + // XEP-0071: XHTML-IM + QString xhtml; + + // Request message receipt as per XEP-0184. + QString receiptId; + bool receiptRequested; +}; + /// Constructs a QXmppMessage. /// /// \param from @@ -58,14 +85,23 @@ static const char *ns_xhtml = "http://www.w3.org/1999/xhtml"; QXmppMessage::QXmppMessage(const QString& from, const QString& to, const QString& body, const QString& thread) - : QXmppStanza(from, to), - m_type(Chat), - m_stampType(QXmppMessage::DelayedDelivery), - m_state(None), - m_attentionRequested(false), - m_body(body), - m_thread(thread), - m_receiptRequested(false) + : QXmppStanza(from, to) + , d(new QXmppMessagePrivate) +{ + d->type = Chat; + d->stampType = DelayedDelivery; + d->state = None; + d->attentionRequested = false; + d->body = body; + d->thread = thread; + d->receiptRequested = false; +} + +/// Constructs a copy of \a other. + +QXmppMessage::QXmppMessage(const QXmppMessage &other) + : QXmppStanza(other) + , d(other.d) { } @@ -74,12 +110,21 @@ QXmppMessage::~QXmppMessage() } +/// Assigns \a other to this message. + +QXmppMessage& QXmppMessage::operator=(const QXmppMessage &other) +{ + QXmppStanza::operator=(other); + d = other.d; + return *this; +} + /// Returns the message's body. /// QString QXmppMessage::body() const { - return m_body; + return d->body; } /// Sets the message's body. @@ -88,7 +133,7 @@ QString QXmppMessage::body() const void QXmppMessage::setBody(const QString& body) { - m_body = body; + d->body = body; } /// Returns true if the user's attention is requested, as defined @@ -96,7 +141,7 @@ void QXmppMessage::setBody(const QString& body) bool QXmppMessage::isAttentionRequested() const { - return m_attentionRequested; + return d->attentionRequested; } /// Sets whether the user's attention is requested, as defined @@ -106,7 +151,7 @@ bool QXmppMessage::isAttentionRequested() const void QXmppMessage::setAttentionRequested(bool requested) { - m_attentionRequested = requested; + d->attentionRequested = requested; } /// Returns true if a delivery receipt is requested, as defined @@ -114,7 +159,7 @@ void QXmppMessage::setAttentionRequested(bool requested) bool QXmppMessage::isReceiptRequested() const { - return m_receiptRequested; + return d->receiptRequested; } /// Sets whether a delivery receipt is requested, as defined @@ -124,7 +169,7 @@ bool QXmppMessage::isReceiptRequested() const void QXmppMessage::setReceiptRequested(bool requested) { - m_receiptRequested = requested; + d->receiptRequested = requested; if (requested && id().isEmpty()) generateAndSetNextId(); } @@ -134,7 +179,7 @@ void QXmppMessage::setReceiptRequested(bool requested) QString QXmppMessage::receiptId() const { - return m_receiptId; + return d->receiptId; } /// Make this message a delivery receipt for the message with @@ -142,7 +187,7 @@ QString QXmppMessage::receiptId() const void QXmppMessage::setReceiptId(const QString &id) { - m_receiptId = id; + d->receiptId = id; } /// Returns the message's type. @@ -150,7 +195,7 @@ void QXmppMessage::setReceiptId(const QString &id) QXmppMessage::Type QXmppMessage::type() const { - return m_type; + return d->type; } /// Sets the message's type. @@ -159,14 +204,14 @@ QXmppMessage::Type QXmppMessage::type() const void QXmppMessage::setType(QXmppMessage::Type type) { - m_type = type; + d->type = type; } /// Returns the message's timestamp (if any). QDateTime QXmppMessage::stamp() const { - return m_stamp; + return d->stamp; } /// Sets the message's timestamp. @@ -175,7 +220,7 @@ QDateTime QXmppMessage::stamp() const void QXmppMessage::setStamp(const QDateTime &stamp) { - m_stamp = stamp; + d->stamp = stamp; } /// Returns the message's chat state. @@ -183,7 +228,7 @@ void QXmppMessage::setStamp(const QDateTime &stamp) QXmppMessage::State QXmppMessage::state() const { - return m_state; + return d->state; } /// Sets the message's chat state. @@ -192,7 +237,7 @@ QXmppMessage::State QXmppMessage::state() const void QXmppMessage::setState(QXmppMessage::State state) { - m_state = state; + d->state = state; } /// Returns the message's subject. @@ -200,7 +245,7 @@ void QXmppMessage::setState(QXmppMessage::State state) QString QXmppMessage::subject() const { - return m_subject; + return d->subject; } /// Sets the message's subject. @@ -209,14 +254,14 @@ QString QXmppMessage::subject() const void QXmppMessage::setSubject(const QString& subject) { - m_subject = subject; + d->subject = subject; } /// Returns the message's thread. QString QXmppMessage::thread() const { - return m_thread; + return d->thread; } /// Sets the message's thread. @@ -225,7 +270,7 @@ QString QXmppMessage::thread() const void QXmppMessage::setThread(const QString& thread) { - m_thread = thread; + d->thread = thread; } /// Returns the message's XHTML body as defined by @@ -233,7 +278,7 @@ void QXmppMessage::setThread(const QString& thread) QString QXmppMessage::xhtml() const { - return m_xhtml; + return d->xhtml; } /// Sets the message's XHTML body as defined by @@ -241,7 +286,7 @@ QString QXmppMessage::xhtml() const void QXmppMessage::setXhtml(const QString &xhtml) { - m_xhtml = xhtml; + d->xhtml = xhtml; } /// \cond @@ -250,17 +295,17 @@ void QXmppMessage::parse(const QDomElement &element) QXmppStanza::parse(element); const QString type = element.attribute("type"); - m_type = Normal; + d->type = Normal; for (int i = Error; i <= Headline; i++) { if (type == message_types[i]) { - m_type = static_cast(i); + d->type = static_cast(i); break; } } - m_body = element.firstChildElement("body").text(); - m_subject = element.firstChildElement("subject").text(); - m_thread = element.firstChildElement("thread").text(); + d->body = element.firstChildElement("body").text(); + d->subject = element.firstChildElement("subject").text(); + d->thread = element.firstChildElement("thread").text(); // chat states for (int i = Active; i <= Paused; i++) @@ -269,7 +314,7 @@ void QXmppMessage::parse(const QDomElement &element) if (!stateElement.isNull() && stateElement.namespaceURI() == ns_chat_states) { - m_state = static_cast(i); + d->state = static_cast(i); break; } } @@ -279,40 +324,40 @@ void QXmppMessage::parse(const QDomElement &element) if (!htmlElement.isNull() && htmlElement.namespaceURI() == ns_xhtml_im) { QDomElement bodyElement = htmlElement.firstChildElement("body"); if (!bodyElement.isNull() && bodyElement.namespaceURI() == ns_xhtml) { - QTextStream stream(&m_xhtml, QIODevice::WriteOnly); + QTextStream stream(&d->xhtml, QIODevice::WriteOnly); bodyElement.save(stream, 0); - m_xhtml = m_xhtml.mid(m_xhtml.indexOf('>') + 1); - m_xhtml.replace(" xmlns=\"http://www.w3.org/1999/xhtml\"", ""); - m_xhtml.replace("", ""); - m_xhtml = m_xhtml.trimmed(); + d->xhtml = d->xhtml.mid(d->xhtml.indexOf('>') + 1); + d->xhtml.replace(" xmlns=\"http://www.w3.org/1999/xhtml\"", ""); + d->xhtml.replace("", ""); + d->xhtml = d->xhtml.trimmed(); } } // XEP-0184: Message Delivery Receipts QDomElement receivedElement = element.firstChildElement("received"); if (!receivedElement.isNull() && receivedElement.namespaceURI() == ns_message_receipts) { - m_receiptId = receivedElement.attribute("id"); + d->receiptId = receivedElement.attribute("id"); // compatibility with old-style XEP - if (m_receiptId.isEmpty()) - m_receiptId = id(); + if (d->receiptId.isEmpty()) + d->receiptId = id(); } else { - m_receiptId = QString(); + d->receiptId = QString(); } - m_receiptRequested = element.firstChildElement("request").namespaceURI() == ns_message_receipts; + d->receiptRequested = element.firstChildElement("request").namespaceURI() == ns_message_receipts; // XEP-0203: Delayed Delivery QDomElement delayElement = element.firstChildElement("delay"); if (!delayElement.isNull() && delayElement.namespaceURI() == ns_delayed_delivery) { const QString str = delayElement.attribute("stamp"); - m_stamp = QXmppUtils::datetimeFromString(str); - m_stampType = QXmppMessage::DelayedDelivery; + d->stamp = QXmppUtils::datetimeFromString(str); + d->stampType = DelayedDelivery; } // XEP-0224: Attention - m_attentionRequested = element.firstChildElement("attention").namespaceURI() == ns_attention; + d->attentionRequested = element.firstChildElement("attention").namespaceURI() == ns_attention; QXmppElementList extensions; QDomElement xElement = element.firstChildElement("x"); @@ -322,9 +367,9 @@ void QXmppMessage::parse(const QDomElement &element) { // XEP-0091: Legacy Delayed Delivery const QString str = xElement.attribute("stamp"); - m_stamp = QDateTime::fromString(str, "yyyyMMddThh:mm:ss"); - m_stamp.setTimeSpec(Qt::UTC); - m_stampType = QXmppMessage::LegacyDelayedDelivery; + d->stamp = QDateTime::fromString(str, "yyyyMMddThh:mm:ss"); + d->stamp.setTimeSpec(Qt::UTC); + d->stampType = LegacyDelayedDelivery; } else { // other extensions extensions << QXmppElement(xElement); @@ -341,40 +386,40 @@ void QXmppMessage::toXml(QXmlStreamWriter *xmlWriter) const helperToXmlAddAttribute(xmlWriter, "id", id()); helperToXmlAddAttribute(xmlWriter, "to", to()); helperToXmlAddAttribute(xmlWriter, "from", from()); - helperToXmlAddAttribute(xmlWriter, "type", message_types[m_type]); - if (!m_subject.isEmpty()) - helperToXmlAddTextElement(xmlWriter, "subject", m_subject); - if (!m_body.isEmpty()) - helperToXmlAddTextElement(xmlWriter, "body", m_body); - if (!m_thread.isEmpty()) - helperToXmlAddTextElement(xmlWriter, "thread", m_thread); + helperToXmlAddAttribute(xmlWriter, "type", message_types[d->type]); + if (!d->subject.isEmpty()) + helperToXmlAddTextElement(xmlWriter, "subject", d->subject); + if (!d->body.isEmpty()) + helperToXmlAddTextElement(xmlWriter, "body", d->body); + if (!d->thread.isEmpty()) + helperToXmlAddTextElement(xmlWriter, "thread", d->thread); error().toXml(xmlWriter); // chat states - if (m_state > None && m_state <= Paused) + if (d->state > None && d->state <= Paused) { - xmlWriter->writeStartElement(chat_states[m_state]); + xmlWriter->writeStartElement(chat_states[d->state]); xmlWriter->writeAttribute("xmlns", ns_chat_states); xmlWriter->writeEndElement(); } // XEP-0071: XHTML-IM - if (!m_xhtml.isEmpty()) { + if (!d->xhtml.isEmpty()) { xmlWriter->writeStartElement("html"); xmlWriter->writeAttribute("xmlns", ns_xhtml_im); xmlWriter->writeStartElement("body"); xmlWriter->writeAttribute("xmlns", ns_xhtml); xmlWriter->writeCharacters(""); - xmlWriter->device()->write(m_xhtml.toUtf8()); + xmlWriter->device()->write(d->xhtml.toUtf8()); xmlWriter->writeEndElement(); xmlWriter->writeEndElement(); } // time stamp - if (m_stamp.isValid()) + if (d->stamp.isValid()) { - QDateTime utcStamp = m_stamp.toUTC(); - if (m_stampType == QXmppMessage::DelayedDelivery) + QDateTime utcStamp = d->stamp.toUTC(); + if (d->stampType == DelayedDelivery) { // XEP-0203: Delayed Delivery xmlWriter->writeStartElement("delay"); @@ -391,20 +436,20 @@ void QXmppMessage::toXml(QXmlStreamWriter *xmlWriter) const } // XEP-0184: Message Delivery Receipts - if (!m_receiptId.isEmpty()) { + if (!d->receiptId.isEmpty()) { xmlWriter->writeStartElement("received"); xmlWriter->writeAttribute("xmlns", ns_message_receipts); - xmlWriter->writeAttribute("id", m_receiptId); + xmlWriter->writeAttribute("id", d->receiptId); xmlWriter->writeEndElement(); } - if (m_receiptRequested) { + if (d->receiptRequested) { xmlWriter->writeStartElement("request"); xmlWriter->writeAttribute("xmlns", ns_message_receipts); xmlWriter->writeEndElement(); } // XEP-0224: Attention - if (m_attentionRequested) { + if (d->attentionRequested) { xmlWriter->writeStartElement("attention"); xmlWriter->writeAttribute("xmlns", ns_attention); xmlWriter->writeEndElement(); -- cgit v1.2.3