File indexing completed on 2023-10-01 08:41:45

0001 /*
0002     Copyright (C) 2012  Lasath Fernando <kde@lasath.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017 */
0018 
0019 
0020 #include "message.h"
0021 #include "message-private.h"
0022 
0023 #include "ktp-debug.h"
0024 #include <QSharedData>
0025 
0026 #include <TelepathyQt/ContactManager>
0027 #include <TelepathyQt/Connection>
0028 
0029 using namespace KTp;
0030 
0031 Message& Message::operator=(const Message &other) {
0032     d = other.d;
0033     return *this;
0034 }
0035 
0036 Message::Message(Message::Private *dd):
0037     d(dd)
0038 {
0039 }
0040 
0041 Message::Message(const Tp::Message &original, const KTp::MessageContext &context) :
0042     d(new Private)
0043 {
0044     d->sentTime = original.sent();
0045     d->token = original.messageToken();
0046     d->messageType = original.messageType();
0047     d->isHistory = false;
0048     d->direction = KTp::Message::LocalToRemote;
0049 
0050     setMainMessagePart(original.text());
0051 
0052     if (context.account()->connection()) {
0053         d->sender = KTp::ContactPtr::qObjectCast(context.account()->connection()->selfContact());
0054     } else {
0055         d->senderAlias = context.account()->nickname();
0056         d->senderId = context.account()->uniqueIdentifier();
0057     }
0058 }
0059 
0060 Message::Message(const Tp::ReceivedMessage &original, const KTp::MessageContext &context) :
0061     d(new Private)
0062 {
0063     Q_UNUSED(context)
0064 
0065     d->sentTime = original.sent();
0066     if (d->sentTime.isNull()) {
0067         d->sentTime = original.received();
0068     }
0069 
0070     d->token = original.messageToken();
0071     d->messageType = original.messageType();
0072     d->isHistory = original.isScrollback();
0073 
0074     setMainMessagePart(original.text());
0075 
0076     if (!original.sender().isNull()) {
0077         d->sender = KTp::ContactPtr::qObjectCast(original.sender());
0078     } else {
0079         d->senderAlias = original.senderNickname();
0080     }
0081 
0082     bool isLocalToRemote = false;
0083 
0084     if (!d->sender.isNull()) {
0085         if (context.channel()->interfaces().contains(TP_QT_IFACE_CHANNEL_INTERFACE_GROUP)) {
0086             isLocalToRemote = d->sender->id() == context.channel()->groupSelfContact()->id();
0087         } else {
0088             isLocalToRemote = d->sender->id() == context.channel()->connection()->selfContact()->id();
0089         }
0090     }
0091 
0092     if (isLocalToRemote) {
0093         d->direction = KTp::Message::LocalToRemote;
0094     } else {
0095         d->direction = KTp::Message::RemoteToLocal;
0096     }
0097 }
0098 
0099 Message::Message(const Message& other):
0100     d(other.d)
0101 {
0102 }
0103 
0104 Message::~Message()
0105 {
0106 }
0107 
0108 QString Message::mainMessagePart() const
0109 {
0110     return d->mainPart;
0111 }
0112 
0113 void Message::setMainMessagePart(const QString& message)
0114 {
0115     d->mainPart = message;
0116 }
0117 
0118 void Message::appendMessagePart(const QString& part)
0119 {
0120     d->parts << part;
0121 }
0122 
0123 void Message::appendScript(const QString& script)
0124 {
0125     // Append the script only if it is not already appended to avoid multiple
0126     // execution of the scripts.
0127     if (!d->scripts.contains(script)) {
0128         d->scripts << script;
0129     }
0130 }
0131 
0132 QString Message::finalizedMessage() const
0133 {
0134     QString msg = d->mainPart + QLatin1Char('\n') +
0135         d->parts.join(QLatin1String("\n"));
0136 
0137 //     qCDebug(KTP_COMMONINTERNALS) << msg;
0138     return msg;
0139 }
0140 
0141 QString Message::finalizedScript() const
0142 {
0143     if (d->scripts.empty()) {
0144         return QString();
0145     }
0146 
0147     QString finalScript = d->scripts.join(QString());
0148 
0149     if (!finalScript.isEmpty()) {
0150         finalScript.append(QLatin1String("false;"));
0151     }
0152 
0153 //    qCDebug(KTP_COMMONINTERNALS) << finalScript;
0154     return finalScript;
0155 }
0156 
0157 QVariant Message::property(const char *name) const
0158 {
0159     return d->properties[QLatin1String(name)];
0160 }
0161 
0162 void Message::setProperty(const char *name, const QVariant& value)
0163 {
0164     d->properties[QLatin1String(name)] = value;
0165 }
0166 
0167 QDateTime Message::time() const
0168 {
0169     return d->sentTime;
0170 }
0171 
0172 QString Message::token() const
0173 {
0174     return d->token;
0175 }
0176 
0177 Tp::ChannelTextMessageType Message::type() const
0178 {
0179     return d->messageType;
0180 }
0181 
0182 QString Message::senderAlias() const
0183 {
0184     if (d->sender) {
0185         return d->sender->alias();
0186     }
0187     return d->senderAlias;
0188 }
0189 
0190 QString Message::senderId() const
0191 {
0192     if (d->sender) {
0193         return d->sender->id();
0194     }
0195     return d->senderId;
0196 }
0197 
0198 KTp::ContactPtr Message::sender() const
0199 {
0200     return d->sender;
0201 }
0202 
0203 int Message::partsSize() const
0204 {
0205     return d->parts.size();
0206 }
0207 
0208 bool Message::isHistory() const
0209 {
0210     return d->isHistory;
0211 }
0212 
0213 KTp::Message::MessageDirection Message::direction() const
0214 {
0215     return d->direction;
0216 }
0217 
0218 bool KTp::Message::operator==(const KTp::Message &other) const
0219 {
0220     return mainMessagePart() == other.mainMessagePart()
0221         && time() == other.time()
0222         && senderId() == other.senderId();
0223 }