~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/meanwhile/meanwhilesession.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    meanwhilesession.h - interface to the 'C' meanwhile session
 
3
 
 
4
    Copyright (c) 2005      by Jeremy Kerr <jk@ozlabs.org>
 
5
 
 
6
    *************************************************************************
 
7
    *                                                                       *
 
8
    * This program is free software; you can redistribute it and/or modify  *
 
9
    * it under the terms of the GNU General Public License as published by  *
 
10
    * the Free Software Foundation; either version 2 of the License, or     *
 
11
    * (at your option) any later version.                                   *
 
12
    *                                                                       *
 
13
    *************************************************************************
 
14
*/
 
15
#ifndef MEANWHILESESSION_H
 
16
#define MEANWHILESESSION_H
 
17
 
 
18
#include "meanwhileaccount.h"
 
19
#include "meanwhilecontact.h"
 
20
 
 
21
#include <meanwhile/mw_session.h>
 
22
#include <meanwhile/mw_service.h>
 
23
#include <meanwhile/mw_srvc_aware.h>
 
24
#include <meanwhile/mw_srvc_im.h>
 
25
#include <meanwhile/mw_srvc_resolve.h>
 
26
//Added by qt3to4:
 
27
#include <Q3ValueList>
 
28
 
 
29
class QTcpSocket;
 
30
 
 
31
struct MeanwhileClientID {
 
32
    int         id;
 
33
    const char *name;
 
34
};
 
35
 
 
36
 
 
37
/**
 
38
 * A class to handle libmeanwhile session management.
 
39
 */
 
40
class MeanwhileSession : public QObject
 
