~ubuntu-branches/ubuntu/trusty/gnome-contacts/trusty

« back to all changes in this revision

Viewing changes to .pc/01-birthday_icon.patch/src/contacts-contact-pane.vala

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-08-21 17:36:56 UTC
  • mfrom: (1.3.3)
  • Revision ID: package-import@ubuntu.com-20120821173656-3l5eenoi2wemd3a4
Tags: 3.5.90-0ubuntu1
* New upstream release
* debian/patches/missing_vapi.patch:
  - Add missing file
* debian/patches/01-birthday_icon.patch
  - Applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
2
 
/*
3
 
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
using Gtk;
20
 
using Folks;
21
 
using Gee;
22
 
 
23
 
public class Contacts.FieldRow : Contacts.Row {
24
 
  Clickable clickable;
25
 
  int start;
26
 
  bool has_child_focus;
27
 
  ContactPane pane;
28
 
 
29
 
  /* show_as_editable means we prelight, can_focus, show selected, etc.
30
 
     It doesn't mean we can't edit the row. For instance the
31
 
     Card row is editing when we're editing the full name, but
32
 
     thats not represented in the UI as editing the row. */
33
 
  protected bool show_as_editable;
34
 
  protected bool is_editing;
35
 
 
36
 
  public FieldRow(RowGroup group, ContactPane pane) {
37
 
    base (group);
38
 
    this.pane = pane;
39
 
    set_redraw_on_allocate (true); // Since we draw the focus rect
40
 
 
41
 
    this.button_press_event.connect ( (ev) => {
42
 
        if (!is_editing)
43
 
          this.pane.exit_edit_mode (true);
44
 
        return false;
45
 
      });
46
 
 
47
 
    clickable = new Clickable (this);
48
 
    clickable.set_focus_on_click (true);
49
 
    clickable.clicked.connect ( () => { this.clicked (); } );
50
 
    start = 0;
51
 
 
52
 
    /* This should really be in class construct, but that doesn't seem to work... */
53
 
    activate_signal = GLib.Signal.lookup ("activate-row", typeof (FieldRow));
54
 
  }
55
 
 
56
 
  public void set_editing (bool val) {
57
 
    is_editing = val;
58
 
  }
59
 
 
60
 
  public void reset () {
61
 
    start = 0;
62
 
  }
63
 
 
64
 
  public signal void clicked ();
65
 
 
66
 
  public override bool focus (DirectionType direction) {
67
 
    var row_can_focus = get_can_focus ();
68
 
 
69
 
    /* Non-focusable rows get the standard behvaiour */
70
 
    if (!row_can_focus)
71
 
      return base.focus (direction);
72
 
 
73
 
    /* Focusable rows have to also support focusable children,
74
 
       which is not supported by Container.focus(), so we
75
 
       work around that. */
76
 
 
77
 
    bool res = false;
78
 
 
79
 
    bool recurse_into = false;
80
 
    if (has_focus) {
81
 
      switch (direction) {
82
 
      case DirectionType.RIGHT:
83
 
      case DirectionType.TAB_FORWARD:
84
 
        recurse_into = true;
85
 
        break;
86
 
      }
87
 
    } else if (this.get_focus_child () != null) {
88
 
      recurse_into = true;
89
 
    } else {
90
 
      switch (direction) {
91
 
      case DirectionType.LEFT:
92
 
      case DirectionType.TAB_BACKWARD:
93
 
        recurse_into = true;
94
 
        break;
95
 
      }
96
 
    }
97
 
 
98
 
    if (recurse_into) {
99
 
      set_can_focus (false);
100
 
      res = base.focus (direction);
101
 
      set_can_focus (true);
102
 
 
103
 
      if (!res && !has_focus) {
104
 
        switch (direction) {
105
 
        case DirectionType.LEFT:
106
 
        case DirectionType.TAB_BACKWARD:
107
 
          this.grab_focus ();
108
 
          res = true;
109
 
          break;
110
 
        }
111
 
      }
112
 
    } else {
113
 
      if (!has_focus) {
114
 
        this.grab_focus ();
115
 
        res = true;
116
 
      }
117
 
    }
118
 
 
119
 
    return res;
120
 
  }
121
 
 
122
 
  [CCode (action_signal = true)]
123
 
  public virtual signal void activate_row () {
124
 
    clickable.activate ();
125
 
  }
126
 
 
127
 
  public override void realize () {
128
 
    base.realize ();
129
 
    clickable.realize_for (event_window);
130
 
  }
131
 
 
132
 
  public override void unrealize () {
133
 
    base.unrealize ();
134
 
    clickable.unrealize ();
135
 
  }
136
 
 
137
 
  public override bool draw (Cairo.Context cr) {
138
 
    Allocation allocation;
139
 
    this.get_allocation (out allocation);
140
 
 
141
 
    var context = this.get_style_context ();
142
 
 
143
 
    context.save ();
144
 
    StateFlags state = 0;
145
 
    if (show_as_editable) {
146
 
      state = clickable.state & (StateFlags.ACTIVE | StateFlags.PRELIGHT);
147
 
      if (is_editing)
148
 
        state |= StateFlags.SELECTED;
149
 
    }
150
 
    context.set_state (state);
151
 
    if (state != 0)
152
 
      context.render_background (cr,
153
 
                                 0, 0, allocation.width, allocation.height);
154
 
 
155
 
    if (this.has_visible_focus ())
156
 
      context.render_focus (cr, 0, 0, allocation.width, allocation.height);
157
 
 
158
 
    context.restore ();
159
 
 
160
 
    base.draw (cr);
161
 
 
162
 
    return true;
163
 
  }
164
 
 
165
 
  public override void parent_set (Widget? old_parent) {
166
 
    if (old_parent != null) {
167
 
      var old_parent_container = (old_parent as Container);
168
 
      old_parent_container.set_focus_child.disconnect (parent_set_focus_child);
169
 
    }
170
 
 
171
 
    var parent_container = (this.get_parent () as Container);
172
 
    has_child_focus = parent_container != null && parent_container.get_focus_child () == this;
173
 
    if (parent_container != null)
174
 
      parent_container.set_focus_child.connect (parent_set_focus_child);
175
 
  }
176
 
 
177
 
  public virtual signal void lost_child_focus () {
178
 
  }
179
 
 
180
 
  public void parent_set_focus_child (Container container, Widget? widget) {
181
 
    var old_has_child_focus = has_child_focus;
182
 
    has_child_focus = widget == this;
183
 
 
184
 
    if (old_has_child_focus && !has_child_focus) {
185
 
          Idle.add(() => {
186
 
              if (!has_child_focus)
187
 
                lost_child_focus ();
188
 
              return false;
189
 
            });
190
 
    }
191
 
  }
192
 
 
193
 
  public void pack (Widget w) {
194
 
    this.attach (w, 1, start++);
195
 
  }
196
 
 
197
 
  public void pack_label (string s) {
198
 
    var l = new Label (s);
199
 
    l.set_halign (Align.START);
200
 
    l.get_style_context ().add_class ("dim-label");
201
 
    pack (l);
202
 
  }
203
 
 
204
 
  public void pack_header (string s) {
205
 
    var l = new Label (s);
206
 
    l.set_markup (
207
 
      Markup.printf_escaped ("<span font='24px'>%s</span>", s));
208
 
    l.set_halign (Align.START);
209
 
    pack (l);
210
 
  }
211
 
 
212
 
  public Grid pack_header_in_grid (string s, out Label label) {
213
 
    var grid = new Grid ();
214
 
    grid.set_column_spacing (4);
215
 
    var l = new Label (s);
216
 
    label = l;
217
 
    l.set_markup (
218
 
      Markup.printf_escaped ("<span font='24px'>%s</span>", s));
219
 
    l.set_halign (Align.START);
220
 
    l.set_hexpand (true);
221
 
 
222
 
    grid.set_halign (Align.FILL);
223
 
    grid.add (l);
224
 
 
225
 
    pack (grid);
226
 
 
227
 
    return grid;
228
 
  }
229
 
 
230
 
  public Label pack_text (bool wrap = false) {
231
 
    var l = new Label ("");
232
 
    if (wrap) {
233
 
      l.set_line_wrap (true);
234
 
      l.set_line_wrap_mode (Pango.WrapMode.WORD_CHAR);
235
 
    } else {
236
 
      l.set_ellipsize (Pango.EllipsizeMode.END);
237
 
    }
238
 
    l.set_halign (Align.START);
239
 
    pack (l);
240
 
    return l;
241
 
  }
242
 
 
243
 
  public void pack_text_detail (out Label text_label, out Label detail_label, bool wrap = false) {
244
 
    var grid = new Grid ();
245
 
 
246
 
    var l = new Label ("");
247
 
    l.set_hexpand (true);
248
 
    l.set_halign (Align.START);
249
 
    if (wrap) {
250
 
      l.set_line_wrap (true);
251
 
      l.set_line_wrap_mode (Pango.WrapMode.WORD_CHAR);
252
 
    } else {
253
 
      l.set_ellipsize (Pango.EllipsizeMode.END);
254
 
    }
255
 
    grid.add (l);
256
 
 
257
 
    text_label = l;
258
 
 
259
 
    l = new Label ("");
260
 
    l.set_halign (Align.END);
261
 
    l.get_style_context ().add_class ("dim-label");
262
 
    detail_label = l;
263
 
 
264
 
    grid.set_halign (Align.FILL);
265
 
    grid.add (l);
266
 
 
267
 
    pack (grid);
268
 
  }
269
 
 
270
 
  public void pack_widget_detail_combo (Widget w, AbstractFieldDetails detail, TypeSet type_set, out TypeCombo combo) {
271
 
    var grid = new Grid ();
272
 
    grid.set_column_spacing (16);
273
 
 
274
 
    grid.add (w);
275
 
 
276
 
    combo = new TypeCombo (type_set);
277
 
    combo.set_hexpand (false);
278
 
    combo.set_halign (Align.END);
279
 
    combo.set_active (detail);
280
 
 
281
 
    grid.set_halign (Align.FILL);
282
 
    grid.add (combo);
283
 
 
284
 
    pack (grid);
285
 
  }
286
 
 
287
 
 
288
 
  public void pack_entry_detail_combo (string text, AbstractFieldDetails detail, TypeSet type_set, out Entry entry, out TypeCombo combo) {
289
 
    entry = new Entry ();
290
 
    entry.get_style_context ().add_class ("contacts-entry");
291
 
    entry.set_text (text);
292
 
    entry.set_hexpand (true);
293
 
    entry.set_halign (Align.FILL);
294
 
 
295
 
    pack_widget_detail_combo (entry, detail, type_set, out combo);
296
 
  }
297
 
 
298
 
  public Entry pack_entry (string s) {
299
 
    var e = new Entry ();
300
 
    e.get_style_context ().add_class ("contacts-entry");
301
 
    e.set_text (s);
302
 
    e.set_halign (Align.FILL);
303
 
    pack (e);
304
 
    return e;
305
 
  }
