Updated Makefile.am files after make -f git.mk
[anjuta.git] / plugins / jhbuild / plugin.c
blob73bb9079cfabae969561a281bf6a2350ed954403
1 /* -*- Mode: C; indent-spaces-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2012 Carl-Anton Ingmarsson
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
21 #include <config.h>
23 #include "plugin.h"
25 #include <libanjuta/anjuta-debug.h>
26 #include <libanjuta/interfaces/ianjuta-environment.h>
28 #include <sys/wait.h>
30 struct _JHBuildPluginClass
32 AnjutaPluginClass parent_class;
35 #define JHBUILD_PLUGIN_ERROR (jhbuild_plugin_error_quark())
36 static GQuark jhbuild_plugin_error_quark(void);
38 /* G_DEFINE_QUARK is defined in GLib 2.34 provide a fallback for Glib 2.32 */
39 #ifndef G_DEFINE_QUARK
40 #define G_DEFINE_QUARK(QN, q_n) \
41 GQuark \
42 q_n##_quark (void) \
43 { \
44 return g_quark_from_string (#QN); \
46 #endif
48 G_DEFINE_QUARK(JHBUILD_PLUGIN_ERROR, jhbuild_plugin_error);
50 static char *
51 jhbuild_plugin_environment_get_real_directory(IAnjutaEnvironment *obj,
52 gchar *dir,
53 GError **err)
55 return dir;
58 static gboolean
59 jhbuild_plugin_environment_override(IAnjutaEnvironment* environment,
60 char** dirp,
61 char*** argvp,
62 char*** envp,
63 GError** error)
65 JHBuildPlugin* self = ANJUTA_PLUGIN_JHBUILD(environment);
67 gboolean add_prefix = FALSE;
68 guint argvp_length;
69 char** new_argv;
71 if (g_str_has_suffix (*argvp[0], "configure") || g_str_has_suffix (*argvp[0], "autogen.sh"))
73 char** argv_iter;
75 add_prefix = TRUE;
76 for (argv_iter = *argvp; *argv_iter; argv_iter++)
78 if (g_str_has_prefix (*argv_iter, "--prefix") ||
79 g_str_has_prefix (*argv_iter, "--libdir"))
81 add_prefix = FALSE;
82 break;
87 argvp_length = g_strv_length (*argvp);
88 new_argv = g_new(char*, argvp_length + (add_prefix ? 5 : 3));
89 new_argv[0] = g_strdup("jhbuild");
90 new_argv[1] = g_strdup("run");
92 memcpy(new_argv + 2, *argvp, sizeof(char*) * argvp_length);
94 if (add_prefix)
96 new_argv[2 + argvp_length] = g_strdup_printf("--prefix=%s", self->prefix);
97 new_argv[2 + argvp_length + 1] = g_strdup_printf("--libdir=%s", self->libdir);
98 new_argv[2 + argvp_length + 2] = NULL;
100 else
101 new_argv[2 + argvp_length] = NULL;
103 g_free(*argvp);
104 *argvp = new_argv;
106 return TRUE;
109 static char*
110 run_jhbuild_env (GError** error)
112 char* argv[] = {"jhbuild", "run", "env", NULL};
113 GError* err = NULL;
114 char* standard_output;
115 char* standard_error;
116 int exit_status;
118 if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
119 &standard_output, &standard_error, &exit_status, &err))
121 g_propagate_prefixed_error (error, err, _("Failed to run \"jhbuild run\""));
122 return NULL;
125 if (WIFEXITED(exit_status) && WEXITSTATUS(exit_status) != 0)
127 g_set_error(error, JHBUILD_PLUGIN_ERROR, 0, _("Failed to run \"jhbuild run\" (%s)"), standard_error);
128 g_free(standard_error);
129 g_free(standard_output);
130 return NULL;
133 g_free(standard_error);
134 return standard_output;
137 static gboolean
138 jhbuild_plugin_get_jhbuild_info(JHBuildPlugin* self, GError** error)
140 char* output;
141 char** env_variables;
142 char** variable;
144 if (!(output = run_jhbuild_env(error)))
145 return FALSE;
147 env_variables = g_strsplit(output, "\n", 0);
148 g_free(output);
150 /* Scan for JHBUILD_PREFIX and JHBUILD_LIBDIR */
151 for (variable = env_variables; *variable; ++variable)
153 if (g_str_has_prefix (*variable, "JHBUILD_PREFIX="))
155 const char* value;
157 value = *variable + strlen("JHBUILD_PREFIX=");
158 self->prefix = g_strdup(value);
160 else if (g_str_has_prefix (*variable, "JHBUILD_LIBDIR="))
162 const char* value;
164 value = *variable + strlen("JHBUILD_LIBDIR=");
165 self->libdir = g_strdup(value);
168 g_strfreev(env_variables);
170 if (!self->prefix)
172 g_set_error_literal (error, ANJUTA_SHELL_ERROR, 0,_( "Could not find the JHBuild install prefix."));
173 g_strfreev(env_variables);
174 return FALSE;
177 if (!self->libdir)
179 g_set_error_literal (error, JHBUILD_PLUGIN_ERROR, 0,
180 _("Could not find the JHBuild library directory. "
181 "You need JHBuild from 2012-11-06 or later."));
182 return FALSE;
185 return TRUE;
188 static gboolean
189 jhbuild_plugin_activate(AnjutaPlugin* plugin)
191 JHBuildPlugin *self = ANJUTA_PLUGIN_JHBUILD (plugin);
193 GError *err = NULL;
195 if (!jhbuild_plugin_get_jhbuild_info(self, &err))
197 anjuta_util_dialog_error(GTK_WINDOW(self->shell),
198 _("Failed to activate the JHBuild Plugin: %s"), err->message);
199 g_error_free (err);
200 return FALSE;
202 return TRUE;
205 static gboolean
206 jhbuild_plugin_deactivate (AnjutaPlugin *plugin)
208 return TRUE;
211 /* GObject functions
212 *---------------------------------------------------------------------------*/
214 /* Used in dispose and finalize */
215 static gpointer parent_class;
217 static void
218 jhbuild_plugin_instance_init (GObject *obj)
220 JHBuildPlugin *self = ANJUTA_PLUGIN_JHBUILD (obj);
222 self->shell = anjuta_plugin_get_shell (ANJUTA_PLUGIN(self));
225 static void
226 jhbuild_plugin_finalize (GObject *obj)
228 JHBuildPlugin *self = ANJUTA_PLUGIN_JHBUILD (obj);
230 g_free(self->prefix);
231 g_free(self->libdir);
234 G_OBJECT_CLASS (parent_class)->finalize (obj);
237 static void
238 jhbuild_plugin_class_init (GObjectClass *klass)
240 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
242 parent_class = g_type_class_peek_parent (klass);
244 plugin_class->activate = jhbuild_plugin_activate;
245 plugin_class->deactivate = jhbuild_plugin_deactivate;
246 klass->finalize = jhbuild_plugin_finalize;
249 static void
250 ienvironment_iface_init(IAnjutaEnvironmentIface *iface)
252 iface->override = jhbuild_plugin_environment_override;
253 iface->get_real_directory = jhbuild_plugin_environment_get_real_directory;
256 ANJUTA_PLUGIN_BEGIN (JHBuildPlugin, jhbuild_plugin);
257 ANJUTA_PLUGIN_ADD_INTERFACE (ienvironment, IANJUTA_TYPE_ENVIRONMENT);
258 ANJUTA_PLUGIN_END;
260 ANJUTA_SIMPLE_PLUGIN (JHBuildPlugin, jhbuild_plugin);