File: cvs-execute.c

package info (click to toggle)
anjuta 2%3A3.34.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 72,440 kB
  • sloc: ansic: 207,444; sh: 47,499; cpp: 11,461; makefile: 3,586; yacc: 2,821; perl: 2,094; lex: 1,546; xml: 904; python: 149; sql: 99; javascript: 51; java: 10
file content (312 lines) | stat: -rw-r--r-- 8,693 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
    cvs-execute.c
    Copyright (C) 2004 Johannes Schmid

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "cvs-execute.h"
#include "plugin.h"

#include <libanjuta/interfaces/ianjuta-message-manager.h>
#include <libanjuta/interfaces/ianjuta-document-manager.h>
#include <libanjuta/interfaces/ianjuta-editor.h>
#include <libanjuta/anjuta-launcher.h>
#include <libanjuta/anjuta-debug.h>

#include <unistd.h>

#define CVS_ICON ""
#define CVS_INFO_REGEXP "(cvs update:.|cvs server:.)"
#define CVS_ERR_REGEXP "^C ."


static GtkWidget* status_text;

static void
on_mesg_view_destroy(CVSPlugin* plugin, gpointer destroyed_view)
{
	plugin->mesg_view = NULL;
}

static void
on_cvs_mesg_format (IAnjutaMessageView *view, const gchar *line,
					  AnjutaPlugin *plugin)
{
	IAnjutaMessageViewType type;
	GRegex *info, *err;
	GError *error = NULL;
	
	g_return_if_fail (line != NULL);

	/* Compile the regexps for message types. */
	info = g_regex_new (CVS_INFO_REGEXP, 0, 0, &error);
	if (error != NULL)
	{
		g_error_free (error);
		return;
	}
	err = g_regex_new (CVS_ERR_REGEXP, 0, 0, &error);
	if (error != NULL)
	{
		g_error_free (error);
		return;
	}		
	
	/* Match against type regexps to find the message type. */
	if (g_regex_match (info, line, 0, NULL))
		type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
	else if (g_regex_match (info, line, 0, NULL))
		type = IANJUTA_MESSAGE_VIEW_TYPE_ERROR;
	else 
		type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;

	ianjuta_message_view_append (view, type, line, "", NULL);
	
	g_regex_unref (info);
	g_regex_unref (err);
}

static void
on_cvs_mesg_parse (IAnjutaMessageView *view, const gchar *line,
					 AnjutaPlugin *plugin)
{
	/* FIXME: Parse the line and determine if there is filename to goto.
	   If there is, extract filename then open it.

	*/

#if 0
	gchar *filename;
	gint lineno;
	
	if ((filename = parse_filename (line)))
	{
		GFile *file;
		IAnjutaFileLoader *loader;
		
		/* Go to file and line number */
		loader = anjuta_shell_get_interface (plugin->shell, IAnjutaFileLoader,
											 NULL);
		
		/* FIXME: Determine full file path */
		file = g_file_new_for_path (filename);
		ianjuta_file_loader_load (loader, uri, FALSE, NULL);
		g_object_unref (file);
		g_free (filename);
	}
#endif
}

static void
on_cvs_terminated (AnjutaLauncher *launcher, gint child_pid, gint status,
				   gulong time_taken, CVSPlugin *plugin)
{
	g_return_if_fail (plugin != NULL);
	/* DEBUG_PRINT ("%s", "Shuting down cvs message view"); */
	
	if (status != 0)
	{
		ianjuta_message_view_append (plugin->mesg_view,
									 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
			_("CVS command failed. See above for details"), "", NULL);
	}
	else
	{
		gchar *mesg;
		mesg = g_strdup_printf (ngettext("CVS command successful! Time taken: %ld second",
										 "CVS command successful! Time taken: %ld seconds",
										 time_taken), time_taken);
		ianjuta_message_view_append (plugin->mesg_view,
									 IANJUTA_MESSAGE_VIEW_TYPE_INFO,
									 mesg, "", NULL);
		g_free (mesg);
	}
	plugin->executing_command = FALSE;
}

static void
on_cvs_message (AnjutaLauncher *launcher,
					   AnjutaLauncherOutputType output_type,
					   const gchar * mesg, gpointer user_data)
{
	CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
	if (plugin->mesg_view)
		ianjuta_message_view_buffer_append (plugin->mesg_view, mesg, NULL);
}

static void 
on_cvs_status(AnjutaLauncher *launcher,
					   AnjutaLauncherOutputType output_type,
					   const gchar * mesg, gpointer user_data)
{
	CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
	switch(output_type)
	{
		case ANJUTA_LAUNCHER_OUTPUT_STDERR:
			if (plugin->mesg_view)
				ianjuta_message_view_buffer_append (plugin->mesg_view,
													mesg, NULL);
			break;
		default:
		{
			GtkTextBuffer* textbuf;
			GtkTextIter end;
			
			if (status_text)
			{
				textbuf =
					gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_text));
				gtk_text_buffer_get_end_iter(textbuf, &end);
				
				gtk_text_buffer_insert(textbuf, &end, mesg, -1);
			}
		}
	}	
}

