summaryrefslogtreecommitdiff
path: root/libnautilus/nautilus-idle-queue.c
blob: 11cb8d2751fd355bc353d5beb1bf309bfb4e3625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: 8; c-basic-offset: 8 -*- */

/*
 *  libnautilus: A library for nautilus view implementations.
 *
 *  Copyright (C) 2001 Eazel, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Darin Adler <darin@eazel.com>
 *
 */

#include <config.h>
#include "nautilus-idle-queue.h"

#include <gtk/gtkmain.h>

struct NautilusIdleQueue {
	GList *functions;
	guint idle_id;
	gboolean in_idle;
	gboolean destroy;
};

typedef struct {
	GFunc callback;
	gpointer data;
	gpointer callback_data;
	GFreeFunc free_callback_data;
} QueuedFunction;

static gboolean
execute_queued_functions (gpointer callback_data)
{
	NautilusIdleQueue *queue;
	GList *functions, *node;
	QueuedFunction *function;

	queue = callback_data;

	/* We could receive more incoming functions while dispatching
	 * these, so keep going until the queue is empty.
	 */
	queue->in_idle = TRUE;
	while (queue->functions != NULL) {
		functions = g_list_reverse (queue->functions);
		queue->functions = NULL;

		for (node = functions; node != NULL; node = node->next) {
			function = node->data;

			(* function->callback) (function->data, function->callback_data);
			if (function->free_callback_data != NULL) {
				(* function->free_callback_data) (function->callback_data);
			}
		}

		g_list_free (functions);
	}
	queue->in_idle = FALSE;

	queue->idle_id = 0;

	if (queue->destroy) {
		nautilus_idle_queue_destroy (queue);
	}

	return FALSE;
}

NautilusIdleQueue *
nautilus_idle_queue_new (void)
{
	return g_new0 (NautilusIdleQueue, 1);
}

void
nautilus_idle_queue_add (NautilusIdleQueue *queue,
			 GFunc callback,
			 gpointer data,
			 gpointer callback_data,
			 GFreeFunc free_callback_data)
{
	QueuedFunction *function;

	function = g_new (QueuedFunction, 1);
	function->callback = callback;
	function->data = data;
	function->callback_data = callback_data;
	function->free_callback_data = free_callback_data;

	queue->functions = g_list_prepend (queue->functions, function);

	if (queue->idle_id == 0) {
		queue->idle_id = gtk_idle_add (execute_queued_functions, queue);
	}
}

void
nautilus_idle_queue_destroy (NautilusIdleQueue *queue)
{
	GList *node;
	QueuedFunction *function;

	if (queue->in_idle) {
		queue->destroy = TRUE;
		return;
	}

	for (node = queue->functions; node != NULL; node = node->next) {
		function = node->data;
		
		if (function->free_callback_data != NULL) {
			(* function->free_callback_data) (function->callback_data);
		}
	}
	
	g_list_free (queue->functions);

	if (queue->idle_id != 0) {
		gtk_idle_remove (queue->idle_id);
	}

	g_free (queue);
}