From 2eccee1a93b59ef10709100c83d7dfcc26b87706 Mon Sep 17 00:00:00 2001 From: Xan Lopez Date: Fri, 13 Mar 2009 08:29:48 +0000 Subject: Add profile migration. Migrate cookies from Mozilla, both sqlite and txt formats (sqlite has priority in case both exist). svn path=/trunk/; revision=8880 --- src/Makefile.am | 2 + src/ephy-main.c | 7 ++- src/ephy-profile-migration.c | 136 +++++++++++++++++++++++++++++++++++++++++++ src/ephy-profile-migration.h | 25 ++++++++ 4 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 src/ephy-profile-migration.c create mode 100644 src/ephy-profile-migration.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index a94d08285..4116b9b0d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,6 +31,7 @@ NOINST_H_FILES = \ ephy-lockdown.h \ ephy-location-action.h \ ephy-navigation-action.h \ + ephy-profile-migration.h \ ephy-tabs-menu.h \ ephy-toolbars-model.h \ ephy-toolbar.h \ @@ -77,6 +78,7 @@ libephymain_la_SOURCES = \ ephy-lockdown.c \ ephy-navigation-action.c \ ephy-notebook.c \ + ephy-profile-migration.c \ ephy-session.c \ ephy-shell.c \ ephy-statusbar.c \ diff --git a/src/ephy-main.c b/src/ephy-main.c index 3efd40818..0df019c11 100644 --- a/src/ephy-main.c +++ b/src/ephy-main.c @@ -32,6 +32,7 @@ #include "ephy-session.h" #include "ephy-shell.h" #include "ephy-prefs.h" +#include "ephy-profile-migration.h" #include "ephy-debug.h" #include "eggsmclient.h" @@ -737,7 +738,6 @@ main (int argc, } /* We're not remoting; start our services */ - if (!ephy_file_helpers_init (profile_directory, private_instance, keep_temp_directory || profile_directory, @@ -750,9 +750,12 @@ main (int argc, exit (1); } + /* Migrate profile */ + _ephy_profile_migrate (); + eel_gconf_monitor_add ("/apps/epiphany/general"); ephy_stock_icons_init (); - load_accels (); + load_accels (); /* Extensions may want these, so don't initialize in window-cmds */ gtk_about_dialog_set_url_hook (handle_url, NULL, NULL); diff --git a/src/ephy-profile-migration.c b/src/ephy-profile-migration.c new file mode 100644 index 000000000..576457ea4 --- /dev/null +++ b/src/ephy-profile-migration.c @@ -0,0 +1,136 @@ +/* + * Copyright © 2009 Xan López + * + * 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 2, 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "config.h" + +#include "ephy-file-helpers.h" +#include "ephy-profile-migration.h" + +#include +#include + +/* + * What to do to add new migration steps: + * - Bump PROFILE_MIGRATION_VERSION + * - Add your function at the end of the 'migrators' array + */ + +#define PROFILE_MIGRATION_VERSION 1 + +typedef void (*EphyProfileMigrator) (void); + +static void +migrate_cookies () +{ + const char *cookies_file_sqlite = "cookies.sqlite"; + const char *cookies_file_txt = "cookies.txt"; + char *src_sqlite = NULL, *src_txt = NULL, *dest = NULL; + + dest = g_build_filename (ephy_dot_dir (), cookies_file_sqlite, NULL); + /* If we already have a cookies.sqlite file, do nothing */ + if (g_file_test (dest, G_FILE_TEST_EXISTS)) + goto out; + + src_sqlite = g_build_filename (ephy_dot_dir (), "mozilla", + "epiphany", cookies_file_sqlite, NULL); + src_txt = g_build_filename (ephy_dot_dir (), "mozilla", + "epiphany", cookies_file_txt, NULL); + + /* First check if we have a cookies.sqlite file in Mozilla */ + if (g_file_test (src_sqlite, G_FILE_TEST_EXISTS)) { + GFile *gsrc, *gdest; + + /* Copy the file */ + gsrc = g_file_new_for_path (src_sqlite); + gdest = g_file_new_for_path (dest); + + if (!g_file_copy (gsrc, gdest, 0, NULL, NULL, NULL, NULL)) + g_warning (_("Failed to copy cookies file from Mozilla.")); + + g_object_unref (gsrc); + g_object_unref (gdest); + } else if (g_file_test (src_txt, G_FILE_TEST_EXISTS)) { + /* Create a SoupCookieJarSQLite with the contents of the txt file */ + GSList *cookies, *p; + SoupCookieJar *txt, *sqlite; + + txt = soup_cookie_jar_text_new (src_txt, TRUE); + sqlite = soup_cookie_jar_sqlite_new (dest, FALSE); + cookies = soup_cookie_jar_all_cookies (txt); + + for (p = cookies; p; p = p->next) { + SoupCookie *cookie = (SoupCookie*)p->data; + /* Cookie is stolen, so we won't free it */ + soup_cookie_jar_add_cookie (sqlite, cookie); + } + + g_slist_free (cookies); + g_object_unref (txt); + g_object_unref (sqlite); + } + + out: + g_free (src_sqlite); + g_free (src_txt); + g_free (dest); +} + +const EphyProfileMigrator migrators[] = { + migrate_cookies +}; + +#define PROFILE_MIGRATION_FILE ".migrated" + +void +_ephy_profile_migrate () +{ + int latest, i; + char *migrated_file, *contents; + + /* Figure out the latest migration that occured */ + migrated_file = g_build_filename (ephy_dot_dir (), + PROFILE_MIGRATION_FILE, + NULL); + if (g_file_test (migrated_file, G_FILE_TEST_EXISTS)) { + gsize size; + int result; + + g_file_get_contents (migrated_file, &contents, &size, NULL); + result = sscanf(contents, "%d", &latest); + g_free (contents); + + if (result != 1) { + g_warning (_("Failed to read latest migration marker, aborting profile migration.")); + return; + } + } else + /* Never migrated */ + latest = 0; + + for (i = latest; i < PROFILE_MIGRATION_VERSION; i++) { + EphyProfileMigrator m = migrators[i]; + m(); + } + + /* Write down the latest migration */ + contents = g_strdup_printf ("%d", PROFILE_MIGRATION_VERSION); + g_file_set_contents (migrated_file, contents, -1, NULL); + g_free (contents); +} + diff --git a/src/ephy-profile-migration.h b/src/ephy-profile-migration.h new file mode 100644 index 000000000..51114a88d --- /dev/null +++ b/src/ephy-profile-migration.h @@ -0,0 +1,25 @@ +/* + * Copyright © 2009 Xan López + * + * 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 2, 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef EPHY_PROFILE_MIGRATION_H +#define EPHY_PROFILE_MIGRATION_H + +void _ephy_profile_migrate (void); + +#endif -- cgit v1.2.3