From f8cd126d6fc94d563f4cf4d818ad1f6c8e512cc9 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Sat, 23 Jan 2021 22:05:18 +0100 Subject: [PATCH] record-store: Handle old and new database locations gracefully Because we rename the project and binaries to 'gnome-calls' we want to make sure that the call history is preserved by doing the following: If there is an old directory '~/.local/share/calls' try to rename it to 'gnome-calls'. Use new location if it succeeds and fall back to old location if it failed. --- src/calls-record-store.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/calls-record-store.c b/src/calls-record-store.c index d8bd93f..64f7cc7 100644 --- a/src/calls-record-store.c +++ b/src/calls-record-store.c @@ -30,6 +30,7 @@ #include #include +#include #include @@ -749,8 +750,37 @@ calls_record_store_class_init (CallsRecordStoreClass *klass) static void calls_record_store_init (CallsRecordStore *self) { - self->filename = g_build_filename (g_get_user_data_dir (), - APP_DATA_NAME, + gboolean exist_old, exist_new, new_is_dir; + g_autofree gchar *old_dir = g_build_filename (g_get_user_data_dir (), + "calls", + NULL); + g_autofree gchar *new_dir = g_build_filename (g_get_user_data_dir (), + APP_DATA_NAME, + NULL); + gchar *used_dir = NULL; + + exist_old = g_file_test (old_dir, G_FILE_TEST_EXISTS); + exist_new = g_file_test (new_dir, G_FILE_TEST_EXISTS); + new_is_dir = g_file_test (new_dir, G_FILE_TEST_IS_DIR); + + if (exist_old && !exist_new) { + g_debug ("Trying to move database from `%s' to `%s'", old_dir, new_dir); + + if (g_rename (old_dir, new_dir) == 0) { + used_dir = new_dir; + } else { + g_warning ("Moving folders to new location failed!"); + g_debug ("Continuing to use old location"); + used_dir = old_dir; + } + } else if (exist_new && new_is_dir) + used_dir = new_dir; + else + used_dir = old_dir; + + g_assert (used_dir); + + self->filename = g_build_filename (used_dir, RECORD_STORE_FILENAME, NULL); }