[easytag] Use XDG directories for configuration and cache



commit 856b4553904c59e564d5795903095098fad643db
Author: David King <amigadave amigadave com>
Date:   Sun Dec 2 19:39:47 2012 +0000

    Use XDG directories for configuration and cache
    
    Fixes bug 689419.

 src/browser.c |    6 +-
 src/cddb.c    |   55 +++------
 src/easytag.c |    7 -
 src/easytag.h |    1 -
 src/id3_tag.c |   26 +---
 src/log.c     |   35 ++++--
 src/setting.c |  372 ++++++++++++++++++---------------------------------------
 7 files changed, 163 insertions(+), 339 deletions(-)
---
diff --git a/src/browser.c b/src/browser.c
index c1fa161..507d92a 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -216,7 +216,7 @@ gboolean    Run_Program (gchar *program_name, GList *args_list);
  */
 void Browser_Load_Home_Directory (void)
 {
-    Browser_Tree_Select_Dir(HOME_VARIABLE);
+    Browser_Tree_Select_Dir (g_get_home_dir ());
 }
 
 /*
@@ -264,7 +264,7 @@ void Browser_Load_Default_Directory (void)
     if (!path_utf8 || strlen(path_utf8)<=0)
     {
         g_free(path_utf8);
-        path_utf8 = g_strdup(HOME_VARIABLE);
+        path_utf8 = g_strdup (g_get_home_dir ());
     }
 
     // 'DEFAULT_PATH_TO_MP3' is stored in UTF-8, we must convert it to the file system encoding before...
@@ -3635,7 +3635,7 @@ GtkWidget *Create_Browser_Items (GtkWidget *parent)
     gtk_widget_show_all(VerticalBox);
 
     /* Set home variable as current path */
-    Browser_Update_Current_Path(HOME_VARIABLE);
+    Browser_Update_Current_Path (g_get_home_dir ());
 
     return VerticalBox;
 }
diff --git a/src/cddb.c b/src/cddb.c
index 0afb780..5950261 100644
--- a/src/cddb.c
+++ b/src/cddb.c
@@ -102,7 +102,7 @@ static char *cddb_genre_vs_id3_genre [][2] =
 
 
 // File for result of the Cddb/Freedb request (on remote access)
-gchar *CDDB_RESULT_FILE                            = ".easytag/cddb_result_file.tmp";
+static const gchar CDDB_RESULT_FILE[] = "cddb_result_file.tmp";
 
 
 /****************
@@ -1745,28 +1745,15 @@ void Cddb_Close_Connection (gint socket_id)
  */
 gint Cddb_Write_Result_To_File (gint socket_id, gulong *bytes_read_total)
 {
-    gchar *home_path = NULL;
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE  *file;
+    gint result;
 
+    /* Cache directory was already created by Log_Print(). */
+    file_path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME,
+                                  CDDB_RESULT_FILE, NULL);
 
-    /* The file to write */
-    if (!HOME_VARIABLE)
-        return FALSE;
-
-    home_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                            NULL);
-
-    file_path = g_strconcat(home_path,CDDB_RESULT_FILE,NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
-
-    if ( (file=fopen(file_path,"w+")) != NULL )
+    if ((file = fopen (file_path, "w+")) != NULL)
     {
         gchar cddb_out[MAX_STRING_LEN+1];
         gint  bytes_read = 0;
@@ -1801,16 +1788,17 @@ gint Cddb_Write_Result_To_File (gint socket_id, gulong *bytes_read_total)
 
         if (bytes_read < 0)
         {
-            Log_Print(LOG_ERROR,_("Error when reading cddb response (%s)!"),g_strerror(errno));
+            Log_Print (LOG_ERROR, _("Error when reading CDDB response (%s)"),
+	               g_strerror(errno));
             return -1; // Error!
         }
 
     } else
     {
-        Log_Print(LOG_ERROR,_("Can't create file '%s' (%s)"),file_path,g_strerror(errno));
+        Log_Print (LOG_ERROR, _("Cannot create file '%s' (%s)"), file_path,
+	           g_strerror(errno));
     }
     g_free(file_path);
-    g_free(home_path);
 
     return 0;
 }
