~ubuntu-branches/ubuntu/trusty/anjuta/trusty

« back to all changes in this revision

Viewing changes to plugins/code-analyzer/plugin.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-08-17 23:48:26 UTC
  • mfrom: (1.1.50)
  • Revision ID: package-import@ubuntu.com-20120817234826-fvk3rfp6nmfaqi9p
Tags: 2:3.5.5-0ubuntu1
* New upstream release.
* debian/control.in:
  - Bump vala dependency to 0.18 series
  - Drop graphviz from build-depends
* debian/watch: Watch for unstable releases

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2
 
/*
3
 
 * plugin.c
4
 
 * Copyright (C) Johannes Schmid 2010 <jhs@gnome.org>
5
 
 * 
6
 
 * code-analyzer is free software: you can redistribute it and/or modify it
7
 
 * under the terms of the GNU General Public License as published by the
8
 
 * Free Software Foundation, either version 3 of the License, or
9
 
 * (at your option) any later version.
10
 
 * 
11
 
 * code-analyzer is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
 * See the GNU General Public License for more details.
15
 
 * 
16
 
 * You should have received a copy of the GNU General Public License along
17
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
#include <libanjuta/anjuta-shell.h>
22
 
#include <libanjuta/anjuta-debug.h>
23
 
#include <libanjuta/interfaces/ianjuta-environment.h>
24
 
#include <libanjuta/interfaces/ianjuta-preferences.h>
25
 
#include <glib/gi18n.h>
26
 
 
27
 
#include "plugin.h"
28
 
 
29
 
#define ICON_FILE "code-analyzer.png"
30
 
 
31
 
#define BUILDER_FILE PACKAGE_DATA_DIR"/glade/code-analyzer.ui"
32
 
#define CLANG_PREFS_ROOT "clang_preferences"
33
 
#define PREF_SCHEMA "org.gnome.anjuta.code-analyzer"
34
 
 
35
 
#define PREF_ENABLED "clang-enable"
36
 
#define PREF_CC_PATH "clang-cc-path"
37
 
#define PREF_CXX_PATH "clang-cxx-path"
38
 
 
39
 
static gpointer parent_class;
40
 
 
41
 
static gboolean
42
 
code_analyzer_activate (AnjutaPlugin *plugin)
43
 
{
44
 
        CodeAnalyzerPlugin *code_analyzer;
45
 
        
46
 
        DEBUG_PRINT ("%s", "CodeAnalyzerPlugin: Activating CodeAnalyzerPlugin plugin ...");
47
 
        code_analyzer = (CodeAnalyzerPlugin*) plugin;
48
 
 
49
 
        return TRUE;
50
 
}
51
 
 
52
 
static gboolean
53
 
code_analyzer_deactivate (AnjutaPlugin *plugin)
54
 
{
55
 
 
56
 
        DEBUG_PRINT ("%s", "CodeAnalyzerPlugin: Dectivating CodeAnalyzerPlugin plugin ...");
57
 
        
58
 
        return TRUE;
59
 
}
60
 
 
61
 
static void
62
 
code_analyzer_finalize (GObject *obj)
63
 
{
64
 
        /* Finalization codes here */
65
 
        G_OBJECT_CLASS (parent_class)->finalize (obj);
66
 
}
67
 
 
68
 
static void
69
 
code_analyzer_dispose (GObject *obj)
70
 
{
71
 
        /* Disposition codes */
72
 
        G_OBJECT_CLASS (parent_class)->dispose (obj);
73
 
}
74
 
 
75
 
static void
76
 
code_analyzer_instance_init (GObject *obj)
77
 
{
78
 
        CodeAnalyzerPlugin *plugin = (CodeAnalyzerPlugin*)obj;
79
 
        plugin->settings = g_settings_new (PREF_SCHEMA);
80
 
}
81
 
 
82
 
static void
83
 
code_analyzer_class_init (GObjectClass *klass) 
84
 
{
85
 
        AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
86
 
 
87
 
        parent_class = g_type_class_peek_parent (klass);
88
 
 
89
 
        plugin_class->activate = code_analyzer_activate;
90
 
        plugin_class->deactivate = code_analyzer_deactivate;
91
 
        klass->finalize = code_analyzer_finalize;
92
 
        klass->dispose = code_analyzer_dispose;
93
 
}
94
 
 
95
 
static gchar*
96
 
code_analyzer_get_cc_path (CodeAnalyzerPlugin* ca_plugin)
97
 
{
98
 
        gchar* path = g_settings_get_string (ca_plugin->settings,
99
 
                                             PREF_CC_PATH);
100
 
        if (!g_file_test (path, G_FILE_TEST_IS_EXECUTABLE))
101
 
        {
102
 
                g_free (path);
103
 
                return NULL;
104
 
        }
105
 
        return path;
106
 
}
107
 
 
108
 
static gchar*
109
 
code_analyzer_get_cxx_path (CodeAnalyzerPlugin* ca_plugin)
110
 
{
111
 
        gchar* path = g_settings_get_string (ca_plugin->settings,
112
 
                                             PREF_CXX_PATH);
113
 
        if (!g_file_test (path, G_FILE_TEST_IS_EXECUTABLE))
114
 
        {
115
 
                g_free (path);
116
 
                return NULL;
117
 
        }
118
 
        return path;
119
 
}
120
 
 
121
 
 
122
 
/* IAnjutaEnvironment implementation */
123
 
static gboolean 
124
 
