1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
6 * anjuta is free software.
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)
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"
28 * SECTION: anjuta-command
29 * @short_description: System for creating objects that provide a standard
30 * interface to external components (libraries, processes,
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
55 struct _AnjutaCommandPriv
70 static guint anjuta_command_signals
[LAST_SIGNAL
] = { 0 };
72 G_DEFINE_TYPE (AnjutaCommand
, anjuta_command
, G_TYPE_OBJECT
);
75 anjuta_command_init (AnjutaCommand
*self
)
77 self
->priv
= g_new0 (AnjutaCommandPriv
, 1);
81 anjuta_command_finalize (GObject
*object
)
85 self
= ANJUTA_COMMAND (object
);
87 g_free (self
->priv
->error_message
);
90 G_OBJECT_CLASS (anjuta_command_parent_class
)->finalize (object
);
94 anjuta_command_class_init (AnjutaCommandClass
*klass
)
96 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
98 object_class
->finalize
= anjuta_command_finalize
;
102 klass
->cancel
= NULL
;
103 klass
->notify_data_arrived
= NULL
;
104 klass
->notify_complete
= NULL
;
105 klass
->notify_progress
= NULL
;
106 klass
->set_error_message
= anjuta_command_set_error_message
;
107 klass
->get_error_message
= anjuta_command_get_error_message
;
108 klass
->progress
= NULL
;
111 * AnjutaCommand::data-arrived:
114 * Notifies clients that the command has processed data that is ready to
117 anjuta_command_signals
[DATA_ARRIVED
] =
118 g_signal_new ("data-arrived",
119 G_OBJECT_CLASS_TYPE (klass
),
123 g_cclosure_marshal_VOID__VOID
,
128 * AnjutaCommand::command-finished:
130 * @return_code: The return code of the finished commmand
132 * Indicates that the command has completed. Clients should at least handle
133 * this signal to unref the command object.
135 anjuta_command_signals
[COMMAND_FINISHED
] =
136 g_signal_new ("command-finished",
137 G_OBJECT_CLASS_TYPE (klass
),
141 g_cclosure_marshal_VOID__UINT
,
147 * AnjutaCommand::progress:
149 * @progress: Fraction of the command's task that is complete, between 0.0
150 * and 1.0, inclusive.
152 * Notifies clients of changes in progress during command execution.
154 anjuta_command_signals
[PROGRESS
] =
155 g_signal_new ("progress",
156 G_OBJECT_CLASS_TYPE (klass
),
158 G_STRUCT_OFFSET (AnjutaCommandClass
, progress
),
160 g_cclosure_marshal_VOID__FLOAT
,
166 * anjuta_command_start:
167 * @self: Command object to start
169 * Starts a command. Client code can handle data from the command by connecting
170 * to the ::data-arrived signal.
172 * #AnjutaCommand subclasses should override this method to determine how they
173 * call ::run, which actually does the command's legwork.
176 anjuta_command_start (AnjutaCommand
*self
)
178 ANJUTA_COMMAND_GET_CLASS (self
)->start (self
);
182 * anjuta_command_cancel:
183 * @self: Command object.
185 * Cancels a running command.
188 anjuta_command_cancel (AnjutaCommand
*self
)
190 ANJUTA_COMMAND_GET_CLASS (self
)->cancel (self
);
194 * anjuta_command_notify_data_arrived:
195 * @self: Command object.
197 * Used by base classes derived from #AnjutaCommand to emit the ::data-arrived
198 * signal. This method should be used by both base command classes and
199 * non-base classes as appropriate.
202 anjuta_command_notify_data_arrived (AnjutaCommand
*self
)
204 ANJUTA_COMMAND_GET_CLASS (self
)->notify_data_arrived (self
);
208 * anjuta_command_notify_complete:
209 * @self: Command object.
211 * Used by base classes derived from #AnjutaCommand to emit the
212 * ::command-finished signal. This method should not be used by client code or
213 * #AnjutaCommand objects that are not base classes.
216 anjuta_command_notify_complete (AnjutaCommand
*self
, guint return_code
)
218 ANJUTA_COMMAND_GET_CLASS (self
)->notify_complete (self
, return_code
);
222 * anjuta_command_notify_progress:
223 * @self: Command object.
225 * Emits the ::progress signal. Can be used by both base classes and
226 * commands as needed.
229 anjuta_command_notify_progress (AnjutaCommand
*self
, gfloat progress
)
231 ANJUTA_COMMAND_GET_CLASS (self
)->notify_progress (self
, progress
);
235 * anjuta_command_set_error_message:
236 * @self: Command object.
237 * @error_message: Error message.
239 * Command objects use this to set error messages when they encounter some kind
243 anjuta_command_set_error_message (AnjutaCommand
*self
, gchar
*error_message
)
245 if (self
->priv
->error_message
)
246 g_free (error_message
);
248 self
->priv
->error_message
= g_strdup (error_message
);
252 * anjuta_command_get_error_message:
253 * @self: Command object.
255 * Get the error message from the command, if there is one. This method is
256 * normally used from a ::command-finished handler to report errors to the user
257 * when a command finishes.
259 * Return value: Error message string that must be freed when no longer needed.
260 * If no error is set, return %NULL.
263 anjuta_command_get_error_message (AnjutaCommand
*self
)
265 return g_strdup (self
->priv
->error_message
);