vinagre-prefs.c 15.1 KB
Newer Older
Jonh Wendell's avatar
Jonh Wendell committed
1
2
3
4
/*
 * vinagre-prefs.c
 * This file is part of vinagre
 *
Jonh Wendell's avatar
Jonh Wendell committed
5
 * Copyright (C) Jonh Wendell 2008,2009 <wendell@bani.com.br>
Jonh Wendell's avatar
Jonh Wendell committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * 
 * vinagre-prefs.c 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.
 * 
 * vinagre-prefs.c 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 <gconf/gconf-client.h>
#include <glib/gi18n.h>
23
#include <glade/glade.h>
Jonh Wendell's avatar
Jonh Wendell committed
24
#include "vinagre-prefs.h"
25
#include "vinagre-utils.h"
Jonh Wendell's avatar
Jonh Wendell committed
26
27
28
29
30
31
32

#define VINAGRE_BASE_KEY		"/apps/vinagre"
#define VM_ALWAYS_SHOW_TABS		VINAGRE_BASE_KEY "/always_show_tabs"
#define VM_SHARED_FLAG			VINAGRE_BASE_KEY "/shared_flag"
#define VM_TOOLBAR_VISIBLE	 	VINAGRE_BASE_KEY "/toolbar_visible"
#define VM_STATUSBAR_VISIBLE		VINAGRE_BASE_KEY "/statusbar_visible"
#define VM_SIDE_PANEL_VISIBLE		VINAGRE_BASE_KEY "/side_pane_visible"
33
#define VM_SHOW_ACCELS			VINAGRE_BASE_KEY "/show_accels"
34
#define VM_HISTORY_SIZE			VINAGRE_BASE_KEY "/history_size"
Jonh Wendell's avatar
Jonh Wendell committed
35
36
37
38
39
40

#define VM_WINDOW_STATE			VINAGRE_BASE_KEY "/window_state"
#define VM_WINDOW_WIDTH			VINAGRE_BASE_KEY "/window_width"
#define VM_WINDOW_HEIGHT		VINAGRE_BASE_KEY "/window_height"
#define VM_SIDE_PANEL_SIZE		VINAGRE_BASE_KEY "/side_panel_size"

Jonh Wendell's avatar
Jonh Wendell committed
41
42
43
#define VINAGRE_PLUGINS_DIR		VINAGRE_BASE_KEY "/plugins"
#define VM_ACTIVE_PLUGINS		VINAGRE_PLUGINS_DIR "/active-plugins"

Jonh Wendell's avatar
Jonh Wendell committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
struct _VinagrePrefsPrivate
{
  GConfClient *gconf_client;
};

/* Properties */
enum
{
  PROP_0,
  PROP_SHARED_FLAG,
  PROP_ALWAYS_SHOW_TABS,
  PROP_TOOLBAR_VISIBLE,
  PROP_STATUSBAR_VISIBLE,
  PROP_SIDE_PANEL_VISIBLE,
  PROP_WINDOW_STATE,
  PROP_WINDOW_WIDTH,
  PROP_WINDOW_HEIGHT,
61
  PROP_SIDE_PANEL_SIZE,
62
  PROP_SHOW_ACCELS,
Jonh Wendell's avatar
Jonh Wendell committed
63
64
  PROP_HISTORY_SIZE,
  PROP_ACTIVE_PLUGINS
Jonh Wendell's avatar
Jonh Wendell committed
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
};

G_DEFINE_TYPE (VinagrePrefs, vinagre_prefs, G_TYPE_OBJECT);

static VinagrePrefs *prefs_singleton = NULL;

VinagrePrefs *
vinagre_prefs_get_default (void)
{
  if (G_UNLIKELY (!prefs_singleton))
    prefs_singleton = VINAGRE_PREFS (g_object_new (VINAGRE_TYPE_PREFS,
                                                   NULL));
  return prefs_singleton;
}

