weather-service: use GPtrArray for public API
Using GLists and GSLists is a bad practice. These data structures are too much inneficient and GPtrArray has the advantage of having a free function pointer, so we can hide the underlying free function from the consumers of this API. This also gets rid of another allocation cycle in Month view.
This commit is contained in:
+32
-42
@@ -901,40 +901,20 @@ setup_month_grid (GcalMonthView *self,
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
update_weather (GcalMonthView *self,
|
||||
gboolean clear_old)
|
||||
static GcalWeatherInfo*
|
||||
get_weather_info_for_cell (GcalMonthView *self,
|
||||
guint cell)
|
||||
{
|
||||
GcalMonthCell *first_cell;
|
||||
GPtrArray *weather_infos;
|
||||
GDateTime *first_dt;
|
||||
GSList *weather_infos;
|
||||
GSList *l;
|
||||
GDate first;
|
||||
guint i;
|
||||
|
||||
g_return_if_fail (GCAL_IS_MONTH_VIEW (self));
|
||||
|
||||
/* Drop old weather information */
|
||||
if (clear_old)
|
||||
{
|
||||
guint row;
|
||||
guint col;
|
||||
|
||||
for (row = 0; row < 6; row++)
|
||||
{
|
||||
for (col = 0; col < 7; col++)
|
||||
{
|
||||
GcalMonthCell *cell;
|
||||
|
||||
cell = GCAL_MONTH_CELL (self->month_cell[row][col]);
|
||||
gcal_month_cell_set_weather (cell, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Set new one */
|
||||
if (!self->weather_service)
|
||||
return;
|
||||
return NULL;
|
||||
|
||||
weather_infos = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
|
||||
first_cell = GCAL_MONTH_CELL (self->month_cell[0][0]);
|
||||
first_dt = gcal_month_cell_get_date (first_cell);
|
||||
@@ -944,30 +924,40 @@ update_weather (GcalMonthView *self,
|
||||
g_date_time_get_month (first_dt),
|
||||
g_date_time_get_year (first_dt));
|
||||
|
||||
weather_infos = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
|
||||
for (l = weather_infos; l; l = l->next)
|
||||
for (i = 0; weather_infos && i < weather_infos->len; i++)
|
||||
{
|
||||
GcalWeatherInfo *info;
|
||||
GDate weather_date;
|
||||
gint day_difference;
|
||||
|
||||
info = GCAL_WEATHER_INFO (l->data);
|
||||
info = g_ptr_array_index (weather_infos, i);
|
||||
|
||||
gcal_weather_info_get_date (info, &weather_date);
|
||||
day_difference = g_date_days_between (&first, &weather_date);
|
||||
|
||||
if (day_difference >= 0 && day_difference < 6 * 7)
|
||||
if (day_difference == cell)
|
||||
return info;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
update_weather (GcalMonthView *self,
|
||||
gboolean clear_old)
|
||||
{
|
||||
guint row;
|
||||
guint col;
|
||||
|
||||
g_return_if_fail (GCAL_IS_MONTH_VIEW (self));
|
||||
|
||||
for (row = 0; row < 6; row++)
|
||||
{
|
||||
for (col = 0; col < 7; col++)
|
||||
{
|
||||
GcalMonthCell *wcell;
|
||||
guint row;
|
||||
guint column;
|
||||
|
||||
row = day_difference / 7;
|
||||
column = day_difference % 7;
|
||||
wcell = GCAL_MONTH_CELL (self->month_cell[row][column]);
|
||||
|
||||
gcal_month_cell_set_weather (wcell, info);
|
||||
GcalMonthCell *cell = GCAL_MONTH_CELL (self->month_cell[row][col]);
|
||||
gcal_month_cell_set_weather (cell, get_weather_info_for_cell (self, row * 7 + col));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1052,7 +1042,7 @@ update_month_cells (GcalMonthView *self)
|
||||
}
|
||||
}
|
||||
|
||||
update_weather (self, TRUE);
|
||||
update_weather (self, FALSE);
|
||||
|
||||
self->update_grid_id = 0;
|
||||
|
||||
|
||||
@@ -1705,28 +1705,29 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
||||
*/
|
||||
static gint
|
||||
gcal_week_header_add_weather_infos (GcalWeekHeader *self,
|
||||
GSList *winfos)
|
||||
GPtrArray *weather_infos)
|
||||
{
|
||||
g_autoptr (GDateTime) _week_start = NULL;
|
||||
GSList *iter; /* unowned */
|
||||
g_autoptr (GDateTime) week_start_dt = NULL;
|
||||
GDate week_start;
|
||||
int consumed = 0;
|
||||
gint consumed = 0;
|
||||
guint i;
|
||||
|
||||
g_return_val_if_fail (self != NULL, 0);
|
||||
|
||||
_week_start = get_start_of_week (self->active_date);
|
||||
week_start_dt = get_start_of_week (self->active_date);
|
||||
g_date_set_dmy (&week_start,
|
||||
g_date_time_get_day_of_month (_week_start),
|
||||
g_date_time_get_month (_week_start),
|
||||
g_date_time_get_year (_week_start));
|
||||
g_date_time_get_day_of_month (week_start_dt),
|
||||
g_date_time_get_month (week_start_dt),
|
||||
g_date_time_get_year (week_start_dt));
|
||||
|
||||
for (iter = winfos; iter != NULL; iter = iter->next)
|
||||
for (i = 0; weather_infos && i < weather_infos->len; i++)
|
||||
{
|
||||
GcalWeatherInfo *gwi; /* unowned */
|
||||
GDate gwi_date;
|
||||
gint day_diff;
|
||||
|
||||
gwi = GCAL_WEATHER_INFO (iter->data);
|
||||
gwi = g_ptr_array_index (weather_infos, i);
|
||||
|
||||
gcal_weather_info_get_date (gwi, &gwi_date);
|
||||
|
||||
day_diff = g_date_days_between (&week_start, &gwi_date);
|
||||
@@ -1796,18 +1797,17 @@ gcal_week_header_set_weather_service (GcalWeekHeader *self,
|
||||
void
|
||||
gcal_week_header_update_weather_infos (GcalWeekHeader *self)
|
||||
{
|
||||
GPtrArray* weather_infos;
|
||||
|
||||
g_return_if_fail (GCAL_IS_WEEK_HEADER (self));
|
||||
|
||||
gcal_week_header_clear_weather_infos (self);
|
||||
|
||||
if (self->weather_service != NULL)
|
||||
{
|
||||
GSList* weather_infos = NULL; /* unowned */
|
||||
if (!self->weather_service)
|
||||
return;
|
||||
|
||||
weather_infos = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
gcal_week_header_add_weather_infos (self, weather_infos);
|
||||
}
|
||||
weather_infos = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
gcal_week_header_add_weather_infos (self, weather_infos);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -517,9 +517,7 @@ gcal_week_view_finalize (GObject *object)
|
||||
g_clear_pointer (&self->date, g_free);
|
||||
|
||||
g_clear_object (&self->manager);
|
||||
|
||||
if (self->weather_service != NULL)
|
||||
g_clear_object (&self->weather_service);
|
||||
g_clear_object (&self->weather_service);
|
||||
|
||||
/* Chain up to parent's finalize() method. */
|
||||
G_OBJECT_CLASS (gcal_week_view_parent_class)->finalize (object);
|
||||
|
||||
@@ -2060,22 +2060,25 @@ update_weather (GcalYearView *self)
|
||||
|
||||
g_return_if_fail (GCAL_IS_YEAR_VIEW (self));
|
||||
|
||||
|
||||
if (self->weather_service != NULL && self->date != NULL)
|
||||
if (self->weather_service && self->date)
|
||||
{
|
||||
GSList *witer; /* unowned */
|
||||
GDate date;
|
||||
GPtrArray *weather_infos;
|
||||
GDate date;
|
||||
guint i;
|
||||
|
||||
g_date_set_dmy (&date, self->date->day, self->date->month, self->date->year);
|
||||
|
||||
witer = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
for (; witer != NULL; witer = witer->next)
|
||||
weather_infos = gcal_weather_service_get_weather_infos (self->weather_service);
|
||||
|
||||
for (i = 0; weather_infos && i < weather_infos->len; i++)
|
||||
{
|
||||
GcalWeatherInfo *info; /* unowned */
|
||||
GcalWeatherInfo *info;
|
||||
GDate wdate;
|
||||
|
||||
info = GCAL_WEATHER_INFO (witer->data);
|
||||
info = g_ptr_array_index (weather_infos, i);
|
||||
|
||||
gcal_weather_info_get_date (info, &wdate);
|
||||
|
||||
if (g_date_compare (&date, &wdate) == 0)
|
||||
{
|
||||
const gchar *temp_str; /* unowned */
|
||||
|
||||
Reference in New Issue
Block a user