[libgweather/wip/hadess/app-id: 6/16] gweather: Add application-id property




commit 3d6ac0753e5701765d1f345d457f8b5a7caffc24
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 | 73 +++++++++++++++++++++++++++++++++++++++++-
 libgweather/gweather-weather.h |  5 +++
 4 files changed, 80 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..e5548a49 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
 };
 
@@ -772,6 +773,7 @@ gweather_info_finalize (GObject *object)
        gweather_location_unref (info->glocation);
 
     g_clear_pointer (&info->radar_url, g_free);
+    g_clear_pointer (&info->application_id, g_free);
 
     g_free (info->forecast_attribution);
 
@@ -2153,6 +2155,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 +2209,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 +2232,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 +2266,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 +2283,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 +2337,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]