blob: 313527d0a01fbbd18c73814a6637c48f32480d50 [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 Salenikovich0cf247d2015-07-24 17:36:32 -040050 * creates a menu item allowing the adition of a contact method to a contact
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040051 */
Stepan Salenikovich0cf247d2015-07-24 17:36:32 -040052GtkWidget * menu_item_add_to_contact(ContactMethod *cm, GtkWidget *parent, const GdkRectangle *rect)
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040053{
54 g_return_val_if_fail(cm, NULL);
55
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040056 auto add_to = gtk_menu_item_new_with_mnemonic(_("_Add to contact"));
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 Salenikovich0cf247d2015-07-24 17:36:32 -040060 g_object_set_data(G_OBJECT(add_to), "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 }
67 g_object_set_data_full(G_OBJECT(add_to), "parent-rectangle", copy_rect, (GDestroyNotify)g_free);
68
69 g_signal_connect(add_to, "activate", G_CALLBACK(choose_contact), cm);
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040070
71 return add_to;
72}