[libgweather/wip/forecasts: 5/6] Allow enabling a subset of weather data providers



commit 4ace77264bf28381c85bb1f72e5dab35db6a6566
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Dec 1 02:43:48 2012 +0100

    Allow enabling a subset of weather data providers
    
    Not all providers are under the same licence, or have terms that are
    acceptable for all applications. This allows individual apps to choose
    what's best for them.

 libgweather/Makefile.am                |    3 +-
 libgweather/gweather-enum-types.c.tmpl |    1 +
 libgweather/gweather-weather.h         |   50 +++++++++++++++---
 libgweather/weather-priv.h             |    1 +
 libgweather/weather.c                  |   89 +++++++++++++++++++++++++++++---
 5 files changed, 127 insertions(+), 17 deletions(-)
---
diff --git a/libgweather/Makefile.am b/libgweather/Makefile.am
index e36da5e..bb857ef 100644
--- a/libgweather/Makefile.am
+++ b/libgweather/Makefile.am
@@ -16,12 +16,11 @@ gweather_old_headers = \
 gweather_new_headers = \
 	gweather-location.h location-entry.h \
 	gweather-timezone.h timezone-menu.h \
-	gweather-enums.h
+	gweather-weather.h gweather-enums.h
 libgweatherincdir = $(includedir)/libgweather-3.0/libgweather
 libgweatherinc_HEADERS = \
 	$(gweather_old_headers)		\
 	$(gweather_new_headers)		\
-	gweather-weather.h		\
 	gweather-enum-types.h
 
 noinst_HEADERS = weather-priv.h gweather-win32.h
diff --git a/libgweather/gweather-enum-types.c.tmpl b/libgweather/gweather-enum-types.c.tmpl
index c9521d4..bcb06e7 100644
--- a/libgweather/gweather-enum-types.c.tmpl
+++ b/libgweather/gweather-enum-types.c.tmpl
@@ -3,6 +3,7 @@
 #include "gweather-enum-types.h"
 #include "gweather-location.h"
 #include "gweather-enums.h"
+#include "gweather-weather.h"
 /*** END file-header ***/
 
 /*** BEGIN file-production ***/
diff --git a/libgweather/gweather-weather.h b/libgweather/gweather-weather.h
index 68d5854..70f43e1 100644
--- a/libgweather/gweather-weather.h
+++ b/libgweather/gweather-weather.h
@@ -30,6 +30,24 @@
 
 G_BEGIN_DECLS
 
+/**
+ * GWeatherProvider:
+ * @GWEATHER_PROVIDER_NONE: no provider, no weather information available
+ * @GWEATHER_PROVIDER_METAR: METAR office, providing current conditions worldwide
+ * @GWEATHER_PROVIDER_IWIN: US weather office, providing 7 days of forecast
+ * @GWEATHER_PROVIDER_YAHOO: Yahoo Weather Service, worldwide but non commercial only
+ * @GWEATHER_PROVIDER_YR_NO: Yr.no service, worldwide but requires attribution
+ * @GWEATHER_PROVIDER_ALL: enable all available providers
+ */
+typedef enum { /*< flags, underscore_name=gweather_provider >*/
+    GWEATHER_PROVIDER_NONE = 0,
+    GWEATHER_PROVIDER_METAR = 1,
+    GWEATHER_PROVIDER_IWIN = 1 << 2,
+    GWEATHER_PROVIDER_YAHOO = 1 << 3,
+    GWEATHER_PROVIDER_YR_NO = 1 << 4,
+    GWEATHER_PROVIDER_ALL = 31
+} GWeatherProvider;
+
 typedef struct _GWeatherInfo GWeatherInfo;
 typedef struct _GWeatherInfoClass GWeatherInfoClass;
 typedef struct _GWeatherInfoPrivate GWeatherInfoPrivate;
