~noskcaj/ubuntu/utopic/libfm/merge

« back to all changes in this revision

Viewing changes to src/base/fm-list.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Lee (李健秋)
  • Date: 2010-04-29 03:52:06 UTC
  • Revision ID: james.westby@ubuntu.com-20100429035206-qlj1jwsfcgr5mdx3
Tags: upstream-0.1.11
Import upstream version 0.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      fm-list.c
 
3
 *      
 
4
 *      Copyright 2009 PCMan <pcman.tw@gmail.com>
 
5
 *      
 
6
 *      This program is free software; you can redistribute it and/or modify
 
7
 *      it under the terms of the GNU General Public License as published by
 
8
 *      the Free Software Foundation; either version 2 of the License, or
 
9
 *      (at your option) any later version.
 
10
 *      
 
11
 *      This program is distributed in the hope that it will be useful,
 
12
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *      GNU General Public License for more details.
 
15
 *      
 
16
 *      You should have received a copy of the GNU General Public License
 
17
 *      along with this program; if not, write to the Free Software
 
18
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
 *      MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include "fm-list.h"
 
23
 
 
24
FmList* fm_list_new(FmListFuncs* funcs)
 
25
{
 
26
        FmList* list = g_slice_new(FmList);
 
27
        list->funcs = funcs;
 
28
        g_queue_init(&list->list);
 
29
        list->n_ref = 1;
 
30
        return list;
 
31
}
 
32
 
 
33
FmList* fm_list_ref(gpointer list)
 
34
{
 
35
        g_atomic_int_inc(&FM_LIST(list)->n_ref);
 
36
        return FM_LIST(list);
 
37
}
 
38
 
 
39
void fm_list_unref(gpointer list)
 
40
{
 
41
        if(g_atomic_int_dec_and_test(&FM_LIST(list)->n_ref))
 
42
        {
 
43
                g_queue_foreach((GQueue*)list, (GFunc)FM_LIST(list)->funcs->item_unref, NULL);
 
44
                g_slice_free(FmList, list);
 
45
        }
 
46
}
 
47
 
 
48
void fm_list_clear(gpointer list)
 
49
{
 
50
        g_queue_foreach((GQueue*)list, (GFunc)FM_LIST(list)->funcs->item_unref, NULL);
 
51
        g_queue_clear((GQueue*)list);
 
52
}
 
53
 
 
54
void fm_list_remove(gpointer list, gpointer data)
 
55
{
 
56
        GList* l = ((GQueue*)list)->head;
 
57
        for(;l; l=l->next)
 
58
        {
 
59
                if(l->data == data)
 
60
                {
 
61
                        FM_LIST(list)->funcs->item_unref(data);
 
62
                        break;
 
63
                }
 
64
        }
 
65
        if(l)
 
66
                g_queue_delete_link((GQueue*)data, l);
 
67
}
 
68
 
 
69
void fm_list_remove_all(gpointer list, gpointer data)
 
70
{
 
71
        /* FIXME: the performance can be better... */
 
72
        GList* l = ((GQueue*)list)->head;
 
73
        for(;l; l=l->next)
 
74
        {
 
75
                if(l->data == data)
 
76
                        FM_LIST(list)->funcs->item_unref(data);
 
77
        }
 
78
        g_queue_remove_all((GQueue*)list, data);
 
79
}
 
80
 
 
81
void fm_list_delete_link(gpointer list, gpointer l_)
 
82
{
 
83
        FM_LIST(list)->funcs->item_unref(((GList*)l_)->data);   
 
84
        g_queue_delete_link((GQueue*)list, l_);
 
85
}