~ubuntu-branches/ubuntu/lucid/seahorse/lucid

« back to all changes in this revision

Viewing changes to pgp/seahorse-pgp-operation.c

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2009-01-08 17:05:02 UTC
  • mfrom: (1.2.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090108170502-zs2i7svo0b7l3ecz
Tags: 2.25.4-0ubuntu1
* New upstream version (LP: #315147).
* Update debian/patches/80_autoconf_update.patch
* debian/rules:
  - addition of a shlibs rule for libcryptui0 (>= 2.25.4)
* Fix bdeps to match upstream configure check:
  - set libgnome-keyring-dev to (>= 2.25.3).
  - add libtasn1-3-dev.
* debian/seahorse.install:
  - inclusion of debian/tmp/usr/share/gnome/autostart/seahorse-daemon.desktop.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Seahorse
3
 
 *
4
 
 * Copyright (C) 2006 Stefan Walter
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
 * See the GNU General Public License for more details.
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the
17
 
 * Free Software Foundation, Inc.,
18
 
 * 59 Temple Place, Suite 330,
19
 
 * Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include "seahorse-util.h"
25
 
 
26
 
#include "pgp/seahorse-gpgmex.h"
27
 
#include "pgp/seahorse-pgp-operation.h"
28
 
#include "pgp/seahorse-pgp-source.h"
29
 
 
30
 
#define DEBUG_OPERATION_ENABLE 0
31
 
 
32
 
#ifndef DEBUG_OPERATION_ENABLE
33
 
#if _DEBUG
34
 
#define DEBUG_OPERATION_ENABLE 1
35
 
#else
36
 
#define DEBUG_OPERATION_ENABLE 0
37
 
#endif
38
 
#endif
39
 
 
40
 
#if DEBUG_OPERATION_ENABLE
41
 
#define DEBUG_OPERATION(x) g_printerr x
42
 
#else
43
 
#define DEBUG_OPERATION(x) 
44
 
#endif
45
 
 
46
 
/* -----------------------------------------------------------------------------
47
 
 * DEFINITIONS
48
 
 */
49
 
 
50
 
typedef struct _WatchData {
51
 
    SeahorsePGPOperation *op;   /* The operation we're working with */
52
 
    gint stag;                  /* IO watch source tag */
53
 
    gboolean registered;        /* Whether this watch is currently registered */
54
 
 
55
 
    /* GPGME watch info */
56
 
    int fd;
57
 
    int dir;
58
 
    gpgme_io_cb_t fnc;
59
 
    void *fnc_data;
60
 
    
61
 
} WatchData;
62
 
 
63
 
/* Macros "borrowed" from GDK */
64
 
#define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
65
 
#define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
66
 
 
67
 
typedef struct _SeahorsePGPOperationPrivate {
68
 
    gpgme_ctx_t gctx;               /* The context we watch for the async op to complete */
69
 
    gchar *message;                 /* A progress message to display (or NULL for GPGME messages) */
70
 
    struct gpgme_io_cbs io_cbs;     /* The GPGME IO callback vtable */
71
 
    GList *watches;                 /* Watches GPGME asked us to track */
72
 
    gboolean busy;                  /* If the context is currently executing something */
73
 
    guint def_total;                /* Default total */
74
 
} SeahorsePGPOperationPrivate;
75
 
 
76
 
enum {
77
 
    PROP_0,
78
 
    PROP_GCTX,
79
 
    PROP_MESSAGE,
80
 
    PROP_DEF_TOTAL
81
 
};
82
 
 
83
 
enum {
84
 
    RESULTS,
85
 
    LAST_SIGNAL
86
 
};
87
 
 
88
 
static guint signals[LAST_SIGNAL] = { 0 };
89
 
 
90
 
#define SEAHORSE_PGP_OPERATION_GET_PRIVATE(obj)  \
91
 
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SEAHORSE_TYPE_PGP_OPERATION, SeahorsePGPOperationPrivate))
92
 
 
93
 
/* TODO: This is just nasty. Gotta get rid of these weird macros */
94
 
IMPLEMENT_OPERATION_PROPS(PGP, pgp)
95
 
 
96
 
    g_object_class_install_property (gobject_class, PROP_GCTX,
97
 
        g_param_spec_pointer ("gctx", "GPGME Context", "GPGME Context that this operation is watching.", 
98
 
                              G_PARAM_READABLE));
99
 
 
100
 
    g_object_class_install_property (gobject_class, PROP_MESSAGE,
101
 
        g_param_spec_string ("message", "Progress Message", "Progress message that overrides whatever GPGME gives us", 
102
 
                             NULL, G_PARAM_READABLE | G_PARAM_WRITABLE));   
