libpurple/purplemenu.c

Thu, 21 Jul 2022 01:10:22 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Thu, 21 Jul 2022 01:10:22 -0500
changeset 41454
7cc69bde919d
parent 41403
0186ae21071f
child 41457
f0e7534a555d
permissions
-rw-r--r--

Update pidgin for the purple_account_manager_get_(in)active deprecations

Testing Done:
Compiled and made sure the menus still functioned as expected.

Reviewed at https://reviews.imfreedom.org/r/1536/

/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include "purplemenu.h"

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_menu_populate_dynamic_targets_func(GMenuModel *model, gint index,
                                          gpointer data)
{
	GHashTable *table = data;
	gchar *property = NULL;

	g_return_if_fail(G_IS_MENU(model));

	if(g_menu_model_get_item_attribute(model, index,
	                                   PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET,
	                                   "s", &property))
	{
		const gchar *value;

		value = g_hash_table_lookup(table, property);
		g_free(property);

		if(value != NULL) {
			GMenuItem *item = NULL;

			item = g_menu_item_new_from_model(model, index);
			g_menu_item_set_attribute(item, G_MENU_ATTRIBUTE_TARGET, "s",
			                          value);

			g_menu_remove(G_MENU(model), index);
			g_menu_insert_item(G_MENU(model), index, item);
			g_object_unref(item);
		}
	}
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
purple_menu_walk(GMenuModel *model, PurpleMenuWalkFunc func, gpointer data) {
	gint index = 0;

	for(index = 0; index < g_menu_model_get_n_items(model); index++) {
		GMenuLinkIter *iter = NULL;

		func(model, index, data);

		iter = g_menu_model_iterate_item_links(model, index);
		while(g_menu_link_iter_next(iter)) {
			GMenuModel *link = g_menu_link_iter_get_value(iter);

			purple_menu_walk(link, func, data);

			g_clear_object(&link);
		}

		g_clear_object(&iter);
	}
}

void
purple_menu_populate_dynamic_targets(GMenu *menu, const gchar *first_property,
                                     ...)
{
	GHashTable *table = NULL;
	const gchar *property = NULL, *value = NULL;
	va_list vargs;

	g_return_if_fail(G_IS_MENU(menu));
	g_return_if_fail(first_property != NULL);

	table = g_hash_table_new(g_str_hash, g_str_equal);

	property = first_property;
	va_start(vargs, first_property);

	/* Iterate through the vargs adding values when we find one that isn't
	 * NULL.
	 */
	do {
		value = va_arg(vargs, const gchar *);
		if(value != NULL) {
			g_hash_table_insert(table, (gpointer)property, (gpointer)value);
		}

		/* After adding the value, see if we have another property. */
		property = va_arg(vargs, const gchar *);
	} while(property != NULL);

	va_end(vargs);

	purple_menu_walk(G_MENU_MODEL(menu),
	                 purple_menu_populate_dynamic_targets_func, table);

	g_hash_table_unref(table);
}

mercurial