From 2e7c37ddaa8deb515470ca6eed433130b6e4825d Mon Sep 17 00:00:00 2001 From: Michael Catanzaro Date: Sun, 27 Aug 2017 21:40:12 -0500 Subject: Implement DzlSuggestion vfuncs --- src/ephy-suggestion-model.c | 37 +++++------------- src/ephy-suggestion-model.h | 2 +- src/ephy-suggestion.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ src/ephy-suggestion.h | 33 ++++++++++++++++ src/meson.build | 1 + 5 files changed, 137 insertions(+), 28 deletions(-) create mode 100644 src/ephy-suggestion.c create mode 100644 src/ephy-suggestion.h diff --git a/src/ephy-suggestion-model.c b/src/ephy-suggestion-model.c index a843ef5ff..023b05cfa 100644 --- a/src/ephy-suggestion-model.c +++ b/src/ephy-suggestion-model.c @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* - * Copyright (C) 2017 Christian Hergert + * Copyright © 2017 Christian Hergert * * 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 @@ -19,6 +19,8 @@ #include "config.h" #include "ephy-suggestion-model.h" +#include "ephy-suggestion.h" + #include #include @@ -135,7 +137,7 @@ ephy_suggestion_model_init (EphySuggestionModel *self) static GType ephy_suggestion_model_get_item_type (GListModel *model) { - return DZL_TYPE_SUGGESTION; + return EPHY_TYPE_SUGGESTION; } static guint @@ -324,20 +326,11 @@ query_completed_cb (EphyHistoryService *service, title = ephy_bookmark_get_title (bookmark); if (should_add_bookmark_to_model (self, query, title, url)) { - DzlSuggestion *suggestion; - char *escaped_url = g_markup_escape_text (url, -1); - char *escaped_title = g_markup_escape_text (title, -1); - - suggestion = g_object_new (DZL_TYPE_SUGGESTION, - "title", escaped_url, - "subtitle", escaped_title, - "id", url, - NULL); + EphySuggestion *suggestion; + + suggestion = ephy_suggestion_new (title, url); g_sequence_append (self->items, suggestion); added++; - - g_free (escaped_url); - g_free (escaped_title); } } @@ -346,21 +339,11 @@ query_completed_cb (EphyHistoryService *service, for (const GList *p = g_list_last (urls); p != NULL; p = p->prev) { EphyHistoryURL *url = (EphyHistoryURL *)p->data; - DzlSuggestion *suggestion; - char *escaped_url = g_markup_escape_text (url->url, -1); - char *escaped_title = g_markup_escape_text (url->title, -1); - - suggestion = g_object_new (DZL_TYPE_SUGGESTION, - "icon-name", "web-browser-symbolic", - "id", url->url, - "title", escaped_url, - "subtitle", escaped_title, - NULL); + EphySuggestion *suggestion; + + suggestion = ephy_suggestion_new (url->title, url->url); g_sequence_prepend (self->items, suggestion); added++; - - g_free (escaped_url); - g_free (escaped_title); } g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added); diff --git a/src/ephy-suggestion-model.h b/src/ephy-suggestion-model.h index 613e9aab9..625789076 100644 --- a/src/ephy-suggestion-model.h +++ b/src/ephy-suggestion-model.h @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* - * Copyright (C) 2017 Christian Hergert + * Copyright © 2017 Christian Hergert * * 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 diff --git a/src/ephy-suggestion.c b/src/ephy-suggestion.c new file mode 100644 index 000000000..66dc5a5d0 --- /dev/null +++ b/src/ephy-suggestion.c @@ -0,0 +1,92 @@ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * Copyright © 2017 Igalia S.L. + * + * 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 . + */ + +#include "config.h" +#include "ephy-suggestion.h" + +#include +#include +#include + +struct _EphySuggestion { + DzlSuggestion parent; +}; + +G_DEFINE_TYPE (EphySuggestion, ephy_suggestion, DZL_TYPE_SUGGESTION) + +char * +ephy_suggestion_suggest_suffix (DzlSuggestion *self, + const char *typed_text) +{ + const char *suffix; + const char *url; + + if (typed_text == NULL || strlen (typed_text) == 0 || + g_str_has_prefix ("www.", typed_text) || + g_str_has_prefix ("http://www.", typed_text) || + g_str_has_prefix ("https://www.", typed_text)) + return NULL; + + url = dzl_suggestion_get_id (self); + suffix = strstr (url, typed_text); + + return suffix ? g_strdup (&suffix[strlen (typed_text)]) : NULL; +} + +char * +ephy_suggestion_replace_typed_text (DzlSuggestion *self, + const char *typed_text) +{ + const char *url = dzl_suggestion_get_id (self); + + return g_strdup (url); +} + +static void +ephy_suggestion_class_init (EphySuggestionClass *klass) +{ + DzlSuggestionClass *dzl_suggestion_class = DZL_SUGGESTION_CLASS (klass); + + dzl_suggestion_class->suggest_suffix = ephy_suggestion_suggest_suffix; + dzl_suggestion_class->replace_typed_text = ephy_suggestion_replace_typed_text; +} + +static void +ephy_suggestion_init (EphySuggestion *self) +{ +} + +EphySuggestion * +ephy_suggestion_new (const char *title, + const char *uri) +{ + EphySuggestion *suggestion; + char *escaped_title = g_markup_escape_text (title, -1); + char *escaped_uri = g_markup_escape_text (uri, -1); + + suggestion = g_object_new (EPHY_TYPE_SUGGESTION, + "icon-name", "web-browser-symbolic", + "id", uri, + "title", escaped_title, + "subtitle", escaped_uri, + NULL); + g_free (escaped_title); + g_free (escaped_uri); + + return suggestion; +} diff --git a/src/ephy-suggestion.h b/src/ephy-suggestion.h new file mode 100644 index 000000000..2cbdd319c --- /dev/null +++ b/src/ephy-suggestion.h @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * Copyright © 2017 Igalia S.L. + * + * 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 . + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +#define EPHY_TYPE_SUGGESTION (ephy_suggestion_get_type()) + +G_DECLARE_FINAL_TYPE (EphySuggestion, ephy_suggestion, EPHY, SUGGESTION, DzlSuggestion) + +// FIXME: How about favicon? +EphySuggestion *ephy_suggestion_new (const char *title, + const char *uri); + +G_END_DECLS diff --git a/src/meson.build b/src/meson.build index b6e219718..ba46af732 100644 --- a/src/meson.build +++ b/src/meson.build @@ -35,6 +35,7 @@ libephymain_sources = [ 'ephy-search-engine-dialog.c', 'ephy-session.c', 'ephy-shell.c', + 'ephy-suggestion.c', 'ephy-suggestion-model.c', 'ephy-window.c', 'passwords-dialog.c', -- cgit v1.2.1