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., 51 Franklin St, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19 
20    Author: Maciej Stachowiak <mjs@noisehavoc.org>
21 */
22 
23 #include <config.h>
24 #include "caja-file-queue.h"
25 
26 #include <glib.h>
27 
28 struct CajaFileQueue
29 {
30     GList *head;
31     GList *tail;
32     GHashTable *item_to_link_map;
33 };
34 
35 CajaFileQueue *
caja_file_queue_new(void)36 caja_file_queue_new (void)
37 {
38     CajaFileQueue *queue;
39 
40     queue = g_new0 (CajaFileQueue, 1);
41     queue->item_to_link_map = g_hash_table_new (g_direct_hash, g_direct_equal);
42 
43     return queue;
44 }
45 
46 void
caja_file_queue_destroy(CajaFileQueue * queue)47 caja_file_queue_destroy (CajaFileQueue *queue)
48 {
49     g_hash_table_destroy (queue->item_to_link_map);
50     caja_file_list_free (queue->head);
51     g_free (queue);
52 }
53 
54 void
caja_file_queue_enqueue(CajaFileQueue * queue,CajaFile * file)55 caja_file_queue_enqueue (CajaFileQueue *queue,
56                          CajaFile      *file)
57 {
58     if (g_hash_table_lookup (queue->item_to_link_map, file) != NULL)
59     {
60         /* It's already on the queue. */
61         return;
62     }
63 
64     if (queue->tail == NULL)
65     {
66         queue->head = g_list_append (NULL, file);
67         queue->tail = queue->head;
68     }
69     else
70     {
71         queue->tail = g_list_append (queue->tail, file);
72         queue->tail = queue->tail->next;
73     }
74 
75     caja_file_ref (file);
76     g_hash_table_insert (queue->item_to_link_map, file, queue->tail);
77 }
78 
79 CajaFile *
caja_file_queue_dequeue(CajaFileQueue * queue)80 caja_file_queue_dequeue (CajaFileQueue *queue)
81 {
82     CajaFile *file;
83 
84     file = caja_file_queue_head (queue);
85     caja_file_queue_remove (queue, file);
86 
87     return file;
88 }
89 
90 
91 void
caja_file_queue_remove(CajaFileQueue * queue,CajaFile * file)92 caja_file_queue_remove (CajaFileQueue *queue,
93                         CajaFile *file)
94 {
95     GList *link;
96 
97     link = g_hash_table_lookup (queue->item_to_link_map, file);
98 
99     if (link == NULL)
100     {
101         /* It's not on the queue */
102         return;
103     }
104 
105     if (link == queue->tail)
106     {
107         /* Need to special-case removing the tail. */
108         queue->tail = queue->tail->prev;
109     }
110 
111     queue->head =  g_list_remove_link (queue->head, link);
112     g_list_free (link);
113     g_hash_table_remove (queue->item_to_link_map, file);
114 
115     caja_file_unref (file);
116 }
117 
118 CajaFile *
caja_file_queue_head(CajaFileQueue * queue)119 caja_file_queue_head (CajaFileQueue *queue)
120 {
121     if (queue->head == NULL)
122     {
123         return NULL;
124     }
125 
126     return CAJA_FILE (queue->head->data);
127 }
128 
129 gboolean
caja_file_queue_is_empty(CajaFileQueue * queue)130 caja_file_queue_is_empty (CajaFileQueue *queue)
131 {
132     return (queue->head == NULL);
133 }
134