306
 
 
307
 
  public void left_add (Widget widget) {
308
 
    this.attach (widget, 0, 0);
309
 
    widget.set_halign (Align.END);
310
 
  }
311
 
 
312
 
  public void right_add (Widget widget) {
313
 
    this.attach (widget, 2, 0);
314
 
    widget.set_halign (Align.START);
315
 
  }
316
 
 
317
 
  public Button pack_delete_button () {
318
 
    var image = new Image.from_icon_name ("user-trash-symbolic", IconSize.MENU);
319
 
    var b = new Button();
320
 
    b.add (image);
321
 
    right_add (b);
322
 
    b.set_halign (Align.CENTER);
323
 
    return b;
324
 
  }
325
 
 
326
 
 
327
 
  public virtual signal bool enter_edit_mode () {
328
 
    return false;
329
 
  }
330
 
 
331
 
  public virtual signal void exit_edit_mode (bool save) {
332
 
  }
333
 
}
334
 
 
335
 
public abstract class Contacts.FieldSet : Grid {
336
 
  public class string label_name;
337
 
  public class string detail_name;
338
 
  public class string property_name;
339
 
  public class bool is_single_value;
340
 
 
341
 
  public PersonaSheet sheet { get; construct; }
342
 
  public int row_nr { get; construct; }
343
 
  public bool added;
344
 
  public bool saving;
345
 
  FieldRow label_row;
346
 
  protected ArrayList<DataFieldRow> data_rows = new ArrayList<DataFieldRow>();
347
 
 
348
 
  public abstract void populate ();
349
 
  public abstract DataFieldRow new_field ();
350
 
 
351
 
  construct {
352
 
    this.set_orientation (Orientation.VERTICAL);
353
 
 
354
 
    label_row = new FieldRow (sheet.pane.row_group, sheet.pane);
355
 
    this.add (label_row);
356
 
    label_row.pack_label (label_name);
357
 
  }
358
 
 
359
 
  public void add_to_sheet () {
360
 
    if (!added) {
361
 
      sheet.attach (this, 0, row_nr, 1, 1);
362
 
      added = true;
363
 
    }
364
 
  }
365
 
 
366
 
  public void remove_from_sheet () {
367
 
    if (added) {
368
 
      sheet.remove (this);
369
 
      added = false;
370
 
    }
371
 
  }
372
 
 
373
 
  public bool reads_param (string param) {
374
 
    return param == property_name;
375
 
  }
376
 
 
377
 
  public bool is_empty () {
378
 
    return get_children ().length () == 1;
379
 
  }
380
 
 
381
 
  public void clear () {
382
 
    foreach (var row in data_rows) {
383
 
      row.destroy ();
384
 
    }
385
 
    data_rows.clear ();
386
 
  }
387
 
 
388
 
  public void add_row (DataFieldRow row) {
389
 
    this.add (row);
390
 
    data_rows.add (row);
391
 
 
392
 
    row.clicked.connect( () => {
393
 
        sheet.pane.enter_edit_mode (row);
394
 
      });
395
 
 
396
 
    row.update ();
397
 
  }
398
 
 
399
 
  public void remove_row (DataFieldRow row) {
400
 
    this.remove (row);
401
 
    data_rows.remove (row);
402
 
  }
403
 
 
404
 
  public virtual Value? get_value () {
405
 
    return null;
406
 
  }
407
 
 
408
 
  public void save () {
409
 
    var value = get_value ();
410
 
    if (value == null)
411
 
      warning ("Unimplemented get_value()");
412
 
    else {
413
 
      saving = true;
414
 
      sheet.pane.contact.set_persona_property.begin (sheet.persona, property_name, value,
415
 
                                                     (obj, result) => {
416
 
          try {
417
 
            var contact = obj as Contact;
418
 
            saving = false;
419
 
            contact.set_persona_property.end (result);
420
 
          } catch (Error e2) {
421
 
            App.app.show_message (e2.message);
422
 
            refresh_from_persona ();
423
 
          }
424
 
                                                     });
425
 
    }
426
 
  }
427
 
 
428
 
  public void refresh_from_persona () {
429
 
    this.clear ();
430
 
    this.populate ();
431
 
 
432
 
    if (this.is_empty ())
433
 
      this.remove_from_sheet ();
434
 
    else {
435
 
      this.show_all ();
436
 
      this.add_to_sheet ();
437
 
    }
438
 
  }
439
 
}
440
 
 
441
 
public abstract class Contacts.DataFieldRow : FieldRow {
442
 
  public FieldSet field_set;
443
 
  protected Button? delete_button;
444
 
 
445
 
  public DataFieldRow (FieldSet field_set) {
446
 
    base (field_set.sheet.pane.row_group, field_set.sheet.pane);
447
 
    bool editable =
448
 
      Contact.persona_has_writable_property (field_set.sheet.persona,
449
 
                                             field_set.property_name);
450
 
    set_editable (editable);
451
 
    this.field_set = field_set;
452
 
  }
453
 
 
454
 
  public void set_editable (bool editable) {
455
 
    this.show_as_editable = editable;
456
 
    set_can_focus (editable);
457
 
  }
458
 
 
459
 
  public abstract void update ();
460
 
  public virtual void pack_edit_widgets () {
461
 
  }
462
 
  public virtual bool finish_edit_widgets (bool save) {
463
 
    return false;
464
 
  }
465
 
 
466
 
  public override bool enter_edit_mode () {
467
 
    if (!show_as_editable)
468
 
      return false;
469
 
 
470
 
    this.set_can_focus (false);
471
 
    foreach (var w in this.get_children ()) {
472
 
      w.hide ();
473
 
      w.set_data ("original-widget", true);
474
 
    }
475
 
 
476
 
    this.reset ();
477
 
    delete_button = this.pack_delete_button ();
478
 
    delete_button.clicked.connect ( () => {
479
 
        field_set.remove_row (this);
480
 
        field_set.save ();
481
 
      });
482
 
 
483
 
    this.pack_edit_widgets ();
484
 
 
485
 
    foreach (var w in this.get_children ()) {
486
 
      if (!w.get_data<bool> ("original-widget"))
487
 
        w.show_all ();
488
 
    }
489
 
 
490
 
    return true;
491
 
  }
492
 
 
493
 
  public override void lost_child_focus () {
494
 
    if (field_set.sheet.pane.editing_row == this)
495
 
      field_set.sheet.pane.exit_edit_mode (true);
496
 
  }
497
 
 
498
 
  public override void exit_edit_mode (bool save) {
499
 
    if (!show_as_editable)
500
 
      return;
501
 
 
502
 
    var had_child_focus = this.get_focus_child () != null;
503
 
 
504
 
    var changed = finish_edit_widgets (save);
505
 
 
506
 
    delete_button = null;
507
 
    foreach (var w in this.get_children ()) {
508
 
      if (!w.get_data<bool> ("original-widget"))
509
 
        w.destroy ();
510
 
    }
511
 
 
512
 
    update ();
513
 
    this.show_all ();
514
 
    this.set_can_focus (true);
515
 
    if (had_child_focus)
516
 
      this.grab_focus ();
517
 
 
518
 
    if (save && changed)
519
 
      field_set.save ();
520
 
  }
521
 
 
522
 
  public void setup_entry_for_edit (Entry entry, bool grab_focus = true) {
523
 
    if (grab_focus) {
524
 
      Utils.grab_widget_later (entry);
525
 
    }
526
 
    entry.activate.connect_after ( () => {
527
 
        field_set.sheet.pane.exit_edit_mode (true);
528
 
      });
529
 
    entry.key_press_event.connect ( (key_event) => {
530
 
        if (key_event.keyval == Gdk.Key.Escape) {
531
 
          field_set.sheet.pane.exit_edit_mode (false);
532
 
        }
533
 
        return false;
534
 
      });
535
 
  }
536
 
 
537
 
  public void setup_text_view_for_edit (TextView text, bool grab_focus = true) {
538
 
    if (grab_focus) {
539
 
      Utils.grab_widget_later (text);
540
 
    }
541
 
    text.key_press_event.connect ( (key_event) => {
542
 
        if (key_event.keyval == Gdk.Key.Escape) {
543
 
          field_set.sheet.pane.exit_edit_mode (false);
544
 
        }
545
 
        return false;
546
 
      });
547
 
  }
548
 
}
549
 
 
550
 
class Contacts.LinkFieldRow : DataFieldRow {
551
 
  public UrlFieldDetails details;
552
 
  Label text_label;
553
 
  LinkButton uri_button;
554
 
  Entry? entry;
555
 
 
556
 
  public LinkFieldRow (FieldSet field_set, UrlFieldDetails details) {
557
 
    base (field_set);
558
 
    this.details = details;
559
 
 
560
 
    text_label = this.pack_text ();
561
 
    var image = new Image.from_icon_name ("web-browser-symbolic", IconSize.MENU);
562
 
    image.get_style_context ().add_class ("dim-label");
563
 
    uri_button = new LinkButton("");
564
 
    uri_button.remove (uri_button.get_child ());
565
 
    uri_button.set_relief (ReliefStyle.NONE);
566
 
    uri_button.add (image);
567
 
    this.right_add (uri_button);
568
 
  }
569
 
 
570
 
  public override void update () {
571
 
    text_label.set_text (Contact.format_uri_link_text (details));
572
 
    uri_button.set_uri (details.value);
573
 
  }
574
 
 
575
 
  public override void pack_edit_widgets () {
576
 
    entry = this.pack_entry (details.value);
577
 
    setup_entry_for_edit (entry);
578
 
  }
579
 
 
580
 
  public override bool finish_edit_widgets (bool save) {
581
 
    var old_details = details;
582
 
    var changed = entry.get_text () != details.value;
583
 
    if (save && changed)
584
 
      details = new UrlFieldDetails (entry.get_text (), old_details.parameters);
585
 
    entry = null;
586
 
    return changed;
587
 
  }
588
 
}
589
 
 
590
 
class Contacts.LinkFieldSet : FieldSet {
591
 
  class construct {
592
 
    label_name = C_("Addresses on the Web", "Links");
593
 
    detail_name = C_("Web address", "Link");
594
 
    property_name = "urls";
595
 
  }
596
 
 
597
 
  public override void populate () {
598
 
    var details = sheet.persona as UrlDetails;
599
 
    if (details == null)
600
 
      return;
601
 
 
602
 
    var urls = details.urls;
603
 
    foreach (var url_details in urls) {
604
 
      var row = new LinkFieldRow (this, url_details);
605
 
      add_row (row);
606
 
    }
607
 
  }
608
 
 
609
 
  public override DataFieldRow new_field () {
610
 
    var row = new LinkFieldRow (this, new UrlFieldDetails (""));
611
 
    add_row (row);
612
 
    return row;
613
 
  }
614
 
 
615
 
