Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tilp: Use g_get_user_config_dir() to get the config directory #75

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions tilp/trunk/help/chapter07.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@ <h1>7. Command line</h1>


CONFIG FILE FORMAT
This section describes the format of the .tilp config file which is in
the home directory of the user (Linux) or the tilp.ini file (Win32).
The file is separated into several sections (hardware, gui, calc,
external programs, fonts, screen, misc). But, there is no order, you
can write lines as you want. A line preceded by the '#' symbol is a
comment. Each line has the following format: option=something. Every
line can be put in any order. Beware: any comment added to the file
will be overwritten ! Below is described each option and the possible
values:
This section describes the format of the tilp.ini config file which is
in the configuration directory of the user ($XDG_CONFIG_HOME or
LocalAppData on Windows). Note that the configuration used to be
located in the user's home directory. This file will be automatically
migrated to the new location. The file is separated into several
sections (hardware, gui, calc, external programs, fonts, screen, misc).
But, there is no order, you can write lines as you want. A line
preceded by the '#' symbol is a comment. Each line has the following
format: option=something. Every line can be put in any order. Beware:
any comment added to the file will be overwritten! Below is described
each option and the possible values:

version= Which version of TiLP has written the config file. Used to
display the Release the file and to keep compatibility.
Expand Down
2 changes: 1 addition & 1 deletion tilp/trunk/man/tilp.1
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Starting at version 1.13, a new and shorter syntax can be used:
tilp ti89 blacklink \-p2 \-t20 \-n group.89g

.SH CONFIG FILE FORMAT
This section describes the format of the .tilp config file which is in the home directory of the user (*nix, Win32), or the old tilp.ini file formerly stored alongside the program (Win32). The file is separated into several sections (hardware, gui, calc, external programs, fonts, screen, misc).
This section describes the format of the tilp.ini config file which is in the configuration directory of the user ($XDG_CONFIG_HOME or LocalAppData on windows). The file is separated into several sections (hardware, gui, calc, external programs, fonts, screen, misc).
A line preceded by the '#' symbol is a comment. Each line has the following format: option=something.
Every line can be put in any order. Beware: any comment added to the file will be overwritten!
Here's a description of the options and their possible values:
Expand Down
4 changes: 2 additions & 2 deletions tilp/trunk/po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ msgid "The file <%s> does not exist."
msgstr "Die Datei <%s> existiert nich."

#: ../src/tilp_config.c:382
msgid "Unable to write the config file (~/.tilp or ~/tilp.ini).\n"
msgid "Unable to write the config file ($XDG_CONFIG_HOME/tilp.ini).\n"
msgstr ""
"Die Konfigurationsdatei (~/.tilp or tilp.ini) kann nicht geschrieben "
"Die Konfigurationsdatei ($XDG_CONFIG_HOME/tilp.ini) kann nicht geschrieben "
"werden.\n"

#: ../src/tilp_config.c:544
Expand Down
6 changes: 3 additions & 3 deletions tilp/trunk/po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,10 @@ msgid "The file <%s> does not exist."
msgstr "Le fichier <%s> n'existe pas."

#: ../src/tilp_config.c:382
msgid "Unable to write the config file (~/.tilp or ~/tilp.ini).\n"
msgid "Unable to write the config file ($XDG_CONFIG_HOME/tilp.ini).\n"
msgstr ""
"Impossible d'enregistrer le fichier de configuration (~/.tilp ou ~/tilp."
"ini).\n"
"Impossible d'enregistrer le fichier de configuration ($XDG_CONFIG_HOME/"
"tilp.ini).\n"

#: ../src/tilp_config.c:544
msgid "Configuration file saved."
Expand Down
118 changes: 48 additions & 70 deletions tilp/trunk/src/tilp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,90 +102,68 @@ int tilp_config_default(void)
return 0;
}

