Updated Spanish translation
[anjuta-git-plugin.git] / libanjuta / anjuta-async-command.c
blobd4dc152bb655110994bef0f3370784025b4fbe17
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 struct _AnjutaAsyncCommandPriv
32 GMutex *mutex;
33 guint return_code;
34 gboolean complete;
35 gboolean new_data_arrived;
38 G_DEFINE_TYPE (AnjutaAsyncCommand, anjuta_async_command, ANJUTA_TYPE_COMMAND);
40 static void
41 anjuta_async_command_init (AnjutaAsyncCommand *self)
43 self->priv = g_new0 (AnjutaAsyncCommandPriv, 1);
45 self->priv->mutex = g_mutex_new ();
48 static void
49 anjuta_async_command_finalize (GObject *object)
51 AnjutaAsyncCommand *self;
53 self = ANJUTA_ASYNC_COMMAND (object);
55 g_mutex_free (self->priv->mutex);
56 g_idle_remove_by_data (self);
58 g_free (self->priv);
60 G_OBJECT_CLASS (anjuta_async_command_parent_class)->finalize (object);
63 static gboolean
64 anjuta_async_command_notification_poll (AnjutaCommand *command)
66 AnjutaAsyncCommand *self;
68 self = ANJUTA_ASYNC_COMMAND (command);
70 if (self->priv->new_data_arrived &&
71 g_mutex_trylock (self->priv->mutex))
73 g_signal_emit_by_name (command, "data-arrived");
74 g_mutex_unlock (self->priv->mutex);
75 self->priv->new_data_arrived = FALSE;
78 if (self->priv->complete)
80 g_signal_emit_by_name (command, "command-finished",
81 self->priv->return_code);
82 return FALSE;
84 else
85 return TRUE;
89 static gpointer
90 anjuta_async_command_thread (AnjutaCommand *command)
92 guint return_code;
94 return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
95 anjuta_command_notify_complete (command, return_code);
96 return NULL;
99 static void
100 start_command (AnjutaCommand *command)
102 g_idle_add ((GSourceFunc) anjuta_async_command_notification_poll,
103 command);
104 g_thread_create ((GThreadFunc) anjuta_async_command_thread,
105 command, FALSE, NULL);
108 static void
109 notify_data_arrived (AnjutaCommand *command)
111 AnjutaAsyncCommand *self;
113 self = ANJUTA_ASYNC_COMMAND (command);
115 self->priv->new_data_arrived = TRUE;
118 static void
119 notify_complete (AnjutaCommand *command, guint return_code)
121 AnjutaAsyncCommand *self;
123 self = ANJUTA_ASYNC_COMMAND (command);
125 self->priv->complete = TRUE;
126 self->priv->return_code = return_code;
129 static void
130 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
132 GObjectClass* object_class = G_OBJECT_CLASS (klass);
133 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
135 object_class->finalize = anjuta_async_command_finalize;
137 parent_class->start = start_command;
138 parent_class->notify_data_arrived = notify_data_arrived;
139 parent_class->notify_complete = notify_complete;
142 void
143 anjuta_async_command_set_error_message (AnjutaCommand *command,
144 gchar *error_message)
146 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
147 ANJUTA_COMMAND_GET_CLASS (command)->set_error_message (command,
148 error_message);
149 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
152 gchar *
153 anjuta_async_command_get_error_message (AnjutaCommand *command)
155 gchar *error_message;
157 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
158 error_message = ANJUTA_COMMAND_GET_CLASS (command)->get_error_message (command);
159 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
161 return error_message;
164 void
165 anjuta_async_command_lock (AnjutaAsyncCommand *self)
167 g_mutex_lock (self->priv->mutex);
170 void
171 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
173 g_mutex_unlock (self->priv->mutex);