~cjcurran/indicator-session/null-error-user-dbus-mgr

« back to all changes in this revision

Viewing changes to src/settings-helper.c

  • Committer: Conor Curran
  • Date: 2011-08-22 12:30:24 UTC
  • mfrom: (178.1.20 gsettings-port)
  • Revision ID: conor.curran@canonical.com-20110822123024-bu0atlpuzbjwvrax
gsettingsĀ port

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
A small wrapper utility for connecting to gconf.
 
2
A small wrapper utility for connecting to GSettings.
3
3
 
4
4
Copyright 2009 Canonical Ltd.
5
5
 
19
19
with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
*/
21
21
 
22
 
 
23
 
#include <gconf/gconf-client.h>
24
 
 
 
22
#include <gio/gio.h>
25
23
#include <glib/gi18n.h>
26
24
 
27
25
#include <dbus/dbus-glib.h>
31
29
#include <libdbusmenu-glib/menuitem.h>
32
30
 
33
31
#include "dbus-shared-names.h"
34
 
#include "gconf-helper.h"
35
 
 
36
 
static GConfClient * gconf_client = NULL;
 
32
#include "settings-helper.h"
 
33
 
 
34
static GSettings* settings = NULL;
 
35
 
37
36
static guint confirmation_notify = 0;
38
37
static guint logout_notify = 0;
39
38
static guint restart_notify = 0;
40
39
static guint shutdown_notify = 0;
41
40
 
 
41
static void
 
42
build_settings (void) {
 
43
        if(!settings) {
 
44
                settings = g_settings_new (SESSION_SCHEMA);
 
45
        }
 
46
        return;
 
47
}
 
48
 
42
49
gboolean
43
50
supress_confirmations (void) {
44
 
        if(!gconf_client) {
45
 
                gconf_client = gconf_client_get_default ();
46
 
        }
47
 
        return gconf_client_get_bool (gconf_client, SUPPRESS_KEY, NULL) ;
 
51
        build_settings();
 
52
        return g_settings_get_boolean (settings, SUPPRESS_KEY) ;
48
53
}
49
54
 
50
55
gboolean
51
56
show_logout (void) {
52
 
        if(!gconf_client) {
53
 
                gconf_client = gconf_client_get_default ();
54
 
        }
55
 
        return !gconf_client_get_bool (gconf_client, LOGOUT_KEY, NULL) ;
 
57
        build_settings();
 
58
        return !g_settings_get_boolean (settings, LOGOUT_KEY) ;
56
59
}
57
60
 
58
61
gboolean
59
62
show_restart (void) {
60
 
        if(!gconf_client) {
61
 
                gconf_client = gconf_client_get_default ();
62
 
        }
63
 
        return !gconf_client_get_bool (gconf_client, RESTART_KEY, NULL) ;
 
63
        build_settings();
 
64
        return !g_settings_get_boolean (settings, RESTART_KEY) ;
64
65
}
65
66
 
66
67
gboolean
67
68
show_shutdown (void) {
68
 
        if(!gconf_client) {
69
 
                gconf_client = gconf_client_get_default ();
70
 
        }
71
 
        return !gconf_client_get_bool (gconf_client, SHUTDOWN_KEY, NULL) ;
 
69
        build_settings();
 
70
        return !g_settings_get_boolean (settings, SHUTDOWN_KEY) ;
72
71
}
73
72
 
74
 
