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
;
36 gboolean progress_changed
;
40 G_DEFINE_TYPE (AnjutaAsyncCommand
, anjuta_async_command
, ANJUTA_TYPE_COMMAND
);
43 anjuta_async_command_init (AnjutaAsyncCommand
*self
)
45 self
->priv
= g_new0 (AnjutaAsyncCommandPriv
, 1);
47 self
->priv
->mutex
= g_mutex_new ();
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
);
62 G_OBJECT_CLASS (anjuta_async_command_parent_class
)->finalize (object
);
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
);
98 anjuta_async_command_thread (AnjutaCommand
*command
)
102 return_code
= ANJUTA_COMMAND_GET_CLASS (command
)->run (command
);
103 anjuta_command_notify_complete (command
, return_code
);
108 start_command (AnjutaCommand
*command
)
110 g_idle_add ((GSourceFunc
) anjuta_async_command_notification_poll
,
112 g_thread_create ((GThreadFunc
) anjuta_async_command_thread
,
113 command
, FALSE
, NULL
);
117 notify_data_arrived (AnjutaCommand
*command
)
119 AnjutaAsyncCommand
*self
;
121 self
= ANJUTA_ASYNC_COMMAND (command
);
123 self
->priv
->new_data_arrived
= TRUE
;
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
;
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
;
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
;
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
,
169 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command
));
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
;
185 anjuta_async_command_lock (AnjutaAsyncCommand
*self
)
187 g_mutex_lock (self
->priv
->mutex
);
191 anjuta_async_command_unlock (AnjutaAsyncCommand
*self
)
193 g_mutex_unlock (self
->priv
->mutex
);