2
* -*- Mode:Vala; indent-tabs-mode:t; tab-width:4; encoding:utf8 -*-
3
* Copyright 2016 Canonical Ltd.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 3.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
* Xavi Garcia <xavi.garcia.mena@canonical.com>
25
[DBus (name="com.canonical.UnityGreeter.List")]
26
interface GreeterListInterfaceAccess : Object
28
public abstract async string get_active_entry () throws IOError;
29
public signal void entry_selected (string entry_name);
32
public class AccountsServiceAccess : Object
34
private DBusProxy _user_proxy;
35
private GreeterListInterfaceAccess _greeter_proxy;
36
private double _volume = 0.0;
37
private string _last_running_player = "";
38
private bool _mute = false;
39
private Cancellable _dbus_call_cancellable;
41
public AccountsServiceAccess ()
43
_dbus_call_cancellable = new Cancellable ();
44
setup_accountsservice.begin ();
47
~AccountsServiceAccess ()
49
_dbus_call_cancellable.cancel ();
52
public string last_running_player
56
return _last_running_player;
60
sync_last_running_player_to_accountsservice.begin (value);
72
sync_mute_to_accountsservice.begin (value);
84
sync_volume_to_accountsservice.begin (value);
88
/* AccountsService operations */
89
private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[]? invalidated_properties)
91
Variant volume_variant = changed_properties.lookup_value ("Volume", VariantType.DOUBLE);
92
if (volume_variant != null) {
93
var volume = volume_variant.get_double ();
94
if (volume >= 0 && _volume != volume) {
96
this.notify_property("volume");
100
Variant mute_variant = changed_properties.lookup_value ("Muted", VariantType.BOOLEAN);
101
if (mute_variant != null) {
102
_mute = mute_variant.get_boolean ();
103
this.notify_property("mute");
106
Variant last_running_player_variant = changed_properties.lookup_value ("LastRunningPlayer", VariantType.STRING);
107
if (last_running_player_variant != null) {
108
_last_running_player = last_running_player_variant.get_string ();
109
this.notify_property("last-running-player");
113
private async void setup_user_proxy (string? username_in = null)
115
var username = username_in;
118
// Look up currently selected greeter user, if asked
119
if (username == null) {
121
username = yield _greeter_proxy.get_active_entry ();
122
if (username == "" || username == null)
124
} catch (GLib.Error e) {
125
warning ("unable to find Accounts path for user %s: %s", username == null ? "null" : username, e.message);
130
// Get master AccountsService object
131
DBusProxy accounts_proxy;
133
accounts_proxy = yield DBusProxy.create_for_bus (BusType.SYSTEM, DBusProxyFlags.DO_NOT_LOAD_PROPERTIES | DBusProxyFlags.DO_NOT_CONNECT_SIGNALS, null, "org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts");
134
} catch (GLib.Error e) {
135
warning ("unable to get greeter proxy: %s", e.message);
139
// Find user's AccountsService object
141
var user_path_variant = yield accounts_proxy.call ("FindUserByName", new Variant ("(s)", username), DBusCallFlags.NONE, -1);
143
if (user_path_variant.check_format_string ("(o)", true)) {
144
user_path_variant.get ("(o)", out user_path);
145
_user_proxy = yield DBusProxy.create_for_bus (BusType.SYSTEM, DBusProxyFlags.GET_INVALIDATED_PROPERTIES, null, "org.freedesktop.Accounts", user_path, "com.ubuntu.AccountsService.Sound");
147
warning ("Unable to find user name after calling FindUserByName. Expected type: %s and obtained %s", "(o)", user_path_variant.get_type_string () );
150
} catch (GLib.Error e) {
151
warning ("unable to find Accounts path for user %s: %s", username, e.message);
155
// Get current values and listen for changes
156
_user_proxy.g_properties_changed.connect (accountsservice_props_changed_cb);
158
var props_variant = yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "GetAll", new Variant ("(s)", _user_proxy.get_interface_name ()), null, DBusCallFlags.NONE, -1);
159
if (props_variant.check_format_string ("(@a{sv})", true)) {
161
props_variant.get ("(@a{sv})", out props);
162
accountsservice_props_changed_cb(_user_proxy, props, null);
164
warning ("Unable to get accounts service properties after calling GetAll. Expected type: %s and obtained %s", "(@a{sv})", props_variant.get_type_string () );
167
} catch (GLib.Error e) {
168
debug("Unable to get properties for user %s at first try: %s", username, e.message);
172
private void greeter_user_changed (string username)
174
setup_user_proxy.begin (username);
177
private async void setup_accountsservice ()
179
if (Environment.get_variable ("XDG_SESSION_CLASS") == "greeter") {
181
_greeter_proxy = yield Bus.get_proxy (BusType.SESSION, "com.canonical.UnityGreeter", "/list");
182
} catch (GLib.Error e) {
183
warning ("unable to get greeter proxy: %s", e.message);
186
_greeter_proxy.entry_selected.connect (greeter_user_changed);
187
yield setup_user_proxy ();
189
// We are in a user session. We just need our own proxy
190
unowned string username = Environment.get_variable ("USER");
191
if (username != null && username != "") {
192
yield setup_user_proxy (username);
197
private async void sync_last_running_player_to_accountsservice (string last_running_player)
199
if (_user_proxy == null)
203
yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "Set", new Variant ("(ssv)", _user_proxy.get_interface_name (), "LastRunningPlayer", new Variant ("s", last_running_player)), null, DBusCallFlags.NONE, -1, _dbus_call_cancellable);
204
} catch (GLib.Error e) {
205
warning ("unable to sync last running player %s to AccountsService: %s",last_running_player, e.message);
207
_last_running_player = last_running_player;
210
private async void sync_volume_to_accountsservice (double volume)
212
if (_user_proxy == null)
216
yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "Set", new Variant ("(ssv)", _user_proxy.get_interface_name (), "Volume", new Variant ("d", volume)), null, DBusCallFlags.NONE, -1, _dbus_call_cancellable);
217
} catch (GLib.Error e) {
218
warning ("unable to sync volume %f to AccountsService: %s", volume, e.message);
222
private async void sync_mute_to_accountsservice (bool mute)
224
if (_user_proxy == null)
228
yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "Set", new Variant ("(ssv)", _user_proxy.get_interface_name (), "Muted", new Variant ("b", mute)), null, DBusCallFlags.NONE, -1, _dbus_call_cancellable);
229
} catch (GLib.Error e) {
230
warning ("unable to sync mute %s to AccountsService: %s", mute ? "true" : "false", e.message);