  public override Value? get_value () {
616
 
    var details = sheet.persona as UrlDetails;
617
 
    if (details == null)
618
 
      return null;
619
 
 
620
 
    var new_details = new HashSet<UrlFieldDetails>();
621
 
    foreach (var row in data_rows) {
622
 
      var link_row = row as LinkFieldRow;
623
 
      new_details.add (link_row.details);
624
 
    }
625
 
 
626
 
    var value = Value(new_details.get_type ());
627
 
    value.set_object (new_details);
628
 
 
629
 
    return value;
630
 
  }
631
 
}
632
 
 
633
 
class Contacts.DetailedFieldRow<T> : DataFieldRow {
634
 
  public AbstractFieldDetails<string> _details;
635
 
  TypeSet type_set;
636
 
  Label text_label;
637
 
  Label detail_label;
638
 
  Entry? entry;
639
 
  TypeCombo? combo;
640
 
 
641
 
  public delegate AbstractFieldDetails<string> DataCreate(string s);
642
 
  DataCreate data_create;
643
 
 
644
 
  public T details { get { return (T)_details; } }
645
 
 
646
 
  public DetailedFieldRow (FieldSet field_set, AbstractFieldDetails<string> details, TypeSet type_set, owned DataCreate data_create) {
647
 
    base (field_set);
648
 
    this._details = details;
649
 
    this.type_set = type_set;
650
 
    this.data_create = (owned) data_create;
651
 
    this.pack_text_detail (out text_label, out detail_label);
652
 
  }
653
 
 
654
 
  public override void update () {
655
 
    text_label.set_text (_details.value);
656
 
    detail_label.set_text (type_set.format_type (_details));
657
 
  }
658
 
 
659
 
  public override void pack_edit_widgets () {
660
 
    this.pack_entry_detail_combo (_details.value, _details, type_set, out entry, out combo);
661
 
    setup_entry_for_edit (entry);
662
 
  }
663
 
 
664
 
  public override bool finish_edit_widgets (bool save) {
665
 
    var old_details = _details;
666
 
    bool changed = _details.value != entry.get_text () || combo.modified;
667
 
    if (save && changed) {
668
 
      _details = data_create (entry.get_text ());
669
 
      _details.parameters = old_details.parameters;
670
 
      combo.update_details (_details);
671
 
    }
672
 
    entry = null;
673
 
    combo = null;
674
 
    return changed;
675
 
  }
676
 
}
677
 
 
678
 
class Contacts.EmailFieldSet : FieldSet {
679
 
  class construct {
680
 
    label_name = _("Email");
681
 
    detail_name = _("Email");
682
 
    property_name = "email-addresses";
683
 
  }
684
 
 
685
 
  public override void populate () {
686
 
    var details = sheet.persona as EmailDetails;
687
 
    if (details == null)
688
 
      return;
689
 
    var emails = Contact.sort_fields<EmailFieldDetails>(details.email_addresses);
690
 
    foreach (var email in emails) {
691
 
      var row = new DetailedFieldRow<EmailFieldDetails> (this, email,TypeSet.general, (s) => { return new EmailFieldDetails (s); } );
692
 
      add_row (row);
693
 
    }
694
 
  }
695
 
 
696
 
  public override DataFieldRow new_field () {
697
 
    var row = new DetailedFieldRow<EmailFieldDetails> (this, new EmailFieldDetails("") ,TypeSet.general, (s) => { return new EmailFieldDetails (s); } );
698
 
    add_row (row);
699
 
    return row;
700
 
  }
701
 
 
702
 
  public override Value? get_value () {
703
 
    var details = sheet.persona as EmailDetails;
704
 
    if (details == null)
705
 
      return null;
706
 
 
707
 
    var new_details = new HashSet<EmailFieldDetails>();
708
 
    foreach (var row in data_rows) {
709
 
      var email_row = row as DetailedFieldRow<EmailFieldDetails>;
710
 
      new_details.add (email_row.details);
711
 
    }
712
 
 
713
 
    var value = Value(new_details.get_type ());
714
 
    value.set_object (new_details);
715
 
 
716
 
    return value;
717
 
  }
718
 
}
719
 
 
720
 
class Contacts.PhoneFieldSet : FieldSet {
721
 
  class construct {
722
 
    label_name = _("Phone");
723
 
    detail_name = _("Phone number");
724
 
    property_name = "phone-numbers";
725
 
  }
726
 
  public override void populate () {
727
 
    var details = sheet.persona as PhoneDetails;
728
 
    if (details == null)
729
 
      return;
730
 
    var phone_numbers = Contact.sort_fields<PhoneFieldDetails>(details.phone_numbers);
731
 
    foreach (var phone in phone_numbers) {
732
 
      var row = new DetailedFieldRow<PhoneFieldDetails> (this, phone,TypeSet.phone, (s) => { return new PhoneFieldDetails (s);} );
733
 
      add_row (row);
734
 
    }
735
 
  }
736
 
 
737
 
  public override DataFieldRow new_field () {
738
 
    var row = new DetailedFieldRow<PhoneFieldDetails> (this, new PhoneFieldDetails("") ,TypeSet.phone, (s) => { return new EmailFieldDetails (s); } );
739
 
    add_row (row);
740
 
    return row;
741
 
  }
742
 
 
743
 
  public override Value? get_value () {
744
 
    var details = sheet.persona as PhoneDetails;
745
 
    if (details == null)
746
 
      return null;
747
 
 
748
 
    var new_details = new HashSet<PhoneFieldDetails>();
749
 
    foreach (var row in data_rows) {
750
 
      var phone_row = row as DetailedFieldRow<PhoneFieldDetails>;
751
 
      new_details.add (phone_row.details);
752
 
    }
753
 
 
754
 
    var value = Value(new_details.get_type ());
755
 
    value.set_object (new_details);
756
 
 
757
 
    return value;
758
 
  }
759
 
}
760
 
 
761
 
class Contacts.ChatFieldRow : DataFieldRow {
762
 
  string protocol;
763
 
  ImFieldDetails details;
764
 
 
765
 
  Label text_label;
766
 
 
767
 
  public ChatFieldRow (FieldSet field_set, string protocol, ImFieldDetails details) {
768
 
    base (field_set);
769
 
    this.protocol = protocol;
770
 
    this.details = details;
771
 
    text_label = this.pack_text ();
772
 
    this.set_editable (false);
773
 
  }
774
 
 
775
 
  public override void update () {
776
 
    var im_persona = field_set.sheet.persona as Tpf.Persona;
777
 
    text_label.set_text (Contact.format_im_name (im_persona, protocol, details.value));
778
 
  }
779
 
}
780
 
 
781
 
class Contacts.ChatFieldSet : FieldSet {
782
 
  class construct {
783
 
    label_name = _("Chat");
784
 
    detail_name = _("Chat");
785
 
    property_name = "im-addresses";
786
 
  }
787
 
  public override void populate () {
788
 
    var details = sheet.persona as ImDetails;
789
 
    if (details == null)
790
 
      return;
791
 
    foreach (var protocol in details.im_addresses.get_keys ()) {
792
 
      foreach (var id in details.im_addresses[protocol]) {
793
 
        if (sheet.persona is Tpf.Persona) {
794
 
          var row = new ChatFieldRow (this, protocol, id);
795
 
          add_row (row);
796
 
        }
797
 
      }
798
 
    }
799
 
  }
800
 
 
801
 
  public override DataFieldRow new_field () {
802
 
    var row = new ChatFieldRow (this, "", new ImFieldDetails (""));
803
 
    add_row (row);
804
 
    return row;
805
 
  }
806
 
}
807
 
 
808
 
class Contacts.BirthdayFieldRow : DataFieldRow {
809
 
  public DateTime details;
810
 
  Label text_label;
811
 
  SpinButton? day_spin;
812
 
  SpinButton? year_spin;
813
 
  ComboBoxText? combo;
814
 
 
815
 
  public BirthdayFieldRow (FieldSet field_set, DateTime details) {
816
 
    base (field_set);
817
 
    this.details = details;
818
 
 
819
 
    text_label = this.pack_text ();
820
 
    var image = new Image.from_icon_name ("preferences-system-date-and-time-symbolic", IconSize.MENU);
821
 
    image.get_style_context ().add_class ("dim-label");
822
 
    var button = new Button();
823
 
    button.set_relief (ReliefStyle.NONE);
824
 
    button.add (image);
825
 
    this.right_add (button);
826
 
    button.clicked.connect ( () => {
827
 
        Utils.show_calendar (details);
828
 
      });
829
 
  }
830
 
 
831
 
  public override void update () {
832
 
    text_label.set_text (details.to_local ().format ("%x"));
833
 
  }
834
 
 
835
 
  public override void pack_edit_widgets () {
836
 
    var bday = details.to_local ();
837
 
    var grid = new Grid ();
838
 
    grid.set_column_spacing (16);
839
 
 
840
 
    day_spin = new SpinButton.with_range (0, 31, 1);
841
 
    day_spin.set_digits (0);
842
 
    day_spin.numeric = true;
843
 
    day_spin.set_value ((double)bday.get_day_of_month ());
844
 
    grid.add (day_spin);
845
 
 
846
 
    setup_entry_for_edit (day_spin);
847
 
 
848
 
    combo = new ComboBoxText ();
849
 
    combo.append_text (_("January"));
850
 
    combo.append_text (_("February"));
851
 
    combo.append_text (_("March"));
852
 
    combo.append_text (_("April"));
853
 
    combo.append_text (_("May"));
854
 
    combo.append_text (_("June"));
855
 
    combo.append_text (_("July"));
856
 
    combo.append_text (_("August"));
857
 
    combo.append_text (_("September"));
858
 
    combo.append_text (_("October"));
859
 
    combo.append_text (_("November"));
860
 
    combo.append_text (_("December"));
861
 
    combo.set_active (bday.get_month () - 1);
862
 
    combo.get_style_context ().add_class ("contacts-combo");
863
 
    grid.add (combo);
864
 
 
865
 
    year_spin = new SpinButton.with_range (1800, 3000, 1);
866
 
    year_spin.set_digits (0);
867
 
    year_spin.numeric = true;
868
 
    year_spin.set_value ((double)bday.get_year ());
869
 
    grid.add (year_spin);
870
 
 
871
 
    setup_entry_for_edit (year_spin, false);
872
 
 
873
 
    pack (grid);
874
 
  }
875
 
 
876
 
  public override bool finish_edit_widgets (bool save) {
877
 
    var old_details = details;
878
 
 
879
 
    var bday = new DateTime.local ((int)year_spin.get_value (),
880
 
                                   combo.get_active () + 1,
881
 
                                   (int)day_spin.get_value (),
882
 
                                   0, 0, 0);
883
 
    bday = bday.to_utc ();
884
 
 
885
 
    var changed = !bday.equal (old_details);
886
 
    if (save && changed)
887
 
      details = bday;
888
 
 
889
 
    combo = null;
890
 
    day_spin = null;
891
 
    year_spin = null;
892
 
    return changed;
893
 
  }
894
 
}
895
 
 
896
 
class Contacts.BirthdayFieldSet : FieldSet {
897
 
  class construct {
898
 
    label_name = _("Birthday");
899
 
    detail_name = _("Birthday");
900
 
