xdg: implement a better way

Brief about changes:
- add get_irssi_cache_dir()
- add get_irssi_runtime_dir()
- remove is_xdg_supported()

About the proposed implementation:
- use XDG only when the user has the XDG_CONFIG_HOME/irssi directory
- irssi_dir would equal XDG_DATA_HOME/irssi or ~/.irssi, depending on
whether XDG is enabled or not, respectively.
- if xdg is enabled, variables irssi_cache, irssi_runtime, would equal
their respective XDG paths.
- if xdg is *not* enabled, variables irssi_cache, irssi_runtime, would
equal irssi_dir

Notes for future code:
- functions that use the irssi config file would use get_irssi_config()
- functions that use the irssi data directory would use get_irssi_dir()
- functions that use the irssi cache directory would use
get_irssi_cache_dir()
- functions that use the irssi runtime directory would use
get_irssi_runtime_dir()

Stuff that are still missing for complete implementation:
- add checks to ensure the new referenced directories are present
- docs
This commit is contained in:
altffour 2021-01-07 13:26:45 +03:00
commit 4059827741
No known key found for this signature in database
GPG key ID: B4ADFA86EDF5CCE9
9 changed files with 50 additions and 75 deletions

View file

@ -204,13 +204,7 @@ sub get_names {
$xname =~ s/\W/_/g; $xname =~ s/\W/_/g;
my $pname = "${xname}::"; my $pname = "${xname}::";
if ($xname ne $sname || $sname =~ /_/) { if ($xname ne $sname || $sname =~ /_/) {
my $dir = ""; my $dir = Irssi::get_irssi_dir()."/scripts/";
if (Irssi::is_xdg_supported()) {
$dir = Irssi::get_runtime_dir()."/scripts/";
}
else {
$dir = Irssi::get_irssi_dir()."/scripts/";
}
if ($db && exists $db->{"$sname.pl"}) { if ($db && exists $db->{"$sname.pl"}) {
# $found = 1; # $found = 1;
} elsif (-e $dir.$plname || -e $dir."$sname.pl" || -e $dir."autorun/$sname.pl") { } elsif (-e $dir.$plname || -e $dir."$sname.pl" || -e $dir."autorun/$sname.pl") {

View file

@ -55,9 +55,11 @@ int g_input_add(GIOChannel *source, int condition,
int g_input_add_full(GIOChannel *source, int priority, int condition, int g_input_add_full(GIOChannel *source, int priority, int condition,
GInputFunction function, void *data); GInputFunction function, void *data);
/* return if xdg is being used */ /* return full path for irssi runtime directory */
int is_xdg_supported(); const char *get_irssi_runtime_dir(void);
/* return full path for irssi folder */ /* return full path for irssi cache directory */
const char *get_irssi_cache_dir(void);
/* return full path for irssi data directory */
const char *get_irssi_dir(void); const char *get_irssi_dir(void);
/* return full path for irssi config */ /* return full path for irssi config */
const char *get_irssi_config(void); const char *get_irssi_config(void);

View file

@ -65,18 +65,25 @@ void log_away_deinit(void);
void wcwidth_wrapper_init(void); void wcwidth_wrapper_init(void);
void wcwidth_wrapper_deinit(void); void wcwidth_wrapper_deinit(void);
int xdg_support;
int irssi_gui; int irssi_gui;
int irssi_init_finished; int irssi_init_finished;
int sighup_received; int sighup_received;
time_t client_start_time; time_t client_start_time;
static char *irssi_dir, *irssi_config_file; static char *irssi_dir, /* XDG_DATA_HOME or ~/.irssi */
*irssi_config_file, /* XDG_CONFIG_HOME/irssi/config or ~/.irssi/config */
*irssi_cache_dir, /* XDG_CACHE_HOME/irssi or ~/.irssi */
*irssi_runtime_dir; /* XDG_RUNTIME_HOME/irssi or ~/.irssi */
static GSList *dialog_type_queue, *dialog_text_queue; static GSList *dialog_type_queue, *dialog_text_queue;
int is_xdg_supported() const char *get_irssi_runtime_dir(void)
{ {
return xdg_support; return irssi_runtime_dir;
}
const char *get_irssi_cache_dir(void)
{
return irssi_cache_dir;
} }
const char *get_irssi_dir(void) const char *get_irssi_dir(void)
@ -84,7 +91,7 @@ const char *get_irssi_dir(void)
return irssi_dir; return irssi_dir;
} }
/* return full path for ~/.irssi/config */ /* return full path for irssi config */
const char *get_irssi_config(void) const char *get_irssi_config(void)
{ {
return irssi_config_file; return irssi_config_file;
@ -199,22 +206,27 @@ void core_preinit(const char *path)
const char *home; const char *home;
char *str; char *str;
int len; int len;
int use_xdg = 0;
xdg_support = 0;
if (irssi_dir == NULL) { if (irssi_dir == NULL) {
/* check if %XDG_CONFIG_HOME/irssi exists and default to it */ /* check if %XDG_CONFIG_HOME/irssi exists and use to it */
char *dirp = g_build_filename(g_get_user_config_dir(), "irssi", NULL); char *dirp = g_build_filename(g_get_user_config_dir(), "irssi", NULL);
if (stat(dirp, &statbuf) == 0) { if (stat(dirp, &statbuf) == 0) {
xdg_support = 1; use_xdg = 1;
irssi_dir = dirp; irssi_dir = g_build_filename(g_get_user_data_dir(), "irssi", NULL);
irssi_cache_dir = g_build_filename(g_get_user_cache_dir(), "irssi", NULL);
irssi_runtime_dir =
g_build_filename(g_get_user_runtime_dir(), "irssi", NULL);
} else { /* fallback to non-xdg location */ } else { /* fallback to non-xdg location */
g_free(dirp); g_free(dirp);
home = g_get_home_dir(); home = g_get_home_dir();
if (home == NULL) if (home == NULL)
home = "."; home = ".";
irssi_dir = g_strdup_printf(IRSSI_DIR_FULL, home); irssi_dir = g_strdup_printf(IRSSI_DIR_FULL, home);
xdg_support = 0; /* all special XDG paths are equal to irssi_dir if XDG is not
* supported */
irssi_cache_dir = irssi_dir;
irssi_runtime_dir = irssi_dir;
} }
} else { } else {
str = irssi_dir; str = irssi_dir;
@ -225,10 +237,9 @@ void core_preinit(const char *path)
irssi_dir[len-1] = '\0'; irssi_dir[len-1] = '\0';
} }
if (irssi_config_file == NULL) if (irssi_config_file == NULL)
if (is_xdg_supported()) irssi_config_file = use_xdg ? g_build_filename(g_get_user_config_dir(), "irssi",
irssi_config_file = g_build_filename(irssi_dir, IRSSI_HOME_CONFIG, NULL); IRSSI_HOME_CONFIG, NULL) :
else g_strdup_printf("%s/" IRSSI_HOME_CONFIG, irssi_dir);
irssi_config_file = g_strdup_printf("%s/" IRSSI_HOME_CONFIG, irssi_dir);
else { else {
str = irssi_config_file; str = irssi_config_file;
irssi_config_file = fix_path(str); irssi_config_file = fix_path(str);

View file

@ -111,10 +111,7 @@ void log_away_init(void)
away_filepos = 0; away_filepos = 0;
away_msgs = 0; away_msgs = 0;
if (is_xdg_supported()) awaylog_file = g_strconcat(get_irssi_cache_dir(), "/away.log", NULL);
awaylog_file = g_build_filename(g_get_user_cache_dir(), "irssi", "away.log", NULL);
else
awaylog_file = g_strconcat(get_irssi_dir(), "/away.log", NULL);
settings_add_str("log", "awaylog_file", awaylog_file); settings_add_str("log", "awaylog_file", awaylog_file);
g_free(awaylog_file); g_free(awaylog_file);
settings_add_level("log", "awaylog_level", "msgs hilight"); settings_add_level("log", "awaylog_level", "msgs hilight");

View file

@ -115,9 +115,6 @@ static GModule *module_open(const char *name, int *found)
path = g_strdup(name); path = g_strdup(name);
else { else {
/* first try from home dir */ /* first try from home dir */
if (is_xdg_supported())
str = g_build_filename(g_get_user_data_dir(), "irssi", "modules", NULL);
else
str = g_strdup_printf("%s/modules", get_irssi_dir()); str = g_strdup_printf("%s/modules", get_irssi_dir());
path = g_module_build_path(str, name); path = g_module_build_path(str, name);
g_free(str); g_free(str);

View file

@ -932,10 +932,6 @@ THEME_REC *theme_load(const char *setname)
theme = theme_find(name); theme = theme_find(name);
/* check home dir */ /* check home dir */
if (is_xdg_supported())
fname = g_build_filename(g_get_user_data_dir(), "irssi",
g_strdup_printf("%s.theme", name), NULL);
else
fname = g_strdup_printf("%s/%s.theme", get_irssi_dir(), name, NULL); fname = g_strdup_printf("%s/%s.theme", get_irssi_dir(), name, NULL);
if (stat(fname, &statbuf) != 0) { if (stat(fname, &statbuf) != 0) {
/* check global config dir */ /* check global config dir */
@ -1261,9 +1257,6 @@ static void theme_save(THEME_REC *theme, int save_all)
basename = g_path_get_basename(theme->path); basename = g_path_get_basename(theme->path);
/* always save the theme */ /* always save the theme */
if (is_xdg_supported())
path = g_build_filename(g_get_user_data_dir(), "irssi", basename, NULL);
else
path = g_strdup_printf("%s/%s", get_irssi_dir(), basename, NULL); path = g_strdup_printf("%s/%s", get_irssi_dir(), basename, NULL);
ok = config_write(config, path, 0660) == 0; ok = config_write(config, path, 0660) == 0;
g_free(basename); g_free(basename);
@ -1420,10 +1413,6 @@ void themes_reload(void)
/* first there's default theme.. */ /* first there's default theme.. */
current_theme = theme_load("default"); current_theme = theme_load("default");
if (current_theme == NULL) { if (current_theme == NULL) {
if (is_xdg_supported())
fname =
g_build_filename(g_get_user_data_dir(), "irssi", "default.theme", NULL);
else
fname = g_strdup_printf("%s/default.theme", get_irssi_dir()); fname = g_strdup_printf("%s/default.theme", get_irssi_dir());
current_theme = theme_create(fname, "default"); current_theme = theme_create(fname, "default");
current_theme->default_color = -1; current_theme->default_color = -1;

View file

@ -599,17 +599,17 @@ PPCODE:
XPUSHs(sv_2mortal(new_pv(ret))); XPUSHs(sv_2mortal(new_pv(ret)));
g_free_not_null(ret); g_free_not_null(ret);
bool char *
is_xdg_supported() get_runtime_dir()
CODE: CODE:
RETVAL = (bool) is_xdg_supported(); RETVAL = (char *) get_irssi_runtime_dir();
OUTPUT: OUTPUT:
RETVAL RETVAL
char * char *
get_runtime_dir() get_cache_dir()
CODE: CODE:
RETVAL = (char *) g_build_filename(g_get_user_runtime_dir(), "irssi", NULL); RETVAL = (char *) get_irssi_cache_dir();
OUTPUT: OUTPUT:
RETVAL RETVAL

View file

@ -390,15 +390,7 @@ char *perl_script_get_path(const char *name)
file = IS_PERL_SCRIPT(name) ? g_strdup(name) : file = IS_PERL_SCRIPT(name) ? g_strdup(name) :
g_strdup_printf("%s.pl", name); g_strdup_printf("%s.pl", name);
/* check if xdg paths are being used, if so locate scripts based on that */ /* check from IRSSI_DATA_DIR/scripts/ */
if (is_xdg_supported()) {
path = g_build_filename(g_get_user_data_dir(), "irssi", "scripts", file, NULL);
if (stat(path, &statbuf) != 0) {
g_free(path);
path = NULL;
}
} else {
/* check from ~/.irssi/scripts/ */
path = g_strdup_printf("%s/scripts/%s", get_irssi_dir(), file); path = g_strdup_printf("%s/scripts/%s", get_irssi_dir(), file);
if (stat(path, &statbuf) != 0) { if (stat(path, &statbuf) != 0) {
/* check from SCRIPTDIR */ /* check from SCRIPTDIR */
@ -409,7 +401,6 @@ char *perl_script_get_path(const char *name)
path = NULL; path = NULL;
} }
} }
}
g_free(file); g_free(file);
return path; return path;
} }
@ -433,9 +424,6 @@ void perl_scripts_autorun(void)
struct stat statbuf; struct stat statbuf;
char *path, *fname; char *path, *fname;
if (is_xdg_supported())
path = g_build_filename(g_get_user_data_dir(), "irssi", "scripts", "autorun", NULL);
else
path = g_strdup_printf("%s/scripts/autorun", get_irssi_dir()); path = g_strdup_printf("%s/scripts/autorun", get_irssi_dir());
dirp = opendir(path); dirp = opendir(path);
if (dirp == NULL) { if (dirp == NULL) {

View file

@ -209,9 +209,6 @@ static void sig_complete_load(GList **list, WINDOW_REC *window,
return; return;
/* completing filename parameter for /SCRIPT LOAD */ /* completing filename parameter for /SCRIPT LOAD */
if (is_xdg_supported())
user_dir = g_build_filename(g_get_user_data_dir(), "scripts", NULL);
else
user_dir = g_strdup_printf("%s/scripts", get_irssi_dir()); user_dir = g_strdup_printf("%s/scripts", get_irssi_dir());
*list = filename_complete(word, user_dir); *list = filename_complete(word, user_dir);
*list = g_list_concat(*list, filename_complete(word, SCRIPTDIR)); *list = g_list_concat(*list, filename_complete(word, SCRIPTDIR));