[libgweather/wip/hadess/app-id: 1/9] gweather: Add application-id property




commit 083140aaef4534d2c6a9f66e70ae69e168346683
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Jan 12 11:38:34 2021 +0100

    gweather: Add application-id property
    
    We will use this to supplement the user-agent so that individual
    applications using libgweather are, well, identifiable.

 doc/libgweather-sections.txt   |  2 ++
 libgweather/gweather-private.h |  1 +
 libgweather/gweather-weather.c | 72 +++++++++++++++++++++++++++++++++++++++++-
 libgweather/gweather-weather.h |  5 +++
 4 files changed, 79 insertions(+), 1 deletion(-)
---
diff --git a/doc/libgweather-sections.txt b/doc/libgweather-sections.txt
index ad7d04cf..faa8421d 100644
--- a/doc/libgweather-sections.txt
+++ b/doc/libgweather-sections.txt
@@ -121,6 +121,8 @@ gweather_info_get_forecast_list
 GWeatherProvider
 gweather_info_get_enabled_providers
 gweather_info_set_enabled_providers
+gweather_info_get_application_id
+gweather_info_set_application_id
 gweather_info_get_attribution
 
 <SUBSECTION>
diff --git a/libgweather/gweather-private.h b/libgweather/gweather-private.h
index f5ea83be..e4609491 100644
--- a/libgweather/gweather-private.h
+++ b/libgweather/gweather-private.h
@@ -115,6 +115,7 @@ struct _GWeatherInfo {
 
     GWeatherProvider providers;
     GSettings *settings;
+    char *application_id;
 
     gboolean valid;
     gboolean network_error;
diff --git a/libgweather/gweather-weather.c b/libgweather/gweather-weather.c
index 7a9a02f3..7e2612b5 100644
--- a/libgweather/gweather-weather.c
+++ b/libgweather/gweather-weather.c
@@ -31,7 +31,7 @@
 #include <langinfo.h>
 #include <errno.h>
 
-#include <glib.h>
+#include <gio/gio.h>
 
 #include "gweather-weather.h"
 #include "gweather-private.h"
@@ -66,6 +66,7 @@ enum {
     PROP_0,
     PROP_LOCATION,
     PROP_ENABLED_PROVIDERS,
+    PROP_APPLICATION_ID,
     PROP_LAST
 };
 
@@ -2153,6 +2154,44 @@ gweather_info_set_enabled_providers (GWeatherInfo     *info,
     g_object_notify (G_OBJECT (info), "enabled-providers");
 }
 
+/**
+ * gweather_info_get_application_id:
+ * @info: a #GWeatherInfo
+ *
+ * Get the [application ID](https://docs.flatpak.org/en/latest/conventions.html#application-ids)
+ * of the application fetching the weather.
+ *
+ * Returns: the application ID
+ */
+const char *
+gweather_info_get_application_id (GWeatherInfo *info)
+{
+    g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
+
+    return info->application_id;
+}
+
+/**
+ * gweather_info_set_application_id:
+ * @info: a #GWeatherInfo
+ * @application_id: the application ID to set
+ *
+ * Sets the [application ID](https://docs.flatpak.org/en/latest/conventions.html#application-ids)
+ * of the application fetching the weather.
+ *
+ * If the application uses #GApplication, then the application ID
+ * will be automatically filled in.
+ */
+void
+gweather_info_set_application_id (GWeatherInfo *info,
+                                 const char   *application_id)
+{
+    g_return_if_fail (GWEATHER_IS_INFO (info));
+    g_return_if_fail (g_application_id_is_valid (application_id));
+
+    g_clear_pointer (&info->application_id, g_free);
+    info->application_id = g_strdup (application_id);
+}
 
 static void
 gweather_info_set_property (GObject *object,
@@ -2169,6 +2208,9 @@ gweather_info_set_property (GObject *object,
     case PROP_ENABLED_PROVIDERS:
        gweather_info_set_enabled_providers (self, g_value_get_flags (value));
        break;
+    case PROP_APPLICATION_ID:
+       gweather_info_set_application_id (self, g_value_get_string (value));
+       break;
     default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -2189,11 +2231,30 @@ gweather_info_get_property (GObject    *object,
     case PROP_ENABLED_PROVIDERS:
        g_value_set_flags (value, self->providers);
        break;
+    case PROP_APPLICATION_ID:
+        g_value_set_string (value, self->application_id);
+       break;
     default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
 }
 
+static void
+gweather_info_constructed (GObject *object)
+{
+    GWeatherInfo *info = GWEATHER_INFO (object);
+    GApplication *app;
+
+    if (info->application_id != NULL)
+        return;
+    app = g_application_get_default ();
+    if (!app)
+        return;
+
+    gweather_info_set_application_id (info,
+                                      g_application_get_application_id (app));
+}
+
 void
 gweather_info_class_init (GWeatherInfoClass *klass)
 {
@@ -2204,6 +2265,7 @@ gweather_info_class_init (GWeatherInfoClass *klass)
     gobject_class->finalize = gweather_info_finalize;
     gobject_class->set_property = gweather_info_set_property;
     gobject_class->get_property = gweather_info_get_property;
+    gobject_class->constructed = gweather_info_constructed;
 
     pspec = g_param_spec_boxed ("location",
                                "Location",
@@ -2220,6 +2282,13 @@ gweather_info_class_init (GWeatherInfoClass *klass)
                                G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
     g_object_class_install_property (gobject_class, PROP_ENABLED_PROVIDERS, pspec);
 
+    pspec = g_param_spec_string ("application-id",
+                                "Application ID",
+                                "An unique reverse-DNS application ID",
+                                NULL,
+                                G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
+    g_object_class_install_property (gobject_class, PROP_APPLICATION_ID, pspec);
+
     /**
      * GWeatherInfo::updated:
      * @object: the emitter of the signal.
@@ -2267,6 +2336,7 @@ _gweather_info_new_clone (GWeatherInfo *original)
     return g_object_new (GWEATHER_TYPE_INFO,
                          "location", original->glocation,
                          "enabled-providers", original->providers,
+                         "application-id", original->application_id,
                          NULL);
 }
 
diff --git a/libgweather/gweather-weather.h b/libgweather/gweather-weather.h
index a34f3107..02fcaa40 100644
--- a/libgweather/gweather-weather.h
+++ b/libgweather/gweather-weather.h
@@ -70,6 +70,11 @@ GWeatherProvider         gweather_info_get_enabled_providers (GWeatherInfo
 void                     gweather_info_set_enabled_providers (GWeatherInfo        *info,
                                                              GWeatherProvider     providers);
 
+const char             * gweather_info_get_application_id  (GWeatherInfo *info);
+void                     gweather_info_set_application_id  (GWeatherInfo *info,
+                                                           const char   *application_id);
+
+
 gboolean                gweather_info_is_valid            (GWeatherInfo *info);
 gboolean                gweather_info_network_error       (GWeatherInfo *info);
 


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