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  * Portions based on the original Subversion plugin
7  * Copyright (C) Johannes Schmid 2005
8  *
9  * anjuta is free software.
10  *
11  * You may redistribute it and/or modify it under the terms of the
12  * GNU General Public License, as published by the Free Software
13  * Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  *
16  * anjuta is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with anjuta.  If not, write to:
23  * 	The Free Software Foundation, Inc.,
24  * 	51 Franklin Street, Fifth Floor
25  * 	Boston, MA  02110-1301, USA.
26  */
27 
28 #include "anjuta-async-command.h"
29 
30 /**
31  * SECTION: anjuta-async-command
32  * @short_description: #AnjutaCommand subclass that serves as the base for
33  *					   commands that need to run in another thread.
34  * @include: libanjuta/anjuta-async-command.h
35  *
36  * #AnjutaAsyncCommand provides a simple way for plugins to run tasks that
37  * are synchronous and usually take several seconds or longer to execute in
38  * another thread so that such tasks do no block Anjuta's user interface.
39  *
40  * #AnjutaAsyncCommand automatically runs and manages the thread when the
41  * command starts, and destroys it when the command finishes. Aside from
42  * locking protected data with anjuta_async_command_lock/unlock, clients, and
43  * even commands themselves need not even be concerned that their tasks are
44  * rnning on another thread.
45  *
46  * For an example of how #AnjutaAsyncCommand is used, see the Subversion plugin.
47  */
48 
49 struct _AnjutaAsyncCommandPriv
50 {
51 	GMutex mutex;
52 	guint return_code;
53 	gboolean complete;
54 	gboolean new_data_arrived;
55 	gboolean progress_changed;
56 	gfloat progress;
57 };
58 
59 G_DEFINE_TYPE (AnjutaAsyncCommand, anjuta_async_command, ANJUTA_TYPE_COMMAND);
60 
61 static void
anjuta_async_command_init(AnjutaAsyncCommand * self)62 anjuta_async_command_init (AnjutaAsyncCommand *self)
63 {
64 	self->priv = g_new0 (AnjutaAsyncCommandPriv, 1);
65 
66 	g_mutex_init (&self->priv->mutex);
67 }
68 
69 static void
anjuta_async_command_finalize(GObject * object)70 anjuta_async_command_finalize (GObject *object)
71 {
72 	AnjutaAsyncCommand *self;
73 
74 	self = ANJUTA_ASYNC_COMMAND (object);
75 
76 	g_mutex_clear (&self->priv->mutex);
77 	g_idle_remove_by_data (self);
78 
79 	g_free (self->priv);
80 
81 	G_OBJECT_CLASS (anjuta_async_command_parent_class)->finalize (object);
82 }
83 
84 static gboolean
anjuta_async_command_notification_poll(AnjutaCommand * command)85 anjuta_async_command_notification_poll (AnjutaCommand *command)
86 {
87 	AnjutaAsyncCommand *self;
88 
89 	self = ANJUTA_ASYNC_COMMAND (command);
90 
91 	if (self->priv->new_data_arrived &&
92 		g_mutex_trylock (&self->priv->mutex))
93 	{
94 		g_signal_emit_by_name (command, "data-arrived");
95 		g_mutex_unlock (&self->priv->mutex);
96 		self->priv->new_data_arrived = FALSE;
97 	}
98 
99 	if (self->priv->progress_changed)
100 	{
101 		g_signal_emit_by_name (command, "progress", self->priv->progress);
102 		self->priv->progress_changed = FALSE;
103 	}
104 
105 	if (self->priv->complete)
106 	{
107 		g_signal_emit_by_name (command, "command-finished",
108 							   self->priv->return_code);
109 		return FALSE;
110 	}
111 	else
112 		return TRUE;
113 
114 }
115 
116 static gpointer
anjuta_async_command_thread(AnjutaCommand * command)117 anjuta_async_command_thread (AnjutaCommand *command)
118 {
119 	guint return_code;
120 
121 	return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
122 	anjuta_command_notify_complete (command, return_code);
123 
124 	g_object_unref (command);
125 	return NULL;
126 }
127 
128 static void
start_command(AnjutaCommand * command)129 start_command (AnjutaCommand *command)
130 {
131 	g_idle_add ((GSourceFunc) anjuta_async_command_notification_poll,
132 				command);
133 	g_thread_new ("AnjutaCommand Thread",
134 	              (GThreadFunc) anjuta_async_command_thread, g_object_ref (command));
135 }
136 
137 static void
notify_data_arrived(AnjutaCommand * command)138 notify_data_arrived (AnjutaCommand *command)
139 {
140 	AnjutaAsyncCommand *self;
141 
142 	self = ANJUTA_ASYNC_COMMAND (command);
143 
144 	self->priv->new_data_arrived = TRUE;
145 }
146 
147 static void
notify_complete(AnjutaCommand * command,guint return_code)148 notify_complete (AnjutaCommand *command, guint return_code)
149 {
150 	AnjutaAsyncCommand *self;
151 
152 	self = ANJUTA_ASYNC_COMMAND (command);
153 
154 	self->priv->complete = TRUE;
155 	self->priv->return_code = return_code;
156 }
157 
158 static void
notify_progress(AnjutaCommand * command,gfloat progress)159 notify_progress (AnjutaCommand *command, gfloat progress)
160 {
161 	AnjutaAsyncCommand *self;
162 
163 	self = ANJUTA_ASYNC_COMMAND (command);
164 
165 	self->priv->progress_changed = TRUE;
166 	self->priv->progress = progress;
167 }
168 
169 static void
anjuta_async_command_class_init(AnjutaAsyncCommandClass * klass)170 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
171 {
172 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
173 	AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
174 
175 	object_class->finalize = anjuta_async_command_finalize;
176 
177 	parent_class->start = start_command;
178 	parent_class->notify_data_arrived = notify_data_arrived;
179 	parent_class->notify_complete = notify_complete;
180 	parent_class->notify_progress = notify_progress;
181 }
182 
183 /**
184  * anjuta_async_command_set_error_message:
185  * @command: AnjutaAsyncCommand object
186  * @error_message: The error message that should be used
187  *
188  * Set the error message this async command resulted in
189  */
190 void
anjuta_async_command_set_error_message(AnjutaCommand * command,const gchar * error_message)191 anjuta_async_command_set_error_message (AnjutaCommand *command,
192 										const gchar *error_message)
193 {
194 	anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
195 	ANJUTA_COMMAND_GET_CLASS (command)->set_error_message (command,
196 														   error_message);
197 	anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
198 }
199 
200 /**
201  * anjuta_async_command_get_error_message:
202  * @command: AnjutaAsyncCommand object
203  *
204  * Returns: The error message the async command resulted in. The caller
205  * should free the string
206  */
207 gchar *
anjuta_async_command_get_error_message(AnjutaCommand * command)208 anjuta_async_command_get_error_message (AnjutaCommand *command)
209 {
210 	gchar *error_message;
211 
212 	anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
213 	error_message = ANJUTA_COMMAND_GET_CLASS (command)->get_error_message (command);
214 	anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
215 
216 	return error_message;
217 }
218 
219 /**
220  * anjuta_async_command_lock:
221  * @self: AnjutaAsyncCommand object.
222  *
223  * Locks the command's built-in mutex.
224  */
225 void
anjuta_async_command_lock(AnjutaAsyncCommand * self)226 anjuta_async_command_lock (AnjutaAsyncCommand *self)
227 {
228 	g_mutex_lock (&self->priv->mutex);
229 }
230 
231 /**
232  * anjuta_async_command_unlock:
233  * @self: AnjutaAsyncCommand object.
234  *
235  * Unlocks the command's built-in mutex.
236  */
237 void
anjuta_async_command_unlock(AnjutaAsyncCommand * self)238 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
239 {
240 	g_mutex_unlock (&self->priv->mutex);
241 }
242