libanjuta: set the info for the default value of a property
[anjuta.git] / libanjuta / anjuta-command.c
blob7c83c3fe3bed12faa52641d42bb739e1c2cf4758
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>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "anjuta-command.h"
27 /**
28 * SECTION: anjuta-command
29 * @short_description: System for creating objects that provide a standard
30 * interface to external components (libraries, processes,
31 * etc.)
32 * @see_also: #AnjutaAsyncCommand, #AnjutaSyncCommand
33 * @include libanjuta/anjuta-command.h
35 * #AnjutaCommand is the base class for objects that are designed to provide
36 * a layer of abstraction between UI code and some other component, like a
37 * library or child process. AnjutaCommand provides a simple and consistent
38 * interface for plugins to interact with these components without needing
39 * to concern themselves with the exact details of how these components work.
41 * To create command objects, plugins derive them from an #AnjutaCommand
42 * subclass like #AnjutaAsyncCommand, which runs commands in another thread or
43 * #AnjutaSyncCommand, which runs commands synchronously.
45 * These classes determine how ::run is called and how signals are emitted.
46 * ::run is responsible for actually doing the work of the command. It is the
47 * responsiblity of the command object that does a certain task to implement
48 * ::run to do its job. Everything else is normally implemented by its parent
49 * classes at this point
51 * For an example of how to use #AnjutaCommand, see the Subversion and Git
52 * plugins.
55 struct _AnjutaCommandPriv
57 gboolean running;
58 gchar *error_message;
61 enum
63 DATA_ARRIVED,
64 COMMAND_STARTED,
65 COMMAND_FINISHED,
66 PROGRESS,
68 LAST_SIGNAL
72 static guint anjuta_command_signals[LAST_SIGNAL] = { 0 };
74 G_DEFINE_TYPE (AnjutaCommand, anjuta_command, G_TYPE_OBJECT);
76 static void
77 anjuta_command_init (AnjutaCommand *self)
79 self->priv = g_new0 (AnjutaCommandPriv, 1);
82 static void
83 anjuta_command_finalize (GObject *object)
85 AnjutaCommand *self;
87 self = ANJUTA_COMMAND (object);
89 g_free (self->priv->error_message);
90 g_free (self->priv);
92 G_OBJECT_CLASS (anjuta_command_parent_class)->finalize (object);
95 static gboolean
96 start_automatic_monitor (AnjutaCommand *self)
98 return FALSE;
101 static void
102 stop_automatic_monitor (AnjutaCommand *self)
106 static void
107 data_arrived (AnjutaCommand *command)
111 static void
112 command_started (AnjutaCommand *command)
114 command->priv->running = TRUE;
117 static void
118 command_finished (AnjutaCommand *command, guint return_code)
120 command->priv->running = FALSE;
123 static void
124 progress (AnjutaCommand *command, gfloat progress)
128 static void
129 anjuta_command_class_init (AnjutaCommandClass *klass)
131 GObjectClass* object_class = G_OBJECT_CLASS (klass);
133 object_class->finalize = anjuta_command_finalize;
135 klass->run = NULL;
136 klass->start = NULL;
137 klass->cancel = NULL;
138 klass->notify_data_arrived = NULL;
139 klass->notify_complete = NULL;
140 klass->notify_progress = NULL;
141 klass->set_error_message = anjuta_command_set_error_message;
142 klass->get_error_message = anjuta_command_get_error_message;
143 klass->start_automatic_monitor = start_automatic_monitor;
144 klass->stop_automatic_monitor = stop_automatic_monitor;
145 klass->data_arrived = data_arrived;
146 klass->command_started = command_started;
147 klass->command_finished = command_finished;
148 klass->progress = progress;
151 * AnjutaCommand::data-arrived:
152 * @command: Command
154 * Notifies clients that the command has processed data that is ready to
155 * be used.
157 anjuta_command_signals[DATA_ARRIVED] =
158 g_signal_new ("data-arrived",
159 G_OBJECT_CLASS_TYPE (klass),
160 G_SIGNAL_RUN_LAST,
161 G_STRUCT_OFFSET (AnjutaCommandClass, data_arrived),
162 NULL, NULL,
163 g_cclosure_marshal_VOID__VOID,
164 G_TYPE_NONE,
168 * AnjuaCommand::command-started:
169 * @command: Command
171 * Indicates that a command has begun executing. This signal is intended to
172 * be used for commands that start themselves automatically.
174 * <note>
175 * <para>
176 * Sublasses that override the method for this signal should chain up to
177 * the parent implementation to ensure proper handling of running/not
178 * running states.
179 * </para>
180 * </note>
182 anjuta_command_signals[COMMAND_STARTED] =
183 g_signal_new ("command-started",
184 G_OBJECT_CLASS_TYPE (klass),
185 G_SIGNAL_RUN_FIRST,
186 G_STRUCT_OFFSET (AnjutaCommandClass, command_started),
187 NULL, NULL,
188 g_cclosure_marshal_VOID__VOID,
189 G_TYPE_NONE,
193 * AnjutaCommand::command-finished:
194 * @command: Command
195 * @return_code: The return code of the finished commmand
197 * Indicates that the command has completed. Clients should at least handle
198 * this signal to unref the command object.
200 * <note>
201 * <para>
202 * Sublasses that override the method for this signal should chain up to
203 * the parent implementation to ensure proper handling of running/not
204 * running states.
205 * </para>
206 * </note>
208 anjuta_command_signals[COMMAND_FINISHED] =
209 g_signal_new ("command-finished",
210 G_OBJECT_CLASS_TYPE (klass),
211 G_SIGNAL_RUN_FIRST,
212 G_STRUCT_OFFSET (AnjutaCommandClass, command_finished),
213 NULL, NULL,
214 g_cclosure_marshal_VOID__UINT ,
215 G_TYPE_NONE, 1,
216 G_TYPE_UINT);
220 * AnjutaCommand::progress:
221 * @command: Command
222 * @progress: Fraction of the command's task that is complete, between 0.0
223 * and 1.0, inclusive.
225 * Notifies clients of changes in progress during command execution.
227 anjuta_command_signals[PROGRESS] =
228 g_signal_new ("progress",
229 G_OBJECT_CLASS_TYPE (klass),
230 G_SIGNAL_RUN_FIRST,
231 G_STRUCT_OFFSET (AnjutaCommandClass, progress),
232 NULL, NULL,
233 g_cclosure_marshal_VOID__FLOAT ,
234 G_TYPE_NONE, 1,
235 G_TYPE_FLOAT);
239 * anjuta_command_start:
240 * @self: Command object to start
242 * Starts a command. Client code can handle data from the command by connecting
243 * to the ::data-arrived signal.
245 * #AnjutaCommand subclasses should override this method to determine how they
246 * call ::run, which actually does the command's legwork.
248 void
249 anjuta_command_start (AnjutaCommand *self)
251 if (!self->priv->running)
253 g_signal_emit_by_name (self, "command-started");
255 ANJUTA_COMMAND_GET_CLASS (self)->start (self);
260 * anjuta_command_cancel:
261 * @self: Command object.
263 * Cancels a running command.
265 void
266 anjuta_command_cancel (AnjutaCommand *self)
268 ANJUTA_COMMAND_GET_CLASS (self)->cancel (self);
272 * anjuta_command_notify_data_arrived:
273 * @self: Command object.
275 * Used by base classes derived from #AnjutaCommand to emit the ::data-arrived
276 * signal. This method should be used by both base command classes and
277 * non-base classes as appropriate.
279 void
280 anjuta_command_notify_data_arrived (AnjutaCommand *self)
282 ANJUTA_COMMAND_GET_CLASS (self)->notify_data_arrived (self);
286 * anjuta_command_notify_complete:
287 * @self: Command object
288 * @return_code: The returned code that is passed to the notify callback
290 * Used by base classes derived from #AnjutaCommand to emit the
291 * ::command-finished signal. This method should not be used by client code or
292 * #AnjutaCommand objects that are not base classes.
294 void
295 anjuta_command_notify_complete (AnjutaCommand *self, guint return_code)
297 ANJUTA_COMMAND_GET_CLASS (self)->notify_complete (self, return_code);
301 * anjuta_command_notify_progress:
302 * @self: Command object.
303 * @progress: The of the command that is passed to the notify callback
305 * Emits the ::progress signal. Can be used by both base classes and
306 * commands as needed.
308 void
309 anjuta_command_notify_progress (AnjutaCommand *self, gfloat progress)
311 ANJUTA_COMMAND_GET_CLASS (self)->notify_progress (self, progress);
315 * anjuta_command_is_running:
316 * @self: Command object.
318 * Return value: %TRUE if the command is currently running; %FALSE otherwise.
320 gboolean
321 anjuta_command_is_running (AnjutaCommand *self)
323 return self->priv->running;
327 * anjuta_command_set_error_message:
328 * @self: Command object.
329 * @error_message: Error message.
331 * Command objects use this to set error messages when they encounter some kind
332 * of failure.
334 void
335 anjuta_command_set_error_message (AnjutaCommand *self, const gchar *error_message)
337 g_free (self->priv->error_message);
339 self->priv->error_message = g_strdup (error_message);
343 * anjuta_command_get_error_message:
344 * @self: Command object.
346 * Get the error message from the command, if there is one. This method is
347 * normally used from a ::command-finished handler to report errors to the user
348 * when a command finishes.
350 * Return value: Error message string that must be freed when no longer needed.
351 * If no error is set, return %NULL.
353 gchar *
354 anjuta_command_get_error_message (AnjutaCommand *self)
356 return g_strdup (self->priv->error_message);
360 * anjuta_command_start_automatic_monitor:
361 * @self: Command object.
363 * Sets up any monitoring needed for commands that should start themselves
364 * automatically in response to some event.
366 * Return value: %TRUE if automatic starting is supported by the command and
367 * no errors were encountered; %FALSE if automatic starting is unsupported or on
368 * error.
370 gboolean
371 anjuta_command_start_automatic_monitor (AnjutaCommand *self)
373 return ANJUTA_COMMAND_GET_CLASS (self)->start_automatic_monitor (self);
377 * anjuta_command_stop_automatic_monitor:
378 * @self: Command object.
380 * Stops automatic monitoring for self executing commands. For commands that
381 * do not support self-starting, this function does nothing.
383 void
384 anjuta_command_stop_automatic_monitor (AnjutaCommand *self)
386 ANJUTA_COMMAND_GET_CLASS (self)->stop_automatic_monitor (self);