summaryrefslogtreecommitdiff
path: root/pluma/pluma-plugin.c
blob: b89ea11d4dccc7f09e638e1cd9ab86846cc04d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * pluma-plugin.h
 * This file is part of pluma
 *
 * Copyright (C) 2002-2005 Paolo Maggi 
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA. 
 */
 
/*
 * Modified by the pluma Team, 2002-2005. See the AUTHORS file for a 
 * list of people on the pluma Team.  
 * See the ChangeLog files for a list of changes. 
 *
 * $Id$
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pluma-plugin.h"
#include "pluma-dirs.h"

/* properties */
enum {
	PROP_0,
	PROP_INSTALL_DIR,
	PROP_DATA_DIR_NAME,
	PROP_DATA_DIR
};

typedef struct _PlumaPluginPrivate PlumaPluginPrivate;

struct _PlumaPluginPrivate
{
	gchar *install_dir;
	gchar *data_dir_name;
};

#define PLUMA_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), PLUMA_TYPE_PLUGIN, PlumaPluginPrivate))

G_DEFINE_TYPE(PlumaPlugin, pluma_plugin, G_TYPE_OBJECT)

static void
dummy (PlumaPlugin *plugin, PlumaWindow *window)
{
	/* Empty */
}

static GtkWidget *
create_configure_dialog	(PlumaPlugin *plugin)
{
	return NULL;
}

static gboolean
is_configurable (PlumaPlugin *plugin)
{
	return (PLUMA_PLUGIN_GET_CLASS (plugin)->create_configure_dialog !=
		create_configure_dialog);
}

static void
pluma_plugin_get_property (GObject    *object,
			   guint       prop_id,
			   GValue     *value,
			   GParamSpec *pspec)
{
	switch (prop_id)
	{
		case PROP_INSTALL_DIR:
			g_value_take_string (value, pluma_plugin_get_install_dir (PLUMA_PLUGIN (object)));
			break;
		case PROP_DATA_DIR:
			g_value_take_string (value, pluma_plugin_get_data_dir (PLUMA_PLUGIN (object)));
			break;
		default:
			g_return_if_reached ();
	}
}

static void
pluma_plugin_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
	PlumaPluginPrivate *priv = PLUMA_PLUGIN_GET_PRIVATE (object);

	switch (prop_id)
	{
		case PROP_INSTALL_DIR:
			priv->install_dir = g_value_dup_string (value);
			break;
		case PROP_DATA_DIR_NAME:
			priv->data_dir_name = g_value_dup_string (value);
			break;
		default:
			g_return_if_reached ();
	}
}

static void
pluma_plugin_finalize (GObject *object)
{
	PlumaPluginPrivate *priv = PLUMA_PLUGIN_GET_PRIVATE (object);

	g_free (priv->install_dir);
	g_free (priv->data_dir_name);

	G_OBJECT_CLASS (pluma_plugin_parent_class)->finalize (object);
}

