blob: 773a8849a0b21916f64f47738ab1932ac8bf2629 [file] [log] [blame]
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -04003 * 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 Salenikovichf2d76c52015-07-17 17:54:56 -040018 */
19
20#include "menus.h"
21
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040022#include <glib/gi18n.h>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040023#include <contactmethod.h>
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040024#include "../contactpopover.h"
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040025
26/**
27 * checks if the given contact method is already associated with a contact
28 */
29gboolean
30contact_method_has_contact(ContactMethod *cm)
31{
32 g_return_val_if_fail(cm, FALSE);
33
34 return cm->contact() != NULL;
35}
36
37static void
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040038choose_contact(GtkWidget *item, ContactMethod *contactmethod)
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040039{
40 // we get the parent widget which should be stored in the item object
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040041 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 Salenikovichf2d76c52015-07-17 17:54:56 -040047}
48
49/**
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040050 * Takes a GtkMenuItem and connects its activate signal to a popup which adds the given
51 * ContactMethod to a Person
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040052 */
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040053GtkMenuItem*
54menu_item_add_to_contact(GtkMenuItem *item, ContactMethod *cm, GtkWidget *parent, const GdkRectangle *rect)
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040055{
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040056 g_return_val_if_fail(item && cm, NULL);
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040057
58 /* save the parent widget in the item object, so we can retrieve
59 * it in the callback */
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040060 g_object_set_data(G_OBJECT(item), "parent-widget", parent);
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040061
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040062 GdkRectangle *copy_rect = NULL;
63 if (rect) {
64 copy_rect = g_new0(GdkRectangle, 1);
65 memcpy(copy_rect, rect, sizeof(GdkRectangle));
66 }
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040067 g_object_set_data_full(G_OBJECT(item), "parent-rectangle", copy_rect, (GDestroyNotify)g_free);
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040068
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040069 g_signal_connect(item, "activate", G_CALLBACK(choose_contact), cm);
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040070
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040071 return item;
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040072}