geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

demoproxy.c
Go to the documentation of this file.
1/*
2 * demoproxy.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2015 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
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.
24 *
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.
29 */
30
31/* plugin API, always comes first */
32#include "geanyplugin.h"
33
34typedef struct {
35 GKeyFile *file;
36 gchar *help_text;
37 GSList *menu_items;
38}
40
41
42static gboolean proxy_init(GeanyPlugin *plugin, gpointer pdata)
43{
44 PluginContext *data;
45 gint i = 0;
46 gchar *text;
47
48 data = (PluginContext *) pdata;
49
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)
55 {
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);
60
61 if (!text)
62 break;
63
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);
70 }
71
72 return TRUE;
73}
74
75
76static void proxy_help(GeanyPlugin *plugin, gpointer pdata)
77{
78 PluginContext *data;
79 GtkWidget *dialog;
80
81 data = (PluginContext *) pdata;
82
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);
91
92 gtk_dialog_run(GTK_DIALOG(dialog));
93 gtk_widget_destroy(dialog);
94}
95
96
97static void proxy_cleanup(GeanyPlugin *plugin, gpointer pdata)
98{
99 PluginContext *data = (PluginContext *) pdata;
100
101 g_slist_free_full(data->menu_items, (GDestroyNotify) gtk_widget_destroy);
102 g_free(data->help_text);
103}
104
105
106static gint demoproxy_probe(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
107{
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)
114 {
115 if (fgets(linebuf, sizeof(linebuf), f) != NULL)
116 match = utils_str_equal(linebuf, "#!!PROXY_MAGIC!!\n");
117 fclose(f);
118 }
120}
121
122
123static gpointer demoproxy_load(GeanyPlugin *proxy, GeanyPlugin *plugin,
124 const gchar *filename, gpointer pdata)
125{
126 GKeyFile *file;
127 gboolean result;
128
129 file = g_key_file_new();
130 result = g_key_file_load_from_file(file, filename, 0, NULL);
131
132 if (result)
133 {
134 PluginContext *data = g_new0(PluginContext, 1);
135 data->file = file;
136
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);
141
142 plugin->funcs->init = proxy_init;
143 plugin->funcs->help = proxy_help;
144 plugin->funcs->cleanup = proxy_cleanup;
145
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;
150 }
151
152 g_key_file_free(file);
153 return NULL;
154}
155
156
157static void demoproxy_unload(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata)
158{
159 PluginContext *data = load_data;
160
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);
165
166 g_key_file_free(data->file);
167 g_free(data);
168}
169
170
171/* Called by Geany to initialize the plugin. */
172static gboolean demoproxy_init(GeanyPlugin *plugin, gpointer pdata)
173{
174 const gchar *extensions[] = { "ini", "px", NULL };
175
179
180 return geany_plugin_register_proxy(plugin, extensions);
181}
182
183
184/* Called by Geany before unloading the plugin. */
185static void demoproxy_cleanup(GeanyPlugin *plugin, gpointer data)
186{
187}
188
189
190G_MODULE_EXPORT
192{
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");
197
198 plugin->funcs->init = demoproxy_init;
200
201 GEANY_PLUGIN_REGISTER(plugin, 225);
202}
static gint demoproxy_probe(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
Definition: demoproxy.c:106
static gboolean demoproxy_init(GeanyPlugin *plugin, gpointer pdata)
Definition: demoproxy.c:172
static void proxy_help(GeanyPlugin *plugin, gpointer pdata)
Definition: demoproxy.c:76
static void demoproxy_cleanup(GeanyPlugin *plugin, gpointer data)
Definition: demoproxy.c:185
static void demoproxy_unload(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata)
Definition: demoproxy.c:157
static gboolean proxy_init(GeanyPlugin *plugin, gpointer pdata)
Definition: demoproxy.c:42
G_MODULE_EXPORT void geany_load_module(GeanyPlugin *plugin)
Called by Geany when a plugin library is loaded.
Definition: demoproxy.c:191
static void proxy_cleanup(GeanyPlugin *plugin, gpointer pdata)
Definition: demoproxy.c:97
static gpointer demoproxy_load(GeanyPlugin *proxy, GeanyPlugin *plugin, const gchar *filename, gpointer pdata)
Definition: demoproxy.c:123
gchar * text
Definition: editor.c:83
static bool match(const unsigned char *line, const char *word)
Definition: geany_tcl.c:55
Single include for plugins.
@ GEANY_PROXY_IGNORE
The proxy is not responsible at all, and Geany or other plugins are free to probe it.
Definition: plugindata.h:350
@ GEANY_PROXY_MATCH
The proxy is responsible for this file, and creates a plugin for it.
Definition: plugindata.h:355
#define GEANY_PLUGIN_REGISTER_FULL(plugin, min_api_version, pdata, free_func)
Convenience macro to register a plugin with data.
Definition: plugindata.h:335
gint geany_plugin_register_proxy(GeanyPlugin *plugin, const gchar **extensions)
Register the plugin as a proxy for other plugins.
Definition: plugins.c:2042
#define GEANY_PLUGIN_REGISTER(plugin, min_api_version)
Convenience macro to register a plugin.
Definition: plugindata.h:324
#define NULL
Definition: rbtree.h:150
const gchar filename[]
Definition: stash-example.c:4
GtkWidget * dialog
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
struct GeanyMainWidgets * main_widgets
Important widgets in the main window.
Definition: plugindata.h:169
GtkWidget * window
Main window.
Definition: ui_utils.h:80
GtkWidget * tools_menu
Most plugins add menu items to the Tools menu.
Definition: ui_utils.h:85
gboolean(* init)(GeanyPlugin *plugin, gpointer pdata)
Called to initialize the plugin, when the user activates it (must not be NULL)
Definition: plugindata.h:300
void(* help)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin should show some help, optional (can be NULL)
Definition: plugindata.h:304
void(* cleanup)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin is disabled or when Geany exits (must not be NULL)
Definition: plugindata.h:306
Basic information for the plugin and identification.
Definition: plugindata.h:233
GeanyData * geany_data
Pointer to global GeanyData intance.
Definition: plugindata.h:235
PluginInfo * info
Fields set in plugin_set_info().
Definition: plugindata.h:234
GeanyProxyFuncs * proxy_funcs
Hooks implemented by the plugin if it wants to act as a proxy Must be set prior to calling geany_plug...
Definition: plugindata.h:237
GeanyPluginFuncs * funcs
Functions implemented by the plugin, set in geany_load_module()
Definition: plugindata.h:236
void(* unload)(GeanyPlugin *proxy, GeanyPlugin *subplugin, gpointer load_data, gpointer pdata)
Called when the user initiates unloading of a plugin, e.g.
Definition: plugindata.h:397
gint(* probe)(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
Called to determine whether the proxy is truly responsible for the requested plugin.
Definition: plugindata.h:393
gpointer(* load)(GeanyPlugin *proxy, GeanyPlugin *subplugin, const gchar *filename, gpointer pdata)
Called after probe(), to perform the actual job of loading the plugin.
Definition: plugindata.h:395
Demo proxy - example of a basic proxy plugin for Geany.
Definition: demoproxy.c:34
GSList * menu_items
Definition: demoproxy.c:37
GKeyFile * file
Definition: demoproxy.c:35
gchar * help_text
Definition: demoproxy.c:36
const gchar * version
The version of the plugin.
Definition: plugindata.h:101
const gchar * description
The description of the plugin.
Definition: plugindata.h:99
const gchar * name
The name of the plugin.
Definition: plugindata.h:97
const gchar * author
The author of the plugin.
Definition: plugindata.h:103
#define _(String)
Definition: support.h:42
gboolean utils_str_equal(const gchar *a, const gchar *b)
NULL-safe string comparison.
Definition: utils.c:599