static gboolean
vinagre_prefs_get_bool (VinagrePrefs *prefs, const gchar* key, gboolean def)
{
  GError* error = NULL;
  GConfValue* val;

  val = gconf_client_get (prefs->priv->gconf_client, key, &error);

  if (val != NULL)
    {
      gboolean retval = def;

      g_return_val_if_fail (error == NULL, retval);
      
      if (val->type == GCONF_VALUE_BOOL)
        retval = gconf_value_get_bool (val);

      gconf_value_free (val);

      return retval;
    }
  else
      return def;
}

static gboolean
vinagre_prefs_get_int (VinagrePrefs *prefs, const gchar* key, gint def)
{
  GError* error = NULL;
  GConfValue* val;

  val = gconf_client_get (prefs->priv->gconf_client, key, &error);

  if (val != NULL)
    {
      gint retval = def;

      g_return_val_if_fail (error == NULL, retval);
      
      if (val->type == GCONF_VALUE_INT)
        retval = gconf_value_get_int (val);

      gconf_value_free (val);

      return retval;
    }
  else
      return def;
}

Jonh Wendell's avatar
Jonh Wendell committed
130
131
132
133
134
135
136
137
138
static GSList *
vinagre_prefs_get_list (VinagrePrefs *prefs, const gchar* key)
{
  return gconf_client_get_list (prefs->priv->gconf_client,
				key,
				GCONF_VALUE_STRING,
				NULL);
}

Jonh Wendell's avatar
Jonh Wendell committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
static void		 
vinagre_prefs_set_bool (VinagrePrefs *prefs, const gchar* key, gboolean value)
{
  g_return_if_fail (gconf_client_key_is_writable (
		    prefs->priv->gconf_client, key, NULL));
			
  gconf_client_set_bool (prefs->priv->gconf_client, key, value, NULL);
}

static void		 
vinagre_prefs_set_int (VinagrePrefs *prefs, const gchar* key, gint value)
{
  g_return_if_fail (gconf_client_key_is_writable (
		    prefs->priv->gconf_client, key, NULL));
			
  gconf_client_set_int (prefs->priv->gconf_client, key, value, NULL);
}

Jonh Wendell's avatar
Jonh Wendell committed
157
158
159
160
161
162
163
164
165
166
167
168
169
static void
vinagre_prefs_set_list (VinagrePrefs *prefs, const gchar* key, GSList *list)
{
  g_return_if_fail (gconf_client_key_is_writable (
		    prefs->priv->gconf_client, key, NULL));

  gconf_client_set_list (prefs->priv->gconf_client,
			 key,
			 GCONF_VALUE_STRING,
			 list,
			 NULL);
}

Jonh Wendell's avatar
Jonh Wendell committed
170
171
172
173
174
175
176
177
178
static void
vinagre_prefs_always_show_tabs_notify (GConfClient           *client,
				       guint                  cnx_id,
				       GConfEntry            *entry,
				       VinagrePrefs          *prefs)
{
  g_object_notify (G_OBJECT (prefs), "always-show-tabs");
}

179
180
181
182
183
184
185
186
187
static void
vinagre_prefs_show_accels_notify (GConfClient           *client,
				  guint                  cnx_id,
				  GConfEntry            *entry,
				  VinagrePrefs          *prefs)
{
  g_object_notify (G_OBJECT (prefs), "show-accels");
}

Jonh Wendell's avatar
Jonh Wendell committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
static void
vinagre_prefs_init (VinagrePrefs *prefs)
{
  prefs->priv = G_TYPE_INSTANCE_GET_PRIVATE (prefs, VINAGRE_TYPE_PREFS, VinagrePrefsPrivate);

  prefs->priv->gconf_client = gconf_client_get_default ();
  if (prefs->priv->gconf_client == NULL)
    g_critical (_("Cannot initialize preferences manager."));

  gconf_client_add_dir (prefs->priv->gconf_client,
			VINAGRE_BASE_KEY,
			GCONF_CLIENT_PRELOAD_ONELEVEL,
			NULL);

  gconf_client_notify_add (prefs->priv->gconf_client,
			   VM_ALWAYS_SHOW_TABS,
                           (GConfClientNotifyFunc) vinagre_prefs_always_show_tabs_notify,
                           prefs, NULL, NULL);
206
207
208
209
  gconf_client_notify_add (prefs->priv->gconf_client,
			   VM_SHOW_ACCELS,
                           (GConfClientNotifyFunc) vinagre_prefs_show_accels_notify,
                           prefs, NULL, NULL);
Jonh Wendell's avatar
Jonh Wendell committed
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

}