static void 
pluma_plugin_class_init (PlumaPluginClass *klass)
{
    	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	klass->activate = dummy;
	klass->deactivate = dummy;
	klass->update_ui = dummy;
	
	klass->create_configure_dialog = create_configure_dialog;
	klass->is_configurable = is_configurable;

	object_class->get_property = pluma_plugin_get_property;
	object_class->set_property = pluma_plugin_set_property;
	object_class->finalize = pluma_plugin_finalize;

	g_object_class_install_property (object_class,
					 PROP_INSTALL_DIR,
					 g_param_spec_string ("install-dir",
							      "Install Directory",
							      "The directory where the plugin is installed",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	/* the basename of the data dir is set at construction time by the plugin loader
	 * while the full path is constructed on the fly to take into account relocability
	 * that's why we have a writeonly prop and a readonly prop */
	g_object_class_install_property (object_class,
					 PROP_DATA_DIR_NAME,
					 g_param_spec_string ("data-dir-name",
							      "Basename of the data directory",
							      "The basename of the directory where the plugin should look for its data files",
							      NULL,
							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (object_class,
					 PROP_DATA_DIR,
					 g_param_spec_string ("data-dir",
							      "Data Directory",
							      "The full path of the directory where the plugin should look for its data files",
							      NULL,
							      G_PARAM_READABLE));

	g_type_class_add_private (klass, sizeof (PlumaPluginPrivate));
}

static void
pluma_plugin_init (PlumaPlugin *plugin)
{
	/* Empty */
}

/**
 * pluma_plugin_get_install_dir:
 * @plugin: a #PlumaPlugin
 *
 * Get the path of the directory where the plugin is installed.
 *
 * Return value: a newly allocated string with the path of the
 * directory where the plugin is installed
 */
gchar *
pluma_plugin_get_install_dir (PlumaPlugin *plugin)
{
	g_return_val_if_fail (PLUMA_IS_PLUGIN (plugin), NULL);

	return g_strdup (PLUMA_PLUGIN_GET_PRIVATE (plugin)->install_dir);
}

/**
 * pluma_plugin_get_data_dir:
 * @plugin: a #PlumaPlugin
 *
 * Get the path of the directory where the plugin should look for
 * its data files.
 *
 * Return value: a newly allocated string with the path of the
 * directory where the plugin should look for its data files
 */
gchar *
pluma_plugin_get_data_dir (PlumaPlugin *plugin)
{
	PlumaPluginPrivate *priv;
	gchar *pluma_lib_dir;
	gchar *data_dir;

	g_return_val_if_fail (PLUMA_IS_PLUGIN (plugin), NULL);

	priv = PLUMA_PLUGIN_GET_PRIVATE (plugin);

	/* If it's a "user" plugin the data dir is
	 * install_dir/data_dir_name if instead it's a
	 * "system" plugin the data dir is under pluma_data_dir,
	 * so it's under $prefix/share/pluma-2/plugins/data_dir_name
	 * where data_dir_name usually it's the name of the plugin
	 */
	pluma_lib_dir = pluma_dirs_get_pluma_lib_dir ();

	/* CHECK: is checking the prefix enough or should we be more
	 * careful about normalizing paths etc? */
	if (g_str_has_prefix (priv->install_dir, pluma_lib_dir))
	{
		gchar *pluma_data_dir;

		pluma_data_dir = pluma_dirs_get_pluma_data_dir ();

		data_dir = g_build_filename (pluma_data_dir,
					     "plugins",
					     priv->data_dir_name,
					     NULL);

		g_free (pluma_data_dir);
	}
	else
	{
		data_dir = g_build_filename (priv->install_dir,
					     priv->data_dir_name,
					     NULL);
	}

	g_free (pluma_lib_dir);

	return data_dir;
}

/**
 * pluma_plugin_activate:
 * @plugin: a #PlumaPlugin
 * @window: a #PlumaWindow
 * 
 * Activates the plugin.
 */
void
pluma_plugin_activate (PlumaPlugin *plugin,
		       PlumaWindow *window)
{
	g_return_if_fail (PLUMA_IS_PLUGIN (plugin));
	g_return_if_fail (PLUMA_IS_WINDOW (window));
	
	PLUMA_PLUGIN_GET_CLASS (plugin)->activate (plugin, window);
}

/**
 * pluma_plugin_deactivate:
 * @plugin: a #PlumaPlugin
 * @window: a #PlumaWindow
 * 
 * Deactivates the plugin.
 */
void
pluma_plugin_deactivate	(PlumaPlugin *plugin,
			 PlumaWindow *window)
{
	g_return_if_fail (PLUMA_IS_PLUGIN (plugin));
	g_return_if_fail (PLUMA_IS_WINDOW (window));

	PLUMA_PLUGIN_GET_CLASS (plugin)->deactivate (plugin, window);
}

/**
 * pluma_plugin_update_ui:
 * @plugin: a #PlumaPlugin
 * @window: a #PlumaWindow
 *
 * Triggers an update of the user interface to take into account state changes
 * caused by the plugin.
 */		 
void
pluma_plugin_update_ui	(PlumaPlugin *plugin,
			 PlumaWindow *window)
{
	g_return_if_fail (PLUMA_IS_PLUGIN (plugin));
	g_return_if_fail (PLUMA_IS_WINDOW (window));

	PLUMA_PLUGIN_GET_CLASS (plugin)->update_ui (plugin, window);
}

/**
 * pluma_plugin_is_configurable:
 * @plugin: a #PlumaPlugin
 *
 * Whether the plugin is configurable.
 *
 * Returns: TRUE if the plugin is configurable:
 */
gboolean
pluma_plugin_is_configurable (PlumaPlugin *plugin)
{
	g_return_val_if_fail (PLUMA_IS_PLUGIN (plugin), FALSE);

	return PLUMA_PLUGIN_GET_CLASS (plugin)->is_configurable (plugin);
}

/**
 * pluma_plugin_create_configure_dialog:
 * @plugin: a #PlumaPlugin
 *
 * Creates the configure dialog widget for the plugin.
 *
 * Returns: the configure dialog widget for the plugin.
 */
GtkWidget *
pluma_plugin_create_configure_dialog (PlumaPlugin *plugin)
{
	g_return_val_if_fail (PLUMA_IS_PLUGIN (plugin), NULL);
	
	return PLUMA_PLUGIN_GET_CLASS (plugin)->create_configure_dialog (plugin);
}