3 [DBus (name = "org.janet.Moonshot")]
4 public class MoonshotServer : Object {
6 private MainWindow main_window;
8 public MoonshotServer (Gtk.Window window)
10 this.main_window = (MainWindow) window;
14 * This is the function used by the GSS mechanism to get the NAI,
15 * password and certificate of the ID card for the specificated service.
17 * The function will block until the user choose the ID card.
19 * @param nai NAI of the ID Card (optional)
20 * @param password Password of the ID Card (optional)
21 * @param service Service application request an ID Card for
22 * @param nai_out NAI stored in the ID Card
23 * @param password_out Password stored in the ID Card
24 * @param certificate Certificate stored in th ID Card
26 * @return true if the user choose a correct ID card for that service,
29 public async bool get_identity (string nai,
33 out string password_out,
34 out string certificate_out)
36 bool has_service = false;
38 var request = new IdentityRequest (main_window,
42 request.set_callback ((IdentityRequest) => get_identity.callback());
50 var id_card = request.id_card;
52 if (id_card != null) {
53 foreach (string id_card_service in id_card.services)
55 if (id_card_service == service)
61 nai_out = id_card.nai;
62 password_out = id_card.password;
63 certificate_out = "certificate";
65 // User should have been prompted if there was no p/w.
66 return_if_fail (nai_out != null);
67 return_if_fail (password_out != null);
77 * Returns the default identity - most recently used.
79 * @param nai_out NAI stored in the ID card
80 * @param password_out Password stored in the ID card
82 * @return true on success, false if no identities are stored
84 public async bool get_default_identity (out string nai_out,
85 out string password_out)
87 var request = new IdentityRequest.default (main_window);
88 request.set_callback ((IdentityRequest) => get_default_identity.callback());
95 if (request.id_card != null)
97 nai_out = request.id_card.nai;
98 password_out = request.id_card.password;
100 // User should have been prompted if there was no p/w.
101 return_val_if_fail (nai_out != null, false);
102 return_val_if_fail (password_out != null, false);
114 using MoonshotRpcInterface;
116 /* This class must be a singleton, because we use a global RPC
117 * binding handle. I cannot picture a situation where more than
118 * one instance of the same interface would be needed so this
119 * shouldn't be a problem.
121 * Shutdown is automatically done by the RPC runtime when the
124 public class MoonshotServer : Object {
125 private static MainWindow main_window;
127 private static MoonshotServer instance = null;
129 public static void start (Gtk.Window window)
131 main_window = (MainWindow) window;
132 Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
135 public static MoonshotServer get_instance ()
137 if (instance == null)
138 instance = new MoonshotServer ();
142 [CCode (cname = "moonshot_get_identity")]
143 public static void get_identity (Rpc.AsyncCall call,
148 ref string password_out,
149 ref string certificate_out)
153 var request = new IdentityRequest (main_window,
158 // Pass execution to the main loop and block the RPC thread
159 request.mutex = new Mutex ();
160 request.cond = new Cond ();
161 request.set_callback (return_identity_cb);
163 request.mutex.lock ();
164 Idle.add (request.execute);
166 while (request.complete == false)
167 request.cond.wait (request.mutex);
171 certificate_out = "";
173 var id_card = request.id_card;
174 bool has_service = false;
176 if (id_card == null) {
177 foreach (string id_card_service in id_card.services)
179 if (id_card_service == service)
185 // The strings are freed by the RPC runtime
186 nai_out = id_card.nai;
187 password_out = id_card.password;
188 certificate_out = "certificate";
190 return_if_fail (nai_out != null);
191 return_if_fail (password_out != null);
197 // The outputs must be set before this function is called. For this
198 // reason they are 'ref' not 'out' parameters - Vala assigns to the
199 // 'out' parameters only at the end of the function, which is too
201 call.return (&result);
203 request.cond.signal ();
204 request.mutex.unlock ();
207 [CCode (cname = "moonshot_get_default_identity")]
208 public static void get_default_identity (Rpc.AsyncCall call,
210 ref string password_out)
214 var request = new IdentityRequest.default (main_window);
215 request.mutex = new Mutex ();
216 request.cond = new Cond ();
217 request.set_callback (return_identity_cb);
219 request.mutex.lock ();
220 Idle.add (request.execute);
222 while (request.complete == false)
223 request.cond.wait (request.mutex);
228 if (request.id_card != null)
230 nai_out = request.id_card.nai;
231 password_out = request.id_card.password;
233 return_if_fail (nai_out != null);
234 return_if_fail (password_out != null);
243 call.return (&result);
245 request.cond.signal ();
246 request.mutex.unlock ();
249 // Called from the main loop thread when an identity has
251 static void return_identity_cb (IdentityRequest request) {
252 // Notify the RPC thread that the request is complete
253 request.mutex.lock ();
254 request.cond.signal ();
256 // Block the main loop until the RPC call has returned
257 // to avoid any races
258 request.cond.wait (request.mutex);
259 request.mutex.unlock ();