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