Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | be87d2c | 2016-01-25 14:14:34 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 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. |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "menus.h" |
| 21 | |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 22 | #include <glib/gi18n.h> |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 23 | #include <contactmethod.h> |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 24 | #include "../contactpopover.h" |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * checks if the given contact method is already associated with a contact |
| 28 | */ |
| 29 | gboolean |
| 30 | contact_method_has_contact(ContactMethod *cm) |
| 31 | { |
| 32 | g_return_val_if_fail(cm, FALSE); |
| 33 | |
| 34 | return cm->contact() != NULL; |
| 35 | } |
| 36 | |
| 37 | static void |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 38 | choose_contact(GtkWidget *item, ContactMethod *contactmethod) |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 39 | { |
| 40 | // we get the parent widget which should be stored in the item object |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 41 | auto parent = g_object_get_data(G_OBJECT(item), "parent-widget"); |
| 42 | auto rect = g_object_get_data(G_OBJECT(item), "parent-rectangle"); |
| 43 | |
| 44 | auto popover = contact_popover_new(contactmethod, GTK_WIDGET(parent), (GdkRectangle *)rect); |
| 45 | |
| 46 | gtk_widget_show(popover); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 50 | * Takes a GtkMenuItem and connects its activate signal to a popup which adds the given |
| 51 | * ContactMethod to a Person |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 52 | */ |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 53 | GtkMenuItem* |
| 54 | menu_item_add_to_contact(GtkMenuItem *item, ContactMethod *cm, GtkWidget *parent, const GdkRectangle *rect) |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 55 | { |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 56 | g_return_val_if_fail(item && cm, NULL); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 57 | |
| 58 | /* save the parent widget in the item object, so we can retrieve |
| 59 | * it in the callback */ |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 60 | g_object_set_data(G_OBJECT(item), "parent-widget", parent); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 61 | |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 62 | GdkRectangle *copy_rect = NULL; |
| 63 | if (rect) { |
| 64 | copy_rect = g_new0(GdkRectangle, 1); |
| 65 | memcpy(copy_rect, rect, sizeof(GdkRectangle)); |
| 66 | } |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 67 | g_object_set_data_full(G_OBJECT(item), "parent-rectangle", copy_rect, (GDestroyNotify)g_free); |
Stepan Salenikovich | 0cf247d | 2015-07-24 17:36:32 -0400 | [diff] [blame] | 68 | |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 69 | g_signal_connect(item, "activate", G_CALLBACK(choose_contact), cm); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 70 | |
Stepan Salenikovich | 8eaa13e | 2016-08-26 16:51:48 -0400 | [diff] [blame^] | 71 | return item; |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 72 | } |