Added byline
[anjuta.git] / plugins / project-wizard / plugin.c
blob42bd6c5e9fb775d5eafd1c0eeca9103cc50bc2f5
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2000 Naba Kumar
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
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
22 * Plugins functions
24 *---------------------------------------------------------------------------*/
26 #include <config.h>
28 #include "plugin.h"
30 #include "druid.h"
31 #include "tar.h"
33 #include <libanjuta/anjuta-debug.h>
34 #include <libanjuta/interfaces/ianjuta-wizard.h>
35 #include <libanjuta/interfaces/ianjuta-file.h>
38 /* Private functions
39 *---------------------------------------------------------------------------*/
41 static void
42 npw_open_project_template (GFile *destination, GFile *tarfile, gpointer data, GError *error)
44 NPWPlugin *plugin = (NPWPlugin *)data;
46 if (error != NULL)
48 gchar *tarname = g_file_get_path (tarfile);
50 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),_("Unable to extract project template %s: %s"), tarname, error->message);
52 else
54 /* Show the project wizard dialog, loading only the new projects */
55 npw_plugin_show_wizard (plugin, destination);
59 static gboolean
60 npw_install_project_template_with_callback (NPWPlugin *plugin, GFile *file, NPWTarCompleteFunc callback, GError **error)
62 GFileInputStream *stream;
63 gchar *name;
64 gchar *ext;
65 gchar *path;
66 GFile *dest;
67 gboolean ok;
68 GError *err = NULL;
70 /* Check if tarfile exist */
71 stream = g_file_read (file, NULL, error);
72 if (stream == NULL)
74 return FALSE;
76 g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
78 /* Get name without extension */
79 name = g_file_get_basename (file);
80 ext = strchr (name, '.');
81 if (ext != NULL) *ext = '\0';
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)
91 if (err->code == G_IO_ERROR_EXISTS)
93 /* Allow to overwrite directories */
94 ok = TRUE;
95 g_error_free (err);
97 else
99 g_object_unref (dest);
101 return FALSE;
105 ok = npw_tar_extract (dest, file, callback, plugin, error);
106 g_object_unref (dest);
108 return ok;
111 /* Public functions
112 *---------------------------------------------------------------------------*/
114 /* Display the project wizard selection dialog using only templates in the
115 * specified directory if non NULL */
116 gboolean
117 npw_plugin_show_wizard (NPWPlugin *plugin, GFile *project)
119 if (plugin->install != NULL)
121 /* New project wizard is busy copying project file */
123 else if (plugin->druid == NULL)
125 /* Create a new project wizard druid */
126 npw_druid_new (plugin, project);
129 if (plugin->druid != NULL)
131 /* New project wizard druid is waiting for user inputs */
132 npw_druid_show (plugin->druid);
136 return TRUE;
139 /*---------------------------------------------------------------------------*/
141 /* Used in dispose */
142 static gpointer parent_class;
144 static void
145 npw_plugin_instance_init (GObject *obj)
147 NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);
149 plugin->druid = NULL;
150 plugin->install = NULL;
151 plugin->view = NULL;
154 /* dispose is used to unref object created with instance_init */
156 static void
157 npw_plugin_dispose (GObject *obj)
159 NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (obj);
161 /* Warning this function could be called several times */
162 if (plugin->view != NULL)
164 g_object_remove_weak_pointer (G_OBJECT (plugin->view),
165 (gpointer*)(gpointer)&plugin->view);
166 plugin->view = NULL;
169 G_OBJECT_CLASS (parent_class)->dispose (obj);
172 static void
173 npw_plugin_finalize (GObject *obj)
175 G_OBJECT_CLASS (parent_class)->finalize (obj);
178 /* finalize used to free object created with instance init is not used */
180 static gboolean
181 npw_plugin_activate (AnjutaPlugin *plugin)
183 DEBUG_PRINT ("%s", "Project Wizard Plugin: Activating project wizard plugin...");
184 return TRUE;
187 static gboolean
188 npw_plugin_deactivate (AnjutaPlugin *plugin)
190 DEBUG_PRINT ("%s", "Project Wizard Plugin: Deactivating project wizard plugin...");
191 return TRUE;
194 static void
195 npw_plugin_class_init (GObjectClass *klass)
197 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
199 parent_class = g_type_class_peek_parent (klass);
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;
207 /* IAnjutaWizard implementation
208 *---------------------------------------------------------------------------*/
210 static void
211 iwizard_activate (IAnjutaWizard *wiz, GError **err)
213 npw_plugin_show_wizard (ANJUTA_PLUGIN_NPW (wiz), NULL);
216 static void
217 iwizard_iface_init (IAnjutaWizardIface *iface)
219 iface->activate = iwizard_activate;
222 /* IAnjutaFile implementation
223 *---------------------------------------------------------------------------*/
225 static void
226 ifile_open (IAnjutaFile *ifile, GFile* file, GError **error)
228 NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (ifile);
229 GFileInfo *info;
231 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL, NULL);
232 if (info != NULL)
234 if (strcmp(g_file_info_get_content_type (info), "application/x-anjuta-project-template") == 0)
236 npw_plugin_show_wizard (plugin, file);
238 else
240 npw_install_project_template_with_callback (plugin, file, npw_open_project_template, error);
242 g_object_unref (info);
246 static GFile*
247 ifile_get_file (IAnjutaFile* ifile, GError** error)
249 return NULL;
252 static void
253 ifile_iface_init(IAnjutaFileIface *iface)
255 iface->open = ifile_open;
256 iface->get_file = ifile_get_file;
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;
264 ANJUTA_SIMPLE_PLUGIN (NPWPlugin, npw_plugin);
266 /* Control access to anjuta message view to avoid a closed view
267 *---------------------------------------------------------------------------*/
269 static void
270 on_message_buffer_flush (IAnjutaMessageView *view, const gchar *line,
271 NPWPlugin *plugin)
273 npw_plugin_print_view (plugin, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line, "");
276 IAnjutaMessageView*
277 npw_plugin_create_view (NPWPlugin* plugin)
279 if (plugin->view == NULL)
281 IAnjutaMessageManager* man;
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)
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);
296 else
298 ianjuta_message_view_clear (plugin->view, NULL);
301 return plugin->view;
304 void
305 npw_plugin_append_view (NPWPlugin* plugin, const gchar* text)
307 if (plugin->view)
309 ianjuta_message_view_buffer_append (plugin->view, text, NULL);
313 void
314 npw_plugin_print_view (NPWPlugin* plugin, IAnjutaMessageViewType type,
315 const gchar* summary, const gchar* details)
317 if (plugin->view)
319 ianjuta_message_view_append (plugin->view, type, summary, details, NULL);