@@ -1840,28 +1828,19 @@ gint Cddb_Read_Line (FILE **file, gchar **cddb_out)
     if (*file == NULL)
     {
         // Open the file for reading the first time
-        gchar *home_path;
         gchar *file_path;
-        gchar *file_path_tmp;
-
-        home_path = g_strconcat(HOME_VARIABLE,
-                                HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                                NULL);
-        file_path = g_strconcat(home_path,CDDB_RESULT_FILE,NULL);
-        g_free(home_path);
 
-        // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-        file_path_tmp = file_path;
-        file_path = filename_from_display(file_path);
-        g_free(file_path_tmp);
+        file_path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME,
+                                      CDDB_RESULT_FILE, NULL);
 
-        if ( (*file=fopen(file_path,"r"))==0 )
+        if ((*file = fopen (file_path, "r")) == 0)
         {
-            Log_Print(LOG_ERROR,_("Can't open file '%s' (%s)"),file_path,g_strerror(errno));
-            g_free(file_path);
+            Log_Print (LOG_ERROR, _("Cannot open file '%s' (%s)"), file_path,
+                       g_strerror(errno));
+            g_free (file_path);
             return -1; // Error!
         }
-        g_free(file_path);
+        g_free (file_path);
     }
 
     result = fgets(buffer,sizeof(buffer),*file);
diff --git a/src/easytag.c b/src/easytag.c
index 208bde0..a8412c1 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -182,13 +182,6 @@ int main (int argc, char *argv[])
     /* Initialize GTK */
     gtk_init(&argc, &argv);
 
-    /* Get home variable */
-#ifdef WIN32
-        HOME_VARIABLE = weasytag_data_dir();
-#else
-        HOME_VARIABLE = g_get_home_dir();
-#endif
-
     INIT_DIRECTORY = NULL;
 
     /* Starting messages */
diff --git a/src/easytag.h b/src/easytag.h
index be1baf3..4ab0ac8 100644
--- a/src/easytag.h
+++ b/src/easytag.h
@@ -150,7 +150,6 @@ GtkWidget *PictureScrollWindow;
 
 GdkCursor *MouseCursor;
 
-const gchar *HOME_VARIABLE;
 gchar *INIT_DIRECTORY;
 
 #ifndef errno
diff --git a/src/id3_tag.c b/src/id3_tag.c
index f3b80f7..2b5b31b 100644
--- a/src/id3_tag.c
+++ b/src/id3_tag.c
@@ -1253,36 +1253,22 @@ gboolean Id3tag_Check_If_File_Is_Corrupted (gchar *filename)
 gboolean Id3tag_Check_If_Id3lib_Is_Bugged (void)
 {
     FILE *file;
-    unsigned char tmp[16] = {0xFF, 0xFB, 0x00, 0x00, 0x00, 0x00,
-                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                             0x00, 0x00, 0x00, 0x00};
+    guchar tmp[16] = {0xFF, 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     ID3Tag *id3_tag = NULL;
     gchar *filename;
-    gchar *filename_tmp;
     gchar *result = NULL;
     ID3Frame *id3_frame;
     gboolean use_unicode;
 
 
-    if (!HOME_VARIABLE)
-        return FALSE;
-
     // Create a temporary file
-    filename = g_strconcat(HOME_VARIABLE,
-                           HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
-                           ".easytag" G_DIR_SEPARATOR_S "test_easytag.mp3",
-                           NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    filename_tmp = filename;
-    filename = filename_from_display(filename);
-    g_free(filename_tmp);
-
-    if ( (file=fopen(filename,"w+"))==NULL )
+    if ((file = g_mkstemp ("easytagXXXXXX.mp3")) == NULL)
     {
         gchar *filename_utf8 = filename_to_display(filename);
-        Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' (%s)."),filename_utf8,g_strerror(errno));
-        g_free(filename_utf8);
+        Log_Print (LOG_ERROR, _("ERROR while opening file: '%s' (%s)"),
+                   filename_utf8, g_strerror(errno));
+        g_free (filename_utf8);
         return FALSE;
     }
     // Set data in the file
diff --git a/src/log.c b/src/log.c
index cde953e..5e2deda 100644
--- a/src/log.c
+++ b/src/log.c
@@ -21,9 +21,13 @@
 #include <config.h>
 
 #include <glib/gi18n-lib.h>
+#include <errno.h>
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "log.h"
 #include "easytag.h"
@@ -56,7 +60,7 @@ enum
 };
 
 // File for log
-gchar *LOG_FILE = ".easytag/easytag.log";
+static const gchar LOG_FILE[] = "easytag.log";
 
 // Structure used to store information for the temporary list
 typedef struct _Log_Data Log_Data;
@@ -283,23 +287,30 @@ void Log_Print (Log_Error_Type error_type, gchar const *format, ...)
         //g_print("%s",string);
     }
 
-
     // Store also the messages in the log file.
     if (!file_path)
     {
-        gchar *file_path_tmp = NULL;
+        gchar *cache_path = g_build_filename (g_get_user_cache_dir (),
+                                              PACKAGE_TARNAME, NULL);
+
+        if (!g_file_test (cache_path, G_FILE_TEST_IS_DIR))
+        {
+            gint result = g_mkdir_with_parents (cache_path, S_IRWXU);
 
-        file_path = g_strconcat(HOME_VARIABLE,
-                                HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
-                                LOG_FILE,NULL);
+            if (result == -1)
+            {
+                g_printerr ("%s", "Unable to create cache directory");
+                g_free (cache_path);
 
-        // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-        file_path_tmp = file_path;
-        file_path = filename_from_display(file_path);
-        g_free(file_path_tmp);
+                return;
+            }
+        }
+
+        file_path = g_build_filename (cache_path, LOG_FILE, NULL);
+        g_free (cache_path);
     }
 
-    // The first time, the whole file is delete. Else, text is appended
+    // The first time, the whole file is deleted. Else, text is appended.
     if (first_time)
         file = fopen(file_path,"w+");
     else
@@ -317,8 +328,8 @@ void Log_Print (Log_Error_Type error_type, gchar const *format, ...)
         first_time = FALSE;
         fclose(file);
     }