    property_name = "birthday";
901
 
    is_single_value = true;
902
 
  }
903
 
  public override void populate () {
904
 
    var details = sheet.persona as BirthdayDetails;
905
 
    if (details == null)
906
 
      return;
907
 
 
908
 
    DateTime? bday = details.birthday;
909
 
    if (bday != null) {
910
 
      var row = new BirthdayFieldRow (this, bday);
911
 
      add_row (row);
912
 
    }
913
 
  }
914
 
 
915
 
  public override DataFieldRow new_field () {
916
 
    var row = new BirthdayFieldRow (this, new DateTime.now_utc ());
917
 
    add_row (row);
918
 
    return row;
919
 
  }
920
 
 
921
 
  public override Value? get_value () {
922
 
    var details = sheet.persona as BirthdayDetails;
923
 
    if (details == null)
924
 
      return null;
925
 
 
926
 
    DateTime? new_details = null;
927
 
    foreach (var row in data_rows) {
928
 
      var bday_row = row as BirthdayFieldRow;
929
 
      new_details = bday_row.details;
930
 
    }
931
 
 
932
 
    var value = Value(typeof (DateTime));
933
 
    value.set_boxed (new_details);
934
 
 
935
 
    return value;
936
 
  }
937
 
}
938
 
 
939
 
class Contacts.StringFieldRow : DataFieldRow {
940
 
  public string value;
941
 
  Label text_label;
942
 
  Entry? entry;
943
 
 
944
 
  public StringFieldRow (FieldSet field_set, string value) {
945
 
    base (field_set);
946
 
    this.value = value;
947
 
 
948
 
    text_label = this.pack_text ();
949
 
  }
950
 
 
951
 
  public override void update () {
952
 
    text_label.set_text (value);
953
 
  }
954
 
 
955
 
  public override void pack_edit_widgets () {
956
 
    entry = this.pack_entry (value);
957
 
    setup_entry_for_edit (entry);
958
 
  }
959
 
 
960
 
  public override bool finish_edit_widgets (bool save) {
961
 
    var changed = entry.get_text () != value;
962
 
    if (save && changed)
963
 
      value = entry.get_text ();
964
 
    entry = null;
965
 
    return changed;
966
 
  }
967
 
}
968
 
 
969
 
class Contacts.NicknameFieldSet : FieldSet {
970
 
  class construct {
971
 
    label_name = _("Nickname");
972
 
    detail_name = _("Nickname");
973
 
    property_name = "nickname";
974
 
    is_single_value = true;
975
 
  }
976
 
  public override void populate () {
977
 
    var details = sheet.persona as NameDetails;
978
 
    if (details == null)
979
 
      return;
980
 
 
981
 
    if (is_set (details.nickname)) {
982
 
      var row = new StringFieldRow (this, details.nickname);
983
 
      add_row (row);
984
 
    }
985
 
  }
986
 
 
987
 
  public override DataFieldRow new_field () {
988
 
    var row = new StringFieldRow (this, "");
989
 
    add_row (row);
990
 
    return row;
991
 
  }
992
 
 
993
 
  public override Value? get_value () {
994
 
    var details = sheet.persona as NameDetails;
995
 
    if (details == null)
996
 
      return null;
997
 
 
998
 
    var value = Value(typeof (string));
999
 
    value.set_string ("");
1000
 
    foreach (var row in data_rows) {
1001
 
      var string_row = row as StringFieldRow;
1002
 
      value.set_string (string_row.value);
1003
 
    }
1004
 
 
1005
 
    return value;
1006
 
  }
1007
 
}
1008
 
 
1009
 
class Contacts.NoteFieldRow : DataFieldRow {
1010
 
  public NoteFieldDetails details;
1011
 
  Label text_label;
1012
 
  TextView? text;
1013
 
 
1014
 
  public NoteFieldRow (FieldSet field_set, NoteFieldDetails details) {
1015
 
    base (field_set);
1016
 
    this.details = details;
1017
 
 
1018
 
    text_label = this.pack_text (true);
1019
 
  }
1020
 
 
1021
 
  public override void update () {
1022
 
    text_label.set_text (details.value);
1023
 
  }
1024
 
 
1025
 
  public override void pack_edit_widgets () {
1026
 
    text = new TextView ();
1027
 
    text.get_style_context ().add_class ("contacts-entry");
1028
 
    text.set_hexpand (true);
1029
 
    text.set_vexpand (true);
1030
 
    var scrolled = new ScrolledWindow (null, null);
1031
 
    scrolled.set_shadow_type (ShadowType.OUT);
1032
 
    scrolled.add_with_viewport (text);
1033
 
 
1034
 
    pack (scrolled);
1035
 
 
1036
 
    delete_button.set_valign (Align.START);
1037
 
 
1038
 
    text.get_buffer ().set_text (details.value);
1039
 
    text.get_buffer ().set_modified (false);
1040
 
 
1041
 
    setup_text_view_for_edit (text);
1042
 
  }
1043
 
 
1044
 
  public override bool finish_edit_widgets (bool save) {
1045
 
    var old_details = details;
1046
 
    var changed = text.get_buffer (). get_modified ();
1047
 
    if (save && changed) {
1048
 
        TextIter start, end;
1049
 
        text.get_buffer ().get_start_iter (out start);
1050
 
        text.get_buffer ().get_end_iter (out end);
1051
 
        var value = text.get_buffer ().get_text (start, end, true);
1052
 
        details = new NoteFieldDetails (value, old_details.parameters);
1053
 
    }
1054
 
    text = null;
1055
 
 
1056
 
    return changed;
1057
 
  }
1058
 
}
1059
 
 
1060
 
class Contacts.NoteFieldSet : FieldSet {
1061
 
  class construct {
1062
 
    label_name = _("Note");
1063
 
    detail_name = _("Note");
1064
 
    property_name = "notes";
1065
 
    is_single_value = true;
1066
 
  }
1067
 
  public override void populate () {
1068
 
    var details = sheet.persona as NoteDetails;
1069
 
    if (details == null)
1070
 
      return;
1071
 
 
1072
 
    foreach (var note in details.notes) {
1073
 
      var row = new NoteFieldRow (this, note);
1074
 
      add_row (row);
1075
 
    }
1076
 
  }
1077
 
 
1078
 
  public override DataFieldRow new_field () {
1079
 
    var row = new NoteFieldRow (this, new NoteFieldDetails (""));
1080
 
    add_row (row);
1081
 
    return row;
1082
 
  }
1083
 
 
1084
 
  public override Value? get_value () {
1085
 
    var details = sheet.persona as NoteDetails;
1086
 
    if (details == null)
1087
 
      return null;
1088
 
 
1089
 
    var new_details = new HashSet<NoteFieldDetails>();
1090
 
    foreach (var row in data_rows) {
1091
 
      var note_row = row as NoteFieldRow;
1092
 
      new_details.add (note_row.details);
1093
 
    }
1094
 
 
1095
 
    var value = Value(new_details.get_type ());
1096
 
    value.set_object (new_details);
1097
 
 
1098
 
    return value;
1099
 
  }
1100
 
}
1101
 
 
1102
 
class Contacts.AddressFieldRow : DataFieldRow {
1103
 
  public PostalAddressFieldDetails details;
1104
 
  Label? text_label[8];
1105
 
  Label detail_label;
1106
 
  Entry? entry[7];
1107
 
  TypeCombo? combo;
1108
 
 
1109
 
  public AddressFieldRow (FieldSet field_set, PostalAddressFieldDetails details) {
1110
 
    base (field_set);
1111
 
    this.details = details;
1112
 
    this.pack_text_detail (out text_label[0], out detail_label);
1113
 
    for (int i = 1; i < text_label.length; i++) {
1114
 
      text_label[i] = this.pack_text (true);
1115
 
    }
1116
 
  }
1117
 
 
1118
 
  public override void update () {
1119
 
    detail_label.set_text (TypeSet.general.format_type (details));
1120
 
 
1121
 
    string[] strs = Contact.format_address (details.value);
1122
 
    for (int i = 0; i < text_label.length; i++) {
1123
 
      if (i < strs.length && strs[i] != null) {
1124
 
        text_label[i].set_text (strs[i]);
1125
 
        text_label[i].show ();
1126
 
        text_label[i].set_no_show_all (false);
1127
 
      } else {
1128
 
        text_label[i].hide ();
1129
 
        text_label[i].set_no_show_all (true);
1130
 
      }
1131
 
    }
1132
 
  }
1133
 
 
1134
 
  public override void pack_edit_widgets () {
1135
 
 
1136
 
    var grid = new Box (Orientation.VERTICAL, 0);
1137
 
    grid.set_hexpand (true);
1138
 
    grid.set_halign (Align.FILL);
1139
 
 
1140
 
    for (int i = 0; i < entry.length; i++) {
1141
 
      string postal_part;
1142
 
      details.value.get (Contact.postal_element_props[i], out postal_part);
1143
 
      entry[i] = new Entry ();
1144
 
      entry[i].set_hexpand (true);
1145
 
      if (postal_part != null)
1146
 
        entry[i].set_text (postal_part);
1147
 
      entry[i].set ("placeholder-text", Contact.postal_element_names[i]);
1148
 
      entry[i].get_style_context ().add_class ("contacts-entry");
1149
 
      entry[i].get_style_context ().add_class ("contacts-postal-entry");
1150
 
      grid.add (entry[i]);
1151
 
 
1152
 
      setup_entry_for_edit (entry[i], i == 0);
1153
 
    }
1154
 
 
1155
 
    this.pack_widget_detail_combo (grid, details, TypeSet.general, out combo);
1156
 
    delete_button.set_valign (Align.START);
1157
 
    var size_group = new SizeGroup (SizeGroupMode.VERTICAL);
1158
 
    size_group.add_widget (delete_button);
1159
 
    size_group.add_widget (combo);
1160
 
 
1161
 
  }
1162
 
 
1163
 
  public override bool finish_edit_widgets (bool save) {
1164
 
    var old_details = details;
1165
 
 
1166
 
    bool changed = combo.modified;
1167
 
    for (int i = 0; i < entry.length; i++) {
1168
 
      string postal_part;
1169
 
      details.value.get (Contact.postal_element_props[i], out postal_part);
1170
 
      if (entry[i].get_text () != postal_part) {
1171
 
        changed = true;
1172
 
        break;
1173
 
      }
1174
 
    }
1175
 
 
1176
 
    if (save && changed) {
1177
 
      var new_value = new PostalAddress (details.value.po_box,
1178
 
                                         details.value.extension,
1179
 
                                         details.value.street,
1180
 
                                         details.value.locality,
1181
 
                                         details.value.region,
1182
 
                                         details.value.postal_code,
1183
 
                                         details.value.country,
1184
 
                                         details.value.address_format,
1185
 
                                         details.value.uid);
1186
 
      for (int i = 0; i < entry.length; i++)
1187
 
        new_value.set (Contact.postal_element_props[i], entry[i].get_text ());
1188
 
      details = new PostalAddressFieldDetails(new_value, old_details.parameters);
1189
 
      combo.update_details (details);
1190
 
    }
1191
 
 
1192
 
    for (int i = 0; i < entry.length; i++)
1193
 
      entry[i] = null;
1194
 
    combo = null;
1195
 
 
1196
 
    return changed;
1197
 
  }
1198
 
}
1199
 
 
1200
 
