4
* Copyright (C) 2011 Collabora Ltd.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21
* Author: Stef Walter <stefw@collabora.co.uk>
26
#include "gcr-import-button.h"
27
#include "gcr-internal.h"
28
#include "gcr-library.h"
29
#include "gcr-marshal.h"
30
#include "gcr-parser.h"
31
#include "gcr-pkcs11-import-interaction.h"
33
#include <glib/gi18n-lib.h>
41
* SECTION:gcr-import-button
42
* @title: GcrImportButton
43
* @short_description: Button which imports parsed certificates and keys
45
* A button which imports keys and certificates. Shows a spinner when the
46
* button is activated. When more than one importer is available shows
47
* a drop down to select which to import to.
53
* Button which imports parsed certificates and keys.
57
* GcrImportButtonClass:
58
* @parent_class: The parent class
59
* @importing: Emitted when the import begins.
60
* @imported: Emitted when the import completes, or fails.
62
* Class for #GcrImportButton.
65
struct _GcrImportButtonPrivate {
75
GCancellable *cancellable;
85
static guint signals[LAST_SIGNAL] = { 0 };
87
static GQuark QUARK_IMPORTER = 0;
89
G_DEFINE_TYPE (GcrImportButton, gcr_import_button, GTK_TYPE_BUTTON);
92
gcr_import_button_init (GcrImportButton *self)
94
self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_IMPORT_BUTTON, GcrImportButtonPrivate);
95
self->pv->cancellable = g_cancellable_new ();
96
self->pv->label = gtk_label_new ("");
100
update_import_button (GcrImportButton *self)
105
/* Initializing, set a spinner */
106
if (self->pv->queued && !self->pv->ready) {
107
gtk_widget_show (self->pv->spinner);
108
gtk_spinner_start (GTK_SPINNER (self->pv->spinner));
109
gtk_widget_hide (self->pv->arrow);
110
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
111
gtk_widget_set_tooltip_text (GTK_WIDGET (self), _("Initializing..."));
113
/* Importing, set a spinner */
114
} else if (self->pv->importing) {
115
gtk_widget_show (self->pv->spinner);
116
gtk_spinner_start (GTK_SPINNER (self->pv->spinner));
117
gtk_widget_hide (self->pv->arrow);
118
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
119
gtk_widget_set_tooltip_text (GTK_WIDGET (self), _("Import is in progress..."));
121
} else if (self->pv->imported) {
122
gtk_widget_hide (self->pv->spinner);
123
gtk_spinner_stop (GTK_SPINNER (self->pv->spinner));
124
gtk_widget_hide (self->pv->arrow);
125
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
126
message = g_strdup_printf (_("Imported to: %s"), self->pv->imported);
127
gtk_widget_set_tooltip_text (GTK_WIDGET (self), message);
130
/* Not importing, but have importers */
131
} else if (self->pv->importers) {
133
gtk_widget_hide (self->pv->spinner);
134
gtk_spinner_stop (GTK_SPINNER (self->pv->spinner));
135
gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
137
/* More than one importer */
138
if (self->pv->importers->next) {
139
gtk_widget_show (self->pv->arrow);
140
gtk_widget_set_tooltip_text (GTK_WIDGET (self), NULL);
142
/* Only one importer */
144
gtk_widget_hide (self->pv->arrow);
145
g_object_get (self->pv->importers->data, "label", &label, NULL);
146
message = g_strdup_printf (_("Import to: %s"), label);
147
gtk_widget_set_tooltip_text (GTK_WIDGET (self), message);
152
/* No importers, none compatible */
153
} else if (self->pv->created) {
154
gtk_widget_hide (self->pv->spinner);
155
gtk_spinner_stop (GTK_SPINNER (self->pv->spinner));
156
gtk_widget_hide (self->pv->arrow);
158
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
159
gtk_widget_set_tooltip_text (GTK_WIDGET (self), _("Cannot import because there are no compatible importers"));
161
/* No importers yet added */
163
gtk_widget_hide (self->pv->spinner);
164
gtk_spinner_stop (GTK_SPINNER (self->pv->spinner));
165
gtk_widget_hide (self->pv->arrow);
167
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
168
gtk_widget_set_tooltip_text (GTK_WIDGET (self), _("No data to import"));
173
on_library_pkcs11_ready (GObject *source,
174
GAsyncResult *result,
177
GcrImportButton *self = GCR_IMPORT_BUTTON (user_data);
180
self->pv->ready = TRUE;
182
/* Process the parsed items that have been seen */
183
queued = self->pv->queued;
184
self->pv->queued = NULL;
185
for (l = queued; l != NULL; l = g_list_next (l))
186
gcr_import_button_add_parsed (self, l->data);
187
g_assert (self->pv->queued == NULL);
188
g_list_free_full (queued, gcr_parsed_unref);
192
gcr_import_button_constructed (GObject *obj)
194
GcrImportButton *self = GCR_IMPORT_BUTTON (obj);
197
G_OBJECT_CLASS (gcr_import_button_parent_class)->constructed (obj);
199
self->pv->spinner = gtk_spinner_new ();
200
self->pv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
201
grid = gtk_grid_new ();
203
gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_HORIZONTAL);
204
gtk_container_add (GTK_CONTAINER (grid), self->pv->spinner);
205
gtk_container_add (GTK_CONTAINER (grid), self->pv->label);
206
gtk_container_add (GTK_CONTAINER (grid), self->pv->arrow);
207
gtk_grid_set_row_spacing (GTK_GRID (grid), 3);
208
gtk_widget_set_hexpand (grid, TRUE);
209
gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
211
gtk_widget_show (self->pv->label);
212
gtk_widget_show (grid);
214
gtk_container_add (GTK_CONTAINER (self), grid);
216
update_import_button (self);
218
gcr_pkcs11_initialize_async (NULL, on_library_pkcs11_ready, g_object_ref (self));
222
gcr_import_button_set_property (GObject *obj,
227
GcrImportButton *self = GCR_IMPORT_BUTTON (obj);
231
gtk_label_set_label (GTK_LABEL (self->pv->label), g_value_get_string (value));
232
g_object_notify (obj, "label");
235
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
241
gcr_import_button_get_property (GObject *obj,
246
GcrImportButton *self = GCR_IMPORT_BUTTON (obj);
250
g_value_set_string (value, gtk_label_get_label (GTK_LABEL (self->pv->label)));
253
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
259
gcr_import_button_dispose (GObject *obj)
261
GcrImportButton *self = GCR_IMPORT_BUTTON (obj);
263
gck_list_unref_free (self->pv->importers);
264
self->pv->importers = NULL;
265
g_cancellable_cancel (self->pv->cancellable);
266
g_clear_object (&self->pv->menu);
268
g_list_free_full (self->pv->queued, gcr_parsed_unref);
269
self->pv->queued = NULL;
271
G_OBJECT_CLASS (gcr_import_button_parent_class)->dispose (obj);
275
gcr_import_button_finalize (GObject *obj)
277
GcrImportButton *self = GCR_IMPORT_BUTTON (obj);
279
g_object_unref (self->pv->cancellable);
281
G_OBJECT_CLASS (gcr_import_button_parent_class)->finalize (obj);
285
on_import_complete (GObject *importer,
286
GAsyncResult *result,
289
GcrImportButton *self = GCR_IMPORT_BUTTON (user_data);
290
GError *error = NULL;
292
g_return_if_fail (self->pv->imported == NULL);
294
self->pv->importing = FALSE;
296
gcr_importer_import_finish (GCR_IMPORTER (importer), result, &error);
298
g_object_get (importer, "label", &self->pv->imported, NULL);
299
gck_list_unref_free (self->pv->importers);
300
self->pv->importers = NULL;
303
g_signal_emit (self, signals[IMPORTED], 0, importer, error);
304
g_clear_error (&error);
306
update_import_button (self);
310
begin_import (GcrImportButton *self,
311
GcrImporter *importer)
313
GTlsInteraction *interaction;
316
g_return_if_fail (self->pv->importing == FALSE);
318
g_signal_emit (self, signals[IMPORTING], 0, importer);
320
self->pv->importing = TRUE;
321
g_free (self->pv->imported);
322
self->pv->imported = NULL;
324
/* TODO: Hack. Need to figure out how to pair these up... */
325
if (g_strcmp0 (G_OBJECT_TYPE_NAME (importer), "GcrPkcs11Importer") == 0) {
326
window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
327
interaction = _gcr_pkcs11_import_interaction_new (window);
328
gcr_importer_set_interaction (importer, interaction);
329
g_object_unref (interaction);
332
gcr_importer_import_async (importer,
333
self->pv->cancellable,
335
g_object_ref (self));
339
on_importer_menu_activated (GtkMenuItem *menu_item,
342
GcrImportButton *self = GCR_IMPORT_BUTTON (user_data);
343
GcrImporter *importer;
345
importer = g_object_get_qdata (G_OBJECT (menu_item), QUARK_IMPORTER);
346
g_return_if_fail (GCR_IMPORTER (importer));
347
g_return_if_fail (self->pv->importing == FALSE);
349
begin_import (self, importer);
350
update_import_button (self);
354
update_importer_menu (GcrImportButton *self)
356
GtkWidget *menu_item;
362
if (!self->pv->menu) {
363
self->pv->menu = GTK_MENU (gtk_menu_new ());
364
g_object_ref_sink (self->pv->menu);
367
children = gtk_container_get_children (GTK_CONTAINER (self->pv->menu));
368
for (l = children; l != NULL; l = g_list_next (l))
369
gtk_container_remove (GTK_CONTAINER (self->pv->menu), l->data);
370
g_list_free (children);
372
for (l = self->pv->importers; l != NULL; l = g_list_next (l)) {
373
g_object_get (l->data, "label", &label, "icon", &icon, NULL);
374
menu_item = gtk_image_menu_item_new_with_label (label);
375
g_signal_connect (menu_item, "activate", G_CALLBACK (on_importer_menu_activated), self);
376
g_object_set_qdata (G_OBJECT (menu_item), QUARK_IMPORTER, l->data);
377
image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
378
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
379
gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menu_item), TRUE);
380
gtk_widget_show (image);
381
gtk_widget_show (menu_item);
382
gtk_container_add (GTK_CONTAINER (self->pv->menu), menu_item);
383
g_object_unref (icon);
389
on_menu_position (GtkMenu *menu,
395
GcrImportButton *self = GCR_IMPORT_BUTTON (user_data);
396
GtkWidget *widget = GTK_WIDGET (self);
397
GtkAllocation allocation;
398
GtkRequisition menu_req;
399
GdkRectangle monitor;
407
g_return_if_fail (x != NULL);
408
g_return_if_fail (y != NULL);
409
g_return_if_fail (push_in != NULL);
411
gtk_widget_get_allocation (widget, &allocation);
413
if (!gtk_widget_get_has_window (widget)) {
418
window = gtk_widget_get_window (widget);
419
gdk_window_get_root_coords (window, sx, sy, &sx, &sy);
421
gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &menu_req);
422
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
425
*x = sx + allocation.width - menu_req.width;
428
screen = gtk_widget_get_screen (widget);
429
monitor_num = gdk_screen_get_monitor_at_window (screen, window);
432
gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
436
else if (*x + menu_req.width > monitor.x + monitor.width)
437
*x = monitor.x + monitor.width - menu_req.width;
439
if (monitor.y + monitor.height - *y - allocation.height >= menu_req.height)
440
*y += allocation.height;
441
else if (*y - monitor.y >= menu_req.height)
442
*y -= menu_req.height;
443
else if (monitor.y + monitor.height - *y - allocation.height > *y - monitor.y)
444
*y += allocation.height;
446
*y -= menu_req.height;
448
gtk_menu_set_monitor (menu, monitor_num);
450
toplevel = gtk_widget_get_parent (GTK_WIDGET (menu));
451
if (GTK_IS_WINDOW (toplevel) && gtk_widget_get_visible (toplevel))
452
gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
458
gcr_import_button_clicked (GtkButton *button)
460
GcrImportButton *self = GCR_IMPORT_BUTTON (button);
462
g_return_if_fail (self->pv->importing == FALSE);
463
g_return_if_fail (self->pv->importers != NULL);
465
/* More than one importer, show the menu */
466
if (self->pv->importers->next) {
467
update_importer_menu (self);
468
gtk_menu_popup (self->pv->menu, NULL, NULL, on_menu_position,
469
self, 1, gtk_get_current_event_time ());
471
/* Only one importer, import on click */
473
begin_import (self, self->pv->importers->data);
476
update_import_button (self);
480
gcr_import_button_class_init (GcrImportButtonClass *klass)
482
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
483
GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
485
gobject_class->constructed = gcr_import_button_constructed;
486
gobject_class->dispose = gcr_import_button_dispose;
487
gobject_class->finalize = gcr_import_button_finalize;
488
gobject_class->get_property = gcr_import_button_get_property;
489
gobject_class->set_property = gcr_import_button_set_property;
491
button_class->clicked = gcr_import_button_clicked;
493
g_object_class_override_property (gobject_class, PROP_LABEL, "label");
496
* GcrImportButton::importing:
497
* @self: the import button
498
* @importer: the importer that will be imported to
500
* Signal emitted when an import begins.
502
signals[IMPORTING] = g_signal_new ("importing", GCR_TYPE_IMPORT_BUTTON, G_SIGNAL_RUN_LAST,
503
G_STRUCT_OFFSET (GcrImportButtonClass, importing),
504
NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
505
G_TYPE_NONE, 1, G_TYPE_OBJECT);
508
* GcrImportButton::imported:
509
* @self: the import button
510
* @importer: the importer that was imported to
511
* @error: if import was successful %NULL, or an error
513
* Signal emitted when an import completes or fails.
515
signals[IMPORTED] = g_signal_new ("imported", GCR_TYPE_IMPORT_BUTTON, G_SIGNAL_RUN_LAST,
516
G_STRUCT_OFFSET (GcrImportButtonClass, imported),
517
NULL, NULL, _gcr_marshal_VOID__OBJECT_BOXED,
518
G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_ERROR);
520
QUARK_IMPORTER = g_quark_from_static_string ("gcr-import-button-importer");
522
g_type_class_add_private (klass, sizeof (GcrImportButtonPrivate));
526
* gcr_import_button_new:
527
* @label: (allow-none): label to display on the button
529
* Create a new #GcrImportButton.
531
* Returns: (transfer full): a newly created #GcrImportButton
534
gcr_import_button_new (const gchar *label)
536
return g_object_new (GCR_TYPE_IMPORT_BUTTON,
542
* gcr_import_button_add_parsed:
543
* @self: an import button
544
* @parsed: a parsed item
546
* Queue an item to import via the button
549
gcr_import_button_add_parsed (GcrImportButton *self,
554
g_return_if_fail (GCR_IS_IMPORT_BUTTON (self));
555
g_return_if_fail (parsed != NULL);
557
if (!self->pv->ready) {
558
self->pv->queued = g_list_prepend (self->pv->queued, gcr_parsed_ref (parsed));
559
update_import_button (self);
563
g_free (self->pv->imported);
564
self->pv->imported = NULL;
566
if (self->pv->created) {
567
importers = gcr_importer_queue_and_filter_for_parsed (self->pv->importers, parsed);
569
importers = gcr_importer_create_for_parsed (parsed);
570
self->pv->created = TRUE;
573
gck_list_unref_free (self->pv->importers);
574
self->pv->importers = importers;
576
update_import_button (self);