[discident-glib] ean: Add parameters for Amazon lookups



commit 2c54b44221097b1b91ee68d712e16d58084cf813
Author: Bastien Nocera <hadess hadess net>
Date:   Sat Apr 27 19:07:55 2013 +0200

    ean: Add parameters for Amazon lookups
    
    So that the parameters can be passed from the application.

 discident-glib/discident-ean-glib.c         |   51 ++++++++++++++++++++++++++-
 discident-glib/discident-ean-glib.h         |   23 ++++++++++++
 discident-glib/discident-ean-private-glib.h |    5 +++
 discident-glib/discident-error.h            |    4 ++-
 discident-glib/discident-glib.symbols       |    1 +
 5 files changed, 82 insertions(+), 2 deletions(-)
---
diff --git a/discident-glib/discident-ean-glib.c b/discident-glib/discident-ean-glib.c
index a6c6a06..ce97d47 100644
--- a/discident-glib/discident-ean-glib.c
+++ b/discident-glib/discident-ean-glib.c
@@ -44,7 +44,8 @@ G_DEFINE_TYPE (DiscidentEan, discident_ean, G_TYPE_OBJECT)
 
 enum {
        PROP_0,
-       PROP_SERVICE
+       PROP_SERVICE,
+       PROP_PARAMETERS
 };
 
 const char *rl_services[] = {
@@ -66,6 +67,17 @@ service_is_valid (const char *service)
 }
 
 static void
+discident_ean_set_parameters (DiscidentEan *ean,
+                             GHashTable   *parameters)
+{
+       if (parameters == NULL)
+               return;
+       ean->priv->access_key = g_strdup (g_hash_table_lookup (parameters, DISCIDENT_PARAM_ACCESS_KEY));
+       ean->priv->private_key = g_strdup (g_hash_table_lookup (parameters, DISCIDENT_PARAM_PRIVATE_KEY));
+       ean->priv->associate_tag = g_strdup (g_hash_table_lookup (parameters, DISCIDENT_PARAM_ASSOCIATE_TAG));
+}
+
+static void
 discident_ean_set_property (GObject           *object,
                            guint              property_id,
                            const GValue      *value,
@@ -80,6 +92,9 @@ discident_ean_set_property (GObject           *object,
                else
                        g_warning ("Invalid service '%s'", g_value_get_string (value));
                break;
+       case PROP_PARAMETERS:
+               discident_ean_set_parameters (ean, g_value_get_boxed (value));
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
@@ -117,6 +132,10 @@ discident_ean_finalize (GObject *object)
        g_free (ean->priv->message);
        ean->priv->message = NULL;
 
+       g_clear_pointer (&ean->priv->access_key, g_free);
+       g_clear_pointer (&ean->priv->private_key, g_free);
+       g_clear_pointer (&ean->priv->associate_tag, g_free);
+
        G_OBJECT_CLASS (discident_ean_parent_class)->finalize (object);
 }
 
@@ -138,6 +157,13 @@ discident_ean_class_init (DiscidentEanClass *klass)
                                                               "service identifier",
                                                               "rl-en_US",
                                                               G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+        g_object_class_install_property (object_class,
+                                         PROP_PARAMETERS,
+                                         g_param_spec_boxed ("parameters",
+                                                             "service parameters",
+                                                             "service parameters",
+                                                             G_TYPE_HASH_TABLE,
+                                                             G_PARAM_CONSTRUCT | G_PARAM_WRITABLE));
 }
 
 static void
@@ -305,3 +331,26 @@ discident_ean_new (const char *service)
 
        return g_object_new (DISCIDENT_TYPE_EAN, NULL);
 }
+
+/**
+ * discident_ean_new_full:
+ * @service: The identifier for the service, or %NULL to use
+ * the default service.
+ * @parameters: a #GHashTable of string keys and string values. The keys
+ * to use depend on the service used.
+ *
+ * Create a new #DiscidentEan object to lookup barcodes with.
+ *
+ * Returns: a new #DiscidentEan object.
+ **/
+DiscidentEan *
+discident_ean_new_full (const char *service,
+                       GHashTable *parameters)
+{
+       g_return_val_if_fail (!service || service_is_valid (service), NULL);
+
+       if (service)
+               return g_object_new (DISCIDENT_TYPE_EAN, "service", service, "parameters", parameters, 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 a748e09..c562c3a 100644
--- a/discident-glib/discident-ean-glib.h
+++ b/discident-glib/discident-ean-glib.h
@@ -37,6 +37,27 @@ G_BEGIN_DECLS
 typedef struct DiscidentEanPrivate DiscidentEanPrivate;
 
 /**
+ * DISCIDENT_PARAM_ACCESS_KEY:
+ *
+ * Access key used for Amazon lookups.
+ **/
+#define DISCIDENT_PARAM_ACCESS_KEY "access-key"
+
+/**
+ * DISCIDENT_PARAM_PRIVATE_KEY:
+ *
+ * Private key used for signing Amazon lookups.
+ **/
+#define DISCIDENT_PARAM_PRIVATE_KEY "private-key"
+
+/**
+ * DISCIDENT_PARAM_ASSOCIATE_TAG:
+ *
+ * Associate tag used for Amazon lookups.
+ **/
+#define DISCIDENT_PARAM_ASSOCIATE_TAG "associate-tag"
+
+/**
  * DiscidentEan:
  *
  * All the fields in the #DiscidentEan structure are private and should never be accessed directly.
@@ -59,6 +80,8 @@ typedef struct {
 
 GType         discident_ean_get_type     (void);
 DiscidentEan *discident_ean_new          (const char *service);
+DiscidentEan *discident_ean_new_full     (const char *service,
+                                         GHashTable *parameters);
 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 6dfd1c0..e0320e5 100644
--- a/discident-glib/discident-ean-private-glib.h
+++ b/discident-glib/discident-ean-private-glib.h
@@ -31,6 +31,11 @@ struct DiscidentEanPrivate {
        gboolean enabled;
        char *message;
        gboolean login_done;
+
+       /* Amazon */
+       char *access_key;
+       char *private_key;
+       char *associate_tag;
 };
 
 
diff --git a/discident-glib/discident-error.h b/discident-glib/discident-error.h
index fcfeea2..1bbc686 100644
--- a/discident-glib/discident-error.h
+++ b/discident-glib/discident-error.h
@@ -31,12 +31,14 @@ G_BEGIN_DECLS
  * DiscidentError:
  * @DISCIDENT_ERROR_PARSE: An error occured parsing the response from the web service.
  * @DISCIDENT_ERROR_EMPTY_RESPONSE: No answers from the web service.
+ * @DISCIDENT_ERROR_MISSING_PARAMETERS: Parameters are missing to use that web service.
  *
  * Error codes returned by discident-glib functions.
  **/
 typedef enum {
        DISCIDENT_ERROR_PARSE,
-       DISCIDENT_ERROR_EMPTY_RESPONSE
+       DISCIDENT_ERROR_EMPTY_RESPONSE,
+       DISCIDENT_ERROR_MISSING_PARAMETERS
 } DiscidentError;
 
 /**
diff --git a/discident-glib/discident-glib.symbols b/discident-glib/discident-glib.symbols
index 4828c35..140646b 100644
--- a/discident-glib/discident-glib.symbols
+++ b/discident-glib/discident-glib.symbols
@@ -8,6 +8,7 @@ generate_hash
 discident_get_title_for_gtin
 discident_ean_get_type
 discident_ean_new
+discident_ean_new_full
 discident_ean_lookup_sync
 discident_ean_lookup
 discident_ean_lookup_get_barcode


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