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
29 #include <libanjuta/resources.h>
32 #include "text_editor.h"
33 #include "utilities.h"
36 static const gchar szRunInTerminalItem
[] = {"RunInTerminal"};
37 //static const gchar szPgmArgsSection[] = {"PgmArgs"};
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 */
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"));
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"));
70 else /* File mode checks */
74 te
= anjuta_get_current_text_editor ();
77 anjuta_warning(_("No file or Project opened."));
80 if (te
->full_filename
== NULL
)
82 anjuta_warning(_("No executable for this file."));
85 if (anjuta_get_file_property (FILE_PROP_IS_INTERPRETED
, te
->full_filename
, 0) == 0)
89 struct stat s_stat
, e_stat
;
92 prog
= g_strdup (te
->full_filename
);
93 temp
= get_file_extension (prog
);
96 s_re
= stat (te
->full_filename
, &s_stat
);
97 e_re
= stat (prog
, &e_stat
);
99 if ((e_re
!= 0) || (s_re
!= 0))
101 anjuta_warning(_("No executable for this file."));
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
);
118 anjuta_warning (_("Unable to execute Project. Check Settings->Commands."));
121 dir
= project_dbase_get_module_dir (app
->project_dbase
, MODULE_SOURCE
);
123 else /* Execute file */
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."));
135 anjuta_set_file_properties (te
->full_filename
);
136 cmd
= command_editor_get_command_file (app
->command_editor
,
137 COMMAND_EXECUTE_FILE
, te
->filename
);
140 anjuta_warning (_("Unable to execute file. Check Settings->Commands."));
143 dir
= extract_directory (te
->full_filename
);
145 command
= g_strconcat ("anjuta_launcher ", cmd
, NULL
);
149 g_message("Raw Command is: %s", command
);
152 /* Get command and execute */
156 escaped_cmd
= anjuta_util_escape_quotes(command
);
157 prop_set_with_key (e
->props
, "anjuta.current.command", escaped_cmd
);
160 g_message("Escaped Command is: %s", escaped_cmd
);
163 cmd
= command_editor_get_command (app
->command_editor
, COMMAND_TERMINAL
);
168 prop_set_with_key (e
->props
, "anjuta.current.command", command
);
169 cmd
= g_strdup (command
);
173 g_message("Final Command is: %s", cmd
);
176 anjuta_set_execution_dir (dir
);
177 if (dir
) chdir (dir
);
178 gnome_execute_shell (dir
, cmd
);
185 on_executer_dialog_response (GtkDialog
*dialog
, gint response
,
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
)),
198 executer_execute (e
);
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
));
209 prop_set_with_key (e
->props
, EXECUTER_PROGRAM_ARGS_KEY
, options
);
211 prop_set_with_key (e
->props
, EXECUTER_PROGRAM_ARGS_KEY
, "");
215 on_executer_checkbutton_toggled (GtkToggleButton
* togglebutton
,
218 Executer
*e
= user_data
;
219 e
->terminal
= gtk_toggle_button_get_active (togglebutton
);
223 create_executer_dialog (Executer
* e
)
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
),
241 options
= prop_get (e
->props
, EXECUTER_PROGRAM_ARGS_KEY
);
244 gtk_entry_set_text (GTK_ENTRY (e
->m_gui
.combo_entry1
), options
);
247 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (e
->m_gui
.check_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
;
264 executer_new (PropsID props
)
266 Executer
*e
= malloc (sizeof (Executer
));
271 e
->m_PgmArgs
= NULL
;
272 e
->is_showing
= FALSE
;
277 executer_destroy (Executer
* e
)
284 for (i
= 0; i
< g_list_length (e
->m_PgmArgs
); i
++)
285 g_free (g_list_nth (e
->m_PgmArgs
, i
)->data
);
288 g_list_free (e
->m_PgmArgs
);
295 executer_show (Executer
* e
)
299 gtk_widget_show (create_executer_dialog (e
));
301 gtk_combo_set_popdown_strings (GTK_COMBO (e
->m_gui
.combo1
),
303 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (e
->m_gui
.check_terminal
), e
->terminal
);
304 e
->is_showing
= TRUE
;
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
);
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
,