File: plugin.c

package info (click to toggle)
anjuta 2%3A3.34.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 72,440 kB
  • sloc: ansic: 207,444; sh: 47,499; cpp: 11,461; makefile: 3,586; yacc: 2,821; perl: 2,094; lex: 1,546; xml: 904; python: 149; sql: 99; javascript: 51; java: 10
file content (321 lines) | stat: -rw-r--r-- 8,001 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
    plugin.c
    Copyright (C) 2000 Naba Kumar

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
 * Plugins functions
 *
 *---------------------------------------------------------------------------*/

#include <config.h>

#include "plugin.h"

#include "druid.h"
#include "tar.h"

#include <libanjuta/anjuta-debug.h>
#include <libanjuta/interfaces/ianjuta-wizard.h>
#include <libanjuta/interfaces/ianjuta-file.h>


/* Private functions
 *---------------------------------------------------------------------------*/

static void
npw_open_project_template (GFile *destination, GFile *tarfile, gpointer data, GError *error)
{
	NPWPlugin *plugin = (NPWPlugin *)data;

	if (error != NULL)
	{
		gchar *tarname = g_file_get_path (tarfile);

		anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),_("Unable to extract project template %s: %s"), tarname, error->message);
	}
	else
	{
		/* Show the project wizard dialog, loading only the new projects */
		npw_plugin_show_wizard (plugin, destination);
	}
}

static gboolean
npw_install_project_template_with_callback (NPWPlugin *plugin, GFile *file, NPWTarCompleteFunc callback, GError **error)
{
	GFileInputStream *stream;
	gchar *name;
	gchar *ext;
	gchar *path;
	GFile *dest;
	gboolean ok;
	GError *err = NULL;

	/* Check if tarfile exist */
	stream = g_file_read (file, NULL, error);
	if (stream == NULL)
	{
		return FALSE;
	}
	g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);

	/* Get name without extension */
	name = g_file_get_basename (file);
	ext = strchr (name, '.');
	if (ext != NULL) *ext = '\0';

	/* Create a directory for template */
	path = g_build_filename (g_get_user_data_dir (), "anjuta", "templates", name, NULL);
	g_free (name);
	dest = g_file_new_for_path (path);
	g_free (path);
	ok = g_file_make_directory_with_parents (dest, NULL, &err);
	if (err != NULL)
	{
		if (err->code == G_IO_ERROR_EXISTS)
		{
			/* Allow to overwrite directories */
			ok = TRUE;
			g_error_free (err);
		}
		else
		{
			g_object_unref (dest);

			return FALSE;
		}
	}

	ok = npw_tar_extract (dest, file, callback, plugin, error);
	g_object_unref (dest);

	return ok;
}

/* Public functions
 *---------------------------------------------------------------------------*/

/* Display the project wizard selection dialog using only templates in the
 * specified directory if non NULL */
gboolean
npw_plugin_show_wizard (NPWPlugin *plugin, GFile *project)
{
	if (plugin->install != NULL)
	{
		/* New project wizard is busy copying project file */
	}
	else if (plugin->druid == NULL)
	{
		/* Create a new project wizard druid */
		npw_druid_new (plugin, project);
	}

	if (plugin->druid != NULL)
	{
		/* New project wizard druid is waiting for user inputs */
		npw_druid_show (plugin->druid);
	}


	return TRUE;
}

/*---------------------------------------------------------------------------*/

/* Used in dispose */
static gpointer parent_class;

static void
npw_plugin_instance_init (GObject *obj)
{
	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);

	plugin->druid = NULL;
	plugin->install = NULL;
	plugin->view = NULL;
}

/* dispose is used to unref object created with instance_init */

static void
npw_plugin_dispose (GObject *obj)
{
	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);

	/* Warning this function could be called several times */
	if (plugin->view != NULL)
	{
		g_object_remove_weak_pointer (G_OBJECT (plugin->view),
									  (gpointer*)(gpointer)&plugin->view);
		plugin->view = NULL;
	}

	G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static void
npw_plugin_finalize (GObject *obj)
{
	G_OBJECT_CLASS (parent_class)->finalize (obj);
}

