summaryrefslogtreecommitdiff
path: root/src/contacts-types.vala
blob: 2a4058a055d821952239912fcbd39f05772b2645 (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
/* -*- 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 Gee;
using Folks;

public class Contacts.TypeSet : Object  {
  const string X_GOOGLE_LABEL = "x-google-label";
  const int MAX_TYPES = 3;
  private struct InitData {
    unowned string display_name_u;
    unowned string types[3]; //MAX_TYPES
  }

  private class Data : Object {
    public string display_name; // Translated
    public GLib.List<InitData*> init_data;
    public TreeIter iter; // Set if in_store
    public bool in_store;
  }

  // Dummy Data to mark the "Custom..." store entry
  private static Data custom_dummy = new Data ();
  // Dummy Data to mark the "Other..." store entry
  private static Data other_dummy = new Data ();

  // Map from translated display name to Data for all "standard" types
  private HashTable<unowned string, Data> display_name_hash;
  // Map from all type strings in to list of InitData with the data in it
  private HashTable<unowned string, GLib.List<InitData*> > vcard_lookup_hash;
  // Map from display name to TreeIter for all custom types
  private HashTable<string, TreeIter?> custom_hash;

  public Gtk.ListStore store;
  private TreeIter other_iter;

  private TypeSet () {
    display_name_hash = new HashTable<unowned string, Data> (str_hash, str_equal);
    vcard_lookup_hash = new HashTable<unowned string, GLib.List<InitData*> > (str_hash, str_equal);
    custom_hash = new HashTable<string, TreeIter? > (str_hash, str_equal);

    store = new Gtk.ListStore (2,
                               // Display name or null for separator
                               typeof(string?),
                               // Data for standard types, null for custom
                               typeof (Data));
  }

  private void add_data_to_store (Data data, bool is_custom) {
    if (data.in_store)
      return;

    data.in_store = true;
    if (is_custom)
      store.insert_before (out data.iter, null);
    else
      store.append (out data.iter);

    store.set (data.iter, 0, data.display_name, 1, data);
  }

  private void add_init_data (InitData *init_data) {
    unowned string dn = dgettext (Config.GETTEXT_PACKAGE, init_data.display_name_u);
    Data data = display_name_hash.lookup (dn);
    if (data == null) {
      data = new Data ();
      data.display_name = dn;
      display_name_hash.insert (dn, data);
    }

    data.init_data.append (init_data);

    for (int j = 0; j < MAX_TYPES && init_data.types[j] != null; j++) {
      unowned string type = init_data.types[j];
      unowned GLib.List<InitData*> l = vcard_lookup_hash.lookup (type);
      if (l != null) {
	l.append (init_data);
      } else {
	GLib.List<InitData*> l2 = null;
	l2.append (init_data);
	vcard_lookup_hash.insert (type, (owned) l2);
      }
    }
  }

  private void add_init_data_done (string[] standard_untranslated) {
    foreach (var untranslated in standard_untranslated) {
      var data = display_name_hash.lookup (dgettext (Config.GETTEXT_PACKAGE, untranslated));
      if (data != null)
	add_data_to_store (data, false);
      else
	error ("Internal error: Can't find display name %s in TypeSet data", untranslated);
    }

    store.append (out other_iter);
    /* Refers to the type of the detail, could be Home, Work or Other for email, and the same
     * for phone numbers, addresses, etc. */
    store.set (other_iter, 0, _("Other"), 1, other_dummy);
  }

  public void add_custom_label (string label, out TreeIter iter) {
    // If we add a custom name equal to one of the standard ones, reuse that one
    var data = display_name_hash.lookup (label);
    if (data != null) {
      add_data_to_store (data, true);
      iter = data.iter;
      return;
    }

    if (label == _("Other")) {
      iter = other_iter;
      return;
    }

    unowned TreeIter? iterp = custom_hash.lookup (label);
    if (iterp != null) {
      iter = iterp;
      return;
    }

    store.insert_before (out iter, null);
    store.set (iter, 0, label, 1, null);
    custom_hash.insert (label, iter);
  }

  private unowned Data? lookup_data_by_string (string str) {
    unowned GLib.List<InitData *>? l = vcard_lookup_hash.lookup (str);
    foreach (unowned InitData *d in l) {
      if (d.types[1] == null) {
	unowned string dn = dgettext (Config.GETTEXT_PACKAGE, d.display_name_u);
	return display_name_hash.lookup (dn);
      }
    }

    return null;
  }


  private unowned Data? lookup_data (AbstractFieldDetails detail) {
    var i = detail.get_parameter_values ("type");
    if (i == null || i.is_empty)
      return null;

    var list = new Gee.ArrayList<string> ();
    foreach (var s in detail.get_parameter_values ("type")) {
      list.add (s.up ());
    }

    // Make sure all items in the InitData is in the specified type, there might
    // be more, but we ignore them (so a HOME,FOO,PREF,BLAH contact still matches
    // the standard HOME one, but not HOME,FAX
    unowned GLib.List<InitData *>? l = vcard_lookup_hash.lookup (list[0]);
    foreach (unowned InitData *d in l) {
      bool all_found = true;
      for (int j = 0; j < MAX_TYPES && d.types[j] != null; j++) {
	if (!list.contains (d.types[j])) {
	  all_found = false;
	  break;
	}
      }
      if (all_found) {
	unowned string dn = dgettext (Config.GETTEXT_PACKAGE, d.display_name_u);
	return display_name_hash.lookup (dn);
      }
    }

    return null;
  }

  // Looks up (and creates if necessary) the type in the store
  public void lookup_type (AbstractFieldDetails detail, out TreeIter iter) {
    if (detail.parameters.contains (X_GOOGLE_LABEL)) {
      var label = Utils.get_first<string> (detail.parameters.get (X_GOOGLE_LABEL));
      add_custom_label (label, out iter);
      return;
    }

    unowned Data? d = lookup_data (detail);
    if (d != null) {
      add_data_to_store (d, true);
      iter = d.iter;
    } else {
      iter = other_iter;
    }
  }

  public void lookup_type_by_string (string type, out TreeIter iter) {
    unowned Data? d = lookup_data_by_string (type);
    if (d != null) {
      iter = d.iter;
    } else {
      iter = other_iter;
    }
  }

  public string format_type (AbstractFieldDetails detail) {
    if (detail.parameters.contains (X_GOOGLE_LABEL)) {
      return Utils.get_first<string> (detail.parameters.get (X_GOOGLE_LABEL));
    }

    unowned Data? d = lookup_data (detail);
    if (d != null) {
      return d.display_name;
    }

    return _("Other");
  }

  public void update_details (AbstractFieldDetails details, TreeIter iter) {
    var old_parameters = details.parameters;
    details.parameters = new HashMultiMap<string, string> ();
    bool has_pref = false;
    foreach (var value in old_parameters.get ("type")) {
      if (value.ascii_casecmp ("PREF") == 0) {
	has_pref = true;
	break;
      }
    }
    foreach (var param in old_parameters.get_keys()) {
      if (param != "type" && param != X_GOOGLE_LABEL) {
	foreach (var value in old_parameters.get (param)) {
	  details.parameters.set (param, value);
	}
      }
    }

    Data data;
    string display_name;
    store.get (iter, 0, out display_name, 1, out data);

    assert (display_name != null); // Not separator
    assert (data != custom_dummy); // Not custom...

    if (data == null) { // A custom label
      details.parameters.set ("type", "OTHER");
      details.parameters.set (X_GOOGLE_LABEL, display_name);
    } else {
      if (data == other_dummy) {
	  details.parameters.set ("type", "OTHER");
      } else {
	InitData *init_data = data.init_data.data;
	for (int j = 0; j < MAX_TYPES && init_data.types[j] != null; j++) {
	  details.parameters.set ("type", init_data.types[j]);
	}
      }
    }

    if (has_pref)
      details.parameters.set ("type", "PREF");
  }

  public bool is_custom (TreeIter iter) {
    Data data;
    store.get (iter, 1, out data);
    return data == custom_dummy;
  }

  private static TypeSet _general;
  private const InitData[] general_data = {
    // List most specific first, always in upper case
    { N_("Home"), { "HOME" } },
    { N_("Work"), { "WORK" } }
  };
  public static TypeSet general {
    get {
      string[] standard = {
	"Work", "Home"
      };

      if (_general == null) {
	_general = new TypeSet ();
	for (int i = 0; i < general_data.length; i++)
	  _general.add_init_data (&general_data[i]);
	_general.add_init_data_done (standard);
      }

      return _general;
    }
  }

  private static TypeSet _email;
  private const InitData[] email_data = {
    // List most specific first, always in upper case
    { N_("Personal"), { "PERSONAL" } },
    { N_("Home"), { "HOME" } },
    { N_("Work"), { "WORK" } }
  };
  public static TypeSet email {
    get {
      string[] standard = {
	"Personal", "Home", "Work"
      };

      if (_email == null) {
	_email = new TypeSet ();
	for (int i = 0; i < email_data.length; i++)
	  _email.add_init_data (&email_data[i]);
	_email.add_init_data_done (standard);
      }

      return _email;
    }
  }

  private static TypeSet _phone;
  public static TypeSet phone {
    get {
      const InitData[] data = {
	// List most specific first, always in upper case
	{ N_("Assistant"), { "X-EVOLUTION-ASSISTANT" } },
	{ N_("Work"), { "WORK", "VOICE" } },
	{ N_("Work Fax"), { "WORK", "FAX" } },
	{ N_("Callback"),   { "X-EVOLUTION-CALLBACK" } },
	{ N_("Car"),        { "CAR" } },
	{ N_("Company"),    { "X-EVOLUTION-COMPANY" } },
	{ N_("Home"),       { "HOME", "VOICE" } },
	{ N_("Home Fax"),         { "HOME", "FAX" } },
	{ N_("ISDN"),             { "ISDN" } },
	{ N_("Mobile"),     { "CELL" } },
	{ N_("Other"),      { "VOICE" } },
	{ N_("Fax"),        { "FAX" } },
	{ N_("Pager"),            { "PAGER" } },
	{ N_("Radio"),            { "X-EVOLUTION-RADIO" } },
	{ N_("Telex"),            { "X-EVOLUTION-TELEX" } },
	/* To translators: TTY is Teletypewriter */
	{ N_("TTY"),              { "X-EVOLUTION-TTYTDD" } }
	// Also we add the general ones here
      };

      // Make sure these strings are the same as the above
      string[] standard = {
	"Mobile", "Work", "Home"
      };

      if (_phone == null) {
	_phone = new TypeSet ();
	for (int i = 0; i < data.length; i++)
	  _phone.add_init_data (&data[i]);
	for (int i = 0; i < general_data.length; i++)
	  _phone.add_init_data (&general_data[i]);
	_phone.add_init_data_done (standard);
      }

      return _phone;
    }
  }
}

