summaryrefslogtreecommitdiff
path: root/src/contacts-linking.vala
blob: 95663c0364adcc6cab5f59ffcd457457c8dcba91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Gtk;
using Folks;
using Gee;

namespace Contacts {
  internal class LinkData {
    HashMultiMap<string, ImFieldDetails> protocols_addrs_set;
    HashMultiMap<string, WebServiceFieldDetails> web_service_addrs_set;
    Gee.HashSet<string> local_ids;

    public LinkData () {
      protocols_addrs_set = new HashMultiMap<string, ImFieldDetails>(null, null,
								     (GLib.HashFunc) ImFieldDetails.hash,
								     (GLib.EqualFunc) ImFieldDetails.equal);
      web_service_addrs_set = new HashMultiMap<string, WebServiceFieldDetails> (null, null,
										(GLib.HashFunc) WebServiceFieldDetails.hash,
										(GLib.EqualFunc) WebServiceFieldDetails.equal);
      local_ids = new Gee.HashSet<string> ();
    }

    public void add_link_data_for_persona (Persona persona) {
      if (persona is ImDetails)  {
	ImDetails im_details = (ImDetails) persona;

	/* protocols_addrs_set = union (all personas' IM addresses) */
	foreach (var protocol in im_details.im_addresses.get_keys ()) {
	  var im_addresses = im_details.im_addresses.get (protocol);
	  foreach (var im_address in im_addresses) {
	    protocols_addrs_set.set (protocol, im_address);
	  }
	}
      }

      if (persona is WebServiceDetails) {
	WebServiceDetails ws_details = (WebServiceDetails) persona;

	/* web_service_addrs_set = union (all personas' WS addresses) */
	foreach (var web_service in
		 ws_details.web_service_addresses.get_keys ()) {
	  var ws_addresses = ws_details.web_service_addresses.get (web_service);
	  foreach (var ws_fd in ws_addresses) {
	    web_service_addrs_set.set (web_service, ws_fd);
	  }
	}
      }

      if (persona is LocalIdDetails) {
	foreach (var id in ((LocalIdDetails) persona).local_ids) {
	  local_ids.add (id);
	}
      }
    }

    public void add_link_data_for_individual (Individual individual) {
      foreach (var persona in individual.personas)
      add_link_data_for_persona (persona);
    }

    public void remove_known_link_data (Persona persona) {
      if (persona is ImDetails)  {
	ImDetails im_details = (ImDetails) persona;

	/* protocols_addrs_set = union (all personas' IM addresses) */
	foreach (var protocol in im_details.im_addresses.get_keys ()) {
	  var im_addresses = im_details.im_addresses.get (protocol);
	  foreach (var im_address in im_addresses) {
	    protocols_addrs_set.remove (protocol, im_address);
	  }
	}
      }

      if (persona is WebServiceDetails) {
	WebServiceDetails ws_details = (WebServiceDetails) persona;

	/* web_service_addrs_set = union (all personas' WS addresses) */
	foreach (var web_service in
		 ws_details.web_service_addresses.get_keys ()) {
	  var ws_addresses = ws_details.web_service_addresses.get (web_service);
	  foreach (var ws_fd in ws_addresses) {
	    web_service_addrs_set.remove (web_service, ws_fd);
	  }
	}
      }

      if (persona is LocalIdDetails) {
	foreach (var id in ((LocalIdDetails) persona).local_ids) {
	  local_ids.remove (id);
	}
      }
    }

    public HashTable<string, Value?> get_link_details () {
      var details = new HashTable<string, Value?> (str_hash, str_equal);

      if (protocols_addrs_set.size > 0) {
	var im_addresses_value = Value (typeof (MultiMap));
	im_addresses_value.set_object (protocols_addrs_set);
	details.insert (PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES),
			im_addresses_value);
      }

      if (web_service_addrs_set.size > 0) {
	var web_service_addresses_value = Value (typeof (MultiMap));
	web_service_addresses_value.set_object (web_service_addrs_set);
	details.insert (PersonaStore.detail_key
			(PersonaDetail.WEB_SERVICE_ADDRESSES),
			web_service_addresses_value);
      }

