project: Move a few classes to core/

This commit is contained in:
Georges Basile Stavracas Neto
2019-05-04 12:05:39 -03:00
parent 7297d369fb
commit 1a5ff545a2
15 changed files with 18 additions and 15 deletions
+605
View File
@@ -0,0 +1,605 @@
/* gcal-calendar.c
*
* Copyright 2019 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define G_LOG_DOMAIN "GcalCalendar"
#include "gcal-debug.h"
#include "gcal-calendar.h"
struct _GcalCalendar
{
GObject parent;
GdkRGBA color;
ESource *source;
ECalClient *client;
gboolean read_only;
guint color_changed_handler_id;
guint name_changed_handler_id;
guint readonly_changed_handler_id;
guint visible_changed_handler_id;
gboolean initialized;
};
static void g_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GcalCalendar, gcal_calendar, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, g_initable_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, NULL))
G_DEFINE_QUARK (GcalCalendar, gcal_calendar_error);
enum
{
PROP_0,
PROP_CLIENT,
PROP_COLOR,
PROP_ID,
PROP_NAME,
PROP_READ_ONLY,
PROP_SOURCE,
PROP_VISIBLE,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/*
* Auxiliary methods
*/
static void
update_color (GcalCalendar *self)
{
ESourceSelectable *selectable_extension;
const gchar *color;
selectable_extension = e_source_get_extension (self->source, E_SOURCE_EXTENSION_CALENDAR);
color = e_source_selectable_get_color (selectable_extension);
if (!gdk_rgba_parse (&self->color, color))
gdk_rgba_parse (&self->color, "#ffffff");
}
/*
* Callbacks
*/
static void
on_source_color_changed_cb (ESourceSelectable *source,
GParamSpec *pspec,
GcalCalendar *self)
{
update_color (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
}
static void
on_source_name_changed_cb (ESource *source,
GParamSpec *pspec,
GcalCalendar *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
}
static void
on_client_readonly_changed_cb (EClient *client,
GParamSpec *pspec,
GcalCalendar *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_READ_ONLY]);
}
static void
on_source_visible_changed_cb (ESourceSelectable *source,
GParamSpec *pspec,
GcalCalendar *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VISIBLE]);
}
/*
* GInitable iface
*/
static gboolean
gcal_calendar_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
g_autoptr (EClient) client = NULL;
g_autoptr (GError) local_error = NULL;
ESourceSelectable *selectable_extension;
GcalCalendar *self;
GCAL_ENTRY;
self = GCAL_CALENDAR (initable);
g_assert (!self->initialized);
self->initialized = TRUE;
if (!e_source_has_extension (self->source, E_SOURCE_EXTENSION_CALENDAR))
{
g_set_error (error,
GCAL_CALENDAR_ERROR,
GCAL_CALENDAR_ERROR_NOT_CALENDAR,
"ESource is not a calendar");
GCAL_RETURN (FALSE);
}
selectable_extension = e_source_get_extension (self->source, E_SOURCE_EXTENSION_CALENDAR);
update_color (self);
self->color_changed_handler_id = g_signal_connect (selectable_extension,
"notify::color",
G_CALLBACK (on_source_color_changed_cb),
self);
self->visible_changed_handler_id = g_signal_connect (selectable_extension,
"notify::selected",
G_CALLBACK (on_source_visible_changed_cb),
self);
self->name_changed_handler_id = g_signal_connect (self->source,
"notify::display-name",
G_CALLBACK (on_source_name_changed_cb),
self);
/* 15s is arbitrary, but very rarely this will be reached */
client = e_cal_client_connect_sync (self->source,
E_CAL_CLIENT_SOURCE_TYPE_EVENTS,
15,
cancellable,
&local_error);
if (local_error)
{
g_propagate_error (error, local_error);
GCAL_RETURN (FALSE);
}
g_assert (E_IS_CAL_CLIENT (client));
self->client = E_CAL_CLIENT (client);
self->readonly_changed_handler_id = g_signal_connect (client,
"notify::readonly",
G_CALLBACK (on_client_readonly_changed_cb),
self);
GCAL_RETURN (TRUE);
}
static void
g_initable_iface_init (GInitableIface *iface)
{
iface->init = gcal_calendar_initable_init;
}
/*
* GObject overrides
*/
static void
gcal_calendar_finalize (GObject *object)
{
GcalCalendar *self = (GcalCalendar *)object;
g_signal_handler_disconnect (self->source, self->color_changed_handler_id);
self->color_changed_handler_id = 0;
g_signal_handler_disconnect (self->source, self->name_changed_handler_id);
self->name_changed_handler_id = 0;
g_signal_handler_disconnect (self->client, self->readonly_changed_handler_id);
self->readonly_changed_handler_id = 0;
g_clear_object (&self->client);
g_clear_object (&self->source);
G_OBJECT_CLASS (gcal_calendar_parent_class)->finalize (object);
}
static void
gcal_calendar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalCalendar *self = GCAL_CALENDAR (object);
switch (prop_id)
{
case PROP_CLIENT:
g_value_set_object (value, self->client);
break;
case PROP_COLOR:
g_value_set_boxed (value, &self->color);
break;
case PROP_ID:
g_value_set_string (value, gcal_calendar_get_id (self));
break;
case PROP_NAME:
g_value_set_string (value, e_source_get_display_name (self->source));
break;
case PROP_READ_ONLY:
g_value_set_boolean (value, gcal_calendar_is_read_only (self));
break;
case PROP_SOURCE:
g_value_set_object (value, self->source);
break;
case PROP_VISIBLE:
g_value_set_boolean (value, gcal_calendar_get_visible (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_calendar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GcalCalendar *self = GCAL_CALENDAR (object);
switch (prop_id)
{
case PROP_CLIENT:
g_assert_not_reached ();
break;
case PROP_COLOR:
gcal_calendar_set_color (self, g_value_get_boxed (value));
break;
case PROP_ID:
g_assert_not_reached ();
break;
case PROP_NAME:
gcal_calendar_set_name (self, g_value_get_string (value));
break;
case PROP_READ_ONLY:
g_assert_not_reached ();
break;
case PROP_SOURCE:
g_assert (self->source == NULL);
self->source = g_value_dup_object (value);
break;
case PROP_VISIBLE:
gcal_calendar_set_visible (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_calendar_class_init (GcalCalendarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_calendar_finalize;
object_class->get_property = gcal_calendar_get_property;
object_class->set_property = gcal_calendar_set_property;
properties[PROP_CLIENT] = g_param_spec_object ("client",
"Calendar client",
"Calendar client",
E_TYPE_CAL_CLIENT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
properties[PROP_COLOR] = g_param_spec_boxed ("color",
"Color",
"Color of the calendar",
GDK_TYPE_RGBA,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_ID] = g_param_spec_string ("id",
"Identifier",
"Unique identifier of the calendar",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_NAME] = g_param_spec_string ("name",
"Name",
"Name of the calendar",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_READ_ONLY] = g_param_spec_boolean ("read-only",
"Read-only",
"Whether the calendar is read-only or not",
FALSE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
properties[PROP_SOURCE] = g_param_spec_object ("source",
"Source",
"Source",
E_TYPE_SOURCE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
properties[PROP_VISIBLE] = g_param_spec_boolean ("visible",
"Visible",
"Whether the calendar is visible",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_calendar_init (GcalCalendar *self)
{
}
/**
* gcal_calendar_new:
* @source: an #ESource
* @cancellable: (nullable): a cancellable
* @callback:
* @user_data:
*
* Asynchronously creates and initializes a #GcalCalendar
* from @source.
*/
void
gcal_calendar_new (ESource *source,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
return g_async_initable_new_async (GCAL_TYPE_CALENDAR,
G_PRIORITY_DEFAULT,
cancellable,
callback,
user_data,
"source", source,
NULL);
}
/**
* @result:
* @error: (nullable): return location for a #GError
*
* Finishes the operation started by gcal_calendar_new().
*
* Returns: (transfer full)(nullable): a #GcalCalendar
*/
GcalCalendar*
gcal_calendar_new_finish (GAsyncResult *result,
GError **error)
{
g_autoptr (GObject) source_object = NULL;
g_autoptr (GObject) result_object = NULL;
g_return_val_if_fail (G_IS_TASK (result), NULL);
g_return_val_if_fail (!error || !*error, NULL);
source_object = g_async_result_get_source_object (result);
g_assert (source_object != NULL);
result_object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
result,
error);
return (GcalCalendar*) g_steal_pointer (&result_object);
}
/**
* gcal_calendar_get_color:
* @self: a #GcalCalendar
*
* Retrieves the color of @self.
*
* Returns: (transfer none): a #GdkRGBA
*/
const GdkRGBA*
gcal_calendar_get_color (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), NULL);
return &self->color;
}
/**
* gcal_calendar_set_color:
* @self: a #GcalCalendar
* @color: the new color
*
* Sets the color of @self.
*/
void
gcal_calendar_set_color (GcalCalendar *self,
const GdkRGBA *color)
{
g_autofree gchar *color_string = NULL;
ESourceSelectable *selectable_extension;
g_return_if_fail (GCAL_IS_CALENDAR (self));
g_return_if_fail (color != NULL);
if (gdk_rgba_equal (&self->color, color))
return;
self->color = *color;
color_string = gdk_rgba_to_string (color);
selectable_extension = e_source_get_extension (self->source, E_SOURCE_EXTENSION_CALENDAR);
e_source_selectable_set_color (selectable_extension, color_string);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COLOR]);
}
/**
* gcal_calendar_get_id:
* @self: a #GcalCalendar
*
* Retrieves the unique identifier of @self.
*
* Returns: (transfer none): a string
*/
const gchar*
gcal_calendar_get_id (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), NULL);
return e_source_get_uid (self->source);
}
/**
* gcal_calendar_get_name:
* @self: a #GcalCalendar
*
* Retrieves the user-visible name of @self.
*
* Returns: (transfer none)(nullable): a string
*/
const gchar*
gcal_calendar_get_name (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), NULL);
return e_source_get_display_name (self->source);
}
/**
* gcal_calendar_set_name:
* @self: a #GcalCalendar
* @name: new user-visible name
*
* Sets the user-visible name of @self.
*/
void
gcal_calendar_set_name (GcalCalendar *self,
const gchar *name)
{
g_return_if_fail (GCAL_IS_CALENDAR (self));
e_source_set_display_name (self->source, name);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
}
/**
* gcal_calendar_is_read_only:
* @self: a #GcalCalendar
*
* Retrieves whether @self is read-only or not.
*
* Returns: %TRUE if @self is read-only, %FALSE otherwise
*/
gboolean
gcal_calendar_is_read_only (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), FALSE);
return e_client_is_readonly (E_CLIENT (self->client));
}
/**
* gcal_calendar_get_source:
* @self: a #GcalCalendar
*
* Retrieves the #ESource that is backing @self.
*
* Returns: (transfer none): an #ESource
*/
ESource*
gcal_calendar_get_source (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), NULL);
return self->source;
}
/**
* gcal_calendar_get_client:
* @self: a #GcalCalendar
*
* Retrieves the #ECalClient that is backing @self.
*
* Returns: (transfer none): an #ECalClient
*/
ECalClient*
gcal_calendar_get_client (GcalCalendar *self)
{
g_return_val_if_fail (GCAL_IS_CALENDAR (self), NULL);
return self->client;
}
/**
* gcal_calendar_get_visible:
* @self: a #GcalCalendar
*
* Retrieves whether @self is visible to the user or not.
*
* Returns: %TRUE if @self is visible, %FALSE otherwise
*/
gboolean
gcal_calendar_get_visible (GcalCalendar *self)
{
ESourceSelectable *selectable_extension;
g_return_val_if_fail (GCAL_IS_CALENDAR (self), FALSE);
selectable_extension = e_source_get_extension (self->source, E_SOURCE_EXTENSION_CALENDAR);
return e_source_selectable_get_selected (selectable_extension);
}
/**
* gcal_calendar_set_visible:
* @self: a #GcalCalendar
* @visible: visibility of the calendar
*
* Sets whether @self is visible or not.
*/
void
gcal_calendar_set_visible (GcalCalendar *self,
gboolean visible)
{
ESourceSelectable *selectable_extension;
g_return_if_fail (GCAL_IS_CALENDAR (self));
selectable_extension = e_source_get_extension (self->source, E_SOURCE_EXTENSION_CALENDAR);
e_source_selectable_set_selected (selectable_extension, visible);
}
+78
View File
@@ -0,0 +1,78 @@
/* gcal-calendar.h
*
* Copyright 2019 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <libecal/libecal.h>
#include <gdk/gdk.h>
G_BEGIN_DECLS
/**
* GcalCalendarError:
* @GCAL_CALENDAR_ERROR_NOT_CALENDAR: the #ESource is not a calendar
*
* Errors that #GcalCalendar can generate.
*/
typedef enum
{
GCAL_CALENDAR_ERROR_NOT_CALENDAR,
} GcalCalendarError;
#define GCAL_CALENDAR_ERROR (gcal_calendar_error_quark ())
#define GCAL_TYPE_CALENDAR (gcal_calendar_get_type())
G_DECLARE_FINAL_TYPE (GcalCalendar, gcal_calendar, GCAL, CALENDAR, GObject)
GQuark gcal_calendar_error_quark (void);
void gcal_calendar_new (ESource *source,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GcalCalendar* gcal_calendar_new_finish (GAsyncResult *result,
GError **error);
ECalClient* gcal_calendar_get_client (GcalCalendar *self);
const GdkRGBA* gcal_calendar_get_color (GcalCalendar *self);
void gcal_calendar_set_color (GcalCalendar *self,
const GdkRGBA *color);
const gchar* gcal_calendar_get_id (GcalCalendar *self);
const gchar* gcal_calendar_get_name (GcalCalendar *self);
void gcal_calendar_set_name (GcalCalendar *self,
const gchar *name);
gboolean gcal_calendar_is_read_only (GcalCalendar *self);
ESource* gcal_calendar_get_source (GcalCalendar *self);
gboolean gcal_calendar_get_visible (GcalCalendar *self);
void gcal_calendar_set_visible (GcalCalendar *self,
gboolean visible);
G_END_DECLS
+284
View File
@@ -0,0 +1,284 @@
/* gcal-clock.c
*
* Copyright (C) 2017 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/>.
*/
#define G_LOG_DOMAIN "GcalClock"
#include "gcal-clock.h"
#include "gcal-debug.h"
#include "gcal-utils.h"
#include <gio/gio.h>
struct _GcalClock
{
GObject parent;
guint timeout_id;
GDateTime *current;
GDBusProxy *proxy;
GCancellable *cancellable;
};
static gboolean timeout_cb (gpointer user_data);
G_DEFINE_TYPE (GcalClock, gcal_clock, G_TYPE_OBJECT)
enum
{
MINUTE_CHANGED,
HOUR_CHANGED,
DAY_CHANGED,
NUM_SIGNALS
};
static guint signals[NUM_SIGNALS] = { 0, };
/*
* Auxiliary methods
*/
static void
update_current_date (GcalClock *self)
{
g_autoptr (GDateTime) now;
gboolean minute_changed;
gboolean hour_changed;
gboolean day_changed;
GCAL_ENTRY;
now = g_date_time_new_now_local ();
day_changed = g_date_time_get_year (now) != g_date_time_get_year (self->current) ||
g_date_time_get_day_of_year (now) != g_date_time_get_day_of_year (self->current);
hour_changed = day_changed || g_date_time_get_hour (now) != g_date_time_get_hour (self->current);
minute_changed = hour_changed || g_date_time_get_minute (now) != g_date_time_get_minute (self->current);
if (day_changed)
g_signal_emit (self, signals[DAY_CHANGED], 0);
if (hour_changed)
g_signal_emit (self, signals[HOUR_CHANGED], 0);
if (minute_changed)
g_signal_emit (self, signals[MINUTE_CHANGED], 0);
g_debug ("Updating clock time");
gcal_clear_date_time (&self->current);
self->current = g_date_time_ref (now);
GCAL_EXIT;
}
static void
schedule_update_timeout (GcalClock *self)
{
g_autoptr (GDateTime) now;
guint seconds_between;
/* Remove the previous timeout if we came from resume */
if (self->timeout_id > 0)
{
g_source_remove (self->timeout_id);
self->timeout_id = 0;
}
now = g_date_time_new_now_local ();
seconds_between = 60 - g_date_time_get_second (now);
self->timeout_id = g_timeout_add_seconds (seconds_between, timeout_cb, self);
g_debug ("Scheduling update for %d seconds", seconds_between);
}
/*
* Callbacks
*/
static gboolean
timeout_cb (gpointer user_data)
{
GcalClock *self = user_data;
self->timeout_id = 0;
update_current_date (self);
schedule_update_timeout (self);
return G_SOURCE_REMOVE;
}
static void
logind_signal_received_cb (GDBusProxy *logind,
const gchar *sender,
const gchar *signal,
GVariant *params,
GcalClock *self)
{
GVariant *child;
gboolean resuming;
if (!g_str_equal (signal, "PrepareForSleep"))
return;
child = g_variant_get_child_value (params, 0);
resuming = !g_variant_get_boolean (child);
/* Only emit :update when resuming */
if (resuming)
{
update_current_date (self);
schedule_update_timeout (self);
}
g_clear_pointer (&child, g_variant_unref);
}
static void
login_proxy_acquired_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GcalClock *self;
GError *error;
self = GCAL_CLOCK (user_data);
error = NULL;
self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
if (error)
{
g_warning ("Error acquiring logind DBus proxy: %s", error->message);
g_clear_error (&error);
return;
}
g_signal_connect (self->proxy,
"g-signal",
G_CALLBACK (logind_signal_received_cb),
self);
g_debug ("Successfully acquired logind DBus proxy");
}
static void
gcal_clock_finalize (GObject *object)
{
GcalClock *self = (GcalClock *)object;
g_cancellable_cancel (self->cancellable);
if (self->timeout_id > 0)
{
g_source_remove (self->timeout_id);
self->timeout_id = 0;
}
gcal_clear_date_time (&self->current);
g_clear_object (&self->cancellable);
g_clear_object (&self->proxy);
G_OBJECT_CLASS (gcal_clock_parent_class)->finalize (object);
}
static void
gcal_clock_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
gcal_clock_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
gcal_clock_class_init (GcalClockClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_clock_finalize;
object_class->get_property = gcal_clock_get_property;
object_class->set_property = gcal_clock_set_property;
signals[DAY_CHANGED] = g_signal_new ("day-changed",
GCAL_TYPE_CLOCK,
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
0);
signals[HOUR_CHANGED] = g_signal_new ("hour-changed",
GCAL_TYPE_CLOCK,
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
0);
signals[MINUTE_CHANGED] = g_signal_new ("minute-changed",
GCAL_TYPE_CLOCK,
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
0);
}
static void
gcal_clock_init (GcalClock *self)
{
self->current = g_date_time_new_now_local ();
self->cancellable = g_cancellable_new ();
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
self->cancellable,
login_proxy_acquired_cb,
self);
schedule_update_timeout (self);
}
/**
* gcal_clock_new:
*
* Creates a new #GcalClock.
*
* Returns: (transfer full): a newly-allocated #GcalClock.
*/
GcalClock*
gcal_clock_new (void)
{
return g_object_new (GCAL_TYPE_CLOCK, NULL);
}
+35
View File
@@ -0,0 +1,35 @@
/* gcal-clock.h
*
* Copyright (C) 2017 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/>.
*/
#ifndef GCAL_CLOCK_H
#define GCAL_CLOCK_H
#include <glib-object.h>
G_BEGIN_DECLS
#define GCAL_TYPE_CLOCK (gcal_clock_get_type())
G_DECLARE_FINAL_TYPE (GcalClock, gcal_clock, GCAL, CLOCK, GObject)
GcalClock* gcal_clock_new (void);
G_END_DECLS
#endif /* GCAL_CLOCK_H */
File diff suppressed because it is too large Load Diff
+138
View File
@@ -0,0 +1,138 @@
/* gcal-event.h
*
* Copyright (C) 2016 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/>.
*/
#ifndef GCAL_EVENT_H
#define GCAL_EVENT_H
#include "gcal-calendar.h"
#include "gcal-recurrence.h"
#include <glib-object.h>
#include <gtk/gtk.h>
#include <libecal/libecal.h>
#include <libedataserver/libedataserver.h>
G_BEGIN_DECLS
/**
* GcalEventError:
* @GCAL_EVENT_ERROR_INVALID_START_DATE: indicated an invalid start date
*
* Errors that #GcalEvent can generate.
*/
typedef enum
{
GCAL_EVENT_ERROR_INVALID_START_DATE
} GcalEventError;
#define GCAL_EVENT_ERROR gcal_event_error_quark ()
#define GCAL_TYPE_EVENT (gcal_event_get_type())
G_DECLARE_FINAL_TYPE (GcalEvent, gcal_event, GCAL, EVENT, GObject)
GQuark gcal_event_error_quark (void);
GcalEvent* gcal_event_new (GcalCalendar *calendar,
ECalComponent *component,
GError **error);
gboolean gcal_event_get_all_day (GcalEvent *self);
void gcal_event_set_all_day (GcalEvent *self,
gboolean all_day);
GdkRGBA* gcal_event_get_color (GcalEvent *self);
void gcal_event_set_color (GcalEvent *self,
GdkRGBA *color);
ECalComponent* gcal_event_get_component (GcalEvent *self);
GDateTime* gcal_event_get_date_end (GcalEvent *self);
void gcal_event_set_date_end (GcalEvent *self,
GDateTime *dt);
GDateTime* gcal_event_get_date_start (GcalEvent *self);
void gcal_event_set_date_start (GcalEvent *self,
GDateTime *dt);
const gchar* gcal_event_get_description (GcalEvent *self);
void gcal_event_set_description (GcalEvent *self,
const gchar *description);
gboolean gcal_event_has_recurrence (GcalEvent *self);
gboolean gcal_event_has_alarms (GcalEvent *self);
GList* gcal_event_get_alarms (GcalEvent *self);
void gcal_event_add_alarm (GcalEvent *self,
guint type,
gboolean has_sound);
void gcal_event_remove_alarm (GcalEvent *self,
guint type);
const gchar* gcal_event_get_location (GcalEvent *self);
void gcal_event_set_location (GcalEvent *self,
const gchar *location);
GcalCalendar* gcal_event_get_calendar (GcalEvent *self);
void gcal_event_set_calendar (GcalEvent *self,
GcalCalendar *calendar);
const gchar* gcal_event_get_summary (GcalEvent *self);
void gcal_event_set_summary (GcalEvent *self,
const gchar *summary);
const gchar* gcal_event_get_uid (GcalEvent *self);
/* Utilities */
gboolean gcal_event_is_multiday (GcalEvent *self);
gint gcal_event_compare (GcalEvent *event1,
GcalEvent *event2);
gint gcal_event_compare_with_current (GcalEvent *event1,
GcalEvent *event2,
time_t current_time);
void gcal_event_set_recurrence (GcalEvent *event,
GcalRecurrence *recur);
GcalRecurrence* gcal_event_get_recurrence (GcalEvent *self);
void gcal_event_save_original_timezones (GcalEvent *self);
void gcal_event_get_original_timezones (GcalEvent *self,
GTimeZone **start_tz,
GTimeZone **end_tz);
gchar* gcal_event_format_date (GcalEvent *self);
G_END_DECLS
#endif /* GCAL_EVENT_H */
File diff suppressed because it is too large Load Diff
+128
View File
@@ -0,0 +1,128 @@
/* gcal-manager.h
*
* Copyright (C) 2015 Erick Pérez Castellanos <erickpc@gnome.org>
*
* gnome-calendar 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.
*
* gnome-calendar 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/>.
*/
#ifndef __GCAL_MANAGER_H__
#define __GCAL_MANAGER_H__
#include "e-cal-data-model.h"
#include "gcal-calendar.h"
#include "gcal-event.h"
#include <libical/icaltime.h>
G_BEGIN_DECLS
#define GCAL_TYPE_MANAGER (gcal_manager_get_type ())
G_DECLARE_FINAL_TYPE (GcalManager, gcal_manager, GCAL, MANAGER, GObject)
GcalManager* gcal_manager_new (void);
ESource* gcal_manager_get_source (GcalManager *self,
const gchar *uid);
GcalCalendar* gcal_manager_get_calendar_from_source (GcalManager *self,
ESource *source);
GList* gcal_manager_get_calendars (GcalManager *self);
ESource* gcal_manager_get_default_source (GcalManager *self);
void gcal_manager_set_default_source (GcalManager *self,
ESource *source);
void gcal_manager_set_subscriber (GcalManager *self,
ECalDataModelSubscriber *subscriber,
time_t range_start,
time_t range_end);
void gcal_manager_set_search_subscriber (GcalManager *self,
ECalDataModelSubscriber *subscriber,
time_t range_start,
time_t range_end);
void gcal_manager_set_query (GcalManager *self,
const gchar *query);
gchar* gcal_manager_query_client_data (GcalManager *self,
ESource *source,
const gchar *field);
void gcal_manager_refresh (GcalManager *self);
gboolean gcal_manager_is_client_writable (GcalManager *self,
ESource *source);
void gcal_manager_create_event (GcalManager *self,
GcalEvent *event);
void gcal_manager_update_event (GcalManager *self,
GcalEvent *event,
GcalRecurrenceModType mod);
void gcal_manager_remove_event (GcalManager *self,
GcalEvent *event,
GcalRecurrenceModType mod);
void gcal_manager_move_event_to_source (GcalManager *self,
GcalEvent *event,
ESource *dest);
gchar* gcal_manager_add_source (GcalManager *self,
const gchar *name,
const gchar *backend,
const gchar *color);
void gcal_manager_enable_source (GcalManager *self,
ESource *source);
void gcal_manager_disable_source (GcalManager *self,
ESource *source);
void gcal_manager_save_source (GcalManager *self,
ESource *source);
GList* gcal_manager_get_events (GcalManager *self,
icaltimetype *range_start,
icaltimetype *range_end);
gboolean gcal_manager_get_loading (GcalManager *self);
/* GNOME Shell-related functions */
GcalEvent* gcal_manager_get_event_from_shell_search (GcalManager *self,
const gchar *uuid);
void gcal_manager_setup_shell_search (GcalManager *self,
ECalDataModelSubscriber *subscriber);
void gcal_manager_set_shell_search_query (GcalManager *self,
const gchar *query);
void gcal_manager_set_shell_search_subscriber (GcalManager *self,
ECalDataModelSubscriber *subscriber,
time_t range_start,
time_t range_end);
gboolean gcal_manager_shell_search_done (GcalManager *self);
GList* gcal_manager_get_shell_search_events (GcalManager *self);
void gcal_manager_startup (GcalManager *self);
G_END_DECLS
#endif /* __GCAL_MANAGER_H__ */
+316
View File
@@ -0,0 +1,316 @@
/* gcal-recurrence.c
*
* Copyright (C) 2017 Yash Singh <yashdev10p@gmail.com>
*
* gnome-calendar 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.
*
* gnome-calendar 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-recurrence.h"
#include "gcal-utils.h"
#include "gcal-event.h"
#include <glib.h>
G_DEFINE_BOXED_TYPE (GcalRecurrence, gcal_recurrence, gcal_recurrence_ref, gcal_recurrence_unref)
static void
gcal_recurrence_free (GcalRecurrence *self)
{
g_assert (self);
g_assert_cmpint (self->ref_count, ==, 0);
gcal_clear_date_time (&self->limit.until);
g_slice_free (GcalRecurrence, self);
}
/**
* gcal_recurrence_new:
*
* Creates a new #GcalRecurrence
*
* Returns: (transfer full): a #GcalRecurrence
*/
GcalRecurrence*
gcal_recurrence_new (void)
{
GcalRecurrence *new_recur;
new_recur = g_slice_new (GcalRecurrence);
new_recur->frequency = GCAL_RECURRENCE_NO_REPEAT;
new_recur->limit_type = GCAL_RECURRENCE_FOREVER;
new_recur->limit.until = NULL;
new_recur->limit.count = 0;
new_recur->ref_count = 1;
return new_recur;
}
/**
* gcal_recurrence_copy:
* @recur: a #GcalRecurrence
*
* Creates a copy of @recur
*
* Returns: (transfer full): a #GcalRecurrence
*/
GcalRecurrence*
gcal_recurrence_copy (GcalRecurrence *recur)
{
GcalRecurrence *new_recur;
g_return_val_if_fail (recur != NULL, NULL);
new_recur = gcal_recurrence_new ();
new_recur->frequency = recur->frequency;
new_recur->limit_type = recur->limit_type;
if (recur->limit_type == GCAL_RECURRENCE_UNTIL && recur->limit.until)
new_recur->limit.until = g_date_time_ref (recur->limit.until);
new_recur->limit.count = recur->limit.count;
return new_recur;
}
GcalRecurrence *
gcal_recurrence_ref (GcalRecurrence *self)
{
g_return_val_if_fail (self, NULL);
g_return_val_if_fail (self->ref_count, NULL);
g_atomic_int_inc (&self->ref_count);
return self;
}
void
gcal_recurrence_unref (GcalRecurrence *self)
{
g_return_if_fail (self);
g_return_if_fail (self->ref_count);
if (g_atomic_int_dec_and_test (&self->ref_count))
gcal_recurrence_free (self);
}
/**
* gcal_recurrence_is_equal:
* @recur1: (nullable): a #GcalRecurrence
* @recur2: (nullable): a #GcalRecurrence
*
* Checks if @recur1 and @recur2 are equal or not
*
* Returns: %TRUE if both are equal, %FALSE otherwise
*/
gboolean
gcal_recurrence_is_equal (GcalRecurrence *recur1,
GcalRecurrence *recur2)
{
if (recur1 == recur2)
return TRUE;
else if (!recur1 || !recur2)
return FALSE;
if (recur1->frequency != recur2->frequency)
return FALSE;
if (recur1->limit_type != recur2->limit_type)
return FALSE;
if (recur1->limit_type == GCAL_RECURRENCE_UNTIL)
{
if (!g_date_time_equal (recur1->limit.until, recur2->limit.until))
return FALSE;
}
else if (recur1->limit_type == GCAL_RECURRENCE_COUNT)
{
if (recur1->limit.count != recur2->limit.count)
return FALSE;
}
return TRUE;
}
/**
* gcal_recurrence_parse_recurrence_rules:
* @comp: an #ECalComponent
*
* Parses the rrule of the @comp and
* sets the GcalRecurrence struct accordingly.
*
* Returns: (transfer full): a #GcalRecurrence
*/
GcalRecurrence*
gcal_recurrence_parse_recurrence_rules (ECalComponent *comp)
{
GcalRecurrence *recur;
icalproperty *prop;
icalcomponent *icalcomp;
struct icalrecurrencetype rrule;
if (!e_cal_component_has_recurrences (comp))
return NULL;
recur = gcal_recurrence_new ();
icalcomp = e_cal_component_get_icalcomponent (comp);
prop = icalcomponent_get_first_property (icalcomp, ICAL_RRULE_PROPERTY);
g_return_val_if_fail (prop != NULL, NULL);
rrule = icalproperty_get_rrule (prop);
switch (rrule.freq)
{
case ICAL_DAILY_RECURRENCE:
recur->frequency = GCAL_RECURRENCE_DAILY;
break;
case ICAL_WEEKLY_RECURRENCE:
{
if (rrule.by_day[0] == ICAL_MONDAY_WEEKDAY &&
rrule.by_day[1] == ICAL_TUESDAY_WEEKDAY &&
rrule.by_day[2] == ICAL_WEDNESDAY_WEEKDAY &&
rrule.by_day[3] == ICAL_THURSDAY_WEEKDAY &&
rrule.by_day[4] == ICAL_FRIDAY_WEEKDAY &&
rrule.by_day[5] != ICAL_SATURDAY_WEEKDAY &&
rrule.by_day[6] != ICAL_SUNDAY_WEEKDAY)
{
recur->frequency = GCAL_RECURRENCE_MON_FRI;
}
else
{
recur->frequency = GCAL_RECURRENCE_WEEKLY;
}
break;
}
case ICAL_MONTHLY_RECURRENCE:
recur->frequency = GCAL_RECURRENCE_MONTHLY;
break;
case ICAL_YEARLY_RECURRENCE:
recur->frequency = GCAL_RECURRENCE_YEARLY;
break;
default:
recur->frequency = GCAL_RECURRENCE_OTHER;
}
if (rrule.count > 0)
{
recur->limit_type = GCAL_RECURRENCE_COUNT;
recur->limit.count = rrule.count;
}
else if (rrule.until.year != 0)
{
recur->limit_type = GCAL_RECURRENCE_UNTIL;
recur->limit.until = gcal_date_time_from_icaltime (&rrule.until);
}
else
{
recur->limit_type = GCAL_RECURRENCE_FOREVER;
}
return recur;
}
/**
* gcal_recurrence_to_rrule:
* @recur: a #GcalRecurrence
*
* Converts @recur into corresponding rrule.
*
* Returns: (transfer full): a #struct icalrecurrencetype
*/
struct icalrecurrencetype*
gcal_recurrence_to_rrule (GcalRecurrence *recur)
{
struct icalrecurrencetype *rrule;
if (!recur)
return NULL;
/* Initialize and clear the rrule to get rid of unwanted fields */
rrule = g_new0 (struct icalrecurrencetype, 1);
icalrecurrencetype_clear (rrule);
switch (recur->frequency)
{
case GCAL_RECURRENCE_DAILY:
rrule->freq = ICAL_DAILY_RECURRENCE;
break;
case GCAL_RECURRENCE_WEEKLY:
rrule->freq = ICAL_WEEKLY_RECURRENCE;
break;
case GCAL_RECURRENCE_MONTHLY:
rrule->freq = ICAL_MONTHLY_RECURRENCE;
break;
case GCAL_RECURRENCE_YEARLY:
rrule->freq = ICAL_YEARLY_RECURRENCE;
break;
case GCAL_RECURRENCE_NO_REPEAT:
rrule->freq = ICAL_NO_RECURRENCE;
break;
case GCAL_RECURRENCE_MON_FRI:
{
rrule->freq = ICAL_WEEKLY_RECURRENCE;
rrule->by_day[0] = ICAL_MONDAY_WEEKDAY;
rrule->by_day[1] = ICAL_TUESDAY_WEEKDAY;
rrule->by_day[2] = ICAL_WEDNESDAY_WEEKDAY;
rrule->by_day[3] = ICAL_THURSDAY_WEEKDAY;
rrule->by_day[4] = ICAL_FRIDAY_WEEKDAY;
break;
}
default:
rrule->freq = ICAL_NO_RECURRENCE;
break;
}
switch (recur->limit_type)
{
case GCAL_RECURRENCE_COUNT:
rrule->count = recur->limit.count;
break;
case GCAL_RECURRENCE_UNTIL:
{
rrule->until.second = g_date_time_get_second (recur->limit.until);
rrule->until.minute = g_date_time_get_minute (recur->limit.until);
rrule->until.hour = g_date_time_get_hour (recur->limit.until);
rrule->until.day = g_date_time_get_day_of_month (recur->limit.until);
rrule->until.month = g_date_time_get_month (recur->limit.until);
rrule->until.year = g_date_time_get_year (recur->limit.until);
break;
}
case GCAL_RECURRENCE_FOREVER:
break;
default:
break;
}
return rrule;
}
+93
View File
@@ -0,0 +1,93 @@
/* gcal-recurrence.h
*
* Copyright (C) 2017 Yash Singh <yashdev10p@gmail.com>
*
* gnome-calendar 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.
*
* gnome-calendar 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/>.
*/
#ifndef GCAL_RECURRENCE_H
#define GCAL_RECURRENCE_H
#include <glib-object.h>
#include <libecal/libecal.h>
G_BEGIN_DECLS
#define GCAL_TYPE_RECURRENCE (gcal_recurrence_get_type())
typedef struct _GcalRecurrence GcalRecurrence;
typedef enum
{
GCAL_RECURRENCE_NO_REPEAT,
GCAL_RECURRENCE_DAILY,
GCAL_RECURRENCE_MON_FRI,
GCAL_RECURRENCE_WEEKLY,
GCAL_RECURRENCE_MONTHLY,
GCAL_RECURRENCE_YEARLY,
GCAL_RECURRENCE_OTHER
} GcalRecurrenceFrequency;
typedef enum
{
GCAL_RECURRENCE_FOREVER,
GCAL_RECURRENCE_COUNT,
GCAL_RECURRENCE_UNTIL
} GcalRecurrenceLimitType;
typedef enum
{
GCAL_RECURRENCE_MOD_THIS_ONLY = E_CAL_OBJ_MOD_THIS,
GCAL_RECURRENCE_MOD_THIS_AND_FUTURE = E_CAL_OBJ_MOD_THIS_AND_FUTURE,
GCAL_RECURRENCE_MOD_ALL = E_CAL_OBJ_MOD_ALL,
} GcalRecurrenceModType;
struct _GcalRecurrence
{
gint ref_count;
GcalRecurrenceFrequency frequency;
GcalRecurrenceLimitType limit_type;
struct
{
GDateTime *until;
guint count;
} limit;
};
GType gcal_recurrence_get_type (void) G_GNUC_CONST;
GcalRecurrence* gcal_recurrence_new (void);
GcalRecurrence* gcal_recurrence_copy (GcalRecurrence *recur);
GcalRecurrence* gcal_recurrence_ref (GcalRecurrence *self);
void gcal_recurrence_unref (GcalRecurrence *self);
gboolean gcal_recurrence_is_equal (GcalRecurrence *recur1,
GcalRecurrence *recur2);
GcalRecurrence* gcal_recurrence_parse_recurrence_rules (ECalComponent *comp);
struct icalrecurrencetype* gcal_recurrence_to_rrule (GcalRecurrence *recur);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcalRecurrence, gcal_recurrence_unref)
G_END_DECLS
#endif /* GCAL_RECURRENCE_H */
+322
View File
@@ -0,0 +1,322 @@
/* gcal-timer.c
*
* Copyright (C) 2017 - Florian Brosch <flo.brosch@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/>.
*/
#define G_LOG_DOMAIN "Timer"
#include "gcal-timer.h"
/**
* The #GcalTimer object structure.
*
* Do not use this one as #GSource.
* Only use it in combination with its
* methods.
*/
typedef struct _GcalTimer
{
GSource parent;
gint64 last_event;
gint64 default_duration;
} GcalTimer;
/* CbWrapperData:
* @timer: The timer the callback applies to.
* @callback: Original callback.
* @destroy_notify: Original user-data destroy function.
* @data: Original user data.
*
* Internal struct used to hide GSource internals
* from API users.
*/
typedef struct
{
GcalTimer *timer; /* unowned */
GCalTimerFunc callback;
GDestroyNotify destroy_notify;
gpointer data;
} CbWrapperData;
static void timer_func_destroy_notify_wrapper (CbWrapperData *wrapper);
static gboolean timer_func_wrapper (CbWrapperData *wrapper);
static gboolean timer_source_dispatch (GcalTimer *self,
GSourceFunc callback,
CbWrapperData *user_data);
static void schedule_next (GcalTimer *self);
static void timer_source_finalize (GcalTimer *self);
/*< private >*/
static void
timer_func_destroy_notify_wrapper (CbWrapperData *wrapper)
{
g_return_if_fail (wrapper != NULL);
if (wrapper->destroy_notify != NULL && wrapper->data != NULL)
wrapper->destroy_notify (wrapper->data);
g_free (wrapper);
}
static gboolean
timer_func_wrapper (CbWrapperData *wrapper)
{
g_return_val_if_fail (wrapper != NULL, G_SOURCE_REMOVE);
if (wrapper->callback != NULL)
wrapper->callback (wrapper->timer, wrapper->data);
return G_SOURCE_CONTINUE;
}
static gboolean
timer_source_dispatch (GcalTimer *self,
GSourceFunc user_callback,
CbWrapperData *user_data)
{
gboolean result = G_SOURCE_CONTINUE;
g_return_val_if_fail (self != NULL, G_SOURCE_REMOVE);
if (user_callback != NULL)
result = user_callback (user_data);
self->last_event = g_source_get_time ((GSource*) self);
schedule_next (self);
return result;
}
static void
schedule_next (GcalTimer *self)
{
gint64 now;
gint64 next;
g_return_if_fail (self != NULL);
g_return_if_fail (self->last_event >= 0);
now = g_source_get_time ((GSource*) self);
next = self->last_event + self->default_duration*G_GUINT64_CONSTANT(1000000);
if (next > now)
g_source_set_ready_time ((GSource*) self, next);
else
g_source_set_ready_time ((GSource*) self, 0);
}
static void
timer_source_finalize (GcalTimer *self)
{
}
/*< public >*/
/**
* gcal_timer_new:
* @default_duration: The duration (>0) between two events in seconds.
*
* Creates a new #GcalTimer object.
*
* While #GcalTimers are proper #GSources, they are not intended
* to be used directly.
*
* Returns: (transfer full): A new timer object.
*/
GcalTimer*
gcal_timer_new (gint64 default_duration)
{
GMainContext *cntxt; /* unowned */
GcalTimer *self; /* owned */
g_return_val_if_fail (default_duration > 0, NULL);
static GSourceFuncs source_funcs =
{ NULL, /* prepare */
NULL, /* check */
(gboolean (*) (GSource*, GSourceFunc, gpointer)) timer_source_dispatch,
(void (*) (GSource*)) timer_source_finalize
};
self = (GcalTimer*) g_source_new (&source_funcs, sizeof (GcalTimer));
self->default_duration = default_duration;
self->last_event = -1;
cntxt = g_main_context_default ();
g_source_set_ready_time ((GSource*) self, -1);
g_source_attach ((GSource*) self, cntxt);
return g_steal_pointer (&self);
}
/**
* gcal_timer_set_start:
* @self: The #GcalTimer.
*
* Starts this timer.
*/
void
gcal_timer_start (GcalTimer *self)
{
g_return_if_fail (self != NULL);
g_return_if_fail (!gcal_timer_is_running (self));
self->last_event = g_source_get_time ((GSource*) self);
schedule_next (self);
}
/**
* gcal_timer_set_reset:
* @self: The #GcalTimer.
*
* Re-schedules next event.
*/
void
gcal_timer_reset (GcalTimer *self)
{
g_return_if_fail (self != NULL);
g_return_if_fail (gcal_timer_is_running (self));
self->last_event = g_source_get_time ((GSource*) self);
schedule_next (self);
}
/**
* gcal_timer_set_stop:
* @self: The #GcalTimer.
*
* Stops a previously started timer.
*/
void
gcal_timer_stop (GcalTimer *self)
{
g_return_if_fail (self != NULL);
g_return_if_fail (gcal_timer_is_running (self));
g_source_set_ready_time ((GSource*) self, -1);
}
/**
* gcal_timer_is_running
* @self: The #GcalTimer.
*
* Whether this timer is running.
*/
gboolean
gcal_timer_is_running (GcalTimer *self)
{
g_return_val_if_fail (self != NULL, FALSE);
return g_source_get_ready_time ((GSource*) self) >= 0;
}
/**
* gcal_timer_set_duration:
* @self: The #GcalTimer.
* @duration: The new duration
*
* Changes the duration between two events.
*/
void
gcal_timer_set_default_duration (GcalTimer *self,
gint64 duration)
{
gint64 now;
g_return_if_fail (self != NULL);
g_return_if_fail (duration > 0);
now = g_source_get_time ((GSource*) self);
self->default_duration = duration;
if (!gcal_timer_is_running (self))
/* nothing to do (not running) */;
else if (self->last_event + duration < now)
g_source_set_ready_time ((GSource*) self, 0);
else
schedule_next (self);
}
/**
* gcal_timer_get_default_duration:
* @self: The #GcalTimer.
*
* Returns the default duration.
*/
gint64
gcal_timer_get_default_duration (GcalTimer *self)
{
g_return_val_if_fail (self != NULL, -1);
return self->default_duration;
}
/**
* gcal_timer_set_callback:
* @self: The #GcalTimer.
* @func: The event handler callback function.
* @data: (nullable): User data passed to @func.
* @notify: (nullable): free or unref function for @data.
*
* Sets the callback to be triggered after each duration
* or given day-times.
*/
void
gcal_timer_set_callback (GcalTimer *self,
GCalTimerFunc func,
gpointer data,
GDestroyNotify notify)
{
CbWrapperData *wrapper; /* owned */
g_return_if_fail (self != NULL);
g_return_if_fail (func != NULL);
wrapper = g_new0 (CbWrapperData, 1);
wrapper->timer = self;
wrapper->callback = func;
wrapper->destroy_notify = notify;
wrapper->data = data;
g_source_set_callback ((GSource*) self,
(GSourceFunc) timer_func_wrapper,
g_steal_pointer (&wrapper),
(GDestroyNotify) timer_func_destroy_notify_wrapper);
}
/**
* gcal_timer_free:
* @self: The #GcalTimer.
*
* Frees #GcalTimer.
*/
void
gcal_timer_free (GcalTimer *self)
{
g_return_if_fail (self != NULL);
g_source_destroy ((GSource *) self);
g_source_unref ((GSource *) self);
}
+53
View File
@@ -0,0 +1,53 @@
/* gcal-timer.h
*
* Copyright (C) 2017 - Florian Brosch <flo.brosch@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/>.
*/
#ifndef __GCAL_TIMER_H__
#define __GCAL_TIMER_H__
#include <glib.h>
struct _GcalTimer;
typedef struct _GcalTimer GcalTimer;
typedef void (*GCalTimerFunc) (GcalTimer *self, gpointer data);
GcalTimer* gcal_timer_new (gint64 default_duration);
void gcal_timer_start (GcalTimer *self);
void gcal_timer_reset (GcalTimer *self);
void gcal_timer_stop (GcalTimer *self);
gboolean gcal_timer_is_running (GcalTimer *self);
void gcal_timer_set_default_duration (GcalTimer *self,
gint64 duration);
gint64 gcal_timer_get_default_duration (GcalTimer *self);
void gcal_timer_set_callback (GcalTimer *self,
GCalTimerFunc func,
gpointer data,
GDestroyNotify notify);
void gcal_timer_free (GcalTimer *self);
#endif /* __GCAL_TIMER_H__ */