~liuxingcs/+junk/pidgin

« back to all changes in this revision

Viewing changes to libpurple/protocols/msn/transaction.c

  • Committer: liuxing
  • Date: 2013-04-25 11:10:17 UTC
  • Revision ID: liuxingcs@yeah.net-20130425111017-fm4mtfsuvhq0dbqd
pidgin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file transaction.c MSN transaction functions
 
3
 *
 
4
 * purple
 
5
 *
 
6
 * Purple is the legal property of its developers, whose names are too numerous
 
7
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 
8
 * source distribution.
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
23
 */
 
24
 
 
25
#include "internal.h"
 
26
#include "debug.h"
 
27
 
 
28
#include "msn.h"
 
29
#include "transaction.h"
 
30
 
 
31
MsnTransaction *
 
32
msn_transaction_new(MsnCmdProc *cmdproc, const char *command,
 
33
                                        const char *format, ...)
 
34
{
 
35
        MsnTransaction *trans;
 
36
        va_list arg;
 
37
 
 
38
        g_return_val_if_fail(command != NULL, NULL);
 
39
 
 
40
        trans = g_new0(MsnTransaction, 1);
 
41
 
 
42
        trans->cmdproc = cmdproc;
 
43
        trans->command = g_strdup(command);
 
44
        trans->saveable = TRUE;
 
45
 
 
46
        if (format != NULL)
 
47
        {
 
48
                va_start(arg, format);
 
49
                trans->params = g_strdup_vprintf(format, arg);
 
50
                va_end(arg);
 
51
        }
 
52
 
 
53
        /* trans->queue = g_queue_new(); */
 
54
 
 
55
        return trans;
 
56
}
 
57
 
 
58
void
 
59
msn_transaction_destroy(MsnTransaction *trans)
 
60
{
 
61
        g_return_if_fail(trans != NULL);
 
62
 
 
63
        g_free(trans->command);
 
64
        g_free(trans->params);
 
65
        g_free(trans->payload);
 
66
 
 
67
        if (trans->data_free)
 
68
                trans->data_free(trans->data);
 
69
 
 
70
#if 0
 
71
        if (trans->pendent_cmd != NULL)
 
72
                msn_message_unref(trans->pendent_msg);
 
73
#endif
 
74
 
 
75
#if 0
 
76
        MsnTransaction *elem;
 
77
        if (trans->queue != NULL)
 
78
        {
 
79
                while ((elem = g_queue_pop_head(trans->queue)) != NULL)
 
80
                        msn_transaction_destroy(elem);
 
81
 
 
82
                g_queue_free(trans->queue);
 
83
        }
 
84
#endif
 
85
 
 
86
        if (trans->callbacks != NULL && trans->has_custom_callbacks)
 
87
                g_hash_table_destroy(trans->callbacks);
 
88
 
 
89
        if (trans->timer)
 
90
                purple_timeout_remove(trans->timer);
 
91
 
 
92
        g_free(trans);
 
93
}
 
94
 
 
95
char *
 
96
msn_transaction_to_string(MsnTransaction *trans)
 
97
{
 
98
        char *str;
 
99
 
 
100
        g_return_val_if_fail(trans != NULL, FALSE);
 
101
 
 
102
        if (trans->params != NULL)
 
103
                str = g_strdup_printf("%s %u %s\r\n", trans->command, trans->trId, trans->params);
 
104
        else if (trans->saveable)
 
105
                str = g_strdup_printf("%s %u\r\n", trans->command, trans->trId);
 
106
        else
 
107
                str = g_strdup_printf("%s\r\n", trans->command);
 
108
 
 
109
        return str;
 
110
}
 
111
 
 
112
void
 
113
msn_transaction_queue_cmd(MsnTransaction *trans, MsnCommand *cmd)
 
114
{
 
115
        purple_debug_info("msn", "queueing command.\n");
 
116
        trans->pendent_cmd = cmd;
 
117
        msn_command_ref(cmd);
 
118
}
 
119
 
 
120
void
 
121
msn_transaction_unqueue_cmd(MsnTransaction *trans, MsnCmdProc *cmdproc)
 
