~carifio/seahorse/seahorse-remove-broken-help-buttons

« back to all changes in this revision

Viewing changes to libseahorse/seahorse-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-07-22 09:20:59 UTC
  • mto: (3.2.1 sid) (1.4.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 59.
  • Revision ID: james.westby@ubuntu.com-20080722092059-q336t49r9uaveij3
Tags: upstream-2.23.5
ImportĀ upstreamĀ versionĀ 2.23.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 */
22
22
 
23
23
#include <config.h>
24
 
#include <gnome.h>
 
24
 
 
25
#include <string.h>
 
26
 
 
27
#include <glib/gi18n.h>
 
28
 
25
29
#include <glade/glade.h>
26
30
#include <glade/glade-build.h>
27
31
 
28
32
#include "seahorse-widget.h"
 
33
#include "seahorse-gconf.h"
29
34
#include "seahorse-gtkstock.h"
30
35
 
31
36
#define STATUS "status"
49
54
                                     guint                  prop_id,
50
55
                                     GValue                 *value,
51
56
                                     GParamSpec             *pspec);
 
57
                                     
 
58
static GObject* seahorse_widget_constructor (GType                  type, 
 
59
                                             guint                  n_props, 
 
60
                                             GObjectConstructParam* props);                    
52
61
 
53
62
/* signal functions */
54
63
static void     widget_closed        (GtkWidget             *widget,
96
105
        parent_class = g_type_class_peek_parent (klass);
97
106
        gobject_class = G_OBJECT_CLASS (klass);
98
107
        
 
108
        gobject_class->constructor = seahorse_widget_constructor;
99
109
        gobject_class->finalize = object_finalize;
100
110
        gobject_class->set_property = object_set_property;
101
111
        gobject_class->get_property = object_get_property;
119
129
                G_CALLBACK (context_destroyed), swidget);
120
130
}
121
131
 
 
132
static GObject*  
 
133
seahorse_widget_constructor (GType type, guint n_props, GObjectConstructParam* props)
 
134
{
 
135
    SeahorseWidget *swidget;
 
136
    GObject *obj;
 
137
    
 
138
    GtkWindow *window;
 
139
    gint width, height;
 
140
    gchar *widthkey, *heightkey;
 
141
    
 
142
    obj = G_OBJECT_CLASS (parent_class)->constructor (type, n_props, props);
 
143
    swidget = SEAHORSE_WIDGET (obj);
 
144
 
 
145
    widthkey = g_strdup_printf ("%s%s%s", WINDOW_SIZE, swidget->name, "_width");
 
146
    width = seahorse_gconf_get_integer (widthkey);
 
147
    
 
148
    heightkey = g_strdup_printf ("%s%s%s", WINDOW_SIZE, swidget->name, "_height");
 
149
    height = seahorse_gconf_get_integer (heightkey);
 
150
 
 
151
    if (width != 0 && height != 0) {
 
152
        window = GTK_WINDOW (seahorse_widget_get_toplevel (swidget));
 
153
        gtk_window_resize (window, width, height);
 
154
    }
 
155
    
 
156
    g_free (widthkey);
 
157
    g_free (heightkey);
 
158
    
 
159
    return obj;
 
160
}
 
161
 
122
162
/* Disconnects callbacks, destroys main window widget,
123
163
 * and frees the xml definition and any other data */
124
164
static void
131
171
        /* Remove widget from hash and destroy hash if empty */
