gsm-store.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007-2008 William Jon McCann <mccann@jhu.edu>
 *
 * 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
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
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
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include "gsm-store.h"

#define GSM_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_STORE, GsmStorePrivate))

struct GsmStorePrivate
{
        GHashTable *objects;
        gboolean    locked;
};

enum {
        ADDED,
        REMOVED,
        LAST_SIGNAL
};

enum {
        PROP_0,
50
        PROP_LOCKED
51 52
};

53
static guint signals [LAST_SIGNAL] = { 0 };
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

static void     gsm_store_class_init    (GsmStoreClass *klass);
static void     gsm_store_init          (GsmStore      *store);
static void     gsm_store_finalize      (GObject       *object);

G_DEFINE_TYPE (GsmStore, gsm_store, G_TYPE_OBJECT)

GQuark
gsm_store_error_quark (void)
{
        static GQuark ret = 0;
        if (ret == 0) {
                ret = g_quark_from_static_string ("gsm_store_error");
        }

        return ret;
}

guint
gsm_store_size (GsmStore    *store)
{
        return g_hash_table_size (store->priv->objects);
}

gboolean
gsm_store_remove (GsmStore   *store,
                  const char *id)
{
        GObject *found;
        gboolean removed;
William Jon McCann's avatar
William Jon McCann committed
84
        char    *id_copy;
85 86 87 88 89 90 91 92

        g_return_val_if_fail (store != NULL, FALSE);

        found = g_hash_table_lookup (store->priv->objects, id);
        if (found == NULL) {
                return FALSE;
        }

William Jon McCann's avatar
William Jon McCann committed
93 94
        id_copy = g_strdup (id);

95
        g_object_ref (found);
96

William Jon McCann's avatar
William Jon McCann committed
97
        removed = g_hash_table_remove (store->priv->objects, id_copy);
98 99
        g_assert (removed);

William Jon McCann's avatar
William Jon McCann committed
100
        g_signal_emit (store, signals [REMOVED], 0, id_copy);
101 102

        g_object_unref (found);
William Jon McCann's avatar
William Jon McCann committed
103
        g_free (id_copy);
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
        return TRUE;
}

void
gsm_store_foreach (GsmStore    *store,
                   GsmStoreFunc func,
                   gpointer     user_data)
{
        g_return_if_fail (store != NULL);
        g_return_if_fail (func != NULL);

        g_hash_table_find (store->priv->objects,
                           (GHRFunc)func,
                           user_data);
}

GObject *
gsm_store_find (GsmStore    *store,
                GsmStoreFunc predicate,
                gpointer     user_data)
{
        GObject *object;

        g_return_val_if_fail (store != NULL, NULL);
        g_return_val_if_fail (predicate != NULL, NULL);

        object = g_hash_table_find (store->priv->objects,
                                    (GHRFunc)predicate,
                                    user_data);
        return object;
}

GObject *
gsm_store_lookup (GsmStore   *store,
                  const char *id)
{
        GObject *object;

        g_return_val_if_fail (store != NULL, NULL);
        g_return_val_if_fail (id != NULL, NULL);

        object = g_hash_table_lookup (store->priv->objects, id);

        return object;
}


typedef struct
{
        GsmStoreFunc func;
        gpointer     user_data;
        GsmStore    *store;
157
        GList       *removed;
158 159 160 161 162 163 164 165 166 167 168
} WrapperData;

static gboolean
foreach_remove_wrapper (const char  *id,
                        GObject     *object,
                        WrapperData *data)
{
        gboolean res;

        res = (data->func) (id, object, data->user_data);
        if (res) {
169
                data->removed = g_list_prepend (data->removed, g_strdup (id));
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        }

        return res;
}

guint
gsm_store_foreach_remove (GsmStore    *store,
                          GsmStoreFunc func,
                          gpointer     user_data)
{
        guint       ret;
        WrapperData data;

        g_return_val_if_fail (store != NULL, 0);
        g_return_val_if_fail (func != NULL, 0);

        data.store = store;
        data.user_data = user_data;
        data.func = func;
189
        data.removed = NULL;
190 191 192 193 194

        ret = g_hash_table_foreach_remove (store->priv->objects,
                                           (GHRFunc)foreach_remove_wrapper,
                                           &data);

195 196 197
        while (data.removed != NULL) {
                char *id;
                id = data.removed->data;
198
                g_debug ("GsmStore: emitting removed for %s", id);
199
                g_signal_emit (store, signals [REMOVED], 0, id);
200 201
                g_free (data.removed->data);
                data.removed->data = NULL;
202 203 204
                data.removed = g_list_delete_link (data.removed, data.removed);
        }

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
        return ret;
}

