1 /*
2  * This file is part of Amtk - Actions, Menus and Toolbars Kit
3  *
4  * Copyright 2017 - Sébastien Wilmet <swilmet@gnome.org>
5  *
6  * Amtk is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * Amtk is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "amtk-action-map.h"
21 
22 /**
23  * SECTION:amtk-action-map
24  * @Short_description: #GActionMap wrapper functions
25  * @Title: AmtkActionMap
26  *
27  * #GActionMap wrapper functions.
28  */
29 
30 static void
check_dups_in_array(const GActionEntry * entries,const gchar * action_name,gint action_num)31 check_dups_in_array (const GActionEntry *entries,
32 		     const gchar        *action_name,
33 		     gint                action_num)
34 {
35 	gint i;
36 
37 	for (i = 0; i < action_num; i++)
38 	{
39 		const GActionEntry *entry = &entries[i];
40 
41 		if (g_strcmp0 (action_name, entry->name) == 0)
42 		{
43 			g_warning ("amtk_action_map_add_action_entries_check_dups(): "
44 				   "the GActionEntry array contains duplicated entries for the action name '%s'. "
45 				   "The first one will be dropped from the GActionMap.",
46 				   action_name);
47 			return;
48 		}
49 	}
50 }
51 
52 /**
53  * amtk_action_map_add_action_entries_check_dups:
54  * @action_map: a #GActionMap.
55  * @entries: (array length=n_entries) (element-type GActionEntry): a pointer to
56  *           the first item in an array of #GActionEntry structs.
57  * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated.
58  * @user_data: the user data for signal connections.
59  *
60  * A wrapper function for g_action_map_add_action_entries() that checks
61  * duplicates.
62  *
63  * This function first checks - for each entry - that the @action_map doesn't
64  * already contain a #GAction with the same name. A warning is printed if an old
65  * action will be dropped. In any case, it then calls
66  * g_action_map_add_action_entries() with the same arguments as passed to this
67  * function.
68  *
69  * This function also checks if there are duplicates in the @entries array
70  * itself.
71  *
72  * Since: 2.0
73  */
74 void
amtk_action_map_add_action_entries_check_dups(GActionMap * action_map,const GActionEntry * entries,gint n_entries,gpointer user_data)75 amtk_action_map_add_action_entries_check_dups (GActionMap         *action_map,
76 					       const GActionEntry *entries,
77 					       gint                n_entries,
78 					       gpointer            user_data)
79 {
80 	gint i;
81 
82 	g_return_if_fail (G_IS_ACTION_MAP (action_map));
83 	g_return_if_fail (n_entries >= -1);
84 	g_return_if_fail (entries != NULL || n_entries == 0);
85 
86 	for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
87 	{
88 		const GActionEntry *entry = &entries[i];
89 
90 		if (g_action_map_lookup_action (action_map, entry->name) != NULL)
91 		{
92 			g_warning ("%s(): the GActionMap already contains a GAction with the name '%s'. "
93 				   "The old GAction will be dropped from the GActionMap.",
94 				   G_STRFUNC,
95 				   entry->name);
96 		}
97 
98 		check_dups_in_array (entries, entry->name, i);
99 	}
100 
101 	g_action_map_add_action_entries (action_map,
102 					 entries,
103 					 n_entries,
104 					 user_data);
105 }
106