~ubuntu-branches/ubuntu/natty/seahorse/natty

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Seahorse
 *
 * Copyright (C) 2006 Stefan Walter
 *
 * 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 <sys/wait.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <glib/gi18n.h>

#include "seahorse-gkr-operation.h"
#include "seahorse-util.h"
#include "seahorse-passphrase.h"

#include <gnome-keyring.h>

#ifndef DEBUG_OPERATION_ENABLE
#if _DEBUG
#define DEBUG_OPERATION_ENABLE 1
#else
#define DEBUG_OPERATION_ENABLE 0
#endif
#endif

#if DEBUG_OPERATION_ENABLE
#define DEBUG_OPERATION(x)  g_printerr x
#else
#define DEBUG_OPERATION(x)
#endif

/* -----------------------------------------------------------------------------
 * DEFINITIONS
 */
 
struct _SeahorseGkrOperationPrivate {
	gpointer request;
	SeahorseObject *object;   
};

G_DEFINE_TYPE (SeahorseGkrOperation, seahorse_gkr_operation, SEAHORSE_TYPE_OPERATION);

/* -----------------------------------------------------------------------------
 * HELPERS 
 */

static gboolean
check_operation_result (SeahorseGkrOperation *self, GnomeKeyringResult result)
{
    GError *err = NULL;
    gboolean success;
    
    /* This only gets called when we cancel, so ignore */
    if (result == GNOME_KEYRING_RESULT_CANCELLED)
        return FALSE;
    
    success = seahorse_gkr_operation_parse_error (result, &err);
    g_assert (!success || !err);
    
    seahorse_operation_mark_done (SEAHORSE_OPERATION (self), FALSE, err);
    return success;
}

/* -----------------------------------------------------------------------------
 * OBJECT 
 */

static void 
seahorse_gkr_operation_init (SeahorseGkrOperation *self)
{
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, SEAHORSE_TYPE_GKR_OPERATION, SeahorseGkrOperationPrivate);

}

static void 
seahorse_gkr_operation_dispose (GObject *gobject)
{
	SeahorseGkrOperation *self = SEAHORSE_GKR_OPERATION (gobject);

	if (seahorse_operation_is_running (SEAHORSE_OPERATION (self)))
		seahorse_operation_cancel (SEAHORSE_OPERATION (self));
	g_assert (!seahorse_operation_is_running (SEAHORSE_OPERATION (self)));
    
	if (self->pv->object)
		g_object_unref (self->pv->object);
	self->pv->object = NULL;
    
	/* The above cancel should have stopped this */
	g_assert (self->pv->request == NULL);
    
	G_OBJECT_CLASS (seahorse_gkr_operation_parent_class)->dispose (gobject);  
}

static void 
seahorse_gkr_operation_finalize (GObject *gobject)
{
	SeahorseGkrOperation *self = SEAHORSE_GKR_OPERATION (gobject);
    
	g_assert (!self->pv->request);
    
	G_OBJECT_CLASS (seahorse_gkr_operation_parent_class)->finalize (gobject);  
}

static void 
seahorse_gkr_operation_cancel (SeahorseOperation *operation)
{
	SeahorseGkrOperation *self = SEAHORSE_GKR_OPERATION (operation);    

	if (self->pv->request)
		gnome_keyring_cancel_request (self->pv->request);
	self->pv->request = NULL;
    
	if (seahorse_operation_is_running (operation))
		seahorse_operation_mark_done (operation, TRUE, NULL);    
}

static void
seahorse_gkr_operation_class_init (SeahorseGkrOperationClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    
	seahorse_gkr_operation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SeahorseGkrOperationPrivate));

	gobject_class->dispose = seahorse_gkr_operation_dispose;
	gobject_class->finalize = seahorse_gkr_operation_finalize;
	
	SEAHORSE_OPERATION_CLASS (klass)->cancel = seahorse_gkr_operation_cancel;
}

/* -----------------------------------------------------------------------------
 * PUBLIC 
 */

gboolean
seahorse_gkr_operation_parse_error (GnomeKeyringResult result, GError **err)
{
    static GQuark errorq = 0;
    const gchar *message = NULL;

    if (result == GNOME_KEYRING_RESULT_OK)
        return TRUE;
    
    /* These should be handled in the callbacks */
    g_assert (result != GNOME_KEYRING_RESULT_CANCELLED);
    
    /* An error mark it as such */
    switch (result) {
    case GNOME_KEYRING_RESULT_DENIED:
        message = _("Access to the key ring was denied");
        break;
    case GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON:
        message = _("The gnome-keyring daemon is not running");
        break;
    case GNOME_KEYRING_RESULT_ALREADY_UNLOCKED:
        message = _("The key ring has already been unlocked");
        break;
    case GNOME_KEYRING_RESULT_NO_SUCH_KEYRING:
        message = _("No such key ring exists");
        break;
    case GNOME_KEYRING_RESULT_IO_ERROR:
        message = _("Couldn't communicate with key ring daemon");
        break;
    case GNOME_KEYRING_RESULT_ALREADY_EXISTS:
        message = _("The item already exists");
        break;
    case GNOME_KEYRING_RESULT_BAD_ARGUMENTS:
        g_warning ("bad arguments passed to gnome-keyring API");
        /* Fall through */
    default:
        message = _("Internal error accessing gnome-keyring");
        break;
    }
    
    if (!errorq) 
        errorq = g_quark_from_static_string ("seahorse-gnome-keyring");
    
    g_set_error (err, errorq, result, "%s", message);
    return FALSE;
}

