1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3     plugin.c
4     Copyright (C) 2000 Naba Kumar
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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20 
21 /*
22  * Plugins functions
23  *
24  *---------------------------------------------------------------------------*/
25 
26 #include <config.h>
27 
28 #include "plugin.h"
29 
30 #include "druid.h"
31 #include "tar.h"
32 
33 #include <libanjuta/anjuta-debug.h>
34 #include <libanjuta/interfaces/ianjuta-wizard.h>
35 #include <libanjuta/interfaces/ianjuta-file.h>
36 
37 
38 /* Private functions
39  *---------------------------------------------------------------------------*/
40 
41 static void
npw_open_project_template(GFile * destination,GFile * tarfile,gpointer data,GError * error)42 npw_open_project_template (GFile *destination, GFile *tarfile, gpointer data, GError *error)
43 {
44 	NPWPlugin *plugin = (NPWPlugin *)data;
45 
46 	if (error != NULL)
47 	{
48 		gchar *tarname = g_file_get_path (tarfile);
49 
50 		anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),_("Unable to extract project template %s: %s"), tarname, error->message);
51 	}
52 	else
53 	{
54 		/* Show the project wizard dialog, loading only the new projects */
55 		npw_plugin_show_wizard (plugin, destination);
56 	}
57 }
58 
59 static gboolean
npw_install_project_template_with_callback(NPWPlugin * plugin,GFile * file,NPWTarCompleteFunc callback,GError ** error)60 npw_install_project_template_with_callback (NPWPlugin *plugin, GFile *file, NPWTarCompleteFunc callback, GError **error)
61 {
62 	GFileInputStream *stream;
63 	gchar *name;
64 	gchar *ext;
65 	gchar *path;
66 	GFile *dest;
67 	gboolean ok;
68 	GError *err = NULL;
69 
70 	/* Check if tarfile exist */
71 	stream = g_file_read (file, NULL, error);
72 	if (stream == NULL)
73 	{
74 		return FALSE;
75 	}
76 	g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
77 
78 	/* Get name without extension */
79 	name = g_file_get_basename (file);
80 	ext = strchr (name, '.');
81 	if (ext != NULL) *ext = '\0';
82 
83 	/* Create a directory for template */
84 	path = g_build_filename (g_get_user_data_dir (), "anjuta", "templates", name, NULL);
85 	g_free (name);
86 	dest = g_file_new_for_path (path);
87 	g_free (path);
88 	ok = g_file_make_directory_with_parents (dest, NULL, &err);
89 	if (err != NULL)
90 	{
91 		if (err->code == G_IO_ERROR_EXISTS)
92 		{
93 			/* Allow to overwrite directories */
94 			ok = TRUE;
95 			g_error_free (err);
96 		}
97 		else
98 		{
99 			g_object_unref (dest);
100 
101 			return FALSE;
102 		}
103 	}
104 
105 	ok = npw_tar_extract (dest, file, callback, plugin, error);
106 	g_object_unref (dest);
107 
108 	return ok;
109 }
110 
111 /* Public functions
112  *---------------------------------------------------------------------------*/
113 
114 /* Display the project wizard selection dialog using only templates in the
115  * specified directory if non NULL */
116 gboolean
npw_plugin_show_wizard(NPWPlugin * plugin,GFile * project)117 npw_plugin_show_wizard (NPWPlugin *plugin, GFile *project)
118 {
119 	if (plugin->install != NULL)
120 	{
121 		/* New project wizard is busy copying project file */
122 	}
123 	else if (plugin->druid == NULL)
124 	{
125 		/* Create a new project wizard druid */
126 		npw_druid_new (plugin, project);
127 	}
128 
129 	if (plugin->druid != NULL)
130 	{
131 		/* New project wizard druid is waiting for user inputs */
132 		npw_druid_show (plugin->druid);
133 	}
134 
135 
136 	return TRUE;
137 }
138 
139 /*---------------------------------------------------------------------------*/
140 
141 /* Used in dispose */
142 static gpointer parent_class;
143 
144 static void
npw_plugin_instance_init(GObject * obj)145 npw_plugin_instance_init (GObject *obj)
146 {
147 	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);
148 
149 	plugin->druid = NULL;
150 	plugin->install = NULL;
151 	plugin->view = NULL;
152 }
153 
154 /* dispose is used to unref object created with instance_init */
155 
156 static void
npw_plugin_dispose(GObject * obj)157 npw_plugin_dispose (GObject *obj)
158 {
159 	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);
160 
161 	/* Warning this function could be called several times */
162 	if (plugin->view != NULL)
163 	{
164 		g_object_remove_weak_pointer (G_OBJECT (plugin->view),
165 									  (gpointer*)(gpointer)&plugin->view);
166 		plugin->view = NULL;
167 	}
168 
169 	G_OBJECT_CLASS (parent_class)->dispose (obj);
170 }
171 
172 static void
npw_plugin_finalize(GObject * obj)173 npw_plugin_finalize (GObject *obj)
174 {
175 	G_OBJECT_CLASS (parent_class)->finalize (obj);
176 }
177 
178 /* finalize used to free object created with instance init is not used */
179 
180 static gboolean
npw_plugin_activate(AnjutaPlugin * plugin)181 npw_plugin_activate (AnjutaPlugin *plugin)
182 {
183 	DEBUG_PRINT ("%s", "Project Wizard Plugin: Activating project wizard plugin...");
184 	return TRUE;
185 }
186 
187 static gboolean
npw_plugin_deactivate(AnjutaPlugin * plugin)188 npw_plugin_deactivate (AnjutaPlugin *plugin)
189 {
190 	DEBUG_PRINT ("%s", "Project Wizard Plugin: Deactivating project wizard plugin...");
191 	return TRUE;
192 }
193 
194 static void
npw_plugin_class_init(GObjectClass * klass)195 npw_plugin_class_init (GObjectClass *klass)
196 {
197 	AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
198 
199 	parent_class = g_type_class_peek_parent (klass);
200 
201 	plugin_class->activate = npw_plugin_activate;
202 	plugin_class->deactivate = npw_plugin_deactivate;
203 	klass->dispose = npw_plugin_dispose;
204 	klass->finalize = npw_plugin_finalize;
205 }
206 
207 /* IAnjutaWizard implementation
208  *---------------------------------------------------------------------------*/
209 
210 static void
iwizard_activate(IAnjutaWizard * wiz,GError ** err)211 iwizard_activate (IAnjutaWizard *wiz, GError **err)
212 {
213 	npw_plugin_show_wizard (ANJUTA_PLUGIN_NPW (wiz), NULL);
214 }
215 
216 static void
iwizard_iface_init(IAnjutaWizardIface * iface)217 iwizard_iface_init (IAnjutaWizardIface *iface)
218 {
219 	iface->activate = iwizard_activate;
220 }
221 
222 /* IAnjutaFile implementation
223  *---------------------------------------------------------------------------*/
224 
225 static void
ifile_open(IAnjutaFile * ifile,GFile * file,GError ** error)226 ifile_open (IAnjutaFile *ifile, GFile* file, GError **error)
227 {
228 	NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (ifile);
229 	GFileInfo *info;
230 
231 	info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL, NULL);
232 	if (info != NULL)
233 	{
234 		if (strcmp(g_file_info_get_content_type (info), "application/x-anjuta-project-template") == 0)
235 		{
236 			npw_plugin_show_wizard (plugin, file);
237 		}
238 		else
239 		{
240 			npw_install_project_template_with_callback (plugin, file, npw_open_project_template, error);
241 		}
242 		g_object_unref (info);
243 	}
244 }
245 
246 static GFile*
ifile_get_file(IAnjutaFile * ifile,GError ** error)247 ifile_get_file (IAnjutaFile* ifile, GError** error)
248 {
249 	return NULL;
250 }
251 
252 static void
ifile_iface_init(IAnjutaFileIface * iface)253 ifile_iface_init(IAnjutaFileIface *iface)
254 {
255 	iface->open = ifile_open;
256 	iface->get_file = ifile_get_file;
257 }
258 
259 ANJUTA_PLUGIN_BEGIN (NPWPlugin, npw_plugin);
260 ANJUTA_PLUGIN_ADD_INTERFACE (ifile, IANJUTA_TYPE_FILE);
261 ANJUTA_PLUGIN_ADD_INTERFACE (iwizard, IANJUTA_TYPE_WIZARD);
262 ANJUTA_PLUGIN_END;
263 
264 ANJUTA_SIMPLE_PLUGIN (NPWPlugin, npw_plugin);
265 
266 /* Control access to anjuta message view to avoid a closed view
267  *---------------------------------------------------------------------------*/
268 
269 static void
on_message_buffer_flush(IAnjutaMessageView * view,const gchar * line,NPWPlugin * plugin)270 on_message_buffer_flush (IAnjutaMessageView *view, const gchar *line,
271 						 NPWPlugin *plugin)
272 {
273 	npw_plugin_print_view (plugin, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line, "");
274 }
275 
276 IAnjutaMessageView*
npw_plugin_create_view(NPWPlugin * plugin)277 npw_plugin_create_view (NPWPlugin* plugin)
278 {
279 	if (plugin->view == NULL)
280 	{
281 		IAnjutaMessageManager* man;
282 
283 		man = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
284 										  IAnjutaMessageManager, NULL);
285 		plugin->view =
286 			ianjuta_message_manager_add_view (man, _("New Project Assistant"),
287 											  ICON_FILE, NULL);
288 		if (plugin->view != NULL)
289 		{
290 			g_signal_connect (G_OBJECT (plugin->view), "buffer_flushed",
291 							  G_CALLBACK (on_message_buffer_flush), plugin);
292 			g_object_add_weak_pointer (G_OBJECT (plugin->view),
293 									   (gpointer *)(gpointer)&plugin->view);
294 		}
295 	}
296 	else
297 	{
298 		ianjuta_message_view_clear (plugin->view, NULL);
299 	}
300 
301 	return plugin->view;
302 }
303 
304 void
npw_plugin_append_view(NPWPlugin * plugin,const gchar * text)305 npw_plugin_append_view (NPWPlugin* plugin, const gchar* text)
306 {
307 	if (plugin->view)
308 	{
309 		ianjuta_message_view_buffer_append (plugin->view, text, NULL);
310 	}
311 }
312 
313 void
npw_plugin_print_view(NPWPlugin * plugin,IAnjutaMessageViewType type,const gchar * summary,const gchar * details)314 npw_plugin_print_view (NPWPlugin* plugin, IAnjutaMessageViewType type,
315 					   const gchar* summary, const gchar* details)
316 {
317 	if (plugin->view)
318 	{
319 		ianjuta_message_view_append (plugin->view, type, summary, details, NULL);
320 	}
321 }
322