shell-search-provider: Instrument and modernize code
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
#include "gcal-shell-search-provider-generated.h"
|
#include "gcal-shell-search-provider-generated.h"
|
||||||
|
|
||||||
#include "gcal-application.h"
|
#include "gcal-application.h"
|
||||||
|
#include "gcal-debug.h"
|
||||||
#include "gcal-event.h"
|
#include "gcal-event.h"
|
||||||
#include "gcal-window.h"
|
#include "gcal-window.h"
|
||||||
#include "gcal-utils.h"
|
#include "gcal-utils.h"
|
||||||
@@ -59,6 +60,11 @@ enum
|
|||||||
|
|
||||||
static GParamSpec* properties[N_PROPS] = { NULL, };
|
static GParamSpec* properties[N_PROPS] = { NULL, };
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Auxiliary methods
|
||||||
|
*/
|
||||||
|
|
||||||
static gint
|
static gint
|
||||||
sort_event_data (GcalEvent *a,
|
sort_event_data (GcalEvent *a,
|
||||||
GcalEvent *b,
|
GcalEvent *b,
|
||||||
@@ -70,14 +76,15 @@ sort_event_data (GcalEvent *a,
|
|||||||
static gboolean
|
static gboolean
|
||||||
execute_search (GcalShellSearchProvider *self)
|
execute_search (GcalShellSearchProvider *self)
|
||||||
{
|
{
|
||||||
guint i;
|
|
||||||
gchar *search_query;
|
|
||||||
|
|
||||||
icaltimezone *zone;
|
icaltimezone *zone;
|
||||||
|
g_autofree gchar *search_query = NULL;
|
||||||
time_t range_start, range_end;
|
time_t range_start, range_end;
|
||||||
|
guint i;
|
||||||
|
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
if (gcal_manager_get_loading (self->manager))
|
if (gcal_manager_get_loading (self->manager))
|
||||||
return TRUE;
|
GCAL_RETURN (TRUE);
|
||||||
|
|
||||||
zone = gcal_manager_get_system_timezone (self->manager);
|
zone = gcal_manager_get_system_timezone (self->manager);
|
||||||
self->pending_search->date = icaltime_current_time_with_zone (zone);
|
self->pending_search->date = icaltime_current_time_with_zone (zone);
|
||||||
@@ -94,23 +101,23 @@ execute_search (GcalShellSearchProvider *self)
|
|||||||
self->pending_search->terms[0], self->pending_search->terms[0]);
|
self->pending_search->terms[0], self->pending_search->terms[0]);
|
||||||
for (i = 1; i < g_strv_length (self->pending_search->terms); i++)
|
for (i = 1; i < g_strv_length (self->pending_search->terms); i++)
|
||||||
{
|
{
|
||||||
gchar *complete_query;
|
g_autofree gchar *complete_query = NULL;
|
||||||
gchar *second_query = g_strdup_printf ("(or (contains? \"summary\" \"%s\") (contains? \"description\" \"%s\"))",
|
g_autofree gchar *second_query = NULL;
|
||||||
self->pending_search->terms[0], self->pending_search->terms[0]);
|
|
||||||
|
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);
|
complete_query = g_strdup_printf ("(and %s %s)", search_query, second_query);
|
||||||
|
|
||||||
g_free (second_query);
|
g_clear_pointer (&search_query, g_free);
|
||||||
g_free (search_query);
|
search_query = g_steal_pointer (&complete_query);
|
||||||
|
|
||||||
search_query = complete_query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gcal_manager_set_shell_search_query (self->manager, search_query);
|
gcal_manager_set_shell_search_query (self->manager, search_query);
|
||||||
g_free (search_query);
|
|
||||||
|
|
||||||
self->scheduled_search_id = 0;
|
self->scheduled_search_id = 0;
|
||||||
g_application_hold (g_application_get_default ());
|
g_application_hold (g_application_get_default ());
|
||||||
return FALSE;
|
|
||||||
|
GCAL_RETURN (FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -118,11 +125,13 @@ schedule_search (GcalShellSearchProvider *self,
|
|||||||
GDBusMethodInvocation *invocation,
|
GDBusMethodInvocation *invocation,
|
||||||
gchar **terms)
|
gchar **terms)
|
||||||
{
|
{
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
/* don't attempt searches for a single character */
|
/* don't attempt searches for a single character */
|
||||||
if (g_strv_length (terms) == 1 && g_utf8_strlen (terms[0], -1) == 1)
|
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));
|
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(as)", NULL));
|
||||||
return;
|
GCAL_RETURN ();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self->pending_search != NULL)
|
if (self->pending_search != NULL)
|
||||||
@@ -150,13 +159,19 @@ schedule_search (GcalShellSearchProvider *self,
|
|||||||
if (gcal_manager_get_loading (self->manager))
|
if (gcal_manager_get_loading (self->manager))
|
||||||
{
|
{
|
||||||
self->scheduled_search_id = g_timeout_add_seconds (1, (GSourceFunc) execute_search, self);
|
self->scheduled_search_id = g_timeout_add_seconds (1, (GSourceFunc) execute_search, self);
|
||||||
return;
|
GCAL_RETURN ();
|
||||||
}
|
}
|
||||||
|
|
||||||
execute_search (self);
|
execute_search (self);
|
||||||
return;
|
|
||||||
|
GCAL_EXIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Callbacks
|
||||||
|
*/
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
get_initial_result_set_cb (GcalShellSearchProvider *self,
|
get_initial_result_set_cb (GcalShellSearchProvider *self,
|
||||||
GDBusMethodInvocation *invocation,
|
GDBusMethodInvocation *invocation,
|
||||||
@@ -186,16 +201,18 @@ get_result_metas_cb (GcalShellSearchProvider *self,
|
|||||||
{
|
{
|
||||||
GDateTime *local_datetime;
|
GDateTime *local_datetime;
|
||||||
GVariantBuilder abuilder, builder;
|
GVariantBuilder abuilder, builder;
|
||||||
GVariant *icon_variant;
|
|
||||||
GcalEvent *event;
|
GcalEvent *event;
|
||||||
GdkPixbuf *gicon;
|
|
||||||
gchar *uuid, *desc;
|
gchar *uuid, *desc;
|
||||||
gchar *start_date;
|
gchar *start_date;
|
||||||
gint i;
|
gint i;
|
||||||
|
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
|
g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
|
||||||
for (i = 0; i < g_strv_length (results); i++)
|
for (i = 0; i < g_strv_length (results); i++)
|
||||||
{
|
{
|
||||||
|
g_autoptr (GVariant) icon_variant = NULL;
|
||||||
|
g_autoptr (GdkPixbuf) gicon = NULL;
|
||||||
cairo_surface_t *surface;
|
cairo_surface_t *surface;
|
||||||
|
|
||||||
uuid = results[i];
|
uuid = results[i];
|
||||||
@@ -209,8 +226,6 @@ get_result_metas_cb (GcalShellSearchProvider *self,
|
|||||||
gicon = gdk_pixbuf_get_from_surface (surface, 0, 0, 96, 96);
|
gicon = gdk_pixbuf_get_from_surface (surface, 0, 0, 96, 96);
|
||||||
icon_variant = g_icon_serialize (G_ICON (gicon));
|
icon_variant = g_icon_serialize (G_ICON (gicon));
|
||||||
g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
|
g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
|
||||||
g_object_unref (gicon);
|
|
||||||
g_variant_unref (icon_variant);
|
|
||||||
|
|
||||||
local_datetime = g_date_time_to_local (gcal_event_get_date_start (event));
|
local_datetime = g_date_time_to_local (gcal_event_get_date_start (event));
|
||||||
|
|
||||||
@@ -229,7 +244,7 @@ get_result_metas_cb (GcalShellSearchProvider *self,
|
|||||||
}
|
}
|
||||||
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));
|
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));
|
||||||
|
|
||||||
return TRUE;
|
GCAL_RETURN (TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -240,10 +255,12 @@ activate_result_cb (GcalShellSearchProvider *self,
|
|||||||
guint32 timestamp,
|
guint32 timestamp,
|
||||||
GcalShellSearchProvider2 *skel)
|
GcalShellSearchProvider2 *skel)
|
||||||
{
|
{
|
||||||
|
g_autoptr (GcalEvent) event = NULL;
|
||||||
GApplication *application;
|
GApplication *application;
|
||||||
GcalEvent *event;
|
|
||||||
GDateTime *dtstart;
|
GDateTime *dtstart;
|
||||||
|
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
application = g_application_get_default ();
|
application = g_application_get_default ();
|
||||||
|
|
||||||
event = gcal_manager_get_event_from_shell_search (self->manager, result);
|
event = gcal_manager_get_event_from_shell_search (self->manager, result);
|
||||||
@@ -254,9 +271,7 @@ activate_result_cb (GcalShellSearchProvider *self,
|
|||||||
|
|
||||||
g_application_activate (application);
|
g_application_activate (application);
|
||||||
|
|
||||||
g_clear_object (&event);
|
GCAL_RETURN (TRUE);
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -266,25 +281,24 @@ launch_search_cb (GcalShellSearchProvider *self,
|
|||||||
guint32 timestamp,
|
guint32 timestamp,
|
||||||
GcalShellSearchProvider2 *skel)
|
GcalShellSearchProvider2 *skel)
|
||||||
{
|
{
|
||||||
|
g_autofree gchar *terms_joined = NULL;
|
||||||
GApplication *application;
|
GApplication *application;
|
||||||
gchar *terms_joined;
|
GcalWindow *window;
|
||||||
GList *windows;
|
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
application = g_application_get_default ();
|
application = g_application_get_default ();
|
||||||
g_application_activate (application);
|
g_application_activate (application);
|
||||||
|
|
||||||
terms_joined = g_strjoinv (" ", terms);
|
terms_joined = g_strjoinv (" ", terms);
|
||||||
windows = g_list_reverse (gtk_application_get_windows (GTK_APPLICATION (application)));
|
window = (GcalWindow *) gtk_application_get_active_window (GTK_APPLICATION (application));
|
||||||
if (windows != NULL)
|
if (window)
|
||||||
{
|
{
|
||||||
gcal_window_set_search_mode (GCAL_WINDOW (windows->data), TRUE);
|
gcal_window_set_search_mode (window, TRUE);
|
||||||
gcal_window_set_search_query (GCAL_WINDOW (windows->data), terms_joined);
|
gcal_window_set_search_query (window, terms_joined);
|
||||||
|
|
||||||
g_list_free (windows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (terms_joined);
|
GCAL_RETURN (TRUE);
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -295,13 +309,15 @@ query_completed_cb (GcalShellSearchProvider *self,
|
|||||||
GVariantBuilder builder;
|
GVariantBuilder builder;
|
||||||
time_t current_time_t;
|
time_t current_time_t;
|
||||||
|
|
||||||
|
GCAL_ENTRY;
|
||||||
|
|
||||||
g_hash_table_remove_all (self->events);
|
g_hash_table_remove_all (self->events);
|
||||||
|
|
||||||
events = gcal_manager_get_shell_search_events (self->manager);
|
events = gcal_manager_get_shell_search_events (self->manager);
|
||||||
if (events == NULL)
|
if (events == NULL)
|
||||||
{
|
{
|
||||||
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", NULL));
|
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", NULL));
|
||||||
goto out;
|
GCAL_GOTO (out);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
|
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
|
||||||
@@ -326,11 +342,12 @@ query_completed_cb (GcalShellSearchProvider *self,
|
|||||||
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", &builder));
|
g_dbus_method_invocation_return_value (self->pending_search->invocation, g_variant_new ("(as)", &builder));
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_object_unref (self->pending_search->invocation);
|
g_clear_object (&self->pending_search->invocation);
|
||||||
g_strfreev (self->pending_search->terms);
|
g_clear_pointer (&self->pending_search->terms, g_strfreev);
|
||||||
g_clear_pointer (&(self->pending_search), g_free);
|
g_clear_pointer (&self->pending_search, g_free);
|
||||||
g_application_release (g_application_get_default ());
|
g_application_release (g_application_get_default ());
|
||||||
return FALSE;
|
|
||||||
|
GCAL_RETURN (FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -387,8 +404,7 @@ gcal_shell_search_provider_finalize (GObject *object)
|
|||||||
{
|
{
|
||||||
GcalShellSearchProvider *self = (GcalShellSearchProvider *) object;
|
GcalShellSearchProvider *self = (GcalShellSearchProvider *) object;
|
||||||
|
|
||||||
g_hash_table_destroy (self->events);
|
g_clear_pointer (&self->events, g_hash_table_destroy);
|
||||||
|
|
||||||
g_clear_object (&self->manager);
|
g_clear_object (&self->manager);
|
||||||
g_clear_object (&self->skel);
|
g_clear_object (&self->skel);
|
||||||
|
|
||||||
@@ -479,10 +495,10 @@ gcal_shell_search_provider_new (GcalManager *manager)
|
|||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
gcal_shell_search_provider_dbus_export (GcalShellSearchProvider *self,
|
gcal_shell_search_provider_dbus_export (GcalShellSearchProvider *self,
|
||||||
GDBusConnection *connection,
|
GDBusConnection *connection,
|
||||||
const gchar *object_path,
|
const gchar *object_path,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skel), connection, object_path, error);
|
return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skel), connection, object_path, error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user