class Contacts.AddressFieldSet : FieldSet {
1201
 
  class construct {
1202
 
    label_name = _("Addresses");
1203
 
    detail_name = _("Address");
1204
 
    property_name = "postal-addresses";
1205
 
  }
1206
 
  public override void populate () {
1207
 
    var details = sheet.persona as PostalAddressDetails;
1208
 
    if (details == null)
1209
 
      return;
1210
 
 
1211
 
    foreach (var addr in details.postal_addresses) {
1212
 
      var row = new AddressFieldRow (this, addr);
1213
 
      add_row (row);
1214
 
    }
1215
 
  }
1216
 
 
1217
 
  public override DataFieldRow new_field () {
1218
 
    var row = new AddressFieldRow (this,
1219
 
                                   new PostalAddressFieldDetails (
1220
 
                                     new PostalAddress (null,
1221
 
                                                        null,
1222
 
                                                        null,
1223
 
                                                        null,
1224
 
                                                        null,
1225
 
                                                        null,
1226
 
                                                        null,
1227
 
                                                        null,
1228
 
                                                        null)));
1229
 
    add_row (row);
1230
 
    return row;
1231
 
  }
1232
 
 
1233
 
  public override Value? get_value () {
1234
 
    var details = sheet.persona as PostalAddressDetails;
1235
 
    if (details == null)
1236
 
      return null;
1237
 
 
1238
 
    var new_details = new HashSet<PostalAddressFieldDetails>();
1239
 
    foreach (var row in data_rows) {
1240
 
      var addr_row = row as AddressFieldRow;
1241
 
      new_details.add (addr_row.details);
1242
 
    }
1243
 
 
1244
 
    var value = Value(new_details.get_type ());
1245
 
    value.set_object (new_details);
1246
 
 
1247
 
    return value;
1248
 
  }
1249
 
}
1250
 
 
1251
 
public class Contacts.PersonaSheet : Grid {
1252
 
  public ContactPane pane;
1253
 
  public Persona persona;
1254
 
  FieldRow header;
1255
 
  FieldRow footer;
1256
 
 
1257
 
  static Type[] field_set_types = {
1258
 
    typeof(LinkFieldSet),
1259
 
    typeof(EmailFieldSet),
1260
 
    typeof(PhoneFieldSet),
1261
 
    typeof(ChatFieldSet),
1262
 
    typeof(BirthdayFieldSet),
1263
 
    typeof(NicknameFieldSet),
1264
 
    typeof(AddressFieldSet),
1265
 
    typeof(NoteFieldSet)
1266
 
    /* More:
1267
 
       company/department/profession/title/manager/assistant
1268
 
    */
1269
 
  };
1270
 
  FieldSet? field_sets[8]; // This is really the size of field_set_types
1271
 
 
1272
 
  public PersonaSheet(ContactPane pane, Persona persona, int sheet_nr) {
1273
 
    assert (field_sets.length == field_set_types.length);
1274
 
 
1275
 
    this.pane = pane;
1276
 
    this.persona = persona;
1277
 
 
1278
 
    this.set_orientation (Orientation.VERTICAL);
1279
 
    this.set_row_spacing (16);
1280
 
 
1281
 
    int row_nr = 0;
1282
 
 
1283
 
    bool editable = Contact.persona_has_writable_property (persona, "email-addresses") &&
1284
 
      Contact.persona_has_writable_property (persona, "phone-numbers") &&
1285
 
      Contact.persona_has_writable_property (persona, "postal-addresses");
1286
 
 
1287
 
    if (!Contact.persona_is_main (persona) || sheet_nr > 0) {
1288
 
      header = new FieldRow (pane.row_group, pane);
1289
 
 
1290
 
      Label label;
1291
 
      var grid = header.pack_header_in_grid (Contact.format_persona_store_name_for_contact (persona), out label);
1292
 
 
1293
 
      if (!editable) {
1294
 
        var image = new Image.from_icon_name ("changes-prevent-symbolic", IconSize.MENU);
1295
 
 
1296
 
        label.set_hexpand (false);
1297
 
        image.get_style_context ().add_class ("dim-label");
1298
 
        image.set_hexpand (true);
1299
 
        image.set_halign (Align.START);
1300
 
        image.set_valign (Align.CENTER);
1301
 
        grid.add (image);
1302
 
      }
1303
 
 
1304
 
      if (sheet_nr == 0) {
1305
 
        var b = new Button.with_label(_("Add to My Contacts"));
1306
 
        grid.add (b);
1307
 
 
1308
 
        b.clicked.connect ( () => {
1309
 
            link_contacts.begin (pane.contact, null, (obj, result) => {
1310
 
                link_contacts.end (result);
1311
 
                /* TODO: Support undo */
1312
 
              });
1313
 
          });
1314
 
      } else if (pane.contact.individual.personas.size > 1) {
1315
 
        var b = new Button.with_label(_("Unlink"));
1316
 
        grid.add (b);
1317
 
 
1318
 
        b.clicked.connect ( () => {
1319
 
            unlink_persona.begin (pane.contact, persona, (obj, result) => {
1320
 
                unlink_persona.end (result);
1321
 
                /* TODO: Support undo */
1322
 
                /* TODO: Ensure we don't get suggestion for this linkage again */
1323
 
              });
1324
 
          });
1325
 
      }
1326
 
 
1327
 
      this.attach (header, 0, row_nr++, 1, 1);
1328
 
 
1329
 
      header.clicked.connect ( () => {
1330
 
          this.pane.enter_edit_mode (header);
1331
 
        });
1332
 
    }
1333
 
 
1334
 
    for (int i = 0; i < field_set_types.length; i++) {
1335
 
      var field_set = (FieldSet) Object.new(field_set_types[i], sheet: this, row_nr: row_nr++);
1336
 
      field_sets[i] = field_set;
1337
 
 
1338
 
      field_set.populate ();
1339
 
      if (!field_set.is_empty ())
1340
 
        field_set.add_to_sheet ();
1341
 
    }
1342
 
 
1343
 
    if (editable) {
1344
 
      footer = new FieldRow (pane.row_group, pane);
1345
 
      this.attach (footer, 0, row_nr++, 1, 1);
1346
 
 
1347
 
      var b = new Button.with_label (_("Add detail..."));
1348
 
      b.set_halign (Align.START);
1349
 
      b.clicked.connect (add_detail);
1350
 
      footer.pack (b);
1351
 
    }
1352
 
 
1353
 
    persona.notify.connect(persona_notify_cb);
1354
 
  }
1355
 
 
1356
 
  ~PersonaSheet() {
1357
 
    persona.notify.disconnect(persona_notify_cb);
1358
 
  }
1359
 
 
1360
 
  private void add_detail () {
1361
 
    pane.exit_edit_mode (true);
1362
 
    var title = _("Select detail to add to %s").printf (pane.contact.display_name);
1363
 
    var dialog = new Dialog.with_buttons ("",
1364
 
                                          (Window) pane.get_toplevel (),
1365
 
                                          DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT,
1366
 
                                          Stock.CANCEL, ResponseType.CANCEL,
1367
 
                                          Stock.OK, ResponseType.OK);
1368
 
 
1369
 
    dialog.set_resizable (false);
1370
 
    dialog.set_default_response (ResponseType.OK);
1371
 
 
1372
 
    var tree_view = new TreeView ();
1373
 
    var store = new ListStore (2, typeof (string), typeof (FieldSet));
1374
 
    tree_view.set_model (store);
1375
 
    tree_view.set_headers_visible (false);
1376
 
    tree_view.get_selection ().set_mode (SelectionMode.BROWSE);
1377
 
 
1378
 
    var column = new Gtk.TreeViewColumn ();
1379
 
    tree_view.append_column (column);
1380
 
 
1381
 
    var renderer = new Gtk.CellRendererText ();
1382
 
    column.pack_start (renderer, false);
1383
 
    column.add_attribute (renderer, "text", 0);
1384
 
 
1385
 
    var scrolled = new ScrolledWindow(null, null);
1386
 
    scrolled.set_size_request (340, 300);
1387
 
    scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
1388
 
    scrolled.set_vexpand (true);
1389
 
    scrolled.set_hexpand (true);
1390
 
    scrolled.set_shadow_type (ShadowType.IN);
1391
 
    scrolled.add (tree_view);
1392
 
 
1393
 
    var grid = new Grid ();
1394
 
    grid.set_orientation (Orientation.VERTICAL);
1395
 
    grid.set_row_spacing (28);
1396
 
 
1397
 
    var l = new Label (title);
1398
 
    l.set_halign (Align.START);
1399
 
 
1400
 
    grid.add (l);
1401
 
    grid.add (scrolled);
1402
 
 
1403
 
    var box = dialog.get_content_area () as Box;
1404
 
    box.pack_start (grid, true, true, 0);
1405
 
    grid.set_border_width (6);
1406
 
 
1407
 
    TreeIter iter;
1408
 
 
1409
 
    for (int i = 0; i < field_set_types.length; i++) {
1410
 
      var field_set = field_sets[i];
1411
 
      if (!(field_set is ChatFieldSet) &&
1412
 
          Contact.persona_has_writable_property (persona, field_set.property_name) &&
1413
 
          (field_set.is_empty () || !field_set.is_single_value)) {
1414
 
        store.append (out iter);
1415
 
        store.set (iter, 0, field_set.detail_name, 1, field_set);
1416
 
      }
1417
 
    }
1418
 
 
1419
 
    dialog.show_all ();
1420
 
    dialog.response.connect ( (response) => {
1421
 
        if (response == ResponseType.OK) {
1422
 
          FieldSet field_set;
1423
 
          TreeIter iter2;
1424
 
 
1425
 
          if (tree_view.get_selection() .get_selected (null, out iter2)) {
1426
 
            store.get (iter2, 1, out field_set);
1427
 
 
1428
 
            var row = field_set.new_field ();
1429
 
            field_set.show_all ();
1430
 
            field_set.add_to_sheet ();
1431
 
            pane.enter_edit_mode (row);
1432
 
          }
1433
 
        }
1434
 
        dialog.destroy ();
1435
 
      });
1436
 
  }
1437
 
 
1438
 
  private void persona_notify_cb (ParamSpec pspec) {
1439
 
    var name = pspec.get_name ();
1440
 
    foreach (var field_set in field_sets) {
1441
 
      if (field_set.reads_param (name) && !field_set.saving) {
1442
 
        field_set.refresh_from_persona ();
1443
 
      }
1444
 
    }
1445
 
  }
1446
 
}
1447
 
 
1448
 
 
1449
 
public class Contacts.ContactPane : ScrolledWindow {
1450
 
