[Xfce4-commits] <postler:master> Remember window size and open folder

Christian Dywan noreply at xfce.org
Sun Feb 20 23:06:01 CET 2011


Updating branch refs/heads/master
         to 3eb3ff63b6ec50261b2564461793a4b42956987c (commit)
       from a55fb210a90e0c25e61fbf4128831a1931b1ac53 (commit)

commit 3eb3ff63b6ec50261b2564461793a4b42956987c
Author: Sergio Spinatelli <spinatelli.sergio at gmail.com>
Date:   Sat Feb 19 09:54:00 2011 +0100

    Remember window size and open folder
    
    Fixes: https://bugs.launchpad.net/postler/+bug/671574

 postler/postler-bureau.vala  |   48 ++++++++++++++++++++++++++--
 postler/postler-folders.vala |   10 +++--
 postler/postler-state.vala   |   73 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 7 deletions(-)

diff --git a/postler/postler-bureau.vala b/postler/postler-bureau.vala
index 0ac9846..d7247f5 100644
--- a/postler/postler-bureau.vala
+++ b/postler/postler-bureau.vala
@@ -13,6 +13,8 @@ public class Postler.Bureau : Gtk.Window {
     Accounts accounts = new Accounts ();
     Postler.Client client = new Postler.Client ();
 
+    Postler.State previous_state = new State ();
+
     Gtk.UIManager ui;
     Gtk.ActionGroup actions;
 
@@ -332,10 +334,35 @@ public class Postler.Bureau : Gtk.Window {
     }
 
     void action_quit () {
+        update_saved_state ();
         client.quit ();
         Gtk.main_quit ();
     }
 
+    public override bool delete_event (Gdk.Event event) {
+        update_saved_state ();
+        return false;
+    }
+
+    void update_saved_state () {
+        if ((window.get_state () & Gdk.WindowState.MAXIMIZED) != 0)
+            previous_state.window_state = 1;
+        else if ((window.get_state () & Gdk.WindowState.FULLSCREEN) != 0)
+            previous_state.window_state = 2;
+        else
+            previous_state.window_state = 0;
+
+        if (previous_state.window_state == 0) {
+            int width = 0;
+            int height = 0;
+            get_size (out width, out height);
+            previous_state.width = width;
+            previous_state.height = height;
+        }
+        previous_state.open_folder = folders.selected_location ?? "";
+        previous_state.update ();
+    }
+
     void action_about () {
         string[] authors = { "Christian Dywan <christian at twotoasts.de>",
                              "Daniel Foré <daniel.p.fore at gmail.com>",
@@ -512,7 +539,18 @@ public class Postler.Bureau : Gtk.Window {
         Gdk.Rectangle monitor;
         screen.get_monitor_geometry (0, out monitor);
         double window_width = monitor.width / 1.7;
-        set_default_size ((int)window_width, (int)(monitor.height / 1.7));
+        double window_height = monitor.height / 1.7;
+
+        if ((double)previous_state.width != 0)
+            window_width = (double) previous_state.width;
+        if ((double)previous_state.height != 0)
+            window_height = (double) previous_state.height;
+
+        set_default_size ((int)window_width, (int)window_height);
+        if (previous_state.window_state == 1)
+            maximize ();
+        if (previous_state.window_state == 2)
+            fullscreen ();
 
         ui = new Gtk.UIManager ();
         actions = new Gtk.ActionGroup ("Bureau");
@@ -795,7 +833,7 @@ public class Postler.Bureau : Gtk.Window {
                 if (vpaned.position < vpaned.max_position)
                     vpaned.position = vpaned.max_position;
                 else
-                    vpaned.position = (int)(monitor.height / 1.7 / 3);
+                    vpaned.position = (int)(window_height / 3);
                 return true;
             }
             return false;
@@ -806,13 +844,17 @@ public class Postler.Bureau : Gtk.Window {
         folders.set_headers_visible (false);
         messages.set_headers_visible (false);
         messages.set_rules_hint (true);
-        vpaned.set_position ((int)(monitor.height / 1.7 / 3));
+        vpaned.set_position ((int)(window_height / 3));
         hpaned.set_position ((int)(window_width / 4));
 
         folders.messages = messages;
         messages.content = content;
 
         folders.grab_focus ();
+        GLib.Idle.add (() => {
+            folders.select_folder (previous_state.open_folder);
+            return false;
+        });
     }
 }
 
diff --git a/postler/postler-folders.vala b/postler/postler-folders.vala
index f73a696..5224c4b 100644
--- a/postler/postler-folders.vala
+++ b/postler/postler-folders.vala
@@ -456,14 +456,16 @@ public class Postler.Folders : Gtk.TreeView {
 
     public bool select_folder (string folder, Gtk.TreeIter? parent_iter=null)
         requires (messages != null) {
+        if (folder == "")
+            return false;
         Gtk.TreeIter iter;
         if (!store.iter_children (out iter, parent_iter))
             return false;
         do {
-            string name;
-            store.get (iter, Columns.NAME, out name);
+            string location;
+            store.get (iter, Columns.LOCATION, out location);
             /* Folder can be "gmail", "gmail (3)" or "Drafts" */
-            if (name.has_prefix (folder)) {
+            if (location == folder) {
                 get_selection ().select_iter (iter);
                 display_folder_iter (iter);
                 return true;
@@ -608,7 +610,7 @@ public class Postler.Folders : Gtk.TreeView {
                                  Columns.INFO, out account_info);
                 var bureau = new Bureau ();
                 GLib.Idle.add (() => {
-                    bureau.folders.select_folder (account_info.name);
+                    bureau.folders.select_folder (account_info.path + "/INBOX");
                     return false;
                 });
                 bureau.show ();
diff --git a/postler/postler-state.vala b/postler/postler-state.vala
new file mode 100644
index 0000000..502d0f8
--- /dev/null
+++ b/postler/postler-state.vala
@@ -0,0 +1,73 @@
+/*
+ Copyright (C) 2011 Sergio Spinatelli <spinatelli.sergio at gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+public class Postler.State : GLib.Object {
+    string config_path;
+    string state_file;
+    string group = "state";
+    GLib.KeyFile keyfile;
+
+    public int width { get; set; default = 0; }
+    public int height { get; set; default = 0; }
+    public int window_state { get; set; default = 0; }
+    public string open_folder { get; set; default = ""; }
+
+    public State () {
+        unowned string config_dir = Environment.get_user_config_dir ();
+        config_path = config_dir + "/" + Config.PACKAGE_NAME + "/";
+        state_file = config_path + "staterc";
+
+        reload ();
+    }
+
+    void write_keyfile () throws Error {
+        if (DirUtils.create_with_parents (config_path, 0700) != 0)
+            throw new GLib.FileError.FAILED (_("Config folder couldn't be created."));
+        FileUtils.set_contents (state_file, keyfile.to_data ());
+    }
+
+    public void update () {
+        try {
+            keyfile.set_integer (group, "width", width);
+            keyfile.set_integer (group, "height", height);
+            keyfile.set_integer (group, "window-state", window_state);
+            keyfile.set_string (group, "open-folder", open_folder);
+
+            write_keyfile ();
+        } catch (GLib.Error error) {
+            GLib.critical ("Failed to save \"%s\": %s", state_file, error.message);
+        }
+    }
+
+    void reload () {
+        keyfile = new GLib.KeyFile ();
+        try {
+            keyfile.load_from_file (state_file, 0);
+        }
+        catch (GLib.Error error) {
+            GLib.debug ("Failed to load \"%s\": %s", state_file, error.message);
+        }
+
+        try {
+            if (keyfile.has_key (group, "width"))
+                width = keyfile.get_integer (group, "width");
+            if (keyfile.has_key (group, "height"))
+                height = keyfile.get_integer (group, "height");
+            if (keyfile.has_key (group, "window-state"))
+                window_state = keyfile.get_integer (group, "window-state");
+            if (keyfile.has_key (group, "open-folder"))
+                open_folder = keyfile.get_string (group, "open-folder");
+        } catch (GLib.Error error) {
+            /* i18n: File was found but contains invalid values */
+            GLib.critical (_("Failed to parse state in \"%s\": %s"),
+                             state_file, error.message);
+        }
+    }
+}



More information about the Xfce4-commits mailing list