~elementary-apps/pantheon-files/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* eel-glib-extensions.c - implementation of new functions that conceptually
 * belong in glib. Perhaps some of these will be
 * actually rolled into glib someday.
 *
 * Copyright (C) 2000 Eazel, Inc.
 *
 * The Gnome Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation, Inc.,; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Gnome Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1335 USA.
 *
 * Authors: John Sullivan <sullivan@eazel.com>
 *          ammonkey <am.monkeyd@gmail.com>
 */

#include "eel-glib-extensions.h"
#include <sys/time.h>
#include <math.h>

static void
update_auto_boolean (GSettings   *settings,
                     const gchar *key,
                     gpointer     user_data)
{
    int *storage = user_data;

    *storage = g_settings_get_boolean (settings, key);
}

void
eel_g_settings_add_auto_boolean (GSettings *settings,
                                 const char *key,
                                 gboolean *storage)
{
    char *signal;

    *storage = g_settings_get_boolean (settings, key);
    signal = g_strconcat ("changed::", key, NULL);
    g_signal_connect (settings, signal,
                      G_CALLBACK(update_auto_boolean),
                      storage);
}

/**
 * eel_add_weak_pointer
 *
 * Nulls out a saved reference to an object when the object gets destroyed.
 *
 * @pointer_location: Address of the saved pointer.
**/
void
eel_add_weak_pointer (gpointer pointer_location)
{
    gpointer *object_location;

    g_return_if_fail (pointer_location != NULL);

    object_location = (gpointer *) pointer_location;
    if (*object_location == NULL) {
        /* The reference is NULL, nothing to do. */
        return;
    }

    g_return_if_fail (G_IS_OBJECT (*object_location));

    g_object_add_weak_pointer (G_OBJECT (*object_location),
                               object_location);
}

/**
 * eel_remove_weak_pointer
 *
 * Removes the weak pointer that was added by eel_add_weak_pointer.
 * Also nulls out the pointer.
 *
 * @pointer_location: Pointer that was passed to eel_add_weak_pointer.
**/
void
eel_remove_weak_pointer (gpointer pointer_location)
{
    gpointer *object_location;

    g_return_if_fail (pointer_location != NULL);

    object_location = (gpointer *) pointer_location;
    if (*object_location == NULL) {
        /* The object was already destroyed and the reference
         * nulled out, nothing to do.
         */
        return;
    }

    g_return_if_fail (G_IS_OBJECT (*object_location));

    g_object_remove_weak_pointer (G_OBJECT (*object_location),
                                  object_location);

    *object_location = NULL;
}

/**
 * eel_g_object_list_ref
 *
 * Ref all the objects in a list.
 * @list: GList of objects.
**/
GList *
eel_g_object_list_ref (GList *list)
{
    g_list_foreach (list, (GFunc) g_object_ref, NULL);
    return list;
}

/**
 * eel_g_object_list_copy
 *
 * Copy the list of objects, ref'ing each one.
 * @list: GList of objects.
**/
GList *
eel_g_object_list_copy (GList *list)
{
    return g_list_copy (eel_g_object_list_ref (list));
}

/**
 * eel_g_str_list_alphabetize
 *
 * Sort a list of strings using locale-sensitive rules.
 *
 * @list: List of strings and/or NULLs.
 *
 * Return value: @list, sorted.
 **/
GList *
eel_g_str_list_alphabetize (GList *list)
{
    return g_list_sort (list, (GCompareFunc) g_utf8_collate);
}