[epiphany] Fix webapp notification support



commit a72d21d758a3ac8c3dce8d4373941dfbf6f517aa
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Mon Jun 15 18:38:53 2020 +0200

    Fix webapp notification support
    
    Fixes: https://gitlab.gnome.org/GNOME/epiphany/-/issues/1228

 lib/ephy-profile-utils.h                     |   2 +-
 lib/ephy-web-app-utils.c                     |   4 +-
 src/ephy-shell.c                             |  15 ++-
 src/profile-migrator/ephy-profile-migrator.c | 140 +++++++++++++++++++++++++++
 tests/ephy-web-app-utils-test.c              |   2 +-
 5 files changed, 150 insertions(+), 13 deletions(-)
---
diff --git a/lib/ephy-profile-utils.h b/lib/ephy-profile-utils.h
index b01e842d2..c97910063 100644
--- a/lib/ephy-profile-utils.h
+++ b/lib/ephy-profile-utils.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-#define EPHY_PROFILE_MIGRATION_VERSION 34
+#define EPHY_PROFILE_MIGRATION_VERSION 35
 #define EPHY_INSECURE_PASSWORDS_MIGRATION_VERSION 11
 #define EPHY_FIREFOX_SYNC_PASSWORDS_MIGRATION_VERSION 19
 #define EPHY_TARGET_ORIGIN_MIGRATION_VERSION 21
diff --git a/lib/ephy-web-app-utils.c b/lib/ephy-web-app-utils.c
index 3d2c66ee5..eb8f98cca 100644
--- a/lib/ephy-web-app-utils.c
+++ b/lib/ephy-web-app-utils.c
@@ -40,14 +40,14 @@
  * <normalized-name>-checksum.
  *
  * The id is used to uniquely identify the app.
- *  - Program name: epiphany-<id>
+ *  - Program name: org.gnome.Epiphany.WebApp-<id>
  *  - Profile directory: <program-name>
  *  - Desktop file: <profile-dir>/<program-name>.desktop
  *
  * System web applications have a profile dir without a desktop file.
  */
 
-#define EPHY_WEB_APP_PROGRAM_NAME_PREFIX "epiphany-"
+#define EPHY_WEB_APP_PROGRAM_NAME_PREFIX "org.gnome.Epiphany.WebApp-"
 
 char *
 ephy_web_application_get_app_id_from_name (const char *name)
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index 3c5777b4b..421588cac 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -41,6 +41,7 @@
 #include "ephy-title-widget.h"
 #include "ephy-type-builtins.h"
 #include "ephy-user-agent.h"
+#include "ephy-web-app-utils.h"
 #include "ephy-web-view.h"
 #include "ephy-window.h"
 #include "prefs-dialog.h"
@@ -376,6 +377,7 @@ static GActionEntry app_mode_app_entries[] = {
   { "about", show_about, NULL, NULL, NULL },
   { "quit", quit_application, NULL, NULL, NULL },
   { "run-in-background", NULL, "b", "false", run_in_background_change_state},
+  { "notification-clicked", notification_clicked, "t", NULL, NULL},
 };
 
 static void