      if (local_ids.size > 0) {
	var local_ids_value = Value (typeof (Set<string>));
	local_ids_value.set_object (local_ids);
	details.insert (
	  Folks.PersonaStore.detail_key (PersonaDetail.LOCAL_IDS),
	  local_ids_value);
      }
      return details;
    }

    private void get_values_for_updating_persona (Persona persona,
						  out MultiMap<string, ImFieldDetails>? im_value,
						  out MultiMap<string, WebServiceFieldDetails>? web_value,
						  out Set<string>? local_value) {
      im_value = null;
      web_value = null;
      local_value = null;

      if (protocols_addrs_set.size > 0 && persona is ImDetails)  {
	ImDetails im_details = (ImDetails) persona;

	im_value = new HashMultiMap<string, ImFieldDetails>(null, null,
							    (GLib.HashFunc) ImFieldDetails.hash,
							    (GLib.EqualFunc) ImFieldDetails.equal);

	foreach (var protocol in im_details.im_addresses.get_keys ()) {
	  var im_addresses = im_details.im_addresses.get (protocol);
	  foreach (var im_address in im_addresses) {
	    im_value.set (protocol, im_address);
	  }
	}
	foreach (var protocol in protocols_addrs_set.get_keys ()) {
	  var im_addresses = protocols_addrs_set.get (protocol);
	  foreach (var im_address in im_addresses) {
	    im_value.set (protocol, im_address);
	  }
	}
      }

      if (web_service_addrs_set.size > 0 && persona is WebServiceDetails) {
	WebServiceDetails ws_details = (WebServiceDetails) persona;

	web_value = new HashMultiMap<string, WebServiceFieldDetails> (null, null,
								      (GLib.HashFunc) WebServiceFieldDetails.hash,
								      (GLib.EqualFunc) WebServiceFieldDetails.equal);

	foreach (var web_service in
		 ws_details.web_service_addresses.get_keys ()) {
	  var ws_addresses = ws_details.web_service_addresses.get (web_service);
	  foreach (var ws_fd in ws_addresses) {
	    web_value.set (web_service, ws_fd);
	  }
	}
	foreach (var web_service in
		 web_service_addrs_set.get_keys ()) {
	  var ws_addresses = web_service_addrs_set.get (web_service);
	  foreach (var ws_fd in ws_addresses) {
	    web_value.set (web_service, ws_fd);
	  }
	}
      }

      if (local_ids.size > 0 && persona is LocalIdDetails) {
	local_value = new Gee.HashSet<string> ();

	var local_details = (LocalIdDetails) persona;
	local_value.add_all (local_details.local_ids);
	local_value.add_all (local_ids);
      }
    }

