aboutsummaryrefslogtreecommitdiff
path: root/src/mpris2-watcher.vala
blob: 9ef9b2f0fe3df058e0933175cdadc93bec611882 (plain)
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
/*
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 Xml;

[DBus (name = "org.freedesktop.DBus")]
public interface FreeDesktopObject: Object {
  public abstract async string[] list_names() throws IOError;
  public abstract signal void name_owner_changed ( string name,
                                                   string old_owner,
                                                   string new_owner );
}

[DBus (name = "org.freedesktop.DBus.Introspectable")]
public interface FreeDesktopIntrospectable: Object {
  public abstract string Introspect() throws IOError;
}

public errordomain XmlError {
    FILE_NOT_FOUND,
    XML_DOCUMENT_EMPTY
}

public class Mpris2Watcher : GLib.Object
{
  private const string FREEDESKTOP_SERVICE = "org.freedesktop.DBus";
  private const string FREEDESKTOP_OBJECT = "/org/freedesktop/DBus";
  public  const string MPRIS_PREFIX = "org.mpris.MediaPlayer2.";
  private const string MPRIS_MEDIA_PLAYER_PATH = "/org/mpris/MediaPlayer2";

  FreeDesktopObject fdesktop_obj;
  FreeDesktopIntrospectable introspectable;
  
  public signal void client_appeared ( string desktop_file_name,
                                       string dbus_name,
                                       bool use_playlists );
  public signal void client_disappeared ( string dbus_name );

  public Mpris2Watcher ()
  {
  }

  construct
  {  
    try {
      this.fdesktop_obj = Bus.get_proxy_sync ( BusType.SESSION,
                                               FREEDESKTOP_SERVICE,
                                               FREEDESKTOP_OBJECT,
                                               DBusProxyFlags.DO_NOT_LOAD_PROPERTIES );      
      this.fdesktop_obj.name_owner_changed.connect (this.name_changes_detected);      
      this.check_for_active_clients.begin();
    }
    catch ( IOError e ){
      warning( "Mpris2watcher could not set up a watch for mpris clients appearing on the bus: %s",
                e.message );
    }
  }

  // At startup check to see if there are clients up that we are interested in
  // More relevant for development and daemon's like mpd. 
  private async void check_for_active_clients()
  {
    string[] interfaces;
    try{
      interfaces = yield this.fdesktop_obj.list_names();
    }
    catch ( IOError e) {
      warning( "Mpris2watcher could fetch active interfaces at startup: %s",
                e.message );
      return;
    }
    foreach (var address in interfaces) {
      if (address.has_prefix (MPRIS_PREFIX)){
        MprisRoot? mpris2_root = this.create_mpris_root(address);
        if (mpris2_root == null) return;
        bool use_playlists = this.supports_playlists ( address );
        client_appeared (mpris2_root.DesktopEntry, address, use_playlists);
      }
    }
  }

  private void name_changes_detected ( FreeDesktopObject dbus_obj,
                                       string     name,
                                       string     previous_owner,
                                       string     current_owner ) 
  {
    MprisRoot? mpris2_root = this.create_mpris_root(name);                                         

    if (mpris2_root == null) return;

    if (previous_owner != "" && current_owner == "") {
      debug ("Client '%s' gone down", name);
      client_disappeared (name);
    }
    else if (previous_owner == "" && current_owner != "") {
      debug ("Client '%s' has appeared", name);
      bool use_playlists = this.supports_playlists ( name );
      client_appeared (mpris2_root.DesktopEntry, name, use_playlists);
    }
  }

  private MprisRoot? create_mpris_root ( string name ){
    MprisRoot mpris2_root = null;
    if ( name.has_prefix (MPRIS_PREFIX) ){
      try {
        mpris2_root = Bus.get_proxy_sync (  BusType.SESSION,
                                            name,
                                            MPRIS_MEDIA_PLAYER_PATH );
      }
      catch (IOError e){
        warning( "Mpris2watcher could not create a root interface: %s",
                  e.message );
      }
    }
    return mpris2_root;
  }

  private bool supports_playlists ( string name )
  {
    try {
      /* The dbusproxy flag parameter is needed to ensure Banshee does not 
       blow up. I suspect the issue is that if you
       try to instantiate a dbus object which does not have any properties 
       associated  with it, gdbus will attempt to fetch the properties (this is
       in the documentation) but the banshee mpris dbus object more than likely 
       causes a crash because it doesn't check for the presence of properties 
       before attempting to access them.
      */
      this.introspectable = Bus.get_proxy_sync (  BusType.SESSION,
                                                  name,
                                                  MPRIS_MEDIA_PLAYER_PATH, 
                                                  GLib.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
      
      var results = introspectable.Introspect();
      return this.parse_interfaces (results);
    }
    catch (IOError e){
      warning( "Could not create an introspectable object: %s",
                e.message );
    }
    return false;
  }

  private bool parse_interfaces( string interface_info )
  {
    //parse the document from path
    bool result = false;
    Xml.Doc* xml_doc = Parser.parse_doc (interface_info);
    if (xml_doc == null) {
      warning ("Mpris2Watcher - parse-interfaces - failed to instantiate xml doc");
      return false;
    }
    //get the root node. notice the dereferencing operator -> instead of .
    Xml.Node* root_node = xml_doc->get_root_element ();
    if (root_node == null) {
      //free the document manually before throwing because the garbage collector can't work on pointers
      delete xml_doc;
      warning ("Mpris2Watcher - the interface info xml is empty");
      return false;
    }

    //let's parse those nodes
    for (Xml.Node* iter = root_node->children; iter != null; iter = iter->next) {
      //spaces btw. tags are also nodes, discard them
      if (iter->type != ElementType.ELEMENT_NODE){
        continue;
      }
      Xml.Attr* attributes = iter->properties; //get the node's name    
      string interface_name = attributes->children->content;
      debug ( "this dbus object has interface %s ", interface_name );
      if ( interface_name == MPRIS_PREFIX.concat("Playlists")){
        result = true;
      }
    }
    delete xml_doc;
    return result;
  }  
}