1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
6 * Portions based on the original Subversion plugin
7 * Copyright (C) Johannes Schmid 2005
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)
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
35 gboolean new_data_arrived
;
38 G_DEFINE_TYPE (AnjutaAsyncCommand
, anjuta_async_command
, ANJUTA_TYPE_COMMAND
);
41 anjuta_async_command_init (AnjutaAsyncCommand
*self
)
43 self
->priv
= g_new0 (AnjutaAsyncCommandPriv
, 1);
45 self
->priv
->mutex
= g_mutex_new ();
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
);
60 G_OBJECT_CLASS (anjuta_async_command_parent_class
)->finalize (object
);
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
);
90 anjuta_async_command_thread (AnjutaCommand
*command
)
94 return_code
= ANJUTA_COMMAND_GET_CLASS (command
)->run (command
);
95 anjuta_command_notify_complete (command
, return_code
);
100 start_command (AnjutaCommand
*command
)
102 g_idle_add ((GSourceFunc
) anjuta_async_command_notification_poll
,
104 g_thread_create ((GThreadFunc
) anjuta_async_command_thread
,
105 command
, FALSE
, NULL
);
109 notify_data_arrived (AnjutaCommand
*command
)
111 AnjutaAsyncCommand
*self
;
113 self
= ANJUTA_ASYNC_COMMAND (command
);
115 self
->priv
->new_data_arrived
= TRUE
;
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
;
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
;
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
,
149 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command
));
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
;
165 anjuta_async_command_lock (AnjutaAsyncCommand
*self
)
167 g_mutex_lock (self
->priv
->mutex
);
171 anjuta_async_command_unlock (AnjutaAsyncCommand
*self
)
173 g_mutex_unlock (self
->priv
->mutex
);