shell-global: Add new APIs for saving/loading persistent state

https://bugzilla.gnome.org/show_bug.cgi?id=710137
This commit is contained in:
Jasper St. Pierre 2013-10-13 18:46:50 -04:00
parent c9b73ac731
commit 1ac4ab7edc
2 changed files with 94 additions and 44 deletions

View File

@ -80,10 +80,11 @@ struct _ShellGlobal {
const char *datadir;
const char *imagedir;
const char *userdatadir;
StFocusManager *focus_manager;
GFile *userdatadir_path;
GFile *runtime_state_path;
StFocusManager *focus_manager;
guint work_count;
GSList *leisure_closures;
guint leisure_function_id;
@ -252,6 +253,7 @@ shell_global_init (ShellGlobal *global)
/* Ensure config dir exists for later use */
global->userdatadir = g_build_filename (g_get_user_data_dir (), "gnome-shell", NULL);
g_mkdir_with_parents (global->userdatadir, 0700);
global->userdatadir_path = g_file_new_for_path (global->userdatadir);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
byteorder_string = "LE";
@ -306,6 +308,7 @@ shell_global_finalize (GObject *object)
the_object = NULL;
g_clear_object (&global->userdatadir_path);
g_clear_object (&global->runtime_state_path);
G_OBJECT_CLASS(shell_global_parent_class)->finalize (object);
@ -1746,29 +1749,12 @@ shell_global_get_session_mode (ShellGlobal *global)
return global->session_mode;
}
static GFile *
get_runtime_state_path (ShellGlobal *global,
const char *property_name)
static void
save_variant (GFile *dir,
const char *property_name,
GVariant *variant)
{
return g_file_get_child (global->runtime_state_path, property_name);
}
/**
* shell_global_set_runtime_state:
* @global: a #ShellGlobal
* @property_name: Name of the property
* @variant: (allow-none): A #GVariant, or %NULL to unset
*
* Change the value of serialized runtime state.
*/
void
shell_global_set_runtime_state (ShellGlobal *global,
const char *property_name,
GVariant *variant)
{
GFile *path;
path = get_runtime_state_path (global, property_name);
GFile *path = g_file_get_child (dir, property_name);
if (variant == NULL || g_variant_get_data (variant) == NULL)
(void) g_file_delete (path, NULL, NULL);
@ -1783,29 +1769,17 @@ shell_global_set_runtime_state (ShellGlobal *global,
g_object_unref (path);
}
/**
* shell_global_get_runtime_state:
* @global: a #ShellGlobal
* @property_type: Expected data type
* @property_name: Name of the property
*
* The shell maintains "runtime" state which does not persist across
* logout or reboot.
*
* Returns: (transfer floating): The value of a serialized property, or %NULL if none stored
*/
GVariant *
shell_global_get_runtime_state (ShellGlobal *global,
const char *property_type,
const char *property_name)
static GVariant *
load_variant (GFile *dir,
const char *property_type,
const char *property_name)
{
GVariant *res = NULL;
GMappedFile *mfile;
GFile *path;
GFile *path = g_file_get_child (dir, property_name);
char *pathstr;
GError *local_error = NULL;
path = get_runtime_state_path (global, property_name);
pathstr = g_file_get_path (path);
mfile = g_mapped_file_new (pathstr, FALSE, &local_error);
if (!mfile)
@ -1829,3 +1803,73 @@ shell_global_get_runtime_state (ShellGlobal *global,
return res;
}
/**
* shell_global_set_runtime_state:
* @global: a #ShellGlobal
* @property_name: Name of the property
* @variant: (allow-none): A #GVariant, or %NULL to unset
*
* Change the value of serialized runtime state.
*/
void
shell_global_set_runtime_state (ShellGlobal *global,
const char *property_name,
GVariant *variant)
{
save_variant (global->runtime_state_path, property_name, variant);
}
/**
* shell_global_get_runtime_state:
* @global: a #ShellGlobal
* @property_type: Expected data type
* @property_name: Name of the property
*
* The shell maintains "runtime" state which does not persist across
* logout or reboot.
*
* Returns: (transfer floating): The value of a serialized property, or %NULL if none stored
*/
GVariant *
shell_global_get_runtime_state (ShellGlobal *global,
const char *property_type,
const char *property_name)
{
return load_variant (global->runtime_state_path, property_type, property_name);
}
/**
* shell_global_set_persistent_state:
* @global: a #ShellGlobal
* @property_name: Name of the property
* @variant: (allow-none): A #GVariant, or %NULL to unset
*
* Change the value of serialized persistent state.
*/
void
shell_global_set_persistent_state (ShellGlobal *global,
const char *property_name,
GVariant *variant)
{
save_variant (global->userdatadir_path, property_name, variant);
}
/**
* shell_global_get_persistent_state:
* @global: a #ShellGlobal
* @property_type: Expected data type
* @property_name: Name of the property
*
* The shell maintains "persistent" state which will persist after
* logout or reboot.
*
* Returns: (transfer none): The value of a serialized property, or %NULL if none stored
*/
GVariant *
shell_global_get_persistent_state (ShellGlobal *global,
const char *property_type,
const char *property_name)
{
return load_variant (global->userdatadir_path, property_type, property_name);
}

View File

@ -129,13 +129,19 @@ void shell_global_reexec_self (ShellGlobal *global);
const char * shell_global_get_session_mode (ShellGlobal *global);
void shell_global_set_runtime_state (ShellGlobal *global,
const char *property_name,
GVariant *variant);
void shell_global_set_runtime_state (ShellGlobal *global,
const char *property_name,
GVariant *variant);
GVariant * shell_global_get_runtime_state (ShellGlobal *global,
const char *property_type,
const char *property_name);
void shell_global_set_persistent_state (ShellGlobal *global,
const char *property_name,
GVariant *variant);
GVariant * shell_global_get_persistent_state (ShellGlobal *global,
const char *property_type,
const char *property_name);
G_END_DECLS