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
|
#!/usr/bin/env python
#
# This program demonstrates how to use GConf. The key thing is that
# the main window and the prefs dialog have NO KNOWLEDGE of one
# another as far as configuration values are concerned; they don't
# even have to be in the same process. That is, the GConfClient acts
# as the data "model" for configuration information; the main
# application is a "view" of the model; and the prefs dialog is a
# "controller."
#
# You can tell if your application has done this correctly by
# using "gconftool" instead of your preferences dialog to set
# preferences. For example:
#
# gconftool --type=string --set /apps/basic-gconf-app/foo "My string"
#
# If that doesn't work every bit as well as setting the value
# via the prefs dialog, then you aren't doing things right. ;-)
#
#
# If you really want to be mean to your app, make it survive
# this:
#
# gconftool --break-key /apps/basic-gconf-app/foo
#
# Remember, the GConf database is just like an external file or
# the network - it may have bogus values in it. GConf admin
# tools will let people put in whatever they can think of.
#
# GConf does guarantee that string values will be valid UTF-8, for
# convenience.
#
# Throughout, this program is letting GConfClient use its default
# error handlers rather than checking for errors or attaching custom
# handlers to the "unreturned_error" signal. Thus the last arg to
# GConfClient functions is None.
#
# Special mention of an idiom often used in GTK+ apps that does
# not work right with GConf but may appear to at first:
#
# i_am_changing_value = gtk.TRUE
# change_value (value)
# i_am_changing_value = gtk.FALSE
#
# This breaks for several reasons: notification of changes
# may be asynchronous, you may get notifications that are not
# caused by change_value () while change_value () is running,
# since GConf will enter the main loop, and also if you need
# this code to work you are probably going to have issues
# when someone other than yourself sets the value.
#
# A robust solution in this case is often to compare the old
# and new values to see if they've really changed, thus avoiding
# whatever loop you were trying to avoid.
#
import gconf
import gtk
def main ():
# Get the default client
client = gconf.client_get_default ();
# Tell GConfClient that we're interested in the given directory.
# This means GConfClient will receive notification of changes
# to this directory, and cache keys under this directory.
# So _don't_ add "/" or something silly like that or you'll end
# up with a copy of the whole GConf database. ;-)
#
# We use PRELOAD_NONE to avoid loading all config keys on
# startup. If your app pretty much reads all config keys
# on startup, then preloading the cache may make sense.
client.add_dir ("/apps/basic-gconf-app",
gconf.CLIENT_PRELOAD_NONE)
main_window = create_main_window (client)
main_window.show_all ()
gtk.main ()
# This ensures we cleanly detach from the GConf server (assuming
# we hold the last reference). It's purely a bit of cleanliness,
# the server does survive fine if we crash.
#client.unref ()
# Quit app when window is destroyed
def destroy_callback (widget, *data):
gtk.mainquit ()
# Remove the notification callback when the widget monitoring
# notifications is destroyed
def configurable_widget_destroy_callback (widget):
client = widget.get_data ('client')
notify_id = widget.get_data ('notify_id')
if notify_id:
client.notify_remove (notify_id)
# Notification callback for our label widgets that
# monitor the current value of a gconf key. i.e.
# we are conceptually "configuring" the label widgets
def configurable_widget_config_notify (client, cnxn_id, entry, label):
# Note that value can be None (unset) or it can have
# the wrong type! Need to check that to survive
# gconftool --break-key
if not entry.value:
label.set_text ('')
elif entry.value.type == gconf.VALUE_STRING:
label.set_text (entry.value.to_string ())
else:
label.set_text ('!type error!')
# Create a GtkLabel inside a frame, that we can "configure"
# (the label displays the value of the config key).
def create_configurable_widget (client, config_key):
frame = gtk.Frame (config_key)
label = gtk.Label ('')
frame.add (label)
s = client.get_string (config_key)
if s:
label.set_text (s)
notify_id = client.notify_add (config_key,
configurable_widget_config_notify,
label)
# Note that notify_id will be 0 if there was an error,
# so we handle that in our destroy callback.
label.set_data ('notify_id', notify_id)
label.set_data ('client', client)
label.connect ('destroy', configurable_widget_destroy_callback)
return frame
def prefs_dialog_destroyed (dialog, main_window):
main_window.set_data ('prefs', None)
# prefs button clicked
def prefs_clicked (button, main_window):
prefs_dialog = main_window.get_data ('prefs')
if not prefs_dialog:
client = main_window.get_data ('client')
prefs_dialog = create_prefs_dialog (main_window, client)
main_window.set_data ('prefs', prefs_dialog)
prefs_dialog.connect ('destroy', prefs_dialog_destroyed, main_window)
prefs_dialog.show_all ()
else:
# show existing dialog
prefs_dialog.present ()
def create_main_window (client):
w = gtk.Window ()
w.set_title ('basic-gconf-app Main Window')
vbox = gtk.VBox (gtk.FALSE, 5)
vbox.set_border_width (5)
w.add (vbox)
# Create labels that we can "configure"
config = create_configurable_widget (client, "/apps/basic-gconf-app/foo")
vbox.pack_start (config, gtk.TRUE, gtk.TRUE)
config = create_configurable_widget (client, "/apps/basic-gconf-app/bar")
vbox.pack_start (config, gtk.TRUE, gtk.TRUE)
config = create_configurable_widget (client, "/apps/basic-gconf-app/baz")
vbox.pack_start (config, gtk.TRUE, gtk.TRUE)
config = create_configurable_widget (client, "/apps/basic-gconf-app/blah");
vbox.pack_start (config, gtk.TRUE, gtk.TRUE)
w.connect ('destroy', destroy_callback)
w.set_data ('client', client)
prefs = gtk.Button ("Prefs");
vbox.pack_end ( prefs, gtk.FALSE, gtk.FALSE)
prefs.connect ('clicked', prefs_clicked, w)
return w
#
# Preferences dialog code. NOTE that the prefs dialog knows NOTHING
# about the existence of the main window; it is purely a way to fool
# with the GConf database. It never does something like change
# the main window directly; it ONLY changes GConf keys via
# GConfClient. This is _important_, because people may configure
# your app without using your preferences dialog.
#
# This is an instant-apply prefs dialog. For a complicated
# apply/revert/cancel dialog as in GNOME 1, see the
# complex-gconf-app.c example. But don't actually copy that example
# in GNOME 2, thanks. ;-) complex-gconf-app.c does show how
# to use GConfChangeSet.
#
# Commit changes to the GConf database.
def config_entry_commit (entry, *args):
client = entry.get_data ('client')
text = entry.get_chars (0, -1)
key = entry.get_data ('key')
# Unset if the string is zero-length, otherwise set
if text:
client.set_string (key, text)
else:
client.unset (key)
# Create an entry used to edit the given config key
def create_config_entry (prefs_dialog, client, config_key, focus=gtk.FALSE):
hbox = gtk.HBox (gtk.FALSE, 5)
label = gtk.Label (config_key)
entry = gtk.Entry ()
hbox.pack_start (label, gtk.FALSE, gtk.FALSE, 0)
hbox.pack_end (entry, gtk.FALSE, gtk.FALSE, 0)
# this will print an error via default error handler
# if the key isn't set to a string
s = client.get_string (config_key)
if s:
entry.set_text (s)
entry.set_data ('client', client)
entry.set_data ('key', config_key)
# Commit changes if the user focuses out, or hits enter; we don't
# do this on "changed" since it'd probably be a bit too slow to
# round-trip to the server on every "changed" signal.
entry.connect ('focus_out_event', config_entry_commit)
entry.connect ('activate', config_entry_commit)
# Set the entry insensitive if the key it edits isn't writable.
# Technically, we should update this sensitivity if the key gets
# a change notify, but that's probably overkill.
entry.set_sensitive (client.key_is_writable (config_key))
if focus:
entry.grab_focus ()
return hbox
def create_prefs_dialog (parent, client):
dialog = gtk.Dialog ("basic-gconf-app Preferences",
parent,
0,
(gtk.STOCK_CLOSE, gtk.RESPONSE_ACCEPT))
# destroy dialog on button press
dialog.connect ('response', lambda wid,ev: wid.destroy ())
dialog.set_default_response (gtk.RESPONSE_ACCEPT)
# resizing doesn't grow the entries anyhow
dialog.set_resizable (gtk.FALSE)
vbox = gtk.VBox (gtk.FALSE, 5)
vbox.set_border_width (5)
dialog.vbox.pack_start (vbox)
entry = create_config_entry (dialog, client, "/apps/basic-gconf-app/foo", gtk.TRUE)
vbox.pack_start (entry, gtk.FALSE, gtk.FALSE)
entry = create_config_entry (dialog, client, "/apps/basic-gconf-app/bar")
vbox.pack_start (entry, gtk.FALSE, gtk.FALSE)
entry = create_config_entry (dialog, client, "/apps/basic-gconf-app/baz")
vbox.pack_start (entry, gtk.FALSE, gtk.FALSE)
entry = create_config_entry (dialog, client, "/apps/basic-gconf-app/blah")
vbox.pack_start (entry, gtk.FALSE, gtk.FALSE)
return dialog
main ()
|