Fix new identity request code on Windows
[moonshot-ui.git] / src / moonshot-identity-request.vala
1 delegate void ReturnIdentityCallback (IdentityRequest request);
2
3 class IdentityRequest : Object {
4     public IdCard? id_card = null;
5     public bool complete = false;
6
7     private MainWindow main_window;
8     private string nai;
9     private string password;
10     private string certificate;
11
12     // Only one of these is used, we must support two types for
13     // the DBus and MSRPC servers.
14     ReturnIdentityCallback return_identity_cb = null;
15     SourceFunc source_func_cb = null;
16
17     public IdentityRequest (MainWindow                   main_window,
18                             string                       nai,
19                             string                       password,
20                             string                       certificate)
21     {
22         this.main_window = main_window;
23         this.nai = nai;
24         this.password = password;
25         this.certificate = certificate;
26     }
27
28     public void set_return_identity_callback (owned ReturnIdentityCallback cb)
29     {
30 #if VALA_0_12
31         this.return_identity_cb = ((owned) cb);
32 #else
33         this.return_identity_cb = ((IdCard) => cb (IdCard));
34 #endif
35     }
36
37     public void set_source_func_callback (owned SourceFunc cb)
38     {
39 #if VALA_0_12
40         this.source_func_cb = ((owned) cb);
41 #else
42         this.source_func_cb = (() => cb ());
43 #endif
44     }
45
46     public bool execute () {
47         main_window.select_identity (this);
48
49         /* This function works as a GSourceFunc, so it can be passed to
50          * the main loop from other threads
51          */
52         return false;
53     }
54
55     public void return_identity (IdCard? id_card) {
56         this.id_card = id_card;
57         this.complete = true;
58
59         if (return_identity_cb != null)
60             return_identity_cb (this);
61         else if (source_func_cb != null)
62             source_func_cb ();
63         else
64             warn_if_reached ();
65     }
66
67 #if OS_WIN32
68     /* For synchronisation between RPC thread and main loop. Because
69      * these objects are not refcounted, it's best to tie them to the
70      * lifecycle of the IdentityRequest object.
71      */
72     public Mutex mutex;
73     public Cond cond;
74 #endif
75 }