Updated Spanish translation
[anjuta.git] / libanjuta / anjuta-async-command.c
blobe39fcd5d5dae879c61d2ba0b3f8cf3e12a22128c
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>
6 * Portions based on the original Subversion plugin
7 * Copyright (C) Johannes Schmid 2005
8 *
9 * anjuta is free software.
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.
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.
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.
28 #include "anjuta-async-command.h"
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
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.
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.
46 * For an example of how #AnjutaAsyncCommand is used, see the Subversion plugin.
49 struct _AnjutaAsyncCommandPriv
51 GMutex mutex;
52 guint return_code;
53 gboolean complete;
54 gboolean new_data_arrived;
55 gboolean progress_changed;
56 gfloat progress;
59 G_DEFINE_TYPE (AnjutaAsyncCommand, anjuta_async_command, ANJUTA_TYPE_COMMAND);
61 static void
62 anjuta_async_command_init (AnjutaAsyncCommand *self)
64 self->priv = g_new0 (AnjutaAsyncCommandPriv, 1);
66 g_mutex_init (&self->priv->mutex);
69 static void
70 anjuta_async_command_finalize (GObject *object)
72 AnjutaAsyncCommand *self;
74 self = ANJUTA_ASYNC_COMMAND (object);
76 g_mutex_clear (&self->priv->mutex);
77 g_idle_remove_by_data (self);
79 g_free (self->priv);
81 G_OBJECT_CLASS (anjuta_async_command_parent_class)->finalize (object);
84 static gboolean
85 anjuta_async_command_notification_poll (AnjutaCommand *command)
87 AnjutaAsyncCommand *self;
89 self = ANJUTA_ASYNC_COMMAND (command);
91 if (self->priv->new_data_arrived &&
92 g_mutex_trylock (&self->priv->mutex))
94 g_signal_emit_by_name (command, "data-arrived");
95 g_mutex_unlock (&self->priv->mutex);
96 self->priv->new_data_arrived = FALSE;
99 if (self->priv->progress_changed)
101 g_signal_emit_by_name (command, "progress", self->priv->progress);
102 self->priv->progress_changed = FALSE;
105 if (self->priv->complete)
107 g_signal_emit_by_name (command, "command-finished",
108 self->priv->return_code);
109 return FALSE;
111 else
112 return TRUE;
116 static gpointer
117 anjuta_async_command_thread (AnjutaCommand *command)
119 guint return_code;
121 return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
122 anjuta_command_notify_complete (command, return_code);
124 g_object_unref (command);
125 return NULL;
128 static void
129 start_command (AnjutaCommand *command)
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));
137 static void
138 notify_data_arrived (AnjutaCommand *command)
140 AnjutaAsyncCommand *self;
142 self = ANJUTA_ASYNC_COMMAND (command);
144 self->priv->new_data_arrived = TRUE;
147 static void
148 notify_complete (AnjutaCommand *command, guint return_code)
150 AnjutaAsyncCommand *self;
152 self = ANJUTA_ASYNC_COMMAND (command);
154 self->priv->complete = TRUE;
155 self->priv->return_code = return_code;
158 static void
159 notify_progress (AnjutaCommand *command, gfloat progress)
161 AnjutaAsyncCommand *self;
163 self = ANJUTA_ASYNC_COMMAND (command);
165 self->priv->progress_changed = TRUE;
166 self->priv->progress = progress;
169 static void
170 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
172 GObjectClass* object_class = G_OBJECT_CLASS (klass);
173 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
175 object_class->finalize = anjuta_async_command_finalize;
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;
184 * anjuta_async_command_set_error_message:
185 * @command: AnjutaAsyncCommand object
186 * @error_message: The error message that should be used
188 * Set the error message this async command resulted in
190 void
191 anjuta_async_command_set_error_message (AnjutaCommand *command,
192 const gchar *error_message)
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));
201 * anjuta_async_command_get_error_message:
202 * @command: AnjutaAsyncCommand object
204 * Returns: The error message the async command resulted in. The caller
205 * should free the string
207 gchar *
208 anjuta_async_command_get_error_message (AnjutaCommand *command)
210 gchar *error_message;
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));
216 return error_message;
220 * anjuta_async_command_lock:
221 * @self: AnjutaAsyncCommand object.
223 * Locks the command's built-in mutex.
225 void
226 anjuta_async_command_lock (AnjutaAsyncCommand *self)
228 g_mutex_lock (&self->priv->mutex);
232 * anjuta_async_command_unlock:
233 * @self: AnjutaAsyncCommand object.
235 * Unlocks the command's built-in mutex.
237 void
238 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
240 g_mutex_unlock (&self->priv->mutex);