diff --git a/eos-migrate-chrome-profile b/eos-migrate-chrome-profile index 5886bcd..6d97298 100755 --- a/eos-migrate-chrome-profile +++ b/eos-migrate-chrome-profile @@ -29,6 +29,7 @@ from gi.repository import GLib USER_HOME_DIR = os.path.expanduser("~/") +DESKTOP_SHORTCUTS_DIR = os.path.join(USER_HOME_DIR, ".local", "share", "applications") OLD_CHROME_CONFIG_DIR = os.path.join(USER_HOME_DIR, ".config", "google-chrome") NEW_CHROME_DATA_DIR = os.path.join(USER_HOME_DIR, ".var", "app", "com.google.Chrome") NEW_CHROME_CONFIG_DIR = os.path.join(NEW_CHROME_DATA_DIR, "config", "google-chrome") @@ -89,6 +90,43 @@ def update_mimeapps_list(path): keyfile.save_to_file(path) +def update_desktop_shortcut(path): + keyfile = GLib.KeyFile() + keyfile.load_from_file(path, + GLib.KeyFileFlags.KEEP_COMMENTS | + GLib.KeyFileFlags.KEEP_TRANSLATIONS) + + groups, _ = keyfile.get_groups() + for group in groups: + try: + exec_cmd = keyfile.get_string(group, "Exec") + except GLib.GError as error: + if not error.matches(GLib.key_file_error_quark(), + GLib.KeyFileError.KEY_NOT_FOUND): + raise error + continue + + if not exec_cmd.startswith("/usr/bin/eos-google-chrome"): + continue + + keyfile.set_string(group, "Exec", + "/usr/bin/flatpak run com.google.Chrome" + + exec_cmd[len("/usr/bin/eos-google-chrome"):]) + + keyfile.save_to_file(path) + + +def update_desktop_shortcuts(path=DESKTOP_SHORTCUTS_DIR): + try: + shortcuts = os.listdir(path) + except FileNotFoundError: + return + + for filename in shortcuts: + if filename.endswith(".desktop"): + update_desktop_shortcut(os.path.join(path, filename)) + + def update_old_config_references(path): if not os.path.isfile(path): return @@ -107,6 +145,7 @@ def main(): sys.exit(1) update_mimeapps_list(MIMEAPPS_LIST) + update_desktop_shortcuts() if ( os.path.isdir(OLD_CHROME_CONFIG_DIR) and diff --git a/tests/test_migrate_chrome_profile.py b/tests/test_migrate_chrome_profile.py index 1a977e3..ee6b447 100644 --- a/tests/test_migrate_chrome_profile.py +++ b/tests/test_migrate_chrome_profile.py @@ -91,6 +91,126 @@ def _test(self, orig_data, expected_data): self.assertEqual(expected_data, new_data) +class TestUpdateDesktopShortcuts(BaseTestCase): + def setUp(self): + super().setUp() + + self.tmp = tempfile.TemporaryDirectory() + + def tearDown(self): + self.tmp.cleanup() + + def test_no_shortcuts_dir(self): + emfp.update_desktop_shortcuts(os.path.join(self.tmp.name, "no-such-directory")) + + def test_empty_shortcuts_dir(self): + emfp.update_desktop_shortcuts(self.tmp.name) + + def test_not_there(self): + orig_data = textwrap.dedent( + """ + [Desktop Entry] + Exec=/usr/bin/chromium-browser --profile-directory=Default + """ + ).lstrip() + expected_data = orig_data + + self._test(orig_data, expected_data) + + def test_there(self): + orig_data = textwrap.dedent( + """ + #!/usr/bin/env xdg-open + [Desktop Entry] + Version=1.0 + Terminal=false + Type=Application + Name=YouTube + Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" + Icon=chrome-agimnkijcaahngcdmfeangaknmldooml-Profile_1 + StartupWMClass=crx_agimnkijcaahngcdmfeangaknmldooml + Actions=Explore;Subscriptions + + [Desktop Action Explore] + Name=Explore + Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" + + [Desktop Action Subscriptions] + Name=Subscriptions + Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" + """ + ).lstrip() + expected_data = textwrap.dedent( + """ + #!/usr/bin/env xdg-open + [Desktop Entry] + Version=1.0 + Terminal=false + Type=Application + Name=YouTube + Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1" + Icon=chrome-agimnkijcaahngcdmfeangaknmldooml-Profile_1 + StartupWMClass=crx_agimnkijcaahngcdmfeangaknmldooml + Actions=Explore;Subscriptions + + [Desktop Action Explore] + Name=Explore + Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1" + + [Desktop Action Subscriptions] + Name=Subscriptions + Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1" + """ + ).lstrip() + + self._test(orig_data, expected_data) + + def test_multi_languages(self): + orig_data = textwrap.dedent( + """ + # Comment + [Desktop Entry] + Name=Chrome + Name[en_GB]=Chrome, milord + Exec=/usr/bin/eos-google-chrome --profile-directory=Default + """ + ).lstrip() + expected_data = textwrap.dedent( + """ + # Comment + [Desktop Entry] + Name=Chrome + Name[en_GB]=Chrome, milord + Exec=/usr/bin/flatpak run com.google.Chrome --profile-directory=Default + """ + ).lstrip() + + self._test(orig_data, expected_data) + + def test_no_exec_in_group(self): + orig_data = textwrap.dedent( + """ + [Dummy Group] + foo=bar + """ + ).lstrip() + expected_data = orig_data + + self._test(orig_data, expected_data) + + def _test(self, orig_data, expected_data): + test_desktop = os.path.join(self.tmp.name, + "test.desktop") + with open(test_desktop, "w") as f: + f.write(orig_data) + + emfp.update_desktop_shortcuts(self.tmp.name) + with open(test_desktop, "r") as f: + new_data = f.read() + + self.assertEqual(expected_data, new_data) + + class TestUpdateOldConfigReferences(BaseTestCase): def setUp(self): super().setUp()