libmm-common: new helper object to handle common SMS properties

This commit is contained in:
Aleksander Morgado
2012-02-07 23:43:24 +01:00
parent 69b8742ca3
commit 672e9d09e3
4 changed files with 505 additions and 0 deletions

View File

@@ -151,6 +151,7 @@ mm-common-helpers.c: mm-errors-types.h mm-enums-types.h
mm-common-simple-properties.c: mm-errors-types.h mm-enums-types.h
mm-common-connect-properties.c: mm-errors-types.h
mm-common-bearer-properties.c: mm-errors-types.h
mm-common-sms-properties.c: mm-errors-types.h
mm-common-bearer-ip-config.c: mm-errors-types.h
mm-common-location-3gpp.c: mm-errors-types.h
mm-errors-quarks.c: mm-errors-types.h
@@ -163,6 +164,7 @@ include_HEADERS = \
mm-common-simple-properties.h \
mm-common-connect-properties.h \
mm-common-bearer-properties.h \
mm-common-sms-properties.h \
mm-common-bearer-ip-config.h \
mm-common-location-3gpp.h \
mm-gdbus-manager.h \
@@ -183,6 +185,8 @@ libmm_common_la_SOURCES = \
mm-common-connect-properties.c \
mm-common-bearer-properties.h \
mm-common-bearer-properties.c \
mm-common-sms-properties.h \
mm-common-sms-properties.c \
mm-common-bearer-ip-config.h \
mm-common-bearer-ip-config.c \
mm-common-location-3gpp.h \

View File

@@ -28,6 +28,7 @@
#include "mm-common-helpers.h"
#include "mm-common-simple-properties.h"
#include "mm-common-connect-properties.h"
#include "mm-common-sms-properties.h"
#include "mm-common-bearer-properties.h"
#include "mm-common-bearer-ip-config.h"
#include "mm-common-location-3gpp.h"

View File

