// disfluid, implementation of the Solid specification // Copyright (C) 2021 Vivien Kraus // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . namespace Disfluid { class Settings: GLib.Object { public Account? main_account; public Account[] other_accounts; public Client client; private static string? detect_empty (string str) { if (str == "") { return null; } return str; } private static Account? get_account (Disfluid.Api api, GLib.Settings settings) { Account ret; var subject = detect_empty (settings.get_string ("subject")); var issuer = detect_empty (settings.get_string ("issuer")); var key_pair = detect_empty (settings.get_string ("key-pair")); if (subject != null && issuer != null && key_pair != null) { api.make_account_full (out ret, subject, issuer, key_pair, detect_empty (settings.get_string ("id-token-header")), detect_empty (settings.get_string ("id-token")), detect_empty (settings.get_string ("access-token")), detect_empty (settings.get_string ("refresh-token"))); return ret; } return null; } private static void set_account (Disfluid.Api api, GLib.Settings settings, Account? acct) { var subject = new char[acct.get_subject (api, 0, new char[0])]; acct.get_subject (api, 0, subject); var issuer = new char[acct.get_issuer (api, 0, new char[0])]; acct.get_issuer (api, 0, issuer); var key_pair = new char[acct.get_key_pair (api, 0, new char[0])]; acct.get_key_pair (api, 0, key_pair); var id_token_header = new char[acct.get_id_token_header (api, 0, new char[0])]; acct.get_id_token_header (api, 0, id_token_header); var id_token = new char[acct.get_id_token (api, 0, new char[0])]; acct.get_id_token (api, 0, id_token); var access_token = new char[acct.get_access_token (api, 0, new char[0])]; acct.get_access_token (api, 0, access_token); var refresh_token = new char[acct.get_refresh_token (api, 0, new char[0])]; acct.get_refresh_token (api, 0, refresh_token); settings.set_string ("subject", (string) subject); settings.set_string ("issuer", (string) issuer); settings.set_string ("key-pair", (string) key_pair); settings.set_string ("id-token-header", (string) id_token_header); settings.set_string ("id-token", (string) id_token); settings.set_string ("access-token", (string) access_token); settings.set_string ("refresh-token", (string) refresh_token); } public Settings (Disfluid.Api api) { var settings = new GLib.Settings ("eu.planete_kraus.Disfluid"); var client_settings = settings.get_child ("client"); var main_account_settings = settings.get_child ("main-account"); var other_accounts_settings = new GLib.Settings[10]; for (int i = 0 ; i < other_accounts_settings.length; i++) { var name = "other-account-%d".printf (i + 1); other_accounts_settings[i] = settings.get_child (name); } api.make_client (out client, client_settings.get_string ("client-id"), client_settings.get_string ("redirect-uri"), client_settings.get_string ("key-pair")); client_settings.changed.connect ((key) => { api.make_client (out this.client, client_settings.get_string ("client-id"), client_settings.get_string ("redirect-uri"), client_settings.get_string ("key-pair")); }); this.notify["client"].connect (() => { var client_id = new char[this.client.get_id (api, 0, new char[0])]; this.client.get_id (api, 0, client_id); client_settings.set_string ("client-id", (string) client_id); var redirect_uri = new char[this.client.get_redirect_uri (api, 0, new char[0])]; this.client.get_redirect_uri (api, 0, redirect_uri); client_settings.set_string ("redirect-uri", (string) redirect_uri); var key_pair = new char[this.client.get_key_pair (api, 0, new char[0])]; this.client.get_key_pair (api, 0, key_pair); client_settings.set_string ("key-pair", (string) key_pair); }); main_account = get_account (api, main_account_settings); this.notify["main-account"].connect (() => { set_account (api, main_account_settings, this.main_account); }); var all_other_accounts = new Disfluid.Account[other_accounts_settings.length]; var account_index = 0; foreach (var s in other_accounts_settings) { var acc = get_account (api, s); if (acc != null) { acc.copy (api, out all_other_accounts[account_index++]); } } other_accounts = new Account[account_index]; for (int i = 0; i < account_index; i++) { all_other_accounts[i].copy (api, out other_accounts[i]); } this.notify["other-accounts"].connect (() => { var settings_index = 0; foreach (unowned Disfluid.Account account in other_accounts) { set_account (api, other_accounts_settings[settings_index++], account); } }); } } }