static void
vinagre_prefs_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  VinagrePrefs *prefs = VINAGRE_PREFS (object);

  switch (prop_id)
    {
      case PROP_SHARED_FLAG:
	vinagre_prefs_set_bool (prefs, VM_SHARED_FLAG, g_value_get_boolean (value));
	break;
      case PROP_ALWAYS_SHOW_TABS:
	vinagre_prefs_set_bool (prefs, VM_ALWAYS_SHOW_TABS, g_value_get_boolean (value));
	break;
      case PROP_TOOLBAR_VISIBLE:
	vinagre_prefs_set_bool (prefs, VM_TOOLBAR_VISIBLE, g_value_get_boolean (value));
	break;
      case PROP_STATUSBAR_VISIBLE:
	vinagre_prefs_set_bool (prefs, VM_STATUSBAR_VISIBLE, g_value_get_boolean (value));
	break;
      case PROP_SIDE_PANEL_VISIBLE:
	vinagre_prefs_set_bool (prefs, VM_SIDE_PANEL_VISIBLE, g_value_get_boolean (value));
	break;
      case PROP_WINDOW_STATE:
	vinagre_prefs_set_int (prefs, VM_WINDOW_STATE, g_value_get_int (value));
	break;
      case PROP_WINDOW_WIDTH:
	vinagre_prefs_set_int (prefs, VM_WINDOW_WIDTH, g_value_get_int (value));
	break;
      case PROP_WINDOW_HEIGHT:
	vinagre_prefs_set_int (prefs, VM_WINDOW_HEIGHT, g_value_get_int (value));
	break;
      case PROP_SIDE_PANEL_SIZE:
	vinagre_prefs_set_int (prefs, VM_SIDE_PANEL_SIZE, g_value_get_int (value));
	break;
247
248
249
      case PROP_SHOW_ACCELS:
	vinagre_prefs_set_bool (prefs, VM_SHOW_ACCELS, g_value_get_boolean (value));
	break;
250
251
252
      case PROP_HISTORY_SIZE:
	vinagre_prefs_set_int (prefs, VM_HISTORY_SIZE, g_value_get_int (value));
	break;
Jonh Wendell's avatar
Jonh Wendell committed
253
254
255
      case PROP_ACTIVE_PLUGINS:
	vinagre_prefs_set_list (prefs, VM_ACTIVE_PLUGINS, g_value_get_pointer (value));
	break;
Jonh Wendell's avatar
Jonh Wendell committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
      default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	break;
    }
}

static void
vinagre_prefs_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  VinagrePrefs *prefs = VINAGRE_PREFS (object);

  switch (prop_id)
    {
      case PROP_SHARED_FLAG:
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_SHARED_FLAG, TRUE));
	break;
      case PROP_ALWAYS_SHOW_TABS:
273
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_ALWAYS_SHOW_TABS, FALSE));
Jonh Wendell's avatar
Jonh Wendell committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
	break;
      case PROP_TOOLBAR_VISIBLE:
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_TOOLBAR_VISIBLE, TRUE));
	break;
      case PROP_STATUSBAR_VISIBLE:
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_STATUSBAR_VISIBLE, TRUE));
	break;
      case PROP_SIDE_PANEL_VISIBLE:
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_SIDE_PANEL_VISIBLE, TRUE));
	break;
      case PROP_WINDOW_STATE:
	g_value_set_int (value, vinagre_prefs_get_int (prefs, VM_WINDOW_STATE, 0));
	break;
      case PROP_WINDOW_WIDTH:
	g_value_set_int (value, vinagre_prefs_get_int (prefs, VM_WINDOW_WIDTH, 650));
	break;
      case PROP_WINDOW_HEIGHT:
	g_value_set_int (value, vinagre_prefs_get_int (prefs, VM_WINDOW_HEIGHT, 500));
	break;
      case PROP_SIDE_PANEL_SIZE:
	g_value_set_int (value, vinagre_prefs_get_int (prefs, VM_SIDE_PANEL_SIZE, 200));
	break;