41
{
 
42
    Q_OBJECT
 
43
 
 
44
public:
 
45
    /**
 
46
     * Create a session. By default, the session is not connected - you will
 
47
     * need to call login() to initiate the connection process.
 
48
     * @param account The account that the connection is for
 
49
     */
 
50
    MeanwhileSession(MeanwhileAccount *account);
 
51
 
 
52
    /**
 
53
     * Destroy the session
 
54
     */
 
55
    ~MeanwhileSession();
 
56
 
 
57
    /**
 
58
     * Connect to the server. This will open a socket and start login. Note that
 
59
     * the connection process is ascychronous - a loginDone() signal will be
 
60
     * emitted when successfully logged in.
 
61
     */
 
62
    void connect(QString password);
 
63
 
 
64
    /**
 
65
     * Disconnect from the server.
 
66
     */
 
67
    void disconnect();
 
68
 
 
69
    /**
 
70
     * Set our (the local contact's) online status. The internalStatus of the
 
71
     * state argument will be used to define the state message we send - it
 
72
     * should be one of the Status enum fields (and not Offline)
 
73
     * @param state the new state of the local user
 
74
     * @param msg a custom message to use, if required
 
75
     */
 
76
    void setStatus(Kopete::OnlineStatus status,
 
77
            const Kopete::StatusMessage &msg = Kopete::StatusMessage());
 
78
 
 
79
    /**
 
80
     * Add a single contact to be registered for status updates
 
81
     * @param contact The contact to register
 
82
     */
 
83
    void addContact(const Kopete::Contact *contact);
 
84
 
 
85
    /**
 
86
     * Add a list of contacts to be registered for status updates
 
87
     * @param contact The list of contacts to register
 
88
     */
 
89
    void addContacts(const QHash<QString, Kopete::Contact *> &contacts);
 
90
 
 
91
    /**
 
92
     * Send a message (with recipient specified).
 
93
     * @param message The message to send
 
94
     * @return non-zero if  the message could be sent
 
95
     */
 
96
    int sendMessage(Kopete::Message &message);
 
97
 
 
98
    /**
 
99
     * Send a typing notification to a contact
 
100
     * @param contact  The contact to notify
 
101
     * @param isTyping If true, the typing notification is set
 
102
     */
 
103
    void sendTyping(MeanwhileContact *contact, bool isTyping);
 
104
 
 
105
    /**
 
106
     * Determine if the session is connected to the server
 
107
     * @return true if the session is connected
 
108
     */
 
109
    bool isConnected();
 
110
 
 
111
    /**
 
112
     * Determine if the session is in the process of connecting to the server
 
113
     * @return true if the session is connecting
 
114
     */
 
115
    bool isConnecting();
 
116
 
 
117
    /**
 
118
     * Returns an array of well-known meanwhile client IDs
 
119
     */
 
120
    static const struct MeanwhileClientID *getClientIDs();
 
121
 
 
122
    /**
 
123
     * Get the default client ID parameters for kopete
 
124
     */
 
125
    static void getDefaultClientIDParams(int *clientID,
 
126
            int *verMajor, int *verMinor);
 
127
 
 
128
signals:
 
129
    /**
 
130
     * Emitted when the status of the connection changes
 
131
     * @param status The new status of the session
 
132
     */
 
133
    void sessionStateChange(Kopete::OnlineStatus status);
 
134
 
 
135
    /**
 
136
     * Emitted when a notification is received from the server, or other
 
137
     * out-of-band data (eg, the password is incorrect).
 
138
     * @param mesgString A description of the notification
 
139
     */
 
140
    void serverNotification(const QString &mesgString);
 
141
 
 
142
private:
 
143
    /** Main libmeanwhile session object */
 
144
    struct mwSession *session;
 
145
 
 
146
    /** Session handler */
 
147
    struct mwSessionHandler sessionHandler;
 
148
 
 
149
    /** Aware service */
 
150
    struct mwServiceAware *awareService;
 
151
 
 
152
    /** Aware handler */
 
153
    struct mwAwareHandler awareHandler;
 
154
 
 
155
    /** Aware List Handler */
 
156
    struct mwAwareListHandler awareListHandler;
 
157
 
 
158
    /** The aware list */
 
159
    struct mwAwareList *awareList;
 
160
 
 
161
    /** Aware service */
 
162
    struct mwServiceIm *imService;
 
163
 
 
164
    /** Aware handler */
 
165
    struct mwImHandler imHandler;
 
166
 
 
167
    /** Resolve service */
 
168
    struct mwServiceResolve *resolveService;
 
169
 
 
170
    /** Storage service, for contact list */
 
171
    struct mwServiceStorage *storageService;
 
172
 
 
173
    /** Last recorded meanwhile state */
 
174
    enum mwSessionState state;
 
175
 
 
176
    /** The kopete account that this library is for */
 
177
    MeanwhileAccount *account;
 
178
 
 
179
    /** socket to the server */
 
180
    QTcpSocket *socket;
 
181
 
 
182
    /* These structures are stored in the libmeanwhile 'ClientData' fields */
 
183
 
 
184
    /** Stored in the mwConversation struct */
 
185
    struct ConversationData {
 
186
        MeanwhileContact *contact;
 
187
        Kopete::ChatSession *chat;
 
188
        Q3ValueList<Kopete::Message> *queue;
 
189
    };
 
190
 
 
191
    /** (To be) stored in the mwConference struct */
 
192
    struct ConferenceData {
 
193
        Kopete::ChatSession *chatsession;
 
194
    };
 
195
 
 
196
    /**
 
197
     * Initialise the conversation data struct for a conversation, and store it 
 
198
     * in the meanwhile conversation object
 
199
     * @param conv        the meanwhile conversation object
 
200
     * @param contact     the contact that the conversation is with
 
201
     * @param createQueue whether a message queue is required for this
 
202
     *                    conversation
 
203
     * @return The created conversation data struct
 
204
     */
 
205
    struct ConversationData *createConversationData(
 
206
            struct mwConversation *conv, MeanwhileContact *contact,
 
207
            bool createQueue = false);
 
208
 
 
209
    /**
 
210
     * Get the contact for a conversation
 
211
     * @param conv the meanwhile conversation
 
212
     * @return the contact that this conversation is held with
 
213
     */
 
214
    MeanwhileContact *conversationContact(struct mwConversation *conv);
 
215
 
 
216
    /**
 
217
     * Convert a libmeanwhile-type status into one of the MeanwhileProtocol
 
218
     * statuses
 
219
     * @param mstatus The internal status to convert
 
220
     * @return The Meanwhile status
 
221
     */
 
222
    Kopete::OnlineStatus convertStatus(int mstatus);
 
223
 
 
224
    /**
 
225
     * Parse the nickname of a libmeanwhile contact. From what I've seen,
 
226
     * usernames are in the format:
 
227
     *  <userid> - <name>/<domain>/<domain>
 
228
     * @param name the extened username to parse
 
229
     * @return just the name part of the username info
 
230
     */
 
231
    QString getNickName(QString name);
 
232
 
 
233
    /**
 
234
     * Convenience method to call the above from a mwLoginInfo struct. All is
 
235
     * checked for null.
 
236
     * @param logininfo the login info for a contact
 
237
     * @return just the name part of the login info data
 
238
     */
 
239
    QString getNickName(struct mwLoginInfo *logininfo);
 
240
 
 
241
    /**
 
242
     * Resolve a contact to find (and set) the display name. This requires the
 
243
     * session to be connected to use the meanwhile resolve service.
 
244
     * @param contact The contact to resolve
 
245
     */
 
246
     void resolveContactNickname(MeanwhileContact *contact);
 
247
 
 
248
public:
 
249
    void syncContactsToServer();
 
250
    void syncContactsFromServer();
 
251
 
 
252
private slots:
 
253
 
 
254
    /** Notify the library that data is available on the socket */
 
255
    void slotSocketDataAvailable();
 
256
 
 
257
    /**
 
258
     * Notify the library that the socket is about to be closed
 
259
     */
 
260
    void slotSocketAboutToClose();
 
261
 
 
262
private:
 
263
    /* ugly callbacks for libmeanwhile interface. These declare a static method
 
264
     * to proxy the callback from libmeanwhile to a call to the MeanwhileSession
 
265
     */
 
266
 
 
267
#define declare_session_handler_type(type, func, args, ...)                    \
 
268
    static type _handleSession ## func (                                       \
 
269
            struct mwSession *mwsession, __VA_ARGS__) {                        \
 
270
        MeanwhileSession *session =                                            \
 
271
            (MeanwhileSession *)mwSession_getClientData(mwsession);            \
 
272
        return session->handleSession ## func args;                            \
 
273
    };                                                                         \
 
