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.
12 *
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.
17 *
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.
23 */
24
25 #include "anjuta-sync-command.h"
26
27 /**
28 * SECTION: anjuta-sync-command
29 * @short_description: #AnjutaCommand subclass that serves as the base for
30 * commands that run synchronously.
31 * @include: libanjuta/anjuta-sync-command.h
32 *
33 * #AnjutaSyncCommand allows plugins to abstract the work of tasks that do not
34 * need to be run in another thread. This class can provide a base for
35 * abstratraction between client code and asynchronous facilities such as
36 * #AnjutaLauncher or GIO, and is especially useful when complicated tasks
37 * are being performed.
38 *
39 * #AnjutaSyncCommand simply calls ::run directly from ::start, and emits the
40 * command-finished signal as soon as it returns.
41 *
42 * For an example of how #AnjutaSyncCommand is used, see the Git plugin.
43 */
44
45 G_DEFINE_TYPE (AnjutaSyncCommand, anjuta_sync_command, ANJUTA_TYPE_COMMAND);
46
47 static void
anjuta_sync_command_init(AnjutaSyncCommand * self)48 anjuta_sync_command_init (AnjutaSyncCommand *self)
49 {
50
51 }
52
53 static void
anjuta_sync_command_finalize(GObject * object)54 anjuta_sync_command_finalize (GObject *object)
55 {
56 G_OBJECT_CLASS (anjuta_sync_command_parent_class)->finalize (object);
57 }
58
59 static void
start_command(AnjutaCommand * command)60 start_command (AnjutaCommand *command)
61 {
62 guint return_code;
63
64 return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
65 anjuta_command_notify_complete (command, return_code);
66 }
67
68 static void
notify_data_arrived(AnjutaCommand * command)69 notify_data_arrived (AnjutaCommand *command)
70 {
71 g_signal_emit_by_name (command, "data-arrived");
72 }
73
74 static void
notify_complete(AnjutaCommand * command,guint return_code)75 notify_complete (AnjutaCommand *command, guint return_code)
76 {
77 g_signal_emit_by_name (command, "command-finished",
78 return_code);
79 }
80
81 static void
notify_progress(AnjutaCommand * command,gfloat progress)82 notify_progress (AnjutaCommand *command, gfloat progress)
83 {
84 g_signal_emit_by_name (command, "progress", progress);
85 }
86
87 static void
anjuta_sync_command_class_init(AnjutaSyncCommandClass * klass)88 anjuta_sync_command_class_init (AnjutaSyncCommandClass *klass)
89 {
90 GObjectClass* object_class = G_OBJECT_CLASS (klass);
91 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
92
93 object_class->finalize = anjuta_sync_command_finalize;
94
95 parent_class->start = start_command;
96 parent_class->notify_data_arrived = notify_data_arrived;
97 parent_class->notify_complete = notify_complete;
98 parent_class->notify_progress = notify_progress;
99 }
100