summaryrefslogtreecommitdiff
path: root/TelepathyQt/contact-messenger.cpp
blob: 2c13a8455e4b0a4ceac746f85109c5b1b194ccad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2011 Nokia Corporation
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt/ContactMessenger>

#include "TelepathyQt/_gen/contact-messenger.moc.hpp"

#include "TelepathyQt/debug-internal.h"

#include "TelepathyQt/future-internal.h"

#include <TelepathyQt/Account>
#include <TelepathyQt/ChannelDispatcher>
#include <TelepathyQt/ClientRegistrar>
#include <TelepathyQt/MessageContentPartList>
#include <TelepathyQt/PendingSendMessage>
#include <TelepathyQt/SimpleTextObserver>
#include <TelepathyQt/TextChannel>

namespace Tp
{

struct TP_QT_NO_EXPORT ContactMessenger::Private
{
    Private(ContactMessenger *parent, const AccountPtr &account, const QString &contactIdentifier)
        : parent(parent),
          account(account),
          contactIdentifier(contactIdentifier),
          cdMessagesInterface(nullptr)
    {
    }

    PendingSendMessage *sendMessage(const Message &message, MessageSendingFlags flags);

    ContactMessenger *parent;
    AccountPtr account;
    QString contactIdentifier;
    SimpleTextObserverPtr observer;
    Tp::Client::ChannelDispatcherInterfaceMessages1Interface *cdMessagesInterface;
};

PendingSendMessage *ContactMessenger::Private::sendMessage(const Message &message,
        MessageSendingFlags flags)
{
    if (!cdMessagesInterface) {
        cdMessagesInterface = new Tp::Client::ChannelDispatcherInterfaceMessages1Interface(
                account->dbusConnection(),
                TP_QT_CHANNEL_DISPATCHER_BUS_NAME, TP_QT_CHANNEL_DISPATCHER_OBJECT_PATH, parent);
    }

    PendingSendMessage *op = new PendingSendMessage(ContactMessengerPtr(parent), message);

    Tp::MessagePartList parts;
    foreach (const Tp::MessagePart &part, message.parts()) {
        parts << static_cast<QMap<QString, QDBusVariant> >(part);
    }

    connect(new QDBusPendingCallWatcher(
                cdMessagesInterface->SendMessage(QDBusObjectPath(account->objectPath()),
                    contactIdentifier, parts, (uint) flags)),
            SIGNAL(finished(QDBusPendingCallWatcher*)),
            op,
            SLOT(onCDMessageSent(QDBusPendingCallWatcher*)));
    return op;
}

/**
 * \class ContactMessenger
 * \ingroup clientaccount
 * \headerfile TelepathyQt/contact-messenger.h <TelepathyQt/ContactMessenger>
 *
 * \brief The ContactMessenger class provides an easy way to send text messages to a contact
 *        and also track sent/receive text messages from the same contact.
 */

/**
 * Create a new ContactMessenger object.
 *
 * \param account The account this messenger is communicating with.
 * \param contact The contact this messenger is communicating with.
 * \return An ContactMessengerPtr object pointing to the newly created ContactMessenger object,
 *         or a null ContactMessengerPtr if \a contact is null.
 */
ContactMessengerPtr ContactMessenger::create(const AccountPtr &account,
        const ContactPtr &contact)
{
    if (!contact) {
        warning() << "Contact used to create a ContactMessenger object must be "
            "valid";
        return ContactMessengerPtr();
    }
    return ContactMessengerPtr(new ContactMessenger(account, contact->id()));
}

/**
 * Create a new ContactMessenger object.
 *
 * \param account The account this messenger is communicating with.
 * \param contactIdentifier The identifier of the contact this messenger is communicating with.
 * \return An ContactMessengerPtr object pointing to the newly created ContactMessenger object,
 *         or a null ContactMessengerPtr if \a contact is null.
 */
ContactMessengerPtr ContactMessenger::create(const AccountPtr &account,
        const QString &contactIdentifier)
{
    if (contactIdentifier.isEmpty()) {
        warning() << "Contact identifier used to create a ContactMessenger object must be "
            "non-empty";
        return ContactMessengerPtr();
    }
    return ContactMessengerPtr(new ContactMessenger(account, contactIdentifier));
}

/**
 * Construct a new ContactMessenger object.
 *
 * \param account The account this messenger is communicating with.
 * \param contactIdentifier The identifier of the contact this messenger is communicating with.
 */
ContactMessenger::ContactMessenger(const AccountPtr &account, const QString &contactIdentifier)
    : mPriv(new Private(this, account, contactIdentifier))
{
    mPriv->observer = SimpleTextObserver::create(account, contactIdentifier);
    connect(mPriv->observer.data(),
            SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString,Tp::TextChannelPtr)),
            SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString,Tp::TextChannelPtr)));
    connect(mPriv->observer.data(),
            SIGNAL(messageReceived(Tp::ReceivedMessage,Tp::TextChannelPtr)),
            SIGNAL(messageReceived(Tp::ReceivedMessage,Tp::TextChannelPtr)));
}