@@ -0,0 +1,413 @@
/* -*- 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) 2011 Aleksander Morgado <aleksander@gnu.org>
*/
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "mm-errors-types.h"
#include "mm-common-helpers.h"
#include "mm-common-sms-properties.h"
G_DEFINE_TYPE (MMCommonSmsProperties, mm_common_sms_properties, G_TYPE_OBJECT);
#define PROPERTY_TEXT "text"
#define PROPERTY_NUMBER "number"
#define PROPERTY_SMSC "smsc"
#define PROPERTY_VALIDITY "validity"
#define PROPERTY_CLASS "class"
struct _MMCommonSmsPropertiesPrivate {
gchar *text;
gchar *number;
gchar *smsc;
gboolean validity_set;
guint validity;
gboolean class_set;
guint class;
};
/*****************************************************************************/
void
mm_common_sms_properties_set_text (MMCommonSmsProperties *self,
const gchar *text)
{
g_free (self->priv->text);
self->priv->text = g_strdup (text);
}
void
mm_common_sms_properties_set_number (MMCommonSmsProperties *self,
const gchar *number)
{
g_free (self->priv->number);
self->priv->number = g_strdup (number);
}
void
mm_common_sms_properties_set_smsc (MMCommonSmsProperties *self,
const gchar *smsc)
{
g_free (self->priv->smsc);
self->priv->smsc = g_strdup (smsc);
}
void
mm_common_sms_properties_set_validity (MMCommonSmsProperties *self,
guint validity)
{
self->priv->validity_set = TRUE;
self->priv->validity = validity;
}
void
mm_common_sms_properties_set_class (MMCommonSmsProperties *self,
guint class)
{
self->priv->class_set = TRUE;
self->priv->class = class;
}
/*****************************************************************************/
const gchar *
mm_common_sms_properties_get_text (MMCommonSmsProperties *self)
{
return self->priv->text;
}
const gchar *
mm_common_sms_properties_get_number (MMCommonSmsProperties *self)
{
return self->priv->number;
}
const gchar *
mm_common_sms_properties_get_smsc (MMCommonSmsProperties *self)
{
return self->priv->smsc;
}
guint
mm_common_sms_properties_get_validity (MMCommonSmsProperties *self)
{
return self->priv->validity;
}
guint
mm_common_sms_properties_get_class (MMCommonSmsProperties *self)
{
return self->priv->class;
}
/*****************************************************************************/
GVariant *
mm_common_sms_properties_get_dictionary (MMCommonSmsProperties *self)
{
GVariantBuilder builder;
/* We do allow NULL */
if (!self)
return NULL;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
if (self->priv->text)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_TEXT,
g_variant_new_string (self->priv->text));
if (self->priv->number)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_NUMBER,
g_variant_new_string (self->priv->number));
if (self->priv->smsc)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_SMSC,
g_variant_new_string (self->priv->smsc));
if (self->priv->validity_set)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_VALIDITY,
g_variant_new_uint32 (self->priv->validity));
if (self->priv->class_set)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_CLASS,
g_variant_new_uint32 (self->priv->class));
return g_variant_ref_sink (g_variant_builder_end (&builder));
}
/*****************************************************************************/
static guint
parse_uint (const gchar *str,
GError **error)
{
guint num;
errno = 0;
num = strtoul (str, NULL, 10);
if ((num < G_MAXUINT32) && (errno == 0))
return num;
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid properties string, cannot parset '%s' as uint",
str);
return 0;
}
static gboolean
consume_string (MMCommonSmsProperties *self,
const gchar *key,
const gchar *value,
GError **error)
{
if (g_str_equal (key, PROPERTY_TEXT))
mm_common_sms_properties_set_text (self, value);
else if (g_str_equal (key, PROPERTY_NUMBER))
mm_common_sms_properties_set_number (self, value);
else if (g_str_equal (key, PROPERTY_SMSC))
mm_common_sms_properties_set_smsc (self, value);
else if (g_str_equal (key, PROPERTY_VALIDITY)) {
GError *inner_error = NULL;
guint n;
n = parse_uint (value, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return FALSE;
}
mm_common_sms_properties_set_validity (self, n);
} else if (g_str_equal (key, PROPERTY_CLASS)) {
GError *inner_error = NULL;
guint n;
n = parse_uint (value, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return FALSE;
}
mm_common_sms_properties_set_class (self, n);
} else {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid properties string, unexpected key '%s'",
key);
return FALSE;
}
return TRUE;
}
MMCommonSmsProperties *
mm_common_sms_properties_new_from_string (const gchar *str,
GError **error)
{
GError *inner_error = NULL;
MMCommonSmsProperties *properties;
gchar **words;
gchar *key;
gchar *value;
guint i;
properties = mm_common_sms_properties_new ();
/* Expecting input as:
* key1=string,key2=true,key3=false...
* */
words = g_strsplit_set (str, ",= ", -1);
if (!words)
return properties;
i = 0;
key = words[i];
while (key) {
value = words[++i];
if (!value) {
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid properties string, no value for key '%s'",
key);
break;
}
if (!consume_string (properties,
key,
value,
&inner_error))
break;
key = words[++i];
}
/* If error, destroy the object */
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (properties);
properties = NULL;
}
g_strfreev (words);
return properties;
}
/*****************************************************************************/
static gboolean
consume_variant (MMCommonSmsProperties *properties,
const gchar *key,
GVariant *value,
GError **error)
{
if (g_str_equal (key, PROPERTY_TEXT))
mm_common_sms_properties_set_text (
properties,
g_variant_get_string (value, NULL));
else if (g_str_equal (key, PROPERTY_NUMBER))
mm_common_sms_properties_set_number (
properties,
g_variant_get_string (value, NULL));
else if (g_str_equal (key, PROPERTY_SMSC))
mm_common_sms_properties_set_smsc (
properties,
g_variant_get_string (value, NULL));
else if (g_str_equal (key, PROPERTY_VALIDITY))
mm_common_sms_properties_set_validity (
properties,
g_variant_get_uint32 (value));
else if (g_str_equal (key, PROPERTY_CLASS))
mm_common_sms_properties_set_class (
properties,
g_variant_get_uint32 (value));
else {
/* Set error */
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Invalid properties dictionary, unexpected key '%s'",
key);
return FALSE;
}
return TRUE;
}
MMCommonSmsProperties *
mm_common_sms_properties_new_from_dictionary (GVariant *dictionary,
GError **error)
{
GError *inner_error = NULL;
GVariantIter iter;
gchar *key;
GVariant *value;
MMCommonSmsProperties *properties;
properties = mm_common_sms_properties_new ();
if (!dictionary)
return properties;
g_variant_iter_init (&iter, dictionary);
while (!inner_error &&
g_variant_iter_next (&iter, "{sv}", &key, &value)) {
consume_variant (properties,
key,
value,
&inner_error);
g_free (key);
g_variant_unref (value);
}
/* If error, destroy the object */
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (properties);
properties = NULL;
}
return properties;
}
/*****************************************************************************/
MMCommonSmsProperties *
mm_common_sms_properties_dup (MMCommonSmsProperties *orig)
{
GVariant *dict;
MMCommonSmsProperties *copy;
GError *error = NULL;
dict = mm_common_sms_properties_get_dictionary (orig);
copy = mm_common_sms_properties_new_from_dictionary (dict, &error);
g_assert_no_error (error);
g_variant_unref (dict);
return copy;
}
/*****************************************************************************/
MMCommonSmsProperties *
mm_common_sms_properties_new (void)
{
return (MM_COMMON_SMS_PROPERTIES (
g_object_new (MM_TYPE_COMMON_SMS_PROPERTIES, NULL)));
}
static void
mm_common_sms_properties_init (MMCommonSmsProperties *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
MM_TYPE_COMMON_SMS_PROPERTIES,
MMCommonSmsPropertiesPrivate);
}
static void
finalize (GObject *object)
{
MMCommonSmsProperties *self = MM_COMMON_SMS_PROPERTIES (object);
g_free (self->priv->text);
g_free (self->priv->number);
g_free (self->priv->smsc);
G_OBJECT_CLASS (mm_common_sms_properties_parent_class)->finalize (object);
}
static void
mm_common_sms_properties_class_init (MMCommonSmsPropertiesClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (MMCommonSmsPropertiesPrivate));
object_class->finalize = finalize;
}

