Skip to content

Commit

Permalink
plugins: use GQueue to restore GLib compatibility
Browse files Browse the repository at this point in the history
g_ptr_array_insert() is too recent (2.40), but prepending is required. GQueue
is a fine replacement with better old-glib support, at the expense of working
with a doubly-linked list instead of plain array.
  • Loading branch information
kugel- committed Sep 21, 2015
1 parent 6f69f4e commit 5cda5b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/plugins.c
Expand Up @@ -97,7 +97,7 @@ static PluginProxy builtin_so_proxy = {
.plugin = &builtin_so_proxy_plugin,
};

static GPtrArray *active_proxies = NULL;
static GQueue active_proxies = G_QUEUE_INIT;

static void plugin_free(Plugin *plugin);

Expand Down Expand Up @@ -899,24 +899,23 @@ static void free_subplugins(Plugin *proxy)
}
}


/* Returns true when the removal was successful (=> never for non-pluxies) */
static gboolean
unregister_proxy(Plugin *proxy)
{
PluginProxy *p;
gint i;
gboolean is_proxy = FALSE;
GList *node;

/* Remove the proxy from the proxy list first. It might appear more than once (once
* for each extension), but if it doesn't appear at all it's not actually a proxy */
foreach_ptr_array(p, i, active_proxies)
foreach_queue(node, &active_proxies)
{
p = node->data;
if (p->plugin == proxy)
{
is_proxy = TRUE;
g_ptr_array_remove(active_proxies, p);
i -= 1;
g_queue_delete_link(&active_proxies, node);
}
}
return is_proxy;
Expand Down Expand Up @@ -1017,8 +1016,8 @@ static gboolean check_plugin_path(const gchar *fname)
static PluginProxy* is_plugin(const gchar *file)
{
PluginProxy *proxy;
GList *node;
const gchar *ext;
gint i;

/* extract file extension to avoid g_str_has_suffix() in the loop */
ext = (const gchar *)strrchr(file, '.');
Expand All @@ -1031,8 +1030,9 @@ static PluginProxy* is_plugin(const gchar *file)
ext += 1;
/* O(n*m), (m being extensions per proxy) doesn't scale very well in theory
* but not a problem in practice yet */
foreach_ptr_array(proxy, i, active_proxies)
foreach_queue(node, &active_proxies)
{
proxy = node->data;
if (utils_str_casecmp(ext, proxy->extension) != 0)
{
Plugin *p = proxy->plugin;
Expand Down Expand Up @@ -1069,7 +1069,7 @@ load_active_plugins(void)
/* If proxys are loaded we have to restart to load plugins that sort before their proxy */
do
{
proxies = active_proxies->len;
proxies = active_proxies.length;
if (failed_plugins_list != NULL)
g_list_free_full(failed_plugins_list, (GDestroyNotify) g_free);
failed_plugins_list = NULL;
Expand All @@ -1086,7 +1086,7 @@ load_active_plugins(void)
failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
}
}
} while (proxies != active_proxies->len);
} while (proxies != active_proxies.length);
}


Expand Down Expand Up @@ -1297,8 +1297,7 @@ void plugins_init(void)
g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);

active_proxies = g_ptr_array_sized_new(1);
g_ptr_array_add(active_proxies, &builtin_so_proxy);
g_queue_push_head(&active_proxies, &builtin_so_proxy);
}


Expand Down Expand Up @@ -1472,7 +1471,7 @@ static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer
/* save the filename and proxy of the plugin */
file_name = g_strdup(p->filename);
proxy = p->proxy;
prev_num_proxies = active_proxies->len;
prev_num_proxies = active_proxies.length;

/* unload plugin module */
if (!state)
Expand Down Expand Up @@ -1528,11 +1527,11 @@ static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer
}
/* We need to find out if a proxy was added or removed because that affects the plugin list
* presented by the plugin manager */
if (prev_num_proxies != active_proxies->len)
if (prev_num_proxies != active_proxies.length)
{
/* Rescan the plugin list as we now support more. Gives some "already loaded" warnings
* they are unproblematic */
if (prev_num_proxies < active_proxies->len)
if (prev_num_proxies < active_proxies.length)
load_all_plugins();

pm_populate(pm_widgets.store);
Expand Down Expand Up @@ -2003,7 +2002,7 @@ gboolean geany_plugin_register_proxy(GeanyPlugin *plugin, const gchar **extensio
g_strlcpy(proxy->extension, *ext, sizeof(proxy->extension));
proxy->plugin = p;
/* prepend, so that plugins automatically override core providers for a given extension */
g_ptr_array_insert(active_proxies, 0, proxy);
g_queue_push_head(&active_proxies, proxy);
}

return TRUE;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.h
Expand Up @@ -116,6 +116,14 @@ G_BEGIN_DECLS
#define foreach_slist(node, list) \
foreach_list(node, list)

/* Iterates all the nodes in @a queue. Safe against removal during iteration
* @param node should be a (@c GList*).
* @param list @c GQueue to traverse. */
#define foreach_queue(node, q) \
for (GList *_node = (q)->head, *next = (q)->head->next; \
(node = _node) != NULL; \
_node = next, next = next ? next->next : NULL)

/** Iterates through each unsorted filename in a @c GDir.
* @param filename (@c const @c gchar*) locale-encoded filename, without path. Do not modify or free.
* @param dir @c GDir created with @c g_dir_open(). Call @c g_dir_close() afterwards.
Expand Down

0 comments on commit 5cda5b7

Please sign in to comment.