forked from linuxmint/nemo-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseahorse-widget.c
381 lines (311 loc) · 11 KB
/
seahorse-widget.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Seahorse
*
* Copyright (C) 2002 Jacob Perkins
* Copyright (C) 2005 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, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <string.h>
#include <glib/gi18n.h>
#include "seahorse-widget.h"
#define STATUS "status"
enum {
PROP_0,
PROP_NAME
};
enum {
DESTROY,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void class_init (SeahorseWidgetClass *klass);
static void object_init (SeahorseWidget *swidget);
static void object_dispose (GObject *object);
static void object_finalize (GObject *gobject);
static void object_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void object_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
/* signal functions */
G_MODULE_EXPORT void on_widget_closed (GtkWidget *widget,
SeahorseWidget *swidget);
G_MODULE_EXPORT void on_widget_help (GtkWidget *widget,
SeahorseWidget *swidget);
G_MODULE_EXPORT gboolean on_widget_delete_event (GtkWidget *widget,
GdkEvent *event,
SeahorseWidget *swidget);
static GObjectClass *parent_class = NULL;
/* Hash of widgets with name as key */
static GHashTable *widgets = NULL;
GType
seahorse_widget_get_type (void)
{
static GType widget_type = 0;
if (!widget_type) {
static const GTypeInfo widget_info = {
sizeof (SeahorseWidgetClass), NULL, NULL,
(GClassInitFunc) class_init,
NULL, NULL, sizeof (SeahorseWidget), 0, (GInstanceInitFunc) object_init
};
widget_type = g_type_register_static (G_TYPE_OBJECT, "SeahorseWidget",
&widget_info, 0);
}
return widget_type;
}
static void
seahorse_widget_constructed (GObject *object)
{
SeahorseWidget *self = SEAHORSE_WIDGET (object);
GtkWindow *window;
gint width, height;
gchar *path;
G_OBJECT_CLASS (parent_class)->constructed (object);
/* Load window size for windows that aren't dialogs */
window = GTK_WINDOW (seahorse_widget_get_toplevel (self));
if (!GTK_IS_DIALOG (window)) {
path = g_strdup_printf ("/org/nemo/plugins/seahorse/windows/%s/", self->name);
self->settings = g_settings_new_with_path ("org.nemo.plugins.seahorse.window", path);
g_free (path);
width = g_settings_get_int (self->settings, "width");
height = g_settings_get_int (self->settings, "height");
if (width > 0 && height > 0)
gtk_window_resize (window, width, height);
}
}
static void
class_init (SeahorseWidgetClass *klass)
{
GObjectClass *gobject_class;
parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->constructed = seahorse_widget_constructed;
gobject_class->dispose = object_dispose;
gobject_class->finalize = object_finalize;
gobject_class->set_property = object_set_property;
gobject_class->get_property = object_get_property;
g_object_class_install_property (gobject_class, PROP_NAME,
g_param_spec_string ("name", "Widget name", "Name of gtkbuilder file and main widget",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
signals[DESTROY] = g_signal_new ("destroy", SEAHORSE_TYPE_WIDGET,
G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (SeahorseWidgetClass, destroy),
NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
static void
object_init (SeahorseWidget *swidget)
{
}
static void
object_dispose (GObject *object)
{
SeahorseWidget *swidget = SEAHORSE_WIDGET (object);
if (!swidget->in_destruction) {
swidget->in_destruction = TRUE;
g_signal_emit (swidget, signals[DESTROY], 0);
swidget->in_destruction = FALSE;
}
G_OBJECT_CLASS (parent_class)->dispose (object);
}
/* Disconnects callbacks, destroys main window widget,
* and frees the xml definition and any other data */
static void
object_finalize (GObject *gobject)
{
SeahorseWidget *swidget;
swidget = SEAHORSE_WIDGET (gobject);
/* Remove widget from hash and destroy hash if empty */
if (widgets) {
g_hash_table_remove (widgets, swidget->name);
if (g_hash_table_size (widgets) == 0) {
g_hash_table_destroy (widgets);
widgets = NULL;
}
}
if (seahorse_widget_get_widget (swidget, swidget->name))
gtk_widget_destroy (GTK_WIDGET (seahorse_widget_get_widget (swidget, swidget->name)));
g_object_unref (swidget->gtkbuilder);
swidget->gtkbuilder = NULL;
g_clear_object (&swidget->settings);
g_free (swidget->name);
G_OBJECT_CLASS (parent_class)->finalize (gobject);
}
static void
object_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
SeahorseWidget *swidget;
GError *error = NULL;
GtkWidget *w;
char *path;
swidget = SEAHORSE_WIDGET (object);
switch (prop_id) {
/* Loads gtkbuilder xml definition from name, connects common callbacks */
case PROP_NAME:
g_return_if_fail (swidget->name == NULL);
swidget->name = g_value_dup_string (value);
path = g_strdup_printf ("%sseahorse-%s.xml",
SEAHORSE_UIDIR, swidget->name);
swidget->gtkbuilder = gtk_builder_new ();
gtk_builder_add_from_file (swidget->gtkbuilder, path, &error);
if (error != NULL) {
g_warning ("couldn't load ui file: %s", error->message);
g_error_free (error);
}
g_free (path);
g_return_if_fail (swidget->gtkbuilder != NULL);
gtk_builder_connect_signals (swidget->gtkbuilder, swidget);
w = GTK_WIDGET (seahorse_widget_get_widget (swidget, swidget->name));
/*TODO: glade_xml_ensure_accel (swidget->gtkbuilder);*/
gtk_window_set_icon_name (GTK_WINDOW (w), "seahorse");
break;
}
}
static void
object_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
SeahorseWidget *swidget;
swidget = SEAHORSE_WIDGET (object);
switch (prop_id) {
case PROP_NAME:
g_value_set_string (value, swidget->name);
break;
default:
break;
}
}
/* Destroys widget */
G_MODULE_EXPORT void
on_widget_closed (GtkWidget *widget, SeahorseWidget *swidget)
{
seahorse_widget_destroy (swidget);
}
/* Closed widget */
G_MODULE_EXPORT gboolean
on_widget_delete_event (GtkWidget *widget, GdkEvent *event, SeahorseWidget *swidget)
{
on_widget_closed (widget, swidget);
return FALSE; /* propogate event */
}
/**
* seahorse_widget_new:
* @name: Name of widget, filename part of gtkbuilder file, and name of main window
* @parent: GtkWindow to make the parent of the new swidget
*
* Creates a new #SeahorseWidget.
*
* Returns: The new #SeahorseWidget, or NULL if the widget already exists
**/
SeahorseWidget*
seahorse_widget_new (const gchar *name, GtkWindow *parent)
{
/* Check if have widget hash */
SeahorseWidget *swidget = seahorse_widget_find (name);
GtkWindow *window;
/* If widget already exists, present */
if (swidget != NULL) {
gtk_window_present (GTK_WINDOW (seahorse_widget_get_widget (swidget, swidget->name)));
return NULL;
}
/* If widget doesn't already exist, create & insert into hash */
swidget = g_object_new (SEAHORSE_TYPE_WIDGET, "name", name, NULL);
if(!widgets)
widgets = g_hash_table_new ((GHashFunc)g_str_hash, (GCompareFunc)g_str_equal);
g_hash_table_insert (widgets, g_strdup (name), swidget);
if (parent != NULL) {
window = GTK_WINDOW (seahorse_widget_get_widget (swidget, swidget->name));
gtk_window_set_transient_for (window, parent);
}
return swidget;
}
/**
* seahorse_widget_new_allow_multiple:
* @name: Name of widget, filename part of gtkbuilder file, and name of main window
* @parent: GtkWindow to make the parent of the new swidget
*
* Creates a new #SeahorseWidget without checking if it already exists.
*
* Returns: The new #SeahorseWidget
**/
SeahorseWidget*
seahorse_widget_new_allow_multiple (const gchar *name, GtkWindow *parent)
{
GtkWindow *window;
SeahorseWidget *swidget = g_object_new (SEAHORSE_TYPE_WIDGET, "name", name, NULL);
if (parent != NULL) {
window = GTK_WINDOW (seahorse_widget_get_widget (swidget, swidget->name));
gtk_window_set_transient_for (window, parent);
}
gtk_builder_connect_signals (swidget->gtkbuilder, NULL);
return swidget;
}
SeahorseWidget*
seahorse_widget_find (const gchar *name)
{
/* Check if have widget hash */
if (widgets != NULL)
return SEAHORSE_WIDGET (g_hash_table_lookup (widgets, name));
return NULL;
}
/**
* seahorse_widget_get_toplevel
* @swidget: The seahorse widget
*
* Return the top level widget in this seahorse widget
*
* Returns: The top level widget
**/
GtkWidget*
seahorse_widget_get_toplevel (SeahorseWidget *swidget)
{
GtkWidget *widget = GTK_WIDGET (seahorse_widget_get_widget (swidget, swidget->name));
g_return_val_if_fail (widget != NULL, NULL);
return widget;
}
GtkWidget*
seahorse_widget_get_widget (SeahorseWidget *swidget, const char *identifier)
{
GtkWidget *widget = GTK_WIDGET (gtk_builder_get_object (swidget->gtkbuilder, identifier));
if (widget == NULL)
g_warning ("could not find widget %s for seahorse-%s.xml", identifier, swidget->name);
return widget;
}
/**
* seahorse_widget_destroy:
* @swidget: #SeahorseWidget to destroy
*
* Unrefs @swidget.
**/
void
seahorse_widget_destroy (SeahorseWidget *self)
{
GtkWidget *widget;
gint width, height;
g_return_if_fail (self != NULL && SEAHORSE_IS_WIDGET (self));
widget = seahorse_widget_get_toplevel (self);
/* Save window size */
if (self->settings) {
gtk_window_get_size (GTK_WINDOW (widget), &width, &height);
g_settings_set_int (self->settings, "width", width);
g_settings_set_int (self->settings, "height", height);
}
/* Destroy Widget */
if (!self->destroying) {
self->destroying = TRUE;
gtk_widget_destroy (seahorse_widget_get_toplevel (self));
g_object_unref (self);
}
}