public class Contacts.TypeCombo : Grid  {
  TypeSet type_set;
  ComboBox combo;
  Entry entry;
  TreeIter last_active;
  bool custom_mode;
  bool in_manual_change;
  public bool modified;

  public signal void changed ();

  public TypeCombo (TypeSet type_set) {
    this.type_set = type_set;

    combo = new ComboBox.with_model (type_set.store);
    combo.set_halign (Align.FILL);
    combo.set_hexpand (true);
    this.add (combo);

    var renderer = new CellRendererText ();
    combo.pack_start (renderer, true);
    combo.set_attributes (renderer,
			  "text", 0);
    combo.set_row_separator_func ( (model, iter) => {
	string? s;
	model.get (iter, 0, out s);
	return s == null;
      });

    entry = new Entry ();
    entry.set_halign (Align.FILL);
    entry.set_hexpand (true);
    // Make the default entry small so we don't unnecessarily
    // expand the labels (it'll be expanded a bit anyway)
    entry.width_chars = 4;

    this.add (entry);

    combo.set_no_show_all (true);
    entry.set_no_show_all (true);

    combo.show ();

    combo.changed.connect (combo_changed);
    entry.focus_out_event.connect (entry_focus_out_event);
    entry.activate.connect (entry_activate);
    entry.key_release_event.connect (entry_key_release);
  }

