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