23 #include <KLocalizedString>
25 #include <TelepathyQt/ReceivedMessage>
26 #include <TelepathyQt/TextChannel>
27 #include <TelepathyQt/Account>
31 class MessagesModel::MessagesModelPrivate
35 Tp::AccountPtr account;
36 QList<KTp::Message> messages;
42 d(new MessagesModelPrivate)
46 QHash<int, QByteArray> roles;
59 return d->textChannel;
62 bool MessagesModel::verifyPendingOperation(Tp::PendingOperation *op)
64 bool operationSucceeded =
true;
67 kWarning() << op->errorName() <<
"+" << op->errorMessage();
68 operationSucceeded =
false;
71 return operationSucceeded;
74 void MessagesModel::setupChannelSignals(
const Tp::TextChannelPtr &channel)
76 connect(channel.data(),
77 SIGNAL(messageReceived(Tp::ReceivedMessage)),
78 SLOT(onMessageReceived(Tp::ReceivedMessage)));
79 connect(channel.data(),
80 SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
81 SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString)));
82 connect(channel.data(),
83 SIGNAL(pendingMessageRemoved(Tp::ReceivedMessage)),
84 SLOT(onPendingMessageRemoved()));
90 setupChannelSignals(channel);
93 removeChannelSignals(d->textChannel);
96 d->textChannel = channel;
98 QList<Tp::ReceivedMessage> messageQueue = channel->messageQueue();
99 Q_FOREACH(
const Tp::ReceivedMessage &message, messageQueue) {
100 bool messageAlreadyInModel =
false;
104 if (current.
token() == message.messageToken()) {
105 messageAlreadyInModel =
true;
109 if (!messageAlreadyInModel) {
110 onMessageReceived(message);
115 void MessagesModel::onMessageReceived(
const Tp::ReceivedMessage &message)
117 int unreadCount = d->textChannel->messageQueue().size();
118 kDebug() <<
"unreadMessagesCount =" <<
unreadCount;
119 kDebug() <<
"text =" << message.text();
120 kDebug() <<
"messageToken =" << message.messageToken();
122 if (message.messageType() == Tp::ChannelTextMessageTypeNormal ||
123 message.messageType() == Tp::ChannelTextMessageTypeAction) {
126 beginInsertRows(QModelIndex(), length, length);
130 message, d->account, d->textChannel));
144 void MessagesModel::onMessageSent(
const Tp::Message &message, Tp::MessageSendingFlags flags,
const QString &messageToken)
147 Q_UNUSED(messageToken);
150 beginInsertRows(QModelIndex(), length, length);
151 kDebug() <<
"text =" << message.text();
154 message, d->account, d->textChannel));
159 void MessagesModel::onPendingMessageRemoved()
168 if (index.isValid()) {
179 if (requestedData.
type() == Tp::ChannelTextMessageTypeAction) {
190 result = requestedData.
time();
194 kError() <<
"Attempting to access data at invalid index (" << index <<
")";
203 return d->messages.size();
208 if (message.isEmpty()) {
209 kWarning() <<
"Attempting to send empty string";
211 Tp::PendingOperation *op;
212 QString modifiedMessage = message;
213 if (d->textChannel->supportsMessageType(Tp::ChannelTextMessageTypeAction)
214 && modifiedMessage.startsWith(QLatin1String(
"/me "))) {
216 modifiedMessage.remove(0,4);
217 op = d->textChannel->send(modifiedMessage, Tp::ChannelTextMessageTypeAction);
219 op = d->textChannel->send(modifiedMessage);
222 SIGNAL(finished(Tp::PendingOperation*)),
223 SLOT(verifyPendingOperation(Tp::PendingOperation*)));
227 void MessagesModel::removeChannelSignals(
const Tp::TextChannelPtr &channel)
229 QObject::disconnect(channel.data(),
230 SIGNAL(messageReceived(Tp::ReceivedMessage)),
232 SLOT(onMessageReceived(Tp::ReceivedMessage))
234 QObject::disconnect(channel.data(),
235 SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
237 SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString))
243 return d->textChannel->messageQueue().size();
248 QList<Tp::ReceivedMessage> queue
249 = d->textChannel->messageQueue();
251 kDebug() <<
"Conversation Visible, Acknowledging " << queue.size() <<
" messages.";
253 d->textChannel->acknowledge(queue);
258 void MessagesModel::selfDequeued()
267 if (d->visible != visible) {
268 d->visible = visible;
290 return d->textChannel->isRequested();