@@ -1211,22 +1213,17 @@ ephy_shell_get_prefs_dialog (EphyShell *shell)
 void
 _ephy_shell_create_instance (EphyEmbedShellMode mode)
 {
-  g_autofree gchar *id = NULL;
+  const char *id = NULL;
 
   g_assert (ephy_shell == NULL);
 
   if (mode == EPHY_EMBED_SHELL_MODE_APPLICATION) {
     const char *profile_dir = ephy_profile_dir ();
-    g_autofree char *basename = g_path_get_basename (profile_dir);
-    g_auto (GStrv) split = g_strsplit_set (basename, "-", -1);
-    guint len = g_strv_length (split);
+    const char *web_id = ephy_web_application_get_program_name_from_profile_directory (profile_dir);
 
-    if (len > 0)
-      id = g_strconcat (APPLICATION_ID, ".WebApp-", split[len - 1], NULL);
-    else
-      id = g_strconcat (APPLICATION_ID, ".WebApp", NULL);
+    id = web_id;
   } else {
-    id = g_strdup (APPLICATION_ID);
+    id = APPLICATION_ID;
   }
 
   ephy_shell = EPHY_SHELL (g_object_new (EPHY_TYPE_SHELL,
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 4586ef0f1..12512c0a9 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -1282,6 +1282,145 @@ migrate_adblock_to_shared_cache_dir (void)
     g_warning ("Cannot delete adblock directory %s: %s", directory, error->message);
 }
 
+static void
+migrate_webapp_names (void)
+{
+  /* Rename webapp folder, desktop file name and content from
+   *   epiphany-XXX
+   * to
+   *  org.gnome.Epiphany.WebApp-XXX
+   */
+  g_autoptr (GFile) parent_directory = NULL;
+  g_autoptr (GFileEnumerator) children = NULL;
+  g_autoptr (GFileInfo) info = NULL;
+  g_autoptr (GError) error = NULL;
+  const char *parent_directory_path = g_get_user_data_dir ();
+
+  parent_directory = g_file_new_for_path (parent_directory_path);
+  children = g_file_enumerate_children (parent_directory,
+                                        "standard::name",
+                                        0, NULL, NULL);
+  if (!children)
+    return;
+
+  info = g_file_enumerator_next_file (children, NULL, &error);
+  if (!info) {
+    g_warning ("Cannot enumerate profile directory: %s", error->message);
+    return;
+  }
+
+  while (info) {
+    const char *name;
+
+    name = g_file_info_get_name (info);
+    if (g_str_has_prefix (name, "epiphany-")) {
+      g_auto (GStrv) split = g_strsplit_set (name, "-", 2);
+      guint len = g_strv_length (split);
+
+      if (len == 2) {
+        g_autofree char *old_desktop_file_name = NULL;
+        g_autofree char *new_desktop_file_name = NULL;
+        g_autofree char *old_desktop_path_name = NULL;
+        g_autofree char *new_desktop_path_name = NULL;
+        g_autofree char *app_desktop_file_name = NULL;
+        g_autoptr (GFile) app_link_file = NULL;
+        g_autoptr (GFile) old_desktop_file = NULL;
+        g_autoptr (GFileInfo) file_info = NULL;
+        goffset file_size;
+        g_autofree char *new_name = g_strconcat ("org.gnome.Epiphany.WebApp-", split[1], NULL);
+
+        /* Rename directory */
+        old_desktop_path_name = g_strconcat (parent_directory_path, G_DIR_SEPARATOR_S, name, NULL);
+        new_desktop_path_name = g_strconcat (parent_directory_path, G_DIR_SEPARATOR_S, new_name, NULL);
+        if (g_rename (old_desktop_path_name, new_desktop_path_name) == -1) {
+          g_warning ("Cannot rename web application directroy from '%s' to '%s'", old_desktop_path_name, 
new_desktop_path_name);
+          goto next;
+        }
+
+        /* Create new desktop file */
+        old_desktop_file_name = g_strconcat (parent_directory_path, G_DIR_SEPARATOR_S, new_name, 
G_DIR_SEPARATOR_S, name, ".desktop", NULL);
+        new_desktop_file_name = g_strconcat (parent_directory_path, G_DIR_SEPARATOR_S, new_name, 
G_DIR_SEPARATOR_S, new_name, ".desktop", NULL);
+
+        /* Fix paths in desktop file */
+        old_desktop_file = g_file_new_for_path (old_desktop_file_name);
+        file_info = g_file_query_info (old_desktop_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 
G_FILE_QUERY_INFO_NONE, NULL, &error);
+        if (!file_info) {
+          g_warning ("Cannot query file info for '%s': %s", old_desktop_file_name, error->message);
+          goto next;
+        }
+
+        file_size = g_file_info_get_size (file_info);
+        if (file_size != 0) {
+          g_autofree unsigned char *data = NULL;
+          g_autoptr (GFileInputStream) input_stream = NULL;
+          g_autoptr (GFileOutputStream) output_stream = NULL;
+          g_autofree char *new_data = NULL;
+          g_autoptr (GFile) new_desktop_file = NULL;
+
+          input_stream = g_file_read (old_desktop_file, NULL, &error);
+          if (!input_stream) {
+            g_warning ("Cannot open old desktop file: %s", error->message);
+            goto next;
+          }
+
+          data = g_malloc0 (file_size);
+          if (!g_input_stream_read_all (G_INPUT_STREAM (input_stream), data, file_size, NULL, NULL, &error)) 
{
+            g_warning ("Cannot read old desktop file: %s", error->message);
+            goto next;
+          }
+
+          new_data = ephy_string_find_and_replace ((const char *)data, name, new_name);
+          new_desktop_file = g_file_new_for_path (new_desktop_file_name);
+
+          output_stream = g_file_create (new_desktop_file, G_FILE_CREATE_PRIVATE, NULL, &error);
+          if (!output_stream) {
+            g_warning ("Cannot create new desktop file: %s", error->message);
+            goto next;
+          }
+
+          if (g_output_stream_write (G_OUTPUT_STREAM (output_stream), new_data, strlen (new_data), NULL, 
&error) == -1) {
+            g_warning ("Error writing data to new desktop file: %s", error->message);
+            goto next;
+          }
+
+          if (!g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, &error)) {
+            g_warning ("Cannot close output stream of new desktop file: %s", error->message);
+            goto next;
+          }
+
+          if (!g_file_delete (old_desktop_file, NULL, &error)) {
+            g_warning ("Cannot delete old desktop file: %s", error->message);
+            goto next;
+          }
+        }
+
+        /* Remove old symlink */
+        app_desktop_file_name = g_strconcat (g_get_user_data_dir (), G_DIR_SEPARATOR_S, "applications", 
G_DIR_SEPARATOR_S, name, ".desktop", NULL);
+        if (g_remove (app_desktop_file_name) == -1) {
+          g_warning ("Cannot remove old desktop file symlink '%s'", app_desktop_file_name);
+          goto next;
+        }
+
+        /* Create new symlink */
+        app_desktop_file_name = g_strconcat (g_get_user_data_dir (), G_DIR_SEPARATOR_S, "applications", 
G_DIR_SEPARATOR_S, new_name, ".desktop", NULL);
+        app_link_file = g_file_new_for_path (app_desktop_file_name);
+
+        if (!g_file_make_symbolic_link (app_link_file, new_desktop_file_name, NULL, &error)) {
+          g_warning ("Cannot create symlink to new desktop file: %s", error->message);
+          goto next;
+        }
+      }
+    }
+
+next:
+    info = g_file_enumerator_next_file (children, NULL, &error);
+    if (!info && error) {
+      g_warning ("Cannot enumerate next file: %s", error->message);
+      return;
+    }
+  }
+}
+
 static void
 migrate_nothing (void)
 {
@@ -1331,6 +1470,7 @@ const EphyProfileMigrator migrators[] = {
   /* 32 */ migrate_webapps_harder,
   /* 33 */ migrate_adblock_to_content_filters,
   /* 34 */ migrate_adblock_to_shared_cache_dir,
+  /* 35 */ migrate_webapp_names,
 };
 
 static gboolean
diff --git a/tests/ephy-web-app-utils-test.c b/tests/ephy-web-app-utils-test.c
index 75b0642c9..5026388db 100644
--- a/tests/ephy-web-app-utils-test.c
+++ b/tests/ephy-web-app-utils-test.c
@@ -83,7 +83,7 @@ test_web_app_lifetime (void)
     g_free (exec);
     g_assert_null (g_key_file_get_string (key_file, "Desktop Entry", "Icon", NULL));
     wm_class = g_key_file_get_string (key_file, "Desktop Entry", "StartupWMClass", NULL);
-    program_name = g_strdup_printf ("epiphany-%s", id);
+    program_name = g_strdup_printf ("org.gnome.Epiphany.WebApp-%s", id);
     g_assert_cmpstr (wm_class, ==, program_name);
     g_free (program_name);
     g_free (wm_class);


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