From f677414715d5a79c0d88a9833690e5b8f6e5254a Mon Sep 17 00:00:00 2001 From: Niels De Graef Date: Sun, 24 Jul 2022 13:33:04 +0200 Subject: Add a preferences window This allows us to simplify the main menu, as well as having an easy way in the future to add extra options. --- data/contacts.gresource.xml | 2 +- .../scalable/actions/external-link-symbolic.svg | 72 +++++++++++++++++--- data/ui/contacts-accounts-list.ui | 16 ----- data/ui/contacts-main-window.ui | 11 +-- data/ui/contacts-preferences-window.ui | 10 +++ po/POTFILES.in | 2 + po/POTFILES.skip | 1 + src/contacts-accounts-list.vala | 47 +++++++------ src/contacts-addressbook-dialog.vala | 7 +- src/contacts-app.vala | 38 ++--------- src/contacts-preferences-window.vala | 78 ++++++++++++++++++++++ src/contacts-utils.vala | 6 ++ src/meson.build | 1 + 13 files changed, 196 insertions(+), 95 deletions(-) delete mode 100644 data/ui/contacts-accounts-list.ui create mode 100644 data/ui/contacts-preferences-window.ui create mode 100644 src/contacts-preferences-window.vala diff --git a/data/contacts.gresource.xml b/data/contacts.gresource.xml index 533e3d0..89c8052 100644 --- a/data/contacts.gresource.xml +++ b/data/contacts.gresource.xml @@ -14,7 +14,6 @@ icons/scalable/actions/website-symbolic.svg gtk/help-overlay.ui - ui/contacts-accounts-list.ui ui/contacts-avatar-selector.ui ui/contacts-contact-pane.ui ui/contacts-crop-dialog.ui @@ -22,6 +21,7 @@ ui/contacts-link-suggestion-grid.ui ui/contacts-linked-personas-dialog.ui ui/contacts-main-window.ui + ui/contacts-preferences-window.ui ui/contacts-setup-window.ui diff --git a/data/icons/scalable/actions/external-link-symbolic.svg b/data/icons/scalable/actions/external-link-symbolic.svg index 1ac7ee7..74f85c3 100644 --- a/data/icons/scalable/actions/external-link-symbolic.svg +++ b/data/icons/scalable/actions/external-link-symbolic.svg @@ -1,11 +1,61 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + external-link + + \ No newline at end of file diff --git a/data/ui/contacts-accounts-list.ui b/data/ui/contacts-accounts-list.ui deleted file mode 100644 index b95ce63..0000000 --- a/data/ui/contacts-accounts-list.ui +++ /dev/null @@ -1,16 +0,0 @@ - - - - diff --git a/data/ui/contacts-main-window.ui b/data/ui/contacts-main-window.ui index 2e20f97..289c56c 100644 --- a/data/ui/contacts-main-window.ui +++ b/data/ui/contacts-main-window.ui @@ -16,16 +16,9 @@
- Change Address Book… - app.change-book + Preferences + app.show-preferences - - Online Accounts <sup>↗</sup> - app.online-accounts - True - -
-
Keyboard Shortcuts win.show-help-overlay diff --git a/data/ui/contacts-preferences-window.ui b/data/ui/contacts-preferences-window.ui new file mode 100644 index 0000000..fc4d2b4 --- /dev/null +++ b/data/ui/contacts-preferences-window.ui @@ -0,0 +1,10 @@ + + + + diff --git a/po/POTFILES.in b/po/POTFILES.in index 36e9a36..4ce720e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -11,6 +11,7 @@ data/ui/contacts-editor-menu.ui data/ui/contacts-link-suggestion-grid.ui data/ui/contacts-linked-personas-dialog.ui data/ui/contacts-main-window.ui +data/ui/contacts-preferences-window.ui data/ui/contacts-setup-window.ui src/contacts-accounts-list.vala src/contacts-addressbook-dialog.vala @@ -32,6 +33,7 @@ src/contacts-link-suggestion-grid.vala src/contacts-linked-personas-dialog.vala src/contacts-main-window.vala src/contacts-operation.vala +src/contacts-preferences-window.vala src/contacts-settings.vala src/contacts-setup-window.vala src/contacts-type-combo.vala diff --git a/po/POTFILES.skip b/po/POTFILES.skip index f5fcf7a..627134e 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -22,6 +22,7 @@ src/contacts-link-suggestion-grid.c src/contacts-linked-personas-dialog.c src/contacts-main-window.c src/contacts-operation.c +src/contacts-preferences-window.c src/contacts-settings.c src/contacts-setup-window.c src/contacts-type-combo.c diff --git a/src/contacts-accounts-list.vala b/src/contacts-accounts-list.vala index 202c160..db04cc9 100644 --- a/src/contacts-accounts-list.vala +++ b/src/contacts-accounts-list.vala @@ -25,14 +25,12 @@ using Folks; * * Internally, each "address book" is a {@link Folks.PersonaStore}. */ -[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-accounts-list.ui")] -public class Contacts.AccountsList : Adw.Bin { - - [GtkChild] - private unowned Gtk.ListBox listbox; +public class Contacts.AccountsList : Adw.PreferencesGroup { private Gtk.SingleSelection selection; + private GenericArray rows = new GenericArray (); + /** The selected PersonaStore (or null if none) */ public PersonaStore? selected_store { get { return (PersonaStore) this.selection.selected_item; } @@ -47,32 +45,20 @@ public class Contacts.AccountsList : Adw.Bin { var model = new Gtk.FilterListModel (contacts_store.address_books, (owned) filter); + model.items_changed.connect (on_model_items_changed); + on_model_items_changed (model, 0, 0, model.get_n_items ()); + // Setup the selection model this.selection = new Gtk.SingleSelection (model); // Update the row when the selection model changes this.selection.selection_changed.connect ((sel, pos, n_items) => { for (uint i = pos; i < pos + n_items; i++) { - var row = (AddressbookRow?) this.listbox.get_row_at_index ((int) i); - if (row != null) - row.selected = this.selection.is_selected (i); + this.rows[i].selected = this.selection.is_selected (i); } notify_property ("selected-store"); }); - // Now bind the listbox to it - this.listbox.bind_model (model, (item) => { - unowned var persona_store = (PersonaStore) item; - var row = new AddressbookRow (persona_store); - - // Update the selection model when the row is activated - row.activated.connect ((row) => { - this.selection.set_selected ((uint) row.get_index ()); - }); - - return row; - }); - // Initially, the primary store (if set) is selected for (uint i = 0; i < model.get_n_items (); i++) { var persona_store = (PersonaStore) model.get_item (i); @@ -81,6 +67,25 @@ public class Contacts.AccountsList : Adw.Bin { } } + private void on_model_items_changed (ListModel model, uint pos, uint removed, uint added) { + for (uint i = pos; i < pos + removed; i++) { + remove (this.rows[i]); + this.rows.remove_index (i); + } + + for (uint i = pos; i < pos + added; i++) { + var persona_store = (PersonaStore) model.get_item(i); + var row = new AddressbookRow (persona_store); + add (row); + this.rows.add (row); + + // Update the selection model when the row is activated + row.activated.connect ((row) => { + this.selection.set_selected ((uint) row.get_index ()); + }); + } + } + private class AddressbookRow : Adw.ActionRow { public PersonaStore persona_store { get; construct set; } diff --git a/src/contacts-addressbook-dialog.vala b/src/contacts-addressbook-dialog.vala index 03b13b0..14d7153 100644 --- a/src/contacts-addressbook-dialog.vala +++ b/src/contacts-addressbook-dialog.vala @@ -74,10 +74,7 @@ public class Contacts.AddressbookDialog : Gtk.Dialog { return; unowned var e_store = (Edsf.PersonaStore) this.accounts_list.selected_store; - if (e_store != null) { - eds_source_registry.set_default_address_book (e_store.source); - var settings = new GLib.Settings ("org.freedesktop.folks"); - settings.set_string ("primary-store", "eds:%s".printf (e_store.id)); - } + if (e_store != null) + Utils.set_primary_store (e_store); } } diff --git a/src/contacts-app.vala b/src/contacts-app.vala index dcf3e6e..b127554 100644 --- a/src/contacts-app.vala +++ b/src/contacts-app.vala @@ -36,8 +36,7 @@ public class Contacts.App : Adw.Application { { "quit", quit_action }, { "help", show_help }, { "about", show_about }, - { "change-book", change_address_book }, - { "online-accounts", online_accounts }, + { "show-preferences", show_preferences }, { "show-contact", on_show_contact, "s"} }; @@ -121,30 +120,9 @@ public class Contacts.App : Adw.Application { dialog.show (); } - public void online_accounts () { - try { - var proxy = new DBusProxy.for_bus_sync (BusType.SESSION, - DBusProxyFlags.NONE, - null, - "org.gnome.Settings", - "/org/gnome/Settings", - "org.gtk.Actions"); - - var builder = new VariantBuilder (new VariantType ("av")); - builder.add ("v", new Variant.string ("")); - var param = new Variant.tuple ({ - new Variant.string ("launch-panel"), - new Variant.array (new VariantType ("v"), { - new Variant ("v", new Variant ("(sav)", "online-accounts", builder)) - }), - new Variant.array (new VariantType ("{sv}"), {}) - }); - - proxy.call_sync ("Activate", param, DBusCallFlags.NONE, -1); - } catch (Error e) { - // TODO: Show error dialog - warning ("Couldn't open online-accounts: %s", e.message); - } + public void show_preferences () { + var prefs_window = new PreferencesWindow (this.contacts_store, this.window); + prefs_window.show (); } public void show_help () { @@ -278,19 +256,15 @@ public class Contacts.App : Adw.Application { private void run_setup () { debug ("Running initial setup"); - // Disable change-book action (don't want the user to do that during setup) - unowned var change_book_action = lookup_action ("change-book") as SimpleAction; - change_book_action.set_enabled (false); - // Create and show the setup window var setup_window = new SetupWindow (this, this.contacts_store); setup_window.setup_done.connect ((selected_store) => { setup_window.destroy (); - eds_source_registry.set_default_address_book (selected_store.source); + unowned var edsf_store = (Edsf.PersonaStore) selected_store; + Utils.set_primary_store (edsf_store); this.settings.did_initial_setup = true; - change_book_action.set_enabled (true); // re-enable change-book action create_window (); }); setup_window.show (); diff --git a/src/contacts-preferences-window.vala b/src/contacts-preferences-window.vala new file mode 100644 index 0000000..e6abbd2 --- /dev/null +++ b/src/contacts-preferences-window.vala @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2022 Niels De Graef + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using Folks; + +[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-preferences-window.ui")] +public class Contacts.PreferencesWindow : Adw.PreferencesWindow { + + [GtkChild] + private unowned Adw.PreferencesPage address_books_page; + + public PreferencesWindow (Store contacts_store, Gtk.Window? transient_for) { + Object (transient_for: transient_for, search_enabled: false); + + var acc_list = new AccountsList (contacts_store); + acc_list.title = _("Primary Address Book"); + acc_list.description = _("New contacts will be added to the selected address book. You are able to view and edit contacts from other address books."); + this.address_books_page.add (acc_list); + + acc_list.notify["selected-store"].connect ((obj, pspec) => { + var edsf_store = (Edsf.PersonaStore) acc_list.selected_store; + Utils.set_primary_store (edsf_store); + }); + + var goa_button_content = new Adw.ButtonContent (); + goa_button_content.label = _("_Online Accounts"); + goa_button_content.use_underline = true; + goa_button_content.icon_name = "external-link-symbolic"; + var goa_button = new Gtk.Button (); + goa_button.set_child (goa_button_content); + goa_button.tooltip_text = _("Opens the Online Accounts panel in GNOME Settings"); + goa_button.margin_top = 36; + goa_button.halign = Gtk.Align.CENTER; + goa_button.add_css_class ("pill"); + goa_button.clicked.connect (on_goa_button_clicked); + acc_list.add (goa_button); + } + + private void on_goa_button_clicked (Gtk.Button goa_button) { + try { + var proxy = new DBusProxy.for_bus_sync (BusType.SESSION, + DBusProxyFlags.NONE, + null, + "org.gnome.Settings", + "/org/gnome/Settings", + "org.gtk.Actions"); + + var builder = new VariantBuilder (new VariantType ("av")); + builder.add ("v", new Variant.string ("")); + var param = new Variant.tuple ({ + new Variant.string ("launch-panel"), + new Variant.array (new VariantType ("v"), { + new Variant ("v", new Variant ("(sav)", "online-accounts", builder)) + }), + new Variant.array (new VariantType ("{sv}"), {}) + }); + + proxy.call_sync ("Activate", param, DBusCallFlags.NONE, -1); + } catch (Error e) { + // TODO: Show error dialog + warning ("Couldn't open online-accounts: %s", e.message); + } + } +} diff --git a/src/contacts-utils.vala b/src/contacts-utils.vala index a5c28c0..f5d6a66 100644 --- a/src/contacts-utils.vala +++ b/src/contacts-utils.vala @@ -29,6 +29,12 @@ namespace Contacts { namespace Contacts.Utils { + public void set_primary_store (Edsf.PersonaStore e_store) { + eds_source_registry.set_default_address_book (e_store.source); + var settings = new GLib.Settings ("org.freedesktop.folks"); + settings.set_string ("primary-store", "eds:%s".printf (e_store.id)); + } + public void compose_mail (string email) { var mailto_uri = "mailto:" + Uri.escape_string (email, "@" , false); Gtk.show_uri (null, mailto_uri, 0); diff --git a/src/meson.build b/src/meson.build index 1f3890e..c246b61 100644 --- a/src/meson.build +++ b/src/meson.build @@ -87,6 +87,7 @@ contacts_vala_sources = files( 'contacts-link-suggestion-grid.vala', 'contacts-linked-personas-dialog.vala', 'contacts-main-window.vala', + 'contacts-preferences-window.vala', 'contacts-settings.vala', 'contacts-setup-window.vala', 'contacts-type-combo.vala', -- cgit v1.2.1