From a07a88311d4efbaa8682354c199910a8d96eb0a7 Mon Sep 17 00:00:00 2001 From: Conor Curran Date: Thu, 24 Jun 2010 01:25:13 +0100 Subject: attempting to subscribe to client desktop file path notifications --- src/Makefile.am | 3 +- src/familiar-players-db.vala | 152 +++++++++++++++++++++++++++++++++++++++++++ src/music-player-bridge.vala | 45 ++++++++----- src/sound-service.c | 4 +- 4 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 src/familiar-players-db.vala (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index a2a54e8..685ef71 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,7 +57,8 @@ music_bridge_VALASOURCES = \ player-controller.vala \ mpris-controller-v2.vala \ mpris-controller.vala \ - player-item.vala + player-item.vala \ + familiar-players-db.vala music_bridge_VALAFLAGS = \ --ccode \ diff --git a/src/familiar-players-db.vala b/src/familiar-players-db.vala new file mode 100644 index 0000000..5ed209b --- /dev/null +++ b/src/familiar-players-db.vala @@ -0,0 +1,152 @@ +/* +Copyright 2010 Canonical Ltd. + +Authors: + Conor Curran + +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 . +*/ + +using Gee; +using GLib.Path; +using GLib.DirUtils; +using GLib.FileUtils; +using GLib.Timeout; +using GLib.Environment; + +// TODO: more refactoring needed here +public class FamiliarPlayersDB : GLib.Object +{ + private const string GROUP_NAME = "Seen Database"; + private const string KEY_NAME = "DesktopFiles"; + private HashMap players_DB; + private string file_name; + private string dir_name; + private KeyFile key_file; + private uint write_id; + + public FamiliarPlayersDB() + { + this.write_id = 0; + this.players_DB = new HashMap(); + this.dir_name = build_filename(get_user_cache_dir(), "indicators", "sound"); + this.file_name = build_filename(this.dir_name, "familiar-players-db.keyfile"); + if(create_key_file() && check_for_keys() && load_data_from_key_file()){ + debug("keyfiles in place and ready for action"); + } + else{ + this.key_file = null; + warning("FamiliarPlayersDB:: problems loading key file - can't go any further"); + } + } + + private bool create_key_file(){ + if (test(this.file_name, GLib.FileTest.EXISTS)) { + this.key_file = new KeyFile(); + try{ + if (this.key_file.load_from_file(this.file_name, KeyFileFlags.NONE) == true) { + return true; + } + } + catch(FileError e){ + warning("FamiliarPlayersDB - error trying to load KeyFile"); + } + } + return false; + } + + private bool check_for_keys(){ + try{ + if(this.key_file.has_key(GROUP_NAME, KEY_NAME) == true){ + return true; + } + } + catch(KeyFileError e){ + return false; + } + warning("Seen DB '%s' does not have key '%s' in group '%s'", this.file_name, KEY_NAME, GROUP_NAME); + return false; + } + + private bool load_data_from_key_file() + { + try{ + string[] desktops = this.key_file.get_string_list(GROUP_NAME, + KEY_NAME); + + int i = 0; + while (desktops[i] != null) { + this.players_DB.set(desktops[i], true); + } + return true; + } + catch(FileError error){ + warning("Error loading the Desktop string list"); + return false; + } + } + + private bool write_db() + { + KeyFile keyfile = new KeyFile(); + string[] desktops = {}; + //Set keys = this.players_DB.keys; + foreach(string key in this.players_DB.keys){ + desktops += key; + } + keyfile.set_string_list(GROUP_NAME, + KEY_NAME, + desktops); + size_t data_length; + string data = null; + try{ + data = keyfile.to_data(out data_length); + } + catch(Error e){ + warning("Problems dumping keyfile to a string"); + return false; + } + + if(create_with_parents(this.dir_name, 0700) != 0){ + warning("Unable to make directory: %s", this.dir_name); + return false; + } + + try{ + if(set_contents(this.file_name, data, (ssize_t)data_length) == false){ + warning("Unable to write out file '%s'", this.file_name); + } + } + catch(FileError err){ + warning("Unable to write out file '%s'", this.file_name); + } + return true; + } + + public void db_add(string desktop) + { + if(already_familiar(desktop) == false){ + if(this.write_id != 0){ + Source.remove(this.write_id); + this.write_id = 0; + } + this.write_id = Timeout.add_seconds(60, write_db); + this.players_DB.set(desktop.dup(), true); + } + } + + public bool already_familiar(string desktop) + { + return this.players_DB.get(desktop); + } +} \ No newline at end of file diff --git a/src/music-player-bridge.vala b/src/music-player-bridge.vala index b03ecbd..fb63866 100644 --- a/src/music-player-bridge.vala +++ b/src/music-player-bridge.vala @@ -1,5 +1,4 @@ /* -This service primarily controls PulseAudio and is driven by the sound indicator menu on the panel. Copyright 2010 Canonical Ltd. Authors: @@ -28,9 +27,11 @@ public class MusicPlayerBridge : GLib.Object private Listener listener; private Dbusmenu.Menuitem root_menu; private HashMap registered_clients; + private FamiliarPlayersDB playersDB; public MusicPlayerBridge() { + playersDB = new FamiliarPlayersDB(); registered_clients = new HashMap (); listener = Listener.ref_default(); listener.indicator_added.connect(on_indicator_added); @@ -46,26 +47,24 @@ public class MusicPlayerBridge : GLib.Object root_menu = menu; } - public void on_indicator_added(Indicate.ListenerServer object, Indicate.ListenerIndicator p0) - { - debug("MusicPlayerBridge-> on_indicator_added"); - } - - public void on_indicator_removed(Indicate.ListenerServer object, Indicate.ListenerIndicator p0) - { - debug("MusicPlayerBridge -> on_indicator_removed"); - } - - public void on_indicator_modified(Indicate.ListenerServer object, Indicate.ListenerIndicator p0, string s) - { - debug("MusicPlayerBridge -> indicator_modified with vale %s", s ); - } +//static void +//desktop_cb (IndicateListener * listener, IndicateListenerServer * server, gchar * value, gpointer data) - public void on_server_added(Indicate.ListenerServer object, string type) + public void desktop_info_callback(Indicate.Listener listener, + Indicate.ListenerServer server, + owned string value, void* data) + { + + } + + public void on_server_added(Indicate.ListenerServer object, string type) { debug("MusicPlayerBridge -> on_server_added with value %s", type); if(server_is_not_of_interest(type)) return; string client_name = type.split(".")[1]; + listener_get_server_property_cb cb = (listener_get_server_property_cb)desktop_info_callback; + this.listener.server_get_desktop(object, cb); + if (root_menu != null && client_name != null){ PlayerController ctrl = new PlayerController(root_menu, client_name, true); registered_clients.set(client_name, ctrl); @@ -98,6 +97,20 @@ public class MusicPlayerBridge : GLib.Object { debug("MusicPlayerBridge-> on_server_count_changed with value %u", i); } + public void on_indicator_added(Indicate.ListenerServer object, Indicate.ListenerIndicator p0) + { + debug("MusicPlayerBridge-> on_indicator_added"); + } + + public void on_indicator_removed(Indicate.ListenerServer object, Indicate.ListenerIndicator p0) + { + debug("MusicPlayerBridge -> on_indicator_removed"); + } + + public void on_indicator_modified(Indicate.ListenerServer object, Indicate.ListenerIndicator p0, string s) + { + debug("MusicPlayerBridge -> indicator_modified with vale %s", s ); + } } diff --git a/src/sound-service.c b/src/sound-service.c index bcdac07..ea04e4b 100644 --- a/src/sound-service.c +++ b/src/sound-service.c @@ -45,8 +45,8 @@ service_shutdown (IndicatorService *service, gpointer user_data) if (mainloop != NULL) { g_debug("Service shutdown !"); // TODO: uncomment for release !! - close_pulse_activites(); - g_main_loop_quit(mainloop); + //close_pulse_activites(); + //g_main_loop_quit(mainloop); } return; } -- cgit v1.2.3 From 98c55dde34ae7ed062ae22712b998ef4787b14ee Mon Sep 17 00:00:00 2001 From: Conor Curran Date: Thu, 24 Jun 2010 14:09:49 +0100 Subject: record of played application being stored correctly in keyfile --- src/Makefile.am | 2 +- src/familiar-players-db.vala | 9 +++++++-- src/music-player-bridge.vala | 40 ++++++++++++++++++++++------------------ src/player-item.vala | 19 +++++++++++++++++-- 4 files changed, 47 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 685ef71..e64d13d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,8 +64,8 @@ music_bridge_VALAFLAGS = \ --ccode \ -H music-player-bridge.h -d . \ --library music-bridge \ + --vapidir=$(top_srcdir)/vapi/ \ --vapidir=./ \ - --vapidir=../vapi/ \ --thread \ --pkg gee-1.0 \ --pkg Indicate-0.2 \ diff --git a/src/familiar-players-db.vala b/src/familiar-players-db.vala index 5ed209b..3d7da1f 100644 --- a/src/familiar-players-db.vala +++ b/src/familiar-players-db.vala @@ -100,7 +100,6 @@ public class FamiliarPlayersDB : GLib.Object { KeyFile keyfile = new KeyFile(); string[] desktops = {}; - //Set keys = this.players_DB.keys; foreach(string key in this.players_DB.keys){ desktops += key; } @@ -133,7 +132,7 @@ public class FamiliarPlayersDB : GLib.Object return true; } - public void db_add(string desktop) + public void insert(string desktop) { if(already_familiar(desktop) == false){ if(this.write_id != 0){ @@ -149,4 +148,10 @@ public class FamiliarPlayersDB : GLib.Object { return this.players_DB.get(desktop); } + + public Gee.Set records() + { + return this.players_DB.keys; + } + } \ No newline at end of file diff --git a/src/music-player-bridge.vala b/src/music-player-bridge.vala index fb63866..02333a6 100644 --- a/src/music-player-bridge.vala +++ b/src/music-player-bridge.vala @@ -32,6 +32,7 @@ public class MusicPlayerBridge : GLib.Object public MusicPlayerBridge() { playersDB = new FamiliarPlayersDB(); + try_to_add_inactive_familiar_clients(); registered_clients = new HashMap (); listener = Listener.ref_default(); listener.indicator_added.connect(on_indicator_added); @@ -42,30 +43,20 @@ public class MusicPlayerBridge : GLib.Object listener.server_count_changed.connect(on_server_count_changed); } - public void set_root_menu_item(Dbusmenu.Menuitem menu) - { - root_menu = menu; - } - -//static void -//desktop_cb (IndicateListener * listener, IndicateListenerServer * server, gchar * value, gpointer data) - - public void desktop_info_callback(Indicate.Listener listener, - Indicate.ListenerServer server, - owned string value, void* data) - { - + private void try_to_add_inactive_familiar_clients(){ + //foreach(string app in this.playersDB.records()){ + // + //} } public void on_server_added(Indicate.ListenerServer object, string type) { debug("MusicPlayerBridge -> on_server_added with value %s", type); if(server_is_not_of_interest(type)) return; - string client_name = type.split(".")[1]; - listener_get_server_property_cb cb = (listener_get_server_property_cb)desktop_info_callback; - this.listener.server_get_desktop(object, cb); - + string client_name = type.split(".")[1]; if (root_menu != null && client_name != null){ + listener_get_server_property_cb cb = (listener_get_server_property_cb)desktop_info_callback; + this.listener.server_get_desktop(object, cb, this); PlayerController ctrl = new PlayerController(root_menu, client_name, true); registered_clients.set(client_name, ctrl); debug("client of name %s has successfully registered with us", client_name); @@ -93,7 +84,20 @@ public class MusicPlayerBridge : GLib.Object return false; } - public void on_server_count_changed(Indicate.ListenerServer object, uint i) + private void desktop_info_callback(Indicate.ListenerServer server, + owned string path, void* data) + { + debug("we got a desktop file path hopefully: %s", path); + MusicPlayerBridge bridge = data as MusicPlayerBridge; + bridge.playersDB.insert(path); + } + + public void set_root_menu_item(Dbusmenu.Menuitem menu) + { + root_menu = menu; + } + + public void on_server_count_changed(Indicate.ListenerServer object, uint i) { debug("MusicPlayerBridge-> on_server_count_changed with value %u", i); } diff --git a/src/player-item.vala b/src/player-item.vala index 09b0e6b..d1cf08c 100644 --- a/src/player-item.vala +++ b/src/player-item.vala @@ -28,17 +28,32 @@ public class PlayerItem : Dbusmenu.Menuitem { } + private static bool ensure_valid_updates(HashTable data, HashSet attributes) + { + if(data == null){ + return false; + } + if(data.size() < attributes.size){ + warning("update hash was too small for the target"); + return false; + } + return true; + } + public void update(HashTable data, HashSet attributes) { debug("PlayerItem::update()"); + if(ensure_valid_updates(data, attributes) == false){ + debug("PlayerItem::Update -> update hash is not what we were expecting"); + return; + } foreach(string property in attributes){ string[] input_keys = property.split("-"); string search_key = input_keys[input_keys.length-1 : input_keys.length][0]; debug("search key = %s", search_key); - Value v = data.lookup(search_key); - if (v.holds (typeof (string))){ + debug("with value : %s", v.get_string()); this.property_set(property, this.sanitize_string(v.get_string())); } else if (v.holds (typeof (int))){ -- cgit v1.2.3 From 81d9af3fc3f1d3784e623ede592e6d91032353ad Mon Sep 17 00:00:00 2001 From: Conor Curran Date: Thu, 24 Jun 2010 17:23:39 +0100 Subject: moving towards offline application launching --- src/Makefile.am | 3 ++- src/familiar-players-db.vala | 9 +++------ src/metadata-menu-item.vala | 11 +++++++++++ src/music-player-bridge.vala | 35 +++++++++++++++++++++++++++++------ src/player-controller.vala | 14 ++++++-------- src/player-item.vala | 31 ++++++++++++++++++------------- src/sound-service.c | 4 ++-- src/transport-menu-item.vala | 5 +++++ 8 files changed, 76 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index e64d13d..8805ff9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,7 +71,8 @@ music_bridge_VALAFLAGS = \ --pkg Indicate-0.2 \ --pkg Dbusmenu-Glib-0.2 \ --pkg common-defs \ - --pkg dbus-glib-1 + --pkg dbus-glib-1 \ + --pkg gio-unix-2.0 $(MAINTAINER_VALAFLAGS) diff --git a/src/familiar-players-db.vala b/src/familiar-players-db.vala index 3d7da1f..88bc01f 100644 --- a/src/familiar-players-db.vala +++ b/src/familiar-players-db.vala @@ -78,15 +78,12 @@ public class FamiliarPlayersDB : GLib.Object return false; } - private bool load_data_from_key_file() - { + private bool load_data_from_key_file(){ try{ string[] desktops = this.key_file.get_string_list(GROUP_NAME, KEY_NAME); - - int i = 0; - while (desktops[i] != null) { - this.players_DB.set(desktops[i], true); + foreach(string s in desktops){ + this.players_DB.set(s, true); } return true; } diff --git a/src/metadata-menu-item.vala b/src/metadata-menu-item.vala index f62cd46..541fbf4 100644 --- a/src/metadata-menu-item.vala +++ b/src/metadata-menu-item.vala @@ -37,5 +37,16 @@ public class MetadataMenuitem : PlayerItem attrs.add(MENUITEM_ARTURL); return attrs; } + + public override void check_layout(){ + this.property_set_bool(MENUITEM_PROP_VISIBLE, this.populated()); + debug("check layout for the metadata = %s", this.populated().to_string()); + } + + public bool populated() + { + return (this.property_get(MENUITEM_TEXT_TITLE) != null && + this.property_get(MENUITEM_TEXT_TITLE) != ""); + } } \ No newline at end of file diff --git a/src/music-player-bridge.vala b/src/music-player-bridge.vala index 02333a6..6fc9032 100644 --- a/src/music-player-bridge.vala +++ b/src/music-player-bridge.vala @@ -20,6 +20,7 @@ with this program. If not, see . using Indicate; using Dbusmenu; using Gee; +using GLib; public class MusicPlayerBridge : GLib.Object { @@ -32,7 +33,6 @@ public class MusicPlayerBridge : GLib.Object public MusicPlayerBridge() { playersDB = new FamiliarPlayersDB(); - try_to_add_inactive_familiar_clients(); registered_clients = new HashMap (); listener = Listener.ref_default(); listener.indicator_added.connect(on_indicator_added); @@ -42,11 +42,33 @@ public class MusicPlayerBridge : GLib.Object listener.server_removed.connect(on_server_removed); listener.server_count_changed.connect(on_server_count_changed); } - + // Alpha 2 not in use ... yet. private void try_to_add_inactive_familiar_clients(){ - //foreach(string app in this.playersDB.records()){ - // - //} + // for now just use one of the entries. + int count = 0; + foreach(string app in this.playersDB.records()){ + if(count == 0){ + debug("we have found %s", app); + string[] bits = app.split("/"); + + try{ + string app_name = bits[bits.length -1].split(".")[0]; + debug("we have found %s", app_name); + PlayerController ctrl = new PlayerController(this.root_menu, + app_name, + false); + this.registered_clients.set(app_name, ctrl); + DesktopAppInfo info = new DesktopAppInfo.from_filename(app_name); + string desc = info.get_display_name(); + debug("description from app %s", desc); + count += 1; + } + catch(Error er){ + warning("desktop path in cache is not formatted as we have anticipated"); + } + } + break; + } } public void on_server_added(Indicate.ListenerServer object, string type) @@ -94,7 +116,8 @@ public class MusicPlayerBridge : GLib.Object public void set_root_menu_item(Dbusmenu.Menuitem menu) { - root_menu = menu; + this.root_menu = menu; + //try_to_add_inactive_familiar_clients(); } public void on_server_count_changed(Indicate.ListenerServer object, uint i) diff --git a/src/player-controller.vala b/src/player-controller.vala index 862bb29..0d8dc01 100644 --- a/src/player-controller.vala +++ b/src/player-controller.vala @@ -31,7 +31,8 @@ public class PlayerController : GLib.Object private bool is_active; public ArrayList custom_items; private MprisController mpris_adaptor; - + private string desktop_path; + public PlayerController(Dbusmenu.Menuitem root, string client_name, bool active) { this.root_menu = root; @@ -48,6 +49,10 @@ public class PlayerController : GLib.Object 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(); } public void vanish() @@ -80,13 +85,6 @@ public class PlayerController : GLib.Object return true; } - //public void update_playing_info(HashMap data) - //{ - // debug("PlayerController - update_playing_info"); - // MetadataMenuitem item = this.custom_items[METADATA] as MetadataMenuitem; - // item.update(data, MetadataMenuitem.attributes_format()); - //} - private static string format_client_name(string client_name) { string formatted = client_name; diff --git a/src/player-item.vala b/src/player-item.vala index d1cf08c..7fcf912 100644 --- a/src/player-item.vala +++ b/src/player-item.vala @@ -27,24 +27,12 @@ public class PlayerItem : Dbusmenu.Menuitem public PlayerItem() { } - - private static bool ensure_valid_updates(HashTable data, HashSet attributes) - { - if(data == null){ - return false; - } - if(data.size() < attributes.size){ - warning("update hash was too small for the target"); - return false; - } - return true; - } public void update(HashTable data, HashSet attributes) { debug("PlayerItem::update()"); if(ensure_valid_updates(data, attributes) == false){ - debug("PlayerItem::Update -> update hash is not what we were expecting"); + debug("PlayerItem::Update -> The hashtable update does not contain what we were expecting - just leave it!"); return; } foreach(string property in attributes){ @@ -64,6 +52,8 @@ public class PlayerItem : Dbusmenu.Menuitem this.property_set_bool(property, v.get_boolean()); } } + // TODO: not working + //this.check_layout(); } public void set_adaptor(MprisController adaptor) @@ -71,6 +61,17 @@ public class PlayerItem : Dbusmenu.Menuitem this.mpris_adaptor = adaptor; } + private static bool ensure_valid_updates(HashTable data, HashSet attributes) + { + if(data == null){ + return false; + } + if(data.size() < attributes.size){ + warning("update hash was too small for the target"); + return false; + } + return true; + } public static string sanitize_string(string st) { @@ -100,5 +101,9 @@ public class PlayerItem : Dbusmenu.Menuitem separator.property_set(MENUITEM_PROP_TYPE, CLIENT_TYPES_SEPARATOR); return separator; } + + public virtual void check_layout(){ + warning("this should not be hit"); + } } diff --git a/src/sound-service.c b/src/sound-service.c index ea04e4b..bcdac07 100644 --- a/src/sound-service.c +++ b/src/sound-service.c @@ -45,8 +45,8 @@ service_shutdown (IndicatorService *service, gpointer user_data) if (mainloop != NULL) { g_debug("Service shutdown !"); // TODO: uncomment for release !! - //close_pulse_activites(); - //g_main_loop_quit(mainloop); + close_pulse_activites(); + g_main_loop_quit(mainloop); } return; } diff --git a/src/transport-menu-item.vala b/src/transport-menu-item.vala index 39a0cab..264e153 100644 --- a/src/transport-menu-item.vala +++ b/src/transport-menu-item.vala @@ -41,6 +41,11 @@ public class TransportMenuitem : PlayerItem this.mpris_adaptor.toggle_playback(input_value.get_boolean()); } + public override void check_layout(){ + // nothing to be done for this item - always active + } + + public static HashSet attributes_format() { HashSet attrs = new HashSet(); -- cgit v1.2.3