Updated Spanish translation
[anjuta-git-plugin.git] / plugins / cvs-plugin / cvs-execute.c
blobacbee93b79418c334fe37e14566218850d3588f4
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 cvs-execute.c
4 Copyright (C) 2004 Johannes Schmid
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 "cvs-execute.h"
22 #include "plugin.h"
24 #include <libanjuta/interfaces/ianjuta-message-manager.h>
25 #include <libanjuta/interfaces/ianjuta-document-manager.h>
26 #include <libanjuta/interfaces/ianjuta-editor.h>
27 #include <libanjuta/anjuta-launcher.h>
28 #include <libanjuta/anjuta-debug.h>
30 #include <glade/glade.h>
31 #include <pcre.h>
33 #define CVS_ICON ""
34 #define CVS_INFO_REGEXP "(cvs update:.|cvs server:.)"
35 #define CVS_ERR_REGEXP "^C ."
38 static GtkWidget* status_text;
40 static void
41 on_mesg_view_destroy(CVSPlugin* plugin, gpointer destroyed_view)
43 plugin->mesg_view = NULL;
46 static void
47 on_cvs_mesg_format (IAnjutaMessageView *view, const gchar *line,
48 AnjutaPlugin *plugin)
50 IAnjutaMessageViewType type;
51 pcre *info, *err;
52 const gchar *err_buf;
53 int err_ptr, output[16];
55 g_return_if_fail (line != NULL);
57 /* Compile the regexps for message types. */
58 if (!(info = pcre_compile(CVS_INFO_REGEXP, 0, &err_buf, &err_ptr, NULL)))
60 g_free((gchar *) err_buf);
61 return;
63 if (!(err = pcre_compile(CVS_ERR_REGEXP, 0, &err_buf, &err_ptr, NULL)))
65 g_free((gchar *) err_buf);
66 return;
69 /* Match against type regexps to find the message type. */
70 if (pcre_exec(info, NULL, line, strlen(line), 0, 0, output, 16) >= 0)
72 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
74 else if (pcre_exec(err, NULL, line, strlen(line), 0, 0, output, 16) >= 0)
76 type = IANJUTA_MESSAGE_VIEW_TYPE_ERROR;
78 else type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
80 ianjuta_message_view_append (view, type, line, "", NULL);
82 pcre_free(info);
83 pcre_free(err);
86 static void
87 on_cvs_mesg_parse (IAnjutaMessageView *view, const gchar *line,
88 AnjutaPlugin *plugin)
90 /* FIXME: Parse the line and determine if there is filename to goto.
91 If there is, extract filename then open it.
95 #if 0
96 gchar *filename;
97 gint lineno;
99 if ((filename = parse_filename (line)))
101 gchar *uri;
102 IAnjutaFileLoader *loader;
104 /* Go to file and line number */
105 loader = anjuta_shell_get_interface (plugin->shell, IAnjutaFileLoader,
106 NULL);
108 /* FIXME: Determine full file path */
109 uri = gnome_vfs_get_uri_from_local_path(filename);
110 ianjuta_file_loader_load (loader, uri, FALSE, NULL);
111 g_free (uri);
112 g_free (filename);
114 #endif
117 static void
118 on_cvs_terminated (AnjutaLauncher *launcher, gint child_pid, gint status,
119 gulong time_taken, CVSPlugin *plugin)
121 g_return_if_fail (plugin != NULL);
122 /* DEBUG_PRINT ("Shuting down cvs message view"); */
124 if (status != 0)
126 ianjuta_message_view_append (plugin->mesg_view,
127 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
128 _("CVS command failed - See above for details"), "", NULL);
130 else
132 gchar *mesg;
133 mesg = g_strdup_printf (ngettext("CVS command successful! - Time taken: %ld second",
134 "CVS command successful! - Time taken: %ld seconds",
135 time_taken), time_taken);
136 ianjuta_message_view_append (plugin->mesg_view,
137 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
138 mesg, "", NULL);
139 g_free (mesg);
141 plugin->executing_command = FALSE;
144 static void
145 on_cvs_message (AnjutaLauncher *launcher,
146 AnjutaLauncherOutputType output_type,
147 const gchar * mesg, gpointer user_data)
149 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
150 if (plugin->mesg_view)
151 ianjuta_message_view_buffer_append (plugin->mesg_view, mesg, NULL);
154 static void
155 on_cvs_status(AnjutaLauncher *launcher,
156 AnjutaLauncherOutputType output_type,
157 const gchar * mesg, gpointer user_data)
159 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
160 switch(output_type)
162 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
163 if (plugin->mesg_view)
164 ianjuta_message_view_buffer_append (plugin->mesg_view,
165 mesg, NULL);
166 break;
167 default:
169 GtkTextBuffer* textbuf;
170 GtkTextIter end;
172 if (status_text)
174 textbuf =
175 gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_text));
176 gtk_text_buffer_get_end_iter(textbuf, &end);
178 gtk_text_buffer_insert(textbuf, &end, mesg, -1);
184 static void
185 on_cvs_diff(AnjutaLauncher *launcher,
186 AnjutaLauncherOutputType output_type,
187 const gchar * mesg, gpointer user_data)
189 g_return_if_fail (user_data != NULL);
190 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
192 switch(output_type)
194 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
195 if (plugin->mesg_view)
196 ianjuta_message_view_buffer_append (plugin->mesg_view,
197 mesg, NULL);
198 break;
199 default:
200 ianjuta_editor_append (plugin->diff_editor, mesg, -1, NULL);
204 static gboolean
205 on_cvs_status_destroy(GtkWidget* window, GdkEvent* event, gpointer data)
207 status_text = NULL;
209 return FALSE; /* Do not block the event */
212 static void
213 cvs_execute_common (CVSPlugin* plugin, const gchar* command, const gchar* dir,
214 AnjutaLauncherOutputCallback output)
216 IAnjutaMessageManager *mesg_manager;
218 g_return_if_fail (command != NULL);
219 g_return_if_fail (dir != NULL);
221 if (plugin->executing_command)
223 anjuta_util_dialog_error
224 (NULL,_("CVS command is running - please wait until it finishes!"),
225 NULL);
226 return;
229 mesg_manager = anjuta_shell_get_interface
230 (ANJUTA_PLUGIN (plugin)->shell, IAnjutaMessageManager, NULL);
231 plugin->mesg_view =
232 ianjuta_message_manager_get_view_by_name(mesg_manager, _("CVS"), NULL);
233 if (!plugin->mesg_view)
235 plugin->mesg_view =
236 ianjuta_message_manager_add_view (mesg_manager, _("CVS"),
237 CVS_ICON, NULL);
238 g_object_weak_ref (G_OBJECT (plugin->mesg_view),
239 (GWeakNotify)on_mesg_view_destroy, plugin);
240 g_signal_connect (G_OBJECT (plugin->mesg_view), "buffer-flushed",
241 G_CALLBACK (on_cvs_mesg_format), plugin);
242 g_signal_connect (G_OBJECT (plugin->mesg_view), "message-clicked",
243 G_CALLBACK (on_cvs_mesg_parse), plugin);
245 ianjuta_message_view_clear(plugin->mesg_view, NULL);
247 if (plugin->launcher == NULL)
249 plugin->launcher = anjuta_launcher_new ();
251 g_signal_connect (G_OBJECT (plugin->launcher), "child-exited",
252 G_CALLBACK (on_cvs_terminated), plugin);
254 chdir (dir);
255 plugin->executing_command = TRUE;
257 /* DEBUG_PRINT ("CVS Executing: %s", command); */
258 ianjuta_message_view_append (plugin->mesg_view,
259 IANJUTA_MESSAGE_VIEW_TYPE_NORMAL,
260 command, "", NULL);
261 anjuta_launcher_execute (plugin->launcher, command, output, plugin);
264 void
265 cvs_execute(CVSPlugin* plugin, const gchar* command, const gchar* dir)
267 cvs_execute_common(plugin, command, dir, on_cvs_message);
270 void
271 cvs_execute_status(CVSPlugin* plugin, const gchar* command, const gchar* dir)
273 GladeXML* gxml;
274 GtkWidget* window;
276 gxml = glade_xml_new(GLADE_FILE, "cvs_status_output", NULL);
277 window = glade_xml_get_widget(gxml, "cvs_status_output");
278 status_text = glade_xml_get_widget(gxml, "cvs_status_text");
280 g_signal_connect(G_OBJECT(window), "delete-event",
281 G_CALLBACK(on_cvs_status_destroy), status_text);
283 gtk_widget_show(window);
284 cvs_execute_common(plugin, command, dir, on_cvs_status);
287 void
288 cvs_execute_diff(CVSPlugin* plugin, const gchar* command, const gchar* dir)
290 IAnjutaDocumentManager *docman;
292 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
293 IAnjutaDocumentManager, NULL);
294 plugin->diff_editor =
295 ianjuta_document_manager_add_buffer(docman, "cvs.diff", "", NULL);
296 cvs_execute_common(plugin, command, dir, on_cvs_diff);
299 void
300 cvs_execute_log(CVSPlugin* plugin, const gchar* command, const gchar* dir)
302 IAnjutaDocumentManager *docman;
304 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
305 IAnjutaDocumentManager, NULL);
306 plugin->diff_editor =
307 ianjuta_document_manager_add_buffer(docman, "cvs.log", "", NULL);
308 cvs_execute_common(plugin, command, dir, on_cvs_diff);