summaryrefslogtreecommitdiff
path: root/src/gclue-min-uint.c
blob: 558bfd76de0b8c5e433209f6bf61bbd343d1de97 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* vim: set et ts=8 sw=8: */
/* gclue-min-uint.c
 *
 * Copyright 2018 Collabora Ltd.
 *
 * Geoclue 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.
 *
 * Geoclue 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 Geoclue; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 */

#include "gclue-min-uint.h"

/**
 * SECTION:gclue-min-uint
 * @short_description: Easy way to keep track of minimum of a bunch of values
 * @include: gclue-glib/gclue-location-source.h
 *
 * This is a helper class that keeps a list of guint values and the minimum
 * value from this list. It is used by location sources to use the minimum
 * time-threshold (location update rate) from all the time-thresholds requested
 * by different applications.
 **/

struct _GClueMinUINTPrivate
{
        GHashTable *all_values;

        gboolean notify_value;
};

G_DEFINE_TYPE_WITH_CODE (GClueMinUINT,
                         gclue_min_uint,
                         G_TYPE_OBJECT,
                         G_ADD_PRIVATE (GClueMinUINT))

enum
{
        PROP_0,
        PROP_VALUE,
        LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

typedef struct
{
        GClueMinUINT *muint;
        GObject *owner;
} OwnerData;

static gboolean
on_owner_weak_ref_notify_defered (OwnerData *data)
{
        gclue_min_uint_drop_value (data->muint, data->owner);
        g_object_unref (data->muint);
        g_slice_free (OwnerData, data);

        return FALSE;
}

static void
on_owner_weak_ref_notify (gpointer data, GObject *object)
{
        OwnerData *owner_data = g_slice_new (OwnerData);
        owner_data->muint = GCLUE_MIN_UINT (data);
        g_object_ref (owner_data->muint);
        owner_data->owner = object;

        // Let's ensure owner is really gone before we drop its value
        g_idle_add ((GSourceFunc) on_owner_weak_ref_notify_defered, owner_data);
}

static void
gclue_min_uint_finalize (GObject *object)
{
        g_clear_pointer (&GCLUE_MIN_UINT (object)->priv->all_values,
                         g_hash_table_unref);

        /* Chain up to the parent class */
        G_OBJECT_CLASS (gclue_min_uint_parent_class)->finalize (object);
}

static void
gclue_min_uint_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
        GClueMinUINT *muint = GCLUE_MIN_UINT (object);

        switch (prop_id) {
        case PROP_VALUE:
                g_value_set_uint (value, gclue_min_uint_get_value (muint));
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_min_uint_class_init (GClueMinUINTClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gclue_min_uint_finalize;
        object_class->get_property = gclue_min_uint_get_property;

        gParamSpecs[PROP_VALUE] = g_param_spec_uint ("value",
                                                     "Value",
                                                     "The minimum value",
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE);
        g_object_class_install_property (object_class,
                                         PROP_VALUE,
                                         gParamSpecs[PROP_VALUE]);
}

static void
gclue_min_uint_init (GClueMinUINT *muint)
{
        muint->priv = gclue_min_uint_get_instance_private (muint);
        muint->priv->all_values = g_hash_table_new (g_direct_hash,
                                                    g_direct_equal);
        muint->priv->notify_value = TRUE;
}

/**
 * gclue_min_uint_new
 *
 * Returns: A new #GClueMinUINT instance.
 **/
GClueMinUINT *
gclue_min_uint_new (void)
{
        return g_object_new (GCLUE_TYPE_MIN_UINT, NULL);
}

/**
 * gclue_min_uint_get_value
 * @muint: a #GClueMinUINT
 *
 * Returns: The current minimum value from the list.
 **/
guint
gclue_min_uint_get_value (GClueMinUINT *muint)
{
        guint value;
        GList *values, *l;

        g_return_val_if_fail (GCLUE_IS_MIN_UINT(muint), 0);

        if (g_hash_table_size (muint->priv->all_values) == 0)
                return 0;

        values = g_hash_table_get_values (muint->priv->all_values);
        value = GPOINTER_TO_UINT (values->data);

        for (l = values->next; l; l = l->next) {
                guint i = GPOINTER_TO_UINT (l->data);

                if (value > i) {
                        value = i;
                }
        }

        g_list_free (values);

        return value;
}

/**
 * gclue_min_uint_add_value
 * @muint: a #GClueMinUINT
 * @value: A value to add to the list
 * @owner: the object adding this value
 *
 * If @owner has already added a value previously, this call will simply replace
 * that. i-e Each object can only add one value at a time.
 **/
void
gclue_min_uint_add_value (GClueMinUINT *muint,
                          guint         value,
                          GObject      *owner)
{
        g_return_if_fail (GCLUE_IS_MIN_UINT(muint));

        g_hash_table_replace (muint->priv->all_values,
                              owner,
                              GUINT_TO_POINTER (value));
        g_object_weak_ref (owner, on_owner_weak_ref_notify, muint);

        g_object_notify_by_pspec (G_OBJECT (muint), gParamSpecs[PROP_VALUE]);
}

/**
 * gclue_min_uint_drop_value
 * @muint: a #GClueMinUINT
 * @owner: the object that adadded a value previously
 **/
void
gclue_min_uint_drop_value (GClueMinUINT *muint,
                           GObject      *owner)
{
        g_return_if_fail (GCLUE_IS_MIN_UINT(muint));

        if (!g_hash_table_remove (muint->priv->all_values, owner)) {
                return;
        }

        g_object_notify_by_pspec (G_OBJECT (muint), gParamSpecs[PROP_VALUE]);
}