Integrate adding files with the file manager
[anjuta-git-plugin.git] / plugins / scratchbox / plugin.c
blob1717a908a58b80b1808da3ae485c96eee6ecf06c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2008 Sébastien Granjoux
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 <libanjuta/anjuta-debug.h>
31 #include <libanjuta/interfaces/ianjuta-environment.h>
32 #include <libanjuta/interfaces/ianjuta-preferences.h>
34 /* Constantes
35 *---------------------------------------------------------------------------*/
37 #define ICON_FILE "anjuta-scratchbox-48.png"
38 #define GLADE_FILE PACKAGE_DATA_DIR"/glade/anjuta-scratchbox.glade"
40 #define SB_ENTRY "preferences_folder:text:/scratchbox:0:build.scratchbox.path"
42 #define PREF_SB_PATH "build.scratchbox.path"
44 /* Type defintions
45 *---------------------------------------------------------------------------*/
47 struct _ScratchboxPluginClass
49 AnjutaPluginClass parent_class;
52 struct _ScratchboxPlugin
54 AnjutaPlugin parent;
56 gchar *user_dir;
60 /* Callback for saving session
61 *---------------------------------------------------------------------------*/
63 static void
64 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, ScratchboxPlugin *self)
68 static void on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, ScratchboxPlugin *self)
72 /* Callbacks
73 *---------------------------------------------------------------------------*/
75 /* Actions table
76 *---------------------------------------------------------------------------*/
78 /* AnjutaPlugin functions
79 *---------------------------------------------------------------------------*/
81 static gboolean
82 scratchbox_plugin_activate (AnjutaPlugin *plugin)
84 ScratchboxPlugin *self = ANJUTA_PLUGIN_SCRATCHBOX (plugin);
86 DEBUG_PRINT ("Scratchbox 1 Plugin: Activating plugin...");
88 /* Connect to session signal */
89 g_signal_connect (plugin->shell, "save-session",
90 G_CALLBACK (on_session_save), self);
91 g_signal_connect (plugin->shell, "load-session",
92 G_CALLBACK (on_session_load), self);
94 return TRUE;
97 static gboolean
98 scratchbox_plugin_deactivate (AnjutaPlugin *plugin)
100 ScratchboxPlugin *self = ANJUTA_PLUGIN_SCRATCHBOX (plugin);
102 DEBUG_PRINT ("Scratchbox 1 Plugin: Deactivating plugin...");
104 g_signal_handlers_disconnect_by_func (plugin->shell, G_CALLBACK (on_session_save), self);
105 g_signal_handlers_disconnect_by_func (plugin->shell, G_CALLBACK (on_session_load), self);
107 return TRUE;
110 /* IAnjutaEnvironment implementation
111 *---------------------------------------------------------------------------*/
113 static gboolean
114 ienvironment_override (IAnjutaEnvironment* environment, gchar **dir, gchar ***argvp, gchar ***envp, GError** err)
116 ScratchboxPlugin *plugin = ANJUTA_PLUGIN_SCRATCHBOX (environment);
117 AnjutaPreferences* prefs;
118 gchar* sb_dir;
119 gsize len;
121 prefs = anjuta_shell_get_preferences (ANJUTA_PLUGIN (plugin)->shell, NULL);
122 sb_dir = anjuta_preferences_get(prefs, PREF_SB_PATH);
124 if (plugin->user_dir) g_free (plugin->user_dir);
125 plugin->user_dir = g_strconcat (sb_dir, G_DIR_SEPARATOR_S,
126 "users", G_DIR_SEPARATOR_S,
127 g_get_user_name(), NULL);
129 len = strlen (plugin->user_dir);
130 if (strncmp (*dir, plugin->user_dir, len) == 0)
132 /* Build in scratchbox environment */
133 gchar **new_argv;
134 gsize len_argv = g_strv_length (*argvp);
136 /* Add scratchbox login */
137 new_argv = g_new (gchar*, len_argv + 3);
138 memcpy (new_argv + 2, *argvp, sizeof(gchar *) * (len_argv + 1));
139 new_argv[0] = g_strconcat (sb_dir, G_DIR_SEPARATOR_S, "login", NULL);
140 new_argv[1] = g_strconcat ("-d ", (*dir) + len, NULL);
142 g_free (*argvp);
143 *argvp = new_argv;
145 g_free (sb_dir);
147 return TRUE;
150 static gchar*
151 ienvironment_get_real_directory (IAnjutaEnvironment* environment, gchar *dir, GError** err)
153 ScratchboxPlugin *plugin = ANJUTA_PLUGIN_SCRATCHBOX (environment);
155 if (plugin->user_dir)
157 gchar *real_dir;
159 real_dir = g_strconcat(plugin->user_dir, dir, NULL);
160 g_free (dir);
162 return real_dir;
164 else
166 return dir;
170 static void
171 ienvironment_iface_init(IAnjutaEnvironmentIface* iface)
173 iface->override = ienvironment_override;
174 iface->get_real_directory = ienvironment_get_real_directory;
177 /* IAnjutaPreferences implementation
178 *---------------------------------------------------------------------------*/
180 static GladeXML *gxml;
182 static void
183 ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
185 GtkWidget *sb_entry;
187 /* Create the preferences page */
188 gxml = glade_xml_new (GLADE_FILE, "preferences_dialog_scratchbox", NULL);
189 sb_entry = glade_xml_get_widget(gxml, SB_ENTRY);
191 anjuta_preferences_add_page (prefs, gxml, "Scratchbox", _("Scratchbox"), ICON_FILE);
194 static void
195 ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
197 GtkWidget *sb_entry;
199 sb_entry = glade_xml_get_widget(gxml, SB_ENTRY);
201 anjuta_preferences_remove_page(prefs, _("Scratchbox"));
203 g_object_unref (gxml);
206 static void
207 ipreferences_iface_init(IAnjutaPreferencesIface* iface)
209 iface->merge = ipreferences_merge;
210 iface->unmerge = ipreferences_unmerge;
213 /* GObject functions
214 *---------------------------------------------------------------------------*/
216 /* Used in dispose and finalize */
217 static gpointer parent_class;
219 static void
220 scratchbox_plugin_instance_init (GObject *obj)
222 ScratchboxPlugin *plugin = ANJUTA_PLUGIN_SCRATCHBOX (obj);
224 plugin->user_dir = NULL;
227 /* dispose is used to unref object created with instance_init */
229 static void
230 scratchbox_plugin_dispose (GObject *obj)
232 ScratchboxPlugin *plugin = ANJUTA_PLUGIN_SCRATCHBOX (obj);
234 /* Warning this function could be called several times */
236 if (plugin->user_dir)
238 g_free (plugin->user_dir);
239 plugin->user_dir = NULL;
242 G_OBJECT_CLASS (parent_class)->dispose (obj);
245 static void
246 scratchbox_plugin_finalize (GObject *obj)
248 /*ScratchboxPlugin *self = ANJUTA_PLUGIN_SCRATCHBOX (obj);*/
250 G_OBJECT_CLASS (parent_class)->finalize (obj);
253 /* finalize used to free object created with instance init is not used */
255 static void
256 scratchbox_plugin_class_init (GObjectClass *klass)
258 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
260 parent_class = g_type_class_peek_parent (klass);
262 plugin_class->activate = scratchbox_plugin_activate;
263 plugin_class->deactivate = scratchbox_plugin_deactivate;
264 klass->dispose = scratchbox_plugin_dispose;
265 klass->finalize = scratchbox_plugin_finalize;
268 /* AnjutaPlugin declaration
269 *---------------------------------------------------------------------------*/
271 ANJUTA_PLUGIN_BEGIN (ScratchboxPlugin, scratchbox_plugin);
272 ANJUTA_PLUGIN_ADD_INTERFACE (ienvironment, IANJUTA_TYPE_ENVIRONMENT);
273 ANJUTA_PLUGIN_ADD_INTERFACE (ipreferences, IANJUTA_TYPE_PREFERENCES);
274 ANJUTA_PLUGIN_END;
276 ANJUTA_SIMPLE_PLUGIN (ScratchboxPlugin, scratchbox_plugin);
278 /* Public functions
279 *---------------------------------------------------------------------------*/