pidgin/pidgin

Parents 0dc72eacd8bf
Children b0f820b59429
Replace PidginAccountsDisabledMenu with PidginInactiveAccountsMenu which is a GMenuModel subclass.

This gives us a bit more control as we are always in control of the attributes,
so we don't need to modify the menu in place. This also made it trivial to add
the protocol icons which are very helpful when you have the same user name on
multiple protocols.

Testing Done:
Enabled and disabled two accounts a lot while under valgrind.

Reviewed at https://reviews.imfreedom.org/r/1366/
--- a/libpurple/purpleaccountmanager.c Fri Mar 25 02:51:58 2022 -0500
+++ b/libpurple/purpleaccountmanager.c Fri Mar 25 02:56:25 2022 -0500
@@ -205,6 +205,24 @@
return active;
}
+GList *
+purple_account_manager_get_inactive(PurpleAccountManager *manager) {
+ GList *inactive = NULL, *l = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_ACCOUNT_MANAGER(manager), NULL);
+
+ for(l = manager->accounts; l != NULL; l = l->next) {
+ PurpleAccount *account = l->data;
+
+ if(!purple_account_get_enabled(account, purple_core_get_ui())) {
+ inactive = g_list_append(inactive, account);
+ }
+ }
+
+ return inactive;
+}
+
+
void
purple_account_manager_reorder(PurpleAccountManager *manager,
PurpleAccount *account,
--- a/libpurple/purpleaccountmanager.h Fri Mar 25 02:51:58 2022 -0500
+++ b/libpurple/purpleaccountmanager.h Fri Mar 25 02:56:25 2022 -0500
@@ -126,6 +126,19 @@
GList *purple_account_manager_get_active(PurpleAccountManager *manager);
/**
+ * purple_account_manager_get_inactive:
+ * @manager: The account manager instance.
+ *
+ * Gets the list of all inactive accounts.
+ *
+ * Returns: (transfer container) (element-type PurpleAccount): The list of all
+ * inactive accounts.
+ *
+ * Since: 3.0.0
+ */
+GList *purple_account_manager_get_inactive(PurpleAccountManager *manager);
+
+/**
* purple_account_manager_find_by_id:
* @manager: The account manager instance.
* @id: The id of the account.
--- a/pidgin/meson.build Fri Mar 25 02:51:58 2022 -0500
+++ b/pidgin/meson.build Fri Mar 25 02:56:25 2022 -0500
@@ -24,7 +24,6 @@
'pidginaccountfilterconnected.c',
'pidginaccountfilterprotocol.c',
'pidginaccountmanager.c',
- 'pidginaccountsdisabledmenu.c',
'pidginaccountsenabledmenu.c',
'pidginaccountsmenu.c',
'pidginaccountstore.c',
@@ -43,6 +42,7 @@
'pidgindialog.c',
'pidgingdkpixbuf.c',
'pidginiconname.c',
+ 'pidgininactiveaccountsmenu.c',
'pidgininfopane.c',
'pidgininvitedialog.c',
'pidginmessage.c',
@@ -89,7 +89,6 @@
'pidginaccountfilterconnected.h',
'pidginaccountfilterprotocol.h',
'pidginaccountmanager.h',
- 'pidginaccountsdisabledmenu.h',
'pidginaccountsenabledmenu.h',
'pidginaccountsmenu.h',
'pidginaccountstore.h',
@@ -108,6 +107,7 @@
'pidgindebug.h',
'pidgingdkpixbuf.h',
'pidginiconname.h',
+ 'pidgininactiveaccountsmenu.h',
'pidgininfopane.h',
'pidgininvitedialog.h',
'pidginmessage.h',
--- a/pidgin/pidginaccountsdisabledmenu.c Fri Mar 25 02:51:58 2022 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +0,0 @@
-/*
- * 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 "pidginaccountsdisabledmenu.h"
-
-#include "pidgincore.h"
-
-/******************************************************************************
- * Helpers
- *****************************************************************************/
-static void
-pidgin_accounts_disabled_menu_refresh_helper(PurpleAccount *account,
- gpointer data)
-{
- GMenu *menu = data;
-
- if(!purple_account_get_enabled(account, PIDGIN_UI)) {
- gchar *label = NULL, *action = NULL;
- const gchar *account_name = purple_account_get_username(account);
- const gchar *protocol_name = purple_account_get_protocol_name(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);
- action = g_strdup_printf("app.enable-account::%s",
- purple_account_get_id(account));
-
- g_menu_append(menu, label, action);
-
- g_free(label);
- g_free(action);
- }
-}
-
-static void
-pidgin_accounts_disabled_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_disabled_menu_refresh_helper,
- menu);
-
- if(g_menu_model_get_n_items(G_MENU_MODEL(menu)) == 0) {
- g_menu_append(menu, _("No disabled accounts"), "disabled");
- }
-}
-
-/******************************************************************************
- * Callbacks
- *****************************************************************************/
-static void
-pidgin_accounts_disabled_menu_enabled_cb(G_GNUC_UNUSED PurpleAccount *account,
- gpointer data)
-{
- pidgin_accounts_disabled_menu_refresh(data);
-}
-
-static void
-pidgin_accounts_disabled_menu_disabled_cb(G_GNUC_UNUSED PurpleAccount *account,
- gpointer data)
-{
- pidgin_accounts_disabled_menu_refresh(data);
-}
-
-static void
-pidgin_accounts_disabled_menu_weak_notify_cb(G_GNUC_UNUSED gpointer data,
- GObject *obj)
-{
- purple_signals_disconnect_by_handle(obj);
-}
-
-/******************************************************************************
- * Public API
- *****************************************************************************/
-GMenu *
-pidgin_accounts_disabled_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_disabled_menu_weak_notify_cb, NULL);
-
- /* Populate ourselves with any accounts that are already disabled. */
- pidgin_accounts_disabled_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_disabled_menu_enabled_cb),
- menu);
- purple_signal_connect(handle, "account-disabled", menu,
- G_CALLBACK(pidgin_accounts_disabled_menu_disabled_cb),
- menu);
-
- return menu;
-}
--- a/pidgin/pidginaccountsdisabledmenu.h Fri Mar 25 02:51:58 2022 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/*
- * 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_DISABLED_MENU_H
-#define PIDGIN_ACCOUNTS_DISABLED_MENU_H
-
-#include <glib.h>
-#include <gio/gio.h>
-
-#include <purple.h>
-
-G_BEGIN_DECLS
-
-/**
- * pidgin_accounts_disabled_menu_new:
- *
- * Creates a [class@Gio.Menu] that will automatically update itself to include
- * accounts that are disabled in libpurple.
- *
- * Returns: (transfer full): The new menu instance.
- *
- * Since: 3.0.0
- */
-GMenu *pidgin_accounts_disabled_menu_new(void);
-
-G_END_DECLS
-
-#endif /* PIDGIN_ACCOUNTS_DISABLED_MENU_H */
\ No newline at end of file
--- a/pidgin/pidginapplication.c Fri Mar 25 02:51:58 2022 -0500
+++ b/pidgin/pidginapplication.c Fri Mar 25 02:56:25 2022 -0500
@@ -43,11 +43,11 @@
#include "gtkxfer.h"
#include "pidginabout.h"
#include "pidginaccountmanager.h"
-#include "pidginaccountsdisabledmenu.h"
#include "pidginaccountsenabledmenu.h"
#include "pidginconversationwindow.h"
#include "pidgincore.h"
#include "pidgindebug.h"
+#include "pidgininactiveaccountsmenu.h"
#include "pidginmooddialog.h"
#include "pidgin/pidginpluginsdialog.h"
#include "pidgin/pidginstatusmanager.h"
@@ -131,12 +131,13 @@
static void
pidgin_application_populate_dynamic_menus(PidginApplication *application) {
GMenu *source = NULL, *target = NULL;
+ GMenuModel *model = NULL;
- /* Link the AccountsDisabledMenu into its proper location. */
- source = pidgin_accounts_disabled_menu_new();
+ /* Link the InactiveAccountsMenu into its proper location. */
+ model = pidgin_inactive_accounts_menu_new();
target = gtk_application_get_menu_by_id(GTK_APPLICATION(application),
- "disabled-accounts");
- g_menu_append_section(target, NULL, G_MENU_MODEL(source));
+ "inactive-accounts");
+ g_menu_append_section(target, NULL, model);
/* Link the AccountsEnabledMenu into its proper location. */
source = pidgin_accounts_enabled_menu_new();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pidgin/pidgininactiveaccountsmenu.c Fri Mar 25 02:56:25 2022 -0500
@@ -0,0 +1,217 @@
+/*
+ * 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 "pidgininactiveaccountsmenu.h"
+
+struct _PidginInactiveAccountsMenu {
+ GMenuModel parent;
+
+ GList *accounts;
+};
+
+G_DEFINE_TYPE(PidginInactiveAccountsMenu, pidgin_inactive_accounts_menu,
+ G_TYPE_MENU_MODEL)
+
+/******************************************************************************
+ * Callbacks
+ *****************************************************************************/
+static void
+pidgin_inactive_accounts_menu_refresh(PidginInactiveAccountsMenu *menu) {
+ PurpleAccountManager *manager = NULL;
+ gint removed = 0, added = 0;
+
+ /* When refreshing we're always removing at least 1 item because of the
+ * "no disabled accounts" item that we put in place when all accounts
+ * are enabled.
+ */
+ removed = MAX(1, g_list_length(menu->accounts));
+
+ /* Grab the manager and get all the disabled accounts. */
+ manager = purple_account_manager_get_default();
+ g_list_free(menu->accounts);
+ menu->accounts = purple_account_manager_get_inactive(manager);
+
+ /* Similar to the aboved note about removed items, if every account is
+ * enabled, we add an item saying "no disabled accounts".
+ */
+ added = MAX(1, g_list_length(menu->accounts));
+
+ /* Tell any listeners that our menu has changed. */
+ g_menu_model_items_changed(G_MENU_MODEL(menu), 0, removed, added);
+}
+
+static void
+pidgin_inactive_accounts_menu_changed_cb(G_GNUC_UNUSED PurpleAccount *account,
+ gpointer data)
+{
+ PidginInactiveAccountsMenu *menu = data;
+
+ pidgin_inactive_accounts_menu_refresh(menu);
+}
+
+/******************************************************************************
+ * GMenuModel Implementation
+ *****************************************************************************/
+static gboolean
+pidgin_inactive_accounts_menu_is_mutable(GMenuModel *model) {
+ return TRUE;
+}
+
+static gboolean
+pidgin_inactive_accounts_menu_get_n_items(GMenuModel *model) {
+ PidginInactiveAccountsMenu *menu = NULL;
+
+ menu = PIDGIN_INACTIVE_ACCOUNTS_MENU(model);
+
+ if(menu->accounts == NULL) {
+ return 1;
+ }
+
+ return g_list_length(menu->accounts);
+}
+
+static void
+pidgin_inactive_accounts_menu_get_item_attributes(GMenuModel *model,
+ gint index,
+ GHashTable **attributes)
+{
+ PidginInactiveAccountsMenu *menu = NULL;
+ PurpleAccount *account = NULL;
+ PurpleProtocol *protocol = NULL;
+ GVariant *value = NULL;
+ const gchar *account_name = NULL, *protocol_name = NULL;
+
+ menu = PIDGIN_INACTIVE_ACCOUNTS_MENU(model);
+
+ /* Create our hash table of attributes to return. */
+ *attributes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
+ (GDestroyNotify)g_variant_unref);
+
+ /* If we don't have any disabled accounts, just return a single item,
+ * stating as much.
+ */
+ if(menu->accounts == NULL) {
+ value = g_variant_new_string(_("No disabled accounts"));
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_LABEL,
+ g_variant_ref_sink(value));
+
+ value = g_variant_new_string("disabled");
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_ACTION,
+ g_variant_ref_sink(value));
+
+ return;
+ }
+
+ account = g_list_nth_data(menu->accounts, index);
+ if(account == NULL) {
+ return;
+ }
+
+ account_name = purple_account_get_username(account);
+ protocol_name = purple_account_get_protocol_name(account);
+
+ /* translators: This format string is intended to contain the account
+ * name followed by the protocol name to uniquely identify a specific
+ * account.
+ */
+ value = g_variant_new_printf(_("%s (%s)"), account_name, protocol_name);
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_LABEL,
+ g_variant_ref_sink(value));
+
+ value = g_variant_new_string("app.enable-account");
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_ACTION,
+ g_variant_ref_sink(value));
+
+ value = g_variant_new_printf("%s", purple_account_get_id(account));
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_TARGET,
+ g_variant_ref_sink(value));
+
+ protocol = purple_account_get_protocol(account);
+ if(protocol != NULL) {
+ value = g_variant_new_printf("%s", purple_protocol_get_icon_name(protocol));
+ g_hash_table_insert(*attributes, G_MENU_ATTRIBUTE_ICON,
+ g_variant_ref_sink(value));
+ }
+}
+
+static void
+pidgin_inactive_accounts_menu_get_item_links(GMenuModel *model, gint index,
+ GHashTable **links)
+{
+ *links = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+ g_object_unref);
+}
+
+/******************************************************************************
+ * GObject Implementation
+ *****************************************************************************/
+static void
+pidgin_inactive_accounts_menu_dispose(GObject *obj) {
+ purple_signals_disconnect_by_handle(obj);
+
+ G_OBJECT_CLASS(pidgin_inactive_accounts_menu_parent_class)->dispose(obj);
+}
+
+static void
+pidgin_inactive_accounts_menu_constructed(GObject *obj) {
+ G_OBJECT_CLASS(pidgin_inactive_accounts_menu_parent_class)->constructed(obj);
+
+ pidgin_inactive_accounts_menu_refresh(PIDGIN_INACTIVE_ACCOUNTS_MENU(obj));
+}
+
+static void
+pidgin_inactive_accounts_menu_init(PidginInactiveAccountsMenu *menu) {
+ gpointer handle = NULL;
+
+ /* Wire up the purple signals we care about. */
+ handle = purple_accounts_get_handle();
+ purple_signal_connect(handle, "account-enabled", menu,
+ G_CALLBACK(pidgin_inactive_accounts_menu_changed_cb),
+ menu);
+ purple_signal_connect(handle, "account-disabled", menu,
+ G_CALLBACK(pidgin_inactive_accounts_menu_changed_cb),
+ menu);
+}
+
+static void
+pidgin_inactive_accounts_menu_class_init(PidginInactiveAccountsMenuClass *klass) {
+ GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+ GMenuModelClass *model_class = G_MENU_MODEL_CLASS(klass);
+
+ obj_class->constructed = pidgin_inactive_accounts_menu_constructed;
+ obj_class->dispose = pidgin_inactive_accounts_menu_dispose;
+
+ model_class->is_mutable = pidgin_inactive_accounts_menu_is_mutable;
+ model_class->get_n_items = pidgin_inactive_accounts_menu_get_n_items;
+ model_class->get_item_attributes = pidgin_inactive_accounts_menu_get_item_attributes;
+ model_class->get_item_links = pidgin_inactive_accounts_menu_get_item_links;
+}
+
+/******************************************************************************
+ * Public API
+ *****************************************************************************/
+GMenuModel *
+pidgin_inactive_accounts_menu_new(void) {
+ return g_object_new(PIDGIN_TYPE_INACTIVE_ACCOUNTS_MENU, NULL);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pidgin/pidgininactiveaccountsmenu.h Fri Mar 25 02:56:25 2022 -0500
@@ -0,0 +1,55 @@
+/*
+ * 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_INACTIVE_ACCOUNTS_MENU_H
+#define PIDGIN_INACTIVE_ACCOUNTS_MENU_H
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <purple.h>
+
+G_BEGIN_DECLS
+
+#define PIDGIN_TYPE_INACTIVE_ACCOUNTS_MENU (pidgin_inactive_accounts_menu_get_type())
+G_DECLARE_FINAL_TYPE(PidginInactiveAccountsMenu, pidgin_inactive_accounts_menu,
+ PIDGIN, INACTIVE_ACCOUNTS_MENU, GMenuModel)
+
+/**
+ * pidgin_inactive_accounts_menu_new:
+ *
+ * Creates a menu that will automatically update itself to include accounts
+ * that are inactive in libpurple.
+ *
+ * Returns: (transfer full): The new menu instance.
+ *
+ * Since: 3.0.0
+ */
+GMenuModel *pidgin_inactive_accounts_menu_new(void);
+
+G_END_DECLS
+
+#endif /* PIDGIN_INACTIVE_ACCOUNTS_MENU_H */
\ No newline at end of file
--- a/pidgin/resources/gtk/menus.ui Fri Mar 25 02:51:58 2022 -0500
+++ b/pidgin/resources/gtk/menus.ui Fri Mar 25 02:56:25 2022 -0500
@@ -184,7 +184,7 @@
<submenu>
<attribute name="label" translatable="yes">_Enable Account</attribute>
- <section id="disabled-accounts"/>
+ <section id="inactive-accounts"/>
</submenu>
</section>
<section id="enabled-accounts"/>
--- a/po/POTFILES.in Fri Mar 25 02:51:58 2022 -0500
+++ b/po/POTFILES.in Fri Mar 25 02:56:25 2022 -0500
@@ -336,7 +336,6 @@
pidgin/pidginaccountchooser.c
pidgin/pidginaccountfilterconnected.c
pidgin/pidginaccountfilterprotocol.c
-pidgin/pidginaccountsdisabledmenu.c
pidgin/pidginaccountsenabledmenu.c
pidgin/pidginaccountsmenu.c
pidgin/pidginaccountstore.c
@@ -357,6 +356,7 @@
pidgin/pidgindnd.c
pidgin/pidgingdkpixbuf.c
pidgin/pidginiconname.c
+pidgin/pidgininactiveaccountsmenu.c
pidgin/pidgininfopane.c
pidgin/pidgininvitedialog.c
pidgin/pidginmessage.c