1 /* -*- Mode: C; indent-spaces-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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
25 #include <libanjuta/anjuta-debug.h>
26 #include <libanjuta/interfaces/ianjuta-environment.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) \
44 return g_quark_from_string (#QN); \
48 G_DEFINE_QUARK(JHBUILD_PLUGIN_ERROR
, jhbuild_plugin_error
);
51 jhbuild_plugin_environment_get_real_directory(IAnjutaEnvironment
*obj
,
59 jhbuild_plugin_environment_override(IAnjutaEnvironment
* environment
,
65 JHBuildPlugin
* self
= ANJUTA_PLUGIN_JHBUILD(environment
);
67 gboolean add_prefix
= FALSE
;
71 if (g_str_has_suffix (*argvp
[0], "configure") || g_str_has_suffix (*argvp
[0], "autogen.sh"))
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"))
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
);
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
;
101 new_argv
[2 + argvp_length
] = NULL
;
110 run_jhbuild_env (GError
** error
)
112 char* argv
[] = {"jhbuild", "run", "env", NULL
};
114 char* standard_output
;
115 char* standard_error
;
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\""));
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
);
133 g_free(standard_error
);
134 return standard_output
;
138 jhbuild_plugin_get_jhbuild_info(JHBuildPlugin
* self
, GError
** error
)
141 char** env_variables
;
144 if (!(output
= run_jhbuild_env(error
)))
147 env_variables
= g_strsplit(output
, "\n", 0);
150 /* Scan for JHBUILD_PREFIX and JHBUILD_LIBDIR */
151 for (variable
= env_variables
; *variable
; ++variable
)
153 if (g_str_has_prefix (*variable
, "JHBUILD_PREFIX="))
157 value
= *variable
+ strlen("JHBUILD_PREFIX=");
158 self
->prefix
= g_strdup(value
);
160 else if (g_str_has_prefix (*variable
, "JHBUILD_LIBDIR="))
164 value
= *variable
+ strlen("JHBUILD_LIBDIR=");
165 self
->libdir
= g_strdup(value
);
168 g_strfreev(env_variables
);
172 g_set_error_literal (error
, ANJUTA_SHELL_ERROR
, 0,_( "Could not find the JHBuild install prefix."));
173 g_strfreev(env_variables
);
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."));
189 jhbuild_plugin_activate(AnjutaPlugin
* plugin
)
191 JHBuildPlugin
*self
= ANJUTA_PLUGIN_JHBUILD (plugin
);
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
);
206 jhbuild_plugin_deactivate (AnjutaPlugin
*plugin
)
212 *---------------------------------------------------------------------------*/
214 /* Used in dispose and finalize */
215 static gpointer parent_class
;
218 jhbuild_plugin_instance_init (GObject
*obj
)
220 JHBuildPlugin
*self
= ANJUTA_PLUGIN_JHBUILD (obj
);
222 self
->shell
= anjuta_plugin_get_shell (ANJUTA_PLUGIN(self
));
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
);
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
;
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
);
260 ANJUTA_SIMPLE_PLUGIN (JHBuildPlugin
, jhbuild_plugin
);