blob: 1f8f6c99a474ee3131bc4207f09f2785cd27cefa [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"
38#include "delegates/pixbufdelegate.h"
39#include "utils/models.h"
40
41enum
42{
43 PERSON_SELECTED,
44 NEW_PERSON_CLICKED,
45
46 LAST_SIGNAL
47};
48
49struct _ChooseContactView
50{
51 GtkBox parent;
52};
53
54struct _ChooseContactViewClass
55{
56 GtkBoxClass parent_class;
57};
58
59typedef struct _ChooseContactViewPrivate ChooseContactViewPrivate;
60
61struct _ChooseContactViewPrivate
62{
63 GtkWidget *treeview_choose_contact;
64 GtkWidget *button_create_contact;
65
66 ContactMethod *cm;
67
68 QSortFilterProxyModel *sorted_contacts;
69};
70
71static guint choose_contact_signals[LAST_SIGNAL] = { 0 };
72
73G_DEFINE_TYPE_WITH_PRIVATE(ChooseContactView, choose_contact_view, GTK_TYPE_BOX);
74
75#define CHOOSE_CONTACT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHOOSE_CONTACT_VIEW_TYPE, ChooseContactViewPrivate))
76
77static void
78render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
79 GtkCellRenderer *cell,
80 GtkTreeModel *tree_model,
81 GtkTreeIter *iter,
82 G_GNUC_UNUSED gpointer data)
83{
84 /* show a photo for the top level (Person) */
85 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
86 int depth = gtk_tree_path_get_depth(path);
87 gtk_tree_path_free(path);
88 if (depth == 1) {
89 /* get person */
90 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(tree_model), iter);
91 if (idx.isValid()) {
92 QVariant var_c = idx.data(static_cast<int>(Person::Role::Object));
93 Person *c = var_c.value<Person *>();
94 /* get photo */
95 QVariant var_p = PixbufDelegate::instance()->contactPhoto(c, QSize(50, 50), false);
96 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
97 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
98 return;
99 }
100 }
101
102 /* otherwise, make sure its an empty pixbuf */
103 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
104}
105
106static void
107select_cb(ChooseContactView *self)
108{
109 g_return_if_fail(IS_CHOOSE_CONTACT_VIEW(self));
110 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
111
112 /* get the selected collection */
113 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_choose_contact));
114 auto idx = get_index_from_selection(selection);
115 if (idx.isValid()) {
116 auto p = idx.data(static_cast<int>(Person::Role::Object)).value<Person *>();
117
118 g_signal_emit(self, choose_contact_signals[PERSON_SELECTED], 0, p);
119 } else {
120 g_warning("invalid Person selected");
121 }
122}
123
124static void
125create_contact_cb(G_GNUC_UNUSED GtkButton *button, ChooseContactView *self)
126{
127 g_return_if_fail(IS_CHOOSE_CONTACT_VIEW(self));
128
129 g_signal_emit(self, choose_contact_signals[NEW_PERSON_CLICKED], 0);
130}
131
132static void
133choose_contact_view_init(ChooseContactView *self)
134{
135 gtk_widget_init_template(GTK_WIDGET(self));
136
137 ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self);
138
139 priv->sorted_contacts = new QSortFilterProxyModel(PersonModel::instance());
140 priv->sorted_contacts->setSourceModel(PersonModel::instance());
141 priv->sorted_contacts->setSortCaseSensitivity(Qt::CaseInsensitive);
142 priv->sorted_contacts->sort(0);
143
144 auto contacts_model = gtk_q_sort_filter_tree_model_new(
145 priv->sorted_contacts,
146 1,
147 Qt::DisplayRole, G_TYPE_STRING);
148 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_choose_contact), GTK_TREE_MODEL(contacts_model));
149 g_object_unref(contacts_model); /* the model should be freed when the view is destroyed */
150
151 /* photo and name/contact method colparentumn */
152 GtkCellArea *area = gtk_cell_area_box_new();
153 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
154 gtk_tree_view_column_set_title(column, "Name");
155
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}