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