inspector: Autosave css

Save and restore the contents of the css editor in a file.
This commit is contained in:
Matthias Clasen 2019-04-06 18:00:09 +00:00
parent c92e53622f
commit 4b40cc27a9

View File

@ -104,10 +104,23 @@ query_tooltip_cb (GtkWidget *widget,
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorCssEditor, gtk_inspector_css_editor, GTK_TYPE_BOX)
static char *
get_autosave_path (void)
{
return g_build_filename (g_get_user_cache_dir (), "gtk-4.0", "inspector-css-autosave", NULL);
}
static void
set_initial_text (GtkInspectorCssEditor *ce)
{
gchar *initial_text;
char *initial_text = NULL;
char *autosave_file = NULL;
gsize len;
autosave_file = get_autosave_path ();
g_file_get_contents (autosave_file, &initial_text, &len, NULL);
if (!initial_text)
initial_text = g_strconcat ("/*\n",
_("You can type here any CSS rule recognized by GTK."), "\n",
_("You can temporarily disable this custom CSS by clicking on the “Pause” button above."), "\n\n",
@ -115,6 +128,27 @@ set_initial_text (GtkInspectorCssEditor *ce)
"*/\n\n", NULL);
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (ce->priv->text), initial_text, -1);
g_free (initial_text);
g_free (autosave_file);
}
static void
autosave_contents (GtkInspectorCssEditor *ce)
{
char *autosave_file = NULL;
char *dir = NULL;
char *contents;
GtkTextIter start, end;
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (ce->priv->text), &start, &end);
contents = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (ce->priv->text), &start, &end, TRUE);
autosave_file = get_autosave_path ();
dir = g_path_get_dirname (autosave_file);
g_mkdir_with_parents (dir, 600);
g_file_set_contents (autosave_file, contents, -1, NULL);
g_free (dir);
g_free (autosave_file);
g_free (contents);
}
static void
@ -229,6 +263,7 @@ update_timeout (gpointer data)
ce->priv->timeout = 0;
autosave_contents (ce);
update_style (ce);
return G_SOURCE_REMOVE;