103
 
 
104
 
    g_object_class_install_property (gobject_class, PROP_DEF_TOTAL,
105
 
        g_param_spec_uint ("default-total", "Default Total", "Default total to use instead of GPGME's progress total.",
106
 
                           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_WRITABLE));
107
 
 
108
 
    signals[RESULTS] = g_signal_new ("results", SEAHORSE_TYPE_PGP_OPERATION, 
109
 
                G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (SeahorsePGPOperationClass, results),
110
 
                NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
111
 
 
112
 
    g_type_class_add_private (gobject_class, sizeof (SeahorsePGPOperationPrivate));
113
 
 
114
 
END_IMPLEMENT_OPERATION_PROPS
115
 
 
116
 
 
117
 
/* -----------------------------------------------------------------------------
118
 
 * HELPERS
119
 
 */
120
 
 
121
 
/* Called when there's data for GPG on a file descriptor */
122
 
static gboolean
123
 
io_callback (GIOChannel *source, GIOCondition condition, WatchData *watch)
124
 
{
125
 
    DEBUG_OPERATION (("PGPOP: io for GPGME on %d\n", watch->fd));
126
 
    (watch->fnc) (watch->fnc_data, g_io_channel_unix_get_fd (source));
127
 
    return TRUE;
128
 
}
129
 
 
130
 
/* Register a GPGME callback with GLib. */
131
 
static void
132
 
register_watch (WatchData *watch)
133
 
{
134
 
    GIOChannel *channel;
135
 
    
136
 
    if (watch->registered)
137
 
        return;
138
 
    
139
 
    DEBUG_OPERATION (("PGPOP: registering watch %d\n", watch->fd));
140
 
    
141
 
    channel = g_io_channel_unix_new (watch->fd);
142
 
    watch->stag = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, 
143
 
                                       watch->dir ? READ_CONDITION : WRITE_CONDITION,
144
 
                                       (GIOFunc)io_callback, watch, NULL);
145
 
    watch->registered = TRUE;
146
 
    g_io_channel_unref (channel);
147
 
}
148
 
 
149
 
/* Unregister GPGME callback with GLib. */
150
 
static void
151
 
unregister_watch (WatchData *watch)
152
 
{
153
 
    if (!watch->registered)
154
 
        return;
155
 
    
156
 
    DEBUG_OPERATION (("PGPOP: unregistering watch %d\n", watch->fd));
157
 
    
158
 
    g_source_remove (watch->stag);
159
 
    watch->stag = 0;
160
 
    watch->registered = FALSE;
161
 
}
162
 
 
163
 
/* -----------------------------------------------------------------------------
164
 
 * CALLED FROM GPGME
165
 
 */
166
 
 
167
 
/* Called by GPGME when a change of progress state */
168
 
static void 
169
 
progress_cb (void *data, const char *what, int type, 
170
 
             int current, int total)
171
 
{
172
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (data);
173
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
174
 
    
175
 
    DEBUG_OPERATION (("PGPOP: got progress: %s %d/%d\n", what, current, total));
176
 
    
177
 
    if (total <= 0)
178
 
        total = pv->def_total;
179
 
    
180
 
    if (seahorse_operation_is_running (SEAHORSE_OPERATION (pop)))
181
 
        seahorse_operation_mark_progress_full (SEAHORSE_OPERATION (pop), 
182
 
                                               pv->message ? pv->message : what, 
183
 
                                               current, total);
184
 
}
185
 
 
186
 
/* Register a callback. */
187
 
static gpg_error_t
188
 
register_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, 
189
 
             void **tag)
190
 
{
191
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (data);
192
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
193
 
    WatchData *watch;
194
 
    
195
 
    DEBUG_OPERATION (("PGPOP: request to register watch %d\n", fd));
196
 
    
197
 
    watch = g_new0 (WatchData, 1);
198
 
    watch->registered = FALSE;
199
 
    watch->fd = fd;
200
 
    watch->dir = dir;
201
 
    watch->fnc = fnc;
202
 
    watch->fnc_data = fnc_data;
203
 
    watch->op = pop;
204
 
    
205
 
    /* 
206
 
     * If the context is busy, we already have a START event, and can
207
 
     * register watches freely.
208
 
     */
209
 
    if (pv->busy)
210
 
        register_watch (watch);
211
 
 
212
 
    pv->watches = g_list_append (pv->watches, watch);
213
 
    *tag = watch;
214
 
 
215
 
    return GPG_OK;
216
 
}
217
 
 
218
 
/* Remove a callback. */
219
 
static void
220
 
remove_cb (void *tag)
221
 
