diff --git a/meson.build b/meson.build index a07f92d..0d2e0ed 100644 --- a/meson.build +++ b/meson.build @@ -56,7 +56,7 @@ sources = files( 'src/Services/Accounts/InstanceAccount.vala', 'src/Services/Accounts/AccountStore.vala', 'src/Services/Accounts/FileAccountStore.vala', - 'src/Services/Accounts/IAccountListener.vala', + 'src/Services/Accounts/IAccountHolder.vala', 'src/Services/Accounts/Mastodon/MastodonAccount.vala', 'src/Services/Streams.vala', 'src/Services/Settings.vala', diff --git a/src/Services/Accounts/AccountStore.vala b/src/Services/Accounts/AccountStore.vala index 2a24bdc..96a1524 100644 --- a/src/Services/Accounts/AccountStore.vala +++ b/src/Services/Accounts/AccountStore.vala @@ -5,6 +5,9 @@ public abstract class Tootle.AccountStore : GLib.Object { public ArrayList saved { get; set; default = new ArrayList (); } public InstanceAccount? active { get; set; default = null; } + public signal void changed (ArrayList accounts); + public signal void switched (InstanceAccount? account); + public bool ensure_active_account () { var has_active = false; var account = find_by_handle (settings.active_account); @@ -14,9 +17,9 @@ public abstract class Tootle.AccountStore : GLib.Object { } has_active = account != null; - if (has_active) - activate (account); - else + activate (account); + + if (!has_active) app.present_window (); return has_active; @@ -53,7 +56,7 @@ public abstract class Tootle.AccountStore : GLib.Object { message (@"Removing account: $(account.handle)"); account.unsubscribe (); saved.remove (account); - saved.notify_property ("size"); + changed (saved); save (); ensure_active_account (); } @@ -70,22 +73,29 @@ public abstract class Tootle.AccountStore : GLib.Object { return iter.@get (); } - public void activate (InstanceAccount account) { - message (@"Activating $(account.handle)..."); - account.verify_credentials.begin ((obj, res) => { - try { - account.verify_credentials.end (res); - account.error = null; - } - catch (Error e) { - warning (@"Couldn't activate account $(account.handle):"); - warning (e.message); - account.error = e; - } - }); + public void activate (InstanceAccount? account) { + if (account == null) { + message ("Reset active account"); + return; + } + else { + message (@"Activating $(account.handle)..."); + account.verify_credentials.begin ((obj, res) => { + try { + account.verify_credentials.end (res); + account.error = null; + settings.active_account = account.handle; + } + catch (Error e) { + warning (@"Couldn't activate account $(account.handle):"); + warning (e.message); + account.error = e; + } + }); + } accounts.active = account; - settings.active_account = account.handle; + switched (active); } [Signal (detailed = true)] diff --git a/src/Services/Accounts/IAccountHolder.vala b/src/Services/Accounts/IAccountHolder.vala new file mode 100644 index 0000000..924071f --- /dev/null +++ b/src/Services/Accounts/IAccountHolder.vala @@ -0,0 +1,18 @@ +public interface Tootle.IAccountHolder : GLib.Object { + + protected abstract InstanceAccount? account { get; set; default = null; } + + protected void account_listener_init () { + accounts.switched.connect (on_account_changed); + accounts.changed.connect (on_accounts_changed); + on_account_changed (accounts.active); + } + protected void account_listener_free () { + accounts.switched.disconnect (on_account_changed); + accounts.changed.disconnect (on_accounts_changed); + } + + public virtual void on_account_changed (InstanceAccount? account) {} + public virtual void on_accounts_changed (Gee.ArrayList accounts) {} + +} diff --git a/src/Services/Accounts/IAccountListener.vala b/src/Services/Accounts/IAccountListener.vala deleted file mode 100644 index bab756c..0000000 --- a/src/Services/Accounts/IAccountListener.vala +++ /dev/null @@ -1,25 +0,0 @@ -[Deprecated] -public interface Tootle.IAccountListener : GLib.Object { - - protected void account_listener_init () { - accounts.notify["active"].connect (_on_active_acc_update); - accounts.saved.notify["size"].connect (_on_saved_accs_update); - on_account_changed (accounts.active); - } - protected void account_listener_free () { - accounts.notify["active"].disconnect (_on_active_acc_update); - accounts.saved.notify["size"].disconnect (_on_saved_accs_update); - } - - void _on_active_acc_update (ParamSpec s) { - on_account_changed (accounts.active); - } - - void _on_saved_accs_update (ParamSpec s) { - on_accounts_changed (accounts.saved); - } - - public virtual void on_account_changed (InstanceAccount? account) {} - public virtual void on_accounts_changed (Gee.ArrayList accounts) {} - -} diff --git a/src/Views/Notifications.vala b/src/Views/Notifications.vala index c9cd1a9..d096898 100644 --- a/src/Views/Notifications.vala +++ b/src/Views/Notifications.vala @@ -1,7 +1,7 @@ using Gtk; using Gdk; -public class Tootle.Views.Notifications : Views.Timeline, IAccountListener, IStreamListener { +public class Tootle.Views.Notifications : Views.Timeline, IAccountHolder, IStreamListener { protected int64 last_id = 0; diff --git a/src/Views/Thread.vala b/src/Views/Thread.vala index 28ccfb6..11f323f 100644 --- a/src/Views/Thread.vala +++ b/src/Views/Thread.vala @@ -1,9 +1,9 @@ using Gtk; -public class Tootle.Views.Thread : Views.Base, IAccountListener { +public class Tootle.Views.Thread : Views.Base, IAccountHolder { + protected InstanceAccount? account { get; set; default = null; } public API.Status root_status { get; construct set; } - protected InstanceAccount? account = null; protected Widgets.Status root_widget; public Thread (API.Status status) { diff --git a/src/Views/Timeline.vala b/src/Views/Timeline.vala index 339c97c..bc0ef0c 100644 --- a/src/Views/Timeline.vala +++ b/src/Views/Timeline.vala @@ -1,13 +1,13 @@ using Gtk; using Gdk; -public class Tootle.Views.Timeline : IAccountListener, IStreamListener, Views.Base { +public class Tootle.Views.Timeline : IAccountHolder, IStreamListener, Views.Base { public string url { get; construct set; } public bool is_public { get; construct set; default = false; } public Type accepts { get; set; default = typeof (API.Status); } - protected InstanceAccount? account = null; + protected InstanceAccount? account { get; set; default = null; } protected ulong on_status_added_sigig; public bool is_last_page { get; set; default = false; } diff --git a/src/Widgets/AccountsButton.vala b/src/Widgets/AccountsButton.vala index 3df784e..7bcfb3c 100644 --- a/src/Widgets/AccountsButton.vala +++ b/src/Widgets/AccountsButton.vala @@ -1,7 +1,9 @@ using Gtk; [GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/widgets/accounts_button.ui")] -public class Tootle.Widgets.AccountsButton : Gtk.MenuButton, IAccountListener { +public class Tootle.Widgets.AccountsButton : Gtk.MenuButton, IAccountHolder { + + protected InstanceAccount? account { get; set; default = null; } [GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/widgets/accounts_button_item.ui")] class Item : ListBoxRow {