Add a progress signal to AnjutaCommand and its subclasses in libanjuta
[anjuta-git-plugin.git] / libanjuta / anjuta-async-command.c
blobbd675d99ee215e0349858cb95d6b0034b9d736fc
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;
36 gboolean progress_changed;
37 gfloat progress;
40 G_DEFINE_TYPE (AnjutaAsyncCommand, anjuta_async_command, ANJUTA_TYPE_COMMAND);
42 static void
43 anjuta_async_command_init (AnjutaAsyncCommand *self)
45 self->priv = g_new0 (AnjutaAsyncCommandPriv, 1);
47 self->priv->mutex = g_mutex_new ();
50 static void
51 anjuta_async_command_finalize (GObject *object)
53 AnjutaAsyncCommand *self;
55 self = ANJUTA_ASYNC_COMMAND (object);
57 g_mutex_free (self->priv->mutex);
58 g_idle_remove_by_data (self);
60 g_free (self->priv);
62 G_OBJECT_CLASS (anjuta_async_command_parent_class)->finalize (object);
65 static gboolean
66 anjuta_async_command_notification_poll (AnjutaCommand *command)
68 AnjutaAsyncCommand *self;
70 self = ANJUTA_ASYNC_COMMAND (command);
72 if (self->priv->new_data_arrived &&
73 g_mutex_trylock (self->priv->mutex))
75 g_signal_emit_by_name (command, "data-arrived");
76 g_mutex_unlock (self->priv->mutex);
77 self->priv->new_data_arrived = FALSE;
80 if (self->priv->progress_changed)
82 g_signal_emit_by_name (command, "progress", self->priv->progress);
83 self->priv->progress_changed = FALSE;
86 if (self->priv->complete)
88 g_signal_emit_by_name (command, "command-finished",
89 self->priv->return_code);
90 return FALSE;
92 else
93 return TRUE;
97 static gpointer
98 anjuta_async_command_thread (AnjutaCommand *command)
100 guint return_code;
102 return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
103 anjuta_command_notify_complete (command, return_code);
104 return NULL;
107 static void
108 start_command (AnjutaCommand *command)
110 g_idle_add ((GSourceFunc) anjuta_async_command_notification_poll,
111 command);
112 g_thread_create ((GThreadFunc) anjuta_async_command_thread,
113 command, FALSE, NULL);
116 static void
117 notify_data_arrived (AnjutaCommand *command)
119 AnjutaAsyncCommand *self;
121 self = ANJUTA_ASYNC_COMMAND (command);
123 self->priv->new_data_arrived = TRUE;
126 static void
127 notify_complete (AnjutaCommand *command, guint return_code)
129 AnjutaAsyncCommand *self;
131 self = ANJUTA_ASYNC_COMMAND (command);
133 self->priv->complete = TRUE;
134 self->priv->return_code = return_code;
137 static void
138 notify_progress (AnjutaCommand *command, gfloat progress)
140 AnjutaAsyncCommand *self;
142 self = ANJUTA_ASYNC_COMMAND (command);
144 self->priv->progress_changed = TRUE;
145 self->priv->progress = progress;
148 static void
149 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
151 GObjectClass* object_class = G_OBJECT_CLASS (klass);
152 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
154 object_class->finalize = anjuta_async_command_finalize;
156 parent_class->start = start_command;
157 parent_class->notify_data_arrived = notify_data_arrived;
158 parent_class->notify_complete = notify_complete;
159 parent_class->notify_progress = notify_progress;
162 void
163 anjuta_async_command_set_error_message (AnjutaCommand *command,
164 gchar *error_message)
166 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
167 ANJUTA_COMMAND_GET_CLASS (command)->set_error_message (command,
168 error_message);
169 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
172 gchar *
173 anjuta_async_command_get_error_message (AnjutaCommand *command)
175 gchar *error_message;
177 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
178 error_message = ANJUTA_COMMAND_GET_CLASS (command)->get_error_message (command);
179 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
181 return error_message;
184 void
185 anjuta_async_command_lock (AnjutaAsyncCommand *self)
187 g_mutex_lock (self->priv->mutex);
190 void
191 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
193 g_mutex_unlock (self->priv->mutex);