274
    type handleSession ## func(__VA_ARGS__)
 
275
#define declare_session_handler(func, args, ...)                               \
 
276
    static void _handleSession ## func (                                       \
 
277
            struct mwSession *mwsession, ## __VA_ARGS__) {                     \
 
278
        MeanwhileSession *session =                                            \
 
279
            (MeanwhileSession *)mwSession_getClientData(mwsession);            \
 
280
        session->handleSession ## func args;                                   \
 
281
    };                                                                         \
 
282
    void handleSession ## func(__VA_ARGS__)
 
283
 
 
284
    declare_session_handler_type(int, IOWrite, (buf, len),
 
285
            const guchar *buf, gsize len);
 
286
    declare_session_handler(IOClose,());
 
287
    declare_session_handler(Clear,());
 
288
    declare_session_handler(StateChange, (state, info),
 
289
            enum mwSessionState state, gpointer info);
 
290
    declare_session_handler(SetPrivacyInfo,());
 
291
    declare_session_handler(SetUserStatus,());
 
292
    declare_session_handler(Admin, (text), const char *text);
 
293
    declare_session_handler(Announce, (from, may_reply, text),
 
294
            struct mwLoginInfo *from, gboolean may_reply, const char *text);
 
295
 
 
296
#define declare_aware_handler(func, args, ...)                                 \
 
297
    static void _handleAware ## func (                                         \
 
298
            struct mwServiceAware *srvc, ## __VA_ARGS__) {                     \
 
299
        MeanwhileSession *session = (MeanwhileSession *)                       \
 
300
            mwService_getClientData((struct mwService *)srvc);                 \
 
301
        return session->handleAware ## func args;                              \
 
302
    }; \
 
303
    void handleAware ## func(__VA_ARGS__)
 
304
 
 
305
    declare_aware_handler(Attrib, (attrib), struct mwAwareAttribute *attrib);
 
306
    declare_aware_handler(Clear,());
 
307
 
 
308
#define declare_aware_list_handler(func, args, ...)                            \
 
309
    static void _handleAwareList ## func (                                     \
 
310
            struct mwAwareList *list, ## __VA_ARGS__){                         \
 
311
        MeanwhileSession *session = (MeanwhileSession *)                       \
 
312
            mwAwareList_getClientData(list);                                   \
 
313
        return session->handleAwareList ## func args;                          \
 
314
    }; \
 
315
    void handleAwareList ## func(__VA_ARGS__)
 
316
 
 
317
    declare_aware_list_handler(Aware, (snapshot),
 
318
            struct mwAwareSnapshot *snapshot);
 
319
    declare_aware_list_handler(Attrib, (id, attrib),
 
320
            struct mwAwareIdBlock *id, struct mwAwareAttribute *attrib);
 
321
    declare_aware_list_handler(Clear,());
 
322
 
 
323
#define declare_im_handler(func, args, ...)                                    \
 
324
    static void _handleIm ## func (                                            \
 
325
            struct mwConversation *conv, ## __VA_ARGS__) {                     \
 
326
        MeanwhileSession *session = (MeanwhileSession *)                       \
 
327
            mwService_getClientData(                                           \
 
328
                (struct mwService *)mwConversation_getService(conv));          \
 
329
        return session->handleIm ## func args;                                 \
 
330
    }; \
 
331
    void handleIm ## func (struct mwConversation *conv, ## __VA_ARGS__)
 
332
 
 
333
    declare_im_handler(ConvOpened, (conv));
 
334
    declare_im_handler(ConvClosed, (conv, err), guint32 err);
 
335
    declare_im_handler(ConvReceived, (conv, type, msg),
 
336
            enum mwImSendType type, gconstpointer msg);
 
337
 
 
338
    /* resolve service */
 
339
    static void _handleResolveLookupResults(struct mwServiceResolve *srvc,
 
340
            guint32 id, guint32 code, GList *results, gpointer data) {
 
341
        MeanwhileSession *session = (MeanwhileSession *)
 
342
            mwService_getClientData(MW_SERVICE(srvc));
 
343
        session->handleResolveLookupResults(srvc, id, code, results, data);
 
344
    };
 
345
    void handleResolveLookupResults(struct mwServiceResolve *srvc, guint32 id,
 
346
            guint32 code, GList *results, gpointer data);
 
347
 
 
348
    /* storage service */
 
349
    static void _handleStorageLoad(struct mwServiceStorage *srvc,
 
350
            guint32 result, struct mwStorageUnit *item, gpointer data) {
 
351
        MeanwhileSession *session = (MeanwhileSession *)
 
352
            mwService_getClientData(MW_SERVICE(srvc));
 
353
        session->handleStorageLoad(srvc, result, item, data);
 
354
    };
 
355
    void handleStorageLoad(struct mwServiceStorage *srvc,
 
356
            guint32 result, struct mwStorageUnit *item, gpointer data);
 
357
 
 
358
    void handleRedirect(const char *host);
 
359
};
 
360
 
 
361
#endif
 
362