• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
message.cpp
Go to the documentation of this file.
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 
22 #include <KDebug>
23 #include <QSharedData>
24 
25 #include <TelepathyQt/ContactManager>
26 #include <TelepathyQt/Connection>
27 
28 #include <TelepathyLoggerQt4/Entity>
29 
30 using namespace KTp;
31 
32 class Message::Private : public QSharedData {
33 
34  public:
35  Private() :
36  isHistory(false)
37  {}
38 
39  QDateTime sentTime;
40  QString token;
41  Tp::ChannelTextMessageType messageType;
42  QVariantMap properties;
43  QString mainPart;
44  QStringList parts;
45  QStringList scripts;
46  bool isHistory;
47  MessageDirection direction;
48 };
49 
50 Message& Message::operator=(const Message &other) {
51  d = other.d;
52  return *this;
53 }
54 
55 Message::Message(const Tp::Message &original, const KTp::MessageContext &context) :
56  d(new Private)
57 {
58  Q_UNUSED(context)
59  d->sentTime = original.sent();
60  d->token = original.messageToken();
61  d->messageType = original.messageType();
62  d->isHistory = false;
63  d->direction = KTp::Message::LocalToRemote;
64 
65  setMainMessagePart(original.text());
66 
67  setProperty("senderName", context.account()->nickname());
68  setProperty("senderAvatar", context.account()->avatar().avatarData);
69  setProperty("senderId",
70  context.account()->connection()->selfContact()->id());
71 }
72 
73 Message::Message(const Tp::ReceivedMessage &original, const KTp::MessageContext &context) :
74  d(new Private)
75 {
76  Q_UNUSED(context)
77 
78  d->sentTime = original.sent();
79  if (d->sentTime.isNull()) {
80  d->sentTime = original.received();
81  }
82 
83  d->token = original.messageToken();
84  d->messageType = original.messageType();
85  d->isHistory = original.isScrollback();
86  d->direction = KTp::Message::RemoteToLocal;
87 
88  setMainMessagePart(original.text());
89 
90  if (!original.sender().isNull()) {
91  setProperty("senderName", original.sender()->alias());
92  setProperty("senderAvatar", original.sender()->avatarData().fileName);
93  setProperty("senderId", original.sender()->id());
94  } else {
95  setProperty("senderName", original.senderNickname());
96  }
97 }
98 
99 Message::Message(const Tpl::TextEventPtr &original, const KTp::MessageContext &context) :
100  d(new Private)
101 {
102  d->sentTime = original->timestamp();
103  d->token = original->messageToken();
104  d->messageType = original->messageType();
105  d->isHistory = true;
106 
107  setProperty("senderName", original->sender()->alias());
108  setProperty("senderId", original->sender()->identifier());
109 
110  if (context.account() && context.account()->connection() && context.channel()) {
111  if (original->sender()->identifier() == context.account()->normalizedName()) {
112  d->direction = KTp::Message::LocalToRemote;
113  setProperty("senderAvatar", context.account()->
114  connection()->selfContact()->avatarData().fileName);
115  } else {
116  d->direction = KTp::Message::RemoteToLocal;
117  setProperty("senderAvatar",
118  context.channel()->targetContact()->avatarData().fileName);
119  }
120  }
121 
122  setMainMessagePart(original->message());
123 }
124 
125 Message::Message(const QString &messageText, const MessageContext &context) :
126  d(new Private)
127 {
128  d->sentTime = QDateTime::currentDateTime();
129  d->messageType = Tp::ChannelTextMessageTypeNormal;
130  d->direction = LocalToRemote;
131  d->isHistory = false;
132 
133  setProperty("senderName", context.account()->nickname());
134  setProperty("senderId", context.account()->
135  connection()->selfContact()->id());
136  setProperty("senderAvatar", context.account()->
137  connection()->selfContact()->avatarData().fileName);
138 
139  setMainMessagePart(messageText);
140 }
141 
142 Message::Message(const Message& other):
143  d(other.d)
144 {
145 }
146 
147 Message::~Message()
148 {
149 }
150 
151 QString Message::mainMessagePart() const
152 {
153  return d->mainPart;
154 }
155 
156 void Message::setMainMessagePart(const QString& message)
157 {
158  d->mainPart = message;
159 }
160 
161 void Message::appendMessagePart(const QString& part)
162 {
163  d->parts << part;
164 }
165 
166 void Message::appendScript(const QString& script)
167 {
168  // Append the script only if it is not already appended to avoid multiple
169  // execution of the scripts.
170  if (!d->scripts.contains(script)) {
171  d->scripts << script;
172  }
173 }
174 
175 QString Message::finalizedMessage() const
176 {
177  QString msg = d->mainPart + QLatin1String("\n") +
178  d->parts.join(QLatin1String("\n"));
179 
180 // kDebug() << msg;
181  return msg;
182 }
183 
184 QString Message::finalizedScript() const
185 {
186  if (d->scripts.empty()) {
187  return QString();
188  }
189 
190  QString finalScript = d->scripts.join(QLatin1String(""));
191 
192  if (!finalScript.isEmpty()) {
193  finalScript.append(QLatin1String("false;"));
194  }
195 
196 // kDebug() << finalScript;
197  return finalScript;
198 }
199 
200 QVariant Message::property(const char *name) const
201 {
202  return d->properties[QLatin1String(name)];
203 }
204 
205 void Message::setProperty(const char *name, const QVariant& value)
206 {
207  d->properties[QLatin1String(name)] = value;
208 }
209 
210 QDateTime Message::time() const
211 {
212  return d->sentTime;
213 }
214 
215 QString Message::token() const
216 {
217  return d->token;
218 }
219 
220 Tp::ChannelTextMessageType Message::type() const
221 {
222  return d->messageType;
223 }
224 
225 QString Message::senderAlias() const
226 {
227  return property("senderName").toString();
228 }
229 
230 QString Message::senderId() const
231 {
232  return property("senderId").toString();
233 }
234 
235 int Message::partsSize() const
236 {
237  return d->parts.size();
238 }
239 
240 bool Message::isHistory() const
241 {
242  return d->isHistory;
243 }
244 
245 KTp::Message::MessageDirection Message::direction() const
246 {
247  return d->direction;
248 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal