Provision argument for handling language
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 27 Feb 2018 11:02:36 +0000 (12:02 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 27 Feb 2018 11:02:36 +0000 (12:02 +0100)
The requests 'runnables' and 'detail' accept a parameter
'lang' to -in future- get localized data about apps.

Change-Id: I5f1804870c6b3c0d1aedf1a75cde1e2c09a5a605
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afm-binding.c
src/afm-udb.c
src/afm-udb.h

index 7fe9f3d..e4375a5 100644 (file)
@@ -39,6 +39,7 @@ static const char _a_l_c_[]     = "application-list-changed";
 static const char _detail_[]    = "detail";
 static const char _id_[]        = "id";
 static const char _install_[]   = "install";
+static const char _lang_[]      = "lang";
 static const char _not_found_[] = "not-found";
 static const char _once_[]      = "once";
 static const char _pause_[]     = "pause";
@@ -89,6 +90,22 @@ static void application_list_changed(const char *operation, const char *data)
        afb_event_broadcast(applist_changed_event, e);
 }
 
+/*
+ * Retrieve the required language from 'req'.
+ */
+static const char *get_lang(struct afb_req req)
+{
+       const char *lang;
+
+       /* get the optional language */
+       lang = afb_req_value(req, _lang_);
+
+       /* TODO use the req to get the lang of the session (if any) */
+
+       return lang;
+}
+
+
 /*
  * retrieves the 'appid' in parameters received with the
  * request 'req' for the 'method'.
@@ -187,9 +204,14 @@ static void reply_status(struct afb_req req, int status, const char *errstr)
  */
 static void runnables(struct afb_req req)
 {
+       const char *lang;
        struct json_object *resp;
-       INFO("method runnables called");
-       resp = afm_udb_applications_public(afudb, afb_req_get_uid(req));
+
+       /* get the language */
+       lang = get_lang(req);
+
+       /* get the details */
+       resp = afm_udb_applications_public(afudb, afb_req_get_uid(req), lang);
        afb_req_success(req, resp, NULL);
 }
 
@@ -198,6 +220,7 @@ static void runnables(struct afb_req req)
  */
 static void detail(struct afb_req req)
 {
+       const char *lang;
        const char *appid;
        struct json_object *resp;
 
@@ -205,8 +228,11 @@ static void detail(struct afb_req req)
        if (!onappid(req, _detail_, &appid))
                return;
 
+       /* get the language */
+       lang = get_lang(req);
+
        /* wants details for appid */
-       resp = afm_udb_get_application_public(afudb, appid, afb_req_get_uid(req));
+       resp = afm_udb_get_application_public(afudb, appid, afb_req_get_uid(req), lang);
        if (resp)
                afb_req_success(req, resp, NULL);
        else
@@ -324,7 +350,6 @@ static void terminate(struct afb_req req)
 static void runners(struct afb_req req)
 {
        struct json_object *resp;
-       INFO("method runners called");
        resp = afm_urun_list(afudb, afb_req_get_uid(req));
        afb_req_success(req, resp, NULL);
 }
@@ -342,6 +367,9 @@ static void state(struct afb_req req)
        }
 }
 
+/*
+ * On querying installation of widget(s)
+ */
 static void install(struct afb_req req)
 {
        const char *wgtfile;
index 718aa5c..26a1f85 100644 (file)
@@ -78,6 +78,11 @@ struct afm_updt {
        struct afm_apps applications;
 };
 
+/*
+ * The default language
+ */
+static char *default_lang;
+
 /*
  * Release the data of the afm_apps object 'apps'.
  */
@@ -469,6 +474,13 @@ error:
        return -1;
 }
 
+void afm_udb_set_default_lang(const char *lang)
+{
+       char *oldval = default_lang;
+       default_lang = lang ? strdup(lang) : NULL;
+       free(oldval);
+}
+
 /*
  * Get the list of the applications private data of the afm_udb object 'afudb'.
  * The list is returned as a JSON-array that must be released using
@@ -486,7 +498,7 @@ struct json_object *afm_udb_applications_private(struct afm_udb *afudb, int uid)
  * 'json_object_put'.
  * Returns NULL in case of error.
  */
-struct json_object *afm_udb_applications_public(struct afm_udb *afudb, int uid)
+struct json_object *afm_udb_applications_public(struct afm_udb *afudb, int uid, const char *lang)
 {
        return json_object_get(afudb->applications.pubarr);
 }
@@ -496,7 +508,7 @@ struct json_object *afm_udb_applications_public(struct afm_udb *afudb, int uid)
  * It returns a JSON-object that must be released using 'json_object_put'.
  * Returns NULL in case of error.
  */
-static struct json_object *get_no_case(struct json_object *object, const char *id, int uid)
+static struct json_object *get_no_case(struct json_object *object, const char *id, int uid, const char *lang)
 {
        struct json_object *result;
        struct json_object_iter i;
@@ -520,7 +532,7 @@ static struct json_object *get_no_case(struct json_object *object, const char *i
  */
 struct json_object *afm_udb_get_application_private(struct afm_udb *afudb, const char *id, int uid)
 {
-       return get_no_case(afudb->applications.prvobj, id, uid);
+       return get_no_case(afudb->applications.prvobj, id, uid, NULL);
 }
 
 /*
@@ -529,9 +541,9 @@ struct json_object *afm_udb_get_application_private(struct afm_udb *afudb, const
  * Returns NULL in case of error.
  */
 struct json_object *afm_udb_get_application_public(struct afm_udb *afudb,
-                                                       const char *id, int uid)
+                                                       const char *id, int uid, const char *lang)
 {
-       return get_no_case(afudb->applications.pubobj, id, uid);
+       return get_no_case(afudb->applications.pubobj, id, uid, lang);
 }
 
 
index dc30c9c..fd15e05 100644 (file)
@@ -23,8 +23,9 @@ extern struct afm_udb *afm_udb_create(int sys, int usr, const char *prefix);
 extern void afm_udb_addref(struct afm_udb *afdb);
 extern void afm_udb_unref(struct afm_udb *afdb);
 extern int afm_udb_update(struct afm_udb *afdb);
+extern void afm_udb_set_default_lang(const char *lang);
 extern struct json_object *afm_udb_applications_private(struct afm_udb *afdb, int uid);
-extern struct json_object *afm_udb_applications_public(struct afm_udb *afdb, int uid);
 extern struct json_object *afm_udb_get_application_private(struct afm_udb *afdb, const char *id, int uid);
-extern struct json_object *afm_udb_get_application_public(struct afm_udb *afdb, const char *id, int uid);
+extern struct json_object *afm_udb_applications_public(struct afm_udb *afdb, int uid, const char *lang);
+extern struct json_object *afm_udb_get_application_public(struct afm_udb *afdb, const char *id, int uid, const char *lang);