5 public class IdentityManagerApp {
6 public IdentityManagerModel model;
7 public IdCard default_id_card;
8 private IdentityManagerView view;
9 private MoonshotServer ipc_server;
12 public OSXApplication osxApp;
14 // the signal handler function.
15 // the current instance of our app class is passed in the
16 // id_manager_app_instanceparameter
17 public static bool on_osx_open_files (OSXApplication osx_app_instance,
19 IdentityManagerApp id_manager_app_instance ) {
20 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
25 private const int WINDOW_WIDTH = 400;
26 private const int WINDOW_HEIGHT = 500;
28 if (view != null) view.show();
31 public IdentityManagerApp (bool headless) {
32 model = new IdentityManagerModel(this);
34 view = new IdentityManagerView(this);
35 LinkedList<IdCard> card_list = model.get_card_list() ;
36 if (card_list.size > 0)
37 this.default_id_card = card_list.first();
43 osxApp = OSXApplication.get_instance();
44 // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12 e.g.
45 // osxApp.ns_application_open_file.connect(install_from_file);
46 // so we have to use this old way
47 Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
50 if (view != null) view.show();
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);
170 /* TODO: If candidate list empty return fail */
172 if (confirm && (view != null))
174 view.queue_identity_request(request);
178 // Send back the identity (we can't directly run the
179 // callback because we may be being called from a 'yield')
180 Idle.add (() => { request.return_identity (identity); return false; });
184 private bool match_service_pattern (string service, string pattern)
186 var pspec = new PatternSpec (pattern);
187 return pspec.match_string (service);
191 private void init_ipc_server ()
193 // Errors will currently be sent via g_log - ie. to an
194 // obtrusive message box, on Windows
196 this.ipc_server = MoonshotServer.get_instance ();
197 MoonshotServer.start (this.view);
200 private void init_ipc_server ()
204 var conn = DBus.Bus.get (DBus.BusType.SESSION);
205 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
206 "/org/freedesktop/DBus",
207 "org.freedesktop.DBus");
209 // try to register service in session bus
210 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
211 assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
213 this.ipc_server = new MoonshotServer (this);
214 conn.register_object ("/org/janet/moonshot", ipc_server);
218 stderr.printf ("%s\n", e.message);
222 private void bus_acquired_cb (DBusConnection conn)
225 conn.register_object ("/org/janet/moonshot", ipc_server);
229 stderr.printf ("%s\n", e.message);
233 private void init_ipc_server ()
235 this.ipc_server = new MoonshotServer (this);
236 GLib.Bus.own_name (GLib.BusType.SESSION,
237 "org.janet.Moonshot",
238 GLib.BusNameOwnerFlags.NONE,
242 error ("Couldn't own name %s on DBus.", name);
249 public static int main(string[] args){
251 bool headless = GLib.Environment.get_variable("DISPLAY") != null;
253 bool headless = false;
259 // Force specific theme settings on Windows without requiring a gtkrc file
260 Gtk.Settings settings = Gtk.Settings.get_default ();
261 settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
262 settings.set_long_property ("gtk-menu-images", 0, "moonshot");
265 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
266 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
267 Intl.textdomain (Config.GETTEXT_PACKAGE);
270 var app = new IdentityManagerApp(headless);
276 MainLoop loop = new MainLoop();