~ci-train-bot/indicator-sound/indicator-sound-ubuntu-zesty-2138

« back to all changes in this revision

Viewing changes to src/accounts-service-access.vala

  • Committer: CI Train Bot
  • Author(s): Xavi Garcia Mena
  • Date: 2016-03-07 10:13:38 UTC
  • mfrom: (529.1.8 last-runnin-player-charles)
  • Revision ID: ci-train-bot@canonical.com-20160307101338-x2wynmsb4sch7jag
This branch sets the last running player using accounts service instead of gsettings.
It also includes a new class AccountsServiceAccess, to centralize all accesses to account service properties.
Approved by: PS Jenkins bot, Charles Kerr

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * -*- Mode:Vala; indent-tabs-mode:t; tab-width:4; encoding:utf8 -*-
 
3
 * Copyright 2016 Canonical Ltd.
 
4
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *
 
17
 * Authors:
 
18
 *      Xavi Garcia <xavi.garcia.mena@canonical.com>
 
19
 */
 
20
 
 
21
using PulseAudio;
 
22
using Notify;
 
23
using Gee;
 
24
 
 
25
[DBus (name="com.canonical.UnityGreeter.List")]
 
26
interface GreeterListInterfaceAccess : Object
 
27
{
 
28
        public abstract async string get_active_entry () throws IOError;
 
29
        public signal void entry_selected (string entry_name);
 
30
}
 
31
 
 
32
public class AccountsServiceAccess : Object
 
33
{
 
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;
 
40
 
 
41
        public AccountsServiceAccess ()
 
42
        {
 
43
                _dbus_call_cancellable = new Cancellable ();
 
44
                setup_accountsservice.begin ();
 
45
        }
 
46
 
 
47
        ~AccountsServiceAccess ()
 
48
        {
 
49
                _dbus_call_cancellable.cancel ();
 
50
        }
 
51
 
 
52
        public string last_running_player 
 
53
        { 
 
54
                get 
 
55
                { 
 
56
                        return _last_running_player; 
 
57
                } 
 
58
                set 
 
59
                { 
 
60
                        sync_last_running_player_to_accountsservice.begin (value);
 
61
                } 
 
62
        }
 
63
 
 
64
        public bool mute 
 
65
        { 
 
66
                get 
 
67
                { 
 
68
                        return _mute; 
 
69
                } 
 
70
                set 
 
71
                { 
 
72
                        sync_mute_to_accountsservice.begin (value);
 
73
                } 
 
74
        }
 
75
 
 
76
        public double volume 
 
77
        { 
 
78
                get 
 
79
                { 
 
80
                        return _volume; 
 
81
                } 
 
82
                set 
 
83
                { 
 
84
                        sync_volume_to_accountsservice.begin (value);
 
85
                } 
 
86
        }
 
87
 
 
88
        /* AccountsService operations */
 
89
        private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[]? invalidated_properties)
 
90
        {
 
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) {
 
95
                                _volume = volume;
 
96
                                this.notify_property("volume");
 
97
                        }
 
98
                }
 
99
 
 
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");
 
104
                }
 
105
 
 
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");
 
110
                }
 
111
        }
 
112
 
 
113
        private async void setup_user_proxy (string? username_in = null)
 
114
        {
 
115
                var username = username_in;
 
116
                _user_proxy = null;
 
117
 
 
118
                // Look up currently selected greeter user, if asked
 
119
                if (username == null) {
 
120
                        try {
 
121
                                username = yield _greeter_proxy.get_active_entry ();
 
122
                                if (username == "" || username == null)
 
123
                                        return;
 
124
                        } catch (GLib.Error e) {
 
125
                                warning ("unable to find Accounts path for user %s: %s", username == null ? "null" : username, e.message);
 
126
                                return;
 
127
                        }
 
128
                }
 
129
 
 
130
                // Get master AccountsService object
 
131
                DBusProxy accounts_proxy;
 
132
                try {
 
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);
 
136
                        return;
 
137
                }
 
138
 
 
139
                // Find user's AccountsService object
 
140
                try {
 
141
                        var user_path_variant = yield accounts_proxy.call ("FindUserByName", new Variant ("(s)", username), DBusCallFlags.NONE, -1);
 
142
                        string user_path;
 
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");
 
146
                        } else {
 
147
                                warning ("Unable to find user name after calling FindUserByName. Expected type: %s and obtained %s", "(o)", user_path_variant.get_type_string () );
 
148
                                return;
 
149
                        }
 
150
                } catch (GLib.Error e) {
 
151
                        warning ("unable to find Accounts path for user %s: %s", username, e.message);
 
152
                        return;
 
153
                }
 
154
 
 
155
                // Get current values and listen for changes
 
156
                _user_proxy.g_properties_changed.connect (accountsservice_props_changed_cb);
 
157
                try {
 
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)) {
 
160
                                Variant props;
 
161
                                props_variant.get ("(@a{sv})", out props);
 
162
                                accountsservice_props_changed_cb(_user_proxy, props, null);
 
163
                        } else {
 
164
                                warning ("Unable to get accounts service properties after calling GetAll. Expected type: %s and obtained %s", "(@a{sv})", props_variant.get_type_string () );
 
165
                                return;
 
166
                        }
 
167
                } catch (GLib.Error e) {
 
168
                        debug("Unable to get properties for user %s at first try: %s", username, e.message);
 
169
                }
 
170
        }       
 
171
 
 
172
        private void greeter_user_changed (string username)
 
173
        {
 
174
                setup_user_proxy.begin (username);
 
175
        }
 
176
 
 
177
        private async void setup_accountsservice ()
 
178
        {
 
179
                if (Environment.get_variable ("XDG_SESSION_CLASS") == "greeter") {
 
180
                        try {
 
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);
 
184
                                return;
 
185
                        }
 
186
                        _greeter_proxy.entry_selected.connect (greeter_user_changed);
 
187
                        yield setup_user_proxy ();
 
188
                } else {
 
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);
 
193
                        }
 
194
                }
 
195
        }
 
196
 
 
197
        private async void sync_last_running_player_to_accountsservice (string last_running_player)
 
198
        {
 
199
                if (_user_proxy == null)
 
200
                        return;
 
201
 
 
202
                try {
 
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);
 
206
                }
 
207
                _last_running_player = last_running_player;
 
208
        }
 
209
 
 
210
        private async void sync_volume_to_accountsservice (double volume)
 
211
        {
 
212
                if (_user_proxy == null)
 
213
                        return;
 
214
 
 
215
                try {
 
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);
 
219
                }
 
220
        }
 
221
 
 
222
        private async void sync_mute_to_accountsservice (bool mute)
 
223
        {
 
224
                if (_user_proxy == null)
 
225
                        return;
 
226
 
 
227
                try {
 
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);
 
231
                }
 
232
        }
 
233
}
 
234
 
 
235