-    g_free(string);
 
+    g_free(string);
 }
 
 /*
diff --git a/src/setting.c b/src/setting.c
index 67786f7..1757ce2 100644
--- a/src/setting.c
+++ b/src/setting.c
@@ -58,42 +58,40 @@
  *    NetBSD's mkdir(2).
  */
 
-// Base directory created into home dir
-#define EASYTAG_DIR                                 ".easytag"
 // File for configuration
-#define CONFIG_FILE                                 EASYTAG_DIR G_DIR_SEPARATOR_S "easytagrc"
+static const gchar CONFIG_FILE[] = "easytagrc";
 // File of masks for tag scanner
-#define SCAN_TAG_MASKS_FILE                         EASYTAG_DIR G_DIR_SEPARATOR_S "scan_tag.mask"
+static const gchar SCAN_TAG_MASKS_FILE[] = "scan_tag.mask";
 // File of masks for rename file scanner
-#define RENAME_FILE_MASKS_FILE                      EASYTAG_DIR G_DIR_SEPARATOR_S "rename_file.mask"
+static const gchar RENAME_FILE_MASKS_FILE[] = "rename_file.mask";
 // File for history of RenameDirectoryMaskCombo combobox
-#define RENAME_DIRECTORY_MASKS_FILE                 EASYTAG_DIR G_DIR_SEPARATOR_S "rename_directory.mask"
+static const gchar RENAME_DIRECTORY_MASKS_FILE[] = "rename_directory.mask";
 // File for history of PlayListNameCombo combobox
-#define PLAY_LIST_NAME_MASKS_FILE                   EASYTAG_DIR G_DIR_SEPARATOR_S "play_list_name.mask"
+static const gchar PLAY_LIST_NAME_MASKS_FILE[] = "play_list_name.mask";
 // File for history of PlayListContentMaskEntry combobox
-#define PLAYLIST_CONTENT_MASKS_FILE                 EASYTAG_DIR G_DIR_SEPARATOR_S "playlist_content.mask"
+static const gchar PLAYLIST_CONTENT_MASKS_FILE[] = "playlist_content.mask";
 // File for history of DefaultPathToMp3 combobox
-#define DEFAULT_PATH_TO_MP3_HISTORY_FILE            EASYTAG_DIR G_DIR_SEPARATOR_S "default_path_to_mp3.history"
+static const gchar DEFAULT_PATH_TO_MP3_HISTORY_FILE[] = "default_path_to_mp3.history";
 // File for history of DefaultComment combobox
