Merge pull request #3580 from techee/gtk_3_24
[geany-mirror.git] / plugins / demoproxy.c
blobdc905e1462bdf0a39217345e9871734e2f5463fa
1 /*
2 * demoproxy.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2015 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /**
22 * Demo proxy - example of a basic proxy plugin for Geany. Sub-plugins add menu items to the
23 * Tools menu and have a help dialog.
25 * Note: This is compiled but not installed by default. On Unix, you can install it by compiling
26 * Geany and then copying (or symlinking) to the plugins/demoproxy.so and
27 * plugins/demoproxytest.px files to ~/.config/geany/plugins
28 * - it will be loaded at next startup.
31 /* plugin API, always comes first */
32 #include "geanyplugin.h"
34 typedef struct {
35 GKeyFile *file;
36 gchar *help_text;
37 GSList *menu_items;
39 PluginContext;
42 static gboolean proxy_init(GeanyPlugin *plugin, gpointer pdata)
44 PluginContext *data;
45 gint i = 0;
46 gchar *text;
48 data = (PluginContext *) pdata;
50 /* Normally, you would instruct the VM/interpreter to call into the actual plugin. The
51 * plugin would be identified by pdata. Because there is no interpreter for
52 * .ini files we do it inline, as this is just a demo */
53 data->help_text = g_key_file_get_locale_string(data->file, "Help", "text", NULL, NULL);
54 while (TRUE)
56 GtkWidget *item;
57 gchar *key = g_strdup_printf("item%d", i++);
58 text = g_key_file_get_locale_string(data->file, "Init", key, NULL, NULL);
59 g_free(key);
61 if (!text)
62 break;
64 item = gtk_menu_item_new_with_label(text);
65 gtk_widget_show(item);
66 gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu), item);
67 gtk_widget_set_sensitive(item, FALSE);
68 data->menu_items = g_slist_prepend(data->menu_items, (gpointer) item);
69 g_free(text);
72 return TRUE;
76 static void proxy_help(GeanyPlugin *plugin, gpointer pdata)
78 PluginContext *data;
79 GtkWidget *dialog;
81 data = (PluginContext *) pdata;
83 dialog = gtk_message_dialog_new(
84 GTK_WINDOW(plugin->geany_data->main_widgets->window),
85 GTK_DIALOG_DESTROY_WITH_PARENT,
86 GTK_MESSAGE_INFO,
87 GTK_BUTTONS_OK,
88 "%s", data->help_text);
89 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
90 _("(From the %s plugin)"), plugin->info->name);
92 gtk_dialog_run(GTK_DIALOG(dialog));
93 gtk_widget_destroy(dialog);
97 static void proxy_cleanup(GeanyPlugin *plugin, gpointer pdata)
99 PluginContext *data = (PluginContext *) pdata;
101 g_slist_free_full(data->menu_items, (GDestroyNotify) gtk_widget_destroy);
102 g_free(data->help_text);
106 static gint demoproxy_probe(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
108 /* We know the extension is right (Geany checks that). For demo purposes we perform an
109 * additional check. This is not necessary when the extension is unique enough. */
110 gboolean match = FALSE;
111 gchar linebuf[128];
112 FILE *f = fopen(filename, "r");
113 if (f != NULL)
115 if (fgets(linebuf, sizeof(linebuf), f) != NULL)
116 match = utils_str_equal(linebuf, "#!!PROXY_MAGIC!!\n");
117 fclose(f);
119 return match ? GEANY_PROXY_MATCH : GEANY_PROXY_IGNORE;
123 static gpointer demoproxy_load(GeanyPlugin *proxy, GeanyPlugin *plugin,
124 const gchar *filename, gpointer pdata)
126 GKeyFile *file;
127 gboolean result;
129 file = g_key_file_new();
130 result = g_key_file_load_from_file(file, filename, 0, NULL);
132 if (result)
134 PluginContext *data = g_new0(PluginContext, 1);
135 data->file = file;
137 plugin->info->name = g_key_file_get_locale_string(data->file, "Info", "name", NULL, NULL);
138 plugin->info->description = g_key_file_get_locale_string(data->file, "Info", "description", NULL, NULL);
139 plugin->info->version = g_key_file_get_locale_string(data->file, "Info", "version", NULL, NULL);
140 plugin->info->author = g_key_file_get_locale_string(data->file, "Info", "author", NULL, NULL);
142 plugin->funcs->init = proxy_init;
143 plugin->funcs->help = proxy_help;
144 plugin->funcs->cleanup = proxy_cleanup;
146 /* Cannot pass g_free as free_func be Geany calls it before unloading, and since
147 * demoproxy_unload() accesses the data this would be catastrophic */
148 GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, NULL);
149 return data;
152 g_key_file_free(file);
153 return NULL;
157 static void demoproxy_unload(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata)
159 PluginContext *data = load_data;
161 g_free((gchar *)plugin->info->name);
162 g_free((gchar *)plugin->info->description);
163 g_free((gchar *)plugin->info->version);
164 g_free((gchar *)plugin->info->author);
166 g_key_file_free(data->file);
167 g_free(data);
171 /* Called by Geany to initialize the plugin. */
172 static gboolean demoproxy_init(GeanyPlugin *plugin, gpointer pdata)
174 const gchar *extensions[] = { "ini", "px", NULL };
176 plugin->proxy_funcs->probe = demoproxy_probe;
177 plugin->proxy_funcs->load = demoproxy_load;
178 plugin->proxy_funcs->unload = demoproxy_unload;
180 return geany_plugin_register_proxy(plugin, extensions);
184 /* Called by Geany before unloading the plugin. */
185 static void demoproxy_cleanup(GeanyPlugin *plugin, gpointer data)
190 G_MODULE_EXPORT
191 void geany_load_module(GeanyPlugin *plugin)
193 plugin->info->name = _("Demo Proxy");
194 plugin->info->description = _("Example Proxy.");
195 plugin->info->version = "0.1";
196 plugin->info->author = _("The Geany developer team");
198 plugin->funcs->init = demoproxy_init;
199 plugin->funcs->cleanup = demoproxy_cleanup;
201 GEANY_PLUGIN_REGISTER(plugin, 225);