ienvironment_override (IAnjutaEnvironment* env_iface,
125
 
                       gchar **dirp, gchar ***argvp, gchar ***envp,
126
 
                       GError** error)
127
 
{
128
 
        CodeAnalyzerPlugin* ca_plugin = (CodeAnalyzerPlugin*) (env_iface);
129
 
        gchar* command = *argvp[0];
130
 
 
131
 
        if (!g_settings_get_boolean (ca_plugin->settings,
132
 
                                     PREF_ENABLED))
133
 
                return TRUE;
134
 
 
135
 
        
136
 
 
137
 
        /* Check if this is a command we are interested in */                        
138
 
        if (strcmp (command, "autogen.sh") ||
139
 
            strcmp (command, "configure") ||
140
 
            strcmp (command, "make"))
141
 
        {
142
 
                gchar** new_env = *envp;
143
 
                gchar** env;
144
 
                gchar* cc = code_analyzer_get_cc_path (ca_plugin);
145
 
                gchar* cxx = code_analyzer_get_cxx_path (ca_plugin);
146
 
                gboolean found_cc = FALSE;
147
 
                gboolean found_cxx = FALSE;
148
 
                /* NULL termination */
149
 
                gsize size = 1;
150
 
 
151
 
                /* Check if paths are correct */
152
 
                if (!cc || !cxx)
153
 
                {
154
 
                        if (error)
155
 
                                *error = g_error_new (ianjuta_environment_error_quark (), 
156
 
                                                      IANJUTA_ENVIRONMENT_CONFIG, "%s",
157
 
                                                      _("Couldn't find clang analyzer, please check "
158
 
                                                        "if it is installed and if the paths are configured "
159
 
                                                        "correctly in the preferences"));
160
 
                        g_free (cc);
161
 
                        g_free (cxx);
162
 
                        return FALSE;
163
 
                }
164
 
 
165
 
                for (env = new_env; env && *env != NULL; env++)
166
 
                {
167
 
                        if (g_str_has_prefix (*env, "CC="))
168
 
                        {
169
 
                                g_free (*env);
170
 
                                *env = g_strdup_printf("CC=%s", cc);
171
 
                                found_cc = TRUE;
172
 
                        }
173
 
                        else if (g_str_has_prefix (*env, "CXX="))
174
 
                        {
175
 
                                g_free (*env);
176
 
                                *env = g_strdup_printf("CXX=%s", cxx);
177
 
                                found_cxx = TRUE;
178
 
                        }
179
 
                        size++;
180
 
                }
181
 
                if (!found_cc)
182
 
                {
183
 
                        new_env = g_realloc (new_env, sizeof (gchar**) * (size + 1));
184
 
                        new_env[size - 1] = g_strdup_printf("CC=%s", cc);
185
 
                        new_env[size] = NULL;
186
 
                        size++;
187
 
                }
188
 
                if (!found_cxx)
189
 
                {
190
 
                        new_env = g_realloc (new_env, sizeof (gchar**) * (size + 1));
191
 
                        new_env[size - 1] = g_strdup_printf("CXX=%s", cxx);
192
 
                        new_env[size] = NULL;
193
 
                }
194
 
                *envp = new_env;
195
 
        }
196
 
        return TRUE;
197
 
}
198
 
 
199
 
static gchar* 
200
 
ienvironment_get_real_directory (IAnjutaEnvironment* env_iface, gchar *dir,
201
 
                                 GError** error)
202
 
{
203
 
        return dir;
204
 
}
205
 
 
206
 
static void
207
 
ienvironment_iface_init (IAnjutaEnvironmentIface* iface)
208
 
{
209
 
        iface->override = ienvironment_override;
210
 
        iface->get_real_directory = ienvironment_get_real_directory;
211
 
}
212
 
 
213
 
/* IAnjutaPreferences implementation
214
 
 *---------------------------------------------------------------------------*/
215
 
 
216
 
static void
217
 
ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
218
 
{
219
 
        GtkBuilder *bxml;
220
 
        CodeAnalyzerPlugin* ca_plugin = (CodeAnalyzerPlugin*) (ipref);
221
 
        /* Create the preferences page */
222
 
        bxml =  anjuta_util_builder_new (BUILDER_FILE, NULL);
223
 
        if (!bxml) return;
224
 
                
225
 
        anjuta_preferences_add_from_builder (prefs, bxml, ca_plugin->settings, 
226
 
                                             CLANG_PREFS_ROOT, _("CLang Analyzer"),  ICON_FILE);
227
 
        
228
 
        g_object_unref (bxml);
229
 
}
230
 
 
231
 
static void
232
 
ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
233
 
{
234
 
        anjuta_preferences_remove_page(prefs, _("CLang Analyzer"));
235
 
}
236
 
 
237
 
static void
238
 
ipreferences_iface_init(IAnjutaPreferencesIface* iface)
239
 
{
240
 
        iface->merge = ipreferences_merge;
241
 
        iface->unmerge = ipreferences_unmerge;  
242
 
}
243
 
 
244
 
 
245
 
ANJUTA_PLUGIN_BEGIN (CodeAnalyzerPlugin, code_analyzer);
246
 
ANJUTA_PLUGIN_ADD_INTERFACE(ienvironment, IANJUTA_TYPE_ENVIRONMENT);
247
 
ANJUTA_PLUGIN_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
248
 
ANJUTA_PLUGIN_END;
249
 
 
250
 
ANJUTA_SIMPLE_PLUGIN (CodeAnalyzerPlugin, code_analyzer);