-#define DEFAULT_TAG_COMMENT_HISTORY_FILE            EASYTAG_DIR G_DIR_SEPARATOR_S "default_tag_comment.history"
+static const gchar DEFAULT_TAG_COMMENT_HISTORY_FILE[] = "default_tag_comment.history";
 // File for history of BrowserEntry combobox
-#define PATH_ENTRY_HISTORY_FILE                     EASYTAG_DIR G_DIR_SEPARATOR_S "browser_path.history"
+static const gchar PATH_ENTRY_HISTORY_FILE[] = "browser_path.history";
 // File for history of run program combobox for directories
-#define RUN_PROGRAM_WITH_DIRECTORY_HISTORY_FILE     EASYTAG_DIR G_DIR_SEPARATOR_S "run_program_with_directory.history"
+static const gchar RUN_PROGRAM_WITH_DIRECTORY_HISTORY_FILE[] = "run_program_with_directory.history";
 // File for history of run program combobox for files
-#define RUN_PROGRAM_WITH_FILE_HISTORY_FILE          EASYTAG_DIR G_DIR_SEPARATOR_S "run_program_with_file.history"
+static const gchar RUN_PROGRAM_WITH_FILE_HISTORY_FILE[] = "run_program_with_file.history";
 // File for history of run player combobox
-#define AUDIO_FILE_PLAYER_HISTORY_FILE              EASYTAG_DIR G_DIR_SEPARATOR_S "audio_file_player.history"
+static const gchar AUDIO_FILE_PLAYER_HISTORY_FILE[] = "audio_file_player.history";
 // File for history of search string combobox
-#define SEARCH_FILE_HISTORY_FILE                    EASYTAG_DIR G_DIR_SEPARATOR_S "search_file.history"
+static const gchar SEARCH_FILE_HISTORY_FILE[] = "search_file.history";
 // File for history of FileToLoad combobox
-#define FILE_TO_LOAD_HISTORY_FILE                   EASYTAG_DIR G_DIR_SEPARATOR_S "file_to_load.history"
+static const gchar FILE_TO_LOAD_HISTORY_FILE[] = "file_to_load.history";
 // File for history of CddbSearchStringEntry combobox
-#define CDDB_SEARCH_STRING_HISTORY_FILE             EASYTAG_DIR G_DIR_SEPARATOR_S "cddb_search_string.history"
+static const gchar CDDB_SEARCH_STRING_HISTORY_FILE[] = "cddb_search_string.history";
 // File for history of CddbSearchStringInResultEntry combobox
-#define CDDB_SEARCH_STRING_IN_RESULT_HISTORY_FILE   EASYTAG_DIR G_DIR_SEPARATOR_S "cddb_search_string_in_result.history"
+static const gchar CDDB_SEARCH_STRING_IN_RESULT_HISTORY_FILE[] = "cddb_search_string_in_result.history";
 // File for history of CddbLocalPath combobox
-#define CDDB_LOCAL_PATH_HISTORY_FILE                EASYTAG_DIR G_DIR_SEPARATOR_S "cddb_local_path.history"
+static const gchar CDDB_LOCAL_PATH_HISTORY_FILE[] = "cddb_local_path.history";
 
 
 
@@ -354,7 +352,7 @@ void Init_Config_Variables (void)
      * Common
      */
     LOAD_ON_STARTUP               = 0;
-    DEFAULT_PATH_TO_MP3           = g_strdup(HOME_VARIABLE);
+    DEFAULT_PATH_TO_MP3           = g_strdup(g_get_home_dir ());
     BROWSE_SUBDIR                 = 1;
 #ifdef WIN32
     BROWSE_HIDDEN_DIR             = 1;
@@ -914,24 +912,18 @@ void Save_Changes_Of_Preferences_Window (void)
 void Save_Config_To_File (void)
 {
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE *file;
 
-    /* The file to write */
-    if (!HOME_VARIABLE) return;
-    file_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
-                            CONFIG_FILE,NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  CONFIG_FILE, NULL);
 