  private Store contacts_store;
1451
 
  private Grid top_grid;
1452
 
  private FieldRow card_row;
1453
 
  private Grid card_grid;
1454
 
  private Grid personas_grid;
1455
 
  public RowGroup row_group;
1456
 
  public RowGroup card_row_group;
1457
 
  public FieldRow? editing_row;
1458
 
 
1459
 
  public Button email_button;
1460
 
  public Button chat_button;
1461
 
  public Button call_button;
1462
 
  public Gtk.Menu context_menu;
1463
 
  private Gtk.MenuItem link_menu_item;
1464
 
  private Gtk.MenuItem delete_menu_item;
1465
 
 
1466
 
  public Contact? contact;
1467
 
 
1468
 
  const int PROFILE_SIZE = 128;
1469
 
 
1470
 
 private async Persona? set_persona_property (Persona persona,
1471
 
                                               string property_name,
1472
 
                                               Value value) throws GLib.Error, PropertyError {
1473
 
    if (persona is FakePersona) {
1474
 
      var fake = persona as FakePersona;
1475
 
      return yield fake.make_real_and_set (property_name, value);
1476
 
    } else {
1477
 
      persona.set_data ("contacts-unedited", true);
1478
 
      yield Contact.set_persona_property (persona, property_name, value);
1479
 
      return null;
1480
 
    }
1481
 
  }
1482
 
 
1483
 
  /* Tries to set the property on all persons that have it writeable, and
1484
 
   * if none, creates a new persona and writes to it, returning the new
1485
 
   * persona.
1486
 
   */
1487
 
  private async Persona? set_individual_property (Contact contact,
1488
 
                                                  string property_name,
1489
 
                                                  Value value) throws GLib.Error, PropertyError {
1490
 
    bool did_set = false;
1491
 
    // Need to make a copy here as it could change during the yields
1492
 
    var personas_copy = contact.individual.personas.to_array ();
1493
 
    foreach (var p in personas_copy) {
1494
 
      if (property_name in p.writeable_properties) {
1495
 
        did_set = true;
1496
 
        yield Contact.set_persona_property (p, property_name, value);
1497
 
      }
1498
 
    }
1499
 
 
1500
 
    if (!did_set) {
1501
 
      var fake = new FakePersona (contact);
1502
 
      return yield fake.make_real_and_set (property_name, value);
1503
 
    }
1504
 
    return null;
1505
 
  }
1506
 
 
1507
 
  private void change_avatar (ContactFrame image_frame) {
1508
 
    this.exit_edit_mode (true);
1509
 
    var dialog = new AvatarDialog (contact);
1510
 
    dialog.show ();
1511
 
    dialog.set_avatar.connect ( (icon) =>  {
1512
 
        Value v = Value (icon.get_type ());
1513
 
        v.set_object (icon);
1514
 
        set_individual_property.begin (contact,
1515
 
                                       "avatar", v,
1516
 
                                       (obj, result) => {
1517
 
                                         try {
1518
 
                                           set_individual_property.end (result);
1519
 
                                         } catch (Error e) {
1520
 
                                           App.app.show_message (e.message);
1521
 
                                           image_frame.set_image (contact.individual, contact);
1522
 
                                         }
1523
 
                                       });
1524
 
      });
1525
 
  }
1526
 
 
1527
 
  public void update_card () {
1528
 
    foreach (var w in card_grid.get_children ()) {
1529
 
      w.destroy ();
1530
 
    }
1531
 
 
1532
 
    if (contact == null)
1533
 
      return;
1534
 
 
1535
 
    var image_frame = new ContactFrame (PROFILE_SIZE, true);
1536
 
    image_frame.clicked.connect ( () => {
1537
 
        change_avatar (image_frame);
1538
 
      });
1539
 
    contact.keep_widget_uptodate (image_frame,  (w) => {
1540
 
        (w as ContactFrame).set_image (contact.individual, contact);
1541
 
      });
1542
 
 
1543
 
    card_grid.attach (image_frame,  0, 0, 1, 3);
1544
 
    card_grid.set_column_spacing (16);
1545
 
 
1546
 
    var l = new Label (null);
1547
 
    l.set_hexpand (true);
1548
 
    l.set_halign (Align.START);
1549
 
    l.set_valign (Align.START);
1550
 
    l.set_margin_top (4);
1551
 
    l.set_ellipsize (Pango.EllipsizeMode.END);
1552
 
    l.xalign = 0.0f;
1553
 
 
1554
 
    contact.keep_widget_uptodate (l,  (w) => {
1555
 
        (w as Label).set_markup (Markup.printf_escaped ("<span font='16'>%s</span>", contact.display_name));
1556
 
      });
1557
 
 
1558
 
    var event_box = new EventBox ();
1559
 
    event_box.set_margin_top (4);
1560
 
    event_box.set_margin_bottom (8);
1561
 
    event_box.set_visible_window (false);
1562
 
 
1563
 
    var clickable = new Clickable (event_box);
1564
 
    event_box.realize.connect_after ( (event) => {
1565
 
        Gdk.Window window = null;
1566
 
        foreach (var win in event_box.get_window ().get_children ()) {
1567
 
          Widget *w = null;
1568
 
          win.get_user_data (out w);
1569
 
          if (w == event_box) {
1570
 
            window = win;
1571
 
          }
1572
 
        }
1573
 
        clickable.realize_for (window);
1574
 
      });
1575
 
    event_box.unrealize.connect_after ( (event) => {
1576
 
        clickable.unrealize ();
1577
 
      });
1578
 
    clickable.clicked.connect ( () => {
1579
 
        this.enter_edit_mode (card_row);
1580
 
      });
1581
 
 
1582
 
    var id1 = card_row.enter_edit_mode.connect_after ( () => {
1583
 
        event_box.remove (l);
1584
 
        var entry = new Entry ();
1585
 
        entry.set_text (contact.display_name);
1586
 
        entry.set_hexpand (true);
1587
 
        entry.show ();
1588
 
        entry.override_font (Pango.FontDescription.from_string ("16px"));
1589
 
        event_box.add (entry);
1590
 
        Utils.grab_widget_later (entry);
1591
 
 
1592
 
        entry.activate.connect_after ( () => {
1593
 
            exit_edit_mode (true);
1594
 
          });
1595
 
        entry.key_press_event.connect ( (key_event) => {
1596
 
            if (key_event.keyval == Gdk.Key.Escape) {
1597
 
              exit_edit_mode (false);
1598
 
            }
1599
 
            return false;
1600
 
          });
1601
 
 
1602
 
        return true;
1603
 
      });
1604
 
 
1605
 
    var id2 = card_row.exit_edit_mode.connect ( (save) => {
1606
 
        Entry entry = event_box.get_child () as Entry;
1607
 
        bool changed = entry.get_text () != contact.display_name;
1608
 
 
1609
 
        if (save && changed) {
1610
 
          // Things look better if we update immediately, rather than after the setting has
1611
 
          // been applied
1612
 
          l.set_markup (Markup.printf_escaped ("<span font='16'>%s</span>", entry.get_text ()));
1613
 
 
1614
 
          Value v = Value (typeof (string));
1615
 
          v.set_string (entry.get_text ());
1616
 
          set_individual_property.begin (contact,
1617
 
                                         "full-name", v,
1618
 
                                         (obj, result) => {
1619
 
                                           try {
1620
 
                                             set_individual_property.end (result);
1621
 
                                           } catch (Error e) {
1622
 
                                             App.app.show_message (e.message);
1623
 
                                             l.set_markup (Markup.printf_escaped ("<span font='16'>%s</span>", contact.display_name));
1624
 
                                           }
1625
 
                                         });
1626
 
        }
1627
 
 
1628
 
        event_box.remove (entry);
1629
 
        event_box.add (l);
1630
 
      });
1631
 
 
1632
 
    var id3 = card_row.lost_child_focus.connect ( () => {
1633
 
        if (editing_row == card_row)
1634
 
          exit_edit_mode (true);
1635
 
      });
1636
 
 
1637
 
    event_box.destroy.connect ( () => {
1638
 
        card_row.disconnect (id1);
1639
 
        card_row.disconnect (id2);
1640
 
        card_row.disconnect (id3);
1641
 
      });
1642
 
 
1643
 
    event_box.add (l);
1644
 
    card_grid.attach (event_box,  1, 0, 1, 1);
1645
 
 
1646
 
    var merged_presence = contact.create_merged_presence_widget ();
1647
 
    merged_presence.set_halign (Align.START);
1648
 
    merged_presence.set_valign (Align.START);
1649
 
    merged_presence.set_vexpand (true);
1650
 
    card_grid.attach (merged_presence,  1, 1, 1, 1);
1651
 
 
1652
 
    var box = new Box (Orientation.HORIZONTAL, 0);
1653
 
    box.set_margin_bottom (4 + 8);
1654
 
    box.set_halign (Align.START);
1655
 
 
1656
 
    box.get_style_context ().add_class ("linked");
1657
 
    box.set_homogeneous (true);
1658
 
    box.set_halign (Align.FILL);
1659
 
    var image = new Image.from_icon_name ("mail-unread-symbolic", IconSize.MENU);
1660
 
    var b = new Button ();
1661
 
    b.add (image);
1662
 
    box.pack_start (b, true, true, 0);
1663
 
    email_button = b;
1664
 
    email_button.clicked.connect (send_email);
1665
 
 
1666
 
    image = new Image.from_icon_name ("user-available-symbolic", IconSize.MENU);
1667
 
    b = new Button ();
1668
 
    b.add (image);
1669
 
    box.pack_start (b, true, true, 0);
1670
 
    chat_button = b;
1671
 
    chat_button.clicked.connect (start_chat);
1672
 
 
1673
 
    image = new Image.from_icon_name ("call-start-symbolic", IconSize.MENU);
1674
 
    b = new Button ();
1675
 
    b.add (image);
1676
 
    box.pack_start (b, true, true, 0);
1677
 
    call_button = b;
1678
 
    call_button.clicked.connect (start_call);
1679
 
 
1680
 
    card_grid.attach (box,  1, 2, 1, 1);
1681
 
 
1682
 
    card_grid.show_all ();
1683
 
 
1684
 
    update_buttons ();
1685
 
  }
1686
 
 
1687
 
