summaryrefslogtreecommitdiff
path: root/src/contacts-contact-pane.vala
blob: 2a16e2262a9738725914da4529f8c9d6580404a7 (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
/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.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 : Adw.Bin {

  private unowned Store store;

  private Contact? contact = null;

  [GtkChild]
  private unowned Gtk.Stack stack;

  [GtkChild]
  private unowned Adw.Clamp contact_sheet_clamp;
  private unowned ContactSheet? sheet = null;

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

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

  public signal void contacts_linked (LinkOperation operation);

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

  public void add_suggestion (Individual individual, Individual other) {
    unowned var parent_overlay = this.get_parent () as Gtk.Overlay;

    remove_suggestion_grid ();
    this.suggestion_grid = new LinkSuggestionGrid (other);
    this.suggestion_grid.valign = Gtk.Align.END;
    parent_overlay.add_overlay (this.suggestion_grid);

    this.suggestion_grid.suggestion_accepted.connect (() => {
      var to_link = new Gee.LinkedList<Individual> ();
      to_link.add (individual);
      to_link.add (other);
      var operation = new LinkOperation (this.store, to_link);
      this.contacts_linked (operation);
      remove_suggestion_grid ();
    });

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

  public void show_contact (Individual? individual) {
    if (individual == null) {
      this.contact = null;
      remove_contact_sheet ();
      this.stack.set_visible_child_name ("none-selected-page");
      return;
    }

    if (this.contact == null || this.contact.individual != individual)
      this.contact = new Contact.for_individual (individual, this.store);
    show_contact_sheet (this.contact);
  }

  private void show_contact_sheet (Contact contact) {
    return_if_fail (contact != null);

    remove_contact_sheet ();
    var contacts_sheet = new ContactSheet (contact);
    contacts_sheet.hexpand = true;
    this.sheet = contacts_sheet;
    this.contact_sheet_clamp.set_child (this.sheet);
    this.stack.set_visible_child_name ("contact-sheet-page");

    // Show potential link suggestions only if it's an existing contact
    if (contact.individual != null) {
      var matches = this.store.aggregator.get_potential_matches (contact.individual, MatchResult.HIGH);
      foreach (var i in matches.keys) {
        if (i != null && Utils.suggest_link_to (this.store, contact.individual, i)) {
          add_suggestion (contact.individual, 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_clamp.set_child (null);
    this.sheet = null;
  }

  private void create_contact_editor () {
    return_if_fail (this.contact != null);

    remove_contact_editor ();
    var contact_editor = new ContactEditor (this.contact);
    contact_editor.hexpand = true;
    this.editor = contact_editor;

    this.contact_editor_box.append (this.editor);
  }

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

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

  public void stop_editing (bool cancel = false) {
    return_if_fail (this.on_edit_mode);

    this.on_edit_mode = false;
    remove_contact_editor ();

    if (cancel) {
      if (this.contact != null) {
        this.stack.set_visible_child_name ("contact-sheet-page");
      } else {
        this.stack.set_visible_child_name ("none-selected-page");
      }
    } else {
      // Save changes if editing wasn't canceled
      apply_changes.begin (this.contact);
    }
  }

  private async void apply_changes (Contact contact) {
    // TODO: block changes to contact
    show_contact_sheet (contact);

    // Wait that the store gets quiescent if it isn't already
    if (!this.store.aggregator.is_quiescent) {
      ulong signal_id;
      SourceFunc callback = apply_changes.callback;
      signal_id = this.store.quiescent.connect (() => {
        callback ();
      });
      yield;
      disconnect (signal_id);
    }

    try {
      yield contact.apply_changes ();
    } catch (Error err) {
      warning ("Couldn't save changes: %s", err.message);
      // XXX do something better here
    }
    show_contact_sheet (contact);
  }

  public void edit_contact () {
    return_if_fail (this.contact != null);
    if (this.on_edit_mode)
      return;

    this.on_edit_mode = true;

    create_contact_editor ();
    this.stack.set_visible_child_name ("contact-editor-page");
  }

  public void new_contact () {
    this.contact = new Contact.for_new (this.store);
    if (this.on_edit_mode)
      return;

    this.on_edit_mode = true;

    create_contact_editor ();
    this.stack.set_visible_child_name ("contact-editor-page");
  }

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

    unowned var parent_overlay = this.get_parent () as Gtk.Overlay;
    parent_overlay.remove_overlay (suggestion_grid);
    this.suggestion_grid = null;
  }
}