Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -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 "createcontactdialog.h" |
| 32 | |
| 33 | #include <contactmethod.h> |
| 34 | #include <personmodel.h> |
| 35 | #include <numbercategorymodel.h> |
| 36 | #include "utils/models.h" |
| 37 | |
| 38 | struct _CreateContactDialog |
| 39 | { |
| 40 | GtkDialog parent; |
| 41 | }; |
| 42 | |
| 43 | struct _CreateContactDialogClass |
| 44 | { |
| 45 | GtkDialogClass parent_class; |
| 46 | }; |
| 47 | |
| 48 | typedef struct _CreateContactDialogPrivate CreateContactDialogPrivate; |
| 49 | |
| 50 | struct _CreateContactDialogPrivate |
| 51 | { |
| 52 | GtkWidget *button_save; |
| 53 | GtkWidget *button_cancel; |
| 54 | GtkWidget *combobox_addressbook; |
| 55 | GtkWidget *entry_name; |
| 56 | GtkWidget *combobox_detail; |
| 57 | GtkWidget *entry_contactmethod; |
| 58 | |
| 59 | ContactMethod *cm; |
| 60 | }; |
| 61 | |
| 62 | G_DEFINE_TYPE_WITH_PRIVATE(CreateContactDialog, create_contact_dialog, GTK_TYPE_DIALOG); |
| 63 | |
| 64 | #define CREATE_CONTACT_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CREATE_CONTACT_DIALOG_TYPE, CreateContactDialogPrivate)) |
| 65 | |
| 66 | static void |
| 67 | save_cb(G_GNUC_UNUSED GtkButton *button, CreateContactDialog *self) |
| 68 | { |
| 69 | g_return_if_fail(IS_CREATE_CONTACT_DIALOG(self)); |
| 70 | CreateContactDialogPrivate *priv = CREATE_CONTACT_DIALOG_GET_PRIVATE(self); |
| 71 | |
| 72 | /* make sure that the entry is not empty */ |
| 73 | auto name = gtk_entry_get_text(GTK_ENTRY(priv->entry_name)); |
| 74 | if (!name or strlen(name) == 0) { |
| 75 | g_warning("new contact must have a name"); |
| 76 | gtk_widget_grab_focus(priv->entry_name); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | /* get the selected collection */ |
| 81 | GtkTreeIter iter; |
| 82 | gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_addressbook), &iter); |
| 83 | auto addressbook_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_addressbook)); |
| 84 | GValue value = G_VALUE_INIT; |
| 85 | gtk_tree_model_get_value(addressbook_model, &iter, 1, &value); |
| 86 | auto collection = (CollectionInterface *)g_value_get_pointer(&value); |
| 87 | |
| 88 | /* get the selected number category */ |
| 89 | const auto& idx = gtk_combo_box_get_index(GTK_COMBO_BOX(priv->combobox_detail)); |
| 90 | if (idx.isValid()) { |
| 91 | auto category = NumberCategoryModel::instance()->getCategory(idx.data().toString()); |
| 92 | priv->cm->setCategory(category); |
| 93 | } |
| 94 | |
| 95 | /* create a new person */ |
| 96 | Person *p = new Person(collection); |
| 97 | p->setFormattedName(name); |
| 98 | |
| 99 | /* associate the new person with the contact method */ |
| 100 | Person::ContactMethods numbers; |
| 101 | numbers << priv->cm; |
| 102 | p->setContactMethods(numbers); |
| 103 | |
| 104 | PersonModel::instance()->addNewPerson(p, collection); |
| 105 | |
| 106 | gtk_dialog_response(GTK_DIALOG(self), GTK_RESPONSE_OK); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | cancel_cb(G_GNUC_UNUSED GtkButton *button, CreateContactDialog *self) |
| 111 | { |
| 112 | gtk_dialog_response(GTK_DIALOG(self), GTK_RESPONSE_CANCEL); |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | create_contact_dialog_init(CreateContactDialog *self) |
| 117 | { |
| 118 | gtk_widget_init_template(GTK_WIDGET(self)); |
| 119 | |
| 120 | CreateContactDialogPrivate *priv = CREATE_CONTACT_DIALOG_GET_PRIVATE(self); |
| 121 | |
| 122 | /* model for the combobox for the choice of addressbooks */ |
| 123 | auto addressbook_model = gtk_list_store_new( |
| 124 | 2, G_TYPE_STRING, G_TYPE_POINTER |
| 125 | ); |
| 126 | |
| 127 | gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_addressbook), |
| 128 | GTK_TREE_MODEL(addressbook_model)); |
| 129 | GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
| 130 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_addressbook), |
| 131 | renderer, FALSE); |
| 132 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_addressbook), |
| 133 | renderer, "text", 0, NULL); |
| 134 | |
| 135 | /* get all the available contact backends (addressbooks) */ |
| 136 | const auto& collections = PersonModel::instance()->enabledCollections(); |
| 137 | for (int i = 0; i < collections.size(); ++i) { |
| 138 | GtkTreeIter iter; |
| 139 | gtk_list_store_append(addressbook_model, &iter); |
| 140 | gtk_list_store_set(addressbook_model, &iter, |
| 141 | 0, collections.at(i)->name().toUtf8().constData(), |
| 142 | 1, collections.at(i), |
| 143 | -1); |
| 144 | } |
| 145 | |
| 146 | /* the first addressbook loaded is probably the vCard... so select the next |
| 147 | * one to be used by default */ |
| 148 | gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_addressbook), 1); |
| 149 | |
| 150 | /* model for the available details to choose from */ |
| 151 | gtk_combo_box_set_qmodel(GTK_COMBO_BOX(priv->combobox_detail), |
| 152 | (QAbstractItemModel *)NumberCategoryModel::instance(), NULL); |
| 153 | |
| 154 | /* set "home" as the default number category */ |
| 155 | const auto& idx = NumberCategoryModel::instance()->nameToIndex("home"); |
| 156 | if (idx.isValid()) |
| 157 | gtk_combo_box_set_active_index(GTK_COMBO_BOX(priv->combobox_detail), idx); |
| 158 | |
| 159 | /* set a default name in the name entry, and grab focus to select the text */ |
| 160 | gtk_entry_set_text(GTK_ENTRY(priv->entry_name), "New Contact"); |
| 161 | gtk_widget_grab_focus(GTK_WIDGET(priv->entry_name)); |
| 162 | |
| 163 | /* connect to the button signals */ |
| 164 | g_signal_connect(priv->button_save, "clicked", G_CALLBACK(save_cb), self); |
| 165 | g_signal_connect(priv->button_cancel, "clicked", G_CALLBACK(cancel_cb), self); |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | create_contact_dialog_dispose(GObject *object) |
| 170 | { |
| 171 | G_OBJECT_CLASS(create_contact_dialog_parent_class)->dispose(object); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | create_contact_dialog_finalize(GObject *object) |
| 176 | { |
| 177 | G_OBJECT_CLASS(create_contact_dialog_parent_class)->finalize(object); |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | create_contact_dialog_class_init(CreateContactDialogClass *klass) |
| 182 | { |
| 183 | G_OBJECT_CLASS(klass)->finalize = create_contact_dialog_finalize; |
| 184 | G_OBJECT_CLASS(klass)->dispose = create_contact_dialog_dispose; |
| 185 | |
| 186 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass), |
| 187 | "/cx/ring/RingGnome/createcontactdialog.ui"); |
| 188 | |
| 189 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, button_save); |
| 190 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, button_cancel); |
| 191 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, combobox_addressbook); |
| 192 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, entry_name); |
| 193 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, combobox_detail); |
| 194 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(klass), CreateContactDialog, entry_contactmethod); |
| 195 | } |
| 196 | |
| 197 | GtkWidget * |
| 198 | create_contact_dialog_new(ContactMethod *cm, GtkWidget *parent) |
| 199 | { |
| 200 | g_return_val_if_fail(cm, NULL); |
| 201 | |
| 202 | if (parent && GTK_IS_WIDGET(parent)) { |
| 203 | /* get parent window so we can center on it */ |
| 204 | parent = gtk_widget_get_toplevel(GTK_WIDGET(parent)); |
| 205 | if (!gtk_widget_is_toplevel(parent)) { |
| 206 | parent = NULL; |
| 207 | g_debug("could not get top level parent"); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | gpointer self = g_object_new(CREATE_CONTACT_DIALOG_TYPE, "transient-for", parent, NULL); |
| 212 | |
| 213 | CreateContactDialogPrivate *priv = CREATE_CONTACT_DIALOG_GET_PRIVATE(self); |
| 214 | |
| 215 | priv->cm = cm; |
| 216 | gtk_entry_set_text(GTK_ENTRY(priv->entry_contactmethod), cm->uri().toUtf8().constData()); |
| 217 | gtk_entry_set_width_chars(GTK_ENTRY(priv->entry_contactmethod), cm->uri().toUtf8().size()); |
| 218 | |
| 219 | return (GtkWidget *)self; |
| 220 | } |