  public void update_buttons () {
1688
 
    if (contact == null)
1689
 
      return;
1690
 
 
1691
 
    var emails = contact.individual.email_addresses;
1692
 
    email_button.set_sensitive (!emails.is_empty);
1693
 
 
1694
 
    var ims = contact.individual.im_addresses;
1695
 
    var im_keys = ims.get_keys ();
1696
 
    bool found_im = false;
1697
 
    bool callable = false;
1698
 
    PresenceType max_presence = 0;
1699
 
    foreach (var protocol in im_keys) {
1700
 
      foreach (var id in ims[protocol]) {
1701
 
        var im_persona = contact.find_im_persona (protocol, id.value);
1702
 
        if (im_persona != null) {
1703
 
          var type = im_persona.presence_type;
1704
 
          if (type != PresenceType.UNSET &&
1705
 
              type != PresenceType.ERROR &&
1706
 
              type != PresenceType.OFFLINE &&
1707
 
              type != PresenceType.UNKNOWN) {
1708
 
            found_im = true;
1709
 
            if (type > max_presence)
1710
 
              max_presence = type;
1711
 
          }
1712
 
        }
1713
 
 
1714
 
        if (contact.is_callable (protocol, id.value) != null)
1715
 
          callable = true;
1716
 
      }
1717
 
    }
1718
 
 
1719
 
    if (contacts_store.can_call) {
1720
 
      var phones = contact.individual.phone_numbers;
1721
 
      if (!phones.is_empty)
1722
 
        callable = true;
1723
 
    }
1724
 
 
1725
 
    string icon;
1726
 
    if (found_im)
1727
 
      icon = Contact.presence_to_icon_symbolic (max_presence);
1728
 
    else
1729
 
      icon = "user-available-symbolic";
1730
 
    (chat_button.get_child () as Image).set_from_icon_name (icon, IconSize.MENU);
1731
 
    chat_button.set_sensitive (found_im);
1732
 
 
1733
 
    call_button.set_sensitive (callable);
1734
 
  }
1735
 
 
1736
 
  public signal void contacts_linked (string? main_contact, string linked_contact, LinkOperation operation);
1737
 
 
1738
 
  public void add_suggestion (Contact c) {
1739
 
    var row = new FieldRow (row_group, this);
1740
 
    personas_grid.add (row);
1741
 
 
1742
 
    var grid = new Grid ();
1743
 
    grid.get_style_context ().add_class ("contacts-suggestion");
1744
 
    grid.set_redraw_on_allocate (true);
1745
 
    grid.draw.connect ( (cr) => {
1746
 
        Allocation allocation;
1747
 
        grid.get_allocation (out allocation);
1748
 
 
1749
 
        var context = grid.get_style_context ();
1750
 
        context.render_background (cr,
1751
 
                                   0, 0,
1752
 
                                   allocation.width, allocation.height);
1753
 
        return false;
1754
 
      });
1755
 
    row.pack (grid);
1756
 
 
1757
 
    var image_frame = new ContactFrame (Contact.SMALL_AVATAR_SIZE);
1758
 
    c.keep_widget_uptodate (image_frame,  (w) => {
1759
 
        (w as ContactFrame).set_image (c.individual, c);
1760
 
      });
1761
 
    image_frame.set_hexpand (false);
1762
 
    grid.attach (image_frame, 0, 0, 1, 2);
1763
 
 
1764
 
    var label = new Label ("");
1765
 
    if (contact.is_main)
1766
 
      label.set_markup (Markup.printf_escaped (_("Does %s from %s belong here?"), c.display_name, c.format_persona_stores ()));
1767
 
    else
1768
 
      label.set_markup (Markup.printf_escaped (_("Do these details belong to %s?"), c.display_name));
1769
 
    label.set_valign (Align.START);
1770
 
    label.set_halign (Align.START);
1771
 
    label.set_line_wrap (true);
1772
 
    label.set_line_wrap_mode (Pango.WrapMode.WORD_CHAR);
1773
 
    label.set_hexpand (false);
1774
 
    label.xalign = 0.0f;
1775
 
    grid.attach (label, 1, 0, 1, 1);
1776
 
 
1777
 
    var bbox = new ButtonBox (Orientation.HORIZONTAL);
1778
 
    var yes = new Button.with_label (_("Yes"));
1779
 
    var no = new Button.with_label (_("No"));
1780
 
 
1781
 
    yes.clicked.connect ( () => {
1782
 
      var linked_contact = c.display_name;
1783
 
      link_contacts.begin (contact, c, (obj, result) => {
1784
 
        var operation = link_contacts.end (result);
1785
 
        this.contacts_linked (null, linked_contact, operation);
1786
 
      });
1787
 
      row.destroy ();
1788
 
    });
1789
 
 
1790
 
    no.clicked.connect ( () => {
1791
 
        contacts_store.add_no_suggest_link (contact, c);
1792
 
        /* TODO: Add undo */
1793
 
        row.destroy ();
1794
 
      });
1795
 
 
1796
 
    bbox.add (yes);
1797
 
    bbox.add (no);
1798
 
    bbox.set_spacing (8);
1799
 
    bbox.set_halign (Align.END);
1800
 
    bbox.set_hexpand (true);
1801
 
    bbox.set_border_width (4);
1802
 
    grid.attach (bbox, 2, 0, 1, 2);
1803
 
  }
1804
 
 
1805
 
  private uint update_personas_timeout;
1806
 
  public void update_personas (bool show_matches = true) {
1807
 
    if (update_personas_timeout != 0) {
1808
 
      Source.remove (update_personas_timeout);
1809
 
      update_personas_timeout = 0;
1810
 
    }
1811
 
 
1812
 
    foreach (var w in personas_grid.get_children ()) {
1813
 
      w.destroy ();
1814
 
    }
1815
 
 
1816
 
    if (contact == null)
1817
 
      return;
1818
 
 
1819
 
    var personas = contact.get_personas_for_display ();
1820
 
 
1821
 
    int i = 0;
1822
 
    foreach (var p in personas) {
1823
 
      var sheet = new PersonaSheet(this, p, i++);
1824
 
      personas_grid.add (sheet);
1825
 
    }
1826
 
 
1827
 
    if (show_matches) {
1828
 
      var matches = contact.store.aggregator.get_potential_matches (contact.individual, MatchResult.HIGH);
1829
 
      foreach (var ind in matches.keys) {
1830
 
        var c = Contact.from_individual (ind);
1831
 
        if (c != null && contact.suggest_link_to (c)) {
1832
 
          add_suggestion (c);
1833
 
        }
1834
 
      }
1835
 
    }
1836
 
 
1837
 
    personas_grid.show_all ();
1838
 
  }
1839
 
 
1840
 
  public void show_contact (Contact? new_contact, bool edit=false, bool show_matches = true) {
1841
 
    if (contact == new_contact)
1842
 
      return;
1843
 
 
1844
 
    if (contact != null && editing_row != null)
1845
 
      exit_edit_mode (true);
1846
 
 
1847
 
    if (contact != null) {
1848
 
      contact.personas_changed.disconnect (personas_changed_cb);
1849
 
      contact.changed.disconnect (contact_changed_cb);
1850
 
    }
1851
 
 
1852
 
    contact = new_contact;
1853
 
 
1854
 
    update_card ();
1855
 
    update_personas (show_matches);
1856
 
 
1857
 
    if (!show_matches) {
1858
 
      update_personas_timeout = Gdk.threads_add_timeout (100, () => {
1859
 
          update_personas ();
1860
 
          return false;
1861
 
        });
1862
 
    }
1863
 
 
1864
 
    bool can_remove = false;
1865
 
 
1866
 
    if (contact != null) {
1867
 
      contact.personas_changed.connect (personas_changed_cb);
1868
 
      contact.changed.connect (contact_changed_cb);
1869
 
 
1870
 
      can_remove = contact.can_remove_personas ();
1871
 
    }
1872
 
 
1873
 
    delete_menu_item.set_sensitive (can_remove);
1874
 
    link_menu_item.set_sensitive (contact != null);
1875
 
  }
1876
 
 
1877
 
  private void personas_changed_cb (Contact contact) {
1878
 
    update_personas ();
1879
 
  }
1880
 
 
1881
 
  private void contact_changed_cb (Contact contact) {
1882
 
    update_buttons ();
1883
 
  }
1884
 
 
1885
 
  public void enter_edit_mode (FieldRow row) {
1886
 
    if (editing_row != row) {
1887
 
      exit_edit_mode (true);
1888
 
      editing_row = null;
1889
 
      if (row.enter_edit_mode ()) {
1890
 
        editing_row = row;
1891
 
        editing_row.set_editing (true);
1892
 
      }
1893
 
    }
1894
 
  }
1895
 
 
1896
 
  public void exit_edit_mode (bool save) {
1897
 
    if (editing_row != null) {
1898
 
      editing_row.exit_edit_mode (save);
1899
 
      editing_row.set_editing (false);
1900
 
    }
1901
 
 
1902
 
    editing_row = null;
1903
 
  }
1904
 
 
1905
 
  private Dialog pick_one_dialog (string title, TreeModel model, out TreeSelection selection) {
1906
 
    var dialog = new Dialog.with_buttons (title,
1907
 
                                          (Window) this.get_toplevel (),
1908
 
                                          DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT,
1909
 
                                          Stock.CANCEL, ResponseType.CANCEL,
1910
 
                                          Stock.OK, ResponseType.OK);
1911
 
 
1912
 
    dialog.set_resizable (false);
1913
 
    dialog.set_default_response (ResponseType.OK);
1914
 
 
1915
 
    var tree_view = new TreeView ();
1916
 
    tree_view.set_model (model);
1917
 
    tree_view.set_headers_visible (false);
1918
 
    tree_view.get_selection ().set_mode (SelectionMode.BROWSE);
1919
 
 
1920
 
    var column = new Gtk.TreeViewColumn ();
1921
 
    tree_view.append_column (column);
1922
 
 
1923
 
    var renderer = new Gtk.CellRendererText ();
1924
 
    column.pack_start (renderer, false);
1925
 
    column.add_attribute (renderer, "text", 0);
1926
 
 
1927
 
    var scrolled = new ScrolledWindow(null, null);
1928
 
    scrolled.set_size_request (340, 300);
1929
 
    scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
1930
 
    scrolled.set_vexpand (true);
1931
 
    scrolled.set_hexpand (true);
1932
 
    scrolled.set_shadow_type (ShadowType.IN);
1933
 
    scrolled.add (tree_view);
1934
 
 
1935
 
    var grid = new Grid ();
1936
 
    grid.set_orientation (Orientation.VERTICAL);
1937
 
    grid.set_row_spacing (6);
1938
 
 
1939
 
    var l = new Label (title);
1940
 
    l.set_halign (Align.START);
1941
 
 
1942
 
    grid.add (l);
1943
 
    grid.add (scrolled);
1944
 
 
1945
 
    var box = dialog.get_content_area () as Box;
1946
 
    box.pack_start (grid, true, true, 0);
1947
 
    grid.set_border_width (6);
1948
 
 
1949
 
    dialog.show_all ();
1950
 
 
1951
 
    selection = tree_view.get_selection ();
1952
 
    return dialog;
1953
 
  }
1954
 
 
1955
 
 
1956
 
