1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
This service primarily controls PulseAudio and is driven by the sound indicator menu on the panel.
Copyright 2010 Canonical Ltd.
Authors:
Conor Curran <conor.curran@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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 <http://www.gnu.org/licenses/>.
*/
using Dbusmenu;
using Gee;
public class PlayerController : GLib.Object
{
public const int WIDGET_QUANTITY = 6;
public static enum widget_order{
SEPARATOR,
TITLE,
METADATA,
SCRUB,
TRANSPORT,
PLAYLIST
}
public enum state{
OFFLINE,
INSTANTIATING,
READY,
CONNECTED,
DISCONNECTED
}
public int current_state = state.OFFLINE;
private Dbusmenu.Menuitem root_menu;
public string name { get; set;}
public ArrayList<PlayerItem> custom_items;
public Mpris2Controller mpris2_adaptor;
public MprisController mpris_adaptor;
public bool mpris2;
public AppInfo? app_info { get; set;}
public int menu_offset { get; set;}
public PlayerController(Dbusmenu.Menuitem root, string client_name, int offset, state initial_state)
{
this.mpris2 = false;
this.root_menu = root;
this.name = format_client_name(client_name.strip());
this.custom_items = new ArrayList<PlayerItem>();
this.current_state = initial_state;
this.menu_offset = offset;
construct_widgets();
establish_mpris_connection();
this.update_layout();
}
public void update_state(state new_state)
{
debug("update_state - player controller %s : new state %i", this.name, new_state);
this.current_state = new_state;
this.update_layout();
}
public void activate()
{
this.establish_mpris_connection();
//this.custom_items[widget_order.METADATA].property_set_bool(MENUITEM_PROP_VISIBLE, true);
}
/*
instantiate()
The user should be able to start the app from the transport bar when in an offline state
There is a need to wait before the application is on DBus before attempting to access its mpris address
Hence only when the it has registered with us via libindicate do we attempt to kick off mpris communication
*/
public void instantiate()
{
debug("instantiate in player controller for %s", this.name);
try{
this.app_info.launch(null, null);
this.update_state(state.INSTANTIATING);
}
catch(GLib.Error error){
warning("Failed to launch app %s with error message: %s", this.name, error.message);
}
}
private void establish_mpris_connection()
{
if(this.current_state != state.READY){
debug("establish_mpris_connection - Not ready to connect");
return;
}
if(this.name == "Vlc"){
debug("establishing a vlc mpris controller");
this.mpris2_adaptor = new Mpris2Controller(this);
this.mpris2 = true;
}
else{
this.mpris_adaptor = new MprisController(this);
}
this.determine_state();
}
public void vanish()
{
foreach(Dbusmenu.Menuitem item in this.custom_items){
root_menu.child_delete(item);
}
}
public void hibernate()
{
update_state(PlayerController.state.OFFLINE);
this.custom_items[widget_order.TRANSPORT].reset(TransportMenuitem.attributes_format());
this.custom_items[widget_order.METADATA].reset(MetadataMenuitem.attributes_format());
this.custom_items[widget_order.SCRUB].reset(ScrubMenuitem.attributes_format());
}
public void update_layout()
{
if(this.current_state != state.CONNECTED){
this.custom_items[widget_order.TRANSPORT].property_set_bool(MENUITEM_PROP_VISIBLE,
false);
this.custom_items[widget_order.SCRUB].property_set_bool(MENUITEM_PROP_VISIBLE,
false);
this.custom_items[widget_order.METADATA].property_set_bool(MENUITEM_PROP_VISIBLE,
false);
this.custom_items[widget_order.PLAYLIST].property_set_bool(MENUITEM_PROP_VISIBLE,
false);
return;
}
debug("update layout - metadata %s", this.custom_items[widget_order.METADATA].populated(MetadataMenuitem.attributes_format()).to_string());
this.custom_items[widget_order.METADATA].property_set_bool(MENUITEM_PROP_VISIBLE,
this.custom_items[widget_order.METADATA].populated(MetadataMenuitem.attributes_format()));
//debug("metadata id %i", this.custom_items[widget_order.METADATA].id);
debug("update layout - scrub %s", this.custom_items[widget_order.SCRUB].populated(ScrubMenuitem.attributes_format()).to_string());
this.custom_items[widget_order.SCRUB].property_set_bool(MENUITEM_PROP_VISIBLE,
this.custom_items[widget_order.SCRUB].populated(ScrubMenuitem.attributes_format()));
this.custom_items[widget_order.TRANSPORT].property_set_bool(MENUITEM_PROP_VISIBLE,
true);
this.custom_items[widget_order.PLAYLIST].property_set_bool(MENUITEM_PROP_VISIBLE,
true);
}
private void construct_widgets()
{
// Separator item
this.custom_items.add(new PlayerItem(CLIENT_TYPES_SEPARATOR));
// Title item
TitleMenuitem title_menu_item = new TitleMenuitem(this);
this.custom_items.add(title_menu_item);
// Metadata item
MetadataMenuitem metadata_item = new MetadataMenuitem();
this.custom_items.add(metadata_item);
// Scrub item
ScrubMenuitem scrub_item = new ScrubMenuitem(this);
this.custom_items.add(scrub_item);
// Transport item
TransportMenuitem transport_item = new TransportMenuitem(this);
this.custom_items.add(transport_item);
this.custom_items.add(create_playlist());
foreach(PlayerItem item in this.custom_items){
root_menu.child_add_position(item, this.menu_offset + this.custom_items.index_of(item));
}
}
private PlayerItem create_playlist()
{
PlayerItem playlist_root = new PlayerItem(CLIENT_TYPES_DEFAULT);
playlist_root.property_set(MENUITEM_PROP_LABEL, "Choose Playlist");
PlayerItem subentry_1 = new PlayerItem(CLIENT_TYPES_DEFAULT);
subentry_1.property_set(MENUITEM_PROP_LABEL, "Raster-noton selection");
PlayerItem subentry_2 = new PlayerItem(CLIENT_TYPES_DEFAULT);
subentry_2.property_set(MENUITEM_PROP_LABEL, "Rune Grammofon selection");
PlayerItem subentry_3 = new PlayerItem(CLIENT_TYPES_DEFAULT);
subentry_3.property_set(MENUITEM_PROP_LABEL, "Kranky selection");
playlist_root.child_append(subentry_1);
playlist_root.child_append(subentry_2);
playlist_root.child_append(subentry_3);
return playlist_root;
}
private static string format_client_name(string client_name)
{
string formatted = client_name;
if(formatted.len() > 1){
formatted = client_name.up(1).concat(client_name.slice(1, client_name.len()));
debug("PlayerController->format_client_name - : %s", formatted);
}
return formatted;
}
// Temporarily we will need to handle to different mpris implemenations
// Do it for now - a couple of weeks should see this messy carry on out of
// the codebase.
public void set_track_position(double pos)
{
if(this.mpris2 == true){
this.mpris2_adaptor.set_position(pos);
}
else{
this.mpris_adaptor.set_position(pos);
}
}
public void transport_update(TransportMenuitem.action update)
{
if(this.mpris2 == true){
this.mpris2_adaptor.transport_event(update);
}
else{
this.mpris_adaptor.transport_event(update);
}
}
public void determine_state()
{
if(this.mpris2 == true){
if(this.mpris2_adaptor.connected() == true){
debug("yup I'm connected");
this.update_state(state.CONNECTED);
}
else{
this.update_state(state.DISCONNECTED);
}
}
else{
if(this.mpris_adaptor.connected() == true){
debug("yup I'm connected");
this.update_state(state.CONNECTED);
}
else{
this.update_state(state.DISCONNECTED);
}
}
}
}
|