[easytag/wip/application-window: 67/79] Save and restore main window state



commit b57cec101a43df9ffc4683219213ac59a2e1a30a
Author: David King <amigadave amigadave com>
Date:   Wed Jul 16 08:19:20 2014 +0100

    Save and restore main window state

 src/application_window.c |  116 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 110 insertions(+), 6 deletions(-)
---
diff --git a/src/application_window.c b/src/application_window.c
index 2acb163..537456d 100644
--- a/src/application_window.c
+++ b/src/application_window.c
@@ -117,6 +117,98 @@ on_main_window_delete_event (GtkWidget *window,
     return TRUE;
 }
 
+static void
+save_state (EtApplicationWindow *self)
+{
+    EtApplicationWindowPrivate *priv;
+    gchar *path;
+    GKeyFile *keyfile;
+    gchar *buffer;
+    gsize length;
+    GError *error = NULL;
+
+    priv = et_application_window_get_instance_private (self);
+    keyfile = g_key_file_new ();
+    path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME,
+                             "state", NULL);
+
+    /* Try to preserve comments by loading an existing keyfile. */
+    if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_KEEP_COMMENTS,
+                                    &error))
+    {
+        g_debug ("Error loading window state during saving: %s",
+                 error->message);
+        g_clear_error (&error);
+    }
+
+    g_key_file_set_integer (keyfile, "EtApplicationWindow", "width",
+                            priv->width);
+    g_key_file_set_integer (keyfile, "EtApplicationWindow", "height",
+                            priv->height);
+    g_key_file_set_boolean (keyfile, "EtApplicationWindow", "is_maximized",
+                            priv->is_maximized);
+
+    /* TODO; Use g_key_file_save_to_file() in GLib 2.40. */
+    buffer = g_key_file_to_data (keyfile, &length, NULL);
+
+    if (!g_file_set_contents (path, buffer, length, &error))
+    {
+        g_warning ("Error saving window state: %s", error->message);
+        g_error_free (error);
+    }
+
+    g_free (buffer);
+    g_free (path);
+    g_key_file_free (keyfile);
+}
+
+static void
+restore_state (EtApplicationWindow *self)
+{
+    EtApplicationWindowPrivate *priv;
+    GtkWindow *window;
+    GKeyFile *keyfile;
+    gchar *path;
+    GError *error = NULL;
+
+    priv = et_application_window_get_instance_private (self);
+    window = GTK_WINDOW (self);
+
+    keyfile = g_key_file_new ();
+    path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME,
+                             "state", NULL);
+
+    if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_KEEP_COMMENTS,
+                                    &error))
+    {
+        g_debug ("Error loading window state: %s", error->message);
+        g_error_free (error);
+        g_key_file_free (keyfile);
+        g_free (path);
+        return;
+    }
+
+    g_free (path);
+
+    /* Ignore errors, as the default values are fine. */
+    priv->width = g_key_file_get_integer (keyfile, "EtApplicationWindow",
+                                          "width", NULL);
+    priv->height = g_key_file_get_integer (keyfile, "EtApplicationWindow",
+                                           "height", NULL);
+    priv->is_maximized = g_key_file_get_boolean (keyfile,
+                                                 "EtApplicationWindow",
+                                                 "is_maximized", NULL);
+
+    gtk_window_set_default_size (window, priv->width, priv->height);
+
+    if (priv->is_maximized)
+    {
+        gtk_window_maximize (window);
+    }
+
+    g_key_file_free (keyfile);
+}
+
 static gboolean
 on_configure_event (GtkWidget *window,
                     GdkEvent *event,
@@ -2972,11 +3064,25 @@ static const GActionEntry actions[] =
 };
 
 static void
+et_application_window_destroy (GtkWidget *widget)
+{
+    EtApplicationWindow *self;
+
+    self = ET_APPLICATION_WINDOW (widget);
+
+    save_state (self);
+
+    GTK_WIDGET_CLASS (et_application_window_parent_class)->destroy (widget);
+}
+
+static void
 et_application_window_finalize (GObject *object)
 {
+    EtApplicationWindow *self;
     EtApplicationWindowPrivate *priv;
 
-    priv = et_application_window_get_instance_private (ET_APPLICATION_WINDOW (object));
+    self = ET_APPLICATION_WINDOW (object);
+    priv = et_application_window_get_instance_private (self);
 
     g_clear_object (&priv->cddb_dialog);
     g_clear_object (&priv->load_files_dialog);
@@ -3023,14 +3129,9 @@ et_application_window_init (EtApplicationWindow *self)
 
     window = GTK_WINDOW (self);
 
-    gtk_window_set_default_size (window, 1024, 768);
     gtk_window_set_icon_name (window, PACKAGE_TARNAME);
     gtk_window_set_title (window, PACKAGE_NAME);
 
-    priv->is_maximized = FALSE;
-    priv->height = 0;
-    priv->width = 0;
-
     g_signal_connect (self, "configure-event",
                       G_CALLBACK (on_configure_event), NULL);
     g_signal_connect (self, "delete-event",
@@ -3038,6 +3139,8 @@ et_application_window_init (EtApplicationWindow *self)
     g_signal_connect (self, "window-state-event",
                       G_CALLBACK (on_window_state_event), NULL);
 
+    restore_state (self);
+
     /* Mainvbox for Menu bar + Tool bar + "Browser Area & FileArea & TagArea" + Log Area + "Status bar & 
Progress bar" */
     main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
     gtk_container_add (GTK_CONTAINER (self), main_vbox);
@@ -3127,6 +3230,7 @@ static void
 et_application_window_class_init (EtApplicationWindowClass *klass)
 {
     G_OBJECT_CLASS (klass)->finalize = et_application_window_finalize;
+    GTK_WIDGET_CLASS (klass)->destroy = et_application_window_destroy;
 
     g_type_class_add_private (klass, sizeof (EtApplicationWindowPrivate));
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]