blob: 26067e078549fff611de89b52d3be09ab121e5a4 [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich9816a942015-04-22 17:49:16 -04003 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.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 3 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Stepan Salenikovich9816a942015-04-22 17:49:16 -040018 */
19
20#include "contactsview.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040024#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040025#include <categorizedcontactmodel.h>
26#include <personmodel.h>
27#include "utils/calling.h"
28#include <memory>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040029#include <globalinstances.h>
30#include "native/pixbufmanipulator.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040031#include <contactmethod.h>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040032#include "defines.h"
33#include "utils/models.h"
Stepan Salenikovich9d294492015-05-14 16:34:24 -040034#include <QtCore/QItemSelectionModel>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040035
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040036static constexpr const char* COPY_DATA_KEY = "copy_data";
Stepan Salenikovich9816a942015-04-22 17:49:16 -040037
38struct _ContactsView
39{
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040040 GtkTreeView parent;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040041};
42
43struct _ContactsViewClass
44{
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040045 GtkTreeViewClass parent_class;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040046};
47
48typedef struct _ContactsViewPrivate ContactsViewPrivate;
49
50struct _ContactsViewPrivate
51{
Stepan Salenikovich9d294492015-05-14 16:34:24 -040052 CategorizedContactModel::SortedProxy *q_sorted_proxy;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040053};
54
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040055G_DEFINE_TYPE_WITH_PRIVATE(ContactsView, contacts_view, GTK_TYPE_TREE_VIEW);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040056
57#define CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONTACTS_VIEW_TYPE, ContactsViewPrivate))
58
59static void
60render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
61 GtkCellRenderer *cell,
62 GtkTreeModel *tree_model,
63 GtkTreeIter *iter,
64 G_GNUC_UNUSED gpointer data)
65{
66 /* check if this is a top level item (category),
Stepan Salenikovich81455562015-05-01 16:28:46 -040067 * or a bottom level item (contact method)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040068 * in this case we don't want to show a photo */
69 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
70 int depth = gtk_tree_path_get_depth(path);
71 gtk_tree_path_free(path);
72 if (depth == 2) {
73 /* get person */
74 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
75 if (idx.isValid()) {
76 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
77 Person *c = var_c.value<Person *>();
78 /* get photo */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040079 QVariant var_p = GlobalInstances::pixmapManipulator().contactPhoto(c, QSize(50, 50), false);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040080 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
81 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
82 return;
83 }
84 }
85
86 /* otherwise, make sure its an empty pixbuf */
87 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
88}
89
90static void
91render_name_and_contact_method(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
92 GtkCellRenderer *cell,
93 GtkTreeModel *tree_model,
94 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -040095 GtkTreeView *treeview)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040096{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -040097 // check if this iter is selected
98 gboolean is_selected = FALSE;
99 if (GTK_IS_TREE_VIEW(treeview)) {
100 auto selection = gtk_tree_view_get_selection(treeview);
101 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
102 }
103
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400104 /**
105 * If contact (person), show the name and the contact method (number)
106 * underneath; if multiple contact methods, then indicate as such
107 *
108 * Otherwise just display the category or contact method
109 */
110 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
111 int depth = gtk_tree_path_get_depth(path);
112 gtk_tree_path_free(path);
113
114 gchar *text = NULL;
115
116 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
117 if (idx.isValid()) {
118 QVariant var = idx.data(Qt::DisplayRole);
119 if (depth == 1) {
120 /* category */
121 text = g_strdup_printf("<b>%s</b>", var.value<QString>().toUtf8().constData());
122 } else if (depth == 2) {
123 /* contact, check for contact methods */
124 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
125 if (var_c.isValid()) {
126 Person *c = var_c.value<Person *>();
127 switch (c->phoneNumbers().size()) {
128 case 0:
129 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
130 break;
131 case 1:
132 {
133 QString number;
134 QVariant var_n = c->phoneNumbers().first()->roleData(Qt::DisplayRole);
135 if (var_n.isValid())
136 number = var_n.value<QString>();
137
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400138 /* we want the color of the status text to be the default color if this iter is
139 * selected so that the treeview is able to invert it against the selection color */
140 if (is_selected) {
141 text = g_strdup_printf("%s\n %s",
142 c->formattedName().toUtf8().constData(),
143 number.toUtf8().constData());
144 } else {
145 text = g_strdup_printf("%s\n <span fgcolor=\"gray\">%s</span>",
146 c->formattedName().toUtf8().constData(),
147 number.toUtf8().constData());
148 }
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400149 break;
150 }
151 default:
152 /* more than one, for now don't show any of the contact methods */
153 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
154 break;
155 }
156 } else {
157 /* should never happen since depth 2 should always be a contact (person) */
158 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
159 }
160 } else {
161 /* contact method (or deeper??) */
162 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
163 }
164 }
165
166 g_object_set(G_OBJECT(cell), "markup", text, NULL);
167 g_free(text);
168
169 /* set the colour */
170 if ( depth == 1) {
171 /* nice blue taken from the ring logo */
172 GdkRGBA rgba = {0.2, 0.75294117647, 0.82745098039, 0.1};
173 g_object_set(G_OBJECT(cell), "cell-background-rgba", &rgba, NULL);
174 } else {
175 g_object_set(G_OBJECT(cell), "cell-background", NULL, NULL);
176 }
177}
178
179static void
180expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
181 GtkTreePath *path,
182 G_GNUC_UNUSED GtkTreeIter *iter,
183 GtkTreeView *treeview)
184{
185 if (gtk_tree_path_get_depth(path) == 2)
186 gtk_tree_view_expand_to_path(treeview, path);
187}
188
189static void
190activate_contact_item(GtkTreeView *tree_view,
191 GtkTreePath *path,
192 G_GNUC_UNUSED GtkTreeViewColumn *column,
193 G_GNUC_UNUSED gpointer user_data)
194{
195 /* expand / contract row */
196 if (gtk_tree_view_row_expanded(tree_view, path))
197 gtk_tree_view_collapse_row(tree_view, path);
198 else
199 gtk_tree_view_expand_row(tree_view, path, FALSE);
200
201 GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
202
203 /* get iter */
204 GtkTreeIter iter;
205 if (gtk_tree_model_get_iter(model, &iter, path)) {
206 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
207 if (idx.isValid()) {
208 int depth = gtk_tree_path_get_depth(path);
209 switch (depth) {
210 case 0:
211 case 1:
212 /* category, nothing to do */
213 break;
214 case 2:
215 {
216 /* contact (person), use contact method if there is only one */
217 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
218 if (var_c.isValid()) {
219 Person *c = var_c.value<Person *>();
220 if (c->phoneNumbers().size() == 1) {
221 /* call with contact method */
222 place_new_call(c->phoneNumbers().first());
223 }
224 }
225 break;
226 }
227 default:
228 {
229 /* contact method (or deeper) */
230 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
231 if (var_n.isValid()) {
232 /* call with contat method */
233 place_new_call(var_n.value<ContactMethod *>());
234 }
235 break;
236 }
237 }
238 }
239 }
240}
241
242static void
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400243copy_contact_info(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
244{
245 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
246 g_return_if_fail(data);
247 gchar* text = (gchar *)data;
248 GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
249 gtk_clipboard_set_text(clip, text, -1);
250}
251
252static gboolean
253contacts_popup_menu(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GtkTreeView *treeview)
254{
255 /* build popup menu when right clicking on contact item
256 * user should be able to copy the contact's name or "number".
257 * other functionality may be added later.
258 */
259
260 /* check for right click */
261 if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS)
262 return FALSE;
263
264 /* we don't want a popup menu for categories for now, so everything deeper
265 * than one */
266 GtkTreeIter iter;
267 GtkTreeModel *model;
268 GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
269 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
270 return FALSE;
271
272 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
273 int depth = gtk_tree_path_get_depth(path);
274 gtk_tree_path_free(path);
275
276 if (depth < 2)
277 return FALSE;
278
279 /* deeper than a category, so create a menu */
280 GtkWidget *menu = gtk_menu_new();
281 QModelIndex idx = get_index_from_selection(selection);
282
283 /* if depth == 2, it is a contact, offer to copy name, and if only one
284 * contact method exists then also the "number",
285 * if depth > 2, then its a contact method, so only offer to copy the number
286 */
287 if (depth == 2) {
288 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
289 if (var_c.isValid()) {
290 Person *c = var_c.value<Person *>();
291
292 /* copy name */
293 gchar *name = g_strdup_printf("%s", c->formattedName().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400294 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy name"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400295 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
296 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, name, (GDestroyNotify)g_free);
297 g_signal_connect(item,
298 "activate",
299 G_CALLBACK(copy_contact_info),
300 NULL);
301
302 /* copy number if there is only one */
303 if (c->phoneNumbers().size() == 1) {
304 gchar *number = g_strdup_printf("%s",c->phoneNumbers().first()->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400305 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400306 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
307 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
308 g_signal_connect(item,
309 "activate",
310 G_CALLBACK(copy_contact_info),
311 NULL);
312 }
313 }
314 } else if (depth > 2) {
315 /* copy number */
316 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
317 if (var_n.isValid()) {
318 ContactMethod *n = var_n.value<ContactMethod *>();
319 gchar *number = g_strdup_printf("%s",n->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400320 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400321 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
322 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
323 g_signal_connect(item,
324 "activate",
325 G_CALLBACK(copy_contact_info),
326 NULL);
327 }
328 }
329
330 /* show menu */
331 gtk_widget_show_all(menu);
332 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
333
334 return TRUE; /* we handled the event */
335}
336
337static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400338contacts_view_init(ContactsView *self)
339{
340 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400341
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400342 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400343
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400344 /* disable default search, we will handle it ourselves;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400345 * otherwise the search steals input focus on key presses */
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400346 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400347
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400348 /* initial set up to be categorized by name and sorted alphabetically */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400349 priv->q_sorted_proxy = &CategorizedContactModel::SortedProxy::instance();
350 CategorizedContactModel::instance().setUnreachableHidden(true);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400351
352 /* for now we always want to sort by ascending order */
353 priv->q_sorted_proxy->model()->sort(0);
354
355 /* select default category (the first one, which is by name) */
356 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
357 priv->q_sorted_proxy->categoryModel()->index(0, 0),
358 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400359
360 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400361 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400362 1,
363 Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400364 gtk_tree_view_set_model(GTK_TREE_VIEW(self), GTK_TREE_MODEL(contact_model));
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400365
366 /* photo and name/contact method column */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400367 GtkCellArea *area = gtk_cell_area_box_new();
368 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400369
370 /* photo renderer */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400371 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400372 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
373
374 /* get the photo */
375 gtk_tree_view_column_set_cell_data_func(
376 column,
377 renderer,
378 (GtkTreeCellDataFunc)render_contact_photo,
379 NULL,
380 NULL);
381
382 /* name and contact method renderer */
383 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich81455562015-05-01 16:28:46 -0400384 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400385 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
386
387 gtk_tree_view_column_set_cell_data_func(
388 column,
389 renderer,
390 (GtkTreeCellDataFunc)render_name_and_contact_method,
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400391 self,
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400392 NULL);
393
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400394 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400395 gtk_tree_view_column_set_resizable(column, TRUE);
396
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400397 gtk_tree_view_expand_all(GTK_TREE_VIEW(self));
398 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), self);
399 g_signal_connect(self, "button-press-event", G_CALLBACK(contacts_popup_menu), self);
400 g_signal_connect(self, "row-activated", G_CALLBACK(activate_contact_item), NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400401
402 gtk_widget_show_all(GTK_WIDGET(self));
403}
404
405static void
406contacts_view_dispose(GObject *object)
407{
408 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
409}
410
411static void
412contacts_view_finalize(GObject *object)
413{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400414 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
415}
416
417static void
418contacts_view_class_init(ContactsViewClass *klass)
419{
420 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
421 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
422}
423
424GtkWidget *
425contacts_view_new()
426{
427 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
428
429 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400430}