132
172
    if (widgets) {
133
173
        g_hash_table_remove (widgets, swidget->name);
134
 
        if (g_hash_table_size == 0) {
 
174
        if (g_hash_table_size (widgets) == 0) {
135
175
                g_hash_table_destroy (widgets);
136
176
                widgets = NULL;
137
177
        }
310
350
void
311
351
seahorse_widget_show_help (SeahorseWidget *swidget)
312
352
{
313
 
    GError *err = NULL;
 
353
    GError *error = NULL;
 
354
    gchar *document = NULL;
 
355
    GtkWidget *dialog = NULL;
314
356
 
315
357
    if (g_str_equal (swidget->name, "key-manager") || 
316
 
        g_str_equal (swidget->name, "keyserver-results"))
317
 
        gnome_help_display_with_doc_id (NULL, PACKAGE, PACKAGE, "introduction", &err);
318
 
    else
319
 
       gnome_help_display_with_doc_id (NULL, PACKAGE, PACKAGE, swidget->name, &err);
320
 
 
321
 
    if (err != NULL) {
322
 
        GtkWidget *dialog;
323
 
 
324
 
        dialog = gtk_message_dialog_new (GTK_WINDOW (seahorse_widget_get_top (swidget)), GTK_DIALOG_MODAL, 
325
 
                                         GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, 
326
 
                                         _("Could not display help: %s"),
327
 
                                         err->message);
328
 
        g_signal_connect (G_OBJECT (dialog), "response",
329
 
                          G_CALLBACK (gtk_widget_destroy), NULL);
 
358
        g_str_equal (swidget->name, "keyserver-results")) {
 
359
        document = g_strdup ("ghelp:" PACKAGE "?introduction");
 
360
    } else {
 
361
        document = g_strdup_printf ("ghelp:" PACKAGE "?%s", swidget->name);
 
362
    }
 
363
 
 
364
    if (!g_app_info_launch_default_for_uri (document, NULL, &error)) {
 
365
        dialog = gtk_message_dialog_new (GTK_WINDOW (seahorse_widget_get_toplevel (swidget)),
 
366
                                         GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 
367
                                         _("Could not display help: %s"), error->message);
 
368
        g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
330
369
        gtk_widget_show (dialog);
331
 
        g_error_free (err);
332
370
    }
 
371
 
 
372
    g_free (document);
 
373
 
 
374
    if (error)
 
375
        g_error_free (error);
 
376
}
 
377
 
 
378
const gchar*
 
379
seahorse_widget_get_name (SeahorseWidget   *swidget)
 
380
{
 
381
        g_return_val_if_fail (SEAHORSE_IS_WIDGET (swidget), NULL);
 
382
        return swidget->name;
333
383
}
334
384
 
335
385
/**
336
 
 * seahorse_widget_get_top
 
386
 * seahorse_widget_get_toplevel
337
387
 * @swidget: The seahorse widget
338
388
 * 
339
389
 * Return the top level widget in this seahorse widget
341
391
 * Returns: The top level widget
342
392
 **/
343
393
GtkWidget*      
344
 
seahorse_widget_get_top     (SeahorseWidget     *swidget)
 
394
seahorse_widget_get_toplevel (SeahorseWidget     *swidget)
345
395
{
346
396
    GtkWidget *widget = glade_xml_get_widget (swidget->xml, swidget->name);
347
397
    g_return_val_if_fail (widget != NULL, NULL);
367
417
{
368
418
    GtkWidget *widget;
369
419
 
370
 
    if (swidget->ui)
371
 
        gtk_ui_manager_ensure_update (swidget->ui);
372
 
 
373
420
    widget = glade_xml_get_widget (swidget->xml, swidget->name);
374
421
    g_return_if_fail (widget != NULL);
375
422
    gtk_widget_show (widget);
406
453
void
407
454
seahorse_widget_destroy (SeahorseWidget *swidget)
408
455
{
 
456
    GtkWidget *widget;
 
457
    gchar *widthkey, *heightkey;
 
458
    gint width, height;
 
459
 
409
460
    g_return_if_fail (swidget != NULL && SEAHORSE_IS_WIDGET (swidget));
 
461
    
 
462
    /* Save window size */
 
463
    widget = seahorse_widget_get_toplevel (swidget);
 
464
    gtk_window_get_size (GTK_WINDOW (widget), &width, &height);
 
465
    
 
466
    widthkey = g_strdup_printf ("%s%s%s", WINDOW_SIZE, swidget->name, "_width");
 
467
    seahorse_gconf_set_integer (widthkey, width);
 
468
    
 
469
    heightkey = g_strdup_printf ("%s%s%s", WINDOW_SIZE, swidget->name, "_height");
 
470
    seahorse_gconf_set_integer (heightkey, height);
 
471
    
 
472
    g_free (widthkey);
 
473
    g_free (heightkey);
 
474
    
 
475
    /* Destroy Widget */
410
476
    if (!swidget->destroying) {
411
477
        swidget->destroying = TRUE;
412
478
        g_object_unref (swidget);
413
479
    }
414
480
}
415
481
 
416
 
/* UI MANAGER CODE ---------------------------------------------------------- */
417
 
 
418
 
static void
419
 
ui_add_widget (GtkUIManager *ui, GtkWidget *widget, SeahorseWidget *swidget)
420
 
{
421
 
    GtkWidget *holder;
422
 
    const gchar *name;
423
 
    
424
 
    /* We automatically add menus and toolbars */
425
 
    if (GTK_IS_MENU_BAR (widget))
426
 
        name = "menu-placeholder";
427
 
    else
428
 
        name = "toolbar-placeholder";
429
 
    
430
 
    if (name != NULL) {
431
 
        /* Find the appropriate position in the glade file */
432
 
        holder = glade_xml_get_widget (swidget->xml, name);
433
 
        if (holder != NULL)
434
 
            gtk_container_add (GTK_CONTAINER (holder), widget);
435
 
        else
436
 
            g_warning ("no place holder found for: %s", name);
437
 
    }
438
 
}
439
 
 
440
 
static void
441
 
ui_load (SeahorseWidget *swidget)
442
 
{
443
 
    GtkWidget *w;
444
 
    GError *err = NULL;
445
 
    gchar *path;
446
 
    
447
 
    if (!swidget->ui) {
448
 
        
449
 
        /* Load the menu/toolbar description file */
450
 
        swidget->ui = gtk_ui_manager_new ();
451
 
        path = g_strdup_printf ("%sseahorse-%s.ui", SEAHORSE_GLADEDIR, swidget->name);
452
 
        gtk_ui_manager_add_ui_from_file (swidget->ui, path, &err);
453
 
                g_free (path);
454
 
        
455
 
        if (err) {
456
 
            g_warning ("couldn't load ui description for '%s': %s", swidget->name, err->message);
457
 
            g_error_free (err);
458
 
            return;
459
 
        }
460
 
 
461
 
        /* The widgets get added in an idle loop later */
462
 
        g_signal_connect (swidget->ui, "add-widget", G_CALLBACK (ui_add_widget), swidget);
463
 
        
464
 
        /* Attach accelerators to the window */
465
 
        w = glade_xml_get_widget (swidget->xml, swidget->name);
466
 
        if (GTK_IS_WINDOW (w))
467
 
            gtk_window_add_accel_group (GTK_WINDOW (w), gtk_ui_manager_get_accel_group (swidget->ui));
468
 
    }    
469
 
}
470
 
 
471
 
static void
472
 
cleanup_actions (GtkActionGroup *group)
473
 
{
474
 
    GList *actions, *l;
475
 
    
476
 
    #define ELIPSIS "..."
477
 
    #define ELIPSIS_LEN 3
478
 
    
479
 
    actions = gtk_action_group_list_actions (group);    
480
 
    
481
 
    for (l = actions; l; l = g_list_next (l)) {
482
 
        GtkAction *action = GTK_ACTION (l->data);
483
 
        gchar *label;
484
 
        guint len;
485
 
        
486
 
        /* Remove the ellipsis from the end of action labels if present */
487
 
        g_object_get (action, "short-label", &label, NULL);
488
 
        if (label) {
489
 
            len = strlen (label);
490
 
            if (strcmp (ELIPSIS, label + (len - ELIPSIS_LEN)) == 0) {
491
 
                label[len - ELIPSIS_LEN] = 0;
492
 
                g_object_set (action, "short-label", label, NULL);
493
 
            }
494
 
            g_free (label);
495
 
        }
496
 
    }
497
 
 
498
 
    g_list_free (actions);    
499
 
}
500
 
 
501
 
/**
502
 
 * seahorse_widget_get_ui_widget
503
 
 * @swidget: The #SeahorseWidget.
504
 
 * @path: The path to the widget. See gtk_ui_manager_get_widget
505
 
 * 
506
 
 * Returns a piece of generated UI. Note this doesn't look in the glade
507
 
 * file but rather looks in the GtkUIManager UI. If no UI has been loaded 
508
 
 * then one will be loaded. The UI file has the same name as the glade file 
509
 
 * but with a 'ui' extension. 
510
 
 */
511
 
GtkWidget*
512
 
seahorse_widget_get_ui_widget (SeahorseWidget *swidget, const gchar *path)
513
 
{
514
 
    g_return_val_if_fail (SEAHORSE_IS_WIDGET (swidget), NULL);
515
 
    
516
 
    ui_load (swidget);    
517
 
    g_return_val_if_fail (swidget->ui, NULL);
518
 
    
519
 
    return gtk_ui_manager_get_widget (swidget->ui, path);
520
 
}
521
 
 
522
 
/**
523
 
 * seahorse_widget_add_actions
524
 
 * @swidget: The #SeahorseWidget.
525
 
 * @actions: A #GtkActionGroup to add to the UI.
526
 
 * 
527
 
 * Adds a GtkActionGroup to this widget's GtkUIManager UI. If no UI
528
 
 * has been loaded then one will be loaded. The UI file has the same
529
 
 * name as the glade file but with a 'ui' extension. 
530
 
 */
531
 
void             
532
 
seahorse_widget_add_actions (SeahorseWidget *swidget, GtkActionGroup *actions)
533
 
{
534
 
    g_return_if_fail (SEAHORSE_IS_WIDGET (swidget));
535
 
    
536
 
    ui_load (swidget);    
537
 
    g_return_if_fail (swidget->ui);
538
 
 
539
 
    cleanup_actions (actions);
540
 
    gtk_ui_manager_insert_action_group (swidget->ui, actions, -1);
541
 
}
542
 
 
543
 
/** 
544
 
 * seahorse_widget_find_actions
545
 
 * @swidget: The #SeahorseWidget.
546
 
 * @name: The name of the action group.
547
 
 * 
548
 
 * Find an #GtkActionGroup previously added to this widget.
549
 
 * 
550
 
 * Returns: The action group.
551
 
 */
552
 
GtkActionGroup*
553
 
seahorse_widget_find_actions (SeahorseWidget *swidget, const gchar *name)
554
 
{
555
 
    GList *l;
556
 
    
557
 
    g_return_val_if_fail (SEAHORSE_IS_WIDGET (swidget), NULL);
558
 
    
559
 
    if (!swidget->ui)
560
 
        return NULL;
561
 
    
562
 
    for (l = gtk_ui_manager_get_action_groups (swidget->ui); l; l = g_list_next (l)) {
563
 
        if (g_str_equal (gtk_action_group_get_name (GTK_ACTION_GROUP (l->data)), name)) 
564
 
            return GTK_ACTION_GROUP (l->data);
565
 
    }
566
 
    
567
 
    return NULL;
 
482
void
 
483
seahorse_widget_connect_glade_signal (SeahorseWidget *swidget, const char *event, 
 
484
                                      GtkCallback callback, gpointer userdata)
 
485
{
 
486
        g_return_if_fail (SEAHORSE_IS_WIDGET (swidget));
 
487
        glade_xml_signal_connect_data (swidget->xml, event, G_CALLBACK (callback), userdata);
568
488
}