Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / libanjuta / anjuta-async-command.c
blob1b46fb2a80e0ce7d7befcef30a700a1ab52161b2
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 self->priv->mutex = g_mutex_new ();
69 static void
70 anjuta_async_command_finalize (GObject *object)
72 AnjutaAsyncCommand *self;
74 self = ANJUTA_ASYNC_COMMAND (object);
76 g_mutex_free (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);
123 return NULL;
126 static void
127 start_command (AnjutaCommand *command)
129 g_idle_add ((GSourceFunc) anjuta_async_command_notification_poll,
130 command);
131 g_thread_create ((GThreadFunc) anjuta_async_command_thread,
132 command, FALSE, NULL);
135 static void
136 notify_data_arrived (AnjutaCommand *command)
138 AnjutaAsyncCommand *self;
140 self = ANJUTA_ASYNC_COMMAND (command);
142 self->priv->new_data_arrived = TRUE;
145 static void
146 notify_complete (AnjutaCommand *command, guint return_code)
148 AnjutaAsyncCommand *self;
150 self = ANJUTA_ASYNC_COMMAND (command);
152 self->priv->complete = TRUE;
153 self->priv->return_code = return_code;
156 static void
157 notify_progress (AnjutaCommand *command, gfloat progress)
159 AnjutaAsyncCommand *self;
161 self = ANJUTA_ASYNC_COMMAND (command);
163 self->priv->progress_changed = TRUE;
164 self->priv->progress = progress;
167 static void
168 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
170 GObjectClass* object_class = G_OBJECT_CLASS (klass);
171 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
173 object_class->finalize = anjuta_async_command_finalize;
175 parent_class->start = start_command;
176 parent_class->notify_data_arrived = notify_data_arrived;
177 parent_class->notify_complete = notify_complete;
178 parent_class->notify_progress = notify_progress;
181 void
182 anjuta_async_command_set_error_message (AnjutaCommand *command,
183 gchar *error_message)
185 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
186 ANJUTA_COMMAND_GET_CLASS (command)->set_error_message (command,
187 error_message);
188 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
191 gchar *
192 anjuta_async_command_get_error_message (AnjutaCommand *command)
194 gchar *error_message;
196 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
197 error_message = ANJUTA_COMMAND_GET_CLASS (command)->get_error_message (command);
198 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
200 return error_message;
204 * anjuta_async_command_lock:
205 * @self: AnjutaAsyncCommand object.
207 * Locks the command's built-in mutex.
209 void
210 anjuta_async_command_lock (AnjutaAsyncCommand *self)
212 g_mutex_lock (self->priv->mutex);
216 * anjuta_async_command_unlock:
217 * @self: AnjutaAsyncCommand object.
219 * Unlocks the command's built-in mutex.
221 void
222 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
224 g_mutex_unlock (self->priv->mutex);