search: Introduce a search engine

This is architectured in such a way that it will
allow us to introduce other searcheable terms in
the future.

All the functionality that in the past was in
GcalManager is now spread across GcalSearchModel,
GcalSearchHitEvent and GcalSearchEngine.
This commit is contained in:
Georges Basile Stavracas Neto
2019-04-29 17:08:53 -03:00
parent 6ecb7465eb
commit 6994723d79
14 changed files with 1140 additions and 62 deletions
+87 -1
View File
@@ -23,10 +23,16 @@
#include "gcal-context.h"
#include "gcal-search-button.h"
#include <math.h>
#define MIN_WIDTH 450
struct _GcalSearchButton
{
DzlSuggestionButton parent;
GCancellable *cancellable;
GcalContext *context;
};
@@ -46,6 +52,74 @@ static GParamSpec *properties [N_PROPS];
* Callbacks
*/
static void
position_suggestion_popover_func (DzlSuggestionEntry *entry,
GdkRectangle *area,
gboolean *is_absolute,
gpointer user_data)
{
gint new_width;
#define RIGHT_MARGIN 6
dzl_suggestion_entry_window_position_func (entry, area, is_absolute, NULL);
new_width = MAX (area->width * 2 / 5, MIN_WIDTH);
area->x += area->width - new_width;
area->width = new_width - RIGHT_MARGIN;
area->y -= 3;
#undef RIGHT_MARGIN
}
static void
on_search_finished_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
g_autoptr (GListModel) model = NULL;
g_autoptr (GError) error = NULL;
DzlSuggestionEntry *entry;
GcalSearchButton *self;
self = GCAL_SEARCH_BUTTON (user_data);
model = gcal_search_engine_search_finish (GCAL_SEARCH_ENGINE (source_object), result, &error);
entry = dzl_suggestion_button_get_entry (DZL_SUGGESTION_BUTTON (self));
dzl_suggestion_entry_set_model (entry, model);
}
static void
on_search_entry_changed_cb (GcalSearchButton *self)
{
g_autofree gchar *sexp_query = NULL;
DzlSuggestionEntry *entry;
GcalSearchEngine *search_engine;
const gchar *typed_text;
entry = dzl_suggestion_button_get_entry (DZL_SUGGESTION_BUTTON (self));
typed_text = dzl_suggestion_entry_get_typed_text (entry);
g_cancellable_cancel (self->cancellable);
if (dzl_str_empty0 (typed_text))
{
g_debug ("Cancelling search");
dzl_suggestion_entry_set_model (entry, NULL);
return;
}
sexp_query = g_strdup_printf ("(contains? \"summary\" \"%s\")", typed_text);
search_engine = gcal_context_get_search_engine (self->context);
gcal_search_engine_search (search_engine,
sexp_query,
50,
self->cancellable,
on_search_finished_cb,
g_object_ref (self));
}
static void
on_unfocus_action_activated_cb (GSimpleAction *action,
GVariant *param,
@@ -89,6 +163,8 @@ gcal_search_button_finalize (GObject *object)
{
GcalSearchButton *self = (GcalSearchButton *)object;
g_cancellable_cancel (self->cancellable);
g_clear_object (&self->cancellable);
g_clear_object (&self->context);
G_OBJECT_CLASS (gcal_search_button_parent_class)->finalize (object);
@@ -177,8 +253,18 @@ gcal_search_button_init (GcalSearchButton *self)
gtk_widget_insert_action_group (GTK_WIDGET (self), "search", G_ACTION_GROUP (group));
entry = dzl_suggestion_button_get_entry (DZL_SUGGESTION_BUTTON (self));
controller = dzl_shortcut_controller_find (GTK_WIDGET (entry));
g_signal_connect_object (entry,
"changed",
G_CALLBACK (on_search_entry_changed_cb),
self,
G_CONNECT_SWAPPED);
dzl_suggestion_entry_set_position_func (entry,
position_suggestion_popover_func,
self,
NULL);
controller = dzl_shortcut_controller_find (GTK_WIDGET (entry));
dzl_shortcut_controller_add_command_callback (controller,
"org.gnome.calendar.search",
"<Primary>f",