296
297
298
      case PROP_SHOW_ACCELS:
	g_value_set_boolean (value, vinagre_prefs_get_bool (prefs, VM_SHOW_ACCELS, TRUE));
	break;
299
300
301
      case PROP_HISTORY_SIZE:
	g_value_set_int (value, vinagre_prefs_get_int (prefs, VM_HISTORY_SIZE, 15));
	break;
Jonh Wendell's avatar
Jonh Wendell committed
302
303
304
      case PROP_ACTIVE_PLUGINS:
	g_value_set_pointer (value, vinagre_prefs_get_list (prefs, VM_ACTIVE_PLUGINS));
	break;
Jonh Wendell's avatar
Jonh Wendell committed
305
306
307
308
309
310
      default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	break;
    }
}

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
static void
vinagre_prefs_dispose (GObject *object)
{
  VinagrePrefs *prefs = VINAGRE_PREFS (object);

  if (prefs->priv->gconf_client)
    {
      gconf_client_remove_dir (prefs->priv->gconf_client,
			       VINAGRE_BASE_KEY,
			       NULL);
      g_object_unref (prefs->priv->gconf_client);
      prefs->priv->gconf_client = NULL;
    }

  G_OBJECT_CLASS (vinagre_prefs_parent_class)->dispose (object);
}


Jonh Wendell's avatar
Jonh Wendell committed
329
330
331
332
333
334
335
336
static void
vinagre_prefs_class_init (VinagrePrefsClass *klass)
{
  GObjectClass* object_class = G_OBJECT_CLASS (klass);
  GObjectClass* parent_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (VinagrePrefsPrivate));

337
  object_class->dispose = vinagre_prefs_dispose;