  private void finish_custom () {
    if (!custom_mode)
      return;

    custom_mode = false;
    var text = entry.get_text ();

    if (text != "") {
      TreeIter iter;
      type_set.add_custom_label (text, out iter);

      last_active = iter;
      combo.set_active_iter (iter);
    } else {
      combo.set_active_iter (last_active);
    }

    combo.show ();
    entry.hide ();
  }

  private void entry_activate () {
    finish_custom ();
  }

  private bool entry_key_release (Gdk.EventKey event) {
    if (event.keyval == Gdk.Key.Escape) {
      entry.set_text ("");
      finish_custom ();
    }
    return true;
  }

  private bool entry_focus_out_event (Gdk.EventFocus event) {
    finish_custom ();
    return false;
  }

  private void combo_changed (ComboBox combo) {
    if (in_manual_change)
      return;

    modified = true;
    TreeIter iter;
    if (combo.get_active_iter (out iter)) {
      if (type_set.is_custom (iter)) {
	custom_mode = true;
	entry.show ();
	entry.grab_focus ();
	combo.hide ();
      } else {
	last_active = iter;
	this.changed ();
      }
    }
  }

  private void set_from_iter (TreeIter iter) {
    in_manual_change = true;
    last_active = iter;
    combo.set_active_iter (iter);
    in_manual_change = false;
    modified = false;
  }

  public void set_active (AbstractFieldDetails details) {
    TreeIter iter;
    type_set.lookup_type (details, out iter);
    set_from_iter (iter);
  }

  public void set_to (string type) {
    TreeIter iter;
    type_set.lookup_type_by_string (type, out iter);
    set_from_iter (iter);
  }

  public void update_details (AbstractFieldDetails details) {
    TreeIter iter;
    combo.get_active_iter (out iter);
    type_set.update_details (details, iter);
  }
}