static void
on_cvs_diff(AnjutaLauncher *launcher,
			AnjutaLauncherOutputType output_type,
			const gchar * mesg, gpointer user_data)
{
	g_return_if_fail (user_data != NULL);
	CVSPlugin* plugin = ANJUTA_PLUGIN_CVS (user_data);
	
	switch(output_type)
	{
		case ANJUTA_LAUNCHER_OUTPUT_STDERR:
			if (plugin->mesg_view)
				ianjuta_message_view_buffer_append (plugin->mesg_view,
													mesg, NULL);
			break;
		default:
			ianjuta_editor_append (plugin->diff_editor, mesg, -1, NULL);
	}
}

static gboolean
on_cvs_status_destroy(GtkWidget* window, GdkEvent* event, gpointer data)
{
	status_text = NULL;
	
	return FALSE; /* Do not block the event */
}

static void 
cvs_execute_common (CVSPlugin* plugin, const gchar* command, const gchar* dir,
					AnjutaLauncherOutputCallback output)
{
	IAnjutaMessageManager *mesg_manager;
	
	g_return_if_fail (command != NULL);
	g_return_if_fail (dir != NULL);	
	
	if (plugin->executing_command)
	{
		anjuta_util_dialog_error
			(NULL,_("CVS command is running — please wait until it finishes!"),
			 NULL);
		return;
	}
		
	mesg_manager = anjuta_shell_get_interface 
		(ANJUTA_PLUGIN (plugin)->shell,	IAnjutaMessageManager, NULL);
	plugin->mesg_view = 
	    ianjuta_message_manager_get_view_by_name(mesg_manager, _("CVS"), NULL);
	if (!plugin->mesg_view)
	{
		plugin->mesg_view =
		     ianjuta_message_manager_add_view (mesg_manager, _("CVS"), 
											   CVS_ICON, NULL);
		g_object_weak_ref (G_OBJECT (plugin->mesg_view), 
						  (GWeakNotify)on_mesg_view_destroy, plugin);
		g_signal_connect (G_OBJECT (plugin->mesg_view), "buffer-flushed",
						  G_CALLBACK (on_cvs_mesg_format), plugin);
		g_signal_connect (G_OBJECT (plugin->mesg_view), "message-clicked",
						  G_CALLBACK (on_cvs_mesg_parse), plugin);
	}
	ianjuta_message_view_clear(plugin->mesg_view, NULL);

	if (plugin->launcher == NULL)
	{
		plugin->launcher = anjuta_launcher_new ();
		
		g_signal_connect (G_OBJECT (plugin->launcher), "child-exited",
						  G_CALLBACK (on_cvs_terminated), plugin);
	}
	chdir (dir);
	plugin->executing_command = TRUE;

	/* DEBUG_PRINT ("CVS Executing: %s", command); */
	ianjuta_message_view_append (plugin->mesg_view,
								 IANJUTA_MESSAGE_VIEW_TYPE_NORMAL,
								 command, "", NULL);
	anjuta_launcher_execute (plugin->launcher, command, output, plugin);
}

void 
cvs_execute(CVSPlugin* plugin, const gchar* command, const gchar* dir)
{
	cvs_execute_common(plugin, command, dir, on_cvs_message);	
}

void
cvs_execute_status(CVSPlugin* plugin, const gchar* command, const gchar* dir)
{
	GtkBuilder* bxml;
	GtkWidget* window;
	GError* error = NULL;
	bxml = gtk_builder_new();
	if (!gtk_builder_add_from_file(bxml, GLADE_FILE, &error))
	{
		g_warning("Couldn't load builder file: %s", error->message);
		g_error_free(error);
	}

	window = GTK_WIDGET(gtk_builder_get_object(bxml, "cvs_status_output"));
	status_text = GTK_WIDGET(gtk_builder_get_object(bxml, "cvs_status_text"));
	
	g_signal_connect(G_OBJECT(window), "delete-event", 
		G_CALLBACK(on_cvs_status_destroy), status_text);
	
	gtk_widget_show(window);
	cvs_execute_common(plugin, command, dir, on_cvs_status);	
}

void
cvs_execute_diff(CVSPlugin* plugin, const gchar* command, const gchar* dir)
{
	IAnjutaDocumentManager *docman;
	
	docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
	                                     IAnjutaDocumentManager, NULL);
	plugin->diff_editor =
		ianjuta_document_manager_add_buffer(docman, "cvs.diff", "", NULL);
	cvs_execute_common(plugin, command, dir, on_cvs_diff);
}

void
cvs_execute_log(CVSPlugin* plugin, const gchar* command, const gchar* dir)
{
	IAnjutaDocumentManager *docman;
	
	docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
	                                     IAnjutaDocumentManager, NULL);
	plugin->diff_editor =
		ianjuta_document_manager_add_buffer(docman, "cvs.log", "", NULL);
	cvs_execute_common(plugin, command, dir, on_cvs_diff);
}