From ba87ae2c2924e0dc356281ad53af17ff27dec41b Mon Sep 17 00:00:00 2001
From: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
Date: Tue, 13 Sep 2016 14:27:20 -0400
Subject: [PATCH] save and restore main window size

Saves and restores the main window size in the GSettings.

The default size is updated to be 800x500. Gtk should automatically
resize it smaller if it doesn't fit on the screen.

Change-Id: Ia85f9fe870cf0695c5095cb138c2b8a61a8855a7
Tuleap: #1030
---
 data/cx.ring.RingGnome.gschema.xml | 10 ++++++++++
 src/ringmainwindow.cpp             | 26 ++++++++++++++++++++++++++
 ui/ringmainwindow.ui               |  3 ---
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/data/cx.ring.RingGnome.gschema.xml b/data/cx.ring.RingGnome.gschema.xml
index 6bd1715e..1f8b59bc 100644
--- a/data/cx.ring.RingGnome.gschema.xml
+++ b/data/cx.ring.RingGnome.gschema.xml
@@ -34,5 +34,15 @@
         <default>true</default>
         <summary>Enable notifications for new chat messages.</summary>
     </key>
+    <key name="window-width" type="i">
+        <default>800</default>
+        <summary>Main window width.</summary>
+        <description>Main window width.</description>
+    </key>
+    <key name="window-height" type="i">
+        <default>500</default>
+        <summary>Main window height.</summary>
+        <description>Main window height.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index 3b979cea..caed435b 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -66,6 +66,7 @@
 #include "recentcontactsview.h"
 #include "chatview.h"
 #include "avatarmanipulation.h"
+#include "utils/files.h"
 
 static constexpr const char* CALL_VIEW_NAME             = "calls";
 static constexpr const char* CREATE_ACCOUNT_VIEW_NAME   = "wizard";
@@ -143,6 +144,8 @@ struct _RingMainWindowPrivate
 
     /* fullscreen */
     gboolean is_fullscreen;
+
+    GSettings *settings;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE(RingMainWindow, ring_main_window, GTK_TYPE_APPLICATION_WINDOW);
@@ -1078,12 +1081,30 @@ history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
     selection_changed(self);
 }
 
+static gboolean
+window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
+{
+    auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
+
+    g_settings_set_int(priv->settings, "window-width", event->width);
+    g_settings_set_int(priv->settings, "window-height", event->height);
+
+    return GDK_EVENT_PROPAGATE;
+}
+
 static void
 ring_main_window_init(RingMainWindow *win)
 {
     RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
     gtk_widget_init_template(GTK_WIDGET(win));
 
+    /* bind to window size settings */
+    priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
+    auto width = g_settings_get_int(priv->settings, "window-width");
+    auto height = g_settings_get_int(priv->settings, "window-height");
+    gtk_window_set_default_size(GTK_WINDOW(win), width, height);
+    g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
+
      /* set window icon */
     GError *error = NULL;
     GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
@@ -1287,6 +1308,11 @@ ring_main_window_dispose(GObject *object)
 static void
 ring_main_window_finalize(GObject *object)
 {
+    RingMainWindow *self = RING_MAIN_WINDOW(object);
+    RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
+
+    g_clear_object(&priv->settings);
+
     G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
 }
 
diff --git a/ui/ringmainwindow.ui b/ui/ringmainwindow.ui
index 5e9f716a..a08e2598 100644
--- a/ui/ringmainwindow.ui
+++ b/ui/ringmainwindow.ui
@@ -3,10 +3,7 @@
 <interface>
   <requires lib="gtk+" version="3.10"/>
   <template class="RingMainWindow" parent="GtkApplicationWindow">
-    <property name="can_focus">False</property>
     <property name="show_menubar">False</property>
-    <property name="default-width">700</property>
-    <property name="default-height">450</property>
     <!-- header definition -->
     <child type="titlebar">
       <object class="GtkHeaderBar" id="header">
-- 
GitLab