2008-06-16 Abderrahim Kitouni <a.kitouni@gmail.com>
[anjuta-git-plugin.git] / plugins / build / executer.c
blobaf57f074056d060e4c1157c8a66175741c044332
1 /*
2 executer.c
3 Copyright (C) 2000 Kh. Naba Kumar Singh
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <string.h>
27 #include <gnome.h>
29 #include <libanjuta/resources.h>
31 #include "anjuta.h"
32 #include "text_editor.h"
33 #include "utilities.h"
34 #include "executer.h"
36 static const gchar szRunInTerminalItem[] = {"RunInTerminal"};
37 //static const gchar szPgmArgsSection[] = {"PgmArgs"};
39 void
40 executer_execute (Executer * e)
42 gchar *dir, *cmd, *command;
44 /* Doing some checks before actualing starting */
45 if (app->project_dbase->project_is_open) /* Project mode */
47 gint target_type;
48 gchar* prog;
50 prog = NULL;
51 target_type = project_dbase_get_target_type (app->project_dbase);
52 if (target_type >= PROJECT_TARGET_TYPE_END_MARK)
54 anjuta_error (_("The target executable of this Project is unknown"));
55 return;
57 else if ( target_type != PROJECT_TARGET_TYPE_EXECUTABLE)
59 anjuta_warning (_("The target executable of this Project is not executable"));
61 prog = project_dbase_get_source_target (app->project_dbase);
62 if (file_is_executable (prog) == FALSE)
64 anjuta_warning(_("The target executable does not exist for this Project"));
65 g_free (prog);
66 return;
68 g_free (prog);
70 else /* File mode checks */
72 TextEditor *te;
74 te = anjuta_get_current_text_editor ();
75 if (!te)
77 anjuta_warning(_("No file or Project opened."));
78 return;
80 if (te->full_filename == NULL)
82 anjuta_warning(_("No executable for this file."));
83 return;
85 if (anjuta_get_file_property (FILE_PROP_IS_INTERPRETED, te->full_filename, 0) == 0)
87 gchar *prog, *temp;
88 gint s_re, e_re;
89 struct stat s_stat, e_stat;
91 prog = NULL;
92 prog = g_strdup (te->full_filename);
93 temp = get_file_extension (prog);
94 if (temp)
95 *(--temp) = '\0';
96 s_re = stat (te->full_filename, &s_stat);
97 e_re = stat (prog, &e_stat);
98 g_free (prog);
99 if ((e_re != 0) || (s_re != 0))
101 anjuta_warning(_("No executable for this file."));
102 return;
104 else if ((!text_editor_is_saved (te)) || (e_stat.st_mtime < s_stat.st_mtime))
106 anjuta_warning (_("Executable is not up-to-date."));
107 /* But continue execution */
109 } /* else continue execution */
112 if (app->project_dbase->project_is_open) /* Execute project */
114 cmd = command_editor_get_command (app->command_editor,
115 COMMAND_EXECUTE_PROJECT);
116 if (cmd == NULL)
118 anjuta_warning (_("Unable to execute Project. Check Settings->Commands."));
119 return;
121 dir = project_dbase_get_module_dir (app->project_dbase, MODULE_SOURCE);
123 else /* Execute file */
125 TextEditor* te;
127 te = anjuta_get_current_text_editor ();
128 g_return_if_fail (te != NULL);
130 if (te->full_filename == NULL || text_editor_is_saved(te) == FALSE)
132 anjuta_warning (_("This file has not been saved. Save it first."));
133 return;
135 anjuta_set_file_properties (te->full_filename);
136 cmd = command_editor_get_command_file (app->command_editor,
137 COMMAND_EXECUTE_FILE, te->filename);
138 if (cmd == NULL)
140 anjuta_warning (_("Unable to execute file. Check Settings->Commands."));
141 return;
143 dir = extract_directory (te->full_filename);
145 command = g_strconcat ("anjuta_launcher ", cmd, NULL);
146 g_free (cmd);
148 #ifdef DEBUG
149 g_message("Raw Command is: %s", command);
150 #endif
152 /* Get command and execute */
153 if(e->terminal)
155 gchar* escaped_cmd;
156 escaped_cmd = anjuta_util_escape_quotes(command);
157 prop_set_with_key (e->props, "anjuta.current.command", escaped_cmd);
159 #ifdef DEBUG
160 g_message("Escaped Command is: %s", escaped_cmd);
161 #endif
163 cmd = command_editor_get_command (app->command_editor, COMMAND_TERMINAL);
164 g_free(escaped_cmd);
166 else
168 prop_set_with_key (e->props, "anjuta.current.command", command);
169 cmd = g_strdup (command);
172 #ifdef DEBUG
173 g_message("Final Command is: %s", cmd);
174 #endif
176 anjuta_set_execution_dir (dir);
177 if (dir) chdir (dir);
178 gnome_execute_shell (dir, cmd);
179 g_free (dir);
180 g_free (command);
181 g_free (cmd);
184 static void
185 on_executer_dialog_response (GtkDialog *dialog, gint response,
186 gpointer user_data)
188 Executer *e =(Executer*) user_data ;
190 e->is_showing = FALSE;
191 gtk_widget_destroy (GTK_WIDGET (dialog));
192 if (response == GTK_RESPONSE_OK)
194 g_return_if_fail( NULL != user_data );
195 e->m_PgmArgs = update_string_list ( e->m_PgmArgs,
196 gtk_entry_get_text (GTK_ENTRY (e->m_gui.combo_entry1)),
197 COMBO_LIST_LENGTH);
198 executer_execute (e);
202 static void
203 on_executer_entry_changed (GtkEditable * editable, gpointer user_data)
205 Executer *e = user_data;
206 const gchar *options;
207 options = gtk_entry_get_text (GTK_ENTRY (editable));
208 if (options)
209 prop_set_with_key (e->props, EXECUTER_PROGRAM_ARGS_KEY, options);
210 else
211 prop_set_with_key (e->props, EXECUTER_PROGRAM_ARGS_KEY, "");
214 static void
215 on_executer_checkbutton_toggled (GtkToggleButton * togglebutton,
216 gpointer user_data)
218 Executer *e = user_data;
219 e->terminal = gtk_toggle_button_get_active (togglebutton);
222 static GtkWidget *
223 create_executer_dialog (Executer * e)
225 gchar* options;
226 GladeXML *gxml;
228 gxml = glade_xml_new (GLADE_FILE_ANJUTA, "executer_dialog", NULL);
229 glade_xml_signal_autoconnect (gxml);
230 e->m_gui.dialog = glade_xml_get_widget (gxml, "executer_dialog");
231 gtk_widget_hide (e->m_gui.dialog);
232 e->m_gui.combo1 = glade_xml_get_widget (gxml, "executer_combo");
233 e->m_gui.combo_entry1 = glade_xml_get_widget (gxml, "executer_entry");
234 e->m_gui.check_terminal =
235 glade_xml_get_widget (gxml, "executer_run_in_term_check");
236 g_object_unref (gxml);
238 gtk_window_set_transient_for (GTK_WINDOW(e->m_gui.dialog),
239 GTK_WINDOW(app));
241 options = prop_get (e->props, EXECUTER_PROGRAM_ARGS_KEY);
242 if (options)
244 gtk_entry_set_text (GTK_ENTRY (e->m_gui.combo_entry1), options);
245 g_free (options);
247 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (e->m_gui.check_terminal),
248 e->terminal);
250 gtk_window_add_accel_group (GTK_WINDOW (e->m_gui.dialog), app->accel_group);
252 g_signal_connect (G_OBJECT (e->m_gui.combo_entry1), "changed",
253 G_CALLBACK (on_executer_entry_changed), e);
254 g_signal_connect (G_OBJECT (e->m_gui.check_terminal), "toggled",
255 G_CALLBACK (on_executer_checkbutton_toggled), e);
256 g_signal_connect (G_OBJECT (e->m_gui.dialog), "response",
257 G_CALLBACK (on_executer_dialog_response), e);
259 gtk_widget_grab_focus (e->m_gui.combo_entry1);
260 return e->m_gui.dialog;
263 Executer *
264 executer_new (PropsID props)
266 Executer *e = malloc (sizeof (Executer));
267 if (e)
269 e->props = props;
270 e->terminal = TRUE;
271 e->m_PgmArgs = NULL ;
272 e->is_showing = FALSE;
274 return e;
276 void
277 executer_destroy (Executer * e)
279 if (e)
281 int i ;
282 if( e->m_PgmArgs )
284 for (i = 0; i < g_list_length (e->m_PgmArgs); i++)
285 g_free (g_list_nth (e->m_PgmArgs, i)->data);
287 if (e->m_PgmArgs)
288 g_list_free (e->m_PgmArgs);
290 g_free (e);
294 void
295 executer_show (Executer * e)
297 if (!e->is_showing)
299 gtk_widget_show (create_executer_dialog (e));
300 if (e->m_PgmArgs)
301 gtk_combo_set_popdown_strings (GTK_COMBO (e->m_gui.combo1),
302 e->m_PgmArgs );
303 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (e->m_gui.check_terminal), e->terminal);
304 e->is_showing = TRUE;
308 void
309 executer_save_session( Executer *e, ProjectDBase *p )
311 g_return_if_fail( NULL != e );
312 g_return_if_fail( NULL != p );
313 g_return_if_fail( p->project_is_open );
314 /* Save 'run in terminal option' */
315 session_save_bool( p, SECSTR(SECTION_EXECUTER), szRunInTerminalItem, e->terminal );
316 session_save_strings( p, SECSTR(SECTION_EXECUTERARGS), e->m_PgmArgs );
319 void
320 executer_load_session( Executer *e, ProjectDBase *p )
322 g_return_if_fail( NULL != e );
323 g_return_if_fail( NULL != p );
324 g_return_if_fail( p->project_is_open );
326 e->m_PgmArgs = session_load_strings( p, SECSTR(SECTION_EXECUTERARGS), e->m_PgmArgs );
327 e->terminal = session_get_bool( p, SECSTR(SECTION_EXECUTER), szRunInTerminalItem, TRUE);
328 if (e->m_PgmArgs && e->m_PgmArgs->data)
330 prop_set_with_key (e->props, EXECUTER_PROGRAM_ARGS_KEY,
331 e->m_PgmArgs->data);