nautilus-file-queue.c 2.96 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *  Copyright (C) 2001 Maciej Stachowiak
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program 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
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *  Author: Maciej Stachowiak <mjs@noisehavoc.org>
 */
19 20 21 22 23 24

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

#include <glib.h>

25 26 27 28 29
struct NautilusFileQueue
{
    GList *head;
    GList *tail;
    GHashTable *item_to_link_map;
30 31 32 33 34
};

NautilusFileQueue *
nautilus_file_queue_new (void)
{
35 36 37 38
    NautilusFileQueue *queue;

    queue = g_new0 (NautilusFileQueue, 1);
    queue->item_to_link_map = g_hash_table_new (g_direct_hash, g_direct_equal);
39

40
    return queue;
41 42 43 44 45
}

void
nautilus_file_queue_destroy (NautilusFileQueue *queue)
{
46 47 48
    g_hash_table_destroy (queue->item_to_link_map);
    nautilus_file_list_free (queue->head);
    g_free (queue);
49 50 51 52
}

void
nautilus_file_queue_enqueue (NautilusFileQueue *queue,
53
                             NautilusFile      *file)
54
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    if (g_hash_table_lookup (queue->item_to_link_map, file) != NULL)
    {
        /* It's already on the queue. */
        return;
    }

    if (queue->tail == NULL)
    {
        queue->head = g_list_append (NULL, file);
        queue->tail = queue->head;
    }
    else
    {
        queue->tail = g_list_append (queue->tail, file);
        queue->tail = queue->tail->next;
    }

    nautilus_file_ref (file);
    g_hash_table_insert (queue->item_to_link_map, file, queue->tail);
74 75 76 77 78
}

NautilusFile *
nautilus_file_queue_dequeue (NautilusFileQueue *queue)
{
79
    NautilusFile *file;
80

81 82
    file = nautilus_file_queue_head (queue);
    nautilus_file_queue_remove (queue, file);
83

84
    return file;
85 86 87 88 89
}


void
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 97 98 99 100
    if (link == NULL)
    {
        /* It's not on the queue */
        return;
    }
101

102 103 104 105 106
    if (link == queue->tail)
    {
        /* Need to special-case removing the tail. */
        queue->tail = queue->tail->prev;
    }
107

108 109 110
    queue->head = g_list_remove_link (queue->head, link);
    g_list_free (link);
    g_hash_table_remove (queue->item_to_link_map, file);
111

112
    nautilus_file_unref (file);
113 114 115 116 117
}

NautilusFile *
nautilus_file_queue_head (NautilusFileQueue *queue)
{
118 119 120 121
    if (queue->head == NULL)
    {
        return NULL;
    }
122

123
    return NAUTILUS_FILE (queue->head->data);
124 125 126 127 128
}

gboolean
nautilus_file_queue_is_empty (NautilusFileQueue *queue)
{
129
    return (queue->head == NULL);
130
}