1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2009 <jrliggett@cox.net>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "anjuta-command-queue.h"
29 static guint anjuta_command_queue_signals
[LAST_SIGNAL
] = { 0 };
32 * SECTION: anjuta-command-queue
33 * @short_description: #AnjutaCommandQueue is used to queue commands
34 * @include: libanjuta/anjuta-async-command.h
36 * #AnjutaCommandQueue always starts the next command in the queue when
37 * the previous command finishes. That also works for asyncronous commands
40 struct _AnjutaCommandQueuePriv
44 AnjutaCommandQueueExecuteMode mode
;
47 G_DEFINE_TYPE (AnjutaCommandQueue
, anjuta_command_queue
, G_TYPE_OBJECT
);
50 anjuta_command_queue_init (AnjutaCommandQueue
*self
)
52 self
->priv
= g_new0 (AnjutaCommandQueuePriv
, 1);
54 self
->priv
->queue
= g_queue_new ();
58 anjuta_command_queue_finalize (GObject
*object
)
60 AnjutaCommandQueue
*self
;
61 GList
*current_command
;
63 self
= ANJUTA_COMMAND_QUEUE (object
);
65 current_command
= self
->priv
->queue
->head
;
67 while (current_command
)
69 g_object_unref (current_command
->data
);
70 current_command
= g_list_next (current_command
);
73 g_queue_free (self
->priv
->queue
);
76 G_OBJECT_CLASS (anjuta_command_queue_parent_class
)->finalize (object
);
80 finished (AnjutaCommandQueue
*queue
)
86 anjuta_command_queue_class_init (AnjutaCommandQueueClass
*klass
)
88 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
90 object_class
->finalize
= anjuta_command_queue_finalize
;
91 klass
->finished
= finished
;
93 anjuta_command_queue_signals
[FINISHED
] = g_signal_new ("finished",
94 G_OBJECT_CLASS_TYPE (klass
),
96 G_STRUCT_OFFSET (AnjutaCommandQueueClass
, finished
),
99 g_cclosure_marshal_VOID__VOID
,
106 on_command_finished (AnjutaCommand
*command
, guint return_code
,
107 AnjutaCommandQueue
*self
)
109 AnjutaCommand
*next_command
;
111 next_command
= g_queue_pop_head (self
->priv
->queue
);
115 g_signal_connect (G_OBJECT (next_command
), "command-finished",
116 G_CALLBACK (on_command_finished
),
119 anjuta_command_start (next_command
);
121 g_object_unref (next_command
);
125 self
->priv
->busy
= FALSE
;
127 if (self
->priv
->mode
== ANJUTA_COMMAND_QUEUE_EXECUTE_MANUAL
)
128 g_signal_emit_by_name (self
, "finished");
133 anjuta_command_queue_new (AnjutaCommandQueueExecuteMode mode
)
135 AnjutaCommandQueue
*self
;
137 self
= g_object_new (ANJUTA_TYPE_COMMAND_QUEUE
, NULL
);
139 self
->priv
->mode
= mode
;
145 * anjuta_command_queue_push:
146 * @self: AnjutaCommandQueue object
147 * @command: The command to add
149 * Adds a command to the Queue and starts it if there are no other commands
154 anjuta_command_queue_push (AnjutaCommandQueue
*self
, AnjutaCommand
*command
)
156 if (self
->priv
->mode
== ANJUTA_COMMAND_QUEUE_EXECUTE_AUTOMATIC
)
158 if (!self
->priv
->busy
)
160 g_signal_connect (G_OBJECT (command
), "command-finished",
161 G_CALLBACK (on_command_finished
),
164 if (self
->priv
->mode
== ANJUTA_COMMAND_QUEUE_EXECUTE_AUTOMATIC
)
166 self
->priv
->busy
= TRUE
;
167 anjuta_command_start (command
);
171 g_queue_push_tail (self
->priv
->queue
, g_object_ref (command
));
174 g_queue_push_tail (self
->priv
->queue
, g_object_ref (command
));
178 anjuta_command_queue_start (AnjutaCommandQueue
*self
)
181 AnjutaCommand
*first_command
;
185 if ((self
->priv
->mode
== ANJUTA_COMMAND_QUEUE_EXECUTE_MANUAL
) &&
188 first_command
= g_queue_pop_head (self
->priv
->queue
);
192 g_signal_connect (G_OBJECT (first_command
), "command-finished",
193 G_CALLBACK (on_command_finished
),
196 self
->priv
->busy
= TRUE
;
199 anjuta_command_start (first_command
);