+ }
+
+ public bool add_identity(IdCard id, bool force_flat_file_store) {
+ if (view != null) return view.add_identity(id, force_flat_file_store);
+ model.add_card(id, force_flat_file_store);
+ return true;
+ }
+
+ public void select_identity(IdentityRequest request) {
+ logger.trace("select_identity");
+
+ IdCard identity = null;
+
+ if (request.select_default)
+ {
+ identity = default_id_card;
+ }
+
+ if (identity == null)
+ {
+ bool has_nai = request.nai != null && request.nai != "";
+ bool has_srv = request.service != null && request.service != "";
+ bool confirm = false;
+
+ foreach (IdCard id in model.get_card_list())
+ {
+ /* If NAI matches, use this id card */
+ if (has_nai && request.nai == id.nai)
+ {
+ identity = id;
+ break;
+ }
+
+ /* If any service matches we add id card to the candidate list */
+ if (has_srv)
+ {
+ foreach (string srv in id.services)
+ {
+ if (request.service == srv)
+ {
+ request.candidates.append(id);
+ continue;
+ }
+ }
+ }
+ }
+
+ /* If more than one candidate we dissasociate service from all ids */
+ if ((identity == null) && has_srv && request.candidates.length() > 1)
+ {
+ foreach (IdCard id in request.candidates)
+ {
+ int i = 0;
+ SList<string> services_list = null;
+ bool has_service = false;
+
+ foreach (string srv in id.services)
+ {
+ if (srv == request.service)
+ {
+ has_service = true;
+ continue;
+ }
+ services_list.append(srv);
+ }
+
+ if (!has_service)
+ continue;
+
+ if (services_list.length() == 0)
+ {
+ id.services = {};
+ continue;
+ }
+
+ string[] services = new string[services_list.length()];
+ foreach (string srv in services_list)
+ {
+ services[i] = srv;
+ i++;
+ }
+
+ id.services = services;
+ }
+ }
+
+ /* If there are no candidates we use the service matching rules */
+ if ((identity == null) && (request.candidates.length() == 0))
+ {
+ foreach (IdCard id in model.get_card_list())
+ {
+ foreach (Rule rule in id.rules)
+ {
+ if (!match_service_pattern(request.service, rule.pattern))
+ continue;
+
+ request.candidates.append(id);
+
+ if (rule.always_confirm == "true")
+ confirm = true;
+ }
+ }
+ }
+
+ if ((identity == null) && has_nai) {
+ // create a temp identity
+ string[] components = request.nai.split("@", 2);
+ identity = new IdCard();
+ identity.display_name = request.nai;
+ identity.username = components[0];
+ if (components.length > 1)
+ identity.issuer = components[1];
+ identity.password = request.password;
+ identity.temporary = true;
+ }
+ if (identity == null) {
+ if (request.candidates.length() != 1) {
+ logger.trace("select_identity: Have %u candidates; user must make selection.".printf(request.candidates.length()));
+ confirm = true;
+ } else {
+ identity = request.candidates.nth_data(0);
+ }
+ }
+
+ if (confirm && (view != null))
+ {
+ if (!explicitly_launched)
+ show();
+ view.queue_identity_request(request);
+ return;
+ }
+ }
+ // Send back the identity (we can't directly run the
+ // callback because we may be being called from a 'yield')
+ GLib.Idle.add(
+ () => {
+ if (view != null) {
+ logger.trace("select_identity (Idle handler): calling check_add_password");
+ identity = view.check_add_password(identity, request, model);
+ }
+ request.return_identity(identity);
+// The following occasionally causes the app to exit without sending the dbus
+// reply, so for now we just don't exit
+// if (!explicitly_launched)
+// Idle.add(() => { Gtk.main_quit(); return false; } );
+ return false;
+ }
+ );
+ return;
+ }
+
+ private bool match_service_pattern(string service, string pattern) {
+ var pspec = new PatternSpec(pattern);
+ return pspec.match_string(service);