[discident-glib] ean: Add "service" property



commit 125efb0512f8534717e9a916ac296120a0eb7f55
Author: Bastien Nocera <hadess hadess net>
Date:   Sun Apr 7 01:26:29 2013 +0200

    ean: Add "service" property
    
    So we can switch to different providers.

 discident-glib/discident-ean-glib.c         |   79 ++++++++++++++++++++++++++-
 discident-glib/discident-ean-glib.h         |    2 +-
 discident-glib/discident-ean-private-glib.h |    3 +
 discident-glib/test-diglib.c                |    8 ++-
 4 files changed, 87 insertions(+), 5 deletions(-)
---
diff --git a/discident-glib/discident-ean-glib.c b/discident-glib/discident-ean-glib.c
index ab4dea5..a6c6a06 100644
--- a/discident-glib/discident-ean-glib.c
+++ b/discident-glib/discident-ean-glib.c
@@ -42,6 +42,68 @@
 
 G_DEFINE_TYPE (DiscidentEan, discident_ean, G_TYPE_OBJECT)
 
+enum {
+       PROP_0,
+       PROP_SERVICE
+};
+
+const char *rl_services[] = {
+       "rl-en_US",
+       "rl-en_GB",
+       "rl-fr_FR"
+};
+
+static gboolean
+service_is_valid (const char *service)
+{
+       guint i;
+
+       for (i = 0; i < G_N_ELEMENTS (rl_services); i++)
+               if (g_strcmp0 (service, rl_services[i]) == 0)
+                       return TRUE;
+
+       return FALSE;
+}
+
+static void
+discident_ean_set_property (GObject           *object,
+                           guint              property_id,
+                           const GValue      *value,
+                           GParamSpec        *pspec)
+{
+       DiscidentEan *ean = (DiscidentEan *) object;
+
+       switch (property_id) {
+       case PROP_SERVICE:
+               if (service_is_valid (g_value_get_string (value)))
+                       ean->priv->service = g_value_dup_string (value);
+               else
+                       g_warning ("Invalid service '%s'", g_value_get_string (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+               break;
+       }
+}
+
+static void
+discident_ean_get_property (GObject           *object,
+                           guint              property_id,
+                           GValue            *value,
+                           GParamSpec        *pspec)
+{
+       DiscidentEan *ean = (DiscidentEan *) object;
+
+       switch (property_id) {
+       case PROP_SERVICE:
+               g_value_set_string (value, ean->priv->service);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+               break;
+       }
+}
+
 static void
 discident_ean_finalize (GObject *object)
 {
@@ -64,8 +126,18 @@ discident_ean_class_init (DiscidentEanClass *klass)
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
        object_class->finalize = discident_ean_finalize;
+        object_class->set_property = discident_ean_set_property;
+        object_class->get_property = discident_ean_get_property;
 
        g_type_class_add_private (klass, sizeof (DiscidentEanPrivate));
+
+        g_object_class_install_property (object_class,
+                                         PROP_SERVICE,
+                                         g_param_spec_string ("service",
+                                                              "service name",
+                                                              "service identifier",
+                                                              "rl-en_US",
+                                                              G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
 }
 
 static void
@@ -218,13 +290,18 @@ discident_ean_lookup_get_barcode (DiscidentEan  *ean,
 
 /**
  * discident_ean_new:
+ * @service: The identifier for the service, or %NULL to use
+ * the default service.
  *
  * Create a new #DiscidentEan object to lookup barcodes with.
  *
  * Returns: a new #DiscidentEan object.
  **/
 DiscidentEan *
-discident_ean_new (void)
+discident_ean_new (const char *service)
 {
+       if (service)
+               return g_object_new (DISCIDENT_TYPE_EAN, "service", service, NULL);
+
        return g_object_new (DISCIDENT_TYPE_EAN, NULL);
 }
diff --git a/discident-glib/discident-ean-glib.h b/discident-glib/discident-ean-glib.h
index a7b473a..a748e09 100644
--- a/discident-glib/discident-ean-glib.h
+++ b/discident-glib/discident-ean-glib.h
@@ -58,7 +58,7 @@ typedef struct {
 } DiscidentEanClass;
 
 GType         discident_ean_get_type     (void);
-DiscidentEan *discident_ean_new          (void);
+DiscidentEan *discident_ean_new          (const char *service);
 gboolean      discident_ean_lookup_sync  (DiscidentEan *ean,
                                          const char   *barcode,
                                          char        **title,
diff --git a/discident-glib/discident-ean-private-glib.h b/discident-glib/discident-ean-private-glib.h
index ea42096..58ec8ba 100644
--- a/discident-glib/discident-ean-private-glib.h
+++ b/discident-glib/discident-ean-private-glib.h
@@ -24,6 +24,9 @@
 #define DISCIDENT_EAN_PRIVATE_GLIB_H
 
 struct DiscidentEanPrivate {
+       char *service;
+
+       /* RL */
        char *server;
        gboolean enabled;
        char *message;
diff --git a/discident-glib/test-diglib.c b/discident-glib/test-diglib.c
index b00ad2a..b9cb6e0 100644
--- a/discident-glib/test-diglib.c
+++ b/discident-glib/test-diglib.c
@@ -12,6 +12,7 @@
 static gboolean option_async = FALSE;
 static gboolean option_file_list = FALSE;
 static gboolean option_lookup_ean = FALSE;
+static char *ean_service = NULL;
 static guint num_queries = 0;
 static GMainLoop *loop = NULL;
 
@@ -88,7 +89,7 @@ test_ean (void)
        GError *error = NULL;
        gboolean ret;
 
-       ean = discident_ean_new ();
+       ean = discident_ean_new (NULL);
 
        /* The Little Book of Stress: Calm is for Wimps, Get Real, Get Stressed */
        ret = discident_ean_lookup_sync (ean, "9780091865856", &title, &img_url, &error);
@@ -112,7 +113,7 @@ test_ean_fail (void)
        GError *error = NULL;
        gboolean ret;
 
-       ean = discident_ean_new ();
+       ean = discident_ean_new (NULL);
 
        /* Ghost In The Shell, FR */
        ret = discident_ean_lookup_sync (ean, "3388330031138", &title, &img_url, &error);
@@ -219,7 +220,7 @@ handle_ean (const char *ean)
        GError *error = NULL;
        gboolean ret;
 
-       object = discident_ean_new ();
+       object = discident_ean_new (ean_service);
 
        if (!option_async) {
                ret = discident_ean_lookup_sync (object, ean, &title, &img_url, &error);
@@ -292,6 +293,7 @@ int main (int argc, char **argv)
                { "async", 'a', 0, G_OPTION_ARG_NONE, &option_async, "Use the async API", NULL },
                { "file-list", 'f', 0, G_OPTION_ARG_NONE, &option_file_list, "Show the file list instead of 
the GTIN", NULL },
                { "ean", 'e', 0, G_OPTION_ARG_NONE, &option_lookup_ean, "Look up an EAN rather than a disc", 
NULL },
+               { "ean-service", 's', 0, G_OPTION_ARG_STRING, &ean_service, "EAN lookup service to use", NULL 
},
                { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &uris, NULL, "[URI...]" },
                { NULL }
        };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]