blob: 4d5a7bdc1facb8245a6ceaa8b1dbcaf41091ea54 [file] [log] [blame]
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -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 "choosecontactview.h"
32
33#include <contactmethod.h>
34#include <personmodel.h>
35#include <QtCore/QSortFilterProxyModel>
36#include <memory>
37#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040038#include <globalinstances.h>
39#include "native/pixbufmanipulator.h"
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040040#include "utils/models.h"
41
42enum
43{
44 PERSON_SELECTED,
45 NEW_PERSON_CLICKED,
46
47 LAST_SIGNAL
48};
49
50struct _ChooseContactView
51{
52 GtkBox parent;
53};
54
55struct _ChooseContactViewClass
56{
57 GtkBoxClass parent_class;
58};
59
60typedef struct _ChooseContactViewPrivate ChooseContactViewPrivate;
61
62struct _ChooseContactViewPrivate
63{
64 GtkWidget *treeview_choose_contact;
65 GtkWidget *button_create_contact;
66
67 ContactMethod *cm;
68
69 QSortFilterProxyModel *sorted_contacts;
70};
71
72static guint choose_contact_signals[LAST_SIGNAL] = { 0 };
73
74G_DEFINE_TYPE_WITH_PRIVATE(ChooseContactView, choose_contact_view, GTK_TYPE_BOX);
75
76#define CHOOSE_CONTACT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHOOSE_CONTACT_VIEW_TYPE, ChooseContactViewPrivate))
77
78static void
79render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
80 GtkCellRenderer *cell,
81 GtkTreeModel *tree_model,
82 GtkTreeIter *iter,
83 G_GNUC_UNUSED gpointer data)
84{
85 /* show a photo for the top level (Person) */
86 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
87 int depth = gtk_tree_path_get_depth(path);
88 gtk_tree_path_free(path);
89 if (depth == 1) {
90 /* get person */
91 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
92 if (idx.isValid()) {
93 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
94 Person *c = var_c.value<Person *>();
95 /* get photo */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040096 QVariant var_p = GlobalInstances::pixmapManipulator().contactPhoto(c, QSize(50, 50), false);
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040097 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
98 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
99 return;
100 }
101 }
102
103 /* otherwise, make sure its an empty pixbuf */
104 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
105}
106
107static void
108select_cb(ChooseContactView *self)
109{
110 g_return_if_fail(IS_CHOOSE_CONTACT_VIEW(self));
111 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
112
113 /* get the selected collection */
114 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_choose_contact));
115 auto idx = get_index_from_selection(selection);
116 if (idx.isValid()) {
117 auto p = idx.data(static_cast<int>(Person::Role::Object)).value<Person *>();
118
119 g_signal_emit(self, choose_contact_signals[PERSON_SELECTED], 0, p);
120 } else {
121 g_warning("invalid Person selected");
122 }
123}
124
125static void
126create_contact_cb(G_GNUC_UNUSED GtkButton *button, ChooseContactView *self)
127{
128 g_return_if_fail(IS_CHOOSE_CONTACT_VIEW(self));
129
130 g_signal_emit(self, choose_contact_signals[NEW_PERSON_CLICKED], 0);
131}
132
133static void
134choose_contact_view_init(ChooseContactView *self)
135{
136 gtk_widget_init_template(GTK_WIDGET(self));
137
138 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
139
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400140 priv->sorted_contacts = new QSortFilterProxyModel(&PersonModel::instance());
141 priv->sorted_contacts->setSourceModel(&PersonModel::instance());
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -0400142 priv->sorted_contacts->setSortCaseSensitivity(Qt::CaseInsensitive);
143 priv->sorted_contacts->sort(0);
144
145 auto contacts_model = gtk_q_sort_filter_tree_model_new(
146 priv->sorted_contacts,
147 1,
148 Qt::DisplayRole, G_TYPE_STRING);
149 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_choose_contact), GTK_TREE_MODEL(contacts_model));
150 g_object_unref(contacts_model); /* the model should be freed when the view is destroyed */
151
152 /* photo and name/contact method colparentumn */
153 GtkCellArea *area = gtk_cell_area_box_new();
154 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -0400155
156 /* photo renderer */
157 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
158 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
159
160 /* get the photo */
161 gtk_tree_view_column_set_cell_data_func(
162 column,
163 renderer,
164 (GtkTreeCellDataFunc)render_contact_photo,
165 NULL,
166 NULL);
167
168 /* name and contact method renderer */
169 renderer = gtk_cell_renderer_text_new();
170 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
171 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
172 gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
173
174 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_choose_contact), column);
175 gtk_tree_view_column_set_resizable(column, TRUE);
176
177 /* connect to the button signals */
178 g_signal_connect_swapped(priv->treeview_choose_contact, "row-activated", G_CALLBACK(select_cb), self);
179 g_signal_connect(priv->button_create_contact, "clicked", G_CALLBACK(create_contact_cb), self);
180}
181
182static void
183choose_contact_view_dispose(GObject *object)
184{
185 G_OBJECT_CLASS(choose_contact_view_parent_class)->dispose(object);
186}
187
188static void
189choose_contact_view_finalize(GObject *object)
190{
191 ChooseContactView *self = CHOOSE_CONTACT_VIEW(object);
192 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
193
194 delete priv->sorted_contacts;
195
196 G_OBJECT_CLASS(choose_contact_view_parent_class)->finalize(object);
197}
198
199static void
200choose_contact_view_class_init(ChooseContactViewClass *klass)
201{
202 G_OBJECT_CLASS(klass)->finalize = choose_contact_view_finalize;
203 G_OBJECT_CLASS(klass)->dispose = choose_contact_view_dispose;
204
205 choose_contact_signals[NEW_PERSON_CLICKED] =
206 g_signal_new("new-person-clicked",
207 G_OBJECT_CLASS_TYPE(G_OBJECT_CLASS(klass)),
208 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
209 0, /* class offset */
210 NULL, /* accumulater */
211 NULL, /* accu data */
212 g_cclosure_marshal_VOID__VOID,
213 G_TYPE_NONE, 0);
214
215 choose_contact_signals[PERSON_SELECTED] =
216 g_signal_new ("person-selected",
217 G_OBJECT_CLASS_TYPE(G_OBJECT_CLASS(klass)),
218 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
219 0, /* class offset */
220 NULL, /* accumulater */
221 NULL, /* accu data */
222 g_cclosure_marshal_VOID__POINTER,
223 G_TYPE_NONE,
224 1, G_TYPE_POINTER);
225
226 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass),
227 "/cx/ring/RingGnome/choosecontactview.ui");
228
229 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), ChooseContactView, treeview_choose_contact);
230 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), ChooseContactView, button_create_contact);
231}
232
233GtkWidget *
234choose_contact_view_new(ContactMethod *cm)
235{
236 g_return_val_if_fail(cm, NULL);
237
238 gpointer self = g_object_new(CHOOSE_CONTACT_VIEW_TYPE, NULL);
239
240 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
241 priv->cm = cm;
242
243 return (GtkWidget *)self;
244}