/**
 * Class destructor.
 */
ContactMessenger::~ContactMessenger()
{
    delete mPriv;
}

/**
 * Return the account this messenger is communicating with.
 *
 * \return A pointer to the Account object.
 */
AccountPtr ContactMessenger::account() const
{
    return mPriv->account;
}

/**
 * Return the identifier of the contact this messenger is communicating with.
 *
 * \return The identifier of the contact.
 */
QString ContactMessenger::contactIdentifier() const
{
    return mPriv->contactIdentifier;
}

/**
 * Return the list of text chats currently being observed.
 *
 * \return A list of pointers to TextChannel objects.
 */
QList<TextChannelPtr> ContactMessenger::textChats() const
{
    return mPriv->observer->textChats();
}

/**
 * Send a message to the contact identified by contactIdentifier() using account().
 *
 * Note that the return from this method isn't ordered in any sane way, meaning that
 * messageSent() can be signalled either before or after the returned PendingSendMessage object
 * finishes.
 *
 * \param text The message text.
 * \param type The message type.
 * \param flags The message flags.
 * \return A PendingSendMessage which will emit PendingSendMessage::finished
 *         once the reply is received and that can be used to check whether sending the
 *         message succeeded or not.
 */
PendingSendMessage *ContactMessenger::sendMessage(const QString &text,
        ChannelTextMessageType type,
        MessageSendingFlags flags)
{
    Message message(type, text);
    return mPriv->sendMessage(message, flags);
}

/**
 * Send a message to the contact identified by contactIdentifier() using account().
 *
 * Note that the return from this method isn't ordered in any sane way, meaning that
 * messageSent() can be signalled either before or after the returned PendingSendMessage object
 * finishes.
 *
 * \param parts The message parts.
 * \param flags The message flags.
 * \return A PendingSendMessage which will emit PendingSendMessage::finished
 *         once the reply is received and that can be used to check whether sending the
 *         message succeeded or not.
 */
PendingSendMessage *ContactMessenger::sendMessage(const MessageContentPartList &parts,
        MessageSendingFlags flags)
{
    Message message(parts.bareParts());
    return mPriv->sendMessage(message, flags);
}

/**
 * \fn void ContactMessenger::messageSent(const Tp::Message &message,
 *                  Tp::MessageSendingFlags flags, const QString &sentMessageToken,
 *                  const Tp::TextChannelPtr &channel);
 *
 * Emitted whenever a text message on account() is sent to the contact
 * identified by contactIdentifier().
 *
 * \param message The message sent.
 * \param flags The flags of the message that was sent.
 * \param sentMessageToken The token of the message that was sent.
 * \param channel The channel from which the message was sent.
 */

/**
 * \fn void ContactMessenger::messageReceived(const Tp::ReceivedMessage &message,
 *                  const Tp::TextChannelPtr &channel);
 *
 * Emitted whenever a text message on account() is received from the contact
 * identified by contactIdentifier().
 *
 * \param message The message received.
 * \param channel The channel from which the message was received.
 */

} // Tp