blob: 04cd7e31f784b04496dfe8e93e797b39e57cb973 [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"
35#include "models/activeitemproxymodel.h"
36#include <categorizedcontactmodel.h>
37#include <personmodel.h>
38#include "utils/calling.h"
39#include <memory>
40#include "delegates/pixbufdelegate.h"
41#include <contactmethod.h>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040042#include "defines.h"
43#include "utils/models.h"
44
45#define COPY_DATA_KEY "copy_data"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040046
47struct _ContactsView
48{
49 GtkScrolledWindow parent;
50};
51
52struct _ContactsViewClass
53{
54 GtkScrolledWindowClass parent_class;
55};
56
57typedef struct _ContactsViewPrivate ContactsViewPrivate;
58
59struct _ContactsViewPrivate
60{
61 ActiveItemProxyModel *q_contact_model;
62};
63
64G_DEFINE_TYPE_WITH_PRIVATE(ContactsView, contacts_view, GTK_TYPE_SCROLLED_WINDOW);
65
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),
76 * or a bottom level item (contact method)gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
77 * 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);
335
336 /* contacts view/model */
337 GtkWidget *treeview_contacts = gtk_tree_view_new();
338 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_contacts), FALSE);
339 gtk_container_add(GTK_CONTAINER(self), treeview_contacts);
340
341 CategorizedContactModel::instance()->setUnreachableHidden(true);
342 priv->q_contact_model = new ActiveItemProxyModel(CategorizedContactModel::instance());
343 priv->q_contact_model->setSortRole(Qt::DisplayRole);
344 priv->q_contact_model->setSortLocaleAware(true);
345 priv->q_contact_model->setSortCaseSensitivity(Qt::CaseInsensitive);
346 priv->q_contact_model->sort(0);
347
348 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
349 (QSortFilterProxyModel *)priv->q_contact_model,
350 1,
351 Qt::DisplayRole, G_TYPE_STRING);
352 gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_contacts), GTK_TREE_MODEL(contact_model));
353
354 /* photo and name/contact method column */
355 GtkCellArea *area = gtk_cell_area_box_new();
356 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
357 gtk_tree_view_column_set_title(column, "Name");
358
359 /* photo renderer */
360 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
361 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
362
363 /* get the photo */
364 gtk_tree_view_column_set_cell_data_func(
365 column,
366 renderer,
367 (GtkTreeCellDataFunc)render_contact_photo,
368 NULL,
369 NULL);
370
371 /* name and contact method renderer */
372 renderer = gtk_cell_renderer_text_new();
373 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
374
375 gtk_tree_view_column_set_cell_data_func(
376 column,
377 renderer,
378 (GtkTreeCellDataFunc)render_name_and_contact_method,
379 NULL,
380 NULL);
381
382 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_contacts), column);
383 gtk_tree_view_column_set_resizable(column, TRUE);
384
385 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview_contacts));
386 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), treeview_contacts);
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400387 g_signal_connect(treeview_contacts, "button-press-event", G_CALLBACK(contacts_popup_menu), treeview_contacts);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400388 g_signal_connect(treeview_contacts, "row-activated", G_CALLBACK(activate_contact_item), NULL);
389
390 gtk_widget_show_all(GTK_WIDGET(self));
391}
392
393static void
394contacts_view_dispose(GObject *object)
395{
396 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
397}
398
399static void
400contacts_view_finalize(GObject *object)
401{
402 ContactsView *self = CONTACTS_VIEW(object);
403 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
404
405 delete priv->q_contact_model;
406
407 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
408}
409
410static void
411contacts_view_class_init(ContactsViewClass *klass)
412{
413 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
414 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
415}
416
417GtkWidget *
418contacts_view_new()
419{
420 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
421
422 return (GtkWidget *)self;
423}