[webp] Added confirmation dialog
[moonshot-ui.git] / src / moonshot-server.vala
1 #if IPC_DBUS
2
3 [DBus (name = "org.janet.Moonshot")]
4 public class MoonshotServer : Object {
5
6     private MainWindow main_window;
7
8     public MoonshotServer (Gtk.Window window)
9     {
10         this.main_window = (MainWindow) window;
11     }
12
13     public async bool get_identity (string nai,
14                                     string password,
15                                     string service,
16                                     out string nai_out,
17                                     out string password_out,
18                                     out string server_certificate_hash,
19                                     out string ca_certificate,
20                                     out string subject_name_constraint,
21                                     out string subject_alt_name_constraint)
22     {
23         var request = new IdentityRequest (main_window,
24                                            nai,
25                                            password,
26                                            service);
27         request.set_callback ((IdentityRequest) => get_identity.callback());
28         request.execute ();
29         yield;
30
31         nai_out = "";
32         password_out = "";
33         server_certificate_hash = "";
34         ca_certificate = "";
35         subject_name_constraint = "";
36         subject_alt_name_constraint = "";
37
38         var id_card = request.id_card;
39
40         if (id_card != null) {
41             nai_out = id_card.nai;
42             password_out = id_card.password;
43
44             server_certificate_hash = "certificate";
45
46             // User should have been prompted if there was no p/w.
47             return_if_fail (nai_out != null);
48             return_if_fail (password_out != null);
49
50             return true;
51         }
52
53         return false;
54     }
55
56     public async bool get_default_identity (out string nai_out,
57                                             out string password_out,
58                                             out string server_certificate_hash,
59                                             out string ca_certificate,
60                                             out string subject_name_constraint,
61                                             out string subject_alt_name_constraint)
62     {
63         var request = new IdentityRequest.default (main_window);
64         request.set_callback ((IdentityRequest) => get_default_identity.callback());
65         request.execute ();
66         yield;
67
68         nai_out = "";
69         password_out = "";
70         server_certificate_hash = "";
71         ca_certificate = "";
72         subject_name_constraint = "";
73         subject_alt_name_constraint = "";
74
75         if (request.id_card != null)
76         {
77             nai_out = request.id_card.nai;
78             password_out = request.id_card.password;
79
80             server_certificate_hash = "certificate";
81
82             // User should have been prompted if there was no p/w.
83             return_val_if_fail (nai_out != null, false);
84             return_val_if_fail (password_out != null, false);
85
86             return true;
87         }
88
89         return false;
90     }
91     
92     public async bool install_id_card (string   display_name,
93                                        string   user_name,
94                                        string   password,
95                                        string   realm,
96                                        Rule[]   rules,
97                                        string[] services,
98                                        string   ca_cert,
99                                        string   subject,
100                                        string   subject_alt,
101                                        string   server_cert)
102     {
103       IdCard idcard = new IdCard ();
104       
105       idcard.display_name = display_name;
106       idcard.username = user_name;
107       idcard.password = password;
108       idcard.issuer = realm;
109       idcard.rules = rules;
110       idcard.services = services;
111       idcard.trust_anchor.ca_cert = ca_cert;
112       idcard.trust_anchor.subject = subject;
113       idcard.trust_anchor.subject_alt = subject_alt;
114       idcard.trust_anchor.server_cert = server_cert;
115
116       /* TODO: Check if display name already exists */
117       
118       idcard.pixbuf = find_icon ("avatar-default", 48);
119
120       /* TODO: Act on close */      
121       var dialog = new WebProvisioning.ConfirmDialog (idcard);
122       dialog.show_all ();
123       var ret = dialog.run ();
124       dialog.hide ();
125
126       if (ret == Gtk.ResponseType.ACCEPT)      
127       {
128         main_window.insert_id_card (idcard);
129         return true;
130       }
131       
132       return false;
133     }
134                                        
135 }
136
137 #elif IPC_MSRPC
138
139 using Rpc;
140 using MoonshotRpcInterface;
141
142 /* This class must be a singleton, because we use a global RPC
143  * binding handle. I cannot picture a situation where more than
144  * one instance of the same interface would be needed so this
145  * shouldn't be a problem.
146  *
147  * Shutdown is automatically done by the RPC runtime when the
148  * process ends
149  */
150 public class MoonshotServer : Object {
151     private static MainWindow main_window;
152
153     private static MoonshotServer instance = null;
154
155     public static void start (Gtk.Window window)
156     {
157         main_window = (MainWindow) window;
158         Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
159     }
160
161     public static MoonshotServer get_instance ()
162     {
163         if (instance == null)
164             instance = new MoonshotServer ();
165         return instance;
166     }
167
168     [CCode (cname = "moonshot_get_identity_rpc")]
169     public static void get_identity (Rpc.AsyncCall call,
170                                      string nai,
171                                      string password,
172                                      string service,
173                                      ref string nai_out,
174                                      ref string password_out,
175                                      ref string server_certificate_hash,
176                                      ref string ca_certificate,
177                                      ref string subject_name_constraint,
178                                      ref string subject_alt_name_constraint)
179     {
180         bool result = false;
181
182         var request = new IdentityRequest (main_window,
183                                            nai,
184                                            password,
185                                            service);
186
187         // Pass execution to the main loop and block the RPC thread
188         request.mutex = new Mutex ();
189         request.cond = new Cond ();
190         request.set_callback (return_identity_cb);
191
192         request.mutex.lock ();
193         Idle.add (request.execute);
194
195         while (request.complete == false)
196             request.cond.wait (request.mutex);
197
198         nai_out = "";
199         password_out = "";
200         server_certificate_hash = "";
201         ca_certificate = "";
202         subject_name_constraint = "";
203         subject_alt_name_constraint = "";
204
205         var id_card = request.id_card;
206
207         if (id_card == null) {
208             // The strings are freed by the RPC runtime
209             nai_out = id_card.nai;
210             password_out = id_card.password;
211             server_certificate_hash = "certificate";
212
213             return_if_fail (nai_out != null);
214             return_if_fail (password_out != null);
215             return_if_fail (server_certificate_hash != null);
216             return_if_fail (ca_certificate != null);
217             return_if_fail (subject_name_constraint != null);
218             return_if_fail (subject_alt_name_constraint != null);
219
220             result = true;
221         }
222
223         // The outputs must be set before this function is called. For this
224         // reason they are 'ref' not 'out' parameters - Vala assigns to the
225         // 'out' parameters only at the end of the function, which is too
226         // late.
227         call.return (&result);
228
229         request.cond.signal ();
230         request.mutex.unlock ();
231     }
232
233     [CCode (cname = "moonshot_get_default_identity_rpc")]
234     public static void get_default_identity (Rpc.AsyncCall call,
235                                              ref string nai_out,
236                                              ref string password_out,
237                                              ref string server_certificate_hash,
238                                              ref string ca_certificate,
239                                              ref string subject_name_constraint,
240                                              ref string subject_alt_name_constraint)
241     {
242         bool result;
243
244         var request = new IdentityRequest.default (main_window);
245         request.mutex = new Mutex ();
246         request.cond = new Cond ();
247         request.set_callback (return_identity_cb);
248
249         request.mutex.lock ();
250         Idle.add (request.execute);
251
252         while (request.complete == false)
253             request.cond.wait (request.mutex);
254
255         nai_out = "";
256         password_out = "";
257         server_certificate_hash = "";
258         ca_certificate = "";
259         subject_name_constraint = "";
260         subject_alt_name_constraint = "";
261
262         if (request.id_card != null)
263         {
264             nai_out = request.id_card.nai;
265             password_out = request.id_card.password;
266             server_certificate_hash = "certificate";
267
268             return_if_fail (nai_out != null);
269             return_if_fail (password_out != null);
270             return_if_fail (server_certificate_hash != null);
271             return_if_fail (ca_certificate != null);
272             return_if_fail (subject_name_constraint != null);
273             return_if_fail (subject_alt_name_constraint != null);
274
275             result = true;
276         }
277         else
278         {
279             result = false;
280         }
281
282         call.return (&result);
283
284         request.cond.signal ();
285         request.mutex.unlock ();
286     }
287
288     // Called from the main loop thread when an identity has
289     // been selected
290     static void return_identity_cb (IdentityRequest request) {
291         // Notify the RPC thread that the request is complete
292         request.mutex.lock ();
293         request.cond.signal ();
294
295         // Block the main loop until the RPC call has returned
296         // to avoid any races
297         request.cond.wait (request.mutex);
298         request.mutex.unlock ();
299     }
300 }
301
302 #endif