[epiphany/mcatanzaro/webapp-profile: 19/19] Add migrator to fix 3.32.0 webapps created from non-default profile dirs



commit a86e74ca15fe137bd9c2dc8c2e7ae1cdab81829e
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sun Mar 17 12:30:18 2019 -0500

    Add migrator to fix 3.32.0 webapps created from non-default profile dirs
    
    Web apps were only created in the right place when created from a
    browser instance using the correct profile directory. Now that this has
    been fixed, we should migrate them to the right place.
    
    Probably very few users will ever be affected by this, because most
    users probably create web apps from the default browser instance. But
    Carlos noticed right away, so it's worth having a migrator.

 lib/ephy-profile-utils.h                     |  2 +-
 src/profile-migrator/ephy-profile-migrator.c | 84 ++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 1 deletion(-)
---
diff --git a/lib/ephy-profile-utils.h b/lib/ephy-profile-utils.h
index ce77b338b..f64f324f5 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 31
+#define EPHY_PROFILE_MIGRATION_VERSION 32
 #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/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 2c28b6763..5ba3fe24e 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -1237,6 +1237,89 @@ migrate_web_extension_config_dir (void)
   ephy_file_delete_dir_recursively (path, NULL);
 }
 
+static void
+migrate_webapps_harder (void)
+{
+  /* We created some webapps in the default profile directory by mistake.
+   * Whoopsie!
+   */
+  g_autoptr(GFileEnumerator) children = NULL;
+  g_autoptr(GFileInfo) info;
+  g_autofree char *parent_directory_path = NULL;
+  g_autoptr(GFile) parent_directory = NULL;
+
+  parent_directory_path = g_build_filename (g_get_user_data_dir (), "epiphany", NULL);
+
+  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, NULL);
+  while (info) {
+    const char *name = g_file_info_get_name (info);
+
+    if (g_str_has_prefix (name, "epiphany-")) {
+      g_autofree char *incorrect_profile_dir = g_build_filename (parent_directory_path, name, NULL);
+      g_autofree char *correct_profile_dir = g_build_filename (g_get_user_data_dir (), name, NULL);
+      g_autofree char *app_file = g_build_filename (incorrect_profile_dir, ".app", NULL);
+
+      if (g_file_test (app_file, G_FILE_TEST_EXISTS)) {
+        g_autoptr(GKeyFile) file = g_key_file_new ();
+        g_autofree char *desktop_file_name = g_strconcat (name, ".desktop", NULL);
+        g_autofree char *desktop_file_path = g_build_filename (correct_profile_dir, desktop_file_name, NULL);
+        g_autoptr(GError) error = NULL;
+
+        move_directory_contents (incorrect_profile_dir, correct_profile_dir);
+
+        // Update Exec and Icon to point to the new profile dir
+        g_key_file_load_from_file (file, desktop_file_path, G_KEY_FILE_NONE, NULL);
+
+        g_autofree char *exec = g_key_file_get_string (file, G_KEY_FILE_DESKTOP_GROUP, 
G_KEY_FILE_DESKTOP_KEY_EXEC, &error);
+        if (exec == NULL) {
+          g_warning ("Failed to get Exec key from %s: %s", desktop_file_path, error->message);
+          continue;
+        }
+        g_autofree char *new_exec = ephy_string_find_and_replace (exec, incorrect_profile_dir, 
correct_profile_dir);
+        LOG ("migrate_profile_directories: setting Exec to %s", new_exec);
+        g_key_file_set_string (file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, new_exec);
+
+        g_autofree char *icon = g_key_file_get_string (file, G_KEY_FILE_DESKTOP_GROUP, 
G_KEY_FILE_DESKTOP_KEY_ICON, &error);
+        if (exec == NULL) {
+          g_warning ("Failed to get Icon key from %s: %s", desktop_file_path, error->message);
+          continue;
+        }
+        g_autofree char *new_icon = ephy_string_find_and_replace (icon, incorrect_profile_dir, 
correct_profile_dir);
+        LOG ("migrate_profile_directories: setting Icon to %s", new_icon);
+        g_key_file_set_string (file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, new_icon);
+
+        if (!g_key_file_save_to_file (file, desktop_file_path, &error)) {
+          g_warning ("Failed to save desktop file %s", error->message);
+          g_clear_error (&error);
+        }
+
+        g_autofree char *desktop_symlink_path = g_build_filename (g_get_user_data_dir (), "applications", 
desktop_file_name, NULL);
+        g_autoptr(GFile) desktop_symlink = g_file_new_for_path (desktop_symlink_path);
+        LOG ("Symlinking %s to %s", desktop_symlink_path, desktop_file_path);
+
+        // Try removing old symlink, failure is ok assuming it doesn't exist.
+        if (!g_file_delete (desktop_symlink, NULL, &error)) {
+          g_warning ("Failed to remove old symbolic link: %s", error->message);
+          g_clear_error (&error);
+        }
+
+        if (!g_file_make_symbolic_link (desktop_symlink, desktop_file_path, NULL, &error))
+          g_warning ("Failed to make symbolic link: %s", error->message);
+      }
+    }
+
+    g_clear_object (&info);
+    info = g_file_enumerator_next_file (children, NULL, NULL);
+  }
+}
+
 static void
 migrate_nothing (void)
 {
@@ -1282,6 +1365,7 @@ const EphyProfileMigrator migrators[] = {
   /* 29 */ migrate_zoom_level,
   /* 30 */ migrate_profile_directories,
   /* 31 */ migrate_web_extension_config_dir,
+  /* 32 */ migrate_webapps_harder,
 };
 
 static gboolean


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