    public async void apply_to_persona (Persona persona) {
      MultiMap<string, ImFieldDetails>? im_value = null;
      MultiMap<string, WebServiceFieldDetails>? web_value = null;
      Set<string>? local_value = null;

      get_values_for_updating_persona (persona,
				       out im_value, out web_value, out local_value);
      if (im_value != null) {
	try {
	  yield (persona as ImDetails).change_im_addresses (im_value);
	} catch (GLib.Error e1) {
	  warning ("Unable to set im address when linking: %s\n", e1.message);
	}
      }
      if (web_value != null) {
	try {
	  yield (persona as WebServiceDetails).change_web_service_addresses (web_value);
	} catch (GLib.Error e2) {
	  warning ("Unable to set web service when linking: %s\n", e2.message);
	}
      }
      if (local_value != null) {
	try {
	  yield (persona as LocalIdDetails).change_local_ids (local_value);
	} catch (GLib.Error e3) {
	  warning ("Unable to set local ids when linking: %s\n", e3.message);
	}
      }
    }
  }

  public async void link_contacts (Contact main, Contact other) {
    // This should not be used as being replaced with the new individual
    // instead we should always pick this contact to keep around
    other.individual.set_data ("contacts-not-replaced", true);

    var link_data = new LinkData ();
    Persona? writable_persona = null;
    Persona? primary = main.find_primary_persona ();
    Persona? other_primary = other.find_primary_persona ();
    if (primary != null) {
      link_data.add_link_data_for_individual (other.individual);
      writable_persona = primary;
    } else if (other_primary != null) {
      link_data.add_link_data_for_individual (main.individual);
      writable_persona = other_primary;
    } else {
      link_data.add_link_data_for_individual (other.individual);
      link_data.add_link_data_for_individual (main.individual);
      var details = link_data.get_link_details ();

      var name = Value (typeof (string));
      name.set_string (main.display_name);
      details.insert (PersonaStore.detail_key (PersonaDetail.FULL_NAME),
		      name);
      try {
	yield main.store.aggregator.add_persona_from_details (null,
							      main.store.aggregator.primary_store, details);
      } catch (GLib.Error e) {
	warning ("Unable to create new persona when linking: %s\n", e.message);
      }
      writable_persona = null;
    }

    if (writable_persona != null) {
      link_data.remove_known_link_data (writable_persona);
      yield link_data.apply_to_persona (writable_persona);
    }
  }

  internal abstract class PersonaAttribute : Object {
    public string property_name;

    public static HashSet<PersonaAttribute> create_set () {
      return new HashSet<PersonaAttribute>((GLib.HashFunc) PersonaAttribute.hash,
					   (GLib.EqualFunc) PersonaAttribute.equal);
    }

    public virtual bool is_removable (Persona from_persona) {
      return (property_name in from_persona.writeable_properties);
    }

    public abstract bool is_referenced_by_persona (Persona persona);

    public abstract string to_string ();

    public virtual bool equal (PersonaAttribute that) {
      return this.property_name == that.property_name;
    }

    public virtual uint hash () {
      return this.property_name.hash ();
    }

    public abstract async void persona_apply_attributes (Persona persona,
							 Set<PersonaAttribute> added_attributes,
							 Set<PersonaAttribute> removed_attributes);
  }

  internal class PersonaAttributeLocalId : PersonaAttribute {
    string value;

    public PersonaAttributeLocalId (string value) {
      property_name = "local-ids";
      this.value = value;
    }

    public override bool is_removable (Persona from_persona) {
      return base.is_removable (from_persona) && value != from_persona.iid;
    }

    public override string to_string () {
      return "local_id: " + value;
    }

    public override bool is_referenced_by_persona (Persona persona) {
      var details = persona as LocalIdDetails;
      if (details == null)
	return false;

      return value in details.local_ids;
    }

    public override async void persona_apply_attributes (Persona persona,
							 Set<PersonaAttribute> added_attributes,
							 Set<PersonaAttribute> removed_attributes) {
      var details = persona as LocalIdDetails;
      if (details == null)
	return;

      var added_values = new HashSet<string> ();
      foreach (var added in added_attributes) {
	added_values.add (((PersonaAttributeLocalId)added).value);
      }

      var removed_values = new HashSet<string> ();
      foreach (var removed in removed_attributes) {
	removed_values.add (((PersonaAttributeLocalId)removed).value);
      }

      var new_values = new HashSet<string> ();
      bool changed = false;
      foreach (var v in details.local_ids) {
	if (v in removed_values) {
	  changed = true;
	  continue;
	}
	new_values.add (v);
	if (v in added_values)
	  added_values.remove (v);
      }
      foreach (var v2 in added_values) {
	changed = true;
	new_values.add (v2);
      }

      if (changed) {
	try {
	  yield details.change_local_ids (new_values);
	} catch (GLib.Error e) {
	  warning ("Unable to set local ids when linking: %s\n", e.message);
	}
      }
    }

    public override bool equal (PersonaAttribute _that) {
      var that = _that as PersonaAttributeLocalId;
      return
	that != null &&
	base.equal (that) &&
	this.value == that.value;
    }

    public override uint hash () {
      return this.value.hash () ^ base.hash ();
    }
  }

  internal class PersonaAttributeImAddress : PersonaAttribute {
    string protocol;
    ImFieldDetails detail;

    public PersonaAttributeImAddress (string protocol, ImFieldDetails detail) {
      property_name = "im-addresses";
      this.protocol = protocol;
      this.detail = detail;
    }

    public override string to_string () {
      return "im_addresses: " + protocol + ":" + detail.value;
    }

    public override bool is_referenced_by_persona (Persona persona) {
      var details = persona as ImDetails;
      if (details == null)
	return false;

      return detail in details.im_addresses.get (protocol);
    }

    public override async void persona_apply_attributes (Persona persona,
							 Set<PersonaAttribute> added_attributes,
							 Set<PersonaAttribute> removed_attributes) {
      var details = persona as ImDetails;
      if (details == null)
	return;

      var added_values = new HashMultiMap<string, ImFieldDetails> (null, null,
								   (GLib.HashFunc) ImFieldDetails.hash,
								   (GLib.EqualFunc) ImFieldDetails.equal);
      foreach (var added in added_attributes) {
	added_values.set (((PersonaAttributeImAddress)added).protocol, ((PersonaAttributeImAddress)added).detail);
      }

      var removed_values = new HashMultiMap<string, ImFieldDetails> (null, null,
								     (GLib.HashFunc) ImFieldDetails.hash,
								     (GLib.EqualFunc) ImFieldDetails.equal);
      foreach (var removed in removed_attributes) {
	removed_values.set (((PersonaAttributeImAddress)removed).protocol, ((PersonaAttributeImAddress)removed).detail);
      }

      var new_values =
	new HashMultiMap<string, ImFieldDetails> (null, null,
						  (GLib.HashFunc) ImFieldDetails.hash,
						  (GLib.EqualFunc) ImFieldDetails.equal);
      bool changed = false;
      foreach (var proto1 in details.im_addresses.get_keys ()) {
	foreach (var detail1 in details.im_addresses.get (proto1)) {
	  if (removed_values.get (proto1).contains (detail1)) {
	    changed = true;
	    continue;
	  }
	  new_values.set (proto1, detail1);
	  if (added_values.get (proto1).contains (detail1)) {
	    added_values.remove (proto1, detail1);
	  }
	}
      }
      foreach (var proto2 in added_values.get_keys ()) {
	foreach (var detail2 in added_values.get (proto2)) {
	  changed = true;
	  new_values.set (proto2, detail2);
	}
      }

      if (changed) {
	try {
	  yield details.change_im_addresses (new_values);
	} catch (GLib.Error e) {
	  warning ("Unable to set im address when linking: %s\n", e.message);
	}
      }
    }

    public override bool equal (PersonaAttribute _that) {
      var that = _that as PersonaAttributeImAddress;
      return
	that != null &&
	base.equal (that) &&
	this.protocol == that.protocol &&
	this.detail.equal (that.detail);
    }

    public override uint hash () {
      return this.protocol.hash () ^ this.detail.hash () ^ base.hash ();
    }
  }

  internal class PersonaAttributeWebService : PersonaAttribute {
    string service;
    WebServiceFieldDetails detail;

    public PersonaAttributeWebService (string service, WebServiceFieldDetails detail) {
      property_name = "web-service-addresses";
      this.service = service;
      this.detail = detail;
    }

    public override string to_string () {
      return "web_service_addresses: " + service + ":" + detail.value;
    }

    public override bool is_referenced_by_persona (Persona persona) {
      var details = persona as WebServiceDetails;
      if (details == null)
	return false;

      return detail in details.web_service_addresses.get (service);
    }

    public override async void persona_apply_attributes (Persona persona,
							 Set<PersonaAttribute> added_attributes,
							 Set<PersonaAttribute> removed_attributes) {
      var details = persona as WebServiceDetails;
      if (details == null)
	return;

      var added_values = new HashMultiMap<string, WebServiceFieldDetails> (null, null,
									   (GLib.HashFunc) WebServiceFieldDetails.hash,
									   (GLib.EqualFunc) WebServiceFieldDetails.equal);
      foreach (var added in added_attributes) {
	added_values.set (((PersonaAttributeWebService)added).service, ((PersonaAttributeWebService)added).detail);
      }

      var removed_values = new HashMultiMap<string, WebServiceFieldDetails> (null, null,
									     (GLib.HashFunc) WebServiceFieldDetails.hash,
									     (GLib.EqualFunc) WebServiceFieldDetails.equal);
      foreach (var removed in removed_attributes) {
	removed_values.set (((PersonaAttributeWebService)removed).service, ((PersonaAttributeWebService)removed).detail);
      }

      var new_values =
	new HashMultiMap<string, WebServiceFieldDetails> (null, null,
							  (GLib.HashFunc) WebServiceFieldDetails.hash,
							  (GLib.EqualFunc) WebServiceFieldDetails.equal);
      bool changed = false;
      foreach (var srv1 in details.web_service_addresses.get_keys ()) {
	foreach (var detail1 in details.web_service_addresses.get (srv1)) {
	  if (removed_values.get (srv1).contains (detail1)) {
	    changed = true;
	    continue;
	  }
	  new_values.set (srv1, detail1);
	  if (added_values.get (srv1).contains (detail1)) {
	    added_values.remove (srv1, detail1);
	  }
	}
      }
      foreach (var srv2 in added_values.get_keys ()) {
	foreach (var detail2 in added_values.get (srv2)) {
	  changed = true;
	  new_values.set (srv2, detail2);
	}
      }

      if (changed) {
	try {
	  yield details.change_web_service_addresses (new_values);
	} catch (GLib.Error e) {
	  warning ("Unable to set web service when linking: %s\n", e.message);
	}
      }
    }

    public override bool equal (PersonaAttribute _that) {
      var that = _that as PersonaAttributeWebService;
      return
	that != null &&
	base.equal (that) &&
	this.service == that.service &&
	this.detail.equal (that.detail);
    }

    public override uint hash () {
      return this.service.hash () ^ this.detail.hash () ^ base.hash ();
    }
  }

  public static Set<PersonaAttribute> get_linkable_attributes (Persona persona) {
    var res = PersonaAttribute.create_set ();

    if (persona is LocalIdDetails) {
      foreach (var id in ((LocalIdDetails) persona).local_ids) {
	res.add (new PersonaAttributeLocalId (id));
      }
    }

    if (persona is ImDetails) {
      foreach (var proto in ((ImDetails) persona).im_addresses.get_keys ()) {
	foreach (var im in ((ImDetails) persona).im_addresses.get (proto)) {
	  res.add (new PersonaAttributeImAddress (proto, im));
	}
      }
    }

    if (persona is WebServiceDetails) {
      foreach (var srv in ((WebServiceDetails) persona).web_service_addresses.get_keys ()) {
	foreach (var web in ((WebServiceDetails) persona).web_service_addresses.get (srv)) {
	  res.add (new PersonaAttributeWebService (srv, web));
	}
      }
    }

    return res;
  }

  public static bool persona_can_link_to (Persona persona, Set<PersonaAttribute> attributes) {
    var property_names = new HashSet<string>(str_hash, str_equal);
    foreach (var a in attributes)
      property_names.add (a.property_name);

    foreach (var p in property_names) {
      if (! (p in persona.writeable_properties))
	return false;
    }
    return true;
  }

  internal bool attr_type_equal (PersonaAttribute a, PersonaAttribute b) {
    return
      a.get_type() == b.get_type() &&
      a.property_name == b.property_name;
  }

  internal uint attr_type_hash (PersonaAttribute key) {
    return (uint)key.get_type() ^ key.property_name. hash ();
  }

  public static async void persona_apply_attributes (Persona persona,
						     Set<PersonaAttribute>? added_attributes,
						     Set<PersonaAttribute>? removed_attributes) {
    var properties = new HashSet<PersonaAttribute>((GLib.HashFunc)attr_type_hash, (GLib.EqualFunc) attr_type_equal);

    if (added_attributes != null) {
      foreach (var a1 in added_attributes) {
	properties.add (a1);
      }
    }
    if (removed_attributes != null) {
      foreach (var a2 in removed_attributes) {
	properties.add (a2);
      }
    }

    foreach (var property in properties) {
      var added = PersonaAttribute.create_set ();
      var removed = PersonaAttribute.create_set ();
      if (added_attributes != null) {
	foreach (var a3 in added_attributes) {
	  if (attr_type_equal (a3, property))
	    added.add (a3);
	}
      }
      if (removed_attributes != null) {
	foreach (var a4 in removed_attributes) {
	  if (attr_type_equal (a4, property))
	    removed.add (a4);
	}
      }
      yield property.persona_apply_attributes (persona, added, removed);
    }
  }

  public async void unlink_persona (Contact contact, Persona persona_to_unlink) {
    var individual = contact.individual;
    var persona_to_unlink_removals = PersonaAttribute.create_set ();
    var other_personas_removals = PersonaAttribute.create_set ();

    foreach (PersonaAttribute a1 in get_linkable_attributes (persona_to_unlink)) {
      // Check that this attribute actually is used to link this persona to the individual
      bool used_to_link = false;
      foreach (var persona in individual.personas) {
	if (persona != persona_to_unlink &&
	    a1.is_referenced_by_persona (persona)) {
	  used_to_link = true;
	  break;
	}
      }
      if (!used_to_link)
	continue; // Wasn't used, no need to do anything about it

      if (a1.is_removable (persona_to_unlink)) {
	// We can remove the attribute from the persona, which should completely break any linkage
	// due to this attribute
	persona_to_unlink_removals.add (a1);
      } else {
	// We can't remove the attribute from the persona, need to make sure no other persona
	// references this
	other_personas_removals.add (a1);
      }
    }

    // At this point we know how to unlink the persona from the individual, however
    // doing so may cause the remaining personas to form disjoint sets rather than
    // a single Individual. Consider two subsets of personas A and B, and the unlinked
    // persona u which make up the original individual. When unlinking u A and B may be
    // disjoint if:
    // * A links to u and u Links to B, then the data from u that linked it to B was
    //   removed (and no other links go between A and B)
    //  or
    // * A and B both link to u, but to unlink them from u we removed the data in A and
    //   B that caused this link (and no other links go between A and B)
    //
    // To fix this up we need to ensure that all the remaining personas in the inidivudal
    // do have links by picking (or creating if there is none) a persona where all linkable
    // attributes are writeable and ensuring that it can reach all the other remaining
    // personas. We do this the easy way by just adding all linkable attributes to this
    // persona
    var main_persona_additions = PersonaAttribute.create_set ();
    foreach (var p1 in individual.personas) {
      if (p1 == persona_to_unlink)
	continue;
      foreach (PersonaAttribute a2 in get_linkable_attributes (p1)) {
	if (a2 in other_personas_removals)
	  continue;
	main_persona_additions.add (a2);
      }
    }

    // Find tha main persona that will be used to add the extra linking info to
    // avoid disjoint sets
    Persona? main_persona = null;
    foreach (var p2 in individual.personas) {
      if (p2 != persona_to_unlink && persona_can_link_to (p2, main_persona_additions)) {
	main_persona = p2;
	if (main_persona.store.is_writeable)
	  break; // Exit if we find a primary persona, as we prefer these
      }
    }

    // We make a copy of the personas as the on in the individual may start
    // changing now
    var other_personas = new HashSet<Persona>();
    foreach (var p3 in individual.personas) {
      if (p3 != persona_to_unlink &&
	  p3 != main_persona)
	other_personas.add (p3);
    }

    if (main_persona == null) {
      var details = new HashTable<string, Value?> (str_hash, str_equal);
      try {
	main_persona = yield contact.store.aggregator.primary_store.add_persona_from_details (details);
      } catch (GLib.Error e) {
	warning ("Unable to create new persona when unlinking: %s\n", e.message);
	return;
      }
    }

    persona_to_unlink.set_data ("contacts-new-contact", true);

    // First apply all additions on the primary persona so that we avoid temporarily being
    // unlinked and then relinked
    // Note, this may cause a new persona to be added to the individual if
    // main_persona is null
    yield persona_apply_attributes (main_persona, main_persona_additions, other_personas_removals);
    foreach (var p in other_personas) {
      yield persona_apply_attributes (p, null, other_personas_removals);
    }
    // Last we do the removals on the persona_to_unlink
    yield persona_apply_attributes (persona_to_unlink, null, persona_to_unlink_removals);
  }
}