/* -----------------------------------------------------------------------------
 * UPDATE INFO OPERATION
 */

static void 
basic_operation_done (GnomeKeyringResult result, SeahorseGkrOperation *self)
{
	g_assert (SEAHORSE_IS_GKR_OPERATION (self));
	self->pv->request = NULL;

	if (!check_operation_result (self, result))
		return;
    
	/* When operation is successful reload the key */
	seahorse_object_refresh (SEAHORSE_OBJECT (self->pv->object));
}

SeahorseOperation*
seahorse_gkr_operation_update_info (SeahorseGkrItem *item, GnomeKeyringItemInfo *info)
{
	SeahorseGkrOperation *self;
    
	g_return_val_if_fail (SEAHORSE_IS_GKR_ITEM (item), NULL);
    
	self = g_object_new (SEAHORSE_TYPE_GKR_OPERATION, NULL);
    
	g_object_ref (item);
	self->pv->object = SEAHORSE_OBJECT (item);
    
	/* Start actual save request */
	g_object_ref (self);
	self->pv->request = gnome_keyring_item_set_info (seahorse_gkr_item_get_keyring_name (item),
	                                                 seahorse_gkr_item_get_item_id (item), info, 
	                                                 (GnomeKeyringOperationDoneCallback)basic_operation_done,
	                                                 self, g_object_unref);
	g_return_val_if_fail (self->pv->request, NULL);
    
	seahorse_operation_mark_start (SEAHORSE_OPERATION (self));
	seahorse_operation_mark_progress (SEAHORSE_OPERATION (self), _("Saving item..."), -1);

	return SEAHORSE_OPERATION (self);
}

SeahorseOperation*
seahorse_gkr_operation_update_acl (SeahorseGkrItem *item, GList *acl)
{
	SeahorseGkrOperation *self;
    
	g_return_val_if_fail (SEAHORSE_IS_GKR_ITEM (item), NULL);
    
	self = g_object_new (SEAHORSE_TYPE_GKR_OPERATION, NULL);

	g_object_ref (item);
	self->pv->object = SEAHORSE_OBJECT (item);
    
	/* Start actual save request */
	g_object_ref (self);
	self->pv->request = gnome_keyring_item_set_acl (seahorse_gkr_item_get_keyring_name (item), 
	                                                seahorse_gkr_item_get_item_id (item), acl, 
	                                                (GnomeKeyringOperationDoneCallback)basic_operation_done,
	                                                self, g_object_unref);
	g_return_val_if_fail (self->pv->request, NULL);
    
	seahorse_operation_mark_start (SEAHORSE_OPERATION (self));
	seahorse_operation_mark_progress (SEAHORSE_OPERATION (self), _("Saving item..."), -1);

	return SEAHORSE_OPERATION (self);
}

static void 
delete_operation_done (GnomeKeyringResult result, SeahorseGkrOperation *self)
{
	g_assert (SEAHORSE_IS_GKR_OPERATION (self));

	self->pv->request = NULL;
	if (check_operation_result (self, result))
		seahorse_context_remove_object (NULL, self->pv->object);
}

SeahorseOperation*
seahorse_gkr_operation_delete_item (SeahorseGkrItem *item)
{
	SeahorseGkrOperation *self;
	    
	g_return_val_if_fail (SEAHORSE_IS_GKR_ITEM (item), NULL);
	    
	self = g_object_new (SEAHORSE_TYPE_GKR_OPERATION, NULL);
	    
	g_object_ref (item);
	self->pv->object = SEAHORSE_OBJECT (item);
	    
	/* Start actual save request */
	g_object_ref (self);
	self->pv->request = gnome_keyring_item_delete (seahorse_gkr_item_get_keyring_name (item), 
	                                               seahorse_gkr_item_get_item_id (item), 
	                                               (GnomeKeyringOperationDoneCallback)delete_operation_done,
	                                               self, g_object_unref);
	g_return_val_if_fail (self->pv->request, NULL);
	
	seahorse_operation_mark_start (SEAHORSE_OPERATION (self));
	seahorse_operation_mark_progress (SEAHORSE_OPERATION (self), _("Deleting item..."), -1);
	
	return SEAHORSE_OPERATION (self);
}

SeahorseOperation*
seahorse_gkr_operation_delete_keyring (SeahorseGkrKeyring *keyring)
{
	SeahorseGkrOperation *self;
	    
	g_return_val_if_fail (SEAHORSE_IS_GKR_KEYRING (keyring), NULL);
	    
	self = g_object_new (SEAHORSE_TYPE_GKR_OPERATION, NULL);
	
	g_object_ref (keyring);
	self->pv->object = SEAHORSE_OBJECT (keyring);
	    
	/* Start actual save request */
	g_object_ref (self);
	self->pv->request = gnome_keyring_delete (seahorse_gkr_keyring_get_name (keyring), 
	                                          (GnomeKeyringOperationDoneCallback)delete_operation_done,
	                                          self, g_object_unref);
	g_return_val_if_fail (self->pv->request, NULL);
	
	seahorse_operation_mark_start (SEAHORSE_OPERATION (self));
	seahorse_operation_mark_progress (SEAHORSE_OPERATION (self), _("Deleting keyring..."), -1);
	
	return SEAHORSE_OPERATION (self);
}