File: request.c

package info (click to toggle)
telepathy-haze 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,812 kB
  • sloc: sh: 11,524; ansic: 9,895; python: 4,812; xml: 607; makefile: 291
file content (271 lines) | stat: -rw-r--r-- 8,185 bytes parent folder | download | duplicates (4)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * request.c - stub implementation of the libpurple request API.
 * Copyright (C) 2007 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "config.h"

#include <glib-object.h>

#include <libpurple/account.h>
#include <libpurple/conversation.h>

#include "debug.h"
#include "request.h"
#include "connection.h"

#ifdef ENABLE_LEAKY_REQUEST_STUBS
static gpointer
haze_request_input (const char *title,
                    const char *primary,
                    const char *secondary,
                    const char *default_value,
                    gboolean multiline,
                    gboolean masked,
                    gchar *hint,
                    const char *ok_text,
                    GCallback ok_cb,
                    const char *cancel_text,
                    GCallback cancel_cb,
                    PurpleAccount *account,
                    const char *who,
                    PurpleConversation *conv,
                    void *user_data)
{
    DEBUG ("ignoring request:");
    DEBUG ("    title: %s", (title ? title : "(null)"));
    DEBUG ("    primary: %s", (primary ? primary : "(null)"));
    DEBUG ("    secondary: %s", (secondary ? secondary : "(null)"));
    DEBUG ("    default_value: %s", default_value ? default_value : "(null)");

    return NULL;
}

static gpointer
haze_request_choice (const char *title,
                     const char *primary,
                     const char *secondary,
                     int default_value,
                     const char *ok_text,
                     GCallback ok_cb,
                     const char *cancel_text,
                     GCallback cancel_cb,
                     PurpleAccount *account,
                     const char *who,
                     PurpleConversation *conv,
                     void *user_data,
                     va_list choices)
{
    DEBUG ("ignoring request:");
    DEBUG ("    title: %s", (title ? title : "(null)"));
    DEBUG ("    primary: %s", (primary ? primary : "(null)"));
    DEBUG ("    secondary: %s", (secondary ? secondary : "(null)"));
    DEBUG ("    default_value: %i", default_value);

    return NULL;
}

static gpointer
haze_request_action (const char *title,
                     const char *primary,
                     const char *secondary,
                     int default_action,
                     PurpleAccount *account,
                     const char *who,
                     PurpleConversation *conv,
                     void *user_data,
                     size_t action_count,
                     va_list actions)
{
    DEBUG ("ignoring request:");
    DEBUG ("    title: %s", (title ? title : "(null)"));
    DEBUG ("    primary: %s", (primary ? primary : "(null)"));
    DEBUG ("    secondary: %s", (secondary ? secondary : "(null)"));

    return NULL;
}
#endif

struct fields_data {
    PurpleAccount *account;
    PurpleRequestFields *fields;
    PurpleRequestField *password;
    PurpleRequestFieldsCb ok_cb;
    PurpleRequestFieldsCb cancel_cb;
    void *user_data;
};

static void
haze_close_request (PurpleRequestType type,
                    void *ui_handle)
{
    struct fields_data *fd = ui_handle;

    haze_connection_cancel_password_request (fd->account);
    purple_request_fields_destroy (fd->fields);
    g_slice_free (struct fields_data, fd);
}

void
haze_request_password_cb (gpointer user_data,
                          const gchar *password)
{
    struct fields_data *fd = user_data;

    if (password)
      {
        purple_request_field_string_set_value (fd->password, password);
        if (fd->ok_cb)
          {
            (fd->ok_cb) (fd->user_data, fd->fields);
          }
      }
    else
      {
        if (fd->cancel_cb)
          {
            (fd->cancel_cb) (fd->user_data, fd->fields);
          }
      }

    purple_request_close (PURPLE_REQUEST_FIELDS, fd);
}

static gboolean
haze_request_fields_destroy (gpointer user_data)
{
    struct fields_data *fd = user_data;

    if (fd->cancel_cb)
      {
        (fd->cancel_cb) (fd->user_data, fd->fields);
      }

    purple_request_close (PURPLE_REQUEST_FIELDS, user_data);

    return FALSE;
}

/*
 * We must support purple_account_request_password() which boils down
 * to purple_request_fields() with certain parameters. I'm not sure
 * if this the best way of doing this, but it works.
 */
static gpointer
haze_request_fields (const char *title,
                     const char *primary,
                     const char *secondary,
                     PurpleRequestFields *fields,
                     const char *ok_text,
                     GCallback ok_cb,
                     const char *cancel_text,
                     GCallback cancel_cb,
                     PurpleAccount *account,
                     const char *who,
                     PurpleConversation *conv,
                     void *user_data)
{
    struct fields_data *fd = g_slice_new0 (struct fields_data);

    /* it is our responsibility to destroy this data */
    fd->account   = account;
    fd->fields    = fields;
    fd->cancel_cb = (PurpleRequestFieldsCb) cancel_cb;
    fd->user_data = user_data;

    if (purple_request_fields_exists (fields, "password") &&
        purple_request_fields_exists (fields, "remember"))
      {

        DEBUG ("triggering password request");

        fd->password = purple_request_fields_get_field (fields, "password");
        fd->ok_cb    = (PurpleRequestFieldsCb) ok_cb;

        haze_connection_request_password (account, fd);

      }
    else
      {
        DEBUG ("ignoring request:");
        DEBUG ("    title: %s", (title ? title : "(null)"));
        DEBUG ("    primary: %s", (primary ? primary : "(null)"));
        DEBUG ("    secondary: %s", (secondary ? secondary : "(null)"));

        /* Avoid leaking of "fields" and "user_data" */
        g_idle_add (haze_request_fields_destroy, fd);
      }

    return fd;
}

#ifdef ENABLE_LEAKY_REQUEST_STUBS
static gpointer
haze_request_file (const char *title,
                   const char *filename,
                   gboolean savedialog,
                   GCallback ok_cb,
                   GCallback cancel_cb,
                   PurpleAccount *account,
                   const char *who,
                   PurpleConversation *conv,
                   void *user_data)
{
    DEBUG ("ignoring request:");
    DEBUG ("    title: %s", (title ? title : "(null)"));
    DEBUG ("    filename: %s", (filename ? filename : "(null)"));

    return NULL;
}

static gpointer
haze_request_folder (const char *title,
                     const char *dirname,
                     GCallback ok_cb,
                     GCallback cancel_cb,
                     PurpleAccount *account,
                     const char *who,
                     PurpleConversation *conv,
                     void *user_data)
{
    DEBUG ("ignoring request:");
    DEBUG ("    title: %s", (title ? title : "(null)"));
    DEBUG ("    dirname: %s", (dirname ? dirname : "(null)"));

    return NULL;
}
#endif

static PurpleRequestUiOps request_uiops =
{
#ifdef ENABLE_LEAKY_REQUEST_STUBS
    .request_input = haze_request_input,
    .request_choice = haze_request_choice,
    .request_action = haze_request_action,
    .request_file = haze_request_file,
    .request_folder = haze_request_folder,
#endif
    .request_fields = haze_request_fields,
    .close_request  = haze_close_request
};

PurpleRequestUiOps *
haze_request_get_ui_ops ()
{
    return &request_uiops;
}