-    if ( Create_Easytag_Directory()==0 || (file=fopen(file_path,"w+"))==0 )
+    if (!Create_Easytag_Directory () || (file = fopen (file_path, "w+")) == 0)
     {
-        Log_Print(LOG_ERROR,_("ERROR: Can't write config file: %s (%s)"),file_path,g_strerror(errno));
-    }else
+        Log_Print (LOG_ERROR,
+                   _("ERROR: Cannot write configuration file: %s (%s)"),
+                   file_path, g_strerror(errno));
+    }
+    else
     {
         gint ConfigVarListLen = sizeof(Config_Variables)/sizeof(tConfigVariable);
         gint i;
@@ -1062,25 +1054,18 @@ void Set_Config (gchar *line)
 void Read_Config (void)
 {
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE *file;
     gchar buffer[MAX_STRING_LEN];
 
     /* The file to read */
-    if (!HOME_VARIABLE) return;
-    file_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                            CONFIG_FILE,NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  CONFIG_FILE, NULL);
 
-    if ( (file=fopen(file_path,"r"))==0 )
+    if ((file = fopen (file_path,"r")) == 0)
     {
-        Log_Print(LOG_ERROR,_("Can't open configuration file '%s' (%s)"),file_path,g_strerror(errno));
-        Log_Print(LOG_OK,_("Loading default configurationâ"));
+        Log_Print (LOG_ERROR, _("Cannot open configuration file '%s' (%s)"),
+                   file_path, g_strerror (errno));
+        Log_Print (LOG_OK, _("Loading default configuration"));
     }else
     {
         while (fgets(buffer,sizeof(buffer),file))
@@ -1106,23 +1091,16 @@ void Read_Config (void)
 void Display_Config (void)
 {
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE *file;
 
     /* The file to write */
-    if (!HOME_VARIABLE) return;
-    file_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                            CONFIG_FILE,NULL);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  CONFIG_FILE, NULL);
 
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
-
-    if ( (file=fopen(file_path,"r"))==0 )
+    if ((file = fopen (file_path, "r")) == 0)
     {
-        g_print(_("Can't open configuration file '%s' (%s)"),file_path,g_strerror(errno));
+        g_print (_("Cannot open configuration file '%s' (%s)"), file_path,
+                 g_strerror (errno));
     }else
     {
         gint ConfigVarListLen = sizeof(Config_Variables)/sizeof(tConfigVariable);
@@ -1160,156 +1138,62 @@ void Display_Config (void)
 }
 
 
-
-
 /*
- * Create the main directory with empty history files
+ * check_or_create_file:
+ * @filename: (type filename): the filename to create
+ *
+ * Check that the provided @filename exists, and if not, create it.
  */
-gboolean Setting_Create_Files (void)
+static void check_or_create_file (const gchar *filename)
 {
-    gchar *home_path = NULL;
-    gchar *home_path_tmp = NULL;
-    gchar *file_path = NULL;
     FILE  *file;
+    gchar *file_path = NULL;
 
-    /* The file to write */
-    if (!HOME_VARIABLE)
-        return FALSE;
-
-    if ( Create_Easytag_Directory()==FALSE )
-        return FALSE;
-
-    home_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
-                            NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    // We do it only for 'home_path' to avoid lot of code...
-    home_path_tmp = home_path;
-    home_path = filename_from_display(home_path);
-    g_free(home_path_tmp);
-
-    file_path = g_strconcat(home_path,CONFIG_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),CONFIG_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,SCAN_TAG_MASKS_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),SCAN_TAG_MASKS_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,RENAME_FILE_MASKS_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),RENAME_FILE_MASKS_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,RENAME_DIRECTORY_MASKS_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),RENAME_DIRECTORY_MASKS_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,DEFAULT_PATH_TO_MP3_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),DEFAULT_PATH_TO_MP3_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,DEFAULT_TAG_COMMENT_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),DEFAULT_TAG_COMMENT_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,PATH_ENTRY_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),PATH_ENTRY_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,PLAY_LIST_NAME_MASKS_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),PLAY_LIST_NAME_MASKS_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,RUN_PROGRAM_WITH_DIRECTORY_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),RUN_PROGRAM_WITH_DIRECTORY_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,RUN_PROGRAM_WITH_FILE_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),RUN_PROGRAM_WITH_FILE_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,AUDIO_FILE_PLAYER_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),AUDIO_FILE_PLAYER_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,SEARCH_FILE_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),SEARCH_FILE_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,FILE_TO_LOAD_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),FILE_TO_LOAD_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
-
-    file_path = g_strconcat(home_path,PLAYLIST_CONTENT_MASKS_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),PLAYLIST_CONTENT_MASKS_FILE,g_strerror(errno));
-    g_free(file_path);
+    g_return_if_fail (filename != NULL);
 
