blob: 8b0f93e895a70c22fde645ff6e228fa6b787c17c [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * 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
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * 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>
34#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040035#include <categorizedcontactmodel.h>
36#include <personmodel.h>
37#include "utils/calling.h"
38#include <memory>
39#include "delegates/pixbufdelegate.h"
40#include <contactmethod.h>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040041#include "defines.h"
42#include "utils/models.h"
Stepan Salenikovich9d294492015-05-14 16:34:24 -040043#include <QtCore/QItemSelectionModel>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040044
45#define COPY_DATA_KEY "copy_data"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040046
47struct _ContactsView
48{
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040049 GtkBox parent;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040050};
51
52struct _ContactsViewClass
53{
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040054 GtkBoxClass parent_class;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040055};
56
57typedef struct _ContactsViewPrivate ContactsViewPrivate;
58
59struct _ContactsViewPrivate
60{
Stepan Salenikovich9d294492015-05-14 16:34:24 -040061 CategorizedContactModel::SortedProxy *q_sorted_proxy;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040062};
63
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -040064G_DEFINE_TYPE_WITH_PRIVATE(ContactsView, contacts_view, GTK_TYPE_BOX);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040065
66#define CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONTACTS_VIEW_TYPE, ContactsViewPrivate))
67
68static void
69render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
70 GtkCellRenderer *cell,
71 GtkTreeModel *tree_model,
72 GtkTreeIter *iter,
73 G_GNUC_UNUSED gpointer data)
74{
75 /* check if this is a top level item (category),
Stepan Salenikovich81455562015-05-01 16:28:46 -040076 * or a bottom level item (contact method)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040077 * in this case we don't want to show a photo */
78 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
79 int depth = gtk_tree_path_get_depth(path);
80 gtk_tree_path_free(path);
81 if (depth == 2) {
82 /* get person */
83 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
84 if (idx.isValid()) {
85 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
86 Person *c = var_c.value<Person *>();
87 /* get photo */
88 QVariant var_p = PixbufDelegate::instance()->contactPhoto(c, QSize(50, 50), false);
89 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
90 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
91 return;
92 }
93 }
94
95 /* otherwise, make sure its an empty pixbuf */
96 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
97}
98
99static void
100render_name_and_contact_method(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
101 GtkCellRenderer *cell,
102 GtkTreeModel *tree_model,
103 GtkTreeIter *iter,
104 G_GNUC_UNUSED gpointer data)
105{
106 /**
107 * If contact (person), show the name and the contact method (number)
108 * underneath; if multiple contact methods, then indicate as such
109 *
110 * Otherwise just display the category or contact method
111 */
112 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
113 int depth = gtk_tree_path_get_depth(path);
114 gtk_tree_path_free(path);
115
116 gchar *text = NULL;
117
118 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
119 if (idx.isValid()) {
120 QVariant var = idx.data(Qt::DisplayRole);
121 if (depth == 1) {
122 /* category */
123 text = g_strdup_printf("<b>%s</b>", var.value<QString>().toUtf8().constData());
124 } else if (depth == 2) {
125 /* contact, check for contact methods */
126 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
127 if (var_c.isValid()) {
128 Person *c = var_c.value<Person *>();
129 switch (c->phoneNumbers().size()) {
130 case 0:
131 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
132 break;
133 case 1:
134 {
135 QString number;
136 QVariant var_n = c->phoneNumbers().first()->roleData(Qt::DisplayRole);
137 if (var_n.isValid())
138 number = var_n.value<QString>();
139
140 text = g_strdup_printf("%s\n <span fgcolor=\"gray\">%s</span>",
141 c->formattedName().toUtf8().constData(),
142 number.toUtf8().constData());
143 break;
144 }
145 default:
146 /* more than one, for now don't show any of the contact methods */
147 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
148 break;
149 }
150 } else {
151 /* should never happen since depth 2 should always be a contact (person) */
152 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
153 }
154 } else {
155 /* contact method (or deeper??) */
156 text = g_strdup_printf("%s", var.value<QString>().toUtf8().constData());
157 }
158 }
159
160 g_object_set(G_OBJECT(cell), "markup", text, NULL);
161 g_free(text);
162
163 /* set the colour */
164 if ( depth == 1) {
165 /* nice blue taken from the ring logo */
166 GdkRGBA rgba = {0.2, 0.75294117647, 0.82745098039, 0.1};
167 g_object_set(G_OBJECT(cell), "cell-background-rgba", &rgba, NULL);
168 } else {
169 g_object_set(G_OBJECT(cell), "cell-background", NULL, NULL);
170 }
171}
172
173static void
174expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
175 GtkTreePath *path,
176 G_GNUC_UNUSED GtkTreeIter *iter,
177 GtkTreeView *treeview)
178{
179 if (gtk_tree_path_get_depth(path) == 2)
180 gtk_tree_view_expand_to_path(treeview, path);
181}
182
183static void
184activate_contact_item(GtkTreeView *tree_view,
185 GtkTreePath *path,
186 G_GNUC_UNUSED GtkTreeViewColumn *column,
187 G_GNUC_UNUSED gpointer user_data)
188{
189 /* expand / contract row */
190 if (gtk_tree_view_row_expanded(tree_view, path))
191 gtk_tree_view_collapse_row(tree_view, path);
192 else
193 gtk_tree_view_expand_row(tree_view, path, FALSE);
194
195 GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
196
197 /* get iter */
198 GtkTreeIter iter;
199 if (gtk_tree_model_get_iter(model, &iter, path)) {
200 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
201 if (idx.isValid()) {
202 int depth = gtk_tree_path_get_depth(path);
203 switch (depth) {
204 case 0:
205 case 1:
206 /* category, nothing to do */
207 break;
208 case 2:
209 {
210 /* contact (person), use contact method if there is only one */
211 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
212 if (var_c.isValid()) {
213 Person *c = var_c.value<Person *>();
214 if (c->phoneNumbers().size() == 1) {
215 /* call with contact method */
216 place_new_call(c->phoneNumbers().first());
217 }
218 }
219 break;
220 }
221 default:
222 {
223 /* contact method (or deeper) */
224 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
225 if (var_n.isValid()) {
226 /* call with contat method */
227 place_new_call(var_n.value<ContactMethod *>());
228 }
229 break;
230 }
231 }
232 }
233 }
234}
235
236static void
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400237copy_contact_info(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
238{
239 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
240 g_return_if_fail(data);
241 gchar* text = (gchar *)data;
242 GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
243 gtk_clipboard_set_text(clip, text, -1);
244}
245
246static gboolean
247contacts_popup_menu(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GtkTreeView *treeview)
248{
249 /* build popup menu when right clicking on contact item
250 * user should be able to copy the contact's name or "number".
251 * other functionality may be added later.
252 */
253
254 /* check for right click */
255 if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS)
256 return FALSE;
257
258 /* we don't want a popup menu for categories for now, so everything deeper
259 * than one */
260 GtkTreeIter iter;
261 GtkTreeModel *model;
262 GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
263 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
264 return FALSE;
265
266 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
267 int depth = gtk_tree_path_get_depth(path);
268 gtk_tree_path_free(path);
269
270 if (depth < 2)
271 return FALSE;
272
273 /* deeper than a category, so create a menu */
274 GtkWidget *menu = gtk_menu_new();
275 QModelIndex idx = get_index_from_selection(selection);
276
277 /* if depth == 2, it is a contact, offer to copy name, and if only one
278 * contact method exists then also the "number",
279 * if depth > 2, then its a contact method, so only offer to copy the number
280 */
281 if (depth == 2) {
282 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
283 if (var_c.isValid()) {
284 Person *c = var_c.value<Person *>();
285
286 /* copy name */
287 gchar *name = g_strdup_printf("%s", c->formattedName().toUtf8().constData());
288 GtkWidget *item = gtk_menu_item_new_with_mnemonic("_Copy name");
289 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
290 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, name, (GDestroyNotify)g_free);
291 g_signal_connect(item,
292 "activate",
293 G_CALLBACK(copy_contact_info),
294 NULL);
295
296 /* copy number if there is only one */
297 if (c->phoneNumbers().size() == 1) {
298 gchar *number = g_strdup_printf("%s",c->phoneNumbers().first()->uri().toUtf8().constData());
299 GtkWidget *item = gtk_menu_item_new_with_mnemonic("_Copy number");
300 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
301 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
302 g_signal_connect(item,
303 "activate",
304 G_CALLBACK(copy_contact_info),
305 NULL);
306 }
307 }
308 } else if (depth > 2) {
309 /* copy number */
310 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
311 if (var_n.isValid()) {
312 ContactMethod *n = var_n.value<ContactMethod *>();
313 gchar *number = g_strdup_printf("%s",n->uri().toUtf8().constData());
314 GtkWidget *item = gtk_menu_item_new_with_mnemonic("_Copy number");
315 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
316 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
317 g_signal_connect(item,
318 "activate",
319 G_CALLBACK(copy_contact_info),
320 NULL);
321 }
322 }
323
324 /* show menu */
325 gtk_widget_show_all(menu);
326 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
327
328 return TRUE; /* we handled the event */
329}
330
331static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400332contacts_view_init(ContactsView *self)
333{
334 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400335
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400336 gtk_orientable_set_orientation(GTK_ORIENTABLE(self), GTK_ORIENTATION_VERTICAL);
337 /* need to be able to focus on widget so that we can auto-scroll to it */
338 gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400339
Stepan Salenikovich81455562015-05-01 16:28:46 -0400340 GtkWidget *label_contacts = gtk_label_new("Contacts");
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400341 gtk_box_pack_start(GTK_BOX(self), label_contacts, FALSE, TRUE, 10);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400342
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400343 GtkWidget *treeview_contacts = gtk_tree_view_new();
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400344 /* set can-focus to false so that the scrollwindow doesn't jump to try to
345 * contain the top of the treeview */
346 gtk_widget_set_can_focus(treeview_contacts, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400347 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_contacts), FALSE);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400348 gtk_box_pack_start(GTK_BOX(self), treeview_contacts, TRUE, TRUE, 0);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400349
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400350 /* disable default search, we will handle it ourselves via LRC;
351 * otherwise the search steals input focus on key presses */
352 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(treeview_contacts), FALSE);
353
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400354 /* initial set up to be categorized by name and sorted alphabetically */
355 priv->q_sorted_proxy = CategorizedContactModel::SortedProxy::instance();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400356 CategorizedContactModel::instance()->setUnreachableHidden(true);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400357
358 /* for now we always want to sort by ascending order */
359 priv->q_sorted_proxy->model()->sort(0);
360
361 /* select default category (the first one, which is by name) */
362 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
363 priv->q_sorted_proxy->categoryModel()->index(0, 0),
364 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400365
366 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400367 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400368 1,
369 Qt::DisplayRole, G_TYPE_STRING);
370 gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_contacts), GTK_TREE_MODEL(contact_model));
371
372 /* photo and name/contact method column */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400373 GtkCellArea *area = gtk_cell_area_box_new();
374 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400375 gtk_tree_view_column_set_title(column, "Name");
376
377 /* photo renderer */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400378 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400379 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
380
381 /* get the photo */
382 gtk_tree_view_column_set_cell_data_func(
383 column,
384 renderer,
385 (GtkTreeCellDataFunc)render_contact_photo,
386 NULL,
387 NULL);
388
389 /* name and contact method renderer */
390 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich81455562015-05-01 16:28:46 -0400391 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
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 gtk_tree_view_column_set_cell_data_func(
395 column,
396 renderer,
397 (GtkTreeCellDataFunc)render_name_and_contact_method,
398 NULL,
399 NULL);
400
401 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_contacts), column);
402 gtk_tree_view_column_set_resizable(column, TRUE);
403
404 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview_contacts));
405 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), treeview_contacts);
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400406 g_signal_connect(treeview_contacts, "button-press-event", G_CALLBACK(contacts_popup_menu), treeview_contacts);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400407 g_signal_connect(treeview_contacts, "row-activated", G_CALLBACK(activate_contact_item), NULL);
408
409 gtk_widget_show_all(GTK_WIDGET(self));
410}
411
412static void
413contacts_view_dispose(GObject *object)
414{
415 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
416}
417
418static void
419contacts_view_finalize(GObject *object)
420{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400421 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
422}
423
424static void
425contacts_view_class_init(ContactsViewClass *klass)
426{
427 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
428 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
429}
430
431GtkWidget *
432contacts_view_new()
433{
434 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
435
436 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400437}