1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
5  *
6  * anjuta is free software.
7  *
8  * You may redistribute it and/or modify it under the terms of the
9  * GNU General Public License, as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * anjuta is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with anjuta.  If not, write to:
20  * 	The Free Software Foundation, Inc.,
21  * 	51 Franklin Street, Fifth Floor
22  * 	Boston, MA  02110-1301, USA.
23  */
24 
25 #include "svn-cat-command.h"
26 
27 struct _SvnCatCommandPriv
28 {
29 	gchar *path;
30 	glong revision;
31 	GQueue *output;
32 };
33 
34 G_DEFINE_TYPE (SvnCatCommand, svn_cat_command, SVN_TYPE_COMMAND);
35 
36 static void
svn_cat_command_init(SvnCatCommand * self)37 svn_cat_command_init (SvnCatCommand *self)
38 {
39 	self->priv = g_new0 (SvnCatCommandPriv, 1);
40 }
41 
42 static void
svn_cat_command_finalize(GObject * object)43 svn_cat_command_finalize (GObject *object)
44 {
45 	SvnCatCommand *self;
46 	GList *current_output;
47 
48 	self = SVN_CAT_COMMAND (object);
49 
50 	g_free (self->priv->path);
51 
52 	current_output = self->priv->output->head;
53 
54 	while (current_output)
55 	{
56 		g_free (current_output->data);
57 		current_output = g_list_next (current_output);
58 	}
59 
60 	g_queue_free (self->priv->output);
61 
62 	g_free (self->priv);
63 
64 	G_OBJECT_CLASS (svn_cat_command_parent_class)->finalize (object);
65 }
66 
67 static guint
svn_cat_command_run(AnjutaCommand * command)68 svn_cat_command_run (AnjutaCommand *command)
69 {
70 	SvnCatCommand *self;
71 	SvnCommand *svn_command;
72 	svn_opt_revision_t revision;
73 	svn_opt_revision_t peg_revision;
74 	svn_stream_t *cat_stream;
75 	apr_file_t *cat_input;
76 	apr_file_t *cat_output;
77 	apr_size_t read_size;
78 	gchar *line;
79 	svn_error_t *error;
80 	apr_status_t apr_error;
81 
82 	self = SVN_CAT_COMMAND (command);
83 	svn_command = SVN_COMMAND (command);
84 
85 	apr_file_pipe_create (&cat_output, &cat_input,
86 						  svn_command_get_pool (svn_command));
87 	apr_file_pipe_timeout_set (cat_output, 0);
88 	apr_file_pipe_timeout_set (cat_input, 0);
89 	cat_stream = svn_stream_from_aprfile2 (cat_input, FALSE,
90 										   svn_command_get_pool (svn_command));
91 
92 	revision.kind = svn_opt_revision_number;
93 	revision.value.number = self->priv->revision;
94 	peg_revision.kind = svn_opt_revision_unspecified;
95 
96 	error = svn_client_cat2 (cat_stream,
97 							 self->priv->path,
98 							 &peg_revision,
99 							 &revision,
100 							 svn_command_get_client_context (svn_command),
101 							 svn_command_get_pool (svn_command));
102 
103 	if (error)
104 	{
105 		svn_command_set_error (svn_command, error);
106 		return 1;
107 	}
108 
109 	while (apr_file_eof (cat_output) != APR_EOF)
110 	{
111 		read_size = 80;
112 		line = g_new0 (gchar, (read_size + 1));
113 
114 		apr_error = apr_file_read (cat_output, line, &read_size);
115 
116 		if (apr_error)
117 			break;
118 
119 		if (strlen (line))
120 		{
121 			anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
122 			g_queue_push_tail (self->priv->output, g_strdup (line));
123 			anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
124 
125 			g_free (line);
126 
127 			anjuta_command_notify_data_arrived (command);
128 		}
129 	}
130 
131 	return 0;
132 }
133 
134 static void
svn_cat_command_class_init(SvnCatCommandClass * klass)135 svn_cat_command_class_init (SvnCatCommandClass *klass)
136 {
137 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
138 	AnjutaCommandClass* command_class = ANJUTA_COMMAND_CLASS (klass);
139 
140 	object_class->finalize = svn_cat_command_finalize;
141 	command_class->run = svn_cat_command_run;
142 }
143 
144 
145 SvnCatCommand *
svn_cat_command_new(const gchar * path,glong revision)146 svn_cat_command_new (const gchar *path, glong revision)
147 {
148 	SvnCatCommand *self;
149 
150 	self = g_object_new (SVN_TYPE_CAT_COMMAND, NULL);
151 	self->priv->path = svn_command_make_canonical_path (SVN_COMMAND (self),
152 														path);
153 	self->priv->revision = revision;
154 	self->priv->output = g_queue_new ();
155 
156 	return self;
157 }
158 
159 void
svn_cat_command_destroy(SvnCatCommand * self)160 svn_cat_command_destroy (SvnCatCommand *self)
161 {
162 	g_object_unref (self);
163 }
164 
165 GQueue *
svn_cat_command_get_output(SvnCatCommand * self)166 svn_cat_command_get_output (SvnCatCommand *self)
167 {
168 	return self->priv->output;
169 }
170