Now ListObjectPaths returns DBus.ObjectPath[]
[dbus-hlid.git] / src / obj.vala
blob0f083957a92ea7d047a2347ef5fae29bf81acf90
1 /*
2 * obj.vala
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 //===========================================================================
23 using GLib;
24 using CONST;
27 //===========================================================================
28 string[] stringListToArray( List<string>? theList )
30 var res = new string[theList.length()];
31 int counter = 0;
32 foreach ( string el in theList )
34 res[counter] = el;
35 counter++;
37 return res;
41 //===========================================================================
42 DBus.ObjectPath[] objectPathListToArray( List<DBus.ObjectPath>? theList )
44 var res = new DBus.ObjectPath[theList.length()];
45 int counter = 0;
46 foreach ( DBus.ObjectPath el in theList )
48 res[counter] = el;
49 counter++;
51 return res;
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 );
70 int res = 0;
71 string name;
72 res = line.scanf( " <node name=\"%a[a-zA-Z0-9_]\"/>", out name );
73 if ( res == 1 )
75 nodes.append( name );
76 message( "object has node '%s'", name );
78 res = line.scanf( " <interface name=\"%a[a-zA-Z0-9_.]\">", out name );
79 if ( res == 1 )
81 message( "object supports interface '%s'", name );
82 interfaces.append( name );
88 //===========================================================================
89 [DBus (name = "org.freesmartphone.DBus")]
90 public class Server : Object
92 DBus.Connection conn;
93 dynamic DBus.Object dbus;
95 construct
97 try
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 );
103 catch (DBus.Error e)
105 error( "dbus error: %s", e.message );
109 public string[]? ListBusNames()
111 string[] names = null;
114 names = dbus.ListNames();
116 catch (DBus.Error e)
118 error( "dbus error: %s", e.message );
119 return names;
121 catch /* all */
123 return names;
125 return names;
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();
135 bool found = false;
136 foreach ( string name in existing_busnames )
138 if ( busname == name )
140 found = true;
141 break;
144 if ( !found )
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) );
166 else
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();
178 bool found = false;
179 foreach ( string name in existing_busnames )
181 if ( busname == name )
183 found = true;
184 break;
187 if ( !found )
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 );
213 else
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 );