static void deprecated_config_path(char* old_ini_file, char* ini_file)
{
int old_exists, new_exists;
old_exists = !access(old_ini_file, F_OK);
new_exists = !access(ini_file, F_OK);

if (old_exists && !new_exists)
{
FILE *in;
FILE *out;

in = fopen(old_ini_file, "rb");
out = fopen(ini_file, "wb");
if (in && out)
{
int c;
while ((c = fgetc(in)) != EOF)
{
fputc(c, out);
}
fclose(out);
fclose(in);
// A config file now exists at the new location.
// Delete the old file.
unlink(old_ini_file);
}
}
}

static char * get_config_path(void)
{
return g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, INI_FILE, NULL);
return g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), "tilp.ini", NULL);
}

#if defined(__LINUX__) || defined(__BSD__) || defined(__MACOSX__)
# define OLD_INI_FILE ".tilp"
#elif defined(__WIN32__)
# define OLD_INI_FILE "tilp.ini"
#endif

/* Chech whether a RC file exists */
int tilp_config_exist(void)
{
#if defined(__LINUX__) || defined(__BSD__) || defined(__MACOSX__)
char * ini_file;
int retval;

ini_file = get_config_path();

retval = !access(ini_file, F_OK);
g_free(ini_file);
return retval;
#elif defined(__WIN32__)
char * ini_file;
char* old_ini_file;
char* ini_file;
int result1, result2, retval;
old_ini_file = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, OLD_INI_FILE, NULL);
ini_file = get_config_path();
deprecated_config_path(old_ini_file, ini_file);
free(old_ini_file);

#if defined(__WIN32__)
// On Windows, there can be two config files:
// * in the install dir (deprecated, as it does not work well with the UAC);
old_ini_file = g_strconcat(inst_paths.base_dir, G_DIR_SEPARATOR_S, INI_FILE, NULL);
result1 = !access(old_ini_file, F_OK);
// * per-user config files (which the *nix versions have been using for ages).
ini_file = get_config_path();
result2 = !access(ini_file, F_OK);

if (result1)
{
// A config file exists at the old location
if (!result2)
{
// No config file exists at the new location, bad.
// Create it.
FILE *in;
FILE *out;

in = fopen(old_ini_file, "rb");
out = fopen(ini_file, "wb");
if (in && out)
{
int c;

while ((c = fgetc(in)) != EOF)
{
fputc(c, out);
}
fclose(out);
fclose(in);
// A config file now exists at the new location.
// Delete the old file.
unlink(old_ini_file);
retval = 1;
}
else
{
// There's a problem...
// Trigger failure in the callers.
retval = 0;
}
}
else
{
// A config file exists at the new location (even if a config file exists at the old location), good.
retval = 1;
}
}
else if (result2)
{
// A config file exists at the new location, good.
retval = 1;
}
else
{
// No config file at either location.
retval = 0;
}
// * in the install dir (deprecated, as it does not work well with the UAC);
old_ini_file = g_strconcat(inst_paths.base_dir, G_DIR_SEPARATOR_S, OLD_INI_FILE, NULL);
deprecated_config_path(old_ini_file, ini_file);
g_free(old_ini_file);
#endif
retval = !access(ini_file, F_OK);
g_free(ini_file);
return retval;
#endif
}


Expand Down Expand Up @@ -379,7 +357,7 @@ int tilp_config_write(void)
f = fopen(ini_file, "wt");
if (f == NULL)
{
gif->msg_box1(_("Error"), _("Unable to write the config file (~/.tilp or ~/tilp.ini).\n"));
gif->msg_box1(_("Error"), _("Unable to write the config file ($XDG_CONFIG_HOME/tilp.ini).\n"));
ret = -1;
goto exit;
}
Expand Down
7 changes: 0 additions & 7 deletions tilp/trunk/src/tilp_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@

#include <stdio.h>

/* Paths */
#if defined(__LINUX__) || defined(__BSD__) || defined(__MACOSX__)
# define INI_FILE "/.tilp"
#elif defined(__WIN32__)
# define INI_FILE "tilp.ini"
#endif

/* Temporary filenames (used by cb_calc.c) */
#define TMPFILE_BACKUP "tilp.backup"
#define TMPFILE_ROMDUMP "tilp.romdump"
Expand Down