static gboolean
_remove_all (const char *id,
             GObject    *object,
             gpointer    data)
{
        return TRUE;
}

void
gsm_store_clear (GsmStore *store)
{
        g_return_if_fail (store != NULL);

        g_debug ("GsmStore: Clearing object store");

        gsm_store_foreach_remove (store,
                                  _remove_all,
                                  NULL);
}

gboolean
gsm_store_add (GsmStore   *store,
               const char *id,
               GObject    *object)
{
        g_return_val_if_fail (store != NULL, FALSE);
        g_return_val_if_fail (id != NULL, FALSE);
        g_return_val_if_fail (object != NULL, FALSE);

        /* If we're locked, we don't accept any new session
           objects. */
        if (store->priv->locked) {
                return FALSE;
        }

        g_debug ("GsmStore: Adding object id %s to store", id);

        g_hash_table_insert (store->priv->objects,
                             g_strdup (id),
                             g_object_ref (object));

        g_signal_emit (store, signals [ADDED], 0, id);

        return TRUE;
}

void
gsm_store_set_locked (GsmStore *store,
                      gboolean  locked)
{
        g_return_if_fail (GSM_IS_STORE (store));

        store->priv->locked = locked;
}

gboolean
gsm_store_get_locked (GsmStore *store)
{
        g_return_val_if_fail (GSM_IS_STORE (store), FALSE);

        return store->priv->locked;
}

static void
gsm_store_set_property (GObject      *object,
                        guint         prop_id,
                        const GValue *value,
                        GParamSpec   *pspec)
{
        GsmStore *self;

        self = GSM_STORE (object);

        switch (prop_id) {
        case PROP_LOCKED:
                gsm_store_set_locked (self, g_value_get_boolean (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gsm_store_get_property (GObject    *object,
                        guint       prop_id,
                        GValue     *value,
                        GParamSpec *pspec)
{
        GsmStore *self;

        self = GSM_STORE (object);

        switch (prop_id) {
        case PROP_LOCKED:
                g_value_set_boolean (value, self->priv->locked);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static void
gsm_store_dispose (GObject *object)
{
        GsmStore *store;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSM_IS_STORE (object));

        store = GSM_STORE (object);

        gsm_store_clear (store);

        G_OBJECT_CLASS (gsm_store_parent_class)->dispose (object);
}

326 327 328 329 330 331 332 333
static void
gsm_store_class_init (GsmStoreClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = gsm_store_get_property;
        object_class->set_property = gsm_store_set_property;
        object_class->finalize = gsm_store_finalize;
334
        object_class->dispose = gsm_store_dispose;
335 336 337 338 339 340

        signals [ADDED] =
                g_signal_new ("added",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GsmStoreClass, added),
341
                              NULL, NULL, NULL,
342 343 344 345 346 347 348
                              G_TYPE_NONE,
                              1, G_TYPE_STRING);
        signals [REMOVED] =
                g_signal_new ("removed",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GsmStoreClass, removed),
349
                              NULL, NULL, NULL,
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                              G_TYPE_NONE,
                              1, G_TYPE_STRING);
        g_object_class_install_property (object_class,
                                         PROP_LOCKED,
                                         g_param_spec_boolean ("locked",
                                                               NULL,
                                                               NULL,
                                                               FALSE,
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

        g_type_class_add_private (klass, sizeof (GsmStorePrivate));
}

static void
_destroy_object (GObject *object)
{
        g_debug ("GsmStore: Unreffing object: %p", object);
        g_object_unref (object);
}

static void
gsm_store_init (GsmStore *store)
{

        store->priv = GSM_STORE_GET_PRIVATE (store);

        store->priv->objects = g_hash_table_new_full (g_str_hash,
                                                      g_str_equal,
                                                      g_free,
                                                      (GDestroyNotify) _destroy_object);
}

static void
gsm_store_finalize (GObject *object)
{
        GsmStore *store;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSM_IS_STORE (object));

        store = GSM_STORE (object);

        g_return_if_fail (store->priv != NULL);

        g_hash_table_destroy (store->priv->objects);

        G_OBJECT_CLASS (gsm_store_parent_class)->finalize (object);
}

GsmStore *
gsm_store_new (void)
{
        GObject *object;

        object = g_object_new (GSM_TYPE_STORE,
                               NULL);

        return GSM_STORE (object);
}