OSDN Git Service

core: Fix being able to register the same custom property multiple times
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 21 Dec 2012 15:23:22 +0000 (17:23 +0200)
committerLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 21 Dec 2012 15:23:22 +0000 (17:23 +0200)
btd_profile_add_custom_prop should check if the a property with the same
name already exists, in addition to that btd_profile_add_custom_prop now
returns a boolean indicating the success or failure of the operation.

src/profile.c
src/profile.h

index 9c8cfa0..c838c1d 100644 (file)
@@ -2204,7 +2204,25 @@ static const GDBusMethodTable methods[] = {
        { }
 };
 
-void btd_profile_add_custom_prop(const char *uuid, const char *type,
+static struct btd_profile_custom_property *find_custom_prop(const char *uuid,
+                                                       const char *name)
+{
+       GSList *l;
+
+       for (l = custom_props; l; l = l->next) {
+               struct btd_profile_custom_property *prop = l->data;
+
+               if (strcasecmp(prop->uuid, uuid) != 0)
+                       continue;
+
+               if (g_strcmp0(prop->name, name) == 0)
+                       return prop;
+       }
+
+       return NULL;
+}
+
+bool btd_profile_add_custom_prop(const char *uuid, const char *type,
                                        const char *name,
                                        btd_profile_prop_exists exists,
                                        btd_profile_prop_get get,
@@ -2212,6 +2230,10 @@ void btd_profile_add_custom_prop(const char *uuid, const char *type,
 {
        struct btd_profile_custom_property *prop;
 
+       prop = find_custom_prop(uuid, name);
+       if (prop != NULL)
+               return false;
+
        prop = g_new0(struct btd_profile_custom_property, 1);
 
        prop->uuid = g_strdup(uuid);
@@ -2222,6 +2244,8 @@ void btd_profile_add_custom_prop(const char *uuid, const char *type,
        prop->user_data = user_data;
 
        custom_props = g_slist_append(custom_props, prop);
+
+       return true;
 }
 
 static void free_property(gpointer data)
@@ -2237,21 +2261,14 @@ static void free_property(gpointer data)
 
 bool btd_profile_remove_custom_prop(const char *uuid, const char *name)
 {
-       GSList *l;
-
-       for (l = custom_props; l; l = l->next) {
-               struct btd_profile_custom_property *prop = l->data;
-
-               if (strcasecmp(prop->uuid, uuid) != 0)
-                       continue;
+       struct btd_profile_custom_property *prop;
 
-               if (g_strcmp0(prop->name, name) != 0)
-                       continue;
+       prop = find_custom_prop(uuid, name);
+       if (prop == NULL)
+               return false;
 
-               custom_props = g_slist_delete_link(custom_props, l);
-               free_property(prop);
-               return true;
-       }
+       custom_props = g_slist_remove(custom_props, prop);
+       free_property(prop);
 
        return false;
 }
index 27ccdd3..d858925 100644 (file)
@@ -67,7 +67,7 @@ typedef bool (*btd_profile_prop_get)(const char *uuid,
                                                DBusMessageIter *iter,
                                                void *user_data);
 
-void btd_profile_add_custom_prop(const char *uuid, const char *type,
+bool btd_profile_add_custom_prop(const char *uuid, const char *type,
                                        const char *name,
                                        btd_profile_prop_exists exists,
                                        btd_profile_prop_get get,