3646edfacbbdeac12fbb8d835f70423c8462ba70
[moonshot-ui.git] / src / moonshot-identity-request.vala
1 public delegate void ReturnIdentityCallback (IdentityRequest request);
2
3 public class IdentityRequest : Object {
4     public IdCard? id_card = null;
5     public bool complete = false;
6     public bool select_default = false;
7
8     private IdentityManagerApp parent_app;
9     public string nai;
10     public string password;
11     public string service;
12     public SList<IdCard> candidates;
13
14     ReturnIdentityCallback callback = null;
15
16     public IdentityRequest (IdentityManagerApp           app,
17                             string                       nai,
18                             string                       password,
19                             string                       service)
20     {
21         this.parent_app = app;
22         this.nai = nai;
23         this.password = password;
24         this.service = service;
25     }
26
27     public IdentityRequest.default (IdentityManagerApp app)
28     {
29         this.parent_app = app;
30         this.select_default = true;
31     }
32
33     public void set_callback (owned ReturnIdentityCallback cb)
34     {
35 #if VALA_0_12
36         this.callback = ((owned) cb);
37 #else
38         this.callback = ((IdCard) => cb (IdCard));
39 #endif
40     }
41
42     public bool execute () {
43         parent_app.select_identity (this);
44
45         /* This function works as a GSourceFunc, so it can be passed to
46          * the main loop from other threads
47          */
48         return false;
49     }
50
51     public void return_identity (IdCard? id_card) {
52         this.id_card = id_card;
53         this.complete = true;
54
55         /* update id_card service list */
56         if (id_card != null && this.service != null && this.service != "")
57         {
58             bool duplicate_service = false;
59
60             foreach (string service in id_card.services)
61             {
62                 if (service == this.service)
63                     duplicate_service = true;
64             }
65             if (duplicate_service == false)
66             {
67                 string[] services = new string[id_card.services.length + 1];
68
69                 for (int i = 0; i < id_card.services.length; i++)
70                     services[i] = id_card.services[i];
71
72                 services[id_card.services.length] = this.service;
73                 id_card.services = services;
74
75                 this.parent_app.model.update_card (id_card);
76             }
77         }
78
79         return_if_fail (callback != null);
80         callback (this);
81     }
82
83 #if OS_WIN32
84     /* For synchronisation between RPC thread and main loop. Because
85      * these objects are not refcounted, it's best to tie them to the
86      * lifecycle of the IdentityRequest object.
87      */
88     public Mutex mutex;
89     public Cond cond;
90 #endif
91 }