[epiphany] Mark addresses currently open in browser



commit e962f8ec4273ab62d30be0045d40a48fd2e4b918
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Fri May 3 22:21:05 2019 +0200

    Mark addresses currently open in browser
    
    Fixes: https://gitlab.gnome.org/GNOME/epiphany/issues/153

 lib/ephy-suggestion.c             | 24 ++++++++++++
 lib/ephy-suggestion.h             | 26 +++++++------
 lib/widgets/ephy-location-entry.c | 61 +++++++++++++++++++++----------
 src/ephy-location-controller.c    | 17 +++++++++
 src/ephy-suggestion-model.c       | 77 +++++++++++++++++++++++++++++++++++++--
 5 files changed, 171 insertions(+), 34 deletions(-)
---
diff --git a/lib/ephy-suggestion.c b/lib/ephy-suggestion.c
index 317485b21..fc4c04ed2 100644
--- a/lib/ephy-suggestion.c
+++ b/lib/ephy-suggestion.c
@@ -149,6 +149,30 @@ ephy_suggestion_new (const char *title_markup,
   return suggestion;
 }
 
+EphySuggestion *
+ephy_suggestion_new_with_custom_subtitle (const char *title_markup,
+                                          const char *unescaped_title,
+                                          const char *subtitle,
+                                          const char *uri)
+{
+  EphySuggestion *suggestion;
+  char *decoded_uri = ephy_uri_decode (uri);
+  char *escaped_uri = g_markup_escape_text (decoded_uri, -1);
+
+  suggestion = g_object_new (EPHY_TYPE_SUGGESTION,
+                             "icon-name", "web-browser-symbolic",
+                             "id", uri,
+                             "subtitle", subtitle,
+                             "title", title_markup,
+                             "unescaped-title", unescaped_title,
+                             NULL);
+
+  g_free (decoded_uri);
+  g_free (escaped_uri);
+
+  return suggestion;
+}
+
 EphySuggestion *
 ephy_suggestion_new_without_subtitle (const char *title_markup,
                                       const char *unescaped_title,
diff --git a/lib/ephy-suggestion.h b/lib/ephy-suggestion.h
index 3994f2340..85bf8a28b 100644
--- a/lib/ephy-suggestion.h
+++ b/lib/ephy-suggestion.h
@@ -27,16 +27,20 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (EphySuggestion, ephy_suggestion, EPHY, SUGGESTION, DzlSuggestion)
 
-EphySuggestion *ephy_suggestion_new                  (const char *title_markup,
-                                                      const char *unescaped_title,
-                                                      const char *uri);
-EphySuggestion *ephy_suggestion_new_without_subtitle (const char *title_markup,
-                                                      const char *unescaped_title,
-                                                      const char *uri);
-const char     *ephy_suggestion_get_unescaped_title  (EphySuggestion *self);
-const char     *ephy_suggestion_get_uri              (EphySuggestion *self);
-
-void            ephy_suggestion_set_favicon          (EphySuggestion  *self,
-                                                      cairo_surface_t *favicon);
+EphySuggestion *ephy_suggestion_new                      (const char *title_markup,
+                                                          const char *unescaped_title,
+                                                          const char *uri);
+EphySuggestion *ephy_suggestion_new_with_custom_subtitle (const char *title_markup,
+                                                          const char *unescaped_title,
+                                                          const char *subtitle,
+                                                          const char *uri);
+EphySuggestion *ephy_suggestion_new_without_subtitle     (const char *title_markup,
+                                                          const char *unescaped_title,
+                                                          const char *uri);
+const char     *ephy_suggestion_get_unescaped_title      (EphySuggestion *self);
+const char     *ephy_suggestion_get_uri                  (EphySuggestion *self);
+
+void            ephy_suggestion_set_favicon              (EphySuggestion  *self,
+                                                          cairo_surface_t *favicon);
 
 G_END_DECLS
diff --git a/lib/widgets/ephy-location-entry.c b/lib/widgets/ephy-location-entry.c
index 93f5552c1..1d5c4f772 100644
--- a/lib/widgets/ephy-location-entry.c
+++ b/lib/widgets/ephy-location-entry.c
@@ -69,6 +69,7 @@ struct _EphyLocationEntry {
   gboolean reader_mode_active;
 
   char *saved_text;
+  char *jump_tab;
 
   guint hash;
 
@@ -297,6 +298,7 @@ ephy_location_entry_finalize (GObject *object)
   EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (object);
 
   g_free (entry->saved_text);
+  g_clear_pointer (&entry->jump_tab, g_free);
 
   G_OBJECT_CLASS (ephy_location_entry_parent_class)->finalize (object);
 }
@@ -501,35 +503,44 @@ entry_key_press_cb (GtkEntry          *entry,
   if (event->keyval == GDK_KEY_Return ||
       event->keyval == GDK_KEY_KP_Enter ||
       event->keyval == GDK_KEY_ISO_Enter) {
-    g_autofree gchar *text = g_strdup (gtk_entry_get_text (GTK_ENTRY (location_entry->url_entry)));
-    gchar *url = g_strstrip (text);
-    g_autofree gchar *new_url = NULL;
-
-    if (strlen (url) > 5 && g_str_has_prefix (url, "http:") && url[5] != '/')
-      new_url = g_strdup_printf ("http://%s";, url + 5);
-    else if (strlen (url) > 6 && g_str_has_prefix (url, "https:") && url[6] != '/')
-      new_url = g_strdup_printf ("https://%s";, url + 6);
-
-    if (new_url) {
+    if (location_entry->jump_tab) {
       g_signal_handlers_block_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
-      gtk_entry_set_text (GTK_ENTRY (location_entry->url_entry), new_url);
+      gtk_entry_set_text (GTK_ENTRY (location_entry->url_entry), location_entry->jump_tab);
       g_signal_handlers_unblock_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
-    }
-
-    if (state == GDK_CONTROL_MASK) {
+      g_clear_pointer (&location_entry->jump_tab, g_free);
+    } else {
       g_autofree gchar *text = g_strdup (gtk_entry_get_text (GTK_ENTRY (location_entry->url_entry)));
       gchar *url = g_strstrip (text);
+      g_autofree gchar *new_url = NULL;
 
-      /* Remove control mask to prevent opening address in a new window */
-      event->state &= ~GDK_CONTROL_MASK;
+      gtk_entry_set_text (GTK_ENTRY (entry), location_entry->jump_tab ? location_entry->jump_tab : text);
 
-      if (!g_utf8_strchr (url, -1, ' ') && !g_utf8_strchr (url, -1, '.')) {
-        g_autofree gchar *new_url = g_strdup_printf ("www.%s.com", url);
+      if (strlen (url) > 5 && g_str_has_prefix (url, "http:") && url[5] != '/')
+        new_url = g_strdup_printf ("http://%s";, url + 5);
+      else if (strlen (url) > 6 && g_str_has_prefix (url, "https:") && url[6] != '/')
+        new_url = g_strdup_printf ("https://%s";, url + 6);
 
+      if (new_url) {
         g_signal_handlers_block_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
         gtk_entry_set_text (GTK_ENTRY (location_entry->url_entry), new_url);
         g_signal_handlers_unblock_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
       }
+
+      if (state == GDK_CONTROL_MASK) {
+        g_autofree gchar *text = g_strdup (gtk_entry_get_text (GTK_ENTRY (location_entry->url_entry)));
+        gchar *url = g_strstrip (text);
+
+        /* Remove control mask to prevent opening address in a new window */
+        event->state &= ~GDK_CONTROL_MASK;
+
+        if (!g_utf8_strchr (url, -1, ' ') && !g_utf8_strchr (url, -1, '.')) {
+          g_autofree gchar *new_url = g_strdup_printf ("www.%s.com", url);
+
+          g_signal_handlers_block_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
+          gtk_entry_set_text (GTK_ENTRY (location_entry->url_entry), new_url);
+          g_signal_handlers_unblock_by_func (location_entry->url_entry, G_CALLBACK (editable_changed_cb), 
location_entry);
+        }
+      }
     }
 
     ephy_location_entry_activate (location_entry);
@@ -773,8 +784,11 @@ ephy_location_entry_suggestion_activated (DzlSuggestionEntry *entry,
 {
   EphyLocationEntry *lentry = EPHY_LOCATION_ENTRY (user_data);
   DzlSuggestion *suggestion = dzl_suggestion_entry_get_suggestion (entry);
+  const gchar *text = ephy_suggestion_get_uri (EPHY_SUGGESTION (suggestion));
+
   g_signal_handlers_block_by_func (entry, G_CALLBACK (editable_changed_cb), user_data);
-  gtk_entry_set_text (GTK_ENTRY (entry), ephy_suggestion_get_uri (EPHY_SUGGESTION (suggestion)));
+  gtk_entry_set_text (GTK_ENTRY (entry), lentry->jump_tab ? lentry->jump_tab : text);
+  g_clear_pointer (&lentry->jump_tab, g_free);
   g_signal_handlers_unblock_by_func (entry, G_CALLBACK (editable_changed_cb), user_data);
 
   g_signal_stop_emission_by_name (entry, "suggestion-activated");
@@ -794,7 +808,14 @@ suggestion_selected (DzlSuggestionEntry *entry,
   const gchar *uri = dzl_suggestion_get_id (suggestion);
 
   g_signal_handlers_block_by_func (entry, G_CALLBACK (editable_changed_cb), user_data);
-  gtk_entry_set_text (GTK_ENTRY (entry), uri);
+  g_clear_pointer (&lentry->jump_tab, g_free);
+
+  if (g_str_has_prefix (uri, "ephy-tab://")) {
+    lentry->jump_tab = g_strdup (uri);
+    gtk_entry_set_text (GTK_ENTRY (entry), dzl_suggestion_get_subtitle (suggestion));
+  } else {
+    gtk_entry_set_text (GTK_ENTRY (entry), uri);
+  }
   gtk_editable_set_position (GTK_EDITABLE (entry), -1);
   g_signal_handlers_unblock_by_func (entry, G_CALLBACK (editable_changed_cb), user_data);
 
diff --git a/src/ephy-location-controller.c b/src/ephy-location-controller.c
index c63a959ab..25284a803 100644
--- a/src/ephy-location-controller.c
+++ b/src/ephy-location-controller.c
@@ -146,6 +146,23 @@ entry_activate_cb (GtkEntry               *entry,
   if (content == NULL || content[0] == '\0')
     return;
 
+  if (g_str_has_prefix (content, "ephy-tab://")) {
+    GtkWidget *notebook = ephy_window_get_notebook (controller->window);
+    GtkWidget *tab;
+    EphyWebView *webview;
+    gint current = gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook));
+    guint64 page = g_ascii_strtoull (content + strlen ("ephy-tab://"), NULL, 0);
+
+    tab = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), current);
+    gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page);
+    webview = ephy_embed_get_web_view (EPHY_EMBED (tab));
+
+    if (ephy_web_view_is_overview (webview))
+      g_signal_emit_by_name (GTK_NOTEBOOK (notebook), "tab-close-request", tab);
+
+    return;
+  }
+
   address = g_strdup (content);
   effective_address = ephy_embed_utils_normalize_or_autosearch_address (g_strstrip (address));
   g_free (address);
diff --git a/src/ephy-suggestion-model.c b/src/ephy-suggestion-model.c
index 799ba0fd5..3a59b505d 100644
--- a/src/ephy-suggestion-model.c
+++ b/src/ephy-suggestion-model.c
@@ -22,6 +22,7 @@
 #include "ephy-embed-shell.h"
 #include "ephy-search-engine-manager.h"
 #include "ephy-suggestion.h"
+#include "ephy-window.h"
 
 #include <dazzle.h>
 #include <glib/gi18n.h>
@@ -192,7 +193,11 @@ should_add_bookmark_to_model (EphySuggestionModel *self,
                               const char          *title,
                               const char          *location)
 {
-  return strstr (title, search_string) || strstr (location, search_string);
+  g_autofree gchar *search_casefold = g_utf8_casefold (search_string, -1);
+  g_autofree gchar *title_casefold = g_utf8_casefold (title, -1);
+  g_autofree gchar *location_casefold = g_utf8_casefold (location, -1);
+
+  return strstr (title_casefold, search_casefold) || strstr (location_casefold, search_casefold);
 }
 
 static void
@@ -347,6 +352,67 @@ add_search_engines (EphySuggestionModel *self,
   return added;
 }
 
+static guint
+add_tabs (EphySuggestionModel *self,
+          const char          *query)
+{
+  GApplication *application;
+  EphyEmbedShell *shell;
+  EphyWindow *window ;
+  GtkWidget *notebook;
+  gint n_pages;
+  gint current;
+  guint added = 0;
+
+  shell = ephy_embed_shell_get_default ();
+  application = G_APPLICATION (shell);
+  window = EPHY_WINDOW (gtk_application_get_active_window (GTK_APPLICATION (application)));
+
+  notebook = ephy_window_get_notebook (window);
+  n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
+  current = gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook));
+
+  for (int i = 0; i < n_pages; i++) {
+    EphyEmbed *embed;
+    EphyWebView *webview;
+    EphySuggestion *suggestion;
+    g_autofree gchar *escaped_title = NULL;
+    g_autofree gchar *markup = NULL;
+    const gchar *display_address;
+    g_autofree gchar *address = NULL;
+    const gchar *title;
+    g_autofree gchar *title_casefold = NULL;
+    g_autofree gchar *display_address_casefold = NULL;
+    g_autofree gchar *query_casefold = NULL;
+
+    if (i == current)
+      continue;
+
+    embed = EPHY_EMBED (gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), i));
+    webview = ephy_embed_get_web_view (embed);
+    display_address = ephy_web_view_get_display_address (webview);
+    address = g_strdup_printf ("ephy-tab://%d", i);
+    title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (webview));
+
+    display_address_casefold = g_utf8_casefold (display_address, -1);
+    query_casefold = g_utf8_casefold (query, -1);
+    if (title)
+      title_casefold = g_utf8_casefold (title, -1);
+
+    if ((title_casefold && strstr (title_casefold, query_casefold)) || strstr (display_address_casefold, 
query_casefold)) {
+      escaped_title = g_markup_escape_text (title, -1);
+      markup = dzl_fuzzy_highlight (escaped_title, query, FALSE);
+      suggestion = ephy_suggestion_new_with_custom_subtitle (markup, title, _("Switch to Tab"), address);
+      load_favicon (self, suggestion, display_address);
+
+      g_sequence_append (self->items, suggestion);
+      added++;
+    }
+  }
+
+  return added;
+}
+
 static void
 query_completed_cb (EphyHistoryService *service,
                     gboolean            success,
@@ -375,7 +441,8 @@ query_completed_cb (EphyHistoryService *service,
   self->items = g_sequence_new (g_object_unref);
 
   if (strlen (query) > 0) {
-    added = add_bookmarks (self, query);
+    added = add_tabs (self, query);
+    added += add_bookmarks (self, query);
     added += add_history (self, urls, query);
     added += add_search_engines (self, query);
   }
@@ -438,6 +505,7 @@ ephy_suggestion_model_get_suggestion_with_uri (EphySuggestionModel *self,
                                                const char          *uri)
 {
   GSequenceIter *iter;
+  g_autofree gchar *uri_casefold = g_utf8_casefold (uri, -1);
 
   g_assert (EPHY_IS_SUGGESTION_MODEL (self));
   g_assert (uri != NULL && *uri != '\0');
@@ -445,9 +513,12 @@ ephy_suggestion_model_get_suggestion_with_uri (EphySuggestionModel *self,
   for (iter = g_sequence_get_begin_iter (self->items);
        !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter)) {
     EphySuggestion *suggestion;
+    g_autofree gchar *suggestion_casefold = NULL;
 
     suggestion = g_sequence_get (iter);
-    if (strcmp (ephy_suggestion_get_uri (suggestion), uri) == 0)
+    suggestion_casefold = g_utf8_casefold (ephy_suggestion_get_uri (suggestion), -1);
+
+    if (strcmp (suggestion_casefold, uri_casefold) == 0)
       return suggestion;
   }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]