blob: 9e78675a1e05e052330ce3d3e26a43dc1dd41927 [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -04002 * Copyright (C) 2015 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040024 * terms of the OpenSSL or SSLeay licenses, Savoir-faire Linux Inc.
Stepan Salenikovich9816a942015-04-22 17:49:16 -040025 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "contactsview.h"
32
33#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040034#include <glib/gi18n.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040035#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040036#include <categorizedcontactmodel.h>
37#include <personmodel.h>
38#include "utils/calling.h"
39#include <memory>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040040#include <globalinstances.h>
41#include "native/pixbufmanipulator.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040042#include <contactmethod.h>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040043#include "defines.h"
44#include "utils/models.h"
Stepan Salenikovich9d294492015-05-14 16:34:24 -040045#include <QtCore/QItemSelectionModel>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040046
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040047static constexpr const char* COPY_DATA_KEY = "copy_data";
Stepan Salenikovich9816a942015-04-22 17:49:16 -040048
49struct _ContactsView
50{
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040051 GtkBox parent;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040052};
53
54struct _ContactsViewClass
55{
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040056 GtkBoxClass parent_class;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040057};
58
59typedef struct _ContactsViewPrivate ContactsViewPrivate;
60
61struct _ContactsViewPrivate
62{
Stepan Salenikovich9d294492015-05-14 16:34:24 -040063 CategorizedContactModel::SortedProxy *q_sorted_proxy;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040064};
65
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040066G_DEFINE_TYPE_WITH_PRIVATE(ContactsView, contacts_view, GTK_TYPE_BOX);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040067
68#define CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONTACTS_VIEW_TYPE, ContactsViewPrivate))
69
70static void
71render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
72 GtkCellRenderer *cell,
73 GtkTreeModel *tree_model,
74 GtkTreeIter *iter,
75 G_GNUC_UNUSED gpointer data)
76{
77 /* check if this is a top level item (category),
Stepan Salenikovich81455562015-05-01 16:28:46 -040078 * or a bottom level item (contact method)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040079 * in this case we don't want to show a photo */
80 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
81 int depth = gtk_tree_path_get_depth(path);
82 gtk_tree_path_free(path);
83 if (depth == 2) {
84 /* get person */
85 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
86 if (idx.isValid()) {
87 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
88 Person *c = var_c.value<Person *>();
89 /* get photo */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040090 QVariant var_p = GlobalInstances::pixmapManipulator().contactPhoto(c, QSize(50, 50), false);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040091 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
92 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
93 return;
94 }
95 }
96
97 /* otherwise, make sure its an empty pixbuf */
98 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
99}
100
101static void
102render_name_and_contact_method(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
103 GtkCellRenderer *cell,
104 GtkTreeModel *tree_model,
105 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400106 GtkTreeView *treeview)
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400107{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400108 // check if this iter is selected
109 gboolean is_selected = FALSE;
110 if (GTK_IS_TREE_VIEW(treeview)) {
111 auto selection = gtk_tree_view_get_selection(treeview);
112 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
113 }
114
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400115 /**
116 * If contact (person), show the name and the contact method (number)
117 * underneath; if multiple contact methods, then indicate as such
118 *
119 * Otherwise just display the category or contact method
120 */
121 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
122 int depth = gtk_tree_path_get_depth(path);
123 gtk_tree_path_free(path);
124
125 gchar *text = NULL;
126
127 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
128 if (idx.isValid()) {
129 QVariant var = idx.data(Qt::DisplayRole);
130 if (depth == 1) {
131 /* category */
132 text = g_strdup_printf("<b>%s</b>", var.value<QString>().toUtf8().constData());
133 } else if (depth == 2) {
134 /* contact, check for contact methods */
135 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
136 if (var_c.isValid()) {
137 Person *c = var_c.value<Person *>();
138 switch (c->phoneNumbers().size()) {
139 case 0:
140 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
141 break;
142 case 1:
143 {
144 QString number;
145 QVariant var_n = c->phoneNumbers().first()->roleData(Qt::DisplayRole);
146 if (var_n.isValid())
147 number = var_n.value<QString>();
148
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400149 /* we want the color of the status text to be the default color if this iter is
150 * selected so that the treeview is able to invert it against the selection color */
151 if (is_selected) {
152 text = g_strdup_printf("%s\n %s",
153 c->formattedName().toUtf8().constData(),
154 number.toUtf8().constData());
155 } else {
156 text = g_strdup_printf("%s\n <span fgcolor=\"gray\">%s</span>",
157 c->formattedName().toUtf8().constData(),
158 number.toUtf8().constData());
159 }
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400160 break;
161 }
162 default:
163 /* more than one, for now don't show any of the contact methods */
164 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
165 break;
166 }
167 } else {
168 /* should never happen since depth 2 should always be a contact (person) */
169 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
170 }
171 } else {
172 /* contact method (or deeper??) */
173 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
174 }
175 }
176
177 g_object_set(G_OBJECT(cell), "markup", text, NULL);
178 g_free(text);
179
180 /* set the colour */
181 if ( depth == 1) {
182 /* nice blue taken from the ring logo */
183 GdkRGBA rgba = {0.2, 0.75294117647, 0.82745098039, 0.1};
184 g_object_set(G_OBJECT(cell), "cell-background-rgba", &rgba, NULL);
185 } else {
186 g_object_set(G_OBJECT(cell), "cell-background", NULL, NULL);
187 }
188}
189
190static void
191expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
192 GtkTreePath *path,
193 G_GNUC_UNUSED GtkTreeIter *iter,
194 GtkTreeView *treeview)
195{
196 if (gtk_tree_path_get_depth(path) == 2)
197 gtk_tree_view_expand_to_path(treeview, path);
198}
199
200static void
201activate_contact_item(GtkTreeView *tree_view,
202 GtkTreePath *path,
203 G_GNUC_UNUSED GtkTreeViewColumn *column,
204 G_GNUC_UNUSED gpointer user_data)
205{
206 /* expand / contract row */
207 if (gtk_tree_view_row_expanded(tree_view, path))
208 gtk_tree_view_collapse_row(tree_view, path);
209 else
210 gtk_tree_view_expand_row(tree_view, path, FALSE);
211
212 GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
213
214 /* get iter */
215 GtkTreeIter iter;
216 if (gtk_tree_model_get_iter(model, &iter, path)) {
217 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
218 if (idx.isValid()) {
219 int depth = gtk_tree_path_get_depth(path);
220 switch (depth) {
221 case 0:
222 case 1:
223 /* category, nothing to do */
224 break;
225 case 2:
226 {
227 /* contact (person), use contact method if there is only one */
228 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
229 if (var_c.isValid()) {
230 Person *c = var_c.value<Person *>();
231 if (c->phoneNumbers().size() == 1) {
232 /* call with contact method */
233 place_new_call(c->phoneNumbers().first());
234 }
235 }
236 break;
237 }
238 default:
239 {
240 /* contact method (or deeper) */
241 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
242 if (var_n.isValid()) {
243 /* call with contat method */
244 place_new_call(var_n.value<ContactMethod *>());
245 }
246 break;
247 }
248 }
249 }
250 }
251}
252
253static void
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400254copy_contact_info(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
255{
256 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
257 g_return_if_fail(data);
258 gchar* text = (gchar *)data;
259 GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
260 gtk_clipboard_set_text(clip, text, -1);
261}
262
263static gboolean
264contacts_popup_menu(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GtkTreeView *treeview)
265{
266 /* build popup menu when right clicking on contact item
267 * user should be able to copy the contact's name or "number".
268 * other functionality may be added later.
269 */
270
271 /* check for right click */
272 if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS)
273 return FALSE;
274
275 /* we don't want a popup menu for categories for now, so everything deeper
276 * than one */
277 GtkTreeIter iter;
278 GtkTreeModel *model;
279 GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
280 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
281 return FALSE;
282
283 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
284 int depth = gtk_tree_path_get_depth(path);
285 gtk_tree_path_free(path);
286
287 if (depth < 2)
288 return FALSE;
289
290 /* deeper than a category, so create a menu */
291 GtkWidget *menu = gtk_menu_new();
292 QModelIndex idx = get_index_from_selection(selection);
293
294 /* if depth == 2, it is a contact, offer to copy name, and if only one
295 * contact method exists then also the "number",
296 * if depth > 2, then its a contact method, so only offer to copy the number
297 */
298 if (depth == 2) {
299 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
300 if (var_c.isValid()) {
301 Person *c = var_c.value<Person *>();
302
303 /* copy name */
304 gchar *name = g_strdup_printf("%s", c->formattedName().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400305 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy name"));
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, name, (GDestroyNotify)g_free);
308 g_signal_connect(item,
309 "activate",
310 G_CALLBACK(copy_contact_info),
311 NULL);
312
313 /* copy number if there is only one */
314 if (c->phoneNumbers().size() == 1) {
315 gchar *number = g_strdup_printf("%s",c->phoneNumbers().first()->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400316 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400317 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
318 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
319 g_signal_connect(item,
320 "activate",
321 G_CALLBACK(copy_contact_info),
322 NULL);
323 }
324 }
325 } else if (depth > 2) {
326 /* copy number */
327 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
328 if (var_n.isValid()) {
329 ContactMethod *n = var_n.value<ContactMethod *>();
330 gchar *number = g_strdup_printf("%s",n->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400331 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400332 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
333 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
334 g_signal_connect(item,
335 "activate",
336 G_CALLBACK(copy_contact_info),
337 NULL);
338 }
339 }
340
341 /* show menu */
342 gtk_widget_show_all(menu);
343 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
344
345 return TRUE; /* we handled the event */
346}
347
348static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400349contacts_view_init(ContactsView *self)
350{
351 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400352
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400353 gtk_orientable_set_orientation(GTK_ORIENTABLE(self), GTK_ORIENTATION_VERTICAL);
354 /* need to be able to focus on widget so that we can auto-scroll to it */
355 gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400356
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400357 GtkWidget *treeview_contacts = gtk_tree_view_new();
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400358 /* set can-focus to false so that the scrollwindow doesn't jump to try to
359 * contain the top of the treeview */
360 gtk_widget_set_can_focus(treeview_contacts, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400361 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_contacts), FALSE);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400362 gtk_box_pack_start(GTK_BOX(self), treeview_contacts, TRUE, TRUE, 0);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400363
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400364 /* disable default search, we will handle it ourselves via LRC;
365 * otherwise the search steals input focus on key presses */
366 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(treeview_contacts), FALSE);
367
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400368 /* initial set up to be categorized by name and sorted alphabetically */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400369 priv->q_sorted_proxy = &CategorizedContactModel::SortedProxy::instance();
370 CategorizedContactModel::instance().setUnreachableHidden(true);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400371
372 /* for now we always want to sort by ascending order */
373 priv->q_sorted_proxy->model()->sort(0);
374
375 /* select default category (the first one, which is by name) */
376 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
377 priv->q_sorted_proxy->categoryModel()->index(0, 0),
378 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400379
380 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400381 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400382 1,
383 Qt::DisplayRole, G_TYPE_STRING);
384 gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_contacts), GTK_TREE_MODEL(contact_model));
385
386 /* photo and name/contact method column */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400387 GtkCellArea *area = gtk_cell_area_box_new();
388 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400389
390 /* photo renderer */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400391 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400392 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
393
394 /* get the photo */
395 gtk_tree_view_column_set_cell_data_func(
396 column,
397 renderer,
398 (GtkTreeCellDataFunc)render_contact_photo,
399 NULL,
400 NULL);
401
402 /* name and contact method renderer */
403 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich81455562015-05-01 16:28:46 -0400404 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400405 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
406
407 gtk_tree_view_column_set_cell_data_func(
408 column,
409 renderer,
410 (GtkTreeCellDataFunc)render_name_and_contact_method,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400411 treeview_contacts,
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400412 NULL);
413
414 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_contacts), column);
415 gtk_tree_view_column_set_resizable(column, TRUE);
416
417 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview_contacts));
418 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), treeview_contacts);
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400419 g_signal_connect(treeview_contacts, "button-press-event", G_CALLBACK(contacts_popup_menu), treeview_contacts);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400420 g_signal_connect(treeview_contacts, "row-activated", G_CALLBACK(activate_contact_item), NULL);
421
422 gtk_widget_show_all(GTK_WIDGET(self));
423}
424
425static void
426contacts_view_dispose(GObject *object)
427{
428 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
429}
430
431static void
432contacts_view_finalize(GObject *object)
433{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400434 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
435}
436
437static void
438contacts_view_class_init(ContactsViewClass *klass)
439{
440 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
441 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
442}
443
444GtkWidget *
445contacts_view_new()
446{
447 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
448
449 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400450}