project: Reorganize much

This commit is contained in:
Georges Basile Stavracas Neto
2019-05-04 19:48:40 -03:00
parent c3e1a377b7
commit a2609d6853
15 changed files with 16 additions and 43 deletions
+422
View File
@@ -0,0 +1,422 @@
/* gcal-context.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 "GcalContext"
#include "gcal-context.h"
#include "gcal-night-light-monitor.h"
#include "gcal-time-zone-monitor.h"
struct _GcalContext
{
GObject parent;
GSettings *desktop_settings;
GcalClock *clock;
GoaClient *goa_client;
GcalManager *manager;
GcalSearchEngine *search_engine;
GSettings *settings;
GcalTimeFormat time_format;
GcalWeatherService *weather_service;
GcalNightLightMonitor *night_light_monitor;
GcalTimeZoneMonitor *timezone_monitor;
};
G_DEFINE_TYPE (GcalContext, gcal_context, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_CLOCK,
PROP_GOA_CLIENT,
PROP_MANAGER,
PROP_SEARCH_ENGINE,
PROP_SETTINGS,
PROP_TIME_FORMAT,
PROP_TIMEZONE,
PROP_WEATHER_SERVICE,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/*
* Auxiliary methods
*/
static void
load_time_format (GcalContext *self)
{
g_autofree gchar *clock_format = NULL;
g_autofree gchar *enum_format = NULL;
clock_format = g_settings_get_string (self->desktop_settings, "clock-format");
if (g_strcmp0 (clock_format, "12h") == 0)
self->time_format = GCAL_TIME_FORMAT_12H;
else
self->time_format = GCAL_TIME_FORMAT_24H;
enum_format = g_enum_to_string (GCAL_TYPE_TIME_FORMAT, self->time_format);
g_debug ("Setting time format to %s", enum_format);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIME_FORMAT]);
}
/*
* Callbacks
*/
static void
on_timezone_changed_cb (GcalTimeZoneMonitor *timezone_monitor,
GParamSpec *pspec,
GcalContext *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIMEZONE]);
}
/*
* GObject overrides
*/
static void
gcal_context_finalize (GObject *object)
{
GcalContext *self = (GcalContext *)object;
gcal_weather_service_stop (self->weather_service);
g_clear_object (&self->clock);
g_clear_object (&self->desktop_settings);
g_clear_object (&self->goa_client);
g_clear_object (&self->manager);
g_clear_object (&self->night_light_monitor);
g_clear_object (&self->timezone_monitor);
g_clear_object (&self->weather_service);
G_OBJECT_CLASS (gcal_context_parent_class)->finalize (object);
}
static void
gcal_context_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalContext *self = GCAL_CONTEXT (object);
switch (prop_id)
{
case PROP_CLOCK:
g_value_set_object (value, self->clock);
break;
case PROP_GOA_CLIENT:
g_value_set_object (value, self->goa_client);
break;
case PROP_MANAGER:
g_value_set_object (value, self->manager);
break;
case PROP_SEARCH_ENGINE:
g_value_set_object (value, self->search_engine);
break;
case PROP_SETTINGS:
g_value_set_object (value, self->settings);
break;
case PROP_TIME_FORMAT:
g_value_set_enum (value, self->time_format);
break;
case PROP_TIMEZONE:
g_value_set_boxed (value, gcal_time_zone_monitor_get_timezone (self->timezone_monitor));
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_context_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_CLOCK:
case PROP_GOA_CLIENT:
case PROP_MANAGER:
case PROP_SEARCH_ENGINE:
case PROP_SETTINGS:
case PROP_TIME_FORMAT:
case PROP_TIMEZONE:
case PROP_WEATHER_SERVICE:
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_context_class_init (GcalContextClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_context_finalize;
object_class->get_property = gcal_context_get_property;
object_class->set_property = gcal_context_set_property;
properties[PROP_CLOCK] = g_param_spec_object ("clock",
"Wall clock",
"Main clock driving the calendar timeline",
GCAL_TYPE_CLOCK,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_GOA_CLIENT] = g_param_spec_object ("goa-client",
"Online Accounts client",
"Online Accounts client",
GOA_TYPE_CLIENT,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_MANAGER] = g_param_spec_object ("manager",
"Data manager",
"Data manager of the application",
GCAL_TYPE_MANAGER,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_SEARCH_ENGINE] = g_param_spec_object ("search-engine",
"Search engine",
"Search engine",
GCAL_TYPE_SEARCH_ENGINE,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_SETTINGS] = g_param_spec_object ("settings",
"GNOME Calendar settings",
"GNOME Calendar settings",
G_TYPE_SETTINGS,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_TIME_FORMAT] = g_param_spec_enum ("time-format",
"Time format",
"System time format",
GCAL_TYPE_TIME_FORMAT,
GCAL_TIME_FORMAT_24H,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_TIMEZONE] = g_param_spec_boxed ("timezone",
"Timezone",
"System timezone",
G_TYPE_TIME_ZONE,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
properties[PROP_WEATHER_SERVICE] = g_param_spec_object ("weather-service",
"Weather service",
"Weather service",
GCAL_TYPE_WEATHER_SERVICE,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_context_init (GcalContext *self)
{
self->clock = gcal_clock_new ();
self->goa_client = goa_client_new_sync (NULL, NULL);
self->manager = gcal_manager_new ();
self->settings = g_settings_new ("org.gnome.calendar");
self->weather_service = gcal_weather_service_new ();
self->search_engine = gcal_search_engine_new (self);
self->timezone_monitor = gcal_time_zone_monitor_new ();
g_signal_connect_object (self->timezone_monitor,
"notify::timezone",
G_CALLBACK (on_timezone_changed_cb),
self,
0);
/* Time format */
self->desktop_settings = g_settings_new ("org.gnome.desktop.interface");
g_signal_connect_object (self->desktop_settings,
"changed::clock-format",
G_CALLBACK (load_time_format),
self,
G_CONNECT_SWAPPED);
load_time_format (self);
}
/**
* gcal_context_new: (skip)
*
* Create a new GcalContext. Only one context must be
* available during the application lifetime.
*
* Returns: (transfer full): a #GcalContext
*/
GcalContext*
gcal_context_new (void)
{
return g_object_new (GCAL_TYPE_CONTEXT, NULL);
}
/**
* gcal_context_get_clock:
*
* Retrieves the #GcalClock from @self.
*
* Returns: (transfer none): a #GcalClock
*/
GcalClock*
gcal_context_get_clock (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->clock;
}
/**
* gcal_context_get_goa_client:
*
* Retrieves the #GoaClient from @self.
*
* Returns: (transfer none): a #GoaClient
*/
GoaClient*
gcal_context_get_goa_client (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->goa_client;
}
/**
* gcal_context_get_manager:
*
* Retrieves the #GcalManager from @self.
*
* Returns: (transfer none): a #GcalManager
*/
GcalManager*
gcal_context_get_manager (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->manager;
}
/**
* gcal_context_get_search_engine:
*
* Retrieves the #GcalSearchEngine from @self.
*
* Returns: (transfer none): a #GcalSearchEngine
*/
GcalSearchEngine*
gcal_context_get_search_engine (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->search_engine;
}
/**
* gcal_context_get_settings:
*
* Retrieves the #GSettings from @self.
*
* Returns: (transfer none): a #GSettings
*/
GSettings*
gcal_context_get_settings (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->settings;
}
/**
* gcal_context_get_time_format:
*
* Retrieves the #GcalTimeFormat from @self.
*
* Returns: a #GcalTimeFormat
*/
GcalTimeFormat
gcal_context_get_time_format (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), 0);
return self->time_format;
}
/**
* gcal_context_get_timezone:
*
* Retrieves the system timezone.
*
* Returns: (transfer none): a #GTimeZone
*/
GTimeZone*
gcal_context_get_timezone (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return gcal_time_zone_monitor_get_timezone (self->timezone_monitor);
}
/**
* gcal_context_get_weather_service:
*
* Retrieves the #GcalWeatherService from @self.
*
* Returns: (transfer none): a #GcalWeatherService
*/
GcalWeatherService*
gcal_context_get_weather_service (GcalContext *self)
{
g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
return self->weather_service;
}
void
gcal_context_startup (GcalContext *self)
{
g_return_if_fail (GCAL_IS_CONTEXT (self));
/* Night Light requires GTK to be initialized */
self->night_light_monitor = gcal_night_light_monitor_new (self);
gcal_manager_startup (self->manager);
}
+57
View File
@@ -0,0 +1,57 @@
/* gcal-context.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 "gcal-clock.h"
#include "gcal-enums.h"
#include "gcal-manager.h"
#include "gcal-search-engine.h"
#include "weather/gcal-weather-service.h"
#include <glib-object.h>
#include <goa/goa.h>
G_BEGIN_DECLS
#define GCAL_TYPE_CONTEXT (gcal_context_get_type())
G_DECLARE_FINAL_TYPE (GcalContext, gcal_context, GCAL, CONTEXT, GObject)
GcalContext* gcal_context_new (void);
GcalClock* gcal_context_get_clock (GcalContext *self);
GoaClient* gcal_context_get_goa_client (GcalContext *self);
GcalManager* gcal_context_get_manager (GcalContext *self);
GcalSearchEngine* gcal_context_get_search_engine (GcalContext *self);
GSettings* gcal_context_get_settings (GcalContext *self);
GcalTimeFormat gcal_context_get_time_format (GcalContext *self);
GTimeZone* gcal_context_get_timezone (GcalContext *self);
GcalWeatherService* gcal_context_get_weather_service (GcalContext *self);
void gcal_context_startup (GcalContext *self);
G_END_DECLS
+107
View File
@@ -0,0 +1,107 @@
/* gcal-log.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/>.
*/
#include "gcal-debug.h"
#include "gcal-log.h"
#include <unistd.h>
#include <glib.h>
G_LOCK_DEFINE_STATIC (channel_lock);
GIOChannel *standard_channel = NULL;
static const gchar* ignored_domains[] =
{
"GdkPixbuf",
NULL
};
static const gchar *
log_level_str (GLogLevelFlags log_level)
{
switch (((gulong)log_level & G_LOG_LEVEL_MASK))
{
case G_LOG_LEVEL_ERROR: return " \033[1;31mERROR\033[0m";
case G_LOG_LEVEL_CRITICAL: return "\033[1;35mCRITICAL\033[0m";
case G_LOG_LEVEL_WARNING: return " \033[1;33mWARNING\033[0m";
case G_LOG_LEVEL_MESSAGE: return " \033[1;34mMESSAGE\033[0m";
case G_LOG_LEVEL_INFO: return " \033[1;32mINFO\033[0m";
case G_LOG_LEVEL_DEBUG: return " \033[1;32mDEBUG\033[0m";
case GCAL_LOG_LEVEL_TRACE: return " \033[1;36mTRACE\033[0m";
default:
return " UNKNOWN";
}
}
static void
gcal_log_handler (const gchar *domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
GTimeVal tv;
struct tm tt;
time_t t;
const gchar *level;
gchar ftime[32];
gchar *buffer;
/* Skip ignored log domains */
if (domain && g_strv_contains (ignored_domains, domain))
return;
level = log_level_str (log_level);
g_get_current_time (&tv);
t = (time_t) tv.tv_sec;
tt = *localtime (&t);
strftime (ftime, sizeof (ftime), "%H:%M:%S", &tt);
buffer = g_strdup_printf ("%s.%04ld %24s: %s: %s\n",
ftime,
tv.tv_usec / 1000,
domain,
level,
message);
/* Safely write to the channel */
G_LOCK (channel_lock);
g_io_channel_write_chars (standard_channel, buffer, -1, NULL, NULL);
g_io_channel_flush (standard_channel, NULL);
G_UNLOCK (channel_lock);
g_free (buffer);
}
void
gcal_log_init (void)
{
static gsize initialized = FALSE;
if (g_once_init_enter (&initialized))
{
standard_channel = g_io_channel_unix_new (STDOUT_FILENO);
g_log_set_default_handler (gcal_log_handler, NULL);
g_once_init_leave (&initialized, TRUE);
}
}
+30
View File
@@ -0,0 +1,30 @@
/* gcal-log.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_LOG_H
#define GCAL_LOG_H
#include <glib-object.h>
G_BEGIN_DECLS
void gcal_log_init (void);
G_END_DECLS
#endif /* GCAL_LOG_H */
+206
View File
@@ -0,0 +1,206 @@
/* gcal-night-light-monitor.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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "gcal-debug.h"
#include "gcal-night-light-monitor.h"
struct _GcalNightLightMonitor
{
GObject parent;
GcalContext *context;
GDBusProxy *night_light_proxy;
};
G_DEFINE_TYPE (GcalNightLightMonitor, gcal_night_light_monitor, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_CONTEXT,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/*
* Callbacks
*/
static void
on_night_light_proxy_properties_changed_cb (GcalNightLightMonitor *self,
GVariant *properties,
const gchar * const *invalidated,
GDBusProxy *proxy)
{
GSettings *settings;
g_assert (GCAL_IS_NIGHT_LIGHT_MONITOR (self));
g_assert (G_IS_DBUS_PROXY (proxy));
settings = gcal_context_get_settings (self->context);
if (g_settings_get_boolean (settings, "follow-night-light"))
{
g_autoptr (GVariant) activev = NULL;
gboolean active;
activev = g_dbus_proxy_get_cached_property (proxy, "NightLightActive");
active = g_variant_get_boolean (activev);
g_object_set (gtk_settings_get_default (),
"gtk-application-prefer-dark-theme",
active,
NULL);
}
}
/*
* GObject overrides
*/
static void
gcal_night_light_monitor_finalize (GObject *object)
{
GcalNightLightMonitor *self = (GcalNightLightMonitor *)object;
g_clear_object (&self->context);
g_clear_object (&self->night_light_proxy);
G_OBJECT_CLASS (gcal_night_light_monitor_parent_class)->finalize (object);
}
static void
gcal_night_light_monitor_constructed (GObject *object)
{
GcalNightLightMonitor *self = (GcalNightLightMonitor *)object;
g_autoptr (GDBusConnection) connection = NULL;
g_autoptr (GDBusProxy) proxy = NULL;
GCAL_ENTRY;
G_OBJECT_CLASS (gcal_night_light_monitor_parent_class)->constructed (object);
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
if (!connection)
GCAL_RETURN ();
proxy = g_dbus_proxy_new_sync (connection,
G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
NULL,
"org.gnome.SettingsDaemon.Color",
"/org/gnome/SettingsDaemon/Color",
"org.gnome.SettingsDaemon.Color",
NULL,
NULL);
if (!proxy)
GCAL_RETURN ();
g_debug ("Created Night Light monitor");
g_signal_connect_object (proxy,
"g-properties-changed",
G_CALLBACK (on_night_light_proxy_properties_changed_cb),
self,
G_CONNECT_SWAPPED);
self->night_light_proxy = g_steal_pointer (&proxy);
on_night_light_proxy_properties_changed_cb (self, NULL, NULL, self->night_light_proxy);
GCAL_EXIT;
}
static void
gcal_night_light_monitor_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalNightLightMonitor *self = GCAL_NIGHT_LIGHT_MONITOR (object);
switch (prop_id)
{
case PROP_CONTEXT:
g_value_set_object (value, self->context);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_night_light_monitor_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GcalNightLightMonitor *self = GCAL_NIGHT_LIGHT_MONITOR (object);
switch (prop_id)
{
case PROP_CONTEXT:
g_assert (self->context == NULL);
self->context = g_value_dup_object (value);
g_object_notify_by_pspec (object, pspec);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_night_light_monitor_class_init (GcalNightLightMonitorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_night_light_monitor_finalize;
object_class->constructed = gcal_night_light_monitor_constructed;
object_class->get_property = gcal_night_light_monitor_get_property;
object_class->set_property = gcal_night_light_monitor_set_property;
properties[PROP_CONTEXT] = g_param_spec_object ("context",
"Context",
"Context",
GCAL_TYPE_CONTEXT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_night_light_monitor_init (GcalNightLightMonitor *self)
{
}
GcalNightLightMonitor*
gcal_night_light_monitor_new (GcalContext *context)
{
return g_object_new (GCAL_TYPE_NIGHT_LIGHT_MONITOR,
"context", context,
NULL);
}
+34
View File
@@ -0,0 +1,34 @@
/* gcal-night-light-monitor.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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include "gcal-context.h"
#include "gcal-window.h"
G_BEGIN_DECLS
#define GCAL_TYPE_NIGHT_LIGHT_MONITOR (gcal_night_light_monitor_get_type())
G_DECLARE_FINAL_TYPE (GcalNightLightMonitor, gcal_night_light_monitor, GCAL, NIGHT_LIGHT_MONITOR, GObject)
GcalNightLightMonitor* gcal_night_light_monitor_new (GcalContext *context);
G_END_DECLS
+497
View File
@@ -0,0 +1,497 @@
/* gcal-shell-search-provider.c
*
* Copyright (C) 2015 Erick Pérez Castellanos <erick.red@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 "GcalShellSearchProvider"
#include "gcal-shell-search-provider.h"
#include "gcal-shell-search-provider-generated.h"
#include "gcal-application.h"
#include "gcal-debug.h"
#include "gcal-event.h"
#include "gcal-window.h"
#include "gcal-utils.h"
typedef struct
{
GDBusMethodInvocation *invocation;
gchar **terms;
} PendingSearch;
struct _GcalShellSearchProvider
{
GObject parent;
GcalShellSearchProvider2 *skel;
GcalContext *context;
PendingSearch *pending_search;
GHashTable *events;
};
static void gcal_subscriber_interface_init (ECalDataModelSubscriberInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GcalShellSearchProvider, gcal_shell_search_provider, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (E_TYPE_CAL_DATA_MODEL_SUBSCRIBER, gcal_subscriber_interface_init));
enum
{
PROP_0,
PROP_CONTEXT,
N_PROPS
};
static GParamSpec* properties[N_PROPS] = { NULL, };
/*
* Auxiliary methods
*/
static gint
sort_event_data (GcalEvent *a,
GcalEvent *b,
gpointer user_data)
{
return gcal_event_compare_with_current (a, b, GPOINTER_TO_INT (user_data));
}
static gboolean
execute_search (GcalShellSearchProvider *self)
{
g_autoptr (GDateTime) start = NULL;
g_autoptr (GDateTime) end = NULL;
g_autoptr (GDateTime) now = NULL;
g_autofree gchar *search_query = NULL;
GcalManager *manager;
time_t range_start, range_end;
guint i;
GCAL_ENTRY;
manager = gcal_context_get_manager (self->context);
now = g_date_time_new_now (gcal_context_get_timezone (self->context));
start = g_date_time_add_weeks (now, -1);
range_start = g_date_time_to_unix (start);
end = g_date_time_add_weeks (now, 3);
range_end = g_date_time_to_unix (end);
gcal_manager_set_shell_search_subscriber (manager,
E_CAL_DATA_MODEL_SUBSCRIBER (self),
range_start, range_end);
search_query = g_strdup_printf ("(or (contains? \"summary\" \"%s\") (contains? \"description\" \"%s\"))",
self->pending_search->terms[0], self->pending_search->terms[0]);
for (i = 1; i < g_strv_length (self->pending_search->terms); i++)
{
g_autofree gchar *complete_query = NULL;
g_autofree gchar *second_query = NULL;
second_query = g_strdup_printf ("(or (contains? \"summary\" \"%s\") (contains? \"description\" \"%s\"))",
self->pending_search->terms[0], self->pending_search->terms[0]);
complete_query = g_strdup_printf ("(and %s %s)", search_query, second_query);
g_clear_pointer (&search_query, g_free);
search_query = g_steal_pointer (&complete_query);
}
gcal_manager_set_shell_search_query (manager, search_query);
g_application_hold (g_application_get_default ());
GCAL_RETURN (FALSE);
}
static void
schedule_search (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar **terms)
{
GCAL_ENTRY;
/* don't attempt searches for a single character */
if (g_strv_length (terms) == 1 && g_utf8_strlen (terms[0], -1) == 1)
{
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(as)", NULL));
GCAL_RETURN ();
}
if (self->pending_search != NULL)
{
g_object_unref (self->pending_search->invocation);
g_strfreev (self->pending_search->terms);
g_application_release (g_application_get_default ());
}
else
{
self->pending_search = g_new0 (PendingSearch, 1);
}
self->pending_search->invocation = g_object_ref (invocation);
self->pending_search->terms = g_strdupv (terms);
execute_search (self);
GCAL_EXIT;
}
/*
* Callbacks
*/
static gboolean
get_initial_result_set_cb (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar **terms,
GcalShellSearchProvider2 *skel)
{
schedule_search (self, invocation, terms);
return TRUE;
}
static gboolean
get_subsearch_result_set_cb (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar **previous_results,
gchar **terms,
GcalShellSearchProvider2 *skel)
{
schedule_search (self, invocation, terms);
return TRUE;
}
static gboolean
get_result_metas_cb (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar **results,
GcalShellSearchProvider2 *skel)
{
GDateTime *local_datetime;
GVariantBuilder abuilder, builder;
GcalEvent *event;
gchar *uuid, *desc;
gchar *start_date;
gint i;
GCAL_ENTRY;
g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
for (i = 0; i < g_strv_length (results); i++)
{
g_autoptr (GVariant) icon_variant = NULL;
g_autoptr (GdkPixbuf) gicon = NULL;
cairo_surface_t *surface;
uuid = results[i];
event = g_hash_table_lookup (self->events, uuid);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (uuid));
g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (gcal_event_get_summary (event)));
surface = get_circle_surface_from_color (gcal_event_get_color (event), 96);
gicon = gdk_pixbuf_get_from_surface (surface, 0, 0, 96, 96);
icon_variant = g_icon_serialize (G_ICON (gicon));
g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
local_datetime = g_date_time_to_local (gcal_event_get_date_start (event));
start_date = g_date_time_format (local_datetime, gcal_event_get_all_day (event) ? "%x" : "%c");
if (gcal_event_get_location (event))
desc = g_strconcat (start_date, ". ", gcal_event_get_location (event), NULL);
else
desc = g_strdup (start_date);
g_variant_builder_add (&builder, "{sv}", "description", g_variant_new_string (desc));
g_variant_builder_add_value (&abuilder, g_variant_builder_end (&builder));
g_clear_pointer (&surface, cairo_surface_destroy);
}
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));
GCAL_RETURN (TRUE);
}
static gboolean
activate_result_cb (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar *result,
gchar **terms,
guint32 timestamp,
GcalShellSearchProvider2 *skel)
{
g_autoptr (GcalEvent) event = NULL;
GApplication *application;
GcalManager *manager;
GDateTime *dtstart;
GCAL_ENTRY;
application = g_application_get_default ();
manager = gcal_context_get_manager (self->context);
event = gcal_manager_get_event_from_shell_search (manager, result);
dtstart = gcal_event_get_date_start (event);
gcal_application_set_uuid (GCAL_APPLICATION (application), result);
gcal_application_set_initial_date (GCAL_APPLICATION (application), dtstart);
g_application_activate (application);
GCAL_RETURN (TRUE);
}
static gboolean
launch_search_cb (GcalShellSearchProvider *self,
GDBusMethodInvocation *invocation,
gchar **terms,
guint32 timestamp,
GcalShellSearchProvider2 *skel)
{
g_autofree gchar *terms_joined = NULL;
GApplication *application;
GcalWindow *window;
GCAL_ENTRY;
application = g_application_get_default ();
g_application_activate (application);
terms_joined = g_strjoinv (" ", terms);
window = (GcalWindow *) gtk_application_get_active_window (GTK_APPLICATION (application));
if (window)
gcal_window_set_search_query (window, terms_joined);
GCAL_RETURN (TRUE);
}
static gboolean
query_completed_cb (GcalShellSearchProvider *self,
GcalManager *manager)
{
GVariantBuilder builder;
GList *events, *l;
time_t current_time_t;
GCAL_ENTRY;
g_hash_table_remove_all (self->events);
events = gcal_manager_get_shell_search_events (manager);
if (events == NULL)
{
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", NULL));
GCAL_GOTO (out);
}
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
current_time_t = time (NULL);
events = g_list_sort_with_data (events, (GCompareDataFunc) sort_event_data, GINT_TO_POINTER (current_time_t));
for (l = events; l != NULL; l = g_list_next (l))
{
const gchar *uid;
uid = gcal_event_get_uid (l->data);
if (g_hash_table_contains (self->events, uid))
continue;
g_variant_builder_add (&builder, "s", uid);
g_hash_table_insert (self->events, g_strdup (uid), l->data);
}
g_list_free (events);
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", &builder));
out:
g_clear_object (&self->pending_search->invocation);
g_clear_pointer (&self->pending_search->terms, g_strfreev);
g_clear_pointer (&self->pending_search, g_free);
g_application_release (g_application_get_default ());
GCAL_RETURN (FALSE);
}
/*
* ECalDataModelSubscriber iface
*/
static void
gcal_shell_search_provider_component_changed (ECalDataModelSubscriber *subscriber,
ECalClient *client,
ECalComponent *comp)
{
;
}
static void
gcal_shell_search_provider_component_removed (ECalDataModelSubscriber *subscriber,
ECalClient *client,
const gchar *uid,
const gchar *rid)
{
;
}
static void
gcal_shell_search_provider_freeze (ECalDataModelSubscriber *subscriber)
{
;
}
static void
gcal_shell_search_provider_thaw (ECalDataModelSubscriber *subscriber)
{
;
}
static void
gcal_subscriber_interface_init (ECalDataModelSubscriberInterface *iface)
{
iface->component_added = gcal_shell_search_provider_component_changed;
iface->component_modified = gcal_shell_search_provider_component_changed;
iface->component_removed = gcal_shell_search_provider_component_removed;
iface->freeze = gcal_shell_search_provider_freeze;
iface->thaw = gcal_shell_search_provider_thaw;
}
/*
* GObject overrides
*/
static void
gcal_shell_search_provider_finalize (GObject *object)
{
GcalShellSearchProvider *self = (GcalShellSearchProvider *) object;
g_clear_pointer (&self->events, g_hash_table_destroy);
g_clear_object (&self->context);
g_clear_object (&self->skel);
G_OBJECT_CLASS (gcal_shell_search_provider_parent_class)->finalize (object);
}
static void
gcal_shell_search_provider_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GcalShellSearchProvider *self = GCAL_SHELL_SEARCH_PROVIDER (object);
switch (property_id)
{
case PROP_CONTEXT:
g_value_set_object (value, self->context);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gcal_shell_search_provider_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GcalShellSearchProvider *self = GCAL_SHELL_SEARCH_PROVIDER (object);
switch (property_id)
{
case PROP_CONTEXT:
if (g_set_object (&self->context, g_value_get_object (value)))
{
GcalManager *manager = gcal_context_get_manager (self->context);
gcal_manager_setup_shell_search (manager, E_CAL_DATA_MODEL_SUBSCRIBER (self));
g_signal_connect_swapped (manager, "query-completed", G_CALLBACK (query_completed_cb), self);
g_object_notify_by_pspec (object, properties[PROP_CONTEXT]);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gcal_shell_search_provider_class_init (GcalShellSearchProviderClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_shell_search_provider_finalize;
object_class->get_property = gcal_shell_search_provider_get_property;
object_class->set_property = gcal_shell_search_provider_set_property;
properties[PROP_CONTEXT] = g_param_spec_object ("context",
"The context object",
"The context object",
GCAL_TYPE_CONTEXT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_shell_search_provider_init (GcalShellSearchProvider *self)
{
self->events = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
self->skel = gcal_shell_search_provider2_skeleton_new ();
g_signal_connect_swapped (self->skel, "handle-get-initial-result-set", G_CALLBACK (get_initial_result_set_cb), self);
g_signal_connect_swapped (self->skel, "handle-get-subsearch-result-set", G_CALLBACK (get_subsearch_result_set_cb), self);
g_signal_connect_swapped (self->skel, "handle-get-result-metas", G_CALLBACK (get_result_metas_cb), self);
g_signal_connect_swapped (self->skel, "handle-activate-result", G_CALLBACK (activate_result_cb), self);
g_signal_connect_swapped (self->skel, "handle-launch-search", G_CALLBACK (launch_search_cb), self);
}
GcalShellSearchProvider*
gcal_shell_search_provider_new (GcalContext *context)
{
return g_object_new (GCAL_TYPE_SHELL_SEARCH_PROVIDER,
"context", context,
NULL);
}
gboolean
gcal_shell_search_provider_dbus_export (GcalShellSearchProvider *self,
GDBusConnection *connection,
const gchar *object_path,
GError **error)
{
return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skel), connection, object_path, error);
}
void
gcal_shell_search_provider_dbus_unexport (GcalShellSearchProvider *self,
GDBusConnection *connection,
const gchar *object_path)
{
if (g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (self->skel), connection))
g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (self->skel), connection);
}
+46
View File
@@ -0,0 +1,46 @@
/* gcal-shell-search-provider.h
*
* Copyright (C) 2015 Erick Pérez Castellanos <erick.red@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_SHELL_SEARCH_PROVIDER_H
#define GCAL_SHELL_SEARCH_PROVIDER_H
#include <glib-object.h>
#include <gio/gio.h>
#include "gcal-context.h"
G_BEGIN_DECLS
#define GCAL_TYPE_SHELL_SEARCH_PROVIDER (gcal_shell_search_provider_get_type())
G_DECLARE_FINAL_TYPE (GcalShellSearchProvider, gcal_shell_search_provider, GCAL, SHELL_SEARCH_PROVIDER, GObject)
GcalShellSearchProvider* gcal_shell_search_provider_new (GcalContext *context);
gboolean gcal_shell_search_provider_dbus_export (GcalShellSearchProvider *search_provider,
GDBusConnection *connection,
const gchar *object_path,
GError **error);
void gcal_shell_search_provider_dbus_unexport (GcalShellSearchProvider *search_provider,
GDBusConnection *connection,
const gchar *object_path);
G_END_DECLS
#endif /* GCAL_SHELL_SEARCH_PROVIDER_H */
+185
View File
@@ -0,0 +1,185 @@
/* gcal-time-zone-monitor.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 "GcalTimeZoneMonitor"
#include "gcal-debug.h"
#include "gcal-time-zone-monitor.h"
#include <gio/gio.h>
struct _GcalTimeZoneMonitor
{
GObject parent;
GTimeZone *timezone;
GDBusProxy *timedate1_proxy;
};
G_DEFINE_TYPE (GcalTimeZoneMonitor, gcal_time_zone_monitor, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_TIMEZONE,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/*
* Callbacks
*/
static void
on_timedate1_proxy_properties_changed_cb (GcalTimeZoneMonitor *self,
GVariant *props,
const gchar * const *invalidated,
GDBusProxy *proxy)
{
g_autoptr (GVariant) timezone_variant = NULL;
const gchar *timezone_identifier = NULL;
g_assert (GCAL_IS_TIME_ZONE_MONITOR (self));
g_assert (G_IS_DBUS_PROXY (proxy));
timezone_variant = g_dbus_proxy_get_cached_property (proxy, "Timezone");
/*
* NULL timezone_variant means NULL timezone_identifier, which falls back
* to regularly checking /etc/localtime
*/
if (timezone_variant)
timezone_identifier = g_variant_get_string (timezone_variant, NULL);
self->timezone = g_time_zone_new (timezone_identifier);
g_debug ("System timezone is %s", g_time_zone_get_identifier (self->timezone));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIMEZONE]);
}
/*
* GObject overrides
*/
static void
gcal_time_zone_monitor_finalize (GObject *object)
{
GcalTimeZoneMonitor *self = (GcalTimeZoneMonitor *)object;
g_clear_object (&self->timedate1_proxy);
g_clear_object (&self->timezone);
G_OBJECT_CLASS (gcal_time_zone_monitor_parent_class)->finalize (object);
}
static void
gcal_time_zone_monitor_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalTimeZoneMonitor *self = GCAL_TIME_ZONE_MONITOR (object);
switch (prop_id)
{
case PROP_TIMEZONE:
g_value_set_boxed (value, self->timezone);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_time_zone_monitor_class_init (GcalTimeZoneMonitorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gcal_time_zone_monitor_finalize;
object_class->get_property = gcal_time_zone_monitor_get_property;
properties[PROP_TIMEZONE] = g_param_spec_boxed ("timezone",
"Timezone",
"System timezone",
G_TYPE_TIME_ZONE,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_time_zone_monitor_init (GcalTimeZoneMonitor *self)
{
g_autoptr (GDBusConnection) connection = NULL;
g_autoptr (GDBusProxy) proxy = NULL;
GCAL_ENTRY;
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
if (!connection)
GCAL_RETURN ();
proxy = g_dbus_proxy_new_sync (connection,
G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
NULL,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
NULL,
NULL);
if (!proxy)
GCAL_RETURN ();
g_debug ("Created Time Zone monitor");
g_signal_connect_object (proxy,
"g-properties-changed",
G_CALLBACK (on_timedate1_proxy_properties_changed_cb),
self,
G_CONNECT_SWAPPED);
self->timedate1_proxy = g_steal_pointer (&proxy);
on_timedate1_proxy_properties_changed_cb (self, NULL, NULL, self->timedate1_proxy);
GCAL_EXIT;
}
GcalTimeZoneMonitor*
gcal_time_zone_monitor_new (void)
{
return g_object_new (GCAL_TYPE_TIME_ZONE_MONITOR, NULL);
}
GTimeZone*
gcal_time_zone_monitor_get_timezone (GcalTimeZoneMonitor *self)
{
g_return_val_if_fail (GCAL_IS_TIME_ZONE_MONITOR (self), NULL);
return self->timezone;
}
+35
View File
@@ -0,0 +1,35 @@
/* gcal-time-zone-monitor.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 <glib-object.h>
G_BEGIN_DECLS
#define GCAL_TYPE_TIME_ZONE_MONITOR (gcal_time_zone_monitor_get_type())
G_DECLARE_FINAL_TYPE (GcalTimeZoneMonitor, gcal_time_zone_monitor, GCAL, TIME_ZONE_MONITOR, GObject)
GcalTimeZoneMonitor* gcal_time_zone_monitor_new (void);
GTimeZone* gcal_time_zone_monitor_get_timezone (GcalTimeZoneMonitor *self);
G_END_DECLS