From 1dd8e2057764c247e601ff1a3e1dc3b9b3b6ec3f Mon Sep 17 00:00:00 2001 From: bleakgrey Date: Sun, 20 May 2018 15:43:42 +0300 Subject: [PATCH] Add settings view --- data/com.github.bleakgrey.tootle.gschema.xml | 10 +++ meson.build | 1 + src/Dialogs/SettingsDialog.vala | 73 ++++++++++++++++++++ src/NetManager.vala | 34 ++++++--- src/Notificator.vala | 17 +++-- src/SettingsManager.vala | 2 + src/Widgets/AccountsButton.vala | 3 +- 7 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 src/Dialogs/SettingsDialog.vala diff --git a/data/com.github.bleakgrey.tootle.gschema.xml b/data/com.github.bleakgrey.tootle.gschema.xml index d75c993..d843e0f 100644 --- a/data/com.github.bleakgrey.tootle.gschema.xml +++ b/data/com.github.bleakgrey.tootle.gschema.xml @@ -36,5 +36,15 @@ Cache images to reduce network load + + 64 + Cache size + Sets the maximum size of cached content + + + true + Update timelines in real-time + + diff --git a/meson.build b/meson.build index 3e8be4e..baff2a7 100644 --- a/meson.build +++ b/meson.build @@ -45,6 +45,7 @@ executable( 'src/Widgets/AttachmentWidget.vala', 'src/Widgets/AttachmentBox.vala', 'src/Dialogs/PostDialog.vala', + 'src/Dialogs/SettingsDialog.vala', 'src/Views/AbstractView.vala', 'src/Views/AddAccountView.vala', 'src/Views/TimelineView.vala', diff --git a/src/Dialogs/SettingsDialog.vala b/src/Dialogs/SettingsDialog.vala new file mode 100644 index 0000000..27cdb7f --- /dev/null +++ b/src/Dialogs/SettingsDialog.vala @@ -0,0 +1,73 @@ +using Gtk; +using Tootle; + +public class Tootle.SettingsDialog : Gtk.Dialog { + + private static SettingsDialog dialog; + + private Gtk.Grid grid; + + public SettingsDialog () { + Object ( + border_width: 6, + deletable: false, + resizable: false, + title: _("Settings"), + transient_for: Tootle.window + ); + + int i = 0; + grid = new Gtk.Grid (); + + grid.attach (new Granite.HeaderLabel (_("Timelines")), 0, i++, 2, 1); + grid.attach (new SettingsLabel (_("Real-time updates:")), 0, i); + grid.attach (new SettingsSwitch ("live-updates"), 1, i++); + + grid.attach (new Granite.HeaderLabel (_("Caching")), 0, i++, 2, 1); + grid.attach (new SettingsLabel (_("Use cache:")), 0, i); + grid.attach (new SettingsSwitch ("cache"), 1, i++); + grid.attach (new SettingsLabel (_("Max cache size (MB):")), 0, i); + var cache_size = new Gtk.SpinButton.with_range (16, 256, 1); + settings.schema.bind ("cache-size", cache_size, "value", SettingsBindFlags.DEFAULT); + grid.attach (cache_size, 1, i++); + + grid.attach (new Granite.HeaderLabel (_("Notifications")), 0, i++, 2, 1); + grid.attach (new SettingsLabel (_("Always receive notifications:")), 0, i); + grid.attach (new SettingsSwitch ("always-online"), 1, i++); + + var content = get_content_area () as Gtk.Box; + content.pack_start (grid, false, false, 0); + + var close = add_button (_("_Close"), Gtk.ResponseType.CLOSE) as Gtk.Button; + close.clicked.connect (() => { + destroy (); + dialog = null; + }); + + show_all (); + } + + public static void open () { + if (dialog == null) + dialog = new SettingsDialog (); + } + + protected class SettingsLabel : Gtk.Label { + public SettingsLabel (string text) { + label = text; + halign = Gtk.Align.END; + margin_start = 12; + margin_end = 12; + } + } + + protected class SettingsSwitch : Gtk.Switch { + public SettingsSwitch (string setting) { + halign = Gtk.Align.START; + valign = Gtk.Align.CENTER; + margin_bottom = 6; + Tootle.settings.schema.bind (setting, this, "active", SettingsBindFlags.DEFAULT); + } + } + +} diff --git a/src/NetManager.vala b/src/NetManager.vala index 782a711..23ed95b 100644 --- a/src/NetManager.vala +++ b/src/NetManager.vala @@ -20,10 +20,8 @@ public class Tootle.NetManager : GLib.Object { private Notificator? notificator; construct { - cache_path = "%s/%s".printf (GLib.Environment.get_user_cache_dir (), "tootle"); + cache_path = "%s/%s".printf (GLib.Environment.get_user_cache_dir (), Tootle.app.application_id); cache = new Soup.Cache (cache_path, Soup.CacheType.SINGLE_USER); - cache.set_max_size (1024 * 1024 * 64); // 64 mb - session = new Soup.Session (); session.ssl_strict = true; session.ssl_use_system_ca_file = true; @@ -35,16 +33,18 @@ public class Tootle.NetManager : GLib.Object { finished (); }); + Tootle.app.shutdown.connect (() => { + cache.dump (); + }); + Tootle.settings.changed.connect (on_settings_changed); + on_settings_changed (); + // Soup.Logger logger = new Soup.Logger (Soup.LoggerLogLevel.BODY, -1); // session.add_feature (logger); } public NetManager() { GLib.Object(); - if (Tootle.settings.cache) { - session.add_feature (cache); - Tootle.app.shutdown.connect (() => cache.flush ()); - } Tootle.accounts.switched.connect (acc => { if (notificator != null) @@ -57,6 +57,24 @@ public class Tootle.NetManager : GLib.Object { }); } + private void on_settings_changed () { + cache.set_max_size (1024 * 1024 * Tootle.settings.cache_size); + + var has_cache = session.has_feature (cache.get_type ()); + if (Tootle.settings.cache) { + if (!has_cache) { + debug ("Turning on cache"); + session.add_feature (cache); + } + } + else { + if (has_cache) { + debug ("Turning off cache"); + session.remove_feature (cache); + } + } + } + public void abort (Soup.Message msg) { session.cancel_message (msg, 0); } @@ -161,6 +179,4 @@ public class Tootle.NetManager : GLib.Object { Tootle.network.queue (msg); } - - } diff --git a/src/Notificator.vala b/src/Notificator.vala index e4a24b7..0d0eee8 100644 --- a/src/Notificator.vala +++ b/src/Notificator.vala @@ -39,17 +39,26 @@ public class Tootle.Notificator : GLib.Object { var type = root.get_string_member ("event"); switch (type) { case "update": - var status = Status.parse (sanitize (root)); - network.status_added (ref status, "home"); + if (Tootle.settings.live_updates) { + var status = Status.parse (sanitize (root)); + network.status_added (ref status, "home"); + } + else + Tootle.app.toast ("New post available"); + break; case "delete": - var id = int64.parse (root.get_string_member("payload")); - network.status_removed (id); + if (Tootle.settings.live_updates) { + var id = int64.parse (root.get_string_member("payload")); + network.status_removed (id); + } + break; case "notification": var notif = Notification.parse (sanitize (root)); toast (notif); network.notification (ref notif); + break; default: warning ("Unknown push event: %s", type); diff --git a/src/SettingsManager.vala b/src/SettingsManager.vala index f902c1a..eca2c82 100644 --- a/src/SettingsManager.vala +++ b/src/SettingsManager.vala @@ -7,6 +7,8 @@ public class Tootle.SettingsManager : Granite.Services.Settings { public string instance_url { get; set; } public bool always_online { get; set; } public bool cache { get; set; } + public int cache_size { get; set; } + public bool live_updates { get; set; } public void clear_account (){ access_token = "null"; diff --git a/src/Widgets/AccountsButton.vala b/src/Widgets/AccountsButton.vala index bc4c488..1ea6c26 100644 --- a/src/Widgets/AccountsButton.vala +++ b/src/Widgets/AccountsButton.vala @@ -67,6 +67,7 @@ public class Tootle.AccountsButton : Gtk.MenuButton{ item_settings = new Gtk.ModelButton (); item_settings.text = _("Settings"); + item_settings.clicked.connect (() => SettingsDialog.open ()); grid = new Gtk.Grid (); grid.orientation = Gtk.Orientation.VERTICAL; @@ -77,7 +78,7 @@ public class Tootle.AccountsButton : Gtk.MenuButton{ grid.attach(new Gtk.Separator (Gtk.Orientation.HORIZONTAL), 0, 4, 1, 1); grid.attach(item_refresh, 0, 5, 1, 1); grid.attach(item_search, 0, 6, 1, 1); - //grid.attach(item_settings, 0, 8, 1, 1); + grid.attach(item_settings, 0, 8, 1, 1); grid.show_all (); menu = new Gtk.Popover (null);