Attachment 'valaclientsample.vala'
Download 1 using DBus;
2 using Gee;
3
4 // valac --pkg dbus-glib-1 --pkg gee-1.0 valaclientsample.val
5
6 // [DBus (name = "org.gnome.evolution.metadata.Manager")]
7 // public interface Manager : GLib.Object {
8 // public abstract void Register (DBus.ObjectPath registrar_path, uint last_modseq);
9 // }
10
11 [DBus (name = "org.freedesktop.email.metadata.Registrar")]
12 public class Registrar: GLib.Object {
13
14 private string service { get; set; }
15
16 public Registrar (string service) {
17 this.service = service;
18 print ("Registrar for %s\n", service);
19 }
20
21 public void Set (string subject, string[] predicates, string[] values, uint modseq) {
22 print ("[%u] set (%s): %s\n", modseq, service, subject);
23 }
24
25 public void Cleanup (uint modseq) {
26 print ("[%u] cleanup (%s)\n", modseq, service);
27 }
28
29 public void SetMany (string[] subjects, string[][] predicates, string[][] values, uint modseq) {
30
31
32 uint len = subjects.length;
33 uint i;
34
35 print ("[%u] setmany (%s): %d\n", modseq, service, subjects.length);
36
37
38 for (i = 0; i < len; i++) {
39 print ("- [%u] setmany (%s): %s\n", modseq, service, subjects[i]);
40 //
41 // There's a bug in Vala that makes lengths of inner arrays of a
42 // stacked array being wrong (apparently the inner array is no
43 // longer NULL terminated after demarshalign, which makes calcu-
44 // lating the length impossible)
45 //
46 // uint plen = 7; // strv_length (predicates[i]);
47 // uint y;
48 //
49 // for (y = 0; y < plen; y++) {
50 // if (predicates[i][y] != null && values[i][y] != null) {
51 // print ("\t%s=%s\n", predicates[i][y], values[i][y]);
52 // }
53 // }
54 }
55
56 }
57
58 public void UnsetMany (string[] subjects, uint modseq) {
59 print ("[%u] unsetmany (%s): %d\n", modseq, service, subjects.length);
60
61 }
62
63 public void Unset (string subject, uint modseq) {
64 print ("[%u] unset (%s): %s\n", modseq, service, subject);
65 }
66 }
67
68 public class MyApplication : GLib.Object {
69
70 public uint stored_time;
71 private DBus.Connection conn;
72 private HashMap<string, Registrar> registrars;
73 private dynamic DBus.Object bus;
74
75 public MyApplication () {
76 registrars = new HashMap<string, Registrar> (str_hash, str_equal, str_equal);
77 }
78
79 private void on_reply (GLib.Error e) {
80 if (e != null)
81 print ("Err?\n");
82 print ("registered\n");
83 }
84
85 private void deactivate (string name) {
86 print ("Unregister %s\n", name);
87 registrars.remove (name);
88 }
89
90 private void activate (string name) {
91 dynamic DBus.Object obj;
92 Registrar registrar = new Registrar (name);
93 DBus.ObjectPath path;
94 string emailclient = "evolution";
95
96 print ("Activating %s\n", name);
97
98
99 conn = DBus.Bus.get (DBus.BusType.SESSION);
100
101 if (name == "org.kde.kmail")
102 emailclient = "kmail";
103
104 path = new DBus.ObjectPath ("/my/application/mail_registrars/" + emailclient);
105
106 obj = conn.get_object (name, "/org/freedesktop/email/metadata/Manager",
107 "org.freedesktop.email.metadata.Manager");
108
109 conn.register_object (path, registrar);
110
111 try {
112 obj.Register (path, stored_time, on_reply);
113
114 registrars.set (name, registrar);
115
116 } catch (GLib.Error e) {
117 message ("Can't register: %s", e.message);
118 }
119 }
120
121 private void on_name_owner_changed (DBus.Object sender, string name, string old_owner, string new_owner) {
122 if (name == "org.gnome.evolution" || name == "org.kde.kmail") {
123 if (new_owner != "" && old_owner == "")
124 activate (name);
125 if (old_owner != "" && new_owner == "")
126 deactivate (name);
127 }
128 }
129
130 private void list_names_reply_cb (string[] names, GLib.Error e) {
131 foreach (string name in names) {
132 if (name == "org.gnome.evolution" || name == "org.kde.kmail") {
133 activate (name);
134 break;
135 }
136 }
137 }
138
139 private bool on_ready () {
140
141 try {
142 bus.list_names (list_names_reply_cb);
143 } catch (GLib.Error e) {
144 message ("Can't list: %s", e.message);
145 }
146
147 return false;
148 }
149
150 public void setup (uint stored_time) throws DBus.Error, GLib.Error {
151
152 this.stored_time = stored_time;
153
154 conn = DBus.Bus.get (DBus.BusType.SESSION);
155
156 bus = conn.get_object ("org.freedesktop.DBus",
157 "/org/freedesktop/DBus",
158 "org.freedesktop.DBus");
159
160 bus.NameOwnerChanged += on_name_owner_changed;
161
162 Idle.add (on_ready);
163 }
164
165 static int main (string[] args) {
166 var loop = new MainLoop (null, false);
167
168 var app = new MyApplication ();
169
170 try {
171 uint a = 0;
172
173 if (args.length > 1)
174 a = (uint) args[1].to_ulong();
175 else
176 a = 0;
177
178 app.setup (a);
179
180 } catch (DBus.Error e) {
181 stderr.printf ("Failed to initialise: %s", e.message);
182 return 1;
183 } catch {
184 stderr.printf ("Dynamic method failure");
185 return 1;
186 }
187
188 loop.run ();
189
190 return 0;
191 }
192 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.