summaryrefslogtreecommitdiff
path: root/src/contacts-link-dialog.vala
blob: c0fe4790b10fd4ecf56abc9aa3f6f40f6bdbc749 (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
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Gtk;
using Folks;

public class Contacts.LinkDialog : Dialog {
  // TODO: Remove later when bound in vala
  private static unowned string C_(string context, string msgid) {
    return GLib.dpgettext2 (Config.GETTEXT_PACKAGE, context, msgid);
  }

  private Contact contact;
  private Contact? selected_contact;
  private Entry filter_entry;
  private View view;
  private ViewWidget list;
  private Grid persona_grid;
  private uint filter_entry_changed_id;

  private void update_contact () {
    // Remove previous personas
    foreach (var w in persona_grid.get_children ()) {
      w.destroy ();
    }

    if (selected_contact == null)
      return;

    var image_frame = new ContactFrame (48);
    image_frame.set_image (selected_contact as AvatarDetails);
    image_frame.set_hexpand (false);
    persona_grid.attach (image_frame, 0, 0, 1, 2);

    var label = new Label ("");
    label.set_markup ("<span font='13'>" + selected_contact.display_name + "</span>");
    label.set_valign (Align.START);
    label.set_halign (Align.START);
    label.set_hexpand (false);
    label.xalign = 0.0f;
    label.set_ellipsize (Pango.EllipsizeMode.END);
    persona_grid.attach (label, 1, 0, 1, 1);

    label = new Label ("");
    label.set_markup ("<span font='9'>" +selected_contact.format_persona_stores () + "</span>");
    label.set_valign (Align.START);
    label.set_halign (Align.START);
    label.set_hexpand (true);
    label.xalign = 0.0f;
    label.set_ellipsize (Pango.EllipsizeMode.END);
    persona_grid.attach (label, 1, 1, 1, 1);

    var grid = new Grid ();
    grid.set_orientation (Orientation.VERTICAL);
    grid.set_border_width (8);

    persona_grid.attach (grid, 0, 2, 2, 1);

    var emails = Contact.sort_fields<EmailFieldDetails>(selected_contact.individual.email_addresses);
    if (!emails.is_empty) {
      label = new Label (_("Email"));
      label.xalign = 0.0f;
      grid.add (label);
      foreach (var email in emails) {
	label = new Label (email.value);
	label.xalign = 0.0f;
	grid.add (label);
      }
      label = new Label (_(""));
      label.xalign = 0.0f;
      grid.add (label);
    }

    var phone_numbers = Contact.sort_fields<PhoneFieldDetails>(selected_contact.individual.phone_numbers);
    if (!phone_numbers.is_empty) {
      label = new Label (_("Phone number"));
      label.xalign = 0.0f;
      grid.add (label);
      foreach (var phone_number in phone_numbers) {
	label = new Label (phone_number.value);
	label.xalign = 0.0f;
	grid.add (label);
      }
    }

    persona_grid.show_all ();
  }

  public LinkDialog (Contact contact) {
    this.contact = contact;
    set_title (_("Link Contact"));
    set_transient_for (App.app.window);
    set_modal (true);
    add_buttons (Stock.CLOSE,  null);

    view = new View (contact.store);
    view.hide_contact (contact);
    if (contact.is_main)
      view.set_show_subset (View.Subset.OTHER);
    else
      view.set_show_subset (View.Subset.MAIN);

    var matches = contact.store.aggregator.get_potential_matches (contact.individual, MatchResult.HIGH);
    foreach (var ind in matches.keys) {
      var c = Contact.from_individual (ind);
      if (c != null) {
	var result = matches.get (ind);
	view.set_custom_sort_prio (c, (int) result);
      }
    }

    list = new ViewWidget (view, ViewWidget.TextDisplay.STORES);

    var grid = new Grid ();
    grid.set_column_homogeneous (true);
    var container = (get_content_area () as Container);
    grid.set_border_width (8);
    container.add (grid);

    var label = new Label (_("Select contacts to link to %s").printf (contact.display_name));
    label.set_valign (Align.CENTER);
    label.set_halign (Align.START);
    label.xalign = 0.0f;
    label.set_ellipsize (Pango.EllipsizeMode.END);
    grid.attach (label, 0, 0, 1, 1);

    var list_frame = new Frame (null);
    list_frame.get_style_context ().add_class ("contact-list-frame");
    grid.attach (list_frame, 0, 1, 1, 1);

    var list_grid = new Grid ();
    list_grid.set_size_request (315, -1);
    list_grid.set_hexpand (false);
    list_frame.add (list_grid);
    list_grid.set_orientation (Orientation.VERTICAL);

    var toolbar = new Toolbar ();
    toolbar.get_style_context ().add_class (STYLE_CLASS_PRIMARY_TOOLBAR);
    toolbar.set_icon_size (IconSize.MENU);
    toolbar.set_vexpand (false);
    list_grid.add (toolbar);

    filter_entry = new Entry ();
    filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-find-symbolic");
    filter_entry.changed.connect (filter_entry_changed);
    filter_entry.icon_press.connect (filter_entry_clear);

    var search_entry_item = new ToolItem ();
    search_entry_item.is_important = false;
    search_entry_item.set_expand (true);
    search_entry_item.add (filter_entry);
    toolbar.add (search_entry_item);

    var scrolled = new ScrolledWindow(null, null);
    scrolled.set_min_content_width (310);
    scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
    scrolled.set_vexpand (true);
    scrolled.set_hexpand (true);
    scrolled.set_shadow_type (ShadowType.NONE);
    scrolled.add (list);
    list_grid.add (scrolled);

    toolbar = new Toolbar ();
    toolbar.get_style_context ().add_class (STYLE_CLASS_PRIMARY_TOOLBAR);
    toolbar.set_icon_size (IconSize.MENU);
    toolbar.set_vexpand (false);
    list_grid.add (toolbar);

    var link_button = new ToolButton (null, C_("link-contacts-button", "Link"));
    link_button.get_style_context ().add_class (STYLE_CLASS_RAISED);
    link_button.is_important = true;
    link_button.sensitive = false;
    toolbar.add (link_button);
    link_button.clicked.connect ( (button) => {
	// TODO: Link selected_contact.individual into contact.individual
	// ensure we get the same individual so that the Contact is the same
	link_contacts.begin (contact, selected_contact, (obj, result) => {
	    link_contacts.end (result);
	  });
      });

    list.selection_changed.connect ( (contact) => {
	selected_contact = contact;
	link_button.sensitive = contact != null;
	update_contact ();
      });

    scrolled = new ScrolledWindow(null, null);
    scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
    scrolled.set_vexpand (true);
    scrolled.set_hexpand (true);
    scrolled.set_shadow_type (ShadowType.NONE);
    grid.attach (scrolled, 1, 1, 1, 1);

    persona_grid = new Grid ();
    persona_grid.set_orientation (Orientation.VERTICAL);
    persona_grid.set_border_width (4);
    persona_grid.set_column_spacing (8);
    scrolled.add_with_viewport (persona_grid);

    response.connect ( (response_id) => {
	this.destroy ();
      });

    set_default_size (710, 510);
  }

  private void refilter () {
    string []? values;
    string str = filter_entry.get_text ();

    if (str.length == 0)
      values = null;
    else {
      str = Utils.canonicalize_for_search (str);
      values = str.split(" ");
    }

    view.set_filter_values (values);
  }

  private bool filter_entry_changed_timeout () {
    filter_entry_changed_id = 0;
    refilter ();
    return false;
  }

  private void filter_entry_changed (Editable editable) {
    if (filter_entry_changed_id != 0)
      Source.remove (filter_entry_changed_id);

    filter_entry_changed_id = Timeout.add (300, filter_entry_changed_timeout);

    if (filter_entry.get_text () == "")
      filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-find-symbolic");
    else
      filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-clear-symbolic");
  }

  private void filter_entry_clear (EntryIconPosition position) {
    filter_entry.set_text ("");
  }
}