-    file_path = g_strconcat(home_path,CDDB_SEARCH_STRING_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),CDDB_SEARCH_STRING_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  filename, NULL);
 
-    file_path = g_strconcat(home_path,CDDB_SEARCH_STRING_IN_RESULT_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
+    if ((file = fopen (file_path, "a+")) != NULL )
+    {
+        fclose (file);
+    }
     else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),CDDB_SEARCH_STRING_IN_RESULT_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
+    {
+        Log_Print (LOG_ERROR, _("Cannot create or open file '%s' (%s)"),
+                   CONFIG_FILE, g_strerror (errno));
+    }
 
-    file_path = g_strconcat(home_path,CDDB_LOCAL_PATH_HISTORY_FILE,NULL);
-    if ( (file=fopen(file_path,"a+")) != NULL )
-        fclose(file);
-    else
-        Log_Print(LOG_ERROR,_("Can't create or open file '%s' (%s)"),CDDB_LOCAL_PATH_HISTORY_FILE,g_strerror(errno));
-    g_free(file_path);
+    g_free (file_path);
+}
 
+/*
+ * Create the main directory with empty history files
+ */
+gboolean Setting_Create_Files (void)
+{
+    /* The file to write */
+    if (!Create_Easytag_Directory ())
+    {
+        return FALSE;
+    }
 
-    g_free(home_path);
+    check_or_create_file (SCAN_TAG_MASKS_FILE);
+    check_or_create_file (RENAME_FILE_MASKS_FILE);
+    check_or_create_file (RENAME_DIRECTORY_MASKS_FILE);
+    check_or_create_file (DEFAULT_PATH_TO_MP3_HISTORY_FILE);
+    check_or_create_file (DEFAULT_TAG_COMMENT_HISTORY_FILE);
+    check_or_create_file (PATH_ENTRY_HISTORY_FILE);
+    check_or_create_file (PLAY_LIST_NAME_MASKS_FILE);
+    check_or_create_file (RUN_PROGRAM_WITH_DIRECTORY_HISTORY_FILE);
+    check_or_create_file (RUN_PROGRAM_WITH_FILE_HISTORY_FILE);
+    check_or_create_file (AUDIO_FILE_PLAYER_HISTORY_FILE);
+    check_or_create_file (SEARCH_FILE_HISTORY_FILE);
+    check_or_create_file (FILE_TO_LOAD_HISTORY_FILE);
+    check_or_create_file (PLAYLIST_CONTENT_MASKS_FILE);
+    check_or_create_file (CDDB_SEARCH_STRING_HISTORY_FILE);
+    check_or_create_file (CDDB_SEARCH_STRING_IN_RESULT_HISTORY_FILE);
+    check_or_create_file (CDDB_LOCAL_PATH_HISTORY_FILE);
 
     return TRUE;
 }
@@ -1319,10 +1203,9 @@ gboolean Setting_Create_Files (void)
 /*
  * Save the contents of a list store to a file
  */
-void Save_List_Store_To_File (gchar *filename, GtkListStore *liststore, gint colnum)
+void Save_List_Store_To_File (const gchar *filename, GtkListStore *liststore, gint colnum)
 {
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE *file;
     gchar *data = NULL;
     gchar *text;
@@ -1332,19 +1215,13 @@ void Save_List_Store_To_File (gchar *filename, GtkListStore *liststore, gint col
         return;
 
     /* The file to write */
-    if (!HOME_VARIABLE) return;
-    file_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                            filename,NULL);
-
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  filename, NULL);
 
