weather: Rework weather management

This commit brings a few changes that I was too lazy to
split in different commits:

 * Remove a few useless arguments in the GcalWeatherService API
 * Move the weather management code from GcalWindow to GcalWeatherSettings
 * A few style changes
This commit is contained in:
Georges Basile Stavracas Neto
2018-01-27 20:36:29 -02:00
parent ab20672aeb
commit 499db7bfd4
13 changed files with 568 additions and 392 deletions
+21 -28
View File
@@ -30,6 +30,11 @@
#include "gcal-weather-service.h"
#define GCAL_WEATHER_CHECK_INTERVAL_RENEW_DEFAULT (3 * 60 * 60) /* seconds */
#define GCAL_WEATHER_CHECK_INTERVAL_NEW_DEFAULT (5 * 60) /* seconds */
#define GCAL_WEATHER_VALID_TIMESPAN_DEFAULT (24 * 60 * 60) /* seconds */
#define GCAL_WEATHER_FORECAST_MAX_DAYS_DEFAULT 3
#define DAY_SECONDS (24 * 60 * 60)
/**
@@ -263,7 +268,7 @@ set_check_interval_new (GcalWeatherService *self,
self->check_interval_new = interval;
update_timeout_interval (self);
g_object_notify ((GObject*) self, "check-interval-new");
g_object_notify (G_OBJECT (self), "check-interval-new");
}
static void
@@ -273,7 +278,7 @@ set_check_interval_renew (GcalWeatherService *self,
self->check_interval_renew = interval;
update_timeout_interval (self);
g_object_notify ((GObject*) self, "check-interval-renew");
g_object_notify (G_OBJECT (self), "check-interval-renew");
}
static gssize
@@ -830,7 +835,7 @@ set_max_days (GcalWeatherService *self,
self->max_days = days;
g_object_notify ((GObject*) self, "max-days");
g_object_notify (G_OBJECT (self), "max-days");
}
static void
@@ -841,7 +846,7 @@ set_valid_timespan (GcalWeatherService *self,
self->valid_timespan = timespan;
g_object_notify ((GObject*) self, "valid-timespan");
g_object_notify (G_OBJECT (self), "valid-timespan");
}
static gboolean
@@ -1057,7 +1062,7 @@ gcal_weather_service_class_init (GcalWeatherServiceClass *klass)
"check-interval-new",
0,
G_MAXUINT,
GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT,
GCAL_WEATHER_CHECK_INTERVAL_NEW_DEFAULT,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
@@ -1073,7 +1078,7 @@ gcal_weather_service_class_init (GcalWeatherServiceClass *klass)
"check-interval-renew",
0,
G_MAXUINT,
GCAL_WEATHER_CHECK_INTERVAL_RENEW_DFLT,
GCAL_WEATHER_CHECK_INTERVAL_RENEW_DEFAULT,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
@@ -1088,7 +1093,7 @@ gcal_weather_service_class_init (GcalWeatherServiceClass *klass)
"valid-timespan",
0,
G_MAXINT64,
GCAL_WEATHER_VALID_TIMESPAN_DFLT,
GCAL_WEATHER_VALID_TIMESPAN_DEFAULT,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
@@ -1115,13 +1120,14 @@ gcal_weather_service_class_init (GcalWeatherServiceClass *klass)
static void
gcal_weather_service_init (GcalWeatherService *self)
{
self->check_interval_new = GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT;
self->check_interval_renew = GCAL_WEATHER_CHECK_INTERVAL_RENEW_DFLT;
self->max_days = GCAL_WEATHER_FORECAST_MAX_DAYS_DEFAULT;
self->check_interval_new = GCAL_WEATHER_CHECK_INTERVAL_NEW_DEFAULT;
self->check_interval_renew = GCAL_WEATHER_CHECK_INTERVAL_RENEW_DEFAULT;
self->location_cancellable = g_cancellable_new ();
self->weather_infos_upated = -1;
self->valid_timespan = -1;
self->valid_timespan = GCAL_WEATHER_VALID_TIMESPAN_DEFAULT;
self->duration_timer = gcal_timer_new (GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT);
self->duration_timer = gcal_timer_new (GCAL_WEATHER_CHECK_INTERVAL_NEW_DEFAULT);
gcal_timer_set_callback (self->duration_timer, (GCalTimerFunc) on_duration_timer_timeout, self, NULL);
self->midnight_timer = gcal_timer_new (24 * 60 * 60);
@@ -1135,29 +1141,16 @@ gcal_weather_service_init (GcalWeatherService *self)
/**
* gcal_weather_service_new:
* @max_days: mumber of days to fetch forecasts for.
* @check_interval_new: timeout used when fetching new weather reports.
* @check_interval_renew: timeout used to update valid weather reports.
*
* Creates a new #GcalWeatherService. This service listens
* to location and weather changes and reports them.
*
* Returns: (transfer full): A newly created #GcalWeatherService.
*/
GcalWeatherService *
gcal_weather_service_new (GTimeZone *time_zone,
guint max_days,
guint check_interval_new,
guint check_interval_renew,
gint64 valid_timespan)
GcalWeatherService*
gcal_weather_service_new (void)
{
return g_object_new (GCAL_TYPE_WEATHER_SERVICE,
"time-zone", time_zone,
"max-days", max_days,
"check-interval-new", check_interval_new,
"check-interval-renew", check_interval_renew,
"valid-timespan", valid_timespan,
NULL);
return g_object_new (GCAL_TYPE_WEATHER_SERVICE, NULL);
}
/**
@@ -1199,7 +1192,7 @@ gcal_weather_service_set_time_zone (GcalWeatherService *self,
/* make sure midnight is timed correctly: */
schedule_midnight (self);
g_object_notify ((GObject*) self, "time-zone");
g_object_notify (G_OBJECT (self), "time-zone");
}
/**
+1 -10
View File
@@ -27,21 +27,12 @@
G_BEGIN_DECLS
#define GCAL_WEATHER_CHECK_INTERVAL_RENEW_DFLT (3 * 60 * 60) /* seconds */
#define GCAL_WEATHER_CHECK_INTERVAL_NEW_DFLT (5 * 60) /* seconds */
#define GCAL_WEATHER_VALID_TIMESPAN_DFLT (24 * 60 * 60) /* seconds */
#define GCAL_WEATHER_FORECAST_MAX_DAYS_DFLT 3
#define GCAL_TYPE_WEATHER_SERVICE (gcal_weather_service_get_type())
G_DECLARE_FINAL_TYPE (GcalWeatherService, gcal_weather_service, GCAL, WEATHER_SERVICE, GObject)
GcalWeatherService* gcal_weather_service_new (GTimeZone *time_zone,
guint max_days,
guint check_interval_new,
guint check_interval_renew,
gint64 valid_timespan);
GcalWeatherService* gcal_weather_service_new (void);
GTimeZone* gcal_weather_service_get_time_zone (GcalWeatherService *self);
+391
View File
@@ -0,0 +1,391 @@
/* gcal-weather-settings.c
*
* Copyright © 2018 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gcal-manager.h"
#include "gcal-utils.h"
#include "gcal-weather-service.h"
#include "gcal-weather-settings.h"
struct _GcalWeatherSettings
{
GtkBox parent;
GtkSwitch *show_weather_switch;
GtkSwitch *weather_auto_location_switch;
GtkWidget *weather_location_entry;
GcalManager *manager;
GcalWeatherService *weather_service;
};
static void on_weather_location_searchbox_changed_cb (GWeatherLocationEntry *entry,
GcalWeatherSettings *self);
static void on_show_weather_changed_cb (GtkSwitch *wswitch,
GParamSpec *pspec,
GcalWeatherSettings *self);
static void on_weather_auto_location_changed_cb (GtkSwitch *lswitch,
GParamSpec *pspec,
GcalWeatherSettings *self);
G_DEFINE_TYPE (GcalWeatherSettings, gcal_weather_settings, GTK_TYPE_BOX)
enum
{
PROP_0,
PROP_MANAGER,
PROP_WEATHER_SERVICE,
N_PROPS
};
static GParamSpec *properties [N_PROPS] = { NULL, };
/*
* Auxiliary methods
*/
static void
load_weather_settings (GcalWeatherSettings *self)
{
g_autoptr (GVariant) location = NULL;
g_autoptr (GVariant) value = NULL;
g_autofree gchar *location_name = NULL;
GSettings *settings;
gboolean show_weather;
gboolean auto_location;
if (!self->manager)
return;
settings = gcal_manager_get_settings (self->manager);
value = g_settings_get_value (settings, "weather-settings");
g_variant_get (value, "(bbsmv)",
&show_weather,
&auto_location,
&location_name,
&location);
g_signal_handlers_block_by_func (self->show_weather_switch, on_show_weather_changed_cb, self);
g_signal_handlers_block_by_func (self->weather_auto_location_switch, on_weather_auto_location_changed_cb, self);
g_signal_handlers_block_by_func (self->weather_location_entry, on_weather_location_searchbox_changed_cb, self);
gtk_switch_set_active (self->show_weather_switch, show_weather);
gtk_switch_set_active (self->weather_auto_location_switch, auto_location);
if (!location && !auto_location)
{
GtkStyleContext *context;
context = gtk_widget_get_style_context (GTK_WIDGET (self->weather_location_entry));
gtk_entry_set_text (GTK_ENTRY (self->weather_location_entry), location_name);
gtk_style_context_add_class (context, "error");
}
else
{
g_autoptr (GWeatherLocation) weather_location;
GWeatherLocation *world;
world = gweather_location_get_world ();
weather_location = gweather_location_deserialize (world, location);
gweather_location_entry_set_location (GWEATHER_LOCATION_ENTRY (self->weather_location_entry), weather_location);
}
g_signal_handlers_unblock_by_func (self->show_weather_switch, on_show_weather_changed_cb, self);
g_signal_handlers_unblock_by_func (self->weather_auto_location_switch, on_weather_auto_location_changed_cb, self);
g_signal_handlers_unblock_by_func (self->weather_location_entry, on_weather_location_searchbox_changed_cb, self);
}
static void
save_weather_settings (GcalWeatherSettings *self)
{
g_autoptr (GWeatherLocation) location = NULL;
GSettings *settings;
GVariant *value;
GVariant *vlocation;
gboolean res;
if (!self->manager)
return;
location = gweather_location_entry_get_location (GWEATHER_LOCATION_ENTRY (self->weather_location_entry));
vlocation = location ? gweather_location_serialize (location) : NULL;
settings = gcal_manager_get_settings (self->manager);
value = g_variant_new ("(bbsmv)",
gtk_switch_get_active (self->show_weather_switch),
gtk_switch_get_active (self->weather_auto_location_switch),
gtk_entry_get_text (GTK_ENTRY (self->weather_location_entry)),
vlocation);
res = g_settings_set_value (settings, "weather-settings", value);
if (!res)
g_warning ("Could not persist weather settings");
}
static void
update_menu_weather_sensitivity (GcalWeatherSettings *self)
{
gboolean weather_enabled;
gboolean autoloc_enabled;
weather_enabled = gtk_switch_get_active (self->show_weather_switch);
autoloc_enabled = gtk_switch_get_active (self->weather_auto_location_switch);
gtk_widget_set_sensitive (GTK_WIDGET (self->weather_auto_location_switch), weather_enabled);
gtk_widget_set_sensitive (GTK_WIDGET (self->weather_location_entry), weather_enabled && !autoloc_enabled);
}
static GWeatherLocation*
get_checked_fixed_location (GcalWeatherSettings *self)
{
g_autoptr (GWeatherLocation) location;
location = gweather_location_entry_get_location (GWEATHER_LOCATION_ENTRY (self->weather_location_entry));
/*
* NOTE: This check feels shabby. However, I couldn't find a better
* one without iterating the model. has-custom-text does not work
* properly. Lets go with it for now.
*/
if (location && gweather_location_get_name (location))
return g_steal_pointer (&location);
return NULL;
}
static void
manage_weather_service (GcalWeatherSettings *self)
{
g_autoptr (GWeatherLocation) location = NULL;
gcal_weather_service_stop (self->weather_service);
if (!gtk_switch_get_active (self->show_weather_switch))
return;
if (gtk_switch_get_active (self->weather_auto_location_switch))
{
gcal_weather_service_run (self->weather_service, NULL);
return;
}
location = get_checked_fixed_location (self);
if (!location)
{
/* TODO: this one should get reported to users */
g_warning ("Unknown location '%s' selected", gtk_entry_get_text (GTK_ENTRY (self->weather_location_entry)));
return;
}
gcal_weather_service_run (self->weather_service, location);
}
/*
* Callbacks
*/
static void
on_show_weather_changed_cb (GtkSwitch *wswitch,
GParamSpec *pspec,
GcalWeatherSettings *self)
{
save_weather_settings (self);
update_menu_weather_sensitivity (self);
manage_weather_service (self);
}
static void
on_weather_auto_location_changed_cb (GtkSwitch *lswitch,
GParamSpec *pspec,
GcalWeatherSettings *self)
{
save_weather_settings (self);
update_menu_weather_sensitivity (self);
manage_weather_service (self);
}
static void
on_weather_location_searchbox_changed_cb (GWeatherLocationEntry *entry,
GcalWeatherSettings *self)
{
GtkStyleContext *context;
GWeatherLocation *location;
gboolean auto_location;
save_weather_settings (self);
context = gtk_widget_get_style_context (self->weather_location_entry);
auto_location = gtk_switch_get_active (self->weather_auto_location_switch);
location = get_checked_fixed_location (self);
if (!location && !auto_location)
{
gtk_style_context_add_class (context, "error");
}
else
{
gtk_style_context_remove_class (context, "error");
manage_weather_service (self);
gweather_location_unref (location);
}
}
/*
* GObject overrides
*/
static void
gcal_weather_settings_finalize (GObject *object)
{
GcalWeatherSettings *self = (GcalWeatherSettings *)object;
g_clear_object (&self->weather_service);
G_OBJECT_CLASS (gcal_weather_settings_parent_class)->finalize (object);
}
static void
gcal_weather_settings_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalWeatherSettings *self = GCAL_WEATHER_SETTINGS (object);
switch (prop_id)
{
case PROP_MANAGER:
g_value_set_object (value, self->manager);
break;
case PROP_WEATHER_SERVICE:
g_value_set_object (value, self->weather_service);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_weather_settings_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GcalWeatherSettings *self = GCAL_WEATHER_SETTINGS (object);
switch (prop_id)
{
case PROP_MANAGER:
if (g_set_object (&self->manager, g_value_get_object (value)))
{
load_weather_settings (self);
update_menu_weather_sensitivity (self);
manage_weather_service (self);
g_object_notify_by_pspec (object, properties[PROP_MANAGER]);
}
break;
case PROP_WEATHER_SERVICE:
gcal_weather_settings_set_weather_service (self, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_weather_settings_class_init (GcalWeatherSettingsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->finalize = gcal_weather_settings_finalize;
object_class->get_property = gcal_weather_settings_get_property;
object_class->set_property = gcal_weather_settings_set_property;
properties[PROP_MANAGER] = g_param_spec_object ("manager",
"Manager",
"Manager",
GCAL_TYPE_MANAGER,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_WEATHER_SERVICE] = g_param_spec_object ("weather-service",
"The weather service object",
"The weather service object",
GCAL_TYPE_WEATHER_SERVICE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/weather-settings.ui");
gtk_widget_class_bind_template_child (widget_class, GcalWeatherSettings, show_weather_switch);
gtk_widget_class_bind_template_child (widget_class, GcalWeatherSettings, weather_auto_location_switch);
gtk_widget_class_bind_template_child (widget_class, GcalWeatherSettings, weather_location_entry);
gtk_widget_class_bind_template_callback (widget_class, on_show_weather_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, on_weather_auto_location_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, on_weather_location_searchbox_changed_cb);
}
static void
gcal_weather_settings_init (GcalWeatherSettings *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}
GcalWeatherService*
gcal_weather_settings_get_weather_service (GcalWeatherSettings *self)
{
g_return_val_if_fail (GCAL_IS_WEATHER_SETTINGS (self), NULL);
return self->weather_service;
}
void
gcal_weather_settings_set_weather_service (GcalWeatherSettings *self,
GcalWeatherService *service)
{
g_return_if_fail (GCAL_IS_WEATHER_SETTINGS (self));
if (!g_set_object (&self->weather_service, service))
return;
/* Recover weather settings */
load_weather_settings (self);
update_menu_weather_sensitivity (self);
manage_weather_service (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WEATHER_SERVICE]);
}
+36
View File
@@ -0,0 +1,36 @@
/* gcal-weather-settings.h
*
* Copyright © 2018 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "gcal-weather-service.h"
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define GCAL_TYPE_WEATHER_SETTINGS (gcal_weather_settings_get_type())
G_DECLARE_FINAL_TYPE (GcalWeatherSettings, gcal_weather_settings, GCAL, WEATHER_SETTINGS, GtkBox)
GcalWeatherService* gcal_weather_settings_get_weather_service (GcalWeatherSettings *self);
void gcal_weather_settings_set_weather_service (GcalWeatherSettings *self,
GcalWeatherService *service);
G_END_DECLS