blob: c0cfede1ed9db5a2937cdd2141d80ec96a11847d [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
47#define 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,
106 G_GNUC_UNUSED gpointer data)
107{
108 /**
109 * If contact (person), show the name and the contact method (number)
110 * underneath; if multiple contact methods, then indicate as such
111 *
112 * Otherwise just display the category or contact method
113 */
114 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
115 int depth = gtk_tree_path_get_depth(path);
116 gtk_tree_path_free(path);
117
118 gchar *text = NULL;
119
120 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
121 if (idx.isValid()) {
122 QVariant var = idx.data(Qt::DisplayRole);
123 if (depth == 1) {
124 /* category */
125 text = g_strdup_printf("<b>%s</b>", var.value<QString>().toUtf8().constData());
126 } else if (depth == 2) {
127 /* contact, check for contact methods */
128 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
129 if (var_c.isValid()) {
130 Person *c = var_c.value<Person *>();
131 switch (c->phoneNumbers().size()) {
132 case 0:
133 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
134 break;
135 case 1:
136 {
137 QString number;
138 QVariant var_n = c->phoneNumbers().first()->roleData(Qt::DisplayRole);
139 if (var_n.isValid())
140 number = var_n.value<QString>();
141
142 text = g_strdup_printf("%s\n <span fgcolor=\"gray\">%s</span>",
143 c->formattedName().toUtf8().constData(),
144 number.toUtf8().constData());
145 break;
146 }
147 default:
148 /* more than one, for now don't show any of the contact methods */
149 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
150 break;
151 }
152 } else {
153 /* should never happen since depth 2 should always be a contact (person) */
154 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
155 }
156 } else {
157 /* contact method (or deeper??) */
158 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
159 }
160 }
161
162 g_object_set(G_OBJECT(cell), "markup", text, NULL);
163 g_free(text);
164
165 /* set the colour */
166 if ( depth == 1) {
167 /* nice blue taken from the ring logo */
168 GdkRGBA rgba = {0.2, 0.75294117647, 0.82745098039, 0.1};
169 g_object_set(G_OBJECT(cell), "cell-background-rgba", &rgba, NULL);
170 } else {
171 g_object_set(G_OBJECT(cell), "cell-background", NULL, NULL);
172 }
173}
174
175static void
176expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
177 GtkTreePath *path,
178 G_GNUC_UNUSED GtkTreeIter *iter,
179 GtkTreeView *treeview)
180{
181 if (gtk_tree_path_get_depth(path) == 2)
182 gtk_tree_view_expand_to_path(treeview, path);
183}
184
185static void
186activate_contact_item(GtkTreeView *tree_view,
187 GtkTreePath *path,
188 G_GNUC_UNUSED GtkTreeViewColumn *column,
189 G_GNUC_UNUSED gpointer user_data)
190{
191 /* expand / contract row */
192 if (gtk_tree_view_row_expanded(tree_view, path))
193 gtk_tree_view_collapse_row(tree_view, path);
194 else
195 gtk_tree_view_expand_row(tree_view, path, FALSE);
196
197 GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
198
199 /* get iter */
200 GtkTreeIter iter;
201 if (gtk_tree_model_get_iter(model, &iter, path)) {
202 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
203 if (idx.isValid()) {
204 int depth = gtk_tree_path_get_depth(path);
205 switch (depth) {
206 case 0:
207 case 1:
208 /* category, nothing to do */
209 break;
210 case 2:
211 {
212 /* contact (person), use contact method if there is only one */
213 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
214 if (var_c.isValid()) {
215 Person *c = var_c.value<Person *>();
216 if (c->phoneNumbers().size() == 1) {
217 /* call with contact method */
218 place_new_call(c->phoneNumbers().first());
219 }
220 }
221 break;
222 }
223 default:
224 {
225 /* contact method (or deeper) */
226 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
227 if (var_n.isValid()) {
228 /* call with contat method */
229 place_new_call(var_n.value<ContactMethod *>());
230 }
231 break;
232 }
233 }
234 }
235 }
236}
237
238static void
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400239copy_contact_info(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
240{
241 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
242 g_return_if_fail(data);
243 gchar* text = (gchar *)data;
244 GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
245 gtk_clipboard_set_text(clip, text, -1);
246}
247
248static gboolean
249contacts_popup_menu(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GtkTreeView *treeview)
250{
251 /* build popup menu when right clicking on contact item
252 * user should be able to copy the contact's name or "number".
253 * other functionality may be added later.
254 */
255
256 /* check for right click */
257 if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS)
258 return FALSE;
259
260 /* we don't want a popup menu for categories for now, so everything deeper
261 * than one */
262 GtkTreeIter iter;
263 GtkTreeModel *model;
264 GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
265 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
266 return FALSE;
267
268 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
269 int depth = gtk_tree_path_get_depth(path);
270 gtk_tree_path_free(path);
271
272 if (depth < 2)
273 return FALSE;
274
275 /* deeper than a category, so create a menu */
276 GtkWidget *menu = gtk_menu_new();
277 QModelIndex idx = get_index_from_selection(selection);
278
279 /* if depth == 2, it is a contact, offer to copy name, and if only one
280 * contact method exists then also the "number",
281 * if depth > 2, then its a contact method, so only offer to copy the number
282 */
283 if (depth == 2) {
284 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
285 if (var_c.isValid()) {
286 Person *c = var_c.value<Person *>();
287
288 /* copy name */
289 gchar *name = g_strdup_printf("%s", c->formattedName().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400290 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy name"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400291 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
292 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, name, (GDestroyNotify)g_free);
293 g_signal_connect(item,
294 "activate",
295 G_CALLBACK(copy_contact_info),
296 NULL);
297
298 /* copy number if there is only one */
299 if (c->phoneNumbers().size() == 1) {
300 gchar *number = g_strdup_printf("%s",c->phoneNumbers().first()->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400301 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400302 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
303 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
304 g_signal_connect(item,
305 "activate",
306 G_CALLBACK(copy_contact_info),
307 NULL);
308 }
309 }
310 } else if (depth > 2) {
311 /* copy number */
312 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
313 if (var_n.isValid()) {
314 ContactMethod *n = var_n.value<ContactMethod *>();
315 gchar *number = g_strdup_printf("%s",n->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
326 /* show menu */
327 gtk_widget_show_all(menu);
328 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
329
330 return TRUE; /* we handled the event */
331}
332
333static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400334contacts_view_init(ContactsView *self)
335{
336 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400337
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400338 gtk_orientable_set_orientation(GTK_ORIENTABLE(self), GTK_ORIENTATION_VERTICAL);
339 /* need to be able to focus on widget so that we can auto-scroll to it */
340 gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400341
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400342 GtkWidget *treeview_contacts = gtk_tree_view_new();
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400343 /* set can-focus to false so that the scrollwindow doesn't jump to try to
344 * contain the top of the treeview */
345 gtk_widget_set_can_focus(treeview_contacts, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400346 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_contacts), FALSE);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400347 gtk_box_pack_start(GTK_BOX(self), treeview_contacts, TRUE, TRUE, 0);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400348
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400349 /* disable default search, we will handle it ourselves via LRC;
350 * otherwise the search steals input focus on key presses */
351 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(treeview_contacts), FALSE);
352
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400353 /* initial set up to be categorized by name and sorted alphabetically */
354 priv->q_sorted_proxy = CategorizedContactModel::SortedProxy::instance();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400355 CategorizedContactModel::instance()->setUnreachableHidden(true);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400356
357 /* for now we always want to sort by ascending order */
358 priv->q_sorted_proxy->model()->sort(0);
359
360 /* select default category (the first one, which is by name) */
361 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
362 priv->q_sorted_proxy->categoryModel()->index(0, 0),
363 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400364
365 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400366 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400367 1,
368 Qt::DisplayRole, G_TYPE_STRING);
369 gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_contacts), GTK_TREE_MODEL(contact_model));
370
371 /* photo and name/contact method column */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400372 GtkCellArea *area = gtk_cell_area_box_new();
373 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400374
375 /* photo renderer */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400376 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400377 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
378
379 /* get the photo */
380 gtk_tree_view_column_set_cell_data_func(
381 column,
382 renderer,
383 (GtkTreeCellDataFunc)render_contact_photo,
384 NULL,
385 NULL);
386
387 /* name and contact method renderer */
388 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich81455562015-05-01 16:28:46 -0400389 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400390 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
391
392 gtk_tree_view_column_set_cell_data_func(
393 column,
394 renderer,
395 (GtkTreeCellDataFunc)render_name_and_contact_method,
396 NULL,
397 NULL);
398
399 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_contacts), column);
400 gtk_tree_view_column_set_resizable(column, TRUE);
401
402 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview_contacts));
403 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), treeview_contacts);
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400404 g_signal_connect(treeview_contacts, "button-press-event", G_CALLBACK(contacts_popup_menu), treeview_contacts);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400405 g_signal_connect(treeview_contacts, "row-activated", G_CALLBACK(activate_contact_item), NULL);
406
407 gtk_widget_show_all(GTK_WIDGET(self));
408}
409
410static void
411contacts_view_dispose(GObject *object)
412{
413 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
414}
415
416static void
417contacts_view_finalize(GObject *object)
418{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400419 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
420}
421
422static void
423contacts_view_class_init(ContactsViewClass *klass)
424{
425 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
426 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
427}
428
429GtkWidget *
430contacts_view_new()
431{
432 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
433
434 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400435}