Fri, 18 Mar 2022 00:09:50 -0500
Add a menu controller for the accounts enabled section of the accounts menu
Testing Done:
Ran and verified the menu items worked.
Reviewed at https://reviews.imfreedom.org/r/1344/
--- a/pidgin/meson.build Thu Mar 17 23:27:18 2022 -0500 +++ b/pidgin/meson.build Fri Mar 18 00:09:50 2022 -0500 @@ -24,6 +24,7 @@ 'pidginaccountfilterconnected.c', 'pidginaccountfilterprotocol.c', 'pidginaccountsdisabledmenu.c', + 'pidginaccountsenabledmenu.c', 'pidginaccountsmenu.c', 'pidginaccountstore.c', 'pidginactiongroup.c', @@ -86,6 +87,7 @@ 'pidginaccountfilterconnected.h', 'pidginaccountfilterprotocol.h', 'pidginaccountsdisabledmenu.h', + 'pidginaccountsenabledmenu.h', 'pidginaccountsmenu.h', 'pidginaccountstore.h', 'pidginactiongroup.h',
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pidgin/pidginaccountsenabledmenu.c Fri Mar 18 00:09:50 2022 -0500 @@ -0,0 +1,150 @@ +/* + * Pidgin - Internet Messenger + * Copyright (C) Pidgin Developers <devel@pidgin.im> + * + * Pidgin 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 <glib/gi18n.h> + +#include "pidginaccountsenabledmenu.h" + +#include "pidgincore.h" + +/****************************************************************************** + * Helpers + *****************************************************************************/ +static GMenu * +pidgin_accounts_enabled_menu_build_submenu(PurpleAccount *account) { + GMenu *menu = NULL, *section = NULL; + gchar *action_id = NULL; + const gchar *account_id = NULL; + + menu = g_menu_new(); + account_id = purple_account_get_id(account); + + /* Add the "Edit Account" section. */ + section = g_menu_new(); + g_menu_append_section(menu, NULL, G_MENU_MODEL(section)); + + action_id = g_strdup_printf("app.edit-account::%s", account_id); + g_menu_append(section, _("Edit Account"), action_id); + g_free(action_id); + + /* Add the "Disable Account" section. */ + section = g_menu_new(); + g_menu_append_section(menu, NULL, G_MENU_MODEL(section)); + + action_id = g_strdup_printf("app.disable-account::%s", account_id); + g_menu_append(section, _("Disable"), action_id); + g_free(action_id); + + return menu; +} + +static void +pidgin_accounts_enabled_menu_refresh_helper(PurpleAccount *account, + gpointer data) +{ + GMenu *menu = data; + + if(purple_account_get_enabled(account, PIDGIN_UI)) { + GMenu *submenu = NULL; + gchar *label = NULL; + const gchar *account_name = purple_account_get_username(account); + const gchar *protocol_name = purple_account_get_protocol_name(account); + + submenu = pidgin_accounts_enabled_menu_build_submenu(account); + + /* translators: This format string is intended to contain the account + * name followed by the protocol name to uniquely identify a specific + * account. + */ + label = g_strdup_printf(_("%s (%s)"), account_name, protocol_name); + + g_menu_append_submenu(menu, label, G_MENU_MODEL(submenu)); + + g_free(label); + } +} + +static void +pidgin_accounts_enabled_menu_refresh(GMenu *menu) { + PurpleAccountManager *manager = NULL; + + g_menu_remove_all(menu); + + manager = purple_account_manager_get_default(); + purple_account_manager_foreach(manager, + pidgin_accounts_enabled_menu_refresh_helper, + menu); +} + +/****************************************************************************** + * Callbacks + *****************************************************************************/ +static void +pidgin_accounts_enabled_menu_enabled_cb(G_GNUC_UNUSED PurpleAccount *account, + gpointer data) +{ + pidgin_accounts_enabled_menu_refresh(data); +} + +static void +pidgin_accounts_enabled_menu_disabled_cb(G_GNUC_UNUSED PurpleAccount *account, + gpointer data) +{ + pidgin_accounts_enabled_menu_refresh(data); +} + +static void +pidgin_accounts_enabled_menu_weak_notify_cb(G_GNUC_UNUSED gpointer data, + GObject *obj) +{ + purple_signals_disconnect_by_handle(obj); +} + +/****************************************************************************** + * Public API + *****************************************************************************/ +GMenu * +pidgin_accounts_enabled_menu_new(void) { + GMenu *menu = NULL; + gpointer handle = NULL; + + /* Create the menu and set our instance as data on it so it'll be freed + * when the menu is destroyed. + */ + menu = g_menu_new(); + g_object_weak_ref(G_OBJECT(menu), + pidgin_accounts_enabled_menu_weak_notify_cb, NULL); + + /* Populate ourselves with any accounts that are already enabled. */ + pidgin_accounts_enabled_menu_refresh(menu); + + /* Wire up the purple signals we care about. */ + handle = purple_accounts_get_handle(); + purple_signal_connect(handle, "account-enabled", menu, + G_CALLBACK(pidgin_accounts_enabled_menu_enabled_cb), + menu); + purple_signal_connect(handle, "account-disabled", menu, + G_CALLBACK(pidgin_accounts_enabled_menu_disabled_cb), + menu); + + return menu; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pidgin/pidginaccountsenabledmenu.h Fri Mar 18 00:09:50 2022 -0500 @@ -0,0 +1,51 @@ +/* + * Pidgin - Internet Messenger + * Copyright (C) Pidgin Developers <devel@pidgin.im> + * + * Pidgin 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/>. + */ + +#if !defined(PIDGIN_GLOBAL_HEADER_INSIDE) && !defined(PIDGIN_COMPILATION) +# error "only <pidgin.h> may be included directly" +#endif + +#ifndef PIDGIN_ACCOUNTS_ENABLED_MENU_H +#define PIDGIN_ACCOUNTS_ENABLED_MENU_H + +#include <glib.h> +#include <gio/gio.h> + +#include <purple.h> + +G_BEGIN_DECLS + +/** + * pidgin_accounts_enabled_menu_new: + * + * Creates a [class@Gio.Menu] that will automatically update itself to include + * accounts that are enabled in libpurple. + * + * Returns: (transfer full): The new menu instance. + * + * Since: 3.0.0 + */ +GMenu *pidgin_accounts_enabled_menu_new(void); + +G_END_DECLS + +#endif /* PIDGIN_ACCOUNTS_ENABLED_MENU_H */ \ No newline at end of file
--- a/pidgin/pidginapplication.c Thu Mar 17 23:27:18 2022 -0500 +++ b/pidgin/pidginapplication.c Fri Mar 18 00:09:50 2022 -0500 @@ -44,6 +44,7 @@ #include "gtkxfer.h" #include "pidginabout.h" #include "pidginaccountsdisabledmenu.h" +#include "pidginaccountsenabledmenu.h" #include "pidginconversationwindow.h" #include "pidgincore.h" #include "pidgindebug.h" @@ -128,13 +129,18 @@ static void pidgin_application_populate_dynamic_menus(PidginApplication *application) { - GMenu *target = NULL; - GMenu *source = NULL; + GMenu *source = NULL, *target = NULL; /* Link the AccountsDisabledMenu into its proper location. */ + source = pidgin_accounts_disabled_menu_new(); target = gtk_application_get_menu_by_id(GTK_APPLICATION(application), "disabled-accounts"); - source = pidgin_accounts_disabled_menu_new(); + g_menu_append_section(target, NULL, G_MENU_MODEL(source)); + + /* Link the AccountsEnabledMenu into its proper location. */ + source = pidgin_accounts_enabled_menu_new(); + target = gtk_application_get_menu_by_id(GTK_APPLICATION(application), + "enabled-accounts"); g_menu_append_section(target, NULL, G_MENU_MODEL(source)); } @@ -257,6 +263,45 @@ purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/enabled", !old); } + +static void +pidgin_application_disable_account(GSimpleAction *simple, GVariant *parameter, + gpointer data) +{ + PurpleAccount *account = NULL; + PurpleAccountManager *manager = NULL; + const gchar *id = NULL; + + id = g_variant_get_string(parameter, NULL); + + manager = purple_account_manager_get_default(); + + account = purple_account_manager_find_by_id(manager, id); + if(PURPLE_IS_ACCOUNT(account)) { + if(purple_account_get_enabled(account, PIDGIN_UI)) { + purple_account_set_enabled(account, PIDGIN_UI, FALSE); + } + } +} + +static void +pidgin_application_edit_account(GSimpleAction *simple, GVariant *parameter, + gpointer data) +{ + PurpleAccount *account = NULL; + PurpleAccountManager *manager = NULL; + const gchar *id = NULL; + + id = g_variant_get_string(parameter, NULL); + + manager = purple_account_manager_get_default(); + + account = purple_account_manager_find_by_id(manager, id); + if(PURPLE_IS_ACCOUNT(account)) { + pidgin_account_dialog_show(PIDGIN_MODIFY_ACCOUNT_DIALOG, account); + } +} + static void pidgin_application_enable_account(GSimpleAction *simple, GVariant *parameter, gpointer data) @@ -385,6 +430,14 @@ .name = "debug", .activate = pidgin_application_debug, }, { + .name = "disable-account", + .activate = pidgin_application_disable_account, + .parameter_type = "s", + }, { + .name = "edit-account", + .activate = pidgin_application_edit_account, + .parameter_type = "s", + }, { .name = "enable-account", .activate = pidgin_application_enable_account, .parameter_type = "s",
--- a/pidgin/resources/gtk/menus.ui Thu Mar 17 23:27:18 2022 -0500 +++ b/pidgin/resources/gtk/menus.ui Fri Mar 18 00:09:50 2022 -0500 @@ -187,9 +187,7 @@ <section id="disabled-accounts"/> </submenu> </section> - <section> - <attribute name="id">enabled-accounts</attribute> - </section> + <section id="enabled-accounts"/> </submenu> <submenu> <attribute name="label" translatable="yes">_Tools</attribute>
--- a/po/POTFILES.in Thu Mar 17 23:27:18 2022 -0500 +++ b/po/POTFILES.in Fri Mar 18 00:09:50 2022 -0500 @@ -337,6 +337,7 @@ pidgin/pidginaccountfilterconnected.c pidgin/pidginaccountfilterprotocol.c pidgin/pidginaccountsdisabledmenu.c +pidgin/pidginaccountsenabledmenu.c pidgin/pidginaccountsmenu.c pidgin/pidginaccountstore.c pidgin/pidginactiongroup.c