static void update_menu_entries_callback (GConfClient *client, guint cnxn_id, GConfEntry  *entry, gpointer data) {
 
73
static void update_menu_entries_callback (GSettings * settings, const gchar * key, gpointer data) {
75
74
        RestartShutdownLogoutMenuItems * restart_shutdown_logout_mi = (RestartShutdownLogoutMenuItems*) data;
76
 
        GConfValue * value = gconf_entry_get_value (entry);
77
 
        const gchar * key = gconf_entry_get_key (entry);
78
75
 
79
76
        if(g_strcmp0 (key, SUPPRESS_KEY) == 0) {
80
 
                if (gconf_value_get_bool (value)) {
 
77
                if (g_settings_get_boolean (settings, key)) {
81
78
                        dbusmenu_menuitem_property_set(restart_shutdown_logout_mi->logout_mi, DBUSMENU_MENUITEM_PROP_LABEL, _("Log Out"));
82
79
                        dbusmenu_menuitem_property_set(restart_shutdown_logout_mi->restart_mi, RESTART_ITEM_LABEL, _("Restart"));
83
80
                        dbusmenu_menuitem_property_set(restart_shutdown_logout_mi->shutdown_mi, DBUSMENU_MENUITEM_PROP_LABEL, _("Shut Down"));
90
87
}
91
88
 
92
89
static void
93
 
update_logout_callback (GConfClient *client, guint cnxn_id, GConfEntry  *entry, gpointer data) {
 
90
update_logout_callback (GSettings * settings, const gchar * key, gpointer data) {
94
91
        DbusmenuMenuitem * mi = (DbusmenuMenuitem*) data;
95
 
        GConfValue * value = gconf_entry_get_value (entry);
96
 
        const gchar * key = gconf_entry_get_key (entry);
97
92
 
98
93
        if(g_strcmp0 (key, LOGOUT_KEY) == 0) {
99
 
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !gconf_value_get_bool(value));
 
94
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !g_settings_get_boolean(settings, key));
100
95
        }
101
96
}
102
97
 
103
98
static void
104
 
update_restart_callback (GConfClient *client, guint cnxn_id, GConfEntry  *entry, gpointer data) {
 
99
update_restart_callback (GSettings * settings, const gchar * key, gpointer data) {
105
100
        DbusmenuMenuitem * mi = (DbusmenuMenuitem*) data;
106
 
        GConfValue * value = gconf_entry_get_value (entry);
107
 
        const gchar * key = gconf_entry_get_key (entry);
108
101
 
109
102
        if(g_strcmp0 (key, RESTART_KEY) == 0) {
110
 
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !gconf_value_get_bool(value));
 
103
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !g_settings_get_boolean(settings, key));
111
104
        }
112
105
}
113
106
 
114
107
static void
115
 
update_shutdown_callback (GConfClient *client, guint cnxn_id, GConfEntry  *entry, gpointer data) {
 
108
update_shutdown_callback (GSettings * settings, const gchar * key, gpointer data) {
116
109
        DbusmenuMenuitem * mi = (DbusmenuMenuitem*) data;
117
 
        GConfValue * value = gconf_entry_get_value (entry);
118
 
        const gchar * key = gconf_entry_get_key (entry);
119
110
 
120
111
        if(g_strcmp0 (key, SHUTDOWN_KEY) == 0) {
121
 
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !gconf_value_get_bool(value));
 
112
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_VISIBLE, !g_settings_get_boolean(settings, key));
122
113
        }
123
114
}
124
115
 
125
116
void
126
117
update_menu_entries(RestartShutdownLogoutMenuItems * restart_shutdown_logout_mi) {
127
118
        /* If we don't have a client, build one. */
128
 
        if(!gconf_client) {
129
 
                gconf_client = gconf_client_get_default ();
130
 
        }
131
 
 
132
 
        /* If we've not gotten any notifications, then we need
133
 
           to add the directory for notifications to come from. */
134
 
        if (confirmation_notify == 0 || logout_notify == 0) {
135
 
                gconf_client_add_dir (gconf_client, GLOBAL_DIR,
136
 
                                      GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
137
 
        }
 
119
        build_settings();
138
120
 
139
121
        if (confirmation_notify != 0) {
140
 
                gconf_client_notify_remove (gconf_client, confirmation_notify);
 
122
                g_signal_handler_disconnect (settings, confirmation_notify);
141
123
                confirmation_notify = 0;
142
124
        }
143
125
 
144
126
        if (logout_notify != 0) {
145
 
                gconf_client_notify_remove (gconf_client, logout_notify);
 
127
                g_signal_handler_disconnect (settings, logout_notify);
146
128
                logout_notify = 0;
147
129
        }
148
130
 
149
131
        if (restart_notify != 0) {
150
 
                gconf_client_notify_remove (gconf_client, restart_notify);
 
132
                g_signal_handler_disconnect (settings, restart_notify);
151
133
                restart_notify = 0;
152
134
        }
153
135
 
154
136
        if (shutdown_notify != 0) {
155
 
                gconf_client_notify_remove (gconf_client, shutdown_notify);
 
137
                g_signal_handler_disconnect (settings, shutdown_notify);
156
138
                shutdown_notify = 0;
157
139
        }
158
140
 
159
 
        confirmation_notify = gconf_client_notify_add (gconf_client, SUPPRESS_KEY,
160
 
                                update_menu_entries_callback, restart_shutdown_logout_mi, NULL, NULL);
161
 
        logout_notify = gconf_client_notify_add (gconf_client, LOGOUT_KEY,
162
 
                                update_logout_callback, restart_shutdown_logout_mi->logout_mi, NULL, NULL);
163
 
        restart_notify = gconf_client_notify_add (gconf_client, RESTART_KEY,
164
 
                                update_restart_callback, restart_shutdown_logout_mi->restart_mi, NULL, NULL);
165
 
        shutdown_notify = gconf_client_notify_add (gconf_client, SHUTDOWN_KEY,
166
 
                                update_shutdown_callback, restart_shutdown_logout_mi->shutdown_mi, NULL, NULL);
 
141
        confirmation_notify = g_signal_connect (settings, "changed::" SUPPRESS_KEY,
 
142
                                G_CALLBACK(update_menu_entries_callback), restart_shutdown_logout_mi);
 
143
        logout_notify = g_signal_connect (settings, "changed::" LOGOUT_KEY,
 
144
                                G_CALLBACK(update_logout_callback), restart_shutdown_logout_mi->logout_mi);
 
145
        restart_notify = g_signal_connect (settings, "changed::" RESTART_KEY,
 
146
                                G_CALLBACK(update_restart_callback), restart_shutdown_logout_mi->restart_mi);
 
147
        shutdown_notify = g_signal_connect (settings, "changed::" SHUTDOWN_KEY,
 
148
                                G_CALLBACK(update_shutdown_callback), restart_shutdown_logout_mi->shutdown_mi);
167
149
 
168
150
        return;
169
151
}