summaryrefslogtreecommitdiff
path: root/src/contacts-contact-pane.vala
blob: eae68ae16c9d1886ad733dfda07df4886d56d0eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * 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 Folks;

const int PROFILE_SIZE = 128;

/**
 * The ContactPage is the right pane. It consists of 3 possible pages:
 * a page if nothing is selected, a ContactSheet to view contact information,
 * and a ContactEditor to edit contact information.
 */
[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-contact-pane.ui")]
public class Contacts.ContactPane : Gtk.ScrolledWindow {

  private MainWindow main_window;

  private Store store;

  public Individual? individual { get; set; default = null; }

  [GtkChild]
  private Gtk.Stack stack;

  [GtkChild]
  private Hdy.StatusPage none_selected_page;

  [GtkChild]
  private Gtk.Container contact_sheet_page;
  private ContactSheet? sheet = null;

  [GtkChild]
  private Gtk.Box contact_editor_page;
  private ContactEditor? editor = null;

  public bool on_edit_mode = false;
  private LinkSuggestionGrid? suggestion_grid = null;

  /* Signals */
  public signal void contacts_linked (string? main_contact, string linked_contact, LinkOperation operation);
  /**
   * Passes the changed display name to all listeners after edit mode has been completed.
   */
  public signal void display_name_changed (string new_display_name);


  public ContactPane (MainWindow main_window, Store contacts_store) {
    this.main_window = main_window;
    this.store = contacts_store;
  }

  public void add_suggestion (Individual i) {
    var parent_overlay = this.get_parent () as Gtk.Overlay;

    remove_suggestion_grid ();
    this.suggestion_grid = new LinkSuggestionGrid (i);
    parent_overlay.add_overlay (this.suggestion_grid);

    this.suggestion_grid.suggestion_accepted.connect ( () => {
        var linked_contact = this.individual.display_name;
        var operation = new LinkOperation (this.store);
        var to_link = new Gee.LinkedList<Individual> ();
        to_link.add (this.individual);
        to_link.add (i);
        operation.execute.begin (to_link);
        this.contacts_linked (null, linked_contact, operation);
        remove_suggestion_grid ();
      });

    this.suggestion_grid.suggestion_rejected.connect ( () => {
        /* TODO: Add undo */
        store.add_no_suggest_link (this.individual, i);
        remove_suggestion_grid ();
      });
  }

  public void show_contact (Individual? individual) {
    if (this.individual == individual)
      return;

    this.individual = individual;

    if (this.individual != null) {
      show_contact_sheet ();
    } else {
      remove_contact_sheet ();
      this.stack.set_visible_child (this.none_selected_page);
    }
  }

  private void show_contact_sheet () {
    assert (this.individual != null);

    remove_contact_sheet();
    this.sheet = new ContactSheet (this.individual, this.store);
    this.contact_sheet_page.add (this.sheet);
    this.stack.set_visible_child (this.contact_sheet_page);

    var matches = this.store.aggregator.get_potential_matches (this.individual, MatchResult.HIGH);
    foreach (var i in matches.keys) {
      if (i != null && Contacts.Utils.suggest_link_to (this.store, this.individual, i)) {
        add_suggestion (i);
        break;
      }
    }
  }

  private void remove_contact_sheet () {
    if (this.sheet == null)
      return;

    // Remove the suggestion grid that goes along with it.
    remove_suggestion_grid ();

    this.contact_sheet_page.remove (this.sheet);
    this.sheet.destroy();
    this.sheet = null;
  }

  private void create_contact_editor () {
    remove_contact_editor ();

    this.editor = new ContactEditor (this.individual, store.aggregator);

    this.contact_editor_page.add (this.editor);
  }

  private void remove_contact_editor () {
    if (this.editor == null)
      return;

    this.contact_editor_page.remove (this.editor);
    this.editor = null;
  }

  private void start_editing() {
    if (this.on_edit_mode || this.individual == null)
      return;

    this.on_edit_mode = true;

    create_contact_editor ();
    this.stack.set_visible_child (this.contact_editor_page);
  }

  public void stop_editing (bool cancel = false) {
    if (!this.on_edit_mode)
      return;

    this.on_edit_mode = false;
    remove_contact_editor ();

    if (cancel) {
      var fake_individual = individual as FakeIndividual;
      if (fake_individual != null && fake_individual.real_individual != null) {
        // Reset individual on to the real one
        this.individual = fake_individual.real_individual;
        this.stack.set_visible_child (this.contact_sheet_page);
      } else {
        this.stack.set_visible_child (this.none_selected_page);
      }
      return;
    }

    /* Save changes if editing wasn't canceled */
    apply_changes.begin ();
  }

  private async void apply_changes () {
    /* Show fake contact to the user */
    /* TODO: block changes to fake contact */
    show_contact_sheet ();
    var fake_individual = individual as FakeIndividual;
    if (fake_individual != null && fake_individual.real_individual == null) {
      // Create a new persona in the primary store based on the fake persona
      yield create_contact (fake_individual.primary_persona);
    } else {
      yield fake_individual.apply_changes_to_real ();
      /* Todo: we need to check if the changes where applied to the contact */
      this.individual = fake_individual.real_individual;
    }

    /* Replace fake contact with real contact */
    show_contact_sheet ();
  }

  public void edit_contact () {
    this.individual = new FakeIndividual.from_real (this.individual);
    start_editing ();
  }

  public void new_contact () {
    var details = new HashTable<string, Value?> (str_hash, str_equal);
    string[] writeable_properties;
    // TODO: make sure we have a primary_store
    if (this.store.aggregator.primary_store != null) {
      // FIXME: We shouldn't use this list but there isn't an other way to find writeable_properties, and we should expect that all properties are writeable
      writeable_properties = this.store.aggregator.primary_store.always_writeable_properties;
    } else {
      writeable_properties = {};
    }

    var fake_persona = new FakePersona (FakePersonaStore.the_store(), writeable_properties, details);
    var fake_personas = new Gee.HashSet<FakePersona> ();
    fake_personas.add (fake_persona);
    this.individual = new FakeIndividual(fake_personas);

    start_editing ();
  }

  // Create a new contact from the FakePersona
  public async void create_contact (FakePersona fake_persona) {
    var details = fake_persona.get_details ();

    if (this.store.aggregator.primary_store == null) {
      show_message_dialog (_("No primary addressbook configured"));
      return;
    }

    // Create the contact
    var primary_store = this.store.aggregator.primary_store;
    Persona? persona = null;
    try {
      persona = yield primary_store.add_persona_from_details (details);
    } catch (Error e) {
      show_message_dialog (_("Unable to create new contacts: %s").printf (e.message));
      this.main_window.set_shown_contact (null);
      return;
    }

    // Now show the real persona to the user
    var individual = persona.individual;

    if (individual != null) {
      //FIXME: This causes a flicker, especially visible when an avatar is set
      this.main_window.set_shown_contact (individual);
    } else {
      show_message_dialog (_("Unable to find newly created contact"));
      this.main_window.set_shown_contact (null);
    }
  }

  private void show_message_dialog (string message) {
    var dialog =
        new Gtk.MessageDialog (this.main_window,
                               Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
                               Gtk.MessageType.ERROR,
                               Gtk.ButtonsType.OK,
                               "%s", message);
    dialog.run ();
    dialog.destroy ();
  }

  private void remove_suggestion_grid () {
    if (this.suggestion_grid == null)
      return;

    this.suggestion_grid.destroy ();
    this.suggestion_grid = null;
  }
}