blob: 0be3115cb19e78c7aae70548633c53cafbdad688 [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 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.
Stepan Salenikovich9816a942015-04-22 17:49:16 -040018 */
19
20#include "contactsview.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040024#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040025#include <categorizedcontactmodel.h>
26#include <personmodel.h>
27#include "utils/calling.h"
28#include <memory>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040029#include <globalinstances.h>
30#include "native/pixbufmanipulator.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040031#include <contactmethod.h>
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040032#include "defines.h"
33#include "utils/models.h"
Stepan Salenikovich9d294492015-05-14 16:34:24 -040034#include <QtCore/QItemSelectionModel>
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -050035#include "numbercategory.h"
Stepan Salenikovich75a216e2015-04-23 14:08:53 -040036
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040037static constexpr const char* COPY_DATA_KEY = "copy_data";
Stepan Salenikovich9816a942015-04-22 17:49:16 -040038
39struct _ContactsView
40{
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040041 GtkTreeView parent;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040042};
43
44struct _ContactsViewClass
45{
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040046 GtkTreeViewClass parent_class;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040047};
48
49typedef struct _ContactsViewPrivate ContactsViewPrivate;
50
51struct _ContactsViewPrivate
52{
Stepan Salenikovich9d294492015-05-14 16:34:24 -040053 CategorizedContactModel::SortedProxy *q_sorted_proxy;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040054};
55
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -040056G_DEFINE_TYPE_WITH_PRIVATE(ContactsView, contacts_view, GTK_TYPE_TREE_VIEW);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040057
58#define CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONTACTS_VIEW_TYPE, ContactsViewPrivate))
59
60static void
61render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
62 GtkCellRenderer *cell,
63 GtkTreeModel *tree_model,
64 GtkTreeIter *iter,
65 G_GNUC_UNUSED gpointer data)
66{
67 /* check if this is a top level item (category),
Stepan Salenikovich81455562015-05-01 16:28:46 -040068 * or a bottom level item (contact method)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040069 * in this case we don't want to show a photo */
70 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
71 int depth = gtk_tree_path_get_depth(path);
72 gtk_tree_path_free(path);
73 if (depth == 2) {
74 /* get person */
75 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
76 if (idx.isValid()) {
77 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
78 Person *c = var_c.value<Person *>();
79 /* get photo */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040080 QVariant var_p = GlobalInstances::pixmapManipulator().contactPhoto(c, QSize(50, 50), false);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040081 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
82 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
83 return;
84 }
85 }
86
87 /* otherwise, make sure its an empty pixbuf */
88 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
89}
90
91static void
92render_name_and_contact_method(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
93 GtkCellRenderer *cell,
94 GtkTreeModel *tree_model,
95 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -040096 GtkTreeView *treeview)
Stepan Salenikovich9816a942015-04-22 17:49:16 -040097{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -040098 // check if this iter is selected
99 gboolean is_selected = FALSE;
100 if (GTK_IS_TREE_VIEW(treeview)) {
101 auto selection = gtk_tree_view_get_selection(treeview);
102 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
103 }
104
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400105 /**
106 * If contact (person), show the name and the contact method (number)
107 * underneath; if multiple contact methods, then indicate as such
108 *
109 * Otherwise just display the category or contact method
110 */
111 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
112 int depth = gtk_tree_path_get_depth(path);
113 gtk_tree_path_free(path);
114
115 gchar *text = NULL;
116
117 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
118 if (idx.isValid()) {
119 QVariant var = idx.data(Qt::DisplayRole);
120 if (depth == 1) {
121 /* category */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400122 text = g_markup_printf_escaped("<b>%s</b>", var.value<QString>().toUtf8().constData());
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400123 } else if (depth == 2) {
124 /* contact, check for contact methods */
125 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
126 if (var_c.isValid()) {
127 Person *c = var_c.value<Person *>();
128 switch (c->phoneNumbers().size()) {
129 case 0:
130 text = g_strdup_printf("%s\n", c->formattedName().toUtf8().constData());
131 break;
132 case 1:
133 {
134 QString number;
135 QVariant var_n = c->phoneNumbers().first()->roleData(Qt::DisplayRole);
136 if (var_n.isValid())
137 number = var_n.value<QString>();
138
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400139 /* we want the color of the status text to be the default color if this iter is
140 * selected so that the treeview is able to invert it against the selection color */
141 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400142 text = g_markup_printf_escaped("%s\n %s",
143 c->formattedName().toUtf8().constData(),
144 number.toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400145 } else {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400146 text = g_markup_printf_escaped("%s\n <span fgcolor=\"gray\">%s</span>",
147 c->formattedName().toUtf8().constData(),
148 number.toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400149 }
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400150 break;
151 }
152 default:
153 /* more than one, for now don't show any of the contact methods */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400154 text = g_markup_printf_escaped("%s\n", c->formattedName().toUtf8().constData());
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400155 break;
156 }
157 } else {
158 /* should never happen since depth 2 should always be a contact (person) */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400159 text = g_markup_printf_escaped("%s", var.value<QString>().toUtf8().constData());
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400160 }
161 } else {
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500162 auto var_object = idx.data(static_cast<int>(Ring::Role::Object));
163 auto cm = var_object.value<ContactMethod *>();
164 if (cm && cm->category()) {
165 // try to get the number category, eg: "home"
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400166 text = g_markup_printf_escaped("(%s) %s", cm->category()->name().toUtf8().constData(),
167 cm->uri().toUtf8().constData());
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500168 } else if (cm) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400169 text = g_markup_printf_escaped("%s", cm->uri().toUtf8().constData());
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500170 } else {
171 /* should only ever be a CM, so this should never execute */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400172 text = g_markup_printf_escaped("%s", var.value<QString>().toUtf8().constData());
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500173 }
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400174 }
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
Julien Baron865676b2016-03-04 20:38:59 +0100254call_contactmethod(G_GNUC_UNUSED GtkWidget *item, ContactMethod *cm)
255{
256 g_return_if_fail(cm);
257 place_new_call(cm);
258}
259
260static void
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400261copy_contact_info(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
262{
263 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
264 g_return_if_fail(data);
265 gchar* text = (gchar *)data;
266 GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
267 gtk_clipboard_set_text(clip, text, -1);
268}
269
aviauff727952016-01-29 16:23:07 -0500270
271static gboolean
272remove_contact_dialog(GtkWidget *widget, Person *person)
273{
274 gboolean response = FALSE;
275 GtkWidget *dialog = gtk_message_dialog_new(NULL,
276 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
277 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
278 _("Are you sure you want to delete contact \"%s\"?"
279 " It will be removed from your system's addressbook."),
280 person->formattedName().toUtf8().constData());
281
282 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
283
284 /* get parent window so we can center on it */
285 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(widget));
286 if (gtk_widget_is_toplevel(parent)) {
287 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
288 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
289 }
290
291 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
292 case GTK_RESPONSE_OK:
293 response = TRUE;
294 break;
295 default:
296 response = FALSE;
297 break;
298 }
299
300 gtk_widget_destroy(dialog);
301
302 return response;
303}
304
305static void
306remove_contact(GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
307{
308 gpointer data = g_object_get_data(G_OBJECT(item), COPY_DATA_KEY);
309 g_return_if_fail(data);
310 Person* person = (Person *)data;
311 if (remove_contact_dialog(item, person)) {
312 person->remove();
313 }
314}
315
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400316static gboolean
317contacts_popup_menu(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GtkTreeView *treeview)
318{
319 /* build popup menu when right clicking on contact item
320 * user should be able to copy the contact's name or "number".
321 * other functionality may be added later.
322 */
323
324 /* check for right click */
325 if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS)
326 return FALSE;
327
328 /* we don't want a popup menu for categories for now, so everything deeper
329 * than one */
330 GtkTreeIter iter;
331 GtkTreeModel *model;
332 GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
333 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
334 return FALSE;
335
336 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
337 int depth = gtk_tree_path_get_depth(path);
338 gtk_tree_path_free(path);
339
340 if (depth < 2)
341 return FALSE;
342
343 /* deeper than a category, so create a menu */
344 GtkWidget *menu = gtk_menu_new();
345 QModelIndex idx = get_index_from_selection(selection);
346
347 /* if depth == 2, it is a contact, offer to copy name, and if only one
348 * contact method exists then also the "number",
349 * if depth > 2, then its a contact method, so only offer to copy the number
350 */
351 if (depth == 2) {
Julien Baron865676b2016-03-04 20:38:59 +0100352 QVariant var_c = idx.data(static_cast<int>(Ring::Role::Object));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400353 if (var_c.isValid()) {
354 Person *c = var_c.value<Person *>();
355
Julien Baron865676b2016-03-04 20:38:59 +0100356 /* call */
357 /* possiblity for multiple numbers */
358 auto cms = c->phoneNumbers();
359 if (cms.size() == 1) {
360 auto item = gtk_menu_item_new_with_mnemonic(_("_Call"));
361 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
362 g_signal_connect(item,
363 "activate",
364 G_CALLBACK(call_contactmethod),
365 cms.at(0));
366 } else if (cms.size() > 1) {
367 // maybe this is not needed since there is the depth > 2 option
368 auto call_item = gtk_menu_item_new_with_mnemonic(_("_Call"));
369 gtk_menu_shell_append(GTK_MENU_SHELL(menu), call_item);
370 auto call_menu = gtk_menu_new();
371 gtk_menu_item_set_submenu(GTK_MENU_ITEM(call_item), call_menu);
372 for (int i = 0; i < cms.size(); ++i) {
373 gchar *number = nullptr;
374 if (cms.at(i)->category()) {
375 // try to get the number category, eg: "home"
376 number = g_strdup_printf("(%s) %s", cms.at(i)->category()->name().toUtf8().constData(),
377 cms.at(i)->uri().toUtf8().constData());
378 } else {
379 number = g_strdup_printf("%s", cms.at(i)->uri().toUtf8().constData());
380 }
381 auto item = gtk_menu_item_new_with_label(number);
382 g_free(number);
383 gtk_menu_shell_append(GTK_MENU_SHELL(call_menu), item);
384 g_signal_connect(item,
385 "activate",
386 G_CALLBACK(call_contactmethod),
387 cms.at(i));
388 }
389 }
390
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400391 /* copy name */
392 gchar *name = g_strdup_printf("%s", c->formattedName().toUtf8().constData());
aviauff727952016-01-29 16:23:07 -0500393 GtkWidget *copy_name_item = gtk_menu_item_new_with_mnemonic(_("_Copy name"));
394 gtk_menu_shell_append(GTK_MENU_SHELL(menu), copy_name_item);
395 g_object_set_data_full(G_OBJECT(copy_name_item), COPY_DATA_KEY, name, (GDestroyNotify)g_free);
396 g_signal_connect(copy_name_item,
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400397 "activate",
398 G_CALLBACK(copy_contact_info),
399 NULL);
400
401 /* copy number if there is only one */
402 if (c->phoneNumbers().size() == 1) {
403 gchar *number = g_strdup_printf("%s",c->phoneNumbers().first()->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400404 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400405 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
406 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
407 g_signal_connect(item,
408 "activate",
409 G_CALLBACK(copy_contact_info),
410 NULL);
411 }
aviauff727952016-01-29 16:23:07 -0500412
413 /* delete contact */
414 GtkWidget *remove_contact_item = gtk_menu_item_new_with_mnemonic(_("_Remove contact"));
415 gtk_menu_shell_append(GTK_MENU_SHELL(menu), remove_contact_item);
416 g_object_set_data_full(G_OBJECT(remove_contact_item), COPY_DATA_KEY, c, (GDestroyNotify)g_free);
417 g_signal_connect(remove_contact_item,
418 "activate",
419 G_CALLBACK(remove_contact),
420 NULL);
421
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400422 }
423 } else if (depth > 2) {
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400424 QVariant var_n = idx.data(static_cast<int>(ContactMethod::Role::Object));
425 if (var_n.isValid()) {
Julien Baron865676b2016-03-04 20:38:59 +0100426 /* copy number */
427 ContactMethod *cm = var_n.value<ContactMethod *>();
428 gchar *number = g_strdup_printf("%s",cm->uri().toUtf8().constData());
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400429 GtkWidget *item = gtk_menu_item_new_with_mnemonic(_("_Copy number"));
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400430 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
431 g_object_set_data_full(G_OBJECT(item), COPY_DATA_KEY, number, (GDestroyNotify)g_free);
432 g_signal_connect(item,
433 "activate",
434 G_CALLBACK(copy_contact_info),
435 NULL);
Julien Baron865676b2016-03-04 20:38:59 +0100436
437 /* call */
438 item = gtk_menu_item_new_with_mnemonic(_("_Call"));
439 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
440 g_signal_connect(item,
441 "activate",
442 G_CALLBACK(call_contactmethod),
443 cm);
Stepan Salenikovich75a216e2015-04-23 14:08:53 -0400444 }
445 }
446
447 /* show menu */
448 gtk_widget_show_all(menu);
449 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
450
451 return TRUE; /* we handled the event */
452}
453
454static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400455contacts_view_init(ContactsView *self)
456{
457 ContactsViewPrivate *priv = CONTACTS_VIEW_GET_PRIVATE(self);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400458
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400459 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
Stepan Salenikovich26457ce2015-05-11 14:37:53 -0400460
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400461 /* disable default search, we will handle it ourselves;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400462 * otherwise the search steals input focus on key presses */
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400463 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400464
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400465 /* initial set up to be categorized by name and sorted alphabetically */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400466 priv->q_sorted_proxy = &CategorizedContactModel::SortedProxy::instance();
467 CategorizedContactModel::instance().setUnreachableHidden(true);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400468
469 /* for now we always want to sort by ascending order */
470 priv->q_sorted_proxy->model()->sort(0);
471
472 /* select default category (the first one, which is by name) */
473 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
474 priv->q_sorted_proxy->categoryModel()->index(0, 0),
475 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400476
477 GtkQSortFilterTreeModel *contact_model = gtk_q_sort_filter_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400478 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400479 1,
480 Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400481 gtk_tree_view_set_model(GTK_TREE_VIEW(self), GTK_TREE_MODEL(contact_model));
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400482
483 /* photo and name/contact method column */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400484 GtkCellArea *area = gtk_cell_area_box_new();
485 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400486
487 /* photo renderer */
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400488 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400489 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
490
491 /* get the photo */
492 gtk_tree_view_column_set_cell_data_func(
493 column,
494 renderer,
495 (GtkTreeCellDataFunc)render_contact_photo,
496 NULL,
497 NULL);
498
499 /* name and contact method renderer */
500 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich81455562015-05-01 16:28:46 -0400501 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400502 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
503
504 gtk_tree_view_column_set_cell_data_func(
505 column,
506 renderer,
507 (GtkTreeCellDataFunc)render_name_and_contact_method,
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400508 self,
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400509 NULL);
510
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400511 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400512 gtk_tree_view_column_set_resizable(column, TRUE);
513
Stepan Salenikovichba1fc2d2015-10-29 16:38:10 -0400514 gtk_tree_view_expand_all(GTK_TREE_VIEW(self));
515 g_signal_connect(contact_model, "row-inserted", G_CALLBACK(expand_if_child), self);
516 g_signal_connect(self, "button-press-event", G_CALLBACK(contacts_popup_menu), self);
517 g_signal_connect(self, "row-activated", G_CALLBACK(activate_contact_item), NULL);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400518
519 gtk_widget_show_all(GTK_WIDGET(self));
520}
521
522static void
523contacts_view_dispose(GObject *object)
524{
525 G_OBJECT_CLASS(contacts_view_parent_class)->dispose(object);
526}
527
528static void
529contacts_view_finalize(GObject *object)
530{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400531 G_OBJECT_CLASS(contacts_view_parent_class)->finalize(object);
532}
533
534static void
535contacts_view_class_init(ContactsViewClass *klass)
536{
537 G_OBJECT_CLASS(klass)->finalize = contacts_view_finalize;
538 G_OBJECT_CLASS(klass)->dispose = contacts_view_dispose;
539}
540
541GtkWidget *
542contacts_view_new()
543{
544 gpointer self = g_object_new(CONTACTS_VIEW_TYPE, NULL);
545
546 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400547}