~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to gcr/gcr-renderer.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * gnome-keyring
3
 
 *
4
 
 * Copyright (C) 2010 Stefan Walter
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 * 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include "gcr-renderer.h"
25
 
 
26
 
#include "gck/gck.h"
27
 
 
28
 
#include <gtk/gtk.h>
29
 
 
30
 
enum {
31
 
        DATA_CHANGED,
32
 
        LAST_SIGNAL
33
 
};
34
 
 
35
 
static guint signals[LAST_SIGNAL] = { 0 };
36
 
 
37
 
typedef struct _GcrRegistered {
38
 
        GckAttributes *attrs;
39
 
        GType renderer_type;
40
 
} GcrRegistered;
41
 
 
42
 
static GArray *registered_renderers = NULL;
43
 
static gboolean registered_sorted = FALSE;
44
 
 
45
 
static void
46
 
gcr_renderer_base_init (gpointer gobject_iface)
47
 
{
48
 
        static gboolean initialized = FALSE;
49
 
        if (!initialized) {
50
 
 
51
 
                g_object_interface_install_property (gobject_iface,
52
 
                         g_param_spec_string ("label", "Label", "The label for the renderer",
53
 
                                              "", G_PARAM_READWRITE));
54
 
 
55
 
                g_object_interface_install_property (gobject_iface,
56
 
                         g_param_spec_boxed ("attributes", "Attributes", "The data displayed in the renderer",
57
 
                                             GCK_TYPE_ATTRIBUTES, G_PARAM_READWRITE));
58
 
 
59
 
                signals[DATA_CHANGED] = g_signal_new ("data-changed", GCR_TYPE_RENDERER, G_SIGNAL_RUN_LAST,
60
 
                                                      G_STRUCT_OFFSET (GcrRendererIface, data_changed),
61
 
                                                      NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
62
 
 
63
 
                initialized = TRUE;
64
 
        }
65
 
}
66
 
 
67
 
GType
68
 
gcr_renderer_get_type (void)
69
 
{
70
 
        static GType type = 0;
71
 
        if (!type) {
72
 
                static const GTypeInfo info = {
73
 
                        sizeof (GcrRendererIface),
74
 
                        gcr_renderer_base_init,  /* base init */
75
 
                        NULL,                    /* base finalize */
76
 
                };
77
 
                type = g_type_register_static (G_TYPE_INTERFACE, "GcrRendererIface", &info, 0);
78
 
        }
79
 
 
80
 
        return type;
81
 
}
82
 
 
83
 
void
84
 
gcr_renderer_render (GcrRenderer *self, GcrViewer *viewer)
85
 
{
86
 
        g_return_if_fail (GCR_IS_RENDERER (self));
87
 
        g_return_if_fail (GCR_RENDERER_GET_INTERFACE (self)->render);
88
 
        GCR_RENDERER_GET_INTERFACE (self)->render (self, viewer);
89
 
}
90
 
 
91
 
void
92
 
gcr_renderer_emit_data_changed (GcrRenderer *self)
93
 
{
94
 
        g_return_if_fail (GCR_IS_RENDERER (self));
95
 
        g_signal_emit (self, signals[DATA_CHANGED], 0);
96
 
}
97
 
 
98
 
static gint
99
 
sort_registered_by_n_attrs (gconstpointer a, gconstpointer b)
100
 
{
101
 
        const GcrRegistered *ra = a;
102
 
        const GcrRegistered *rb = b;
103
 
        gulong na, nb;
104
 
 
105
 
        g_assert (a);
106
 
        g_assert (b);
107
 
 
108
 
        na = gck_attributes_count (ra->attrs);
109
 
        nb = gck_attributes_count (rb->attrs);
110
 
 
111
 
        /* Note we're sorting in reverse order */
112
 
        if (na < nb)
113
 
                return 1;
114
 
        return (na == nb) ? 0 : -1;
115
 
}
116
 
 
117
 
GcrRenderer*
118
 
gcr_renderer_create (const gchar *label, GckAttributes *attrs)
119
 
{
120
 
        GcrRegistered *registered;
121
 
        gboolean matched;
122
 
        gulong n_attrs;
123
 
        gulong j;
124
 
        gsize i;
125
 
 
126
 
        g_return_val_if_fail (attrs, NULL);
127
 
 
128
 
        if (!registered_renderers)
129
 
                return NULL;
130
 
 
131
 
        if (!registered_sorted) {
132
 
                g_array_sort (registered_renderers, sort_registered_by_n_attrs);
133
 
                registered_sorted = TRUE;
134
 
        }
135
 
 
136
 
        for (i = 0; i < registered_renderers->len; ++i) {
137
 
                registered = &(g_array_index (registered_renderers, GcrRegistered, i));
138
 
                n_attrs = gck_attributes_count (registered->attrs);
139
 
 
140
 
                matched = TRUE;
141
 
 
142
 
                for (j = 0; j < n_attrs; ++j) {
143
 
                        if (!gck_attributes_contains (attrs, gck_attributes_at (registered->attrs, j))) {
144
 
                                matched = FALSE;
145
 
                                break;
146
 
                        }
147
 
                }
148
 
 
149
 
                if (matched)
150
 
                        return g_object_new (registered->renderer_type, "label", label,
151
 
                                             "attributes", attrs, NULL);
152
 
        }
153
 
 
154
 
        return NULL;
155
 
}
156
 
 
157
 
void
158
 
gcr_renderer_register (GType renderer_type, GckAttributes *attrs)
159
 
{
160
 
        GcrRegistered registered;
161
 
 
162
 
        if (!registered_renderers)
163
 
                registered_renderers = g_array_new (FALSE, FALSE, sizeof (GcrRegistered));
164
 
 
165
 
        registered.renderer_type = renderer_type;
166
 
        registered.attrs = gck_attributes_ref (attrs);
167
 
        g_array_append_val (registered_renderers, registered);
168
 
        registered_sorted = FALSE;
169
 
}