122
{
 
123
        MsnCommand *cmd;
 
124
 
 
125
        if (!cmdproc->servconn->connected)
 
126
                return;
 
127
 
 
128
        purple_debug_info("msn", "unqueueing command.\n");
 
129
        cmd = trans->pendent_cmd;
 
130
 
 
131
        g_return_if_fail(cmd != NULL);
 
132
 
 
133
        msn_cmdproc_process_cmd(cmdproc, cmd);
 
134
        msn_command_unref(cmd);
 
135
 
 
136
        trans->pendent_cmd = NULL;
 
137
}
 
138
 
 
139
#if 0
 
140
void
 
141
msn_transaction_queue(MsnTransaction *trans, MsnTransaction *elem)
 
142
{
 
143
        if (trans->queue == NULL)
 
144
                trans->queue = g_queue_new();
 
145
 
 
146
        g_queue_push_tail(trans->queue, elem);
 
147
}
 
148
 
 
149
void
 
150
msn_transaction_unqueue(MsnTransaction *trans, MsnCmdProc *cmdproc)
 
151
{
 
152
        MsnTransaction *elem;
 
153
 
 
154
        while ((elem = g_queue_pop_head(trans->queue)) != NULL)
 
155
                msn_cmdproc_send_trans(cmdproc, elem);
 
156
}
 
157
#endif
 
158
 
 
159
void
 
160
msn_transaction_set_payload(MsnTransaction *trans,
 
161
                                                        const char *payload, int payload_len)
 
162
{
 
163
        g_return_if_fail(trans   != NULL);
 
164
        g_return_if_fail(payload != NULL);
 
165
 
 
166
        trans->payload = g_strdup(payload);
 
167
        trans->payload_len = payload_len ? payload_len : strlen(trans->payload);
 
168
}
 
169
 
 
170
void
 
171
msn_transaction_set_data(MsnTransaction *trans, void *data)
 
172
{
 
173
        g_return_if_fail(trans != NULL);
 
174
 
 
175
        trans->data = data;
 
176
}
 
177
 
 
178
void msn_transaction_set_data_free(MsnTransaction *trans, GDestroyNotify fn)
 
179
{
 
180
        g_return_if_fail(trans != NULL);
 
181
        trans->data_free = fn;
 
182
}
 
183
 
 
184
void
 
185
msn_transaction_set_saveable(MsnTransaction  *trans, gboolean saveable)
 
186
{
 
187
        g_return_if_fail(trans != NULL);
 
188
 
 
189
        trans->saveable = saveable;
 
190
}
 
191
 
 
192
void
 
193
msn_transaction_add_cb(MsnTransaction *trans, char *answer,
 
194
                                           MsnTransCb cb)
 
195
{
 
196
        g_return_if_fail(trans  != NULL);
 
197
        g_return_if_fail(answer != NULL);
 
198
 
 
199
        if (trans->callbacks == NULL)
 
200
        {
 
201
                trans->has_custom_callbacks = TRUE;
 
202
                trans->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
 
203
                                                                                                 NULL);
 
204
        }
 
205
        else if (trans->has_custom_callbacks != TRUE)
 
206
                g_return_if_reached ();
 
207
 
 
208
        g_hash_table_insert(trans->callbacks, answer, cb);
 
209
}
 
210
 
 
211
static gboolean
 
212
transaction_timeout(gpointer data)
 
213
{
 
214
        MsnTransaction *trans;
 
215
 
 
216
        trans = data;
 
217
        g_return_val_if_fail(trans != NULL, FALSE);
 
218
 
 
219
#if 0
 
220
        purple_debug_info("msn", "timed out: %s %d %s\n", trans->command, trans->trId, trans->params);
 
221
#endif
 
222
 
 
223
        trans->timer = 0;
 
224
 
 
225
        if (trans->timeout_cb != NULL)
 
226
                trans->timeout_cb(trans->cmdproc, trans);
 
227
 
 
228
        return FALSE;
 
229
}
 
230
 
 
231
void
 
232
msn_transaction_set_timeout_cb(MsnTransaction *trans, MsnTimeoutCb cb)
 
233
{
 
234
        if (trans->timer)
 
235
        {
 
236
                purple_debug_error("msn", "This shouldn't be happening\n");
 
237
                purple_timeout_remove(trans->timer);
 
238
        }
 
239
        trans->timeout_cb = cb;
 
240
        trans->timer = purple_timeout_add_seconds(60, transaction_timeout, trans);
 
241
}
 
242
 
 
243
void
 
244
msn_transaction_set_error_cb(MsnTransaction *trans, MsnErrorCb cb)
 
245
{
 
246
        trans->error_cb = cb;
 
247
}