5 public class IdentityManagerApp {
6 public IdentityManagerModel model;
7 public IdCard default_id_card;
8 public bool explicitly_launched;
9 private IdentityManagerView view;
10 private MoonshotServer ipc_server;
13 public OSXApplication osxApp;
15 // the signal handler function.
16 // the current instance of our app class is passed in the
17 // id_manager_app_instanceparameter
18 public static bool on_osx_open_files (OSXApplication osx_app_instance,
20 IdentityManagerApp id_manager_app_instance ) {
21 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
26 private const int WINDOW_WIDTH = 400;
27 private const int WINDOW_HEIGHT = 500;
29 if (view != null) view.show();
32 public IdentityManagerApp (bool headless) {
33 model = new IdentityManagerModel(this);
35 view = new IdentityManagerView(this);
36 LinkedList<IdCard> card_list = model.get_card_list() ;
37 if (card_list.size > 0)
38 this.default_id_card = card_list.first();
44 osxApp = OSXApplication.get_instance();
45 // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12 e.g.
46 // osxApp.ns_application_open_file.connect(install_from_file);
47 // so we have to use this old way
48 Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
53 public bool add_identity (IdCard id) {
54 /* TODO: add to store here irrespective of view's existence */
55 if (view != null) return view.add_identity(id);
59 public void select_identity (IdentityRequest request) {
60 IdCard identity = null;
62 if (request.select_default)
64 identity = default_id_card;
69 bool has_nai = request.nai != null && request.nai != "";
70 bool has_srv = request.service != null && request.service != "";
72 IdCard nai_provided = null;
74 foreach (IdCard id in model.get_card_list())
76 /* If NAI matches we add id card to the candidate list */
77 if (has_nai && request.nai == id.nai)
80 request.candidates.append (id);
84 /* If any service matches we add id card to the candidate list */
87 foreach (string srv in id.services)
89 if (request.service == srv)
91 request.candidates.append (id);
98 /* If more than one candidate we dissasociate service from all ids */
99 if (has_srv && request.candidates.length() > 1)
101 foreach (IdCard id in request.candidates)
104 SList<string> services_list = null;
105 bool has_service = false;
107 foreach (string srv in id.services)
109 if (srv == request.service)
114 services_list.append (srv);
120 if (services_list.length () == 0)
126 string[] services = new string[services_list.length ()];
127 foreach (string srv in services_list)
133 id.services = services;
137 // model.store_id_cards ();
139 /* If there are no candidates we use the service matching rules */
140 if (request.candidates.length () == 0)
142 foreach (IdCard id in model.get_card_list())
144 foreach (Rule rule in id.rules)
146 if (!match_service_pattern (request.service, rule.pattern))
149 request.candidates.append (id);
151 if (rule.always_confirm == "true")
157 if (request.candidates.length () > 1)
159 if (has_nai && nai_provided != null)
161 identity = nai_provided;
167 if (identity == null)
168 identity = request.candidates.nth_data (0);
169 if (identity == null)
172 /* TODO: If candidate list empty return fail */
174 if (confirm && (view != null))
176 if (!explicitly_launched)
178 view.queue_identity_request(request);
182 // Send back the identity (we can't directly run the
183 // callback because we may be being called from a 'yield')
186 request.return_identity (identity);
187 if (!explicitly_launched)
188 Idle.add( () => { Gtk.main_quit(); return false; } );
195 private bool match_service_pattern (string service, string pattern)
197 var pspec = new PatternSpec (pattern);
198 return pspec.match_string (service);
202 private void init_ipc_server ()
204 // Errors will currently be sent via g_log - ie. to an
205 // obtrusive message box, on Windows
207 this.ipc_server = MoonshotServer.get_instance ();
208 MoonshotServer.start (this.view);
211 private void init_ipc_server ()
215 var conn = DBus.Bus.get (DBus.BusType.SESSION);
216 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
217 "/org/freedesktop/DBus",
218 "org.freedesktop.DBus");
220 // try to register service in session bus
221 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
222 assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
224 this.ipc_server = new MoonshotServer (this);
225 conn.register_object ("/org/janet/moonshot", ipc_server);
229 stderr.printf ("%s\n", e.message);
233 private void bus_acquired_cb (DBusConnection conn)
236 conn.register_object ("/org/janet/moonshot", ipc_server);
240 stderr.printf ("%s\n", e.message);
244 private void init_ipc_server ()
246 this.ipc_server = new MoonshotServer (this);
247 GLib.Bus.own_name (GLib.BusType.SESSION,
248 "org.janet.Moonshot",
249 GLib.BusNameOwnerFlags.NONE,
253 error ("Couldn't own name %s on DBus.", name);
259 static bool explicitly_launched = true;
260 const GLib.OptionEntry[] options = {
261 {"DBusLaunch",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
262 ref explicitly_launched,"launch for dbus rpc use",null},
267 public static int main(string[] args){
269 bool headless = false;
271 bool headless = GLib.Environment.get_variable("DISPLAY") == null;
275 explicitly_launched = false;
278 Gtk.init_with_args(ref args, _(""), options, null);
279 } catch (GLib.Error e) {
280 stdout.printf(_("error: %s\n"),e.message);
281 stdout.printf(_("Run '%s --help' to see a full list of available options"), args[0]);
286 // Force specific theme settings on Windows without requiring a gtkrc file
287 Gtk.Settings settings = Gtk.Settings.get_default ();
288 settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
289 settings.set_long_property ("gtk-menu-images", 0, "moonshot");
292 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
293 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
294 Intl.textdomain (Config.GETTEXT_PACKAGE);
297 var app = new IdentityManagerApp(headless);
298 app.explicitly_launched = explicitly_launched;
300 if (app.explicitly_launched) {
306 MainLoop loop = new MainLoop();