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 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
|
/* -*- 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"
/**
* SECTION: mm-network-timezone
* @title: MMNetworkTimezone
* @short_description: Helper object to handle network timezone information.
*
* The #MMNetworkTimezone is an object handling the timezone information
* reported by the network.
*
* This object is retrieved with either mm_modem_time_peek_network_timezone()
* or mm_modem_time_get_network_timezone().
*/
G_DEFINE_TYPE (MMNetworkTimezone, mm_network_timezone, G_TYPE_OBJECT);
struct _MMNetworkTimezonePrivate {
gint32 offset;
gint32 dst_offset;
gint32 leap_seconds;
};
/*****************************************************************************/
/**
* mm_network_timezone_get_offset:
* @self: a #MMNetworkTimezone.
*
* Gets the timezone offset (in minutes) reported by the network.
*
* Returns: the offset, or %MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN if unknown.
*/
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;
}
void
mm_network_timezone_set_offset (MMNetworkTimezone *self,
gint offset)
{
g_return_if_fail (MM_IS_NETWORK_TIMEZONE (self));
self->priv->offset = offset;
}
/*****************************************************************************/
/**
* mm_network_timezone_get_dst_offset:
* @self: a #MMNetworkTimezone.
*
* Gets the timezone offset due to daylight saving time (in minutes) reported by
* the network.
*
* Returns: the offset, or %MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN if unknown.
*/
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;
}
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;
}
/*****************************************************************************/
/**
* mm_network_timezone_get_leap_seconds:
* @self: a #MMNetworkTimezone.
*
* Gets the number of leap seconds (TAI-UTC), as reported by the network.
*
* Returns: the number of leap seconds, or %MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN if unknown.
*/
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_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));
}
|