1
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
3
* Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
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.
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.
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/>.
22
public class Contacts.LinkDialog : Dialog {
23
// TODO: Remove later when bound in vala
24
private static unowned string C_(string context, string msgid) {
25
return GLib.dpgettext2 (Config.GETTEXT_PACKAGE, context, msgid);
28
private Contact contact;
29
private Contact? selected_contact;
30
private Entry filter_entry;
32
private Grid list_grid;
33
private Grid persona_grid;
34
private uint filter_entry_changed_id;
36
public signal void contacts_linked (string? main_contact, string linked_contact, LinkOperation operation);
38
private void update_contact () {
39
// Remove previous personas
40
foreach (var w in persona_grid.get_children ()) {
44
if (selected_contact == null)
47
var image_frame = new ContactFrame (Contact.SMALL_AVATAR_SIZE);
48
contact.keep_widget_uptodate (image_frame, (w) => {
49
(w as ContactFrame).set_image (selected_contact.individual, selected_contact);
51
image_frame.set_hexpand (false);
52
persona_grid.attach (image_frame, 0, 0, 1, 2);
54
var label = new Label ("");
55
label.set_markup ("<span font='13'>" + selected_contact.display_name + "</span>");
56
label.set_valign (Align.START);
57
label.set_halign (Align.START);
58
label.set_hexpand (false);
60
label.set_ellipsize (Pango.EllipsizeMode.END);
61
persona_grid.attach (label, 1, 0, 1, 1);
63
label = new Label ("");
64
label.set_markup ("<span font='9'>" +selected_contact.format_persona_stores () + "</span>");
65
label.set_valign (Align.START);
66
label.set_halign (Align.START);
67
label.set_hexpand (true);
69
label.set_ellipsize (Pango.EllipsizeMode.END);
70
persona_grid.attach (label, 1, 1, 1, 1);
72
if (contact.is_main) {
73
var link_button = new Button.with_label (C_("contacts link action", "Link"));
74
link_button.set_hexpand (false);
75
link_button.set_valign (Align.CENTER);
76
var bbox = new ButtonBox (Orientation.HORIZONTAL);
77
bbox.add (link_button);
78
persona_grid.attach (bbox, 2, 0, 1, 2);
80
link_button.clicked.connect ( (button) => {
81
var selected_contact_name = selected_contact.display_name;
82
link_contacts.begin (contact, selected_contact, (obj, result) => {
83
var operation = link_contacts.end (result);
84
var undo_bar = new InfoBar.with_buttons (_("Undo"), ResponseType.APPLY, null);
85
undo_bar.set_message_type (MessageType.INFO);
86
var container = (undo_bar.get_content_area () as Container);
87
var message_label = new Label (_("%s linked to the contact").printf (selected_contact_name));
88
//TODO, do something smarter here.
89
message_label.set_ellipsize (Pango.EllipsizeMode.END);
90
container.add (message_label);
91
undo_bar.response.connect ( (response_id) => {
92
if (response_id == ResponseType.APPLY) {
97
Timeout.add (5000, () => {
101
list_grid.add (undo_bar);
102
undo_bar.show_all ();
107
var grid = new Grid ();
108
grid.set_orientation (Orientation.VERTICAL);
109
grid.set_border_width (8);
111
persona_grid.attach (grid, 0, 2, 3, 1);
114
var emails = Contact.sort_fields<EmailFieldDetails>(selected_contact.individual.email_addresses);
115
if (!emails.is_empty) {
116
label = new Label (_("Email"));
119
foreach (var email in emails) {
120
label = new Label (email.value);
121
label.set_ellipsize (Pango.EllipsizeMode.END);
125
label = new Label ("");
130
var phone_numbers = Contact.sort_fields<PhoneFieldDetails>(selected_contact.individual.phone_numbers);
131
if (!phone_numbers.is_empty) {
132
label = new Label (_("Phone number"));
135
foreach (var phone_number in phone_numbers) {
136
label = new Label (phone_number.value);
137
label.set_ellipsize (Pango.EllipsizeMode.END);
143
persona_grid.show_all ();
146
public LinkDialog (Contact contact) {
147
this.contact = contact;
148
set_title (_("Link Contact"));
149
set_transient_for (App.app.window);
152
add_buttons (_("Close"), ResponseType.CLOSE, null);
154
add_buttons (_("Cancel"), ResponseType.CANCEL, _("Link"), ResponseType.APPLY, null);
157
view = new View (contact.store, View.TextDisplay.STORES);
158
view.hide_contact (contact);
160
view.set_show_subset (View.Subset.OTHER);
162
view.set_show_subset (View.Subset.ALL_SEPARATED);
164
var matches = contact.store.aggregator.get_potential_matches (contact.individual, MatchResult.HIGH);
165
foreach (var ind in matches.keys) {
166
var c = Contact.from_individual (ind);
168
var result = matches.get (ind);
169
view.set_custom_sort_prio (c, (int) result);
173
var grid = new Grid ();
174
grid.set_row_spacing (6);
175
grid.set_column_homogeneous (true);
176
var container = (get_content_area () as Container);
177
grid.set_border_width (8);
178
container.add (grid);
180
var label = new Label ("");
182
label.set_markup (_("<span weight='bold'>Link contacts to %s</span>").printf (contact.display_name));
184
label.set_markup (_("<span weight='bold'>Select contact to link to</span>"));
185
label.set_valign (Align.CENTER);
186
label.set_halign (Align.CENTER);
187
label.set_ellipsize (Pango.EllipsizeMode.END);
188
grid.attach (label, 0, 0, 2, 1);
190
var list_frame = new Frame (null);
191
list_frame.get_style_context ().add_class ("contacts-list-frame");
192
grid.attach (list_frame, 0, 1, 1, 1);
194
list_grid = new Grid ();
195
list_grid.set_size_request (315, -1);
196
list_grid.set_hexpand (false);
197
list_frame.add (list_grid);
198
list_grid.set_orientation (Orientation.VERTICAL);
200
var toolbar = new Toolbar ();
201
toolbar.get_style_context ().add_class (STYLE_CLASS_PRIMARY_TOOLBAR);
202
toolbar.set_icon_size (IconSize.MENU);
203
toolbar.set_vexpand (false);
204
list_grid.add (toolbar);
206
filter_entry = new Entry ();
207
filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-find-symbolic");
208
filter_entry.changed.connect (filter_entry_changed);
209
filter_entry.icon_press.connect (filter_entry_clear);
211
var search_entry_item = new ToolItem ();
212
search_entry_item.is_important = false;
213
search_entry_item.set_expand (true);
214
search_entry_item.add (filter_entry);
215
toolbar.add (search_entry_item);
217
var scrolled = new ScrolledWindow(null, null);
218
scrolled.set_min_content_width (310);
219
scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
220
scrolled.set_vexpand (true);
221
scrolled.set_hexpand (true);
222
scrolled.set_shadow_type (ShadowType.NONE);
224
list_grid.add (scrolled);
226
view.selection_changed.connect ( (c) => {
227
selected_contact = c;
231
scrolled = new ScrolledWindow(null, null);
232
scrolled.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
233
scrolled.set_vexpand (true);
234
scrolled.set_hexpand (true);
235
scrolled.set_shadow_type (ShadowType.NONE);
236
grid.attach (scrolled, 1, 1, 1, 1);
238
persona_grid = new Grid ();
239
persona_grid.set_orientation (Orientation.VERTICAL);
240
persona_grid.set_border_width (4);
241
persona_grid.set_column_spacing (8);
242
scrolled.add_with_viewport (persona_grid);
244
response.connect ( (response_id) => {
245
if (response_id == ResponseType.APPLY &&
246
selected_contact != null) {
247
link_contacts.begin (selected_contact, contact, (obj, result) => {
248
var main_contact_name = selected_contact.display_name;
249
var linked_contact_name = contact.display_name;
250
var operation = link_contacts.end (result);
251
this.contacts_linked (main_contact_name, linked_contact_name, operation);
260
set_default_size (710, 510);
263
private void refilter () {
265
string str = filter_entry.get_text ();
270
str = Utils.canonicalize_for_search (str);
271
values = str.split(" ");
274
view.set_filter_values (values);
277
private bool filter_entry_changed_timeout () {
278
filter_entry_changed_id = 0;
283
private void filter_entry_changed (Editable editable) {
284
if (filter_entry_changed_id != 0)
285
Source.remove (filter_entry_changed_id);
287
filter_entry_changed_id = Timeout.add (300, filter_entry_changed_timeout);
289
if (filter_entry.get_text () == "")
290
filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-find-symbolic");
292
filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-clear-symbolic");
295
private void filter_entry_clear (EntryIconPosition position) {
296
filter_entry.set_text ("");