libmm-common: new `MMNetworkTimezone' helper object

This commit is contained in:
Aleksander Morgado
2012-03-16 14:53:20 +01:00
parent bd9d7011b9
commit 455fc68d8a
4 changed files with 289 additions and 0 deletions
+4
View File
@@ -159,6 +159,7 @@ mm-bearer-ip-config.c: mm-errors-types.h
mm-location-3gpp.c: mm-errors-types.h
mm-errors-quarks.c: mm-errors-types.h
mm-unlock-retries.c: mm-enums-types.h
mm-network-timezone.c: mm-errors-types.h
includedir = @includedir@/libmm-common
include_HEADERS = \
@@ -172,6 +173,7 @@ include_HEADERS = \
mm-bearer-ip-config.h \
mm-location-3gpp.h \
mm-unlock-retries.h \
mm-network-timezone.h \
mm-gdbus-manager.h \
mm-gdbus-modem.h \
mm-gdbus-bearer.h \
@@ -200,6 +202,8 @@ libmm_common_la_SOURCES = \
mm-location-3gpp.c \
mm-unlock-retries.h \
mm-unlock-retries.c \
mm-network-timezone.h \
mm-network-timezone.c \
libmm-common.h
libmm_common_la_CPPFLAGS = \
+1
View File
@@ -33,6 +33,7 @@
#include "mm-bearer-ip-config.h"
#include "mm-location-3gpp.h"
#include "mm-unlock-retries.h"
#include "mm-network-timezone.h"
#include "mm-gdbus-manager.h"
#include "mm-gdbus-modem.h"
#include "mm-gdbus-bearer.h"
+216
View File
@@ -0,0 +1,216 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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:
*
* Copyright (C) 2012 Google, Inc.
*/
#include <string.h>
#include "mm-errors-types.h"
#include "mm-network-timezone.h"
G_DEFINE_TYPE (MMNetworkTimezone, mm_network_timezone, G_TYPE_OBJECT);
struct _MMNetworkTimezonePrivate {
gint32 offset;
gint32 dst_offset;
gint32 leap_seconds;
};
/*****************************************************************************/
gint
mm_network_timezone_get_offset (MMNetworkTimezone *self)
{
g_return_val_if_fail (MM_IS_NETWORK_TIMEZONE (self),
MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN);
return self->priv->offset;
}
gint
mm_network_timezone_get_dst_offset (MMNetworkTimezone *self)
{
g_return_val_if_fail (MM_IS_NETWORK_TIMEZONE (self),
MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN);
return self->priv->dst_offset;
}
gint
mm_network_timezone_get_leap_seconds (MMNetworkTimezone *self)
{
g_return_val_if_fail (MM_IS_NETWORK_TIMEZONE (self),
MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN);
return self->priv->leap_seconds;
}
/*****************************************************************************/
void
mm_network_timezone_set_offset (MMNetworkTimezone *self,
gint offset)
{
g_return_if_fail (MM_IS_NETWORK_TIMEZONE (self));
self->priv->offset = offset;
}
void
mm_network_timezone_set_dst_offset (MMNetworkTimezone *self,
gint dst_offset)
{
g_return_if_fail (MM_IS_NETWORK_TIMEZONE (self));
self->priv->dst_offset = dst_offset;
}
void
mm_network_timezone_set_leap_seconds (MMNetworkTimezone *self,
gint leap_seconds)
{
g_return_if_fail (MM_IS_NETWORK_TIMEZONE (self));
self->priv->leap_seconds = leap_seconds;
}
/*****************************************************************************/
GVariant *
mm_network_timezone_get_dictionary (MMNetworkTimezone *self)
{
GVariantBuilder builder;
/* Allow NULL */
if (!self)
return NULL;
g_return_val_if_fail (MM_IS_NETWORK_TIMEZONE (self), NULL);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
if (self->priv->offset != MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN)
g_variant_builder_add (&builder,
"{sv}",
"offset",
g_variant_new_int32 (self->priv->offset));
if (self->priv->dst_offset != MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN)
g_variant_builder_add (&builder,
"{sv}",
"dst-offset",
g_variant_new_int32 (self->priv->dst_offset));
if (self->priv->leap_seconds != MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN)
g_variant_builder_add (&builder,
"{sv}",
"leap-seconds",
g_variant_new_int32 (self->priv->leap_seconds));
return g_variant_ref_sink (g_variant_builder_end (&builder));
}
/*****************************************************************************/
MMNetworkTimezone *
mm_network_timezone_new_from_dictionary (GVariant *dictionary,
GError **error)
{
GError *inner_error = NULL;
GVariantIter iter;
gchar *key;
GVariant *value;
MMNetworkTimezone *self;
self = mm_network_timezone_new ();
if (!dictionary)
return self;
if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{sv}"))) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Cannot create Network Timezone from dictionary: "
"invalid variant type received");
g_object_unref (self);
return NULL;
}
g_variant_iter_init (&iter, dictionary);
while (!inner_error &&
g_variant_iter_next (&iter, "{sv}", &key, &value)) {
/* All currently supported properties are signed integers,
* so we just check the value type here */
if (!g_variant_is_of_type (value, G_VARIANT_TYPE_INT32)) {
/* Set inner error, will stop the loop */
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid status dictionary, unexpected value type '%s'",
g_variant_get_type_string (value));
} else if (g_str_equal (key, "offset"))
self->priv->offset = g_variant_get_int32 (value);
else if (g_str_equal (key, "dst-offset"))
self->priv->dst_offset = g_variant_get_int32 (value);
else if (g_str_equal (key, "leap-seconds"))
self->priv->leap_seconds = g_variant_get_int32 (value);
else {
/* Set inner error, will stop the loop */
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid status dictionary, unexpected key '%s'",
key);
}
g_free (key);
g_variant_unref (value);
}
/* If error, destroy the object */
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (self);
return NULL;
}
return self;
}
/*****************************************************************************/
MMNetworkTimezone *
mm_network_timezone_new (void)
{
return (MM_NETWORK_TIMEZONE (
g_object_new (MM_TYPE_NETWORK_TIMEZONE, NULL)));
}
static void
mm_network_timezone_init (MMNetworkTimezone *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
MM_TYPE_NETWORK_TIMEZONE,
MMNetworkTimezonePrivate);
/* Some defaults */
self->priv->offset = MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN;
self->priv->dst_offset = MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN;
self->priv->leap_seconds = MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN;
}
static void
mm_network_timezone_class_init (MMNetworkTimezoneClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (MMNetworkTimezonePrivate));
}
+68
View File
@@ -0,0 +1,68 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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:
*
* Copyright (C) 2012 Google, Inc.
*/
#ifndef MM_NETWORK_TIMEZONE_H
#define MM_NETWORK_TIMEZONE_H
#include <ModemManager.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define MM_TYPE_NETWORK_TIMEZONE (mm_network_timezone_get_type ())
#define MM_NETWORK_TIMEZONE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_NETWORK_TIMEZONE, MMNetworkTimezone))
#define MM_NETWORK_TIMEZONE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_NETWORK_TIMEZONE, MMNetworkTimezoneClass))
#define MM_IS_NETWORK_TIMEZONE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_NETWORK_TIMEZONE))
#define MM_IS_NETWORK_TIMEZONE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_NETWORK_TIMEZONE))
#define MM_NETWORK_TIMEZONE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_NETWORK_TIMEZONE, MMNetworkTimezoneClass))
#define MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN G_MAXINT32
#define MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN G_MAXINT32
typedef struct _MMNetworkTimezone MMNetworkTimezone;
typedef struct _MMNetworkTimezoneClass MMNetworkTimezoneClass;
typedef struct _MMNetworkTimezonePrivate MMNetworkTimezonePrivate;
struct _MMNetworkTimezone {
GObject parent;
MMNetworkTimezonePrivate *priv;
};
struct _MMNetworkTimezoneClass {
GObjectClass parent;
};
GType mm_network_timezone_get_type (void);
MMNetworkTimezone *mm_network_timezone_new (void);
MMNetworkTimezone *mm_network_timezone_new_from_dictionary (GVariant *dictionary,
GError **error);
gint32 mm_network_timezone_get_offset (MMNetworkTimezone *self);
gint32 mm_network_timezone_get_dst_offset (MMNetworkTimezone *self);
gint32 mm_network_timezone_get_leap_seconds (MMNetworkTimezone *self);
void mm_network_timezone_set_offset (MMNetworkTimezone *self,
gint offset);
void mm_network_timezone_set_dst_offset (MMNetworkTimezone *self,
gint dst_offset);
void mm_network_timezone_set_leap_seconds (MMNetworkTimezone *self,
gint leap_seconds);
GVariant *mm_network_timezone_get_dictionary (MMNetworkTimezone *self);
G_END_DECLS
#endif /* MM_NETWORK_TIMEZONE_H */