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

« back to all changes in this revision

Viewing changes to src/contacts-link-dialog.vala

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Jeremy Bicha, Michael Biebl
  • Date: 2013-09-19 18:23:06 UTC
  • mfrom: (1.3.10) (0.3.4 experimental)
  • mto: This revision was merged to the branch mainline in revision 40.
  • Revision ID: package-import@ubuntu.com-20130919182306-rcatwotzg94pr884
Tags: 3.8.3-1
[ Jeremy Bicha ]
* debian/control.in:
  - Drop alternate build-depends on valac-0.18 since it's no longer
    in Debian

[ Michael Biebl ]
* New upstream release.
* Loosen Build-Depends on libgnome-desktop-3-dev, we do not strictly require
  version (>= 3.6.0) which is not yet available in unstable.
* Bump Standards-Version to 3.9.4. No further changes.
* Upload to unstable.

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
 
 
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);
26
 
  }
27
 
 
28
 
  private Contact contact;
29
 
  private Contact? selected_contact;
30
 
  private Entry filter_entry;
31
 
  private View view;
32
 
  private Grid list_grid;
33
 
  private Grid persona_grid;
34
 
  private uint filter_entry_changed_id;
35
 
 
36
 
  public signal void contacts_linked (string? main_contact, string linked_contact, LinkOperation operation);
37
 
  
38
 
  private void update_contact () {
39
 
    // Remove previous personas
40
 
    foreach (var w in persona_grid.get_children ()) {
41
 
      w.destroy ();
42
 
    }
43
 
 
44
 
    if (selected_contact == null)
45
 
      return;
46
 
 
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);
50
 
      });
51
 
    image_frame.set_hexpand (false);
52
 
    persona_grid.attach (image_frame, 0, 0, 1, 2);
53
 
 
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);
59
 
    label.xalign = 0.0f;
60
 
    label.set_ellipsize (Pango.EllipsizeMode.END);
61
 
    persona_grid.attach (label, 1, 0, 1, 1);
62
 
 
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);
68
 
    label.xalign = 0.0f;
69
 
    label.set_ellipsize (Pango.EllipsizeMode.END);
70
 
    persona_grid.attach (label, 1, 1, 1, 1);
71
 
 
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);
79
 
 
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) {
93
 
                operation.undo ();
94
 
                undo_bar.destroy ();
95
 
              }
96
 
            });
97
 
            Timeout.add (5000, () => {
98
 
              undo_bar.destroy ();
99
 
              return false;
100
 
            });
101
 
            list_grid.add (undo_bar);
102
 
            undo_bar.show_all ();
103
 
          });
104
 
      });
105
 
    }
106
 
 
107
 
    var grid = new Grid ();
108
 
    grid.set_orientation (Orientation.VERTICAL);
109
 
    grid.set_border_width (8);
110
 
 
111
 
    persona_grid.attach (grid, 0, 2, 3, 1);
112
 
 
113
 
 
114
 
    var emails = Contact.sort_fields<EmailFieldDetails>(selected_contact.individual.email_addresses);
115
 
    if (!emails.is_empty) {
116
 
      label = new Label (_("Email"));
117
 
      label.xalign = 0.0f;
118
 
      grid.add (label);
119
 
      foreach (var email in emails) {
120
 
        label = new Label (email.value);
121
 
        label.set_ellipsize (Pango.EllipsizeMode.END);
122
 
        label.xalign = 0.0f;
123
 
        grid.add (label);
124
 
      }
125
 
      label = new Label ("");
126
 
      label.xalign = 0.0f;
127
 
      grid.add (label);
128
 
    }
129
 
 
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"));
133
 
      label.xalign = 0.0f;
134
 
      grid.add (label);
135
 
      foreach (var phone_number in phone_numbers) {
136
 
        label = new Label (phone_number.value);
137
 
        label.set_ellipsize (Pango.EllipsizeMode.END);
138
 
        label.xalign = 0.0f;
139
 
        grid.add (label);
140
 
      }
141
 
    }
142
 
 
143
 
    persona_grid.show_all ();
144
 
  }
145
 
 
146
 
  public LinkDialog (Contact contact) {
147
 
    this.contact = contact;
148
 
    set_title (_("Link Contact"));
149
 
    set_transient_for (App.app.window);
150
 
    set_modal (true);
151
 
    if (contact.is_main)
152
 
      add_buttons (_("Close"), ResponseType.CLOSE, null);
153
 
    else {
154
 
      add_buttons (_("Cancel"), ResponseType.CANCEL, _("Link"), ResponseType.APPLY, null);
155
 
    }
156
 
 
157
 
    view = new View (contact.store, View.TextDisplay.STORES);
158
 
    view.hide_contact (contact);
159
 
    if (contact.is_main)
160
 
      view.set_show_subset (View.Subset.OTHER);
161
 
    else
162
 
      view.set_show_subset (View.Subset.ALL_SEPARATED);
163
 
 
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);
167
 
      if (c != null) {
168
 
        var result = matches.get (ind);
169
 
        view.set_custom_sort_prio (c, (int) result);
170
 
      }
171
 
    }
172
 
 
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);
179
 
 
180
 
    var label = new Label ("");
181
 
    if (contact.is_main)
182
 
      label.set_markup (_("<span weight='bold'>Link contacts to %s</span>").printf (contact.display_name));
183
 
    else
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);
189
 
 
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);
193
 
 
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);
199
 
 
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);
205
 
 
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);
210
 
 
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);
216
 
 
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);
223
 
    scrolled.add (view);
224
 
    list_grid.add (scrolled);
225
 
 
226
 
    view.selection_changed.connect ( (c) => {
227
 
        selected_contact = c;
228
 
        update_contact ();
229
 
      });
230
 
 
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);
237
 
 
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);
243
 
 
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);
252
 
          this.destroy ();
253
 
        });
254
 
      } else
255
 
        this.destroy ();
256
 
 
257
 
      this.hide ();
258
 
    });
259
 
 
260
 
    set_default_size (710, 510);
261
 
  }
262
 
 
263
 
  private void refilter () {
264
 
    string []? values;
265
 
    string str = filter_entry.get_text ();
266
 
 
267
 
    if (str.length == 0)
268
 
      values = null;
269
 
    else {
270
 
      str = Utils.canonicalize_for_search (str);
271
 
      values = str.split(" ");
272
 
    }
273
 
 
274
 
    view.set_filter_values (values);
275
 
  }
276
 
 
277
 
  private bool filter_entry_changed_timeout () {
278
 
    filter_entry_changed_id = 0;
279
 
    refilter ();
280
 
    return false;
281
 
  }
282
 
 
283
 
  private void filter_entry_changed (Editable editable) {
284
 
    if (filter_entry_changed_id != 0)
285
 
      Source.remove (filter_entry_changed_id);
286
 
 
287
 
    filter_entry_changed_id = Timeout.add (300, filter_entry_changed_timeout);
288
 
 
289
 
    if (filter_entry.get_text () == "")
290
 
      filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-find-symbolic");
291
 
    else
292
 
      filter_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "edit-clear-symbolic");
293
 
  }
294
 
 
295
 
  private void filter_entry_clear (EntryIconPosition position) {
296
 
    filter_entry.set_text ("");
297
 
  }
298
 
}