Re-request Authorization with libpurple?

Paul Aurich paul at darkrain42.org
Wed Aug 12 03:47:13 EDT 2009


On Aug 11, 2009, at 20:09, michaelcbrook at msn.com wrote:
>    GList *menu = prpl_info->blist_node_menu((PurpleBlistNode *)buddy);
>
>    int i = 0;
>
>      while (i < g_list_length(menu))
>      {
>
>      PurpleMenuAction *menu_action = g_list_nth_data(menu, i);
>
>        if (strcmp(menu_action->label, "Re-request Authorization")==0)
>        {
>        printf("Found re-request menu option...\n");
>        menu_action->callback((PurpleBlistNode *)buddy);
>        break;
>        }
>
>      i++;
>
>      }


This is an inefficient way to iterate through the linked list  
(g_list_nth_data walks the list to the nth element, so if you run this  
loop over the entire list, you'll end up doing O(n^2) operations).

Typically something like this is used (with variations depending on  
whether or not you need to free the list, which, in this case, you do):

while (menu) {
	PurpleMenuAction *menu_action = menu->data;

	if (g_str_equal(menu_action->label, "...")) {
		do stuff
	}

	/* Destroy the data in the linked list */
	purple_menu_action_free(menu);
	/* Remove this node from the list */
	menu = g_list_delete_link(menu, menu);
}

if you weren't freeing the list, it might look something like:

GList *menu = something;
for ( ; menu; menu = menu->next) {
	/* Do something */
}

~Paul




More information about the Support mailing list