View File

@@ -0,0 +1,87 @@
/* -*- 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_COMMON_SMS_PROPERTIES_H
#define MM_COMMON_SMS_PROPERTIES_H
#include <ModemManager.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define MM_TYPE_COMMON_SMS_PROPERTIES (mm_common_sms_properties_get_type ())
#define MM_COMMON_SMS_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_COMMON_SMS_PROPERTIES, MMCommonSmsProperties))
#define MM_COMMON_SMS_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_COMMON_SMS_PROPERTIES, MMCommonSmsPropertiesClass))
#define MM_IS_COMMON_SMS_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_COMMON_SMS_PROPERTIES))
#define MM_IS_COMMON_SMS_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_COMMON_SMS_PROPERTIES))
#define MM_COMMON_SMS_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_COMMON_SMS_PROPERTIES, MMCommonSmsPropertiesClass))
typedef struct _MMCommonSmsProperties MMCommonSmsProperties;
typedef struct _MMCommonSmsPropertiesClass MMCommonSmsPropertiesClass;
typedef struct _MMCommonSmsPropertiesPrivate MMCommonSmsPropertiesPrivate;
struct _MMCommonSmsProperties {
GObject parent;
MMCommonSmsPropertiesPrivate *priv;
};
struct _MMCommonSmsPropertiesClass {
GObjectClass parent;
};
GType mm_common_sms_properties_get_type (void);
MMCommonSmsProperties *mm_common_sms_properties_new (void);
MMCommonSmsProperties *mm_common_sms_properties_new_from_string (
const gchar *str,
GError **error);
MMCommonSmsProperties *mm_common_sms_properties_new_from_dictionary (
GVariant *dictionary,
GError **error);
MMCommonSmsProperties *mm_common_sms_properties_dup (MMCommonSmsProperties *orig);
void mm_common_sms_properties_set_text (
MMCommonSmsProperties *properties,
const gchar *text);
void mm_common_sms_properties_set_number (
MMCommonSmsProperties *properties,
const gchar *number);
void mm_common_sms_properties_set_smsc (
MMCommonSmsProperties *properties,
const gchar *smsc);
void mm_common_sms_properties_set_validity (
MMCommonSmsProperties *properties,
guint validity);
void mm_common_sms_properties_set_class (
MMCommonSmsProperties *properties,
guint class);
const gchar *mm_common_sms_properties_get_text (
MMCommonSmsProperties *properties);
const gchar *mm_common_sms_properties_get_number (
MMCommonSmsProperties *properties);
const gchar *mm_common_sms_properties_get_smsc (
MMCommonSmsProperties *properties);
guint mm_common_sms_properties_get_validity (
MMCommonSmsProperties *properties);
guint mm_common_sms_properties_get_class (
MMCommonSmsProperties *properties);
GVariant *mm_common_sms_properties_get_dictionary (MMCommonSmsProperties *self);
G_END_DECLS
#endif /* MM_COMMON_SMS_PROPERTIES_H */