@@ -67,6 +85,10 @@ GWeatherInfo *           gweather_info_new_for_world       (GWeatherLocation *wo
 void                     gweather_info_update              (GWeatherInfo *info);
 void			 gweather_info_abort		   (GWeatherInfo *info);
 
+GWeatherProvider         gweather_info_get_enabled_providers (GWeatherInfo        *info);
+void                     gweather_info_set_enabled_providers (GWeatherInfo        *info,
+							      GWeatherProvider     providers);
+
 gboolean		 gweather_info_is_valid		   (GWeatherInfo *info);
 gboolean		 gweather_info_network_error	   (GWeatherInfo *info);
 
@@ -100,19 +122,31 @@ gint			 gweather_info_next_sun_event	   (GWeatherInfo *info);
 
 /* values retrieving functions */
 
-typedef enum _GWeatherWindDirection {
+typedef enum { /*< underscore_name=gweather_wind_direction >*/
     GWEATHER_WIND_INVALID = -1,
     GWEATHER_WIND_VARIABLE,
-    GWEATHER_WIND_N, GWEATHER_WIND_NNE, GWEATHER_WIND_NE, GWEATHER_WIND_ENE,
-    GWEATHER_WIND_E, GWEATHER_WIND_ESE, GWEATHER_WIND_SE, GWEATHER_WIND_SSE,
-    GWEATHER_WIND_S, GWEATHER_WIND_SSW, GWEATHER_WIND_SW, GWEATHER_WIND_WSW,
-    GWEATHER_WIND_W, GWEATHER_WIND_WNW, GWEATHER_WIND_NW, GWEATHER_WIND_NNW,
+    GWEATHER_WIND_N,
+    GWEATHER_WIND_NNE,
+    GWEATHER_WIND_NE,
+    GWEATHER_WIND_ENE,
+    GWEATHER_WIND_E,
+    GWEATHER_WIND_ESE,
+    GWEATHER_WIND_SE,
+    GWEATHER_WIND_SSE,
+    GWEATHER_WIND_S,
+    GWEATHER_WIND_SSW,
+    GWEATHER_WIND_SW,
+    GWEATHER_WIND_WSW,
+    GWEATHER_WIND_W,
+    GWEATHER_WIND_WNW,
+    GWEATHER_WIND_NW,
+    GWEATHER_WIND_NNW,
     GWEATHER_WIND_LAST
 } GWeatherWindDirection;
 
 const gchar * gweather_wind_direction_to_string (GWeatherWindDirection wind);
 
-typedef enum _GWeatherSky {
+typedef enum { /*< underscore_name=gweather_sky >*/
     GWEATHER_SKY_INVALID = -1,
     GWEATHER_SKY_CLEAR,
     GWEATHER_SKY_BROKEN,
@@ -124,7 +158,7 @@ typedef enum _GWeatherSky {
 
 const gchar * gweather_sky_to_string (GWeatherSky sky);
 
-typedef enum _GWeatherConditionPhenomenon {
+typedef enum { /*< underscore_name=gweather_phenomenon >*/
     GWEATHER_PHENOMENON_INVALID = -1,
 
     GWEATHER_PHENOMENON_NONE,
@@ -158,7 +192,7 @@ typedef enum _GWeatherConditionPhenomenon {
     GWEATHER_PHENOMENON_LAST
 } GWeatherConditionPhenomenon;
 
-typedef enum _GWeatherConditionQualifier {
+typedef enum { /*< underscore_name=gweather_qualifier >*/
     GWEATHER_QUALIFIER_INVALID = -1,
 
     GWEATHER_QUALIFIER_NONE,
diff --git a/libgweather/weather-priv.h b/libgweather/weather-priv.h
index 2fb2f9b..1e71075 100644
--- a/libgweather/weather-priv.h
+++ b/libgweather/weather-priv.h
@@ -103,6 +103,7 @@ typedef time_t GWeatherUpdate;
 
 struct _GWeatherInfoPrivate {
     GWeatherForecastType forecast_type;
+    GWeatherProvider providers;
 
     GSettings *settings;
 
diff --git a/libgweather/weather.c b/libgweather/weather.c
index b6ab9a1..6f70317 100644
--- a/libgweather/weather.c
+++ b/libgweather/weather.c
@@ -66,6 +66,7 @@ enum {
     PROP_WORLD,
     PROP_LOCATION,
     PROP_TYPE,
+    PROP_ENABLED_PROVIDERS,
     PROP_LAST
 };
 
@@ -507,6 +508,7 @@ void
 gweather_info_update (GWeatherInfo *info)
 {
     GWeatherInfoPrivate *priv = info->priv;
+    gboolean ok;
 
     /* Update in progress */
     if (!requests_init (info))
@@ -521,22 +523,29 @@ gweather_info_update (GWeatherInfo *info)
 #endif
     }
 
-    metar_start_open (info);
+    if (priv->providers & GWEATHER_PROVIDER_METAR)
+	metar_start_open (info);
 
     if (priv->radar) {
         wx_start_open (info);
     }
 
+    ok = FALSE;
     /* Try national forecast services first */
-    if (iwin_start_open (info))
+    if (priv->providers & GWEATHER_PROVIDER_IWIN)
+	ok = iwin_start_open (info);
+    if (ok)
 	return;
 
     /* Try yr.no next */
-    if (yrno_start_open (info))
+    if (priv->providers & GWEATHER_PROVIDER_YR_NO)
+	ok = yrno_start_open (info);
+    if (ok)
 	return;
 
     /* Try Yahoo! Weather next */
-    yahoo_start_open (info);
+    if (priv->providers & GWEATHER_PROVIDER_YAHOO)
+	yahoo_start_open (info);
 }
 
 void
@@ -1810,6 +1819,32 @@ gweather_info_set_location (GWeatherInfo     *info,
     gweather_info_update (info);
 }
 
+GWeatherProvider
+gweather_info_get_enabled_providers (GWeatherInfo *info)
+{
+    g_return_val_if_fail (GWEATHER_IS_INFO (info),
+			  GWEATHER_PROVIDER_NONE);
+
+    return info->priv->providers;
+}
+
+void
+gweather_info_set_enabled_providers (GWeatherInfo     *info,
+				     GWeatherProvider  providers)
+{
+    g_return_if_fail (GWEATHER_IS_INFO (info));
+
+    if (info->priv->providers == providers)
+	return;
+
+    info->priv->providers = providers;
+
+    gweather_info_abort (info);
+    gweather_info_update (info);
+    g_object_notify (G_OBJECT (info), "enabled-providers");
+}
+
+
 static void
 gweather_info_set_property (GObject *object,
 			    guint property_id,
@@ -1829,6 +1864,37 @@ gweather_info_set_property (GObject *object,
     case PROP_TYPE:
 	priv->forecast_type = g_value_get_enum (value);
 	break;
+    case PROP_ENABLED_PROVIDERS:
+	gweather_info_set_enabled_providers (self, g_value_get_flags (value));
+	break;
+
+    default:
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+gweather_info_get_property (GObject    *object,
+			    guint       property_id,
+			    GValue     *value,
+			    GParamSpec *pspec)
+{
+    GWeatherInfo *self = GWEATHER_INFO (object);
+    GWeatherInfoPrivate *priv = self->priv;
+
+    switch (property_id) {
+    case PROP_WORLD:
+	g_value_set_boxed (value, priv->world);
+	break;
+    case PROP_LOCATION:
+	g_value_set_boxed (value, priv->glocation);
+	break;
+    case PROP_TYPE:
+	g_value_set_enum (value, priv->forecast_type);
+	break;
+    case PROP_ENABLED_PROVIDERS:
+	g_value_set_flags (value, priv->providers);
+	break;
     default:
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -1844,19 +1910,20 @@ 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;
 
     pspec = g_param_spec_boxed ("world",
 				"World",
 				"The hierarchy of locations containing the desired location",
 				GWEATHER_TYPE_LOCATION,
-				G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
+				G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
     g_object_class_install_property (gobject_class, PROP_WORLD, pspec);
 
     pspec = g_param_spec_boxed ("location",
 				"Location",
 				"The location this info represents",
 				GWEATHER_TYPE_LOCATION,
-				G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
+				G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
     g_object_class_install_property (gobject_class, PROP_LOCATION, pspec);
 
     pspec = g_param_spec_enum ("forecast-type",
@@ -1864,9 +1931,17 @@ gweather_info_class_init (GWeatherInfoClass *klass)
 			       "The type of forecast desired (list, zone or state)",
 			       GWEATHER_TYPE_FORECAST_TYPE,
 			       GWEATHER_FORECAST_LIST,
-			       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
+			       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
     g_object_class_install_property (gobject_class, PROP_TYPE, pspec);
 
+    pspec = g_param_spec_flags ("enabled-providers",
+				"Enabled providers",
+				"A bitmask of enabled weather service providers",
+				GWEATHER_TYPE_PROVIDER,
+				GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_IWIN,
+				G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
+    g_object_class_install_property (gobject_class, PROP_ENABLED_PROVIDERS, pspec);
+
     gweather_info_signals[SIGNAL_UPDATED] = g_signal_new ("updated",
 							  GWEATHER_TYPE_INFO,
 							  G_SIGNAL_RUN_FIRST,



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