Warning: |
GNOME Goal: Saving state in a file, not GConf
A lot of applications use GConf to save their state, like window sizes, window states, and paths of the last files opened. This should not be saved in GConf as per GConf's homepage:
Do not store anything but preferences in GConf. Documents, session state, random data blobs do not belong in GConf. Stuff breaks if you do this.
You should instead save this data on-disk, in a file. The file should be under ~/.gnome2/, and contain the name of your application. I recommend using GKeyFile to load and save this data.
- Window state
- Window size
- Whether a sidebar is shown or hidden (not for a toolbar, or other item of UI that can be thought of as a preference)
An example of how to save state using a GKeyfile is available in this Totem bug.
Implementation notes
Read Havoc describing the sort of high-level functions we need:
gboolean gnome_save_window_state (GtkWindow *window, const char *key, GError **error); gboolean gnome_restore_window_state (GtkWindow *window, const char *key, GError **error);
So you would do
- .. when starting up ... main_window = gtk_window_new (...);
success = gnome_restore_window_state (main_window, "main-window", &error);
- .. when exiting, or upon a window state change ...
success = gnome_save_window_state (main_window, "main-window", &error);
When do we save that information? Do we want a gnome_prepare_window_so_that_state_is_automatically_saved (window, "key")? Or do we want the app to call a save_window_state() function explicitly? Will they forget to do it at the right time?
- This needs to save information on the screen size.
- If you try to load a window's state but there's only info available for *another* screen size (i.e. you just upgraded your monitor), we could do a best-effort thing of using the old geometry/state if the window fits, or adjusting it if it doesn't fit.
- Saving windows as iconified is probably a bad idea. You get a "where did my window go?" on restart.
- Need a way to purge out old information. When does a window's information become stale and thus it needs to be removed from GConf's storage? Do apps need to remember to do this manually, or is there an automatic way to know when to do this?
Why stop at just window information? I've put together some routines that make it possible to save
Window size & maximized state
Positions of GtkPaneds
Currently selected GtkListStores (had problems getting TreeStores to work)
Currently selected page in a GtkNotebook
Position of a GtkScale (only will work for GLib 2.11+, so I haven't been able to test it)
- Any string/int/uint/boolean read/write property
The only requirements are that g_set_application_name () has been called, and any widget that needs its state saved has the "name" property set.
Saving requires you to explicitly specify which widgets and which states you want saved. Loading only requires you give it the parent widget (such as the window) and then all of its children's states are implicitly loaded.
Here's the files: gpersist.h, gpersist.c, test.c, testui.glade
Compile with
- gcc $(pkg-config --libs --cflags gtk+-2.0 libglade-2.0) test.c gpersist.c