{
222
 
    SeahorsePGPOperationPrivate *pv;
223
 
    WatchData *watch = (WatchData*)tag;
224
 
 
225
 
    pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (watch->op);
226
 
 
227
 
    DEBUG_OPERATION (("PGPOP: request to remove watch %d\n", watch->fd));
228
 
    
229
 
    pv->watches = g_list_remove (pv->watches, watch);
230
 
    unregister_watch (watch);
231
 
    g_free (watch);
232
 
}
233
 
 
234
 
/* The event callback. */
235
 
static void
236
 
event_cb (void *data, gpgme_event_io_t type, void *type_data)
237
 
{
238
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (data);
239
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
240
 
    gpg_error_t *gerr;
241
 
    GError *error = NULL;
242
 
    GList *list;
243
 
    
244
 
    switch (type) {
245
 
        
246
 
    /* Called when the GPGME context starts an operation */
247
 
    case GPGME_EVENT_START:
248
 
        pv->busy = TRUE;
249
 
    
250
 
        DEBUG_OPERATION (("PGPOP: start event\n"));
251
 
        
252
 
        /* Since we weren't supposed to register these before, do it now */
253
 
        for (list = pv->watches; list; list = g_list_next (list))
254
 
            register_watch ((WatchData*)(list->data));
255
 
    
256
 
        /* And tell everyone we've begun */
257
 
        if (!seahorse_operation_is_running (SEAHORSE_OPERATION (pop)))
258
 
            seahorse_operation_mark_start (SEAHORSE_OPERATION (pop));
259
 
        
260
 
        /* Listens for progress updates from GPG */
261
 
        gpgme_set_progress_cb (pop->gctx, progress_cb, pop);
262
 
 
263
 
        break;
264
 
        
265
 
    /* Called when the GPGME context is finished with an op */
266
 
    case GPGME_EVENT_DONE:
267
 
        pv->busy = FALSE;
268
 
        gerr = (gpgme_error_t*)type_data;
269
 
    
270
 
        DEBUG_OPERATION (("PGPOP: done event (err: %d)\n", *gerr));
271
 
        
272
 
        /* Make sure we have no extra watches left over */
273
 
        for (list = pv->watches; list; list = g_list_next (list))
274
 
            unregister_watch ((WatchData*)(list->data));
275
 
        
276
 
        gpgme_set_progress_cb (pop->gctx, NULL, NULL);
277
 
        
278
 
        /* And try to figure out a good response */
279
 
        if (seahorse_operation_is_running (SEAHORSE_OPERATION (pop))) {
280
 
            
281
 
            /* Cancelled */
282
 
            if (gpgme_err_code (*gerr) == GPG_ERR_CANCELED) {
283
 
                seahorse_operation_mark_done (SEAHORSE_OPERATION (pop), TRUE, NULL);
284
 
                
285
 
            } else {
286
 
                
287
 
                /* Reference guard (marking an op as complete unrefs it) */
288
 
                g_object_ref (pop);
289
 
                
290
 
                /* Other Errors */
291
 
                if (*gerr)
292
 
                    seahorse_gpgme_to_error (*gerr, &error);
293
 
                
294
 
                /* No error, results available */
295
 
                else
296
 
                    g_signal_emit (pop, signals[RESULTS], 0);
297
 
                
298
 
                /* The above event may have started the action again so double check */
299
 
                if (!pv->busy && seahorse_operation_is_running (SEAHORSE_OPERATION (pop)))
300
 
                    seahorse_operation_mark_done (SEAHORSE_OPERATION (pop), FALSE, error);
301
 
                
302
 
                /* End reference guard */
303
 
                g_object_unref (pop);
304
 
            }
305
 
        }
306
 
        break;
307
 
    
308
 
    case GPGME_EVENT_NEXT_KEY:
309
 
    case GPGME_EVENT_NEXT_TRUSTITEM:
310
 
    default:
311
 
        /* Ignore unsupported event types */
312
 
        break;
313
 
    }
314
 
}
315
 
 
316
 
/* -----------------------------------------------------------------------------
317
 
 * OBJECT 
318
 
 */
319
 
 
320
 
static void 
321
 
seahorse_pgp_operation_init (SeahorsePGPOperation *pop)
322
 
{
323
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
324
 
    
325
 
    pop->gctx = seahorse_pgp_source_new_context ();
326
 
    g_return_if_fail (pop->gctx != NULL);
327
 
    
328
 
    pv->busy = FALSE;
329
 
    
330
 
    /* Setup with the context */
331
 
    pv->io_cbs.add = register_cb;
332
 
    pv->io_cbs.add_priv = pop;
333
 
    pv->io_cbs.remove = remove_cb;
334
 
    pv->io_cbs.event = event_cb;
335
 
    pv->io_cbs.event_priv = pop;
336
 
    
337
 
    gpgme_set_io_cbs (pop->gctx, &(pv->io_cbs));
338
 
}
339
 
 
340
 
