weather: Use -night icons only for fold-results when it makes sense.

This commit is contained in:
Florian Brosch
2017-12-11 21:29:33 -02:00
committed by Georges Basile Stavracas Neto
parent b1b5cf2d73
commit a58047257b
+78 -30
View File
@@ -28,6 +28,18 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/**
* Internal structure used to manage known
* weather icons.
*/
typedef struct
{
gchar *name;
gboolean night_support;
} GcalWeatherIconInfo;
/* GcalWeatherService: /* GcalWeatherService:
* *
* @time_zone: The current time zone * @time_zone: The current time zone
@@ -137,9 +149,11 @@ static void gcal_weather_service_set_check_interval (GcalWeatherService
static gssize get_normalized_icon_name_len (const gchar *str); static gssize get_normalized_icon_name_len (const gchar *str);
static gchar* get_normalized_icon_name (GWeatherInfo *gwi); static gchar* get_normalized_icon_name (GWeatherInfo *gwi,
gboolean is_night_icon);
static gint get_icon_name_sortkey (const gchar *icon_name); static gint get_icon_name_sortkey (const gchar *icon_name,
gboolean *supports_night_icon);
static void gcal_weather_service_timer_stop (GcalWeatherService *self); static void gcal_weather_service_timer_stop (GcalWeatherService *self);
@@ -155,6 +169,7 @@ static inline gboolean get_gweather_temperature (GWeatherInfo
gdouble *temp); gdouble *temp);
static gboolean compute_weather_info_data (GSList *samples, static gboolean compute_weather_info_data (GSList *samples,
gboolean is_today,
gchar **icon_name, gchar **icon_name,
gchar **temperature); gchar **temperature);
@@ -387,14 +402,19 @@ get_normalized_icon_name_len (const gchar *str)
* Returns: (transfer full): A normalized icon name. * Returns: (transfer full): A normalized icon name.
*/ */
static gchar* static gchar*
get_normalized_icon_name (GWeatherInfo* wi) get_normalized_icon_name (GWeatherInfo* wi,
gboolean is_night_icon)
{ {
const gchar pfx[] = "-symbolic"; const gchar night_pfx[] = "-night";
const gsize pfx_size = G_N_ELEMENTS (pfx) - 1; const gsize night_pfx_size = G_N_ELEMENTS (night_pfx) - 1;
const gchar sym_pfx[] = "-symbolic";
const gsize sym_pfx_size = G_N_ELEMENTS (sym_pfx) - 1;
const gchar *str; /* unowned */ const gchar *str; /* unowned */
gssize normalized_size; gssize normalized_size;
gchar *buffer = NULL; /* owned */ gchar *buffer = NULL; /* owned */
gchar *bufpos = NULL; /* unowned */
gsize buffer_size; gsize buffer_size;
g_return_val_if_fail (wi != NULL, NULL); g_return_val_if_fail (wi != NULL, NULL);
@@ -406,11 +426,24 @@ get_normalized_icon_name (GWeatherInfo* wi)
normalized_size = get_normalized_icon_name_len (str); normalized_size = get_normalized_icon_name_len (str);
g_return_val_if_fail (normalized_size >= 0, NULL); g_return_val_if_fail (normalized_size >= 0, NULL);
buffer_size = normalized_size + pfx_size + 1; if (is_night_icon)
buffer = g_malloc (buffer_size); buffer_size = normalized_size + night_pfx_size + sym_pfx_size + 1;
else
buffer_size = normalized_size + sym_pfx_size + 1;
memcpy (buffer, str, normalized_size); buffer = g_malloc (buffer_size);
memcpy (buffer + normalized_size, pfx, pfx_size); bufpos = buffer;
memcpy (bufpos, str, normalized_size);
bufpos = bufpos + normalized_size;
if (is_night_icon)
{
memcpy (bufpos, night_pfx, night_pfx_size);
bufpos = bufpos + night_pfx_size;
}
memcpy (bufpos, sym_pfx, sym_pfx_size);
buffer[buffer_size - 1] = '\0'; buffer[buffer_size - 1] = '\0';
return buffer; return buffer;
@@ -426,7 +459,8 @@ get_normalized_icon_name (GWeatherInfo* wi)
* The lower the key, the better the weather. * The lower the key, the better the weather.
*/ */
static gint static gint
get_icon_name_sortkey (const gchar *icon_name) get_icon_name_sortkey (const gchar *icon_name,
gboolean *supports_night_icon)
{ {
/* Note that we can't use gweathers condition /* Note that we can't use gweathers condition
* field as it is not necessarily holds valid values. * field as it is not necessarily holds valid values.
@@ -437,27 +471,33 @@ get_icon_name_sortkey (const gchar *icon_name)
gssize normalized_name_len; gssize normalized_name_len;
const gchar* icons[] = const GcalWeatherIconInfo icons[] =
{ "weather-clear", { {"weather-clear", FALSE},
"weather-few-clouds", {"weather-few-clouds", FALSE},
"weather-overcast", {"weather-overcast", FALSE},
"weather-fog", {"weather-fog", FALSE},
"weather-showers-scattered", {"weather-showers-scattered", FALSE},
"weather-showers", {"weather-showers", FALSE},
"weather-snow", {"weather-snow", FALSE},
"weather-storm", {"weather-storm", FALSE},
"weather-severe-alert" {"weather-severe-alert", FALSE}
}; };
g_return_val_if_fail (icon_name != NULL, -1); g_return_val_if_fail (icon_name != NULL, -1);
g_return_val_if_fail (supports_night_icon != NULL, -1);
*supports_night_icon = FALSE;
normalized_name_len = get_normalized_icon_name_len (icon_name); normalized_name_len = get_normalized_icon_name_len (icon_name);
g_return_val_if_fail (normalized_name_len >= 0, -1); g_return_val_if_fail (normalized_name_len >= 0, -1);
for (int i = 0; i < G_N_ELEMENTS (icons); i++) for (int i = 0; i < G_N_ELEMENTS (icons); i++)
{ {
if (icons[i][normalized_name_len] == '\0' && strncmp (icon_name, icons[i], normalized_name_len) == 0) if (icons[i].name[normalized_name_len] == '\0' && strncmp (icon_name, icons[i].name, normalized_name_len) == 0)
return i; {
*supports_night_icon = icons[i].night_support;
return i;
}
} }
g_warning ("Unknown weather icon '%s'", icon_name); g_warning ("Unknown weather icon '%s'", icon_name);
@@ -596,14 +636,17 @@ get_gweather_temperature (GWeatherInfo *gwi,
* Returns: %TRUE if there is a valid icon name and temperature. * Returns: %TRUE if there is a valid icon name and temperature.
*/ */
static gboolean static gboolean
compute_weather_info_data (GSList *samples, compute_weather_info_data (GSList *samples,
gchar **icon_name, gboolean is_today,
gchar **temperature) gchar **icon_name,
gchar **temperature)
{ {
gboolean phenomenon_supports_night_icon = FALSE;
gint phenomenon_val = -1; gint phenomenon_val = -1;
GWeatherInfo *phenomenon_gwi = NULL; /* unowned */ GWeatherInfo *phenomenon_gwi = NULL; /* unowned */
gdouble temp_val = NAN; gdouble temp_val = NAN;
GWeatherInfo *temp_gwi = NULL; /* unowned */ GWeatherInfo *temp_gwi = NULL; /* unowned */
gboolean has_daytime = FALSE;
GSList *iter; /* unowned */ GSList *iter; /* unowned */
/* Note: I checked three different gweather consumers /* Note: I checked three different gweather consumers
@@ -623,7 +666,8 @@ compute_weather_info_data (GSList *samples,
{ {
GWeatherInfo *gwi; /* unowned */ GWeatherInfo *gwi; /* unowned */
const gchar *icon_name; /* unowned */ const gchar *icon_name; /* unowned */
gint phenomenon = -1; gint phenomenon = -1;
gboolean supports_night_icon;
gdouble temp; gdouble temp;
gboolean valid_temp; gboolean valid_temp;
@@ -631,12 +675,13 @@ compute_weather_info_data (GSList *samples,
icon_name = gweather_info_get_icon_name (gwi); icon_name = gweather_info_get_icon_name (gwi);
if (icon_name != NULL) if (icon_name != NULL)
phenomenon = get_icon_name_sortkey (icon_name); phenomenon = get_icon_name_sortkey (icon_name, &supports_night_icon);
valid_temp = get_gweather_temperature (gwi, &temp); valid_temp = get_gweather_temperature (gwi, &temp);
if (icon_name != NULL && (phenomenon_gwi == NULL || phenomenon > phenomenon_val)) if (phenomenon >= 0 && (phenomenon_gwi == NULL || phenomenon > phenomenon_val))
{ {
phenomenon_supports_night_icon = supports_night_icon;
phenomenon_val = phenomenon; phenomenon_val = phenomenon;
phenomenon_gwi = gwi; phenomenon_gwi = gwi;
} }
@@ -646,11 +691,14 @@ compute_weather_info_data (GSList *samples,
temp_val = temp; temp_val = temp;
temp_gwi = gwi; temp_gwi = gwi;
} }
if (gweather_info_is_daytime (gwi))
has_daytime = TRUE;
} }
if (phenomenon_gwi != NULL && temp_gwi != NULL) if (phenomenon_gwi != NULL && temp_gwi != NULL)
{ {
*icon_name = get_normalized_icon_name (phenomenon_gwi); *icon_name = get_normalized_icon_name (phenomenon_gwi, is_today && !has_daytime && phenomenon_supports_night_icon);
*temperature = gweather_info_get_temp (temp_gwi); *temperature = gweather_info_get_temp (temp_gwi);
return TRUE; return TRUE;
} }
@@ -744,7 +792,7 @@ preprocess_gweather_reports (GcalWeatherService *self,
g_autofree gchar *icon_name; g_autofree gchar *icon_name;
g_autofree gchar *temperature; g_autofree gchar *temperature;
if (compute_weather_info_data (days[i], &icon_name, &temperature)) if (compute_weather_info_data (days[i], i == 0, &icon_name, &temperature))
{ {
GcalWeatherInfo* gcwi; /* owned */ GcalWeatherInfo* gcwi; /* owned */