-    if ( Create_Easytag_Directory()==0 || (file=fopen(file_path,"w+"))==NULL )
+    if (!Create_Easytag_Directory () || (file = fopen (file_path, "w+")) == NULL)
     {
-        Log_Print(LOG_ERROR,_("ERROR: Can't write list to file: %s (%s)"),file_path,g_strerror(errno));
+        Log_Print (LOG_ERROR, _("ERROR: Cannot write list to file: %s (%s)"),
+                   file_path, g_strerror (errno));
     }else
     {
         do
@@ -1367,30 +1244,25 @@ void Save_List_Store_To_File (gchar *filename, GtkListStore *liststore, gint col
 /*
  * Populate a list store with data from a file passed in as first parameter
  */
-gboolean Populate_List_Store_From_File (gchar *filename, GtkListStore *liststore, gint text_column)
+gboolean Populate_List_Store_From_File (const gchar *filename, GtkListStore *liststore, gint text_column)
 {
 
     gchar *file_path = NULL;
-    gchar *file_path_tmp = NULL;
     FILE *file;
     gchar buffer[MAX_STRING_LEN];
     GtkTreeIter iter;
     gboolean entries_set = FALSE;
 
     /* The file to write */
-    if (!filename || !HOME_VARIABLE) return FALSE;
-    file_path = g_strconcat(HOME_VARIABLE,
-                            HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                            filename,NULL);
+    g_return_val_if_fail (filename != NULL, FALSE);
 
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    file_path_tmp = file_path;
-    file_path = filename_from_display(file_path);
-    g_free(file_path_tmp);
+    file_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                  filename, NULL);
 
-    if ( (file=fopen(file_path,"r"))==NULL )
+    if ((file = fopen (file_path, "r")) == NULL)
     {
-        Log_Print(LOG_ERROR,_("Can't open file '%s' (%s)"),file_path,g_strerror(errno));
+        Log_Print (LOG_ERROR, _("Cannot open file '%s' (%s)"), file_path,
+                   g_strerror (errno));
     }else
     {
         gchar *data = NULL;
@@ -1668,49 +1540,33 @@ void Save_Cddb_Local_Path_List (GtkListStore *liststore, gint colnum)
 
 
 
-/*
- * Create the directory used by EasyTAG to store files for each user.
- * If the directory already exists, does nothing and returns 1.
- * If unable to create the directory, returns 0.
+/**
+ * Create the directory used by EasyTAG to store user configuration files.
+ *
+ * Returns: %TRUE if the directory was created, or already exists. %FALSE if
+ * the directory could not be created.
  */
 gboolean Create_Easytag_Directory (void)
 {
     gchar *easytag_path = NULL;
-    gchar *easytag_path_tmp = NULL;
-    DIR *dir;
+    gint result;
 
-    if (!HOME_VARIABLE)
-    {
-        Log_Print(LOG_ERROR,_("ERROR: The environment variable HOME is not defined!"));
-        return FALSE;
-    }
+    /* Directory to create (if it does not exist) with absolute path. */
+    easytag_path = g_build_filename (g_get_user_config_dir (), PACKAGE_TARNAME,
+                                     NULL);
 
-    /* Directory to create (if doesn't exists) with absolute path
-     * Note for NetBSD : avoid passing a trailing slash to mkdir() */
-    easytag_path = g_strconcat(HOME_VARIABLE,
-                               HOME_VARIABLE[strlen(HOME_VARIABLE)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                               EASYTAG_DIR,
-                               //EASYTAG_DIR[strlen(EASYTAG_DIR)-1]!=G_DIR_SEPARATOR?G_DIR_SEPARATOR_S:"",
-                               NULL);
+    result = g_mkdir_with_parents (easytag_path, S_IRWXU);
 
-    // Must convert to the filesystem encoding (else may cause problem under XP with accounts like "Lé)
-    easytag_path_tmp = easytag_path;
-    easytag_path = filename_from_display(easytag_path);
-    g_free(easytag_path_tmp);
-
-    if ( (dir=opendir(easytag_path)) == NULL )
+    if (result == -1)
     {
-        if ( (mkdir(easytag_path,S_IRWXU|S_IXGRP|S_IRGRP)) == -1)
-        {
-            Log_Print(LOG_ERROR,_("ERROR: Can't create directory '%s' (%s)!"),easytag_path,g_strerror(errno));
-            return FALSE;
-        }
-    }else
+        Log_Print (LOG_ERROR,_("ERROR: Cannot create directory '%s' (%s)!"),
+                  easytag_path, g_strerror (errno));
+        g_free (easytag_path);
+        return FALSE;
+    }
+    else
     {
-        closedir(dir);
+        g_free (easytag_path);
+        return TRUE;
     }
-
-    g_free(easytag_path);
-
-    return TRUE;
 }



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