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 //===========================================================================
27 //===========================================================================
28 string[] stringListToArray( List
<string>? theList
)
30 var res
= new
string[theList
.length()];
32 foreach ( string el
in theList
)
41 //===========================================================================
42 DBus
.ObjectPath
[] objectPathListToArray( List
<DBus
.ObjectPath
>? theList
)
44 var res
= new DBus
.ObjectPath
[theList
.length()];
46 foreach ( DBus
.ObjectPath el
in theList
)
54 //===========================================================================
55 public class Introspection
: Object
57 private string[] _xmldata
;
59 public List
<string> interfaces
;
60 public List
<string> nodes
;
62 public Introspection( string xmldata
)
64 debug( "introspection object created" );
65 _xmldata
= xmldata
.split( "\n" );
67 foreach ( string line
in _xmldata
)
69 //debug( "dealing with line '%s'", line );
72 res
= line
.scanf( " <node name=\"%a[a-zA-Z0-9_]\"/>", out name
);
76 message( "object has node '%s'", name
);
78 res
= line
.scanf( " <interface name=\"%a[a-zA-Z0-9_.]\">", out name
);
81 message( "object supports interface '%s'", name
);
82 interfaces
.append( name
);
88 //===========================================================================
89 [DBus (name
= "org.freesmartphone.DBus")]
90 public class Server
: Object
93 dynamic DBus
.Object dbus
;
99 debug( "server object created" );
100 conn
= DBus
.Bus
.get( DBus
.BusType
.SYSTEM
);
101 dbus
= conn
.get_object( DBUS_BUS_NAME
, DBUS_OBJ_PATH
, DBUS_INTERFACE
);
105 error( "dbus error: %s", e
.message
);
109 public string[]?
ListBusNames()
111 string[] names
= null;
114 names
= dbus
.ListNames();
118 error( "dbus error: %s", e
.message
);
128 public DBus
.ObjectPath
[] ListObjectPaths( string? busname
) throws DBus
.Error
131 // Check whether the given busname is present on the bus
133 var paths
= new List
<DBus
.ObjectPath
>();
134 var existing_busnames
= this
.ListBusNames();
136 foreach ( string name
in existing_busnames
)
138 if ( busname
== name
)
146 message( "requested busname '%s' not found.", busname
);
147 // FIXME return a dbus error?
148 return objectPathListToArray( paths
);
150 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)"/" );
151 return objectPathListToArray( paths
);
154 private void listObjectPaths( ref List
<DBus
.ObjectPath
> paths
, string busname
, DBus
.ObjectPath objname
) throws DBus
.Error
156 debug( "listObjectPaths: %s, %s", busname
, objname
);
157 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
158 Introspection data
= new
Introspection( obj
.Introspect() );
159 if ( data
.interfaces
.length() > 1 ) // we don't count the introspection interface that is always present
160 paths
.append( objname
);
161 if ( data
.nodes
.length() > 0 )
162 foreach ( string node
in data
.nodes
)
164 if ( objname
== (DBus
.ObjectPath
)"/" )
165 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+node
) );
167 listObjectPaths( ref paths
, busname
, (DBus
.ObjectPath
)((string)objname
+"/"+node
) );
171 public string[] ListObjectsByInterface( string busname
, string iface
) throws DBus
.Error
174 // Check whether the given busname is present on the bus
176 var paths
= new List
<string>();
177 var existing_busnames
= this
.ListBusNames();
179 foreach ( string name
in existing_busnames
)
181 if ( busname
== name
)
189 message( "requested busname '%s' not found.", busname
);
190 // FIXME return a dbus error
191 return stringListToArray( paths
);
193 listObjectsByInterface( ref paths
, busname
, "/", iface
);
194 return stringListToArray( paths
);
197 private void listObjectsByInterface( ref List
<string> paths
, string busname
, string objname
, string iface
) throws DBus
.Error
199 debug( "listObjectsByInterface: %s, %s, %s", busname
, objname
, iface
);
200 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
201 Introspection data
= new
Introspection( obj
.Introspect() );
202 if ( data
.interfaces
.length() > 1 ) // we don't count the introspection interface that is always present
203 foreach ( string ifacename
in data
.interfaces
)
205 if ( ifacename
== iface
)
206 paths
.append( objname
);
208 if ( data
.nodes
.length() > 0 )
209 foreach ( string node
in data
.nodes
)
211 if ( objname
== "/" )
212 listObjectsByInterface( ref paths
, busname
, objname
+node
, iface
);
214 listObjectsByInterface( ref paths
, busname
, objname
+"/"+node
, iface
);
218 public string[] ListInterfacesByObject( string busname
, DBus
.ObjectPath objname
)
220 dynamic DBus
.Object obj
= conn
.get_object( busname
, objname
, DBUS_INTERFACE_INTROSPECTABLE
);
221 Introspection data
= new
Introspection( obj
.Introspect() );
222 return stringListToArray( data
.interfaces
);