static void 
341
 
seahorse_pgp_operation_set_property (GObject *gobject, guint prop_id, 
342
 
                                     const GValue *value, GParamSpec *pspec)
343
 
{
344
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (gobject);
345
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
346
 
    
347
 
    switch (prop_id) {
348
 
    case PROP_MESSAGE:
349
 
        g_free (pv->message);
350
 
        pv->message = g_strdup (g_value_get_string (value));
351
 
        break;
352
 
    case PROP_DEF_TOTAL:
353
 
        pv->def_total = g_value_get_uint (value);
354
 
        break;
355
 
    }
356
 
}
357
 
 
358
 
static void 
359
 
seahorse_pgp_operation_get_property (GObject *gobject, guint prop_id, 
360
 
                                     GValue *value, GParamSpec *pspec)
361
 
{
362
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (gobject);
363
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
364
 
    
365
 
    switch (prop_id) {
366
 
    case PROP_GCTX:
367
 
        g_value_set_pointer (value, pop->gctx);
368
 
        break;
369
 
    case PROP_MESSAGE:
370
 
        g_value_set_string (value, pv->message);
371
 
        break;
372
 
    case PROP_DEF_TOTAL:
373
 
        g_value_set_uint (value, pv->def_total);
374
 
    }
375
 
}
376
 
 
377
 
static void 
378
 
seahorse_pgp_operation_dispose (GObject *gobject)
379
 
{
380
 
    /* Nothing to do */
381
 
    G_OBJECT_CLASS (pgp_operation_parent_class)->dispose (gobject);
382
 
}
383
 
 
384
 
static void 
385
 
seahorse_pgp_operation_finalize (GObject *gobject)
386
 
{
387
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (gobject);
388
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
389
 
    GList *list;
390
 
    
391
 
    if (pv->busy) {
392
 
        g_critical ("NASTY BUG. Disposing of a SeahorsePGPOperation while GPGME is "
393
 
                    "still performing an operation. SeahorseOperation should ref"
394
 
                    "itself while active");
395
 
    }
396
 
    
397
 
    /* Make sure we have no extra watches left over */
398
 
    for (list = pv->watches; list; list = g_list_next (list)) {
399
 
        unregister_watch ((WatchData*)(list->data));
400
 
        g_free (list->data);
401
 
    }
402
 
    
403
 
    g_list_free (pv->watches);
404
 
    pv->watches = NULL;
405
 
    
406
 
    if (pop->gctx)
407
 
        gpgme_release (pop->gctx);
408
 
    pop->gctx = NULL;
409
 
    
410
 
    g_free (pv->message);
411
 
    pv->message = NULL;
412
 
    
413
 
    G_OBJECT_CLASS (pgp_operation_parent_class)->finalize (gobject);
414
 
}
415
 
 
416
 
static void 
417
 
seahorse_pgp_operation_cancel (SeahorseOperation *operation)
418
 
{
419
 
    SeahorsePGPOperation *pop = SEAHORSE_PGP_OPERATION (operation);
420
 
    SeahorsePGPOperationPrivate *pv = SEAHORSE_PGP_OPERATION_GET_PRIVATE (pop);
421
 
    
422
 
    g_return_if_fail (seahorse_operation_is_running (operation));
423
 
    g_return_if_fail (pop->gctx != NULL);
424
 
    
425
 
    /* This should call in through event_cb and cancel us */
426
 
    if (pv->busy)
427
 
        gpgme_cancel (pop->gctx);
428
 
}
429
 
 
430
 
/* -----------------------------------------------------------------------------
431
 
 * PUBLIC METHODS
432
 
 */
433
 
 
434
 
SeahorsePGPOperation*
435
 
seahorse_pgp_operation_new (const gchar *message)
436
 
{
437
 
    return g_object_new (SEAHORSE_TYPE_PGP_OPERATION, "message", message, NULL);
438
 
}
439
 
 
440
 
void
441
 
seahorse_pgp_operation_mark_failed (SeahorsePGPOperation *pop, gpgme_error_t gerr)
442
 
{
443
 
    SeahorseOperation *op = SEAHORSE_OPERATION (pop);
444
 
    GError *err = NULL;
445
 
    
446
 
    if (!seahorse_operation_is_running (op))
447
 
        seahorse_operation_mark_start (op);
448
 
    
449
 
    seahorse_gpgme_to_error (gerr, &err);
450
 
    seahorse_operation_mark_done (op, FALSE, err);
451
 
}