diff options
Diffstat (limited to 'src/player-controller.vala')
-rw-r--r-- | src/player-controller.vala | 109 |
1 files changed, 85 insertions, 24 deletions
diff --git a/src/player-controller.vala b/src/player-controller.vala index 0d8dc01..3fb4750 100644 --- a/src/player-controller.vala +++ b/src/player-controller.vala @@ -25,66 +25,127 @@ public class PlayerController : GLib.Object { public const int METADATA = 2; private const int TRANSPORT = 3; + + public enum state{ + OFFLINE, + INSTANTIATING, + READY, + CONNECTED, + DISCONNECTED + } + + public int current_state = state.OFFLINE; + private Dbusmenu.Menuitem root_menu; - private string name; - private bool is_active; + public string name { get; set;} public ArrayList<PlayerItem> custom_items; - private MprisController mpris_adaptor; - private string desktop_path; - - public PlayerController(Dbusmenu.Menuitem root, string client_name, bool active) + public MprisController mpris_adaptor; + public AppInfo? app_info { get; set;} + + public PlayerController(Dbusmenu.Menuitem root, string client_name, state initial_state) { this.root_menu = root; this.name = format_client_name(client_name.strip()); - this.is_active = active; this.custom_items = new ArrayList<PlayerItem>(); - self_construct(); - - // Temporary scenario to handle both v1 and v2 of MPRIS. + this.update_state(initial_state); + construct_widgets(); + establish_mpris_connection(); + update_layout(); + } + + public void update_state(state new_state) + { + debug("update_state : new state %i", new_state); + this.current_state = new_state; + } + + public void activate() + { + this.establish_mpris_connection(); + this.custom_items[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() + { + 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"){ this.mpris_adaptor = new MprisControllerV2(this.name, this); } else{ this.mpris_adaptor = new MprisController(this.name, this); - } - this.custom_items[TRANSPORT].set_adaptor(this.mpris_adaptor); - - // At start up if there is no metadata then hide the item. - // TODO: NOT working -> dbus menu bug ? - //((MetadataMenuitem)this.custom_items[METADATA]).check_layout(); + } + if(this.mpris_adaptor.connected() == true){ + this.update_state(state.CONNECTED); + } + else{ + this.update_state(state.DISCONNECTED); + } + this.update_layout(); } - + public void vanish() { foreach(Dbusmenu.Menuitem item in this.custom_items){ root_menu.child_delete(item); } } + + private void update_layout() + { + bool visibility = true; + if(this.current_state != state.CONNECTED){ + visibility = false; + } + debug("about the set the visibility on both the transport and metadata widget to %s", visibility.to_string()); + this.custom_items[TRANSPORT].property_set_bool(MENUITEM_PROP_VISIBLE, visibility); + this.custom_items[METADATA].property_set_bool(MENUITEM_PROP_VISIBLE, visibility); + } + - private bool self_construct() + private void construct_widgets() { // Separator item - this.custom_items.add(PlayerItem.new_separator_item()); + this.custom_items.add(new PlayerItem(CLIENT_TYPES_SEPARATOR)); // Title item - this.custom_items.add(PlayerItem.new_title_item(this.name)); + TitleMenuitem title_menu_item = new TitleMenuitem(this, this.name); + this.custom_items.add(title_menu_item); // Metadata item MetadataMenuitem metadata_item = new MetadataMenuitem(); this.custom_items.add(metadata_item); // Transport item - TransportMenuitem transport_item = new TransportMenuitem(); + TransportMenuitem transport_item = new TransportMenuitem(this); this.custom_items.add(transport_item); int offset = 2; foreach(PlayerItem item in this.custom_items){ root_menu.child_add_position(item, offset + this.custom_items.index_of(item)); } - return true; } - + private static string format_client_name(string client_name) { string formatted = client_name; @@ -94,5 +155,5 @@ public class PlayerController : GLib.Object } return formatted; } - + }
\ No newline at end of file |