1 /*
2 Copyright (C) 2012 Lasath Fernando <kde@lasath.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19
20 #include "message.h"
21 #include "message-private.h"
22
23 #include "ktp-debug.h"
24 #include <QSharedData>
25
26 #include <TelepathyQt/ContactManager>
27 #include <TelepathyQt/Connection>
28
29 using namespace KTp;
30
operator =(const Message & other)31 Message& Message::operator=(const Message &other) {
32 d = other.d;
33 return *this;
34 }
35
Message(Message::Private * dd)36 Message::Message(Message::Private *dd):
37 d(dd)
38 {
39 }
40
Message(const Tp::Message & original,const KTp::MessageContext & context)41 Message::Message(const Tp::Message &original, const KTp::MessageContext &context) :
42 d(new Private)
43 {
44 d->sentTime = original.sent();
45 d->token = original.messageToken();
46 d->messageType = original.messageType();
47 d->isHistory = false;
48 d->direction = KTp::Message::LocalToRemote;
49
50 setMainMessagePart(original.text());
51
52 if (context.account()->connection()) {
53 d->sender = KTp::ContactPtr::qObjectCast(context.account()->connection()->selfContact());
54 } else {
55 d->senderAlias = context.account()->nickname();
56 d->senderId = context.account()->uniqueIdentifier();
57 }
58 }
59
Message(const Tp::ReceivedMessage & original,const KTp::MessageContext & context)60 Message::Message(const Tp::ReceivedMessage &original, const KTp::MessageContext &context) :
61 d(new Private)
62 {
63 Q_UNUSED(context)
64
65 d->sentTime = original.sent();
66 if (d->sentTime.isNull()) {
67 d->sentTime = original.received();
68 }
69
70 d->token = original.messageToken();
71 d->messageType = original.messageType();
72 d->isHistory = original.isScrollback();
73
74 setMainMessagePart(original.text());
75
76 if (!original.sender().isNull()) {
77 d->sender = KTp::ContactPtr::qObjectCast(original.sender());
78 } else {
79 d->senderAlias = original.senderNickname();
80 }
81
82 bool isLocalToRemote = false;
83
84 if (!d->sender.isNull()) {
85 if (context.channel()->interfaces().contains(TP_QT_IFACE_CHANNEL_INTERFACE_GROUP)) {
86 isLocalToRemote = d->sender->id() == context.channel()->groupSelfContact()->id();
87 } else {
88 isLocalToRemote = d->sender->id() == context.channel()->connection()->selfContact()->id();
89 }
90 }
91
92 if (isLocalToRemote) {
93 d->direction = KTp::Message::LocalToRemote;
94 } else {
95 d->direction = KTp::Message::RemoteToLocal;
96 }
97 }
98
Message(const Message & other)99 Message::Message(const Message& other):
100 d(other.d)
101 {
102 }
103
~Message()104 Message::~Message()
105 {
106 }
107
mainMessagePart() const108 QString Message::mainMessagePart() const
109 {
110 return d->mainPart;
111 }
112
setMainMessagePart(const QString & message)113 void Message::setMainMessagePart(const QString& message)
114 {
115 d->mainPart = message;
116 }
117
appendMessagePart(const QString & part)118 void Message::appendMessagePart(const QString& part)
119 {
120 d->parts << part;
121 }
122
appendScript(const QString & script)123 void Message::appendScript(const QString& script)
124 {
125 // Append the script only if it is not already appended to avoid multiple
126 // execution of the scripts.
127 if (!d->scripts.contains(script)) {
128 d->scripts << script;
129 }
130 }
131
finalizedMessage() const132 QString Message::finalizedMessage() const
133 {
134 QString msg = d->mainPart + QLatin1Char('\n') +
135 d->parts.join(QLatin1String("\n"));
136
137 // qCDebug(KTP_COMMONINTERNALS) << msg;
138 return msg;
139 }
140
finalizedScript() const141 QString Message::finalizedScript() const
142 {
143 if (d->scripts.empty()) {
144 return QString();
145 }
146
147 QString finalScript = d->scripts.join(QString());
148
149 if (!finalScript.isEmpty()) {
150 finalScript.append(QLatin1String("false;"));
151 }
152
153 // qCDebug(KTP_COMMONINTERNALS) << finalScript;
154 return finalScript;
155 }
156
property(const char * name) const157 QVariant Message::property(const char *name) const
158 {
159 return d->properties[QLatin1String(name)];
160 }
161
setProperty(const char * name,const QVariant & value)162 void Message::setProperty(const char *name, const QVariant& value)
163 {
164 d->properties[QLatin1String(name)] = value;
165 }
166
time() const167 QDateTime Message::time() const
168 {
169 return d->sentTime;
170 }
171
token() const172 QString Message::token() const
173 {
174 return d->token;
175 }
176
type() const177 Tp::ChannelTextMessageType Message::type() const
178 {
179 return d->messageType;
180 }
181
senderAlias() const182 QString Message::senderAlias() const
183 {
184 if (d->sender) {
185 return d->sender->alias();
186 }
187 return d->senderAlias;
188 }
189
senderId() const190 QString Message::senderId() const
191 {
192 if (d->sender) {
193 return d->sender->id();
194 }
195 return d->senderId;
196 }
197
sender() const198 KTp::ContactPtr Message::sender() const
199 {
200 return d->sender;
201 }
202
partsSize() const203 int Message::partsSize() const
204 {
205 return d->parts.size();
206 }
207
isHistory() const208 bool Message::isHistory() const
209 {
210 return d->isHistory;
211 }
212
direction() const213 KTp::Message::MessageDirection Message::direction() const
214 {
215 return d->direction;
216 }
217
operator ==(const KTp::Message & other) const218 bool KTp::Message::operator==(const KTp::Message &other) const
219 {
220 return mainMessagePart() == other.mainMessagePart()
221 && time() == other.time()
222 && senderId() == other.senderId();
223 }
224