No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 Copyright (C) 2001 Maciej Stachowiak
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 Author: Maciej Stachowiak <mjs@noisehavoc.org>
21 */
22
23 #include <config.h>
24 #include "nautilus-file-queue.h"
25
26 #include <glib.h>
27
28 struct NautilusFileQueue {
29 GList *head;
30 GList *tail;
31 GHashTable *item_to_link_map;
32 };
33
34 NautilusFileQueue *
35 nautilus_file_queue_new (void)
36 {
37 NautilusFileQueue *queue;
38
39 queue = g_new0 (NautilusFileQueue, 1);
40 queue->item_to_link_map = g_hash_table_new (g_direct_hash, g_direct_equal);
41
42 return queue;
43 }
44
45 void
46 nautilus_file_queue_destroy (NautilusFileQueue *queue)
47 {
48 g_hash_table_destroy (queue->item_to_link_map);
49 nautilus_file_list_free (queue->head);
50 g_free (queue);
51 }
52
53 void
54 nautilus_file_queue_enqueue (NautilusFileQueue *queue,
55 NautilusFile *file)
56 {
57 if (g_hash_table_lookup (queue->item_to_link_map, file) != NULL) {
58 /* It's already on the queue. */
59 return;
60 }
61
62 if (queue->tail == NULL) {
63 queue->head = g_list_append (NULL, file);
64 queue->tail = queue->head;
65 } else {
66 queue->tail = g_list_append (queue->tail, file);
67 queue->tail = queue->tail->next;
68 }
69
70 nautilus_file_ref (file);
71 g_hash_table_insert (queue->item_to_link_map, file, queue->tail);
72 }
73
74 NautilusFile *
75 nautilus_file_queue_dequeue (NautilusFileQueue *queue)
76 {
77 NautilusFile *file;
78
79 file = nautilus_file_queue_head (queue);
80 nautilus_file_queue_remove (queue, file);
81
82 return file;
83 }
84
85
86 void
87 nautilus_file_queue_remove (NautilusFileQueue *queue,
88 NautilusFile *file)
89 {
90 GList *link;
91
92 link = g_hash_table_lookup (queue->item_to_link_map, file);
93
94 if (link == NULL) {
95 /* It's not on the queue */
96 return;
97 }
98
99 if (link == queue->tail) {
100 /* Need to special-case removing the tail. */
101 queue->tail = queue->tail->prev;
102 }
103
104 queue->head = g_list_remove_link (queue->head, link);
105 g_list_free (link);
106 g_hash_table_remove (queue->item_to_link_map, file);
107
108 nautilus_file_unref (file);
109 }
110
111 NautilusFile *
112 nautilus_file_queue_head (NautilusFileQueue *queue)
113 {
114 if (queue->head == NULL) {
115 return NULL;
116 }
117
118 return NAUTILUS_FILE (queue->head->data);
119 }
120
121 gboolean
122 nautilus_file_queue_is_empty (NautilusFileQueue *queue)
123 {
124 return (queue->head == NULL);
125 }