4 * Authored by Michael 'Mickey' Lauer <mlauer@vanille-media.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //===========================================================================
26 //===========================================================================
27 [DBus (name
= "org.freesmartphone.DBus")]
28 public abstract interface OrgFreesmartphoneDBus
: Object
30 public abstract string[]?
ListBusNames() throws DBus
.Error
;
31 public abstract DBus
.ObjectPath
[] ListObjectPaths( string? busname
) throws DBus
.Error
;
32 public abstract DBus
.ObjectPath
[] ListObjectsByInterface( string busname
, string iface
) throws DBus
.Error
;
33 public abstract string[] ListInterfacesByObject( string busname
, DBus
.ObjectPath objname
) throws DBus
.Error
;
34 public abstract DBus
.ObjectPath
[] ListChildrenByObject( string busname
, DBus
.ObjectPath objname
, bool recursive
) throws DBus
.Error
;
37 //===========================================================================
38 string[] stringListToArray( List
<string>? theList
)
40 var res
= new
string[theList
.length()];
42 foreach ( string el
in theList
)
51 //===========================================================================
52 DBus
.ObjectPath
[] objectPathListToArray( List
<DBus
.ObjectPath
>? theList
)
54 var res
= new DBus
.ObjectPath
[theList
.length()];
56 foreach ( DBus
.ObjectPath el
in theList
)
64 //===========================================================================
65 public class Introspection
: Object
67 private string[] _xmldata
;
69 public List
<string> interfaces
;
70 public List
<string> nodes
;
72 public Introspection( string xmldata
)
74 debug( "introspection object created" );
75 _xmldata
= xmldata
.split( "\n" );
77 foreach ( string line
in _xmldata
)
79 //debug( "dealing with line '%s'", line );
82 res
= line
.scanf( " <node name=\"%a[a-zA-Z0-9_]\"/>", out name
);
86 message( "object has node '%s'", name
);
88 res
= line
.scanf( " <interface name=\"%a[a-zA-Z0-9_.]\">", out name
);
91 message( "object supports interface '%s'", name
);
92 interfaces
.append( name
);
98 //===========================================================================
99 public class Server
: OrgFreesmartphoneDBus
, Object
101 DBus
.Connection conn
;
102 dynamic DBus
.Object dbus
;
108 debug( "server object created" );
109 conn
= DBus
.Bus
.get( DBus
.BusType
.SYSTEM
);
110 dbus
= conn
.get_object( DBUS_BUS_NAME
, DBUS_OBJ_PATH
, DBUS_INTERFACE
);
114 error( "dbus error: %s", e
.message
);
118 public string[]?
ListBusNames() throws DBus
.Error
120 string[] names
= null;
123 names
= dbus
.ListNames();
127 error( "dbus error: %s", e
.message
);
137 public DBus
.ObjectPath
[] ListObjectPaths( string? busname
) throws DBus
.Error
140 // Check whether the given busname is present on the bus
142 var paths
= new List
<DBus
.ObjectPath
>();
143 var existing_busnames
= this
.ListBusNames();
145 foreach ( string name
in existing_busnames
)
147 if ( busname
== name
)
155 message( "requested busname '%s' not found.", busname
);
156 // FIXME return a dbus error?
157 return objectPathListToArray( paths
);
159 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)"/" );
160 return objectPathListToArray( paths
);
163 private void listObjectPaths( ref List
<DBus
.ObjectPath
> paths
, string busname
, DBus
.ObjectPath objname
) throws DBus
.Error
165 debug( "listObjectPaths: %s, %s", busname
, objname
);
166 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
167 Introspection data
= new
Introspection( obj
.Introspect() );
168 if ( data
.interfaces
.length() > 1 ) // we don't count the introspection interface that is always present
169 paths
.append( objname
);
170 if ( data
.nodes
.length() > 0 )
171 foreach ( string node
in data
.nodes
)
173 if ( objname
== (DBus
.ObjectPath
)"/" )
174 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+node
) );
176 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+"/"+node
) );
180 public DBus
.ObjectPath
[] ListObjectsByInterface( string busname
, string iface
) throws DBus
.Error
183 // Check whether the given busname is present on the bus
185 var paths
= new List
<DBus
.ObjectPath
>();
186 var existing_busnames
= this
.ListBusNames();
188 foreach ( string name
in existing_busnames
)
190 if ( busname
== name
)
198 message( "requested busname '%s' not found.", busname
);
199 // FIXME return a dbus error
200 return objectPathListToArray( paths
);
202 listObjectsByInterface( ref paths
, busname
, (DBus
.ObjectPath
)"/", iface
);
203 return objectPathListToArray( paths
);
206 private void listObjectsByInterface( ref List
<DBus
.ObjectPath
> paths
, string busname
, DBus
.ObjectPath objname
, string iface
) throws DBus
.Error
208 debug( "listObjectsByInterface: %s, %s, %s", busname
, objname
, iface
);
209 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
210 Introspection data
= new
Introspection( obj
.Introspect() );
211 if ( data
.interfaces
.length() > 1 ) // we don't count the introspection interface that is always present
212 foreach ( string ifacename
in data
.interfaces
)
214 if ( ifacename
== iface
)
215 paths
.append( objname
);
217 if ( data
.nodes
.length() > 0 )
218 foreach ( string node
in data
.nodes
)
220 if ( objname
== (DBus
.ObjectPath
)"/" )
221 listObjectsByInterface( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+node
), iface
);
223 listObjectsByInterface( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+"/"+node
), iface
);
227 public string[] ListInterfacesByObject( string busname
, DBus
.ObjectPath objname
) throws DBus
.Error
229 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
230 Introspection data
= new
Introspection( obj
.Introspect() );
231 return stringListToArray( data
.interfaces
);
233 public DBus
.ObjectPath
[] ListChildrenByObject ( string busname
, DBus
.ObjectPath objname
, bool recursive
)
235 var res
= listChildrenByObject( busname
, objname
, recursive
, true);
237 foreach( var p
in res
)
239 return objectPathListToArray( res
);
241 private List
<DBus
.ObjectPath
> listChildrenByObject( string busname
, DBus
.ObjectPath objname
, bool recursive
, bool first
= false )
243 debug( "%s %s", busname
, objname
);
244 var list
= new List
<DBus
.ObjectPath
>();
247 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
248 Introspection data
= new
Introspection( obj
.Introspect() );
249 if( data
.interfaces
.length() > 1 && !first
)
251 debug("new object: %s", objname
);
252 list
.prepend( objname
);
254 if( recursive
== true || first
== true )
257 foreach( string node
in data
.nodes
)
259 DBus
.ObjectPath fullpath
= null;
260 if( objname
== (DBus
.ObjectPath
)"/" )
261 fullpath
= (DBus
.ObjectPath
)((string)objname
+node
);
263 fullpath
= (DBus
.ObjectPath
)((string)objname
+"/"+node
);
265 list
.concat( listChildrenByObject( busname
, fullpath
, recursive
) );
271 critical( "ListChildrenByObject for %s: %s", objname
, e
.message
);