catch up with vala
[dbus-hlid.git] / src / obj.vala
blob8a4481bfcdb2f4238d08a3426ed6da65de023f24
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;
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()];
41 int counter = 0;
42 foreach ( string el in theList )
44 res[counter] = el;
45 counter++;
47 return res;
51 //===========================================================================
52 DBus.ObjectPath[] objectPathListToArray( List<DBus.ObjectPath>? theList )
54 var res = new DBus.ObjectPath[theList.length()];
55 int counter = 0;
56 foreach ( DBus.ObjectPath el in theList )
58 res[counter] = el;
59 counter++;
61 return res;
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 );
80 int res = 0;
81 string name;
82 res = line.scanf( " <node name=\"%a[a-zA-Z0-9_]\"/>", out name );
83 if ( res == 1 )
85 nodes.append( name );
86 message( "object has node '%s'", name );
88 res = line.scanf( " <interface name=\"%a[a-zA-Z0-9_.]\">", out name );
89 if ( res == 1 )
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;
104 construct
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 );
112 catch (DBus.Error e)
114 error( "dbus error: %s", e.message );
118 public string[]? ListBusNames() throws DBus.Error
120 string[] names = null;
123 names = dbus.ListNames();
125 catch (DBus.Error e)
127 error( "dbus error: %s", e.message );
128 return names;
130 catch /* all */
132 return names;
134 return names;
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();
144 bool found = false;
145 foreach ( string name in existing_busnames )
147 if ( busname == name )
149 found = true;
150 break;
153 if ( !found )
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) );
175 else
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();
187 bool found = false;
188 foreach ( string name in existing_busnames )
190 if ( busname == name )
192 found = true;
193 break;
196 if ( !found )
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 );
222 else
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);
236 res.sort( strcmp );
237 foreach( var p in res )
238 debug("res: %s", p);
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 )
256 first = false;
257 foreach( string node in data.nodes )
259 DBus.ObjectPath fullpath = null;
260 if( objname == (DBus.ObjectPath)"/" )
261 fullpath = (DBus.ObjectPath)((string)objname+node);
262 else
263 fullpath = (DBus.ObjectPath)((string)objname+"/"+node);
265 list.concat( listChildrenByObject( busname, fullpath, recursive ) );
269 catch (GLib.Error e)
271 critical( "ListChildrenByObject for %s: %s", objname, e.message );
273 return list;