Updated Makefile.am files after make -f git.mk
[anjuta.git] / plugins / cvs-plugin / cvs-execute.c
blob0a4c3a86c80e3a405d83c2bc3561e81d86b44354
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 <unistd.h>
32 #define CVS_ICON ""
33 #define CVS_INFO_REGEXP "(cvs update:.|cvs server:.)"
34 #define CVS_ERR_REGEXP "^C ."
37 static GtkWidget* status_text;
39 static void
40 on_mesg_view_destroy(CVSPlugin* plugin, gpointer destroyed_view)
42 plugin->mesg_view = NULL;
45 static void
46 on_cvs_mesg_format (IAnjutaMessageView *view, const gchar *line,
47 AnjutaPlugin *plugin)
49 IAnjutaMessageViewType type;
50 GRegex *info, *err;
51 GError *error = NULL;
53 g_return_if_fail (line != NULL);
55 /* Compile the regexps for message types. */
56 info = g_regex_new (CVS_INFO_REGEXP, 0, 0, &error);
57 if (error != NULL)
59 g_error_free (error);
60 return;
62 err = g_regex_new (CVS_ERR_REGEXP, 0, 0, &error);
63 if (error != NULL)
65 g_error_free (error);
66 return;
69 /* Match against type regexps to find the message type. */
70 if (g_regex_match (info, line, 0, NULL))
71 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
72 else if (g_regex_match (info, line, 0, NULL))
73 type = IANJUTA_MESSAGE_VIEW_TYPE_ERROR;
74 else
75 type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
77 ianjuta_message_view_append (view, type, line, "", NULL);
79 g_regex_unref (info);
80 g_regex_unref (err);
83 static void
84 on_cvs_mesg_parse (IAnjutaMessageView *view, const gchar *line,
85 AnjutaPlugin *plugin)
87 /* FIXME: Parse the line and determine if there is filename to goto.
88 If there is, extract filename then open it.
92 #if 0
93 gchar *filename;
94 gint lineno;
96 if ((filename = parse_filename (line)))
98 GFile *file;
99 IAnjutaFileLoader *loader;
101 /* Go to file and line number */
102 loader = anjuta_shell_get_interface (plugin->shell, IAnjutaFileLoader,
103 NULL);
105 /* FIXME: Determine full file path */
106 file = g_file_new_for_path (filename);
107 ianjuta_file_loader_load (loader, uri, FALSE, NULL);
108 g_object_unref (file);
109 g_free (filename);
111 #endif
114 static void
115 on_cvs_terminated (AnjutaLauncher *launcher, gint child_pid, gint status,
116 gulong time_taken, CVSPlugin *plugin)
118 g_return_if_fail (plugin != NULL);
119 /* DEBUG_PRINT ("%s", "Shuting down cvs message view"); */
121 if (status != 0)
123 ianjuta_message_view_append (plugin->mesg_view,
124 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
125 _("CVS command failed. See above for details"), "", NULL);
127 else
129 gchar *mesg;
130 mesg = g_strdup_printf (ngettext("CVS command successful! Time taken: %ld second",
131 "CVS command successful! Time taken: %ld seconds",
132 time_taken), time_taken);
133 ianjuta_message_view_append (plugin->mesg_view,
134 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
135 mesg, "", NULL);
136 g_free (mesg);
138 plugin->executing_command = FALSE;
141 static void
142 on_cvs_message (AnjutaLauncher *launcher,
143 AnjutaLauncherOutputType output_type,
144 const gchar * mesg, gpointer user_data)
146 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
147 if (plugin->mesg_view)
148 ianjuta_message_view_buffer_append (plugin->mesg_view, mesg, NULL);
151 static void
152 on_cvs_status(AnjutaLauncher *launcher,
153 AnjutaLauncherOutputType output_type,
154 const gchar * mesg, gpointer user_data)
156 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
157 switch(output_type)
159 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
160 if (plugin->mesg_view)
161 ianjuta_message_view_buffer_append (plugin->mesg_view,
162 mesg, NULL);
163 break;
164 default:
166 GtkTextBuffer* textbuf;
167 GtkTextIter end;
169 if (status_text)
171 textbuf =
172 gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_text));
173 gtk_text_buffer_get_end_iter(textbuf, &end);
175 gtk_text_buffer_insert(textbuf, &end, mesg, -1);
181 static void
182 on_cvs_diff(AnjutaLauncher *launcher,
183 AnjutaLauncherOutputType output_type,
184 const gchar * mesg, gpointer user_data)
186 g_return_if_fail (user_data != NULL);
187 CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
189 switch(output_type)
191 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
192 if (plugin->mesg_view)
193 ianjuta_message_view_buffer_append (plugin->mesg_view,
194 mesg, NULL);
195 break;
196 default:
197 ianjuta_editor_append (plugin->diff_editor, mesg, -1, NULL);
201 static gboolean
202 on_cvs_status_destroy(GtkWidget* window, GdkEvent* event, gpointer data)
204 status_text = NULL;
206 return FALSE; /* Do not block the event */
209 static void
210 cvs_execute_common (CVSPlugin* plugin, const gchar* command, const gchar* dir,
211 AnjutaLauncherOutputCallback output)
213 IAnjutaMessageManager *mesg_manager;
215 g_return_if_fail (command != NULL);
216 g_return_if_fail (dir != NULL);
218 if (plugin->executing_command)
220 anjuta_util_dialog_error
221 (NULL,_("CVS command is running — please wait until it finishes!"),
222 NULL);
223 return;
226 mesg_manager = anjuta_shell_get_interface
227 (ANJUTA_PLUGIN (plugin)->shell, IAnjutaMessageManager, NULL);
228 plugin->mesg_view =
229 ianjuta_message_manager_get_view_by_name(mesg_manager, _("CVS"), NULL);
230 if (!plugin->mesg_view)
232 plugin->mesg_view =
233 ianjuta_message_manager_add_view (mesg_manager, _("CVS"),
234 CVS_ICON, NULL);
235 g_object_weak_ref (G_OBJECT (plugin->mesg_view),
236 (GWeakNotify)on_mesg_view_destroy, plugin);
237 g_signal_connect (G_OBJECT (plugin->mesg_view), "buffer-flushed",
238 G_CALLBACK (on_cvs_mesg_format), plugin);
239 g_signal_connect (G_OBJECT (plugin->mesg_view), "message-clicked",
240 G_CALLBACK (on_cvs_mesg_parse), plugin);
242 ianjuta_message_view_clear(plugin->mesg_view, NULL);
244 if (plugin->launcher == NULL)
246 plugin->launcher = anjuta_launcher_new ();
248 g_signal_connect (G_OBJECT (plugin->launcher), "child-exited",
249 G_CALLBACK (on_cvs_terminated), plugin);
251 chdir (dir);
252 plugin->executing_command = TRUE;
254 /* DEBUG_PRINT ("CVS Executing: %s", command); */
255 ianjuta_message_view_append (plugin->mesg_view,
256 IANJUTA_MESSAGE_VIEW_TYPE_NORMAL,
257 command, "", NULL);
258 anjuta_launcher_execute (plugin->launcher, command, output, plugin);
261 void
262 cvs_execute(CVSPlugin* plugin, const gchar* command, const gchar* dir)
264 cvs_execute_common(plugin, command, dir, on_cvs_message);
267 void
268 cvs_execute_status(CVSPlugin* plugin, const gchar* command, const gchar* dir)
270 GtkBuilder* bxml;
271 GtkWidget* window;
272 GError* error = NULL;
273 bxml = gtk_builder_new();
274 if (!gtk_builder_add_from_file(bxml, GLADE_FILE, &error))
276 g_warning("Couldn't load builder file: %s", error->message);
277 g_error_free(error);
280 window = GTK_WIDGET(gtk_builder_get_object(bxml, "cvs_status_output"));
281 status_text = GTK_WIDGET(gtk_builder_get_object(bxml, "cvs_status_text"));
283 g_signal_connect(G_OBJECT(window), "delete-event",
284 G_CALLBACK(on_cvs_status_destroy), status_text);
286 gtk_widget_show(window);
287 cvs_execute_common(plugin, command, dir, on_cvs_status);
290 void
291 cvs_execute_diff(CVSPlugin* plugin, const gchar* command, const gchar* dir)
293 IAnjutaDocumentManager *docman;
295 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
296 IAnjutaDocumentManager, NULL);
297 plugin->diff_editor =
298 ianjuta_document_manager_add_buffer(docman, "cvs.diff", "", NULL);
299 cvs_execute_common(plugin, command, dir, on_cvs_diff);
302 void
303 cvs_execute_log(CVSPlugin* plugin, const gchar* command, const gchar* dir)
305 IAnjutaDocumentManager *docman;
307 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
308 IAnjutaDocumentManager, NULL);
309 plugin->diff_editor =
310 ianjuta_document_manager_add_buffer(docman, "cvs.log", "", NULL);
311 cvs_execute_common(plugin, command, dir, on_cvs_diff);