From ef942bd27a759b3016c473abba8066a8ddcb6ffe Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 16:22:15 +0100 Subject: Add config/files.c --- Makefile.am | 2 + src/common.c | 38 --------------- src/common.h | 2 - src/config/accounts.c | 1 + src/config/files.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++ src/config/files.h | 44 +++++++++++++++++ src/config/preferences.c | 1 + src/config/scripts.c | 1 + src/config/theme.c | 5 +- src/config/tlscerts.c | 1 + src/log.c | 1 + src/otr/otr.c | 1 + src/pgp/gpg.c | 1 + src/plugins/plugins.c | 1 + src/plugins/settings.c | 1 + src/plugins/themes.c | 1 + src/profanity.c | 47 +----------------- src/ui/tray.c | 1 + src/xmpp/capabilities.c | 1 + 19 files changed, 187 insertions(+), 87 deletions(-) create mode 100644 src/config/files.c create mode 100644 src/config/files.h diff --git a/Makefile.am b/Makefile.am index 55723688..00e66081 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,6 +40,7 @@ core_sources = \ src/tools/p_sha1.h src/tools/p_sha1.c \ src/tools/autocomplete.c src/tools/autocomplete.h \ src/tools/tinyurl.c src/tools/tinyurl.h \ + src/config/files.c src/config/files.h \ src/config/conflists.c src/config/conflists.h \ src/config/accounts.c src/config/accounts.h \ src/config/tlscerts.c src/config/tlscerts.h \ @@ -78,6 +79,7 @@ unittest_sources = \ src/tools/tinyurl.c src/tools/tinyurl.h \ src/config/accounts.h \ src/config/account.c src/config/account.h \ + src/config/files.c src/config/files.h \ src/config/tlscerts.c src/config/tlscerts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ diff --git a/src/common.c b/src/common.c index b7868c79..98466231 100644 --- a/src/common.c +++ b/src/common.c @@ -449,44 +449,6 @@ contact_presence_from_resource_presence(resource_presence_t resource_presence) } } -gchar* -xdg_get_config_home(void) -{ - gchar *xdg_config_home = getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) - g_strstrip(xdg_config_home); - - if (xdg_config_home && (strcmp(xdg_config_home, "") != 0)) { - return strdup(xdg_config_home); - } else { - GString *default_path = g_string_new(getenv("HOME")); - g_string_append(default_path, "/.config"); - gchar *result = strdup(default_path->str); - g_string_free(default_path, TRUE); - - return result; - } -} - -gchar* -xdg_get_data_home(void) -{ - gchar *xdg_data_home = getenv("XDG_DATA_HOME"); - if (xdg_data_home) - g_strstrip(xdg_data_home); - - if (xdg_data_home && (strcmp(xdg_data_home, "") != 0)) { - return strdup(xdg_data_home); - } else { - GString *default_path = g_string_new(getenv("HOME")); - g_string_append(default_path, "/.local/share"); - gchar *result = strdup(default_path->str); - g_string_free(default_path, TRUE); - - return result; - } -} - char* create_unique_id(char *prefix) { diff --git a/src/common.h b/src/common.h index 5c39f075..678a12ff 100644 --- a/src/common.h +++ b/src/common.h @@ -110,8 +110,6 @@ int utf8_display_len(const char *const str); char* prof_getline(FILE *stream); char* release_get_latest(void); gboolean release_is_new(char *found_version); -gchar* xdg_get_config_home(void); -gchar* xdg_get_data_home(void); gboolean valid_resource_presence_string(const char *const str); const char* string_from_resource_presence(resource_presence_t presence); diff --git a/src/config/accounts.c b/src/config/accounts.c index 7f852e6f..84671b74 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -42,6 +42,7 @@ #include "common.h" #include "log.h" +#include "config/files.h" #include "config/account.h" #include "config/conflists.h" #include "tools/autocomplete.h" diff --git a/src/config/files.c b/src/config/files.c new file mode 100644 index 00000000..5f57f691 --- /dev/null +++ b/src/config/files.c @@ -0,0 +1,124 @@ +/* + * files.c + * + * Copyright (C) 2012 - 2016 James Booth + * + * This file is part of Profanity. + * + * Profanity 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 3 of the License, or + * (at your option) any later version. + * + * Profanity 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 Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ +#include "config.h" + +#include +#include + +#include + +#include "common.h" +#include "log.h" +#include "config/files.h" + +void +files_create_directories(void) +{ + gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_data = xdg_get_data_home(); + + GString *themes_dir = g_string_new(xdg_config); + g_string_append(themes_dir, "/profanity/themes"); + GString *icons_dir = g_string_new(xdg_config); + g_string_append(icons_dir, "/profanity/icons"); + GString *chatlogs_dir = g_string_new(xdg_data); + g_string_append(chatlogs_dir, "/profanity/chatlogs"); + GString *logs_dir = g_string_new(xdg_data); + g_string_append(logs_dir, "/profanity/logs"); + GString *plugins_dir = g_string_new(xdg_data); + g_string_append(plugins_dir, "/profanity/plugins"); + + if (!mkdir_recursive(themes_dir->str)) { + log_error("Error while creating directory %s", themes_dir->str); + } + if (!mkdir_recursive(icons_dir->str)) { + log_error("Error while creating directory %s", icons_dir->str); + } + if (!mkdir_recursive(chatlogs_dir->str)) { + log_error("Error while creating directory %s", chatlogs_dir->str); + } + if (!mkdir_recursive(logs_dir->str)) { + log_error("Error while creating directory %s", logs_dir->str); + } + if (!mkdir_recursive(plugins_dir->str)) { + log_error("Error while creating directory %s", plugins_dir->str); + } + + g_string_free(themes_dir, TRUE); + g_string_free(icons_dir, TRUE); + g_string_free(chatlogs_dir, TRUE); + g_string_free(logs_dir, TRUE); + g_string_free(plugins_dir, TRUE); + + g_free(xdg_config); + g_free(xdg_data); +} + +gchar* +xdg_get_config_home(void) +{ + gchar *xdg_config_home = getenv("XDG_CONFIG_HOME"); + if (xdg_config_home) + g_strstrip(xdg_config_home); + + if (xdg_config_home && (strcmp(xdg_config_home, "") != 0)) { + return strdup(xdg_config_home); + } else { + GString *default_path = g_string_new(getenv("HOME")); + g_string_append(default_path, "/.config"); + gchar *result = strdup(default_path->str); + g_string_free(default_path, TRUE); + + return result; + } +} + +gchar* +xdg_get_data_home(void) +{ + gchar *xdg_data_home = getenv("XDG_DATA_HOME"); + if (xdg_data_home) + g_strstrip(xdg_data_home); + + if (xdg_data_home && (strcmp(xdg_data_home, "") != 0)) { + return strdup(xdg_data_home); + } else { + GString *default_path = g_string_new(getenv("HOME")); + g_string_append(default_path, "/.local/share"); + gchar *result = strdup(default_path->str); + g_string_free(default_path, TRUE); + + return result; + } +} diff --git a/src/config/files.h b/src/config/files.h new file mode 100644 index 00000000..431229f7 --- /dev/null +++ b/src/config/files.h @@ -0,0 +1,44 @@ +/* + * files.h + * + * Copyright (C) 2012 - 2016 James Booth + * + * This file is part of Profanity. + * + * Profanity 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 3 of the License, or + * (at your option) any later version. + * + * Profanity 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 Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#ifndef CONFIG_FILES_H +#define CONFIG_FILES_H + +#include + +gchar* xdg_get_config_home(void); +gchar* xdg_get_data_home(void); +void files_create_directories(void); + +#endif diff --git a/src/config/preferences.c b/src/config/preferences.c index 4377cdfc..3fd32586 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -45,6 +45,7 @@ #include "log.h" #include "preferences.h" #include "tools/autocomplete.h" +#include "config/files.h" #include "config/conflists.h" // preference groups refer to the sections in .profrc, for example [ui] diff --git a/src/config/scripts.c b/src/config/scripts.c index 14f05f29..91dbe4df 100644 --- a/src/config/scripts.c +++ b/src/config/scripts.c @@ -42,6 +42,7 @@ #include "common.h" #include "log.h" +#include "config/files.h" #include "command/cmd_defs.h" #include "ui/ui.h" #include "ui/window_list.h" diff --git a/src/config/theme.c b/src/config/theme.c index 0ba9c786..fb208279 100644 --- a/src/config/theme.c +++ b/src/config/theme.c @@ -47,8 +47,9 @@ #include "common.h" #include "log.h" -#include "theme.h" -#include "preferences.h" +#include "config/files.h" +#include "config/theme.h" +#include "config/preferences.h" static GString *theme_loc; static GKeyFile *theme; diff --git a/src/config/tlscerts.c b/src/config/tlscerts.c index b51f5ff9..03a8bd97 100644 --- a/src/config/tlscerts.c +++ b/src/config/tlscerts.c @@ -40,6 +40,7 @@ #include "log.h" #include "common.h" +#include "config/files.h" #include "config/tlscerts.h" #include "tools/autocomplete.h" diff --git a/src/log.c b/src/log.c index 363c1be7..e0a50055 100644 --- a/src/log.c +++ b/src/log.c @@ -45,6 +45,7 @@ #include "log.h" #include "common.h" +#include "config/files.h" #include "config/preferences.h" #include "xmpp/xmpp.h" diff --git a/src/otr/otr.c b/src/otr/otr.c index 343bdcbc..3e4a90fc 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -40,6 +40,7 @@ #include "log.h" #include "config/preferences.h" +#include "config/files.h" #include "otr/otr.h" #include "otr/otrlib.h" #include "ui/ui.h" diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index fb1f2db1..c2b81c67 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -47,6 +47,7 @@ #include "log.h" #include "common.h" #include "pgp/gpg.h" +#include "config/files.h" #include "tools/autocomplete.h" #include "ui/ui.h" diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 8e9b2d37..2f176ac7 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -38,6 +38,7 @@ #include "log.h" #include "config.h" #include "common.h" +#include "config/files.h" #include "config/preferences.h" #include "plugins/callbacks.h" #include "plugins/autocompleters.h" diff --git a/src/plugins/settings.c b/src/plugins/settings.c index df32b379..4ffe85e5 100644 --- a/src/plugins/settings.c +++ b/src/plugins/settings.c @@ -40,6 +40,7 @@ #include "common.h" #include "config/theme.h" +#include "config/files.h" static GKeyFile *settings; diff --git a/src/plugins/themes.c b/src/plugins/themes.c index cfa718cc..d41ba283 100644 --- a/src/plugins/themes.c +++ b/src/plugins/themes.c @@ -37,6 +37,7 @@ #include "common.h" #include "config/theme.h" +#include "config/files.h" static GKeyFile *themes; diff --git a/src/profanity.c b/src/profanity.c index d8f546a3..3cdec902 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -51,6 +51,7 @@ #include "profanity.h" #include "common.h" #include "log.h" +#include "config/files.h" #include "config/tlscerts.h" #include "config/accounts.h" #include "config/preferences.h" @@ -81,7 +82,6 @@ static void _init(char *log_level); static void _shutdown(void); -static void _create_directories(void); static void _connect_default(const char * const account); static gboolean cont = TRUE; @@ -164,7 +164,7 @@ _init(char *log_level) exit(1); } pthread_mutex_lock(&lock); - _create_directories(); + files_create_directories(); log_level_t prof_log_level = log_level_from_string(log_level); prefs_load(); log_init(prof_log_level); @@ -244,46 +244,3 @@ _shutdown(void) ui_close(); prefs_close(); } - -static void -_create_directories(void) -{ - gchar *xdg_config = xdg_get_config_home(); - gchar *xdg_data = xdg_get_data_home(); - - GString *themes_dir = g_string_new(xdg_config); - g_string_append(themes_dir, "/profanity/themes"); - GString *icons_dir = g_string_new(xdg_config); - g_string_append(icons_dir, "/profanity/icons"); - GString *chatlogs_dir = g_string_new(xdg_data); - g_string_append(chatlogs_dir, "/profanity/chatlogs"); - GString *logs_dir = g_string_new(xdg_data); - g_string_append(logs_dir, "/profanity/logs"); - GString *plugins_dir = g_string_new(xdg_data); - g_string_append(plugins_dir, "/profanity/plugins"); - - if (!mkdir_recursive(themes_dir->str)) { - log_error("Error while creating directory %s", themes_dir->str); - } - if (!mkdir_recursive(icons_dir->str)) { - log_error("Error while creating directory %s", icons_dir->str); - } - if (!mkdir_recursive(chatlogs_dir->str)) { - log_error("Error while creating directory %s", chatlogs_dir->str); - } - if (!mkdir_recursive(logs_dir->str)) { - log_error("Error while creating directory %s", logs_dir->str); - } - if (!mkdir_recursive(plugins_dir->str)) { - log_error("Error while creating directory %s", plugins_dir->str); - } - - g_string_free(themes_dir, TRUE); - g_string_free(icons_dir, TRUE); - g_string_free(chatlogs_dir, TRUE); - g_string_free(logs_dir, TRUE); - g_string_free(plugins_dir, TRUE); - - g_free(xdg_config); - g_free(xdg_data); -} diff --git a/src/ui/tray.c b/src/ui/tray.c index 8471f039..94408dae 100644 --- a/src/ui/tray.c +++ b/src/ui/tray.c @@ -41,6 +41,7 @@ #include "log.h" #include "config/preferences.h" +#include "config/files.h" #include "ui/tray.h" #include "ui/window_list.h" diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index 4d9c0a36..bfc22120 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -55,6 +55,7 @@ #include "common.h" #include "log.h" #include "plugins/plugins.h" +#include "config/files.h" #include "xmpp/xmpp.h" #include "xmpp/stanza.h" #include "xmpp/form.h" -- cgit 1.4.1-2-gfad0