1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/* valadbusbindingprovider.vala
*
* Copyright (C) 2007-2008 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
using Gee;
/**
* Dynamic binding provider for DBus objects.
*/
public class Vala.DBusBindingProvider : Object, BindingProvider {
public CodeContext context {
set {
_context = value;
string_type = (Class) _context.root.scope.lookup ("string");
var dbus_ns = _context.root.scope.lookup ("DBus");
if (dbus_ns != null) {
connection_type = (Typesymbol) dbus_ns.scope.lookup ("Connection");
dbus_error_type = (Typesymbol) dbus_ns.scope.lookup ("Error");
}
}
}
private CodeContext _context;
private Class string_type;
private Typesymbol connection_type;
private Typesymbol dbus_error_type;
private Collection<Symbol> symbols = new ArrayList<Symbol> ();
public DBusBindingProvider () {
}
public Symbol? get_binding (MemberAccess ma) {
if (connection_type != null && ma.inner != null && ma.inner.static_type != null && ma.inner.static_type.data_type == connection_type) {
var type_args = ma.get_type_arguments ();
if (type_args.size != 1) {
return null;
}
Iterator<DataType> type_args_it = type_args.iterator ();
type_args_it.next ();
var ret_type = type_args_it.get ().copy ();
if (!is_dbus_interface (ret_type)) {
return null;
}
var m = _context.create_method ("get_object", ret_type, ma.source_reference);
m.set_cname ("dbus_g_proxy_new_for_name");
m.add_cheader_filename ("dbus/dbus-glib.h");
m.access = SymbolAccessibility.PUBLIC;
var string_type_ref = new ClassType (string_type);
m.add_parameter (_context.create_formal_parameter ("name", string_type_ref));
m.add_parameter (_context.create_formal_parameter ("path", string_type_ref));
symbols.add (m);
return m;
} else if (ma.inner != null && ma.inner.static_type != null && is_dbus_interface (ma.inner.static_type)) {
if (ma.parent_node is InvocationExpression) {
var expr = (InvocationExpression) ma.parent_node;
DataType ret_type;
if (expr.expected_type != null) {
ret_type = expr.expected_type.copy ();
ret_type.transfers_ownership = ret_type.is_reference_type_or_type_parameter ();
} else {
ret_type = new VoidType ();
}
var m = new DBusMethod (ma.member_name, ret_type, ma.source_reference);
if (expr.expected_type != null) {
var error_type = CCodeGenerator.get_data_type_for_symbol (dbus_error_type);
m.add_error_domain (error_type);
}
m.access = SymbolAccessibility.PUBLIC;
m.add_parameter (new FormalParameter.with_ellipsis ());
symbols.add (m);
return m;
} else if (ma.parent_node is Assignment) {
var a = (Assignment) ma.parent_node;
if (a.left != ma) {
return null;
}
var s = new DBusSignal (ma.member_name, new VoidType (), ma.source_reference);
s.access = SymbolAccessibility.PUBLIC;
symbols.add (s);
return s;
}
}
return null;
}
private bool is_dbus_interface (DataType t) {
if (!(t.data_type is Interface)) {
return false;
}
return (t.data_type.get_attribute ("DBusInterface") != null);
}
}
|