summaryrefslogtreecommitdiff
path: root/src/contacts-fake-persona-store.vala
blob: 8891a93472b8a54accaf036724974a6f92d4c027 (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
/*
 * 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 Folks;
using Gee;

/**
 * A "dummy" store which is used to have an equivalent of a PersonaStore for a
 * FakePersona.
 */
public class Contacts.FakePersonaStore : PersonaStore {
  public static FakePersonaStore _the_store;
  public static FakePersonaStore the_store() {
    if (_the_store == null)
      _the_store = new FakePersonaStore ();
    return _the_store;
  }
  private HashMap<string, Persona> _personas;
  private Map<string, Persona> _personas_ro;

  public override string type_id { get { return "fake"; } }

  public FakePersonaStore () {
    Object (id: "uri", display_name: "fake store");
    this._personas = new HashMap<string, Persona> ();
    this._personas_ro = this._personas.read_only_view;
  }

  public override Map<string, Persona> personas {
    get { return this._personas_ro; }
  }

  public override MaybeBool can_add_personas { get { return MaybeBool.FALSE; } }
  public override MaybeBool can_alias_personas { get { return MaybeBool.FALSE; } }
  public override MaybeBool can_group_personas { get { return MaybeBool.FALSE; } }
  public override MaybeBool can_remove_personas { get { return MaybeBool.TRUE; } }
  public override bool is_prepared  { get { return true; } }
  public override bool is_quiescent  { get { return true; } }
  private string[] _always_writeable_properties = {};
  public override string[] always_writeable_properties { get { return this._always_writeable_properties; } }

  public override async void prepare () throws GLib.Error { }

  public override async Persona? add_persona_from_details (HashTable<string, Value?> details) throws Folks.PersonaStoreError {
    return null;
  }

  public override async void remove_persona (Persona persona) throws Folks.PersonaStoreError {
  }
}

/**
 * A "dummy" Persona which is used when creating a new contact
 * The FakePersona is used as a placeholder till we get the real persona from folks
 * It needs to implement all Details we support so that we don't loise any information
 */
const string BACKEND_NAME = "fake-store";

public class Contacts.FakePersona : Persona,
AvatarDetails,
BirthdayDetails,
EmailDetails,
ImDetails,
NameDetails,
NoteDetails,
PhoneDetails,
UrlDetails,
PostalAddressDetails
{
  private HashTable<string, Value?> properties;
  // Keep track of the persona in the actual store
  public weak Persona real_persona { get; set; default = null; }

  private string[] _writeable_properties = {};
  private const string[] _linkable_properties = {};
  public override string[] linkable_properties {
    get { return _linkable_properties; }
  }

  public override string[] writeable_properties {
    get {
      return this._writeable_properties;
    }
  }

  private ArrayList<string> _changed_properties;

  construct {
    this._changed_properties = new ArrayList<string> ();
  }

  public LoadableIcon? avatar {
    get {
      unowned Value? value = this.properties.get ("avatar");
      return (LoadableIcon?) value;
    }
    set {
      this.properties.set ("avatar", value);
    }
  }

  public async void change_avatar (LoadableIcon? avatar) throws PropertyError {
    this.avatar = avatar;
  }

  public string full_name {
    get {
      unowned Value? value = this.properties.get ("full-name");
      if (value == null)
        return "";
      return value.get_string ();
    }
    set {
      this.properties.set ("full-name", value);
    }
  }

  public string nickname {
    get {
      unowned Value? value = this.properties.get ("nickname");
      if (value == null)
        return "";
      return value.get_string ();
    }
    set {
      this.properties.set ("nickname", value);
    }
  }

  //TODO: implement structured_name
  public StructuredName? structured_name {
    get { return null; }
    set {}
  }

  public Set<PhoneFieldDetails> phone_numbers {
    get {
      unowned Value? value = this.properties.get ("phone-numbers");
      if (value == null) {
        var new_value = Value (typeof (Set));
        var set = new FakeHashSet<PhoneFieldDetails> ();
        new_value.set_object (set);
        set.changed.connect (() => { notify_property ("phone-numbers"); });
        this.properties.set ("phone-numbers", new_value);
        value = this.properties.get ("phone-numbers");
      }
      return (Set<PhoneFieldDetails>) value;
    }
    set {
      this.properties.set ("phone-numbers", value);
    }
  }

  public Set<UrlFieldDetails> urls {
    get { 
      unowned Value? value = this.properties.get ("urls");
      if (value == null) {
        var new_value = Value (typeof (Set));
        var set = new FakeHashSet<UrlFieldDetails> ();
        new_value.set_object (set);
        set.changed.connect (() => { notify_property ("urls"); });
        this.properties.set ("urls", new_value);
        value = new_value;
      }
      return (Set<UrlFieldDetails>) value;
    }
    set {
      this.properties.set ("urls", value);
    }
  }

  public Set<PostalAddressFieldDetails> postal_addresses {
    get {
      unowned Value? value = this.properties.get ("postal-addresses");
      if (value == null) {
        var new_value = Value (typeof (Set));
        var set = new FakeHashSet<PostalAddressFieldDetails> ();
        new_value.set_object (set);
        set.changed.connect (() => { notify_property ("postal-addresses"); });
        this.properties.set ("postal-addresses", new_value);
        value = new_value;
      }
      return (Set<PostalAddressFieldDetails>) value;
    }
    set {
      this.properties.set ("postal-addresses", value);
    }
  }

  public Set<NoteFieldDetails> notes {
    get {
      unowned Value? value = this.properties.get ("notes");
      if (value == null) {
        var new_value = Value (typeof (Set));
        var set = new FakeHashSet<NoteFieldDetails> ();
        new_value.set_object (set);
        set.changed.connect (() => { notify_property ("notes"); });
        this.properties.set ("notes", new_value);
        value = new_value;
      }
      return (Set<NoteFieldDetails>) value;
    }
    set {
      this.properties.set ("notes", value);
    }
  }

  public DateTime? birthday {
    get { unowned Value? value = this.properties.get ("birthday");
      if (value == null)
        return null;
      return (DateTime) value;
    }
    set {
      this.properties.set ("birthday", value);
    }
  }

  //TODO implement calender_event_id
  public string? calendar_event_id {
    get { return null; }
    set {}
  }

  public MultiMap<string,ImFieldDetails> im_addresses {
    get {
      unowned Value? value = this.properties.get ("im-addresses");
      if (value == null) {
        var new_value = Value (typeof (MultiMap));
        var set = new FakeHashMultiMap<string, ImFieldDetails> ();
        new_value.set_object (set);
        this.properties.set ("im-addresses", new_value);
        set.changed.connect (() => { notify_property ("im-addresses"); });
        value = new_value;
      }
      return (MultiMap<string, ImFieldDetails>) value;
    }
    set {
      this.properties.set ("im-addresses", value);
    }
  }

  public Set<EmailFieldDetails> email_addresses {
    get {
      unowned Value? value = this.properties.get ("email-addresses");
      if (value == null) {
        var new_value = Value (typeof (Set));
        var set = new FakeHashSet<EmailFieldDetails> ();
        set.changed.connect (() => { notify_property ("email-addresses"); });
        new_value.set_object (set);
        this.properties.set ("email-addresses", new_value);
        value = new_value;
      }
      return (Set<EmailFieldDetails>) value;
    }
    set {
      this.properties.set ("email-addresses", value);
    }
  }

  public FakePersona (PersonaStore store, string[] writeable_properties, HashTable<string, Value?> details) {
    var id = Uuid.string_random();
    var uid = Folks.Persona.build_uid (BACKEND_NAME, store.id, id);
    var iid = "%s:%s".printf (store.id, id);
    Object (display_id: iid,
            uid: uid,
            iid: iid,
            store: store,
            is_user: false);

    this.properties = details;
    this._writeable_properties = writeable_properties;
  }

  public FakePersona.from_real (Persona persona) {
    var details = new HashTable<string, Value?> (str_hash, str_equal);
    this (FakePersonaStore.the_store (), persona.writeable_properties, details);
    // FIXME: get all properties not only writable properties
    var props = persona.writeable_properties;
    foreach (var prop in props) {
      get_property_from_real (persona, prop);
    }

    this.real_persona = persona;
    // FIXME: we are adding property changes also for things we don't care about e.g. individual
    this.notify.connect((obj, ps) => {
      add_to_changed_properties(ps.name);
    });
  }

  private void get_property_from_real (Persona persona, string property_name) {
    // TODO Implement the interface for the commented properties
    switch (property_name) {
      case "alias":
        //alias = ((AliasDetails) persona).alias;
        break;
      case "avatar":
        avatar = ((AvatarDetails) persona).avatar;
        break;
      case "birthday":
        birthday = ((BirthdayDetails) persona).birthday;
        break;
      case "calendar-event-id":
        calendar_event_id = ((BirthdayDetails) persona).calendar_event_id;
        break;
      case "email-addresses":
        foreach (var e in ((EmailDetails) persona).email_addresses) {
          email_addresses.add (new EmailFieldDetails (e.value, e.parameters));
        }
        break;
      case "is-favourite":
        //is_favourite = ((FavouriteDetails) persona).is_favourite;
        break;
      case "gender":
        //gender = ((GenderDetails) persona).gender;
        break;
      case "groups":
        //groups = ((GroupDetails) persona).groups;
        break;
      case "im-addresses":
        im_addresses = ((ImDetails) persona).im_addresses;
        break;
      case "local-ids":
        //local_ids = ((LocalIdDetails) persona).local_ids;
        break;
      case "structured-name":
        structured_name = ((NameDetails) persona).structured_name;
        break;
      case "full-name":
        full_name = ((NameDetails) persona).full_name;
        break;
      case "nickname":
        nickname = ((NameDetails) persona).nickname;
        break;
      case "notes":
        foreach (var e in ((NoteDetails) persona).notes) {
          notes.add (new NoteFieldDetails (e.value, e.parameters, e.id));
        }
        break;
      case "phone-numbers":
        foreach (var e in ((PhoneDetails) persona).phone_numbers) {
          phone_numbers.add (new PhoneFieldDetails (e.value, e.parameters));
        }
        break;
      case "postal-addresses":
        foreach (var e in ((PostalAddressDetails) persona).postal_addresses) {
          postal_addresses.add (new PostalAddressFieldDetails (e.value, e.parameters));
        }
        break;
      case "roles":
        //roles ((RoleDetails) persona).roles;
        break;
      case "urls":
        foreach (var e in ((UrlDetails) persona).urls) {
          urls.add (new UrlFieldDetails (e.value, e.parameters));
        }
        break;
      case "web-service-addresses":
        //web_service_addresses.add_all(((WebServiceDetails) persona).web_service_addresses);
        break;
      default:
        debug ("Unknown property '%s' in FakePersona.get_property_from_real().", property_name);
        break;
    }
  }

  private void add_to_changed_properties (string property_name) {
    debug ("Property: %s was added to the changed property list", property_name);
    if (!this._changed_properties.contains(property_name))
      this._changed_properties.add (property_name);
  }

  public HashTable<string, Value?>  get_details () {
    return this.properties;
  }

  public async void apply_changes_to_real () {
    if (this.real_persona == null) {
      warning ("No real persona to apply changes from fake persona");
      return;
    }
    foreach (var prop in _changed_properties) {
      if (properties.contains (prop)) {
        try {
        yield set_persona_property (this.real_persona, prop, properties.get (prop));
        } catch (Error e) {
          error ("Couldn't write property: %s", e.message);
        }
      }
    }
  }

  private static async void set_persona_property (Persona persona,
                                          string property_name, Value new_value) throws PropertyError, IndividualAggregatorError, PropertyError {
    switch (property_name) {
      case "alias":
        yield ((AliasDetails) persona).change_alias ((string) new_value);
        break;
      case "avatar":
        yield ((AvatarDetails) persona).change_avatar ((LoadableIcon?) new_value);
        break;
      case "birthday":
        yield ((BirthdayDetails) persona).change_birthday ((DateTime?) new_value);
        break;
      case "calendar-event-id":
        yield ((BirthdayDetails) persona).change_calendar_event_id ((string?) new_value);
        break;
      case "email-addresses":
        var original = (Set<EmailFieldDetails>) new_value;
        var copy = new HashSet<EmailFieldDetails> ();
        foreach (var e in original) {
          if (e.value != null && e.value != "")
            copy.add (new EmailFieldDetails (e.value, e.parameters));
        }
        yield ((EmailDetails) persona).change_email_addresses (copy);
        break;
      case "is-favourite":
        yield ((FavouriteDetails) persona).change_is_favourite ((bool) new_value);
        break;
      case "gender":
        yield ((GenderDetails) persona).change_gender ((Gender) new_value);
        break;
      case "groups":
        yield ((GroupDetails) persona).change_groups ((Set<string>) new_value);
        break;
      case "im-addresses":
        yield ((ImDetails) persona).change_im_addresses ((MultiMap<string, ImFieldDetails>) new_value);
        break;
      case "local-ids":
        yield ((LocalIdDetails) persona).change_local_ids ((Set<string>) new_value);
        break;
      case "structured-name":
        yield ((NameDetails) persona).change_structured_name ((StructuredName?) new_value);
        break;
      case "full-name":
        yield ((NameDetails) persona).change_full_name ((string) new_value);
        break;
      case "nickname":
        yield ((NameDetails) persona).change_nickname ((string) new_value);
        break;
      case "notes":
        var original = (Set<NoteFieldDetails>) new_value;
        var copy = new HashSet<NoteFieldDetails> ();
        foreach (var e in original) {
          if (e.value != null && e.value != "")
            copy.add (new NoteFieldDetails (e.value, e.parameters));
        }
        yield ((NoteDetails) persona).change_notes (copy);
        break;
      case "phone-numbers":
        var original = (Set<PhoneFieldDetails>) new_value;
        var copy = new HashSet<PhoneFieldDetails> ();
        foreach (var e in original) {
          if (e.value != null && e.value != "")
            copy.add (new PhoneFieldDetails (e.value, e.parameters));
        }
        yield ((PhoneDetails) persona).change_phone_numbers (copy);
        break;
      case "postal-addresses":
        var original = (Set<PostalAddressFieldDetails>) new_value;
        var copy = new HashSet<PostalAddressFieldDetails> ();
        foreach (var e in original) {
          // TODO: make sure that the Postal Address isn't empty
          if (e.value != null)
            copy.add (new PostalAddressFieldDetails (e.value, e.parameters));
        }
        yield ((PostalAddressDetails) persona).change_postal_addresses (copy);
        break;
      case "roles":
        yield ((RoleDetails) persona).change_roles ((Set<RoleFieldDetails>) new_value);
        break;
      case "urls":
        var original = (Set<UrlFieldDetails>) new_value;
        var copy = new HashSet<UrlFieldDetails> ();
        foreach (var e in original) {
          if (e.value != null && e.value != "")
            copy.add (new UrlFieldDetails (e.value, e.parameters));
        }
        yield ((UrlDetails) persona).change_urls (copy);
        break;
      case "web-service-addresses":
        yield ((WebServiceDetails) persona).change_web_service_addresses ((MultiMap<string, WebServiceFieldDetails>) new_value);
        break;
      default:
        critical ("Unknown property '%s' in Contact.set_persona_property().", property_name);
        break;
    }
  }
}

/**
 * A FakeIndividual
 */
public class Contacts.FakeIndividual : Individual {
  public weak Individual real_individual { get; set; default = null; }
  public weak FakePersona primary_persona { get; set; default = null; }
  public FakeIndividual (Set<FakePersona>? personas) {
    base (personas);
    foreach (var p in personas) {
      // Keep track of the main persona
      if (Contacts.Utils.persona_is_main (p) || personas.size == 1)
        primary_persona = p;
    }
  }

  public FakeIndividual.from_real (Individual individual) {
    var fake_personas = new HashSet<FakePersona> ();
    foreach (var p in individual.personas) {
      var fake_p = new FakePersona.from_real (p);
      // Keep track of the main persona
      if (Contacts.Utils.persona_is_main (p) || individual.personas.size == 1)
        primary_persona = fake_p;
      fake_personas.add (fake_p);
    }
    this (fake_personas);
    this.real_individual = individual;
  }

  public async void apply_changes_to_real () {
    if (this.real_individual == null) {
      warning ("No real individual to apply changes from fake individual");
      return;
    }

    foreach (var p in this.personas) {
        var fake_persona = p as FakePersona;
        if (fake_persona != null) {
          yield fake_persona.apply_changes_to_real ();
        }
      }
  }
}

/**
 * This is the same as Gee.HashSet but adds a changed/added/removed signals
 */
public class Contacts.FakeHashSet<T> : Gee.HashSet<T> {
  public signal void changed ();
  public signal void added ();
  public signal void removed ();

  public FakeHashSet () {
    base ();
  }

  public override bool add (T element) {
    var res = base.add (element);
    if (res) {
      added();
      changed ();
    }
    return res;
  }

  public override bool remove (T element) {
    var res = base.remove (element);
    if (res) {
      removed();
      changed ();
    }
    return res;
  }
}

/**
 * This is the same as Gee.HashMultiMap but adds a changed signal
 */
public class Contacts.FakeHashMultiMap<K, T> : Gee.HashMultiMap<K, T> {
  public signal void changed ();

  public FakeHashMultiMap () {
    base ();
  }
}