fix parser to also recognize objects and interfaces with digits in the name
[dbus-hlid.git] / src / obj.vala
blob47e06c50a2a809b03eb1086155a2abb9686a5f95
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 public class Introspection : Object
44 private string[] _xmldata;
46 public List<string> interfaces;
47 public List<string> nodes;
49 public Introspection( string xmldata )
51 debug( "introspection object created" );
52 _xmldata = xmldata.split( "\n" );
54 foreach ( string line in _xmldata )
56 //debug( "dealing with line '%s'", line );
57 int res = 0;
58 string name;
59 res = line.scanf( " <node name=\"%a[a-zA-Z0-9_]\"/>", out name );
60 if ( res == 1 )
62 nodes.append( name );
63 message( "object has node '%s'", name );
65 res = line.scanf( " <interface name=\"%a[a-zA-Z0-9_.]\">", out name );
66 if ( res == 1 )
68 message( "object supports interface '%s'", name );
69 interfaces.append( name );
75 //===========================================================================
76 [DBus (name = "org.freesmartphone.DBus")]
77 public class Server : Object
79 DBus.Connection conn;
80 dynamic DBus.Object dbus;
82 construct
84 try
86 debug( "server object created" );
87 conn = DBus.Bus.get( DBus.BusType.SYSTEM );
88 dbus = conn.get_object( DBUS_BUS_NAME, DBUS_OBJ_PATH, DBUS_INTERFACE );
89 } catch (DBus.Error e) {
90 error( e.message );
94 public string[]? ListBusNames()
96 string[] names = null;
97 try {
98 names = dbus.ListNames();
99 } catch (DBus.Error e) {
100 error( e.message );
101 return names;
102 } catch {
103 return names;
105 return names;
108 public string[] ListObjectPaths( string? busname ) throws DBus.Error
111 // Check whether the given busname is present on the bus
113 var paths = new List<string>();
114 var existing_busnames = this.ListBusNames();
115 bool found = false;
116 foreach ( string name in existing_busnames )
118 if ( busname == name )
120 found = true;
121 break;
124 if ( !found )
126 message( "requested busname '%s' not found.", busname );
127 // FIXME return a dbus error?
128 return stringListToArray( paths );
130 listObjectPaths( ref paths, busname, "/" );
131 return stringListToArray( paths );
134 private void listObjectPaths( ref List<string> paths, string busname, string objname ) throws DBus.Error
136 debug( "listObjectPaths: %s, %s", busname, objname );
137 dynamic DBus.Object obj = conn.get_object( busname, objname, DBUS_INTERFACE_INTROSPECTABLE );
138 Introspection data = new Introspection( obj.Introspect() );
139 if ( data.interfaces.length() > 1 ) // we don't count the introspection interface that is always present
140 paths.append( objname );
141 if ( data.nodes.length() > 0 )
142 foreach ( string node in data.nodes )
144 if ( objname == "/" )
145 listObjectPaths( ref paths, busname, objname+node );
146 else
147 listObjectPaths( ref paths, busname, objname+"/"+node );
151 public string[] ListObjectsByInterface( string busname, string iface ) throws DBus.Error
154 // Check whether the given busname is present on the bus
156 var paths = new List<string>();
157 var existing_busnames = this.ListBusNames();
158 bool found = false;
159 foreach ( string name in existing_busnames )
161 if ( busname == name )
163 found = true;
164 break;
167 if ( !found )
169 message( "requested busname '%s' not found.", busname );
170 // FIXME return a dbus error
171 return stringListToArray( paths );
173 listObjectsByInterface( ref paths, busname, "/", iface );
174 return stringListToArray( paths );
177 private void listObjectsByInterface( ref List<string> paths, string busname, string objname, string iface ) throws DBus.Error
179 debug( "listObjectsByInterface: %s, %s, %s", busname, objname, iface );
180 dynamic DBus.Object obj = conn.get_object( busname, objname, DBUS_INTERFACE_INTROSPECTABLE );
181 Introspection data = new Introspection( obj.Introspect() );
182 if ( data.interfaces.length() > 1 ) // we don't count the introspection interface that is always present
183 foreach ( string ifacename in data.interfaces )
185 if ( ifacename == iface )
186 paths.append( objname );
188 if ( data.nodes.length() > 0 )
189 foreach ( string node in data.nodes )
191 if ( objname == "/" )
192 listObjectsByInterface( ref paths, busname, objname+node, iface );
193 else
194 listObjectsByInterface( ref paths, busname, objname+"/"+node, iface );