/* finalize used to free object created with instance init is not used */

static gboolean
npw_plugin_activate (AnjutaPlugin *plugin)
{
	DEBUG_PRINT ("%s", "Project Wizard Plugin: Activating project wizard plugin...");
	return TRUE;
}

static gboolean
npw_plugin_deactivate (AnjutaPlugin *plugin)
{
	DEBUG_PRINT ("%s", "Project Wizard Plugin: Deactivating project wizard plugin...");
	return TRUE;
}

static void
npw_plugin_class_init (GObjectClass *klass)
{
	AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	plugin_class->activate = npw_plugin_activate;
	plugin_class->deactivate = npw_plugin_deactivate;
	klass->dispose = npw_plugin_dispose;
	klass->finalize = npw_plugin_finalize;
}

/* IAnjutaWizard implementation
 *---------------------------------------------------------------------------*/

static void
iwizard_activate (IAnjutaWizard *wiz, GError **err)
{
	npw_plugin_show_wizard (ANJUTA_PLUGIN_NPW (wiz), NULL);
}

static void
iwizard_iface_init (IAnjutaWizardIface *iface)
{
	iface->activate = iwizard_activate;
}

/* IAnjutaFile implementation
 *---------------------------------------------------------------------------*/

static void
ifile_open (IAnjutaFile *ifile, GFile* file, GError **error)
{
	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (ifile);
	GFileInfo *info;

	info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL, NULL);
	if (info != NULL)
	{
		if (strcmp(g_file_info_get_content_type (info), "application/x-anjuta-project-template") == 0)
		{
			npw_plugin_show_wizard (plugin, file);
		}
		else
		{
			npw_install_project_template_with_callback (plugin, file, npw_open_project_template, error);
		}
		g_object_unref (info);
	}
}

static GFile*
ifile_get_file (IAnjutaFile* ifile, GError** error)
{
	return NULL;
}

static void
ifile_iface_init(IAnjutaFileIface *iface)
{
	iface->open = ifile_open;
	iface->get_file = ifile_get_file;
}

ANJUTA_PLUGIN_BEGIN (NPWPlugin, npw_plugin);
ANJUTA_PLUGIN_ADD_INTERFACE (ifile, IANJUTA_TYPE_FILE);
ANJUTA_PLUGIN_ADD_INTERFACE (iwizard, IANJUTA_TYPE_WIZARD);
ANJUTA_PLUGIN_END;

ANJUTA_SIMPLE_PLUGIN (NPWPlugin, npw_plugin);

/* Control access to anjuta message view to avoid a closed view
 *---------------------------------------------------------------------------*/

static void
on_message_buffer_flush (IAnjutaMessageView *view, const gchar *line,
						 NPWPlugin *plugin)
{
	npw_plugin_print_view (plugin, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line, "");
}

IAnjutaMessageView*
npw_plugin_create_view (NPWPlugin* plugin)
{
	if (plugin->view == NULL)
	{
		IAnjutaMessageManager* man;

		man = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
										  IAnjutaMessageManager, NULL);
		plugin->view =
			ianjuta_message_manager_add_view (man, _("New Project Assistant"),
											  ICON_FILE, NULL);
		if (plugin->view != NULL)
		{
			g_signal_connect (G_OBJECT (plugin->view), "buffer_flushed",
							  G_CALLBACK (on_message_buffer_flush), plugin);
			g_object_add_weak_pointer (G_OBJECT (plugin->view),
									   (gpointer *)(gpointer)&plugin->view);
		}
	}
	else
	{
		ianjuta_message_view_clear (plugin->view, NULL);
	}

	return plugin->view;
}

void
npw_plugin_append_view (NPWPlugin* plugin, const gchar* text)
{
	if (plugin->view)
	{
		ianjuta_message_view_buffer_append (plugin->view, text, NULL);
	}
}

void
npw_plugin_print_view (NPWPlugin* plugin, IAnjutaMessageViewType type,
					   const gchar* summary, const gchar* details)
{
	if (plugin->view)
	{
		ianjuta_message_view_append (plugin->view, type, summary, details, NULL);
	}
}