Jonh Wendell's avatar
Jonh Wendell committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  object_class->set_property = vinagre_prefs_set_property;
  object_class->get_property = vinagre_prefs_get_property;

  g_object_class_install_property (object_class,
				   PROP_SHARED_FLAG,
				   g_param_spec_boolean ("shared-flag",
							 "Shared Flag",
							 "Whether we should share the remote connection",
							 TRUE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_ALWAYS_SHOW_TABS,
				   g_param_spec_boolean ("always-show-tabs",
							 "Always show tabs",
							 "Whether we should show the tabs even when there is ony one active connection",
353
							 FALSE,
Jonh Wendell's avatar
Jonh Wendell committed
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TOOLBAR_VISIBLE,
				   g_param_spec_boolean ("toolbar-visible",
							 "Toolbar Visibility",
							 "Whether the toolbar is visible",
							 TRUE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_STATUSBAR_VISIBLE,
				   g_param_spec_boolean ("statusbar-visible",
							 "Statusbar Visibility",
							 "Whether the statusbar is visible",
							 TRUE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_SIDE_PANEL_VISIBLE,
				   g_param_spec_boolean ("side-panel-visible",
							 "Side Panel Visibility",
							 "Whether the side panel is visible",
							 TRUE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_WINDOW_STATE,
				   g_param_spec_int ("window-state",
						     "Window State",
						     "Whether the window is maximised",
						     G_MININT, G_MAXINT, 0,
						     G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_WINDOW_WIDTH,
				   g_param_spec_int ("window-width",
						     "Window Width",
						     "The width of window",
						     100, G_MAXINT, 650,
						     G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_WINDOW_HEIGHT,
				   g_param_spec_int ("window-height",
						     "Window Height",
						     "The height of window",
						     100, G_MAXINT, 500,
						     G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_SIDE_PANEL_SIZE,
				   g_param_spec_int ("side-panel-size",
						     "Side Panel Width",
						     "The width of side panel",
						     100, G_MAXINT, 200,
						     G_PARAM_READWRITE));
404
405
406
407
408
  g_object_class_install_property (object_class,
				   PROP_SHOW_ACCELS,
				   g_param_spec_boolean ("show-accels",
							 "Show menu accelerators",
							 "Whether we should show the menu accelerators (keyboard shortcuts)",
409
							 FALSE,
410
							 G_PARAM_READWRITE));
411
412
413
414
415
416
417
  g_object_class_install_property (object_class,
				   PROP_HISTORY_SIZE,
				   g_param_spec_int ("history-size",
						     "History size",
						     "Max number of items in history dropdown entry",
						     0, G_MAXINT, 15,
						     G_PARAM_READWRITE));
Jonh Wendell's avatar
Jonh Wendell committed
418

Jonh Wendell's avatar
Jonh Wendell committed
419
420
421
422
423
424
425
  g_object_class_install_property (object_class,
				   PROP_ACTIVE_PLUGINS,
				   g_param_spec_pointer ("active-plugins",
							 "Active plugins",
							 "The list of active plugins",
							 G_PARAM_READWRITE));

Jonh Wendell's avatar
Jonh Wendell committed
426
}
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508

/* Preferences dialog */

typedef struct {
  GladeXML  *xml;
  GtkWidget *dialog;
  GtkWidget *show_tabs;
  GtkWidget *show_accels;
} VinagrePrefsDialog;

static void
vinagre_prefs_dialog_setup (VinagrePrefsDialog *dialog)
{
  gboolean show_accels, show_tabs;

  g_object_get (vinagre_prefs_get_default (),
		"show-accels", &show_accels,
		"always-show-tabs", &show_tabs,
		NULL);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->show_accels), show_accels);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->show_tabs), show_tabs);
}

static void
vinagre_prefs_dialog_quit (VinagrePrefsDialog *dialog)
{
  gtk_widget_destroy (dialog->dialog);
  g_object_unref (dialog->xml);
  g_free (dialog);
  dialog = NULL;
}

static void
vinagre_prefs_dialog_show_tabs_cb (VinagrePrefsDialog *dialog)
{
  g_object_set (vinagre_prefs_get_default (),
		"always-show-tabs", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->show_tabs)),
		NULL);
}

static void
vinagre_prefs_dialog_show_accels_cb (VinagrePrefsDialog *dialog)
{
  g_object_set (vinagre_prefs_get_default (),
		"show-accels", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->show_accels)),
		NULL);
}

void
vinagre_prefs_dialog_show (VinagreWindow *window)
{
  VinagrePrefsDialog *dialog;

  dialog = g_new (VinagrePrefsDialog, 1);

  dialog->xml = glade_xml_new (vinagre_utils_get_glade_filename (), NULL, NULL);
  dialog->dialog = glade_xml_get_widget (dialog->xml, "preferences_dialog");
  gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), GTK_WINDOW (window));

  dialog->show_tabs = glade_xml_get_widget (dialog->xml, "always_show_tabs_check");
  dialog->show_accels = glade_xml_get_widget (dialog->xml, "show_accels_check");

  vinagre_prefs_dialog_setup (dialog);

  g_signal_connect_swapped (dialog->dialog,
			    "response", 
                            G_CALLBACK (vinagre_prefs_dialog_quit),
                            dialog);

  g_signal_connect_swapped (dialog->show_tabs,
			    "toggled",
			     G_CALLBACK (vinagre_prefs_dialog_show_tabs_cb),
			     dialog);

  g_signal_connect_swapped (dialog->show_accels,
			    "toggled",
			     G_CALLBACK (vinagre_prefs_dialog_show_accels_cb),
			     dialog);

  gtk_widget_show_all (dialog->dialog);
}
509
/* vim: set ts=8: */