  public void send_email () {
1957
 
    var emails = contact.individual.email_addresses;
1958
 
    if (emails.is_empty)
1959
 
      return;
1960
 
    if (emails.size == 1) {
1961
 
      foreach (var email in emails) {
1962
 
        var email_addr = email.value;
1963
 
        Utils.compose_mail (email_addr);
1964
 
      }
1965
 
    } else {
1966
 
      TreeIter iter;
1967
 
 
1968
 
      var store = new ListStore (1, typeof (string));
1969
 
      foreach (var email in emails) {
1970
 
        var email_addr = email.value;
1971
 
        store.append (out iter);
1972
 
        store.set (iter, 0, email_addr);
1973
 
      }
1974
 
 
1975
 
      TreeSelection selection;
1976
 
      var dialog = pick_one_dialog (_("Select email address"), store, out selection);
1977
 
      dialog.response.connect ( (response) => {
1978
 
          if (response == ResponseType.OK) {
1979
 
            string email2;
1980
 
            TreeIter iter2;
1981
 
 
1982
 
            if (selection.get_selected (null, out iter2)) {
1983
 
              store.get (iter2, 0, out email2);
1984
 
              Utils.compose_mail (email2);
1985
 
            }
1986
 
          }
1987
 
          dialog.destroy ();
1988
 
        });
1989
 
    }
1990
 
  }
1991
 
 
1992
 
  struct CallValue {
1993
 
    string phone_nr;
1994
 
    string protocol;
1995
 
    string id;
1996
 
    string name;
1997
 
  }
1998
 
 
1999
 
  public void start_call () {
2000
 
    var ims = contact.individual.im_addresses;
2001
 
    var im_keys = ims.get_keys ();
2002
 
    var call_targets = new ArrayList<CallValue?>();
2003
 
    foreach (var protocol in im_keys) {
2004
 
      foreach (var id in ims[protocol]) {
2005
 
        var im_persona = contact.find_im_persona (protocol, id.value);
2006
 
        if (im_persona != null &&
2007
 
            contact.is_callable (protocol, id.value) != null) {
2008
 
          var type = im_persona.presence_type;
2009
 
          if (type != PresenceType.UNSET &&
2010
 
              type != PresenceType.ERROR &&
2011
 
              type != PresenceType.OFFLINE &&
2012
 
              type != PresenceType.UNKNOWN) {
2013
 
            CallValue? value = { null, protocol, id.value, Contact.format_im_name (im_persona, protocol, id.value) };
2014
 
            call_targets.add (value);
2015
 
          }
2016
 
        }
2017
 
      }
2018
 
    }
2019
 
 
2020
 
    if (contacts_store.can_call) {
2021
 
      var phones = contact.individual.phone_numbers;
2022
 
      foreach (var phone in phones) {
2023
 
        CallValue? value = { phone.value, null, null, phone.value };
2024
 
        call_targets.add (value);
2025
 
      }
2026
 
    }
2027
 
 
2028
 
 
2029
 
    if (call_targets.is_empty)
2030
 
      return;
2031
 
 
2032
 
    if (call_targets.size == 1) {
2033
 
      foreach (var value in call_targets) {
2034
 
        if (value.phone_nr != null)
2035
 
          Utils.start_call (value.phone_nr, this.contacts_store.calling_accounts);
2036
 
        else {
2037
 
          var account = contact.is_callable (value.protocol, value.id);
2038
 
          Utils.start_call_with_account (value.id, account);
2039
 
        }
2040
 
      }
2041
 
    } else {
2042
 
      var store = new ListStore (2, typeof (string), typeof (CallValue?));
2043
 
      foreach (var value in call_targets) {
2044
 
        TreeIter iter;
2045
 
        store.append (out iter);
2046
 
        store.set (iter, 0, value.name, 1, value);
2047
 
      }
2048
 
      TreeSelection selection;
2049
 
      var dialog = pick_one_dialog (_("Select what to call"), store, out selection);
2050
 
      dialog.response.connect ( (response) => {
2051
 
          if (response == ResponseType.OK) {
2052
 
            CallValue? value2;
2053
 
            TreeIter iter2;
2054
 
 
2055
 
            if (selection.get_selected (null, out iter2)) {
2056
 
              store.get (iter2, 1, out value2);
2057
 
              if (value2.phone_nr != null)
2058
 
                Utils.start_call (value2.phone_nr, this.contacts_store.calling_accounts);
2059
 
              else {
2060
 
                var account = contact.is_callable (value2.protocol, value2.id);
2061
 
                Utils.start_call_with_account (value2.id, account);
2062
 
              }
2063
 
            }
2064
 
          }
2065
 
          dialog.destroy ();
2066
 
        });
2067
 
    }
2068
 
  }
2069
 
 
2070
 
  struct ImValue {
2071
 
    string protocol;
2072
 
    string id;
2073
 
    string name;
2074
 
  }
2075
 
 
2076
 
  public void start_chat () {
2077
 
    var ims = contact.individual.im_addresses;
2078
 
    var im_keys = ims.get_keys ();
2079
 
    var online_personas = new ArrayList<ImValue?>();
2080
 
    if (contact != null) {
2081
 
      foreach (var protocol in im_keys) {
2082
 
        foreach (var id in ims[protocol]) {
2083
 
          var im_persona = contact.find_im_persona (protocol, id.value);
2084
 
          if (im_persona != null) {
2085
 
            var type = im_persona.presence_type;
2086
 
            if (type != PresenceType.UNSET &&
2087
 
                type != PresenceType.ERROR &&
2088
 
                type != PresenceType.OFFLINE &&
2089
 
                type != PresenceType.UNKNOWN) {
2090
 
              ImValue? value = { protocol, id.value, Contact.format_im_name (im_persona, protocol, id.value) };
2091
 
              online_personas.add (value);
2092
 
            }
2093
 
          }
2094
 
        }
2095
 
      }
2096
 
    }
2097
 
 
2098
 
    if (online_personas.is_empty)
2099
 
      return;
2100
 
 
2101
 
    if (online_personas.size == 1) {
2102
 
      foreach (var value in online_personas) {
2103
 
        Utils.start_chat (contact, value.protocol, value.id);
2104
 
      }
2105
 
    } else {
2106
 
      var store = new ListStore (2, typeof (string), typeof (ImValue?));
2107
 
      foreach (var value in online_personas) {
2108
 
        TreeIter iter;
2109
 
        store.append (out iter);
2110
 
        store.set (iter, 0, value.name, 1, value);
2111
 
      }
2112
 
      TreeSelection selection;
2113
 
      var dialog = pick_one_dialog (_("Select chat account"), store, out selection);
2114
 
      dialog.response.connect ( (response) => {
2115
 
          if (response == ResponseType.OK) {
2116
 
            ImValue? value2;
2117
 
            TreeIter iter2;
2118
 
 
2119
 
            if (selection.get_selected (null, out iter2)) {
2120
 
              store.get (iter2, 1, out value2);
2121
 
              Utils.start_chat (contact, value2.protocol, value2.id);
2122
 
            }
2123
 
          }
2124
 
          dialog.destroy ();
2125
 
        });
2126
 
    }
2127
 
  }
2128
 
 
2129
 
  public ContactPane (Store contacts_store) {
2130
 
    this.get_style_context ().add_class ("contacts-content");
2131
 
    this.set_shadow_type (ShadowType.IN);
2132
 
 
2133
 
    this.button_press_event.connect ( (e) => {
2134
 
        exit_edit_mode (true);
2135
 
        return false;
2136
 
      });
2137
 
 
2138
 
    this.contacts_store = contacts_store;
2139
 
    row_group = new RowGroup(3);
2140
 
    row_group.set_column_min_width (0, 32);
2141
 
    row_group.set_column_min_width (1, 400);
2142
 
    row_group.set_column_max_width (1, 480);
2143
 
    row_group.set_column_min_width (2, 32);
2144
 
    row_group.set_column_spacing (0, 8);
2145
 
    row_group.set_column_spacing (1, 8);
2146
 
    row_group.set_column_priority (1, 1);
2147
 
 
2148
 
    card_row_group = row_group.copy ();
2149
 
    /* This is kinda lame hardcoding so that the frame inside
2150
 
       the button aligns with the other rows. It really
2151
 
       depends on the theme, but there seems no good way to
2152
 
       do this */
2153
 
    card_row_group.set_column_spacing (0, 0);
2154
 
 
2155
 
    this.set_hexpand (true);
2156
 
    this.set_vexpand (true);
2157
 
    this.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
2158
 
 
2159
 
    top_grid = new Grid ();
2160
 
    top_grid.set_orientation (Orientation.VERTICAL);
2161
 
    top_grid.set_margin_top (40);
2162
 
    top_grid.set_margin_bottom (32);
2163
 
    top_grid.set_row_spacing (20);
2164
 
    this.add_with_viewport (top_grid);
2165
 
    top_grid.set_focus_vadjustment (this.get_vadjustment ());
2166
 
 
2167
 
    var viewport = this.get_child ();
2168
 
    viewport.button_press_event.connect ( (event) => {
2169
 
        if (event.button == 3) {
2170
 
          context_menu.popup (null, null, null, event.button, event.time);
2171
 
          return true;
2172
 
        }
2173
 
        return false;
2174
 
      });
2175
 
 
2176
 
    this.get_child().get_style_context ().add_class ("contacts-main-view");
2177
 
    this.get_child().get_style_context ().add_class ("view");
2178
 
 
2179
 
    card_row = new FieldRow (card_row_group, this);
2180
 
    top_grid.add (card_row);
2181
 
    card_grid = new Grid ();
2182
 
    card_grid.set_vexpand (false);
2183
 
    card_row.pack (card_grid);
2184
 
 
2185
 
    personas_grid = new Grid ();
2186
 
    personas_grid.set_orientation (Orientation.VERTICAL);
2187
 
    personas_grid.set_row_spacing (40);
2188
 
    top_grid.add (personas_grid);
2189
 
 
2190
 
    top_grid.show_all ();
2191
 
 
2192
 
    context_menu = new Gtk.Menu ();
2193
 
    link_menu_item = Utils.add_menu_item (context_menu,_("Add/Remove Linked Contacts..."));
2194
 
    link_menu_item.activate.connect (link_contact);
2195
 
    link_menu_item.set_sensitive (false);
2196
 
    //Utils.add_menu_item (context_menu,_("Send..."));
2197
 
    delete_menu_item = Utils.add_menu_item (context_menu,_("Delete"));
2198
 
    delete_menu_item.activate.connect (delete_contact);
2199
 
    delete_menu_item.set_sensitive (false);
2200
 
 
2201
 
    contacts_store.quiescent.connect (() => {
2202
 
      // Refresh the view when the store is quiescent as we may have missed
2203
 
      // some potential matches while the store was still preparing.
2204
 
      update_personas ();
2205
 
    });
2206
 
  }
2207
 
 
2208
 
  void link_contact () {
2209
 
    var dialog = new LinkDialog (contact);
2210
 
    dialog.contacts_linked.connect ( (main_contact, linked_contact, operation) => {
2211
 
      this.contacts_linked (main_contact, linked_contact, operation);
2212
 
    });
2213
 
    dialog.show_all ();
2214
 
  }
2215
 
 
2216
 
  public signal void will_delete (Contact contact);
2217
 
 
2218
 
  void delete_contact () {
2219
 
    if (contact != null) {
2220
 
      contact.hide ();
2221
 
      this.will_delete (contact);
2222
 
    }
2223
 
  }
2224
 
}