Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 1 | /* |
| 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 Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame^] | 38 | #include <globalinstances.h> |
| 39 | #include "native/pixbufmanipulator.h" |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 40 | #include "utils/models.h" |
| 41 | |
| 42 | enum |
| 43 | { |
| 44 | PERSON_SELECTED, |
| 45 | NEW_PERSON_CLICKED, |
| 46 | |
| 47 | LAST_SIGNAL |
| 48 | }; |
| 49 | |
| 50 | struct _ChooseContactView |
| 51 | { |
| 52 | GtkBox parent; |
| 53 | }; |
| 54 | |
| 55 | struct _ChooseContactViewClass |
| 56 | { |
| 57 | GtkBoxClass parent_class; |
| 58 | }; |
| 59 | |
| 60 | typedef struct _ChooseContactViewPrivate ChooseContactViewPrivate; |
| 61 | |
| 62 | struct _ChooseContactViewPrivate |
| 63 | { |
| 64 | GtkWidget *treeview_choose_contact; |
| 65 | GtkWidget *button_create_contact; |
| 66 | |
| 67 | ContactMethod *cm; |
| 68 | |
| 69 | QSortFilterProxyModel *sorted_contacts; |
| 70 | }; |
| 71 | |
| 72 | static guint choose_contact_signals[LAST_SIGNAL] = { 0 }; |
| 73 | |
| 74 | G_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 | |
| 78 | static void |
| 79 | render_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 Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame^] | 96 | QVariant var_p = GlobalInstances::pixmapManipulator().contactPhoto(c, QSize(50, 50), false); |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 97 | 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 | |
| 107 | static void |
| 108 | select_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 | |
| 125 | static void |
| 126 | create_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 | |
| 133 | static void |
| 134 | choose_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 | |
| 140 | priv->sorted_contacts = new QSortFilterProxyModel(PersonModel::instance()); |
| 141 | priv->sorted_contacts->setSourceModel(PersonModel::instance()); |
| 142 | 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); |
| 155 | gtk_tree_view_column_set_title(column, "Name"); |
| 156 | |
| 157 | /* photo renderer */ |
| 158 | GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new(); |
| 159 | gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE); |
| 160 | |
| 161 | /* get the photo */ |
| 162 | gtk_tree_view_column_set_cell_data_func( |
| 163 | column, |
| 164 | renderer, |
| 165 | (GtkTreeCellDataFunc)render_contact_photo, |
| 166 | NULL, |
| 167 | NULL); |
| 168 | |
| 169 | /* name and contact method renderer */ |
| 170 | renderer = gtk_cell_renderer_text_new(); |
| 171 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 172 | gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE); |
| 173 | gtk_tree_view_column_add_attribute(column, renderer, "text", 0); |
| 174 | |
| 175 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_choose_contact), column); |
| 176 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 177 | |
| 178 | /* connect to the button signals */ |
| 179 | g_signal_connect_swapped(priv->treeview_choose_contact, "row-activated", G_CALLBACK(select_cb), self); |
| 180 | g_signal_connect(priv->button_create_contact, "clicked", G_CALLBACK(create_contact_cb), self); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | choose_contact_view_dispose(GObject *object) |
| 185 | { |
| 186 | G_OBJECT_CLASS(choose_contact_view_parent_class)->dispose(object); |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | choose_contact_view_finalize(GObject *object) |
| 191 | { |
| 192 | ChooseContactView *self = CHOOSE_CONTACT_VIEW(object); |
| 193 | ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self); |
| 194 | |
| 195 | delete priv->sorted_contacts; |
| 196 | |
| 197 | G_OBJECT_CLASS(choose_contact_view_parent_class)->finalize(object); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | choose_contact_view_class_init(ChooseContactViewClass *klass) |
| 202 | { |
| 203 | G_OBJECT_CLASS(klass)->finalize = choose_contact_view_finalize; |
| 204 | G_OBJECT_CLASS(klass)->dispose = choose_contact_view_dispose; |
| 205 | |
| 206 | choose_contact_signals[NEW_PERSON_CLICKED] = |
| 207 | g_signal_new("new-person-clicked", |
| 208 | G_OBJECT_CLASS_TYPE(G_OBJECT_CLASS(klass)), |
| 209 | (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 210 | 0, /* class offset */ |
| 211 | NULL, /* accumulater */ |
| 212 | NULL, /* accu data */ |
| 213 | g_cclosure_marshal_VOID__VOID, |
| 214 | G_TYPE_NONE, 0); |
| 215 | |
| 216 | choose_contact_signals[PERSON_SELECTED] = |
| 217 | g_signal_new ("person-selected", |
| 218 | G_OBJECT_CLASS_TYPE(G_OBJECT_CLASS(klass)), |
| 219 | (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 220 | 0, /* class offset */ |
| 221 | NULL, /* accumulater */ |
| 222 | NULL, /* accu data */ |
| 223 | g_cclosure_marshal_VOID__POINTER, |
| 224 | G_TYPE_NONE, |
| 225 | 1, G_TYPE_POINTER); |
| 226 | |
| 227 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass), |
| 228 | "/cx/ring/RingGnome/choosecontactview.ui"); |
| 229 | |
| 230 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), ChooseContactView, treeview_choose_contact); |
| 231 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), ChooseContactView, button_create_contact); |
| 232 | } |
| 233 | |
| 234 | GtkWidget * |
| 235 | choose_contact_view_new(ContactMethod *cm) |
| 236 | { |
| 237 | g_return_val_if_fail(cm, NULL); |
| 238 | |
| 239 | gpointer self = g_object_new(CHOOSE_CONTACT_VIEW_TYPE, NULL); |
| 240 | |
| 241 | ChooseContactViewPrivate *priv = CHOOSE_CONTACT_VIEW_GET_PRIVATE(self); |
| 242 | priv->cm = cm; |
| 243 | |
| 244 | return (GtkWidget *)self; |
| 245 | } |