From e7bfaa643fd8f57c50390194b0c804cacd5a0de4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 16:06:19 +0100 Subject: [PATCH 1/7] Move autoaway check --- src/profanity.c | 142 +------------------------------ src/xmpp/session.c | 141 ++++++++++++++++++++++++++++++ src/xmpp/session.h | 3 + tests/unittests/xmpp/stub_xmpp.c | 2 + 4 files changed, 149 insertions(+), 139 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 7d90c1c9..d8f546a3 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -63,6 +63,7 @@ #include "ui/ui.h" #include "ui/window_list.h" #include "xmpp/resource.h" +#include "xmpp/session.h" #include "xmpp/xmpp.h" #include "xmpp/muc.h" #include "xmpp/chat_session.h" @@ -78,24 +79,11 @@ #include "pgp/gpg.h" #endif - -static void _check_autoaway(void); static void _init(char *log_level); static void _shutdown(void); static void _create_directories(void); static void _connect_default(const char * const account); -typedef enum { - ACTIVITY_ST_ACTIVE, - ACTIVITY_ST_IDLE, - ACTIVITY_ST_AWAY, - ACTIVITY_ST_XA, -} activity_state_t; - -activity_state_t activity_state; -resource_presence_t saved_presence; -char *saved_status; - static gboolean cont = TRUE; static gboolean force_quit = FALSE; @@ -110,13 +98,12 @@ prof_run(char *log_level, char *account_name) log_info("Starting main event loop"); - activity_state = ACTIVITY_ST_ACTIVE; - saved_status = NULL; + session_init_activity(); char *line = NULL; while(cont && !force_quit) { log_stderr_handler(); - _check_autoaway(); + session_check_autoaway(); line = inp_readline(); if (line) { @@ -163,126 +150,6 @@ _connect_default(const char *const account) } } -static void -_check_autoaway(void) -{ - jabber_conn_status_t conn_status = connection_get_status(); - if (conn_status != JABBER_CONNECTED) { - return; - } - - char *mode = prefs_get_string(PREF_AUTOAWAY_MODE); - gboolean check = prefs_get_boolean(PREF_AUTOAWAY_CHECK); - gint away_time = prefs_get_autoaway_time(); - gint xa_time = prefs_get_autoxa_time(); - int away_time_ms = away_time * 60000; - int xa_time_ms = xa_time * 60000; - - char *account = session_get_account_name(); - resource_presence_t curr_presence = accounts_get_last_presence(account); - char *curr_status = accounts_get_last_status(account); - - unsigned long idle_ms = ui_get_idle_time(); - - switch (activity_state) { - case ACTIVITY_ST_ACTIVE: - if (idle_ms >= away_time_ms) { - if (g_strcmp0(mode, "away") == 0) { - if ((curr_presence == RESOURCE_ONLINE) || (curr_presence == RESOURCE_CHAT) || (curr_presence == RESOURCE_DND)) { - activity_state = ACTIVITY_ST_AWAY; - - // save current presence - saved_presence = curr_presence; - if (saved_status) { - free(saved_status); - } - saved_status = curr_status; - - // send away presence with last activity - char *message = prefs_get_string(PREF_AUTOAWAY_MESSAGE); - if (prefs_get_boolean(PREF_LASTACTIVITY)) { - cl_ev_presence_send(RESOURCE_AWAY, message, idle_ms / 1000); - } else { - cl_ev_presence_send(RESOURCE_AWAY, message, 0); - } - - int pri = accounts_get_priority_for_presence_type(account, RESOURCE_AWAY); - if (message) { - cons_show("Idle for %d minutes, status set to away (priority %d), \"%s\".", away_time, pri, message); - } else { - cons_show("Idle for %d minutes, status set to away (priority %d).", away_time, pri); - } - prefs_free_string(message); - - title_bar_set_presence(CONTACT_AWAY); - } - } else if (g_strcmp0(mode, "idle") == 0) { - activity_state = ACTIVITY_ST_IDLE; - - // send current presence with last activity - cl_ev_presence_send(curr_presence, curr_status, idle_ms / 1000); - } - } - break; - case ACTIVITY_ST_IDLE: - if (check && (idle_ms < away_time_ms)) { - activity_state = ACTIVITY_ST_ACTIVE; - - cons_show("No longer idle."); - - // send current presence without last activity - cl_ev_presence_send(curr_presence, curr_status, 0); - } - break; - case ACTIVITY_ST_AWAY: - if (xa_time_ms > 0 && (idle_ms >= xa_time_ms)) { - activity_state = ACTIVITY_ST_XA; - - // send extended away presence with last activity - char *message = prefs_get_string(PREF_AUTOXA_MESSAGE); - if (prefs_get_boolean(PREF_LASTACTIVITY)) { - cl_ev_presence_send(RESOURCE_XA, message, idle_ms / 1000); - } else { - cl_ev_presence_send(RESOURCE_XA, message, 0); - } - - int pri = accounts_get_priority_for_presence_type(account, RESOURCE_XA); - if (message) { - cons_show("Idle for %d minutes, status set to xa (priority %d), \"%s\".", xa_time, pri, message); - } else { - cons_show("Idle for %d minutes, status set to xa (priority %d).", xa_time, pri); - } - prefs_free_string(message); - - title_bar_set_presence(CONTACT_XA); - } else if (check && (idle_ms < away_time_ms)) { - activity_state = ACTIVITY_ST_ACTIVE; - - cons_show("No longer idle."); - - // send saved presence without last activity - cl_ev_presence_send(saved_presence, saved_status, 0); - contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence); - title_bar_set_presence(contact_pres); - } - break; - case ACTIVITY_ST_XA: - if (check && (idle_ms < away_time_ms)) { - activity_state = ACTIVITY_ST_ACTIVE; - - cons_show("No longer idle."); - - // send saved presence without last activity - cl_ev_presence_send(saved_presence, saved_status, 0); - contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence); - title_bar_set_presence(contact_pres); - } - break; - } - - prefs_free_string(mode); -} - static void _init(char *log_level) { @@ -376,9 +243,6 @@ _shutdown(void) cmd_uninit(); ui_close(); prefs_close(); - if (saved_status) { - free(saved_status); - } } static void diff --git a/src/xmpp/session.c b/src/xmpp/session.c index 5c9c3b4c..9fe448f8 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -44,6 +44,7 @@ #include "config/preferences.h" #include "plugins/plugins.h" #include "event/server_events.h" +#include "event/client_events.h" #include "xmpp/bookmark.h" #include "xmpp/blocking.h" #include "xmpp/connection.h" @@ -74,7 +75,17 @@ static struct { char *tls_policy; } saved_details; +typedef enum { + ACTIVITY_ST_ACTIVE, + ACTIVITY_ST_IDLE, + ACTIVITY_ST_AWAY, + ACTIVITY_ST_XA, +} activity_state_t; + static GTimer *reconnect_timer; +static activity_state_t activity_state; +static resource_presence_t saved_presence; +static char *saved_status; static void _session_reconnect(void); @@ -225,6 +236,9 @@ session_shutdown(void) presence_clear_sub_requests(); connection_shutdown(); + if (saved_status) { + free(saved_status); + } } void @@ -343,6 +357,133 @@ session_lost_connection(void) presence_clear_sub_requests(); } +void +session_init_activity(void) +{ + activity_state = ACTIVITY_ST_ACTIVE; + saved_status = NULL; +} + +void +session_check_autoaway(void) +{ + jabber_conn_status_t conn_status = connection_get_status(); + if (conn_status != JABBER_CONNECTED) { + return; + } + + char *mode = prefs_get_string(PREF_AUTOAWAY_MODE); + gboolean check = prefs_get_boolean(PREF_AUTOAWAY_CHECK); + gint away_time = prefs_get_autoaway_time(); + gint xa_time = prefs_get_autoxa_time(); + int away_time_ms = away_time * 60000; + int xa_time_ms = xa_time * 60000; + + char *account = session_get_account_name(); + resource_presence_t curr_presence = accounts_get_last_presence(account); + char *curr_status = accounts_get_last_status(account); + + unsigned long idle_ms = ui_get_idle_time(); + + switch (activity_state) { + case ACTIVITY_ST_ACTIVE: + if (idle_ms >= away_time_ms) { + if (g_strcmp0(mode, "away") == 0) { + if ((curr_presence == RESOURCE_ONLINE) || (curr_presence == RESOURCE_CHAT) || (curr_presence == RESOURCE_DND)) { + activity_state = ACTIVITY_ST_AWAY; + + // save current presence + saved_presence = curr_presence; + if (saved_status) { + free(saved_status); + } + saved_status = curr_status; + + // send away presence with last activity + char *message = prefs_get_string(PREF_AUTOAWAY_MESSAGE); + if (prefs_get_boolean(PREF_LASTACTIVITY)) { + cl_ev_presence_send(RESOURCE_AWAY, message, idle_ms / 1000); + } else { + cl_ev_presence_send(RESOURCE_AWAY, message, 0); + } + + int pri = accounts_get_priority_for_presence_type(account, RESOURCE_AWAY); + if (message) { + cons_show("Idle for %d minutes, status set to away (priority %d), \"%s\".", away_time, pri, message); + } else { + cons_show("Idle for %d minutes, status set to away (priority %d).", away_time, pri); + } + prefs_free_string(message); + + title_bar_set_presence(CONTACT_AWAY); + } + } else if (g_strcmp0(mode, "idle") == 0) { + activity_state = ACTIVITY_ST_IDLE; + + // send current presence with last activity + cl_ev_presence_send(curr_presence, curr_status, idle_ms / 1000); + } + } + break; + case ACTIVITY_ST_IDLE: + if (check && (idle_ms < away_time_ms)) { + activity_state = ACTIVITY_ST_ACTIVE; + + cons_show("No longer idle."); + + // send current presence without last activity + cl_ev_presence_send(curr_presence, curr_status, 0); + } + break; + case ACTIVITY_ST_AWAY: + if (xa_time_ms > 0 && (idle_ms >= xa_time_ms)) { + activity_state = ACTIVITY_ST_XA; + + // send extended away presence with last activity + char *message = prefs_get_string(PREF_AUTOXA_MESSAGE); + if (prefs_get_boolean(PREF_LASTACTIVITY)) { + cl_ev_presence_send(RESOURCE_XA, message, idle_ms / 1000); + } else { + cl_ev_presence_send(RESOURCE_XA, message, 0); + } + + int pri = accounts_get_priority_for_presence_type(account, RESOURCE_XA); + if (message) { + cons_show("Idle for %d minutes, status set to xa (priority %d), \"%s\".", xa_time, pri, message); + } else { + cons_show("Idle for %d minutes, status set to xa (priority %d).", xa_time, pri); + } + prefs_free_string(message); + + title_bar_set_presence(CONTACT_XA); + } else if (check && (idle_ms < away_time_ms)) { + activity_state = ACTIVITY_ST_ACTIVE; + + cons_show("No longer idle."); + + // send saved presence without last activity + cl_ev_presence_send(saved_presence, saved_status, 0); + contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence); + title_bar_set_presence(contact_pres); + } + break; + case ACTIVITY_ST_XA: + if (check && (idle_ms < away_time_ms)) { + activity_state = ACTIVITY_ST_ACTIVE; + + cons_show("No longer idle."); + + // send saved presence without last activity + cl_ev_presence_send(saved_presence, saved_status, 0); + contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence); + title_bar_set_presence(contact_pres); + } + break; + } + + prefs_free_string(mode); +} + static void _session_reconnect(void) { diff --git a/src/xmpp/session.h b/src/xmpp/session.h index 1bc67b63..2d51d65c 100644 --- a/src/xmpp/session.h +++ b/src/xmpp/session.h @@ -42,4 +42,7 @@ void session_login_failed(void); void session_lost_connection(void); void session_autoping_fail(void); +void session_init_activity(void); +void session_check_autoaway(void); + #endif diff --git a/tests/unittests/xmpp/stub_xmpp.c b/tests/unittests/xmpp/stub_xmpp.c index 064d002f..c80224a7 100644 --- a/tests/unittests/xmpp/stub_xmpp.c +++ b/tests/unittests/xmpp/stub_xmpp.c @@ -7,6 +7,8 @@ // connection functions void session_init(void) {} +void session_init_activity(void) {} +void session_check_autoaway(void) {} jabber_conn_status_t session_connect_with_details(const char * const jid, const char * const passwd, const char * const altdomain, const int port, const char *const tls_policy) From ef942bd27a759b3016c473abba8066a8ddcb6ffe Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 16:22:15 +0100 Subject: [PATCH 2/7] 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" From 37742d71b638ba5343af7398842f84b530d11c29 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 16:27:39 +0100 Subject: [PATCH 3/7] Move resource conversions --- src/common.c | 71 +------------------------------------------- src/common.h | 7 +---- src/config/account.c | 1 + src/log.c | 2 +- src/xmpp/resource.c | 69 ++++++++++++++++++++++++++++++++++++++++++ src/xmpp/resource.h | 5 ++++ 6 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/common.c b/src/common.c index 98466231..035a0505 100644 --- a/src/common.c +++ b/src/common.c @@ -285,7 +285,7 @@ utf8_display_len(const char *const str) } char* -prof_getline(FILE *stream) +file_getline(FILE *stream) { char *buf; char *result; @@ -380,75 +380,6 @@ release_is_new(char *found_version) } } -gboolean -valid_resource_presence_string(const char *const str) -{ - assert(str != NULL); - if ((strcmp(str, "online") == 0) || (strcmp(str, "chat") == 0) || - (strcmp(str, "away") == 0) || (strcmp(str, "xa") == 0) || - (strcmp(str, "dnd") == 0)) { - return TRUE; - } else { - return FALSE; - } -} - -const char* -string_from_resource_presence(resource_presence_t presence) -{ - switch(presence) - { - case RESOURCE_CHAT: - return "chat"; - case RESOURCE_AWAY: - return "away"; - case RESOURCE_XA: - return "xa"; - case RESOURCE_DND: - return "dnd"; - default: - return "online"; - } -} - -resource_presence_t -resource_presence_from_string(const char *const str) -{ - if (str == NULL) { - return RESOURCE_ONLINE; - } else if (strcmp(str, "online") == 0) { - return RESOURCE_ONLINE; - } else if (strcmp(str, "chat") == 0) { - return RESOURCE_CHAT; - } else if (strcmp(str, "away") == 0) { - return RESOURCE_AWAY; - } else if (strcmp(str, "xa") == 0) { - return RESOURCE_XA; - } else if (strcmp(str, "dnd") == 0) { - return RESOURCE_DND; - } else { - return RESOURCE_ONLINE; - } -} - -contact_presence_t -contact_presence_from_resource_presence(resource_presence_t resource_presence) -{ - switch(resource_presence) - { - case RESOURCE_CHAT: - return CONTACT_CHAT; - case RESOURCE_AWAY: - return CONTACT_AWAY; - case RESOURCE_XA: - return CONTACT_XA; - case RESOURCE_DND: - return CONTACT_DND; - default: - return CONTACT_ONLINE; - } -} - char* create_unique_id(char *prefix) { diff --git a/src/common.h b/src/common.h index 678a12ff..9d8c99a7 100644 --- a/src/common.h +++ b/src/common.h @@ -107,15 +107,10 @@ char* str_replace(const char *string, const char *substr, const char *replacemen int str_contains(const char str[], int size, char ch); gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); int utf8_display_len(const char *const str); -char* prof_getline(FILE *stream); +char* file_getline(FILE *stream); char* release_get_latest(void); gboolean release_is_new(char *found_version); -gboolean valid_resource_presence_string(const char *const str); -const char* string_from_resource_presence(resource_presence_t presence); -resource_presence_t resource_presence_from_string(const char *const str); -contact_presence_t contact_presence_from_resource_presence(resource_presence_t resource_presence); - char* p_sha1_hash(char *str); char* create_unique_id(char *prefix); void reset_unique_id(void); diff --git a/src/config/account.c b/src/config/account.c index 879759f5..3e8c8cf1 100644 --- a/src/config/account.c +++ b/src/config/account.c @@ -42,6 +42,7 @@ #include "log.h" #include "config/account.h" #include "xmpp/jid.h" +#include "xmpp/resource.h" ProfAccount* account_new(const gchar *const name, const gchar *const jid, diff --git a/src/log.c b/src/log.c index e0a50055..7fcef45f 100644 --- a/src/log.c +++ b/src/log.c @@ -472,7 +472,7 @@ chat_log_get_previous(const gchar *const login, const gchar *const recipient) g_string_free(header, FALSE); char *line; - while ((line = prof_getline(logp)) != NULL) { + while ((line = file_getline(logp)) != NULL) { history = g_slist_append(history, line); } diff --git a/src/xmpp/resource.c b/src/xmpp/resource.c index 6e6bc5f0..2309883c 100644 --- a/src/xmpp/resource.c +++ b/src/xmpp/resource.c @@ -95,3 +95,72 @@ resource_destroy(Resource *resource) free(resource); } } + +gboolean +valid_resource_presence_string(const char *const str) +{ + assert(str != NULL); + if ((strcmp(str, "online") == 0) || (strcmp(str, "chat") == 0) || + (strcmp(str, "away") == 0) || (strcmp(str, "xa") == 0) || + (strcmp(str, "dnd") == 0)) { + return TRUE; + } else { + return FALSE; + } +} + +const char* +string_from_resource_presence(resource_presence_t presence) +{ + switch(presence) + { + case RESOURCE_CHAT: + return "chat"; + case RESOURCE_AWAY: + return "away"; + case RESOURCE_XA: + return "xa"; + case RESOURCE_DND: + return "dnd"; + default: + return "online"; + } +} + +resource_presence_t +resource_presence_from_string(const char *const str) +{ + if (str == NULL) { + return RESOURCE_ONLINE; + } else if (strcmp(str, "online") == 0) { + return RESOURCE_ONLINE; + } else if (strcmp(str, "chat") == 0) { + return RESOURCE_CHAT; + } else if (strcmp(str, "away") == 0) { + return RESOURCE_AWAY; + } else if (strcmp(str, "xa") == 0) { + return RESOURCE_XA; + } else if (strcmp(str, "dnd") == 0) { + return RESOURCE_DND; + } else { + return RESOURCE_ONLINE; + } +} + +contact_presence_t +contact_presence_from_resource_presence(resource_presence_t resource_presence) +{ + switch(resource_presence) + { + case RESOURCE_CHAT: + return CONTACT_CHAT; + case RESOURCE_AWAY: + return CONTACT_AWAY; + case RESOURCE_XA: + return CONTACT_XA; + case RESOURCE_DND: + return CONTACT_DND; + default: + return CONTACT_ONLINE; + } +} diff --git a/src/xmpp/resource.h b/src/xmpp/resource.h index 22681013..ec22569a 100644 --- a/src/xmpp/resource.h +++ b/src/xmpp/resource.h @@ -49,4 +49,9 @@ Resource* resource_new(const char *const name, resource_presence_t presence, con void resource_destroy(Resource *resource); int resource_compare_availability(Resource *first, Resource *second); +gboolean valid_resource_presence_string(const char *const str); +const char* string_from_resource_presence(resource_presence_t presence); +resource_presence_t resource_presence_from_string(const char *const str); +contact_presence_t contact_presence_from_resource_presence(resource_presence_t resource_presence); + #endif From 6cc4abedc58f294ccb36632cb86464c247687288 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 17:02:09 +0100 Subject: [PATCH 4/7] Move window functions to window_list.c --- src/common.c | 73 --------- src/common.h | 4 +- src/ui/window_list.c | 99 ++++++++++-- tests/unittests/test_common.c | 277 ---------------------------------- tests/unittests/test_common.h | 19 --- tests/unittests/unittests.c | 19 --- 6 files changed, 88 insertions(+), 403 deletions(-) diff --git a/src/common.c b/src/common.c index 035a0505..d3f09a56 100644 --- a/src/common.c +++ b/src/common.c @@ -420,79 +420,6 @@ p_sha1_hash(char *str) return g_base64_encode(digest, sizeof(digest)); } -int -cmp_win_num(gconstpointer a, gconstpointer b) -{ - int real_a = GPOINTER_TO_INT(a); - int real_b = GPOINTER_TO_INT(b); - - if (real_a == 0) { - real_a = 10; - } - - if (real_b == 0) { - real_b = 10; - } - - if (real_a < real_b) { - return -1; - } else if (real_a == real_b) { - return 0; - } else { - return 1; - } -} - -int -get_next_available_win_num(GList *used) -{ - // only console used - if (g_list_length(used) == 1) { - return 2; - } else { - GList *sorted = NULL; - GList *curr = used; - while (curr) { - sorted = g_list_insert_sorted(sorted, curr->data, cmp_win_num); - curr = g_list_next(curr); - } - - int result = 0; - int last_num = 1; - curr = sorted; - // skip console - curr = g_list_next(curr); - while (curr) { - int curr_num = GPOINTER_TO_INT(curr->data); - - if (((last_num != 9) && ((last_num + 1) != curr_num)) || - ((last_num == 9) && (curr_num != 0))) { - result = last_num + 1; - if (result == 10) { - result = 0; - } - g_list_free(sorted); - return (result); - - } else { - last_num = curr_num; - if (last_num == 0) { - last_num = 10; - } - } - curr = g_list_next(curr); - } - result = last_num + 1; - if (result == 10) { - result = 0; - } - - g_list_free(sorted); - return result; - } -} - - static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data) { diff --git a/src/common.h b/src/common.h index 9d8c99a7..c463d62c 100644 --- a/src/common.h +++ b/src/common.h @@ -108,6 +108,7 @@ int str_contains(const char str[], int size, char ch); gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); int utf8_display_len(const char *const str); char* file_getline(FILE *stream); + char* release_get_latest(void); gboolean release_is_new(char *found_version); @@ -115,9 +116,6 @@ char* p_sha1_hash(char *str); char* create_unique_id(char *prefix); void reset_unique_id(void); -int cmp_win_num(gconstpointer a, gconstpointer b); -int get_next_available_win_num(GList *used); - char* get_file_or_linked(char *loc, char *basedir); char* strip_arg_quotes(const char *const input); gboolean is_notify_enabled(void); diff --git a/src/ui/window_list.c b/src/ui/window_list.c index 20994e34..402fbdc1 100644 --- a/src/ui/window_list.c +++ b/src/ui/window_list.c @@ -55,6 +55,9 @@ static int current; static Autocomplete wins_ac; static Autocomplete wins_close_ac; +static int _wins_cmp_num(gconstpointer a, gconstpointer b); +static int _wins_get_next_available_num(GList *used); + void wins_init(void) { @@ -424,7 +427,7 @@ wins_get_next(void) { // get and sort win nums GList *keys = g_hash_table_get_keys(windows); - keys = g_list_sort(keys, cmp_win_num); + keys = g_list_sort(keys, _wins_cmp_num); GList *curr = keys; // find our place in the list @@ -453,7 +456,7 @@ wins_get_previous(void) { // get and sort win nums GList *keys = g_hash_table_get_keys(windows); - keys = g_list_sort(keys, cmp_win_num); + keys = g_list_sort(keys, _wins_cmp_num); GList *curr = keys; // find our place in the list @@ -612,7 +615,7 @@ ProfWin* wins_new_xmlconsole(void) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_xmlconsole(); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -625,7 +628,7 @@ ProfWin* wins_new_chat(const char *const barejid) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_chat(barejid); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -648,7 +651,7 @@ ProfWin* wins_new_muc(const char *const roomjid) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_muc(roomjid); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -661,7 +664,7 @@ ProfWin* wins_new_muc_config(const char *const roomjid, DataForm *form) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_muc_config(roomjid, form); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -672,7 +675,7 @@ ProfWin* wins_new_private(const char *const fulljid) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_private(fulljid); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -685,7 +688,7 @@ ProfWin * wins_new_plugin(const char *const plugin_name, const char * const tag) { GList *keys = g_hash_table_get_keys(windows); - int result = get_next_available_win_num(keys); + int result = _wins_get_next_available_num(keys); g_list_free(keys); ProfWin *newwin = win_create_plugin(plugin_name, tag); g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin); @@ -896,23 +899,95 @@ wins_swap(int source_win, int target_win) } } +static int +_wins_cmp_num(gconstpointer a, gconstpointer b) +{ + int real_a = GPOINTER_TO_INT(a); + int real_b = GPOINTER_TO_INT(b); + + if (real_a == 0) { + real_a = 10; + } + + if (real_b == 0) { + real_b = 10; + } + + if (real_a < real_b) { + return -1; + } else if (real_a == real_b) { + return 0; + } else { + return 1; + } +} + +static int +_wins_get_next_available_num(GList *used) +{ + // only console used + if (g_list_length(used) == 1) { + return 2; + } else { + GList *sorted = NULL; + GList *curr = used; + while (curr) { + sorted = g_list_insert_sorted(sorted, curr->data, _wins_cmp_num); + curr = g_list_next(curr); + } + + int result = 0; + int last_num = 1; + curr = sorted; + // skip console + curr = g_list_next(curr); + while (curr) { + int curr_num = GPOINTER_TO_INT(curr->data); + + if (((last_num != 9) && ((last_num + 1) != curr_num)) || + ((last_num == 9) && (curr_num != 0))) { + result = last_num + 1; + if (result == 10) { + result = 0; + } + g_list_free(sorted); + return (result); + + } else { + last_num = curr_num; + if (last_num == 0) { + last_num = 10; + } + } + curr = g_list_next(curr); + } + result = last_num + 1; + if (result == 10) { + result = 0; + } + + g_list_free(sorted); + return result; + } +} + gboolean wins_tidy(void) { gboolean tidy_required = FALSE; // check for gaps GList *keys = g_hash_table_get_keys(windows); - keys = g_list_sort(keys, cmp_win_num); + keys = g_list_sort(keys, _wins_cmp_num); // get last used GList *last = g_list_last(keys); int last_num = GPOINTER_TO_INT(last->data); // find first free num TODO - Will sort again - int next_available = get_next_available_win_num(keys); + int next_available = _wins_get_next_available_num(keys); // found gap (next available before last window) - if (cmp_win_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) { + if (_wins_cmp_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) { tidy_required = TRUE; } @@ -966,7 +1041,7 @@ wins_create_summary(gboolean unread) GSList *result = NULL; GList *keys = g_hash_table_get_keys(windows); - keys = g_list_sort(keys, cmp_win_num); + keys = g_list_sort(keys, _wins_cmp_num); GList *curr = keys; while (curr) { diff --git a/tests/unittests/test_common.c b/tests/unittests/test_common.c index 76128edf..d03f2123 100644 --- a/tests/unittests/test_common.c +++ b/tests/unittests/test_common.c @@ -172,283 +172,6 @@ void replace_when_new_null(void **state) free(result); } -void compare_win_nums_less(void **state) -{ - gconstpointer a = GINT_TO_POINTER(2); - gconstpointer b = GINT_TO_POINTER(3); - - int result = cmp_win_num(a, b); - - assert_true(result < 0); -} - -void compare_win_nums_equal(void **state) -{ - gconstpointer a = GINT_TO_POINTER(5); - gconstpointer b = GINT_TO_POINTER(5); - - int result = cmp_win_num(a, b); - - assert_true(result == 0); -} - -void compare_win_nums_greater(void **state) -{ - gconstpointer a = GINT_TO_POINTER(7); - gconstpointer b = GINT_TO_POINTER(6); - - int result = cmp_win_num(a, b); - - assert_true(result > 0); -} - -void compare_0s_equal(void **state) -{ - gconstpointer a = GINT_TO_POINTER(0); - gconstpointer b = GINT_TO_POINTER(0); - - int result = cmp_win_num(a, b); - - assert_true(result == 0); -} - -void compare_0_greater_than_1(void **state) -{ - gconstpointer a = GINT_TO_POINTER(0); - gconstpointer b = GINT_TO_POINTER(1); - - int result = cmp_win_num(a, b); - - assert_true(result > 0); -} - -void compare_1_less_than_0(void **state) -{ - gconstpointer a = GINT_TO_POINTER(1); - gconstpointer b = GINT_TO_POINTER(0); - - int result = cmp_win_num(a, b); - - assert_true(result < 0); -} - -void compare_0_less_than_11(void **state) -{ - gconstpointer a = GINT_TO_POINTER(0); - gconstpointer b = GINT_TO_POINTER(11); - - int result = cmp_win_num(a, b); - - assert_true(result < 0); -} - -void compare_11_greater_than_0(void **state) -{ - gconstpointer a = GINT_TO_POINTER(11); - gconstpointer b = GINT_TO_POINTER(0); - - int result = cmp_win_num(a, b); - - assert_true(result > 0); -} - -void compare_0_greater_than_9(void **state) -{ - gconstpointer a = GINT_TO_POINTER(0); - gconstpointer b = GINT_TO_POINTER(9); - - int result = cmp_win_num(a, b); - - assert_true(result > 0); -} - -void compare_9_less_than_0(void **state) -{ - gconstpointer a = GINT_TO_POINTER(9); - gconstpointer b = GINT_TO_POINTER(0); - - int result = cmp_win_num(a, b); - - assert_true(result < 0); -} - -void next_available_when_only_console(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - - int result = get_next_available_win_num(used); - - assert_int_equal(2, result); -} - -void next_available_3_at_end(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - - int result = get_next_available_win_num(used); - - assert_int_equal(3, result); -} - -void next_available_9_at_end(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - - int result = get_next_available_win_num(used); - - assert_int_equal(9, result); -} - -void next_available_0_at_end(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - used = g_list_append(used, GINT_TO_POINTER(9)); - - int result = get_next_available_win_num(used); - - assert_int_equal(0, result); -} - -void next_available_2_in_first_gap(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(9)); - used = g_list_append(used, GINT_TO_POINTER(0)); - - int result = get_next_available_win_num(used); - - assert_int_equal(2, result); -} - -void next_available_9_in_first_gap(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - used = g_list_append(used, GINT_TO_POINTER(0)); - used = g_list_append(used, GINT_TO_POINTER(11)); - used = g_list_append(used, GINT_TO_POINTER(12)); - used = g_list_append(used, GINT_TO_POINTER(13)); - used = g_list_append(used, GINT_TO_POINTER(20)); - - int result = get_next_available_win_num(used); - - assert_int_equal(9, result); -} - -void next_available_0_in_first_gap(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - used = g_list_append(used, GINT_TO_POINTER(9)); - used = g_list_append(used, GINT_TO_POINTER(11)); - used = g_list_append(used, GINT_TO_POINTER(12)); - used = g_list_append(used, GINT_TO_POINTER(13)); - used = g_list_append(used, GINT_TO_POINTER(20)); - - int result = get_next_available_win_num(used); - - assert_int_equal(0, result); -} - -void next_available_11_in_first_gap(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - used = g_list_append(used, GINT_TO_POINTER(9)); - used = g_list_append(used, GINT_TO_POINTER(0)); - used = g_list_append(used, GINT_TO_POINTER(12)); - used = g_list_append(used, GINT_TO_POINTER(13)); - used = g_list_append(used, GINT_TO_POINTER(20)); - - int result = get_next_available_win_num(used); - - assert_int_equal(11, result); -} - -void next_available_24_first_big_gap(void **state) -{ - GList *used = NULL; - used = g_list_append(used, GINT_TO_POINTER(1)); - used = g_list_append(used, GINT_TO_POINTER(2)); - used = g_list_append(used, GINT_TO_POINTER(3)); - used = g_list_append(used, GINT_TO_POINTER(4)); - used = g_list_append(used, GINT_TO_POINTER(5)); - used = g_list_append(used, GINT_TO_POINTER(6)); - used = g_list_append(used, GINT_TO_POINTER(7)); - used = g_list_append(used, GINT_TO_POINTER(8)); - used = g_list_append(used, GINT_TO_POINTER(9)); - used = g_list_append(used, GINT_TO_POINTER(0)); - used = g_list_append(used, GINT_TO_POINTER(11)); - used = g_list_append(used, GINT_TO_POINTER(12)); - used = g_list_append(used, GINT_TO_POINTER(13)); - used = g_list_append(used, GINT_TO_POINTER(14)); - used = g_list_append(used, GINT_TO_POINTER(15)); - used = g_list_append(used, GINT_TO_POINTER(16)); - used = g_list_append(used, GINT_TO_POINTER(17)); - used = g_list_append(used, GINT_TO_POINTER(18)); - used = g_list_append(used, GINT_TO_POINTER(19)); - used = g_list_append(used, GINT_TO_POINTER(20)); - used = g_list_append(used, GINT_TO_POINTER(21)); - used = g_list_append(used, GINT_TO_POINTER(22)); - used = g_list_append(used, GINT_TO_POINTER(23)); - used = g_list_append(used, GINT_TO_POINTER(51)); - used = g_list_append(used, GINT_TO_POINTER(52)); - used = g_list_append(used, GINT_TO_POINTER(53)); - used = g_list_append(used, GINT_TO_POINTER(89)); - used = g_list_append(used, GINT_TO_POINTER(90)); - used = g_list_append(used, GINT_TO_POINTER(100)); - used = g_list_append(used, GINT_TO_POINTER(101)); - used = g_list_append(used, GINT_TO_POINTER(102)); - - int result = get_next_available_win_num(used); - - assert_int_equal(24, result); -} - void test_online_is_valid_resource_presence_string(void **state) { assert_true(valid_resource_presence_string("online")); diff --git a/tests/unittests/test_common.h b/tests/unittests/test_common.h index 3a790017..6da87a68 100644 --- a/tests/unittests/test_common.h +++ b/tests/unittests/test_common.h @@ -11,25 +11,6 @@ void replace_when_sub_empty(void **state); void replace_when_sub_null(void **state); void replace_when_new_empty(void **state); void replace_when_new_null(void **state); -void compare_win_nums_less(void **state); -void compare_win_nums_equal(void **state); -void compare_win_nums_greater(void **state); -void compare_0s_equal(void **state); -void compare_0_greater_than_1(void **state); -void compare_1_less_than_0(void **state); -void compare_0_less_than_11(void **state); -void compare_11_greater_than_0(void **state); -void compare_0_greater_than_9(void **state); -void compare_9_less_than_0(void **state); -void next_available_when_only_console(void **state); -void next_available_3_at_end(void **state); -void next_available_9_at_end(void **state); -void next_available_0_at_end(void **state); -void next_available_2_in_first_gap(void **state); -void next_available_9_in_first_gap(void **state); -void next_available_0_in_first_gap(void **state); -void next_available_11_in_first_gap(void **state); -void next_available_24_first_big_gap(void **state); void test_online_is_valid_resource_presence_string(void **state); void test_chat_is_valid_resource_presence_string(void **state); void test_away_is_valid_resource_presence_string(void **state); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index ea479a94..9edb8b92 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -51,25 +51,6 @@ int main(int argc, char* argv[]) { unit_test(replace_when_sub_null), unit_test(replace_when_new_empty), unit_test(replace_when_new_null), - unit_test(compare_win_nums_less), - unit_test(compare_win_nums_equal), - unit_test(compare_win_nums_greater), - unit_test(compare_0s_equal), - unit_test(compare_0_greater_than_1), - unit_test(compare_1_less_than_0), - unit_test(compare_0_less_than_11), - unit_test(compare_11_greater_than_0), - unit_test(compare_0_greater_than_9), - unit_test(compare_9_less_than_0), - unit_test(next_available_when_only_console), - unit_test(next_available_3_at_end), - unit_test(next_available_9_at_end), - unit_test(next_available_0_at_end), - unit_test(next_available_2_in_first_gap), - unit_test(next_available_9_in_first_gap), - unit_test(next_available_0_in_first_gap), - unit_test(next_available_11_in_first_gap), - unit_test(next_available_24_first_big_gap), unit_test(test_online_is_valid_resource_presence_string), unit_test(test_chat_is_valid_resource_presence_string), unit_test(test_away_is_valid_resource_presence_string), From 29452f8f1b8f7a61773905169b3883f1b494c786 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 17:12:09 +0100 Subject: [PATCH 5/7] Move xgd functions --- src/config/accounts.c | 4 ++-- src/config/files.c | 29 +++++++++++++++++++++++++---- src/config/files.h | 7 +++++-- src/config/preferences.c | 25 ++----------------------- src/config/scripts.c | 8 ++++---- src/config/theme.c | 2 +- src/config/tlscerts.c | 2 +- src/log.c | 4 ++-- src/otr/otr.c | 6 +++--- src/pgp/gpg.c | 2 +- src/plugins/plugins.c | 2 +- src/plugins/settings.c | 4 ++-- src/plugins/themes.c | 2 +- src/ui/inputwin.c | 3 ++- src/ui/tray.c | 2 +- src/xmpp/capabilities.c | 2 +- tests/unittests/helpers.c | 4 ++-- 17 files changed, 56 insertions(+), 52 deletions(-) diff --git a/src/config/accounts.c b/src/config/accounts.c index 84671b74..d087bbb9 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -858,7 +858,7 @@ _save_accounts(void) { gsize g_data_size; gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *base_str = g_string_new(xdg_data); g_string_append(base_str, "/profanity/"); gchar *true_loc = get_file_or_linked(accounts_loc, base_str->str); @@ -873,7 +873,7 @@ _save_accounts(void) static gchar* _get_accounts_file(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *logfile = g_string_new(xdg_data); g_string_append(logfile, "/profanity/accounts"); gchar *result = strdup(logfile->str); diff --git a/src/config/files.c b/src/config/files.c index 5f57f691..7b8759b1 100644 --- a/src/config/files.c +++ b/src/config/files.c @@ -45,8 +45,8 @@ void files_create_directories(void) { - gchar *xdg_config = xdg_get_config_home(); - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_config = files_get_xdg_config_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *themes_dir = g_string_new(xdg_config); g_string_append(themes_dir, "/profanity/themes"); @@ -86,7 +86,28 @@ files_create_directories(void) } gchar* -xdg_get_config_home(void) +files_get_inputrc_path(void) +{ + gchar *xdg_config = files_get_xdg_config_home(); + GString *inputrc_file = g_string_new(xdg_config); + g_free(xdg_config); + + g_string_append(inputrc_file, "/profanity/inputrc"); + + if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) { + gchar *result = strdup(inputrc_file->str); + g_string_free(inputrc_file, TRUE); + + return result; + } + + g_string_free(inputrc_file, TRUE); + + return NULL; +} + +gchar* +files_get_xdg_config_home(void) { gchar *xdg_config_home = getenv("XDG_CONFIG_HOME"); if (xdg_config_home) @@ -105,7 +126,7 @@ xdg_get_config_home(void) } gchar* -xdg_get_data_home(void) +files_get_xdg_data_home(void) { gchar *xdg_data_home = getenv("XDG_DATA_HOME"); if (xdg_data_home) diff --git a/src/config/files.h b/src/config/files.h index 431229f7..ff50cc1d 100644 --- a/src/config/files.h +++ b/src/config/files.h @@ -37,8 +37,11 @@ #include -gchar* xdg_get_config_home(void); -gchar* xdg_get_data_home(void); void files_create_directories(void); +gchar* files_get_inputrc_path(void); + +gchar* files_get_xdg_config_home(void); +gchar* files_get_xdg_data_home(void); + #endif diff --git a/src/config/preferences.c b/src/config/preferences.c index 3fd32586..d2d3b8ce 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -1121,27 +1121,6 @@ prefs_get_aliases(void) } } -gchar* -prefs_get_inputrc(void) -{ - gchar *xdg_config = xdg_get_config_home(); - GString *inputrc_file = g_string_new(xdg_config); - g_free(xdg_config); - - g_string_append(inputrc_file, "/profanity/inputrc"); - - if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) { - gchar *result = strdup(inputrc_file->str); - g_string_free(inputrc_file, TRUE); - - return result; - } - - g_string_free(inputrc_file, TRUE); - - return NULL; -} - void _free_alias(ProfAlias *alias) { @@ -1161,7 +1140,7 @@ _save_prefs(void) { gsize g_data_size; gchar *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); - gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_config = files_get_xdg_config_home(); GString *base_str = g_string_new(xdg_config); g_string_append(base_str, "/profanity/"); gchar *true_loc = get_file_or_linked(prefs_loc, base_str->str); @@ -1176,7 +1155,7 @@ _save_prefs(void) static gchar* _get_preferences_file(void) { - gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_config = files_get_xdg_config_home(); GString *prefs_file = g_string_new(xdg_config); g_string_append(prefs_file, "/profanity/profrc"); gchar *result = strdup(prefs_file->str); diff --git a/src/config/scripts.c b/src/config/scripts.c index 91dbe4df..8ab2684c 100644 --- a/src/config/scripts.c +++ b/src/config/scripts.c @@ -51,7 +51,7 @@ void scripts_init(void) { - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *scriptsdir = g_string_new(data_home); free(data_home); @@ -75,7 +75,7 @@ scripts_init(void) GSList* scripts_list(void) { - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *scriptsdir = g_string_new(data_home); free(data_home); g_string_append(scriptsdir, "/profanity/scripts"); @@ -99,7 +99,7 @@ scripts_list(void) GSList* scripts_read(const char *const script) { - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *scriptpath = g_string_new(data_home); free(data_home); @@ -137,7 +137,7 @@ scripts_read(const char *const script) gboolean scripts_exec(const char *const script) { - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *scriptpath = g_string_new(data_home); free(data_home); diff --git a/src/config/theme.c b/src/config/theme.c index fb208279..55698ed6 100644 --- a/src/config/theme.c +++ b/src/config/theme.c @@ -535,7 +535,7 @@ _load_preferences(void) static gchar* _get_themes_dir(void) { - gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_config = files_get_xdg_config_home(); GString *themes_dir = g_string_new(xdg_config); g_free(xdg_config); g_string_append(themes_dir, "/profanity/themes"); diff --git a/src/config/tlscerts.c b/src/config/tlscerts.c index 03a8bd97..8a93ed51 100644 --- a/src/config/tlscerts.c +++ b/src/config/tlscerts.c @@ -432,7 +432,7 @@ tlscerts_close(void) static gchar* _get_tlscerts_file(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *tlscerts_file = g_string_new(xdg_data); g_string_append(tlscerts_file, "/profanity/tlscerts"); gchar *result = strdup(tlscerts_file->str); diff --git a/src/log.c b/src/log.c index 7fcef45f..0e014947 100644 --- a/src/log.c +++ b/src/log.c @@ -639,7 +639,7 @@ _get_groupchat_log_filename(const char *const room, const char *const login, GDa static gchar* _get_chatlog_dir(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *chatlogs_dir = g_string_new(xdg_data); g_string_append(chatlogs_dir, "/profanity/chatlogs"); gchar *result = strdup(chatlogs_dir->str); @@ -652,7 +652,7 @@ _get_chatlog_dir(void) static gchar* _get_main_log_file(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *logfile = g_string_new(xdg_data); g_string_append(logfile, "/profanity/logs/profanity"); if (!prefs_get_boolean(PREF_LOG_SHARED)) { diff --git a/src/otr/otr.c b/src/otr/otr.c index 3e4a90fc..955e60fa 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -126,7 +126,7 @@ cb_write_fingerprints(void *opdata) { gcry_error_t err = 0; - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *basedir = g_string_new(data_home); free(data_home); @@ -215,7 +215,7 @@ otr_on_connect(ProfAccount *account) jid = strdup(account->jid); log_info("Loading OTR key for %s", jid); - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *basedir = g_string_new(data_home); free(data_home); @@ -393,7 +393,7 @@ otr_keygen(ProfAccount *account) jid = strdup(account->jid); log_info("Generating OTR key for %s", jid); - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *basedir = g_string_new(data_home); free(data_home); diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index c2b81c67..6be3aaaa 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -157,7 +157,7 @@ p_gpg_close(void) void p_gpg_on_connect(const char *const barejid) { - gchar *data_home = xdg_get_data_home(); + gchar *data_home = files_get_xdg_data_home(); GString *pubsfile = g_string_new(data_home); free(data_home); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 2f176ac7..2308e7f3 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -834,7 +834,7 @@ plugins_shutdown(void) char* plugins_get_dir(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *plugins_dir = g_string_new(xdg_data); g_string_append(plugins_dir, "/profanity/plugins"); char *result = strdup(plugins_dir->str); diff --git a/src/plugins/settings.c b/src/plugins/settings.c index 4ffe85e5..418451ff 100644 --- a/src/plugins/settings.c +++ b/src/plugins/settings.c @@ -49,7 +49,7 @@ static void _save_settings(void); void plugin_settings_init(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *fileloc = g_string_new(xdg_data); g_string_append(fileloc, "/profanity/plugin_settings"); g_free(xdg_data); @@ -135,7 +135,7 @@ _save_settings(void) gsize g_data_size; gchar *g_data = g_key_file_to_data(settings, &g_data_size, NULL); - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *fileloc = g_string_new(xdg_data); g_free(xdg_data); diff --git a/src/plugins/themes.c b/src/plugins/themes.c index d41ba283..061638e9 100644 --- a/src/plugins/themes.c +++ b/src/plugins/themes.c @@ -44,7 +44,7 @@ static GKeyFile *themes; void plugin_themes_init(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *fileloc = g_string_new(xdg_data); g_string_append(fileloc, "/profanity/plugin_themes"); g_free(xdg_data); diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index fdfdb0ac..6a4093e1 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -57,6 +57,7 @@ #include "log.h" #include "common.h" #include "command/cmd_ac.h" +#include "config/files.h" #include "config/accounts.h" #include "config/preferences.h" #include "config/theme.h" @@ -427,7 +428,7 @@ _inp_rl_startup_hook(void) rl_variable_bind("disable-completion", "on"); // check for and load ~/.config/profanity/inputrc - char *inputrc = prefs_get_inputrc(); + char *inputrc = files_get_inputrc_path(); if (inputrc) { rl_read_init_file(inputrc); free(inputrc); diff --git a/src/ui/tray.c b/src/ui/tray.c index 94408dae..ab0d26e0 100644 --- a/src/ui/tray.c +++ b/src/ui/tray.c @@ -79,7 +79,7 @@ _get_icons(void) #endif /* ICONS_PATH */ - gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_config = files_get_xdg_config_home(); icons_dir = g_string_new(xdg_config); g_free(xdg_config); g_string_append(icons_dir, "/profanity/icons"); diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index bfc22120..b477b5f8 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -701,7 +701,7 @@ caps_destroy(Capabilities *caps) static gchar* _get_cache_file(void) { - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *cache_file = g_string_new(xdg_data); g_string_append(cache_file, "/profanity/capscache"); gchar *result = strdup(cache_file->str); diff --git a/tests/unittests/helpers.c b/tests/unittests/helpers.c index 1494d803..60f36839 100644 --- a/tests/unittests/helpers.c +++ b/tests/unittests/helpers.c @@ -15,7 +15,7 @@ void create_config_dir(void **state) { setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); - gchar *xdg_config = xdg_get_config_home(); + gchar *xdg_config = files_get_xdg_config_home(); GString *profanity_dir = g_string_new(xdg_config); g_string_append(profanity_dir, "/profanity"); @@ -37,7 +37,7 @@ void remove_config_dir(void **state) void create_data_dir(void **state) { setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1); - gchar *xdg_data = xdg_get_data_home(); + gchar *xdg_data = files_get_xdg_data_home(); GString *profanity_dir = g_string_new(xdg_data); g_string_append(profanity_dir, "/profanity"); From a3a73cf0031fef7b4012b27bc1955c406175f0fe Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 21:35:12 +0100 Subject: [PATCH 6/7] Move all filepath handling to files.c --- src/config/accounts.c | 29 ++++---------- src/config/files.c | 73 ++++++++++++++++++++++++++++++------ src/config/files.h | 22 +++++++++-- src/config/preferences.c | 29 ++++---------- src/config/scripts.c | 41 ++++++++------------ src/config/theme.c | 17 ++------- src/config/tlscerts.c | 18 +-------- src/log.c | 42 +++------------------ src/otr/otr.c | 27 ++++++------- src/pgp/gpg.c | 8 ++-- src/plugins/c_plugins.c | 5 ++- src/plugins/plugins.c | 17 +-------- src/plugins/python_plugins.c | 3 +- src/plugins/settings.c | 35 +++++++---------- src/plugins/themes.c | 19 +++++----- src/ui/inputwin.c | 2 +- src/ui/tray.c | 6 +-- src/xmpp/capabilities.c | 23 +++--------- tests/unittests/helpers.c | 20 +--------- 19 files changed, 174 insertions(+), 262 deletions(-) diff --git a/src/config/accounts.c b/src/config/accounts.c index d087bbb9..96168b5b 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -49,14 +49,13 @@ #include "xmpp/xmpp.h" #include "xmpp/jid.h" -static gchar *accounts_loc; +static char *accounts_loc; static GKeyFile *accounts; static Autocomplete all_ac; static Autocomplete enabled_ac; static void _save_accounts(void); -static gchar* _get_accounts_file(void); void accounts_load(void) @@ -64,7 +63,7 @@ accounts_load(void) log_info("Loading accounts"); all_ac = autocomplete_new(); enabled_ac = autocomplete_new(); - accounts_loc = _get_accounts_file(); + accounts_loc = files_get_data_path(FILE_ACCOUNTS); if (g_file_test(accounts_loc, G_FILE_TEST_EXISTS)) { g_chmod(accounts_loc, S_IRUSR | S_IWUSR); @@ -858,27 +857,13 @@ _save_accounts(void) { gsize g_data_size; gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); - gchar *xdg_data = files_get_xdg_data_home(); - GString *base_str = g_string_new(xdg_data); - g_string_append(base_str, "/profanity/"); - gchar *true_loc = get_file_or_linked(accounts_loc, base_str->str); + + gchar *base = g_path_get_basename(accounts_loc); + gchar *true_loc = get_file_or_linked(accounts_loc, base); g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL); g_chmod(accounts_loc, S_IRUSR | S_IWUSR); - g_free(xdg_data); + + g_free(base); free(true_loc); g_free(g_accounts_data); - g_string_free(base_str, TRUE); -} - -static gchar* -_get_accounts_file(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *logfile = g_string_new(xdg_data); - g_string_append(logfile, "/profanity/accounts"); - gchar *result = strdup(logfile->str); - g_free(xdg_data); - g_string_free(logfile, TRUE); - - return result; } diff --git a/src/config/files.c b/src/config/files.c index 7b8759b1..d60b4efa 100644 --- a/src/config/files.c +++ b/src/config/files.c @@ -35,18 +35,22 @@ #include #include - +#include #include #include "common.h" #include "log.h" #include "config/files.h" +#include "config/preferences.h" + +static char* _files_get_xdg_config_home(void); +static char* _files_get_xdg_data_home(void); void files_create_directories(void) { - gchar *xdg_config = files_get_xdg_config_home(); - gchar *xdg_data = files_get_xdg_data_home(); + gchar *xdg_config = _files_get_xdg_config_home(); + gchar *xdg_data = _files_get_xdg_data_home(); GString *themes_dir = g_string_new(xdg_config); g_string_append(themes_dir, "/profanity/themes"); @@ -85,17 +89,17 @@ files_create_directories(void) g_free(xdg_data); } -gchar* -files_get_inputrc_path(void) +char* +files_get_inputrc_file(void) { - gchar *xdg_config = files_get_xdg_config_home(); + gchar *xdg_config = _files_get_xdg_config_home(); GString *inputrc_file = g_string_new(xdg_config); g_free(xdg_config); g_string_append(inputrc_file, "/profanity/inputrc"); if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) { - gchar *result = strdup(inputrc_file->str); + char *result = strdup(inputrc_file->str); g_string_free(inputrc_file, TRUE); return result; @@ -106,8 +110,53 @@ files_get_inputrc_path(void) return NULL; } -gchar* -files_get_xdg_config_home(void) +char* +files_get_log_file(void) +{ + gchar *xdg_data = _files_get_xdg_data_home(); + GString *logfile = g_string_new(xdg_data); + g_string_append(logfile, "/profanity/logs/profanity"); + if (!prefs_get_boolean(PREF_LOG_SHARED)) { + g_string_append_printf(logfile, "%d", getpid()); + } + g_string_append(logfile, ".log"); + char *result = strdup(logfile->str); + free(xdg_data); + g_string_free(logfile, TRUE); + + return result; +} + +char* +files_get_config_path(char *config_base) +{ + gchar *xdg_config = _files_get_xdg_config_home(); + GString *file_str = g_string_new(xdg_config); + g_string_append(file_str, "/profanity/"); + g_string_append(file_str, config_base); + char *result = strdup(file_str->str); + g_free(xdg_config); + g_string_free(file_str, TRUE); + + return result; +} + +char* +files_get_data_path(char *data_base) +{ + gchar *xdg_data = _files_get_xdg_data_home(); + GString *file_str = g_string_new(xdg_data); + g_string_append(file_str, "/profanity/"); + g_string_append(file_str, data_base); + char *result = strdup(file_str->str); + g_free(xdg_data); + g_string_free(file_str, TRUE); + + return result; +} + +static char* +_files_get_xdg_config_home(void) { gchar *xdg_config_home = getenv("XDG_CONFIG_HOME"); if (xdg_config_home) @@ -118,15 +167,15 @@ files_get_xdg_config_home(void) } else { GString *default_path = g_string_new(getenv("HOME")); g_string_append(default_path, "/.config"); - gchar *result = strdup(default_path->str); + char *result = strdup(default_path->str); g_string_free(default_path, TRUE); return result; } } -gchar* -files_get_xdg_data_home(void) +static char* +_files_get_xdg_data_home(void) { gchar *xdg_data_home = getenv("XDG_DATA_HOME"); if (xdg_data_home) diff --git a/src/config/files.h b/src/config/files.h index ff50cc1d..47a73b43 100644 --- a/src/config/files.h +++ b/src/config/files.h @@ -37,11 +37,27 @@ #include +#define FILE_PROFRC "profrc" +#define FILE_ACCOUNTS "accounts" +#define FILE_TLSCERTS "tlscerts" +#define FILE_PLUGIN_SETTINGS "plugin_settings" +#define FILE_PLUGIN_THEMES "plugin_themes" +#define FILE_CAPSCACHE "capscache" + +#define DIR_THEMES "themes" +#define DIR_ICONS "icons" +#define DIR_SCRIPTS "scripts" +#define DIR_CHATLOGS "chatlogs" +#define DIR_OTR "otr" +#define DIR_PGP "pgp" +#define DIR_PLUGINS "plugins" + void files_create_directories(void); -gchar* files_get_inputrc_path(void); +char* files_get_config_path(char *config_base); +char* files_get_data_path(char *data_base); -gchar* files_get_xdg_config_home(void); -gchar* files_get_xdg_data_home(void); +char* files_get_log_file(void); +char* files_get_inputrc_file(void); #endif diff --git a/src/config/preferences.c b/src/config/preferences.c index d2d3b8ce..bb9693fe 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -62,7 +62,7 @@ #define INPBLOCK_DEFAULT 1000 -static gchar *prefs_loc; +static char *prefs_loc; static GKeyFile *prefs; gint log_maxsize = 0; @@ -70,7 +70,6 @@ static Autocomplete boolean_choice_ac; static Autocomplete room_trigger_ac; static void _save_prefs(void); -static gchar* _get_preferences_file(void); static const char* _get_group(preference_t pref); static const char* _get_key(preference_t pref); static gboolean _get_default_boolean(preference_t pref); @@ -80,7 +79,7 @@ void prefs_load(void) { GError *err; - prefs_loc = _get_preferences_file(); + prefs_loc = files_get_config_path(FILE_PROFRC); if (g_file_test(prefs_loc, G_FILE_TEST_EXISTS)) { g_chmod(prefs_loc, S_IRUSR | S_IWUSR); @@ -1140,29 +1139,15 @@ _save_prefs(void) { gsize g_data_size; gchar *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); - gchar *xdg_config = files_get_xdg_config_home(); - GString *base_str = g_string_new(xdg_config); - g_string_append(base_str, "/profanity/"); - gchar *true_loc = get_file_or_linked(prefs_loc, base_str->str); + gchar *base = g_path_get_basename(prefs_loc); + gchar *true_loc = get_file_or_linked(prefs_loc, base); + g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL); g_chmod(prefs_loc, S_IRUSR | S_IWUSR); - g_free(xdg_config); + + g_free(base); free(true_loc); g_free(g_prefs_data); - g_string_free(base_str, TRUE); -} - -static gchar* -_get_preferences_file(void) -{ - gchar *xdg_config = files_get_xdg_config_home(); - GString *prefs_file = g_string_new(xdg_config); - g_string_append(prefs_file, "/profanity/profrc"); - gchar *result = strdup(prefs_file->str); - g_free(xdg_config); - g_string_free(prefs_file, TRUE); - - return result; } // get the preference group for a specific preference diff --git a/src/config/scripts.c b/src/config/scripts.c index 8ab2684c..41f93e3c 100644 --- a/src/config/scripts.c +++ b/src/config/scripts.c @@ -51,38 +51,31 @@ void scripts_init(void) { - gchar *data_home = files_get_xdg_data_home(); - GString *scriptsdir = g_string_new(data_home); - free(data_home); - - g_string_append(scriptsdir, "/profanity/scripts"); + char *scriptsdir = files_get_data_path(DIR_SCRIPTS); // mkdir if doesn't exist errno = 0; - int res = g_mkdir_with_parents(scriptsdir->str, S_IRWXU); + int res = g_mkdir_with_parents(scriptsdir, S_IRWXU); if (res == -1) { char *errmsg = strerror(errno); if (errmsg) { - log_error("Error creating directory: %s, %s", scriptsdir->str, errmsg); + log_error("Error creating directory: %s, %s", scriptsdir, errmsg); } else { - log_error("Error creating directory: %s", scriptsdir->str); + log_error("Error creating directory: %s", scriptsdir); } } - g_string_free(scriptsdir, TRUE); + free(scriptsdir); } GSList* scripts_list(void) { - gchar *data_home = files_get_xdg_data_home(); - GString *scriptsdir = g_string_new(data_home); - free(data_home); - g_string_append(scriptsdir, "/profanity/scripts"); + char *scriptsdir = files_get_data_path(DIR_SCRIPTS); GSList *result = NULL; - GDir *scripts = g_dir_open(scriptsdir->str, 0, NULL); - g_string_free(scriptsdir, TRUE); + GDir *scripts = g_dir_open(scriptsdir, 0, NULL); + free(scriptsdir); if (scripts) { const gchar *script = g_dir_read_name(scripts); @@ -99,11 +92,10 @@ scripts_list(void) GSList* scripts_read(const char *const script) { - gchar *data_home = files_get_xdg_data_home(); - GString *scriptpath = g_string_new(data_home); - free(data_home); - - g_string_append(scriptpath, "/profanity/scripts/"); + char *scriptsdir = files_get_data_path(DIR_SCRIPTS); + GString *scriptpath = g_string_new(scriptsdir); + free(scriptsdir); + g_string_append(scriptpath, "/"); g_string_append(scriptpath, script); FILE *scriptfile = g_fopen(scriptpath->str, "r"); @@ -137,11 +129,10 @@ scripts_read(const char *const script) gboolean scripts_exec(const char *const script) { - gchar *data_home = files_get_xdg_data_home(); - GString *scriptpath = g_string_new(data_home); - free(data_home); - - g_string_append(scriptpath, "/profanity/scripts/"); + char *scriptsdir = files_get_data_path(DIR_SCRIPTS); + GString *scriptpath = g_string_new(scriptsdir); + free(scriptsdir); + g_string_append(scriptpath, "/"); g_string_append(scriptpath, script); FILE *scriptfile = g_fopen(scriptpath->str, "r"); diff --git a/src/config/theme.c b/src/config/theme.c index 55698ed6..0133b1f6 100644 --- a/src/config/theme.c +++ b/src/config/theme.c @@ -63,7 +63,6 @@ struct colour_string_t { }; static void _load_preferences(void); -static gchar* _get_themes_dir(void); void _theme_list_dir(const gchar *const dir, GSList **result); static GString* _theme_find(const char *const theme_name); static gboolean _theme_load_file(const char *const theme_name); @@ -218,7 +217,7 @@ GSList* theme_list(void) { GSList *result = NULL; - char *themes_dir = _get_themes_dir(); + char *themes_dir = files_get_config_path(DIR_THEMES); _theme_list_dir(themes_dir, &result); free(themes_dir); #ifdef THEMES_PATH @@ -532,16 +531,6 @@ _load_preferences(void) } } -static gchar* -_get_themes_dir(void) -{ - gchar *xdg_config = files_get_xdg_config_home(); - GString *themes_dir = g_string_new(xdg_config); - g_free(xdg_config); - g_string_append(themes_dir, "/profanity/themes"); - return g_string_free(themes_dir, FALSE); -} - void _theme_list_dir(const gchar *const dir, GSList **result) { @@ -560,11 +549,11 @@ static GString* _theme_find(const char *const theme_name) { GString *path = NULL; - gchar *themes_dir = _get_themes_dir(); + char *themes_dir = files_get_config_path(DIR_THEMES); if (themes_dir) { path = g_string_new(themes_dir); - g_free(themes_dir); + free(themes_dir); g_string_append(path, "/"); g_string_append(path, theme_name); if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) { diff --git a/src/config/tlscerts.c b/src/config/tlscerts.c index 8a93ed51..edef2c8c 100644 --- a/src/config/tlscerts.c +++ b/src/config/tlscerts.c @@ -44,10 +44,9 @@ #include "config/tlscerts.h" #include "tools/autocomplete.h" -static gchar *tlscerts_loc; +static char *tlscerts_loc; static GKeyFile *tlscerts; -static gchar* _get_tlscerts_file(void); static void _save_tlscerts(void); static Autocomplete certs_ac; @@ -58,7 +57,7 @@ void tlscerts_init(void) { log_info("Loading TLS certificates"); - tlscerts_loc = _get_tlscerts_file(); + tlscerts_loc = files_get_data_path(FILE_TLSCERTS); if (g_file_test(tlscerts_loc, G_FILE_TEST_EXISTS)) { g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR); @@ -429,19 +428,6 @@ tlscerts_close(void) autocomplete_free(certs_ac); } -static gchar* -_get_tlscerts_file(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *tlscerts_file = g_string_new(xdg_data); - g_string_append(tlscerts_file, "/profanity/tlscerts"); - gchar *result = strdup(tlscerts_file->str); - g_free(xdg_data); - g_string_free(tlscerts_file, TRUE); - - return result; -} - static void _save_tlscerts(void) { diff --git a/src/log.c b/src/log.c index 0e014947..2c1bf7bc 100644 --- a/src/log.c +++ b/src/log.c @@ -85,8 +85,6 @@ static gboolean _key_equals(void *key1, void *key2); static char* _get_log_filename(const char *const other, const char *const login, GDateTime *dt, gboolean create); static char* _get_groupchat_log_filename(const char *const room, const char *const login, GDateTime *dt, gboolean create); -static gchar* _get_chatlog_dir(void); -static gchar* _get_main_log_file(void); static void _rotate_log_file(void); static char* _log_string_from_level(log_level_t level); static void _chat_log_chat(const char *const login, const char *const other, const gchar *const msg, @@ -145,7 +143,7 @@ log_init(log_level_t filter) { level_filter = filter; tz = g_time_zone_new_local(); - gchar *log_file = _get_main_log_file(); + char *log_file = files_get_log_file(); logp = fopen(log_file, "a"); g_chmod(log_file, S_IRUSR | S_IWUSR); mainlogfile = g_string_new(log_file); @@ -226,7 +224,7 @@ log_level_from_string(char *log_level) static void _rotate_log_file(void) { - gchar *log_file = _get_main_log_file(); + char *log_file = files_get_log_file(); size_t len = strlen(log_file); char *log_file_new = malloc(len + 3); @@ -572,7 +570,7 @@ gboolean _key_equals(void *key1, void *key2) static char* _get_log_filename(const char *const other, const char *const login, GDateTime *dt, gboolean create) { - gchar *chatlogs_dir = _get_chatlog_dir(); + char *chatlogs_dir = files_get_data_path(DIR_CHATLOGS); GString *log_file = g_string_new(chatlogs_dir); free(chatlogs_dir); @@ -603,9 +601,9 @@ _get_log_filename(const char *const other, const char *const login, GDateTime *d static char* _get_groupchat_log_filename(const char *const room, const char *const login, GDateTime *dt, gboolean create) { - gchar *chatlogs_dir = _get_chatlog_dir(); + char *chatlogs_dir = files_get_data_path(DIR_CHATLOGS); GString *log_file = g_string_new(chatlogs_dir); - g_free(chatlogs_dir); + free(chatlogs_dir); gchar *login_dir = str_replace(login, "@", "_at_"); g_string_append_printf(log_file, "/%s", login_dir); @@ -636,36 +634,6 @@ _get_groupchat_log_filename(const char *const room, const char *const login, GDa return result; } -static gchar* -_get_chatlog_dir(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *chatlogs_dir = g_string_new(xdg_data); - g_string_append(chatlogs_dir, "/profanity/chatlogs"); - gchar *result = strdup(chatlogs_dir->str); - free(xdg_data); - g_string_free(chatlogs_dir, TRUE); - - return result; -} - -static gchar* -_get_main_log_file(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *logfile = g_string_new(xdg_data); - g_string_append(logfile, "/profanity/logs/profanity"); - if (!prefs_get_boolean(PREF_LOG_SHARED)) { - g_string_append_printf(logfile, "%d", getpid()); - } - g_string_append(logfile, ".log"); - gchar *result = strdup(logfile->str); - free(xdg_data); - g_string_free(logfile, TRUE); - - return result; -} - static char* _log_string_from_level(log_level_t level) { diff --git a/src/otr/otr.c b/src/otr/otr.c index 955e60fa..8aa3bac1 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -126,12 +126,11 @@ cb_write_fingerprints(void *opdata) { gcry_error_t err = 0; - gchar *data_home = files_get_xdg_data_home(); - GString *basedir = g_string_new(data_home); - free(data_home); - + char *otrdir = files_get_data_path(DIR_OTR); + GString *basedir = g_string_new(otrdir); + free(otrdir); gchar *account_dir = str_replace(jid, "@", "_at_"); - g_string_append(basedir, "/profanity/otr/"); + g_string_append(basedir, "/"); g_string_append(basedir, account_dir); g_string_append(basedir, "/"); free(account_dir); @@ -215,12 +214,11 @@ otr_on_connect(ProfAccount *account) jid = strdup(account->jid); log_info("Loading OTR key for %s", jid); - gchar *data_home = files_get_xdg_data_home(); - GString *basedir = g_string_new(data_home); - free(data_home); - + char *otrdir = files_get_data_path(DIR_OTR); + GString *basedir = g_string_new(otrdir); + free(otrdir); gchar *account_dir = str_replace(jid, "@", "_at_"); - g_string_append(basedir, "/profanity/otr/"); + g_string_append(basedir, "/"); g_string_append(basedir, account_dir); g_string_append(basedir, "/"); free(account_dir); @@ -393,12 +391,11 @@ otr_keygen(ProfAccount *account) jid = strdup(account->jid); log_info("Generating OTR key for %s", jid); - gchar *data_home = files_get_xdg_data_home(); - GString *basedir = g_string_new(data_home); - free(data_home); - + char *otrdir = files_get_data_path(DIR_OTR); + GString *basedir = g_string_new(otrdir); + free(otrdir); gchar *account_dir = str_replace(jid, "@", "_at_"); - g_string_append(basedir, "/profanity/otr/"); + g_string_append(basedir, "/"); g_string_append(basedir, account_dir); g_string_append(basedir, "/"); free(account_dir); diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index 6be3aaaa..f861ae97 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -157,12 +157,10 @@ p_gpg_close(void) void p_gpg_on_connect(const char *const barejid) { - gchar *data_home = files_get_xdg_data_home(); - GString *pubsfile = g_string_new(data_home); - free(data_home); - + char *pgpdir = files_get_data_path(DIR_PGP); + GString *pubsfile = g_string_new(pgpdir); gchar *account_dir = str_replace(barejid, "@", "_at_"); - g_string_append(pubsfile, "/profanity/pgp/"); + g_string_append(pubsfile, "/"); g_string_append(pubsfile, account_dir); free(account_dir); diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c index c7f8fcdb..d7de7352 100644 --- a/src/plugins/c_plugins.c +++ b/src/plugins/c_plugins.c @@ -41,6 +41,7 @@ #include "log.h" #include "config/preferences.h" +#include "config/files.h" #include "plugins/api.h" #include "plugins/callbacks.h" #include "plugins/plugins.h" @@ -60,9 +61,9 @@ c_plugin_create(const char *const filename) ProfPlugin *plugin; void *handle = NULL; - gchar *plugins_dir = plugins_get_dir(); + char *plugins_dir = files_get_data_path(DIR_PLUGINS); GString *path = g_string_new(plugins_dir); - g_free(plugins_dir); + free(plugins_dir); g_string_append(path, "/"); g_string_append(path, filename); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 2308e7f3..cdade652 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -131,7 +131,7 @@ plugins_init(void) gboolean plugins_install(const char *const plugin_name, const char *const filename) { - char *plugins_dir = plugins_get_dir(); + char *plugins_dir = files_get_data_path(DIR_PLUGINS); GString *target_path = g_string_new(plugins_dir); free(plugins_dir); g_string_append(target_path, "/"); @@ -266,7 +266,7 @@ GSList* plugins_unloaded_list(void) { GSList *result = NULL; - char *plugins_dir = plugins_get_dir(); + char *plugins_dir = files_get_data_path(DIR_PLUGINS); _plugins_unloaded_list_dir(plugins_dir, &result); free(plugins_dir); @@ -830,16 +830,3 @@ plugins_shutdown(void) callbacks_close(); disco_close(); } - -char* -plugins_get_dir(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *plugins_dir = g_string_new(xdg_data); - g_string_append(plugins_dir, "/profanity/plugins"); - char *result = strdup(plugins_dir->str); - g_free(xdg_data); - g_string_free(plugins_dir, TRUE); - - return result; -} diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index dc1c17f9..16c64fab 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -36,6 +36,7 @@ #include "config.h" #include "config/preferences.h" +#include "config/files.h" #include "plugins/api.h" #include "plugins/callbacks.h" #include "plugins/plugins.h" @@ -77,7 +78,7 @@ python_env_init(void) python_init_prof(); - gchar *plugins_dir = plugins_get_dir(); + char *plugins_dir = files_get_data_path(DIR_PLUGINS); GString *path = g_string_new("import sys\n"); g_string_append(path, "sys.path.append(\""); g_string_append(path, plugins_dir); diff --git a/src/plugins/settings.c b/src/plugins/settings.c index 418451ff..992390cf 100644 --- a/src/plugins/settings.c +++ b/src/plugins/settings.c @@ -49,24 +49,21 @@ static void _save_settings(void); void plugin_settings_init(void) { - gchar *xdg_data = files_get_xdg_data_home(); - GString *fileloc = g_string_new(xdg_data); - g_string_append(fileloc, "/profanity/plugin_settings"); - g_free(xdg_data); + char *settings_file = files_get_data_path(FILE_PLUGIN_SETTINGS); - if (g_file_test(fileloc->str, G_FILE_TEST_EXISTS)) { - g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + if (g_file_test(settings_file, G_FILE_TEST_EXISTS)) { + g_chmod(settings_file, S_IRUSR | S_IWUSR); } settings = g_key_file_new(); - g_key_file_load_from_file(settings, fileloc->str, G_KEY_FILE_KEEP_COMMENTS, NULL); + g_key_file_load_from_file(settings, settings_file, G_KEY_FILE_KEEP_COMMENTS, NULL); gsize g_data_size; gchar *g_data = g_key_file_to_data(settings, &g_data_size, NULL); - g_file_set_contents(fileloc->str, g_data, g_data_size, NULL); - g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + g_file_set_contents(settings_file, g_data, g_data_size, NULL); + g_chmod(settings_file, S_IRUSR | S_IWUSR); g_free(g_data); - g_string_free(fileloc, TRUE); + free(settings_file); } void @@ -135,19 +132,13 @@ _save_settings(void) gsize g_data_size; gchar *g_data = g_key_file_to_data(settings, &g_data_size, NULL); - gchar *xdg_data = files_get_xdg_data_home(); - GString *fileloc = g_string_new(xdg_data); - g_free(xdg_data); - - g_string_append(fileloc, "/profanity/"); - char *base = strdup(fileloc->str); - g_string_append(fileloc, "plugin_settings"); - - gchar *true_loc = get_file_or_linked(fileloc->str, base); - free(base); + char *fileloc = files_get_data_path(FILE_PLUGIN_SETTINGS); + gchar *base = g_path_get_basename(fileloc); + gchar *true_loc = get_file_or_linked(fileloc, base); + g_free(base); g_file_set_contents(true_loc, g_data, g_data_size, NULL); free(true_loc); g_free(g_data); - g_chmod(fileloc->str, S_IRUSR | S_IWUSR); - g_string_free(fileloc, TRUE); + g_chmod(fileloc, S_IRUSR | S_IWUSR); + free(fileloc); } diff --git a/src/plugins/themes.c b/src/plugins/themes.c index 061638e9..e2e62a51 100644 --- a/src/plugins/themes.c +++ b/src/plugins/themes.c @@ -32,6 +32,8 @@ * */ +#include + #include #include @@ -44,24 +46,21 @@ static GKeyFile *themes; void plugin_themes_init(void) { - gchar *xdg_data = files_get_xdg_data_home(); - GString *fileloc = g_string_new(xdg_data); - g_string_append(fileloc, "/profanity/plugin_themes"); - g_free(xdg_data); + char *themes_file = files_get_data_path(FILE_PLUGIN_THEMES); - if (g_file_test(fileloc->str, G_FILE_TEST_EXISTS)) { - g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + if (g_file_test(themes_file, G_FILE_TEST_EXISTS)) { + g_chmod(themes_file, S_IRUSR | S_IWUSR); } themes = g_key_file_new(); - g_key_file_load_from_file(themes, fileloc->str, G_KEY_FILE_KEEP_COMMENTS, NULL); + g_key_file_load_from_file(themes, themes_file, G_KEY_FILE_KEEP_COMMENTS, NULL); gsize g_data_size; gchar *g_data = g_key_file_to_data(themes, &g_data_size, NULL); - g_file_set_contents(fileloc->str, g_data, g_data_size, NULL); - g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + g_file_set_contents(themes_file, g_data, g_data_size, NULL); + g_chmod(themes_file, S_IRUSR | S_IWUSR); g_free(g_data); - g_string_free(fileloc, TRUE); + free(themes_file); } void diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index 6a4093e1..fb7c0b4a 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -428,7 +428,7 @@ _inp_rl_startup_hook(void) rl_variable_bind("disable-completion", "on"); // check for and load ~/.config/profanity/inputrc - char *inputrc = files_get_inputrc_path(); + char *inputrc = files_get_inputrc_file(); if (inputrc) { rl_read_init_file(inputrc); free(inputrc); diff --git a/src/ui/tray.c b/src/ui/tray.c index ab0d26e0..374eefb7 100644 --- a/src/ui/tray.c +++ b/src/ui/tray.c @@ -79,10 +79,8 @@ _get_icons(void) #endif /* ICONS_PATH */ - gchar *xdg_config = files_get_xdg_config_home(); - icons_dir = g_string_new(xdg_config); - g_free(xdg_config); - g_string_append(icons_dir, "/profanity/icons"); + char *icons_dir_s = files_get_config_path(DIR_ICONS); + icons_dir = g_string_new(icons_dir_s); GError *err = NULL; if (!g_file_test(icons_dir->str, G_FILE_TEST_IS_DIR)) { return; diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index b477b5f8..218ed2a2 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -61,7 +61,7 @@ #include "xmpp/form.h" #include "xmpp/capabilities.h" -static gchar *cache_loc; +static char *cache_loc; static GKeyFile *cache; static GHashTable *jid_to_ver; @@ -69,7 +69,6 @@ static GHashTable *jid_to_caps; static char *my_sha1; -static gchar* _get_cache_file(void); static void _save_cache(void); static Capabilities* _caps_by_ver(const char *const ver); static Capabilities* _caps_by_jid(const char *const jid); @@ -79,15 +78,14 @@ void caps_init(void) { log_info("Loading capabilities cache"); - cache_loc = _get_cache_file(); + cache_loc = files_get_data_path(FILE_CAPSCACHE); if (g_file_test(cache_loc, G_FILE_TEST_EXISTS)) { g_chmod(cache_loc, S_IRUSR | S_IWUSR); } cache = g_key_file_new(); - g_key_file_load_from_file(cache, cache_loc, G_KEY_FILE_KEEP_COMMENTS, - NULL); + g_key_file_load_from_file(cache, cache_loc, G_KEY_FILE_KEEP_COMMENTS, NULL); jid_to_ver = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); jid_to_caps = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)caps_destroy); @@ -678,6 +676,8 @@ caps_close(void) cache = NULL; g_hash_table_destroy(jid_to_ver); g_hash_table_destroy(jid_to_caps); + free(cache_loc); + cache_loc = NULL; } void @@ -698,19 +698,6 @@ caps_destroy(Capabilities *caps) } } -static gchar* -_get_cache_file(void) -{ - gchar *xdg_data = files_get_xdg_data_home(); - GString *cache_file = g_string_new(xdg_data); - g_string_append(cache_file, "/profanity/capscache"); - gchar *result = strdup(cache_file->str); - g_free(xdg_data); - g_string_free(cache_file, TRUE); - - return result; -} - static void _save_cache(void) { diff --git a/tests/unittests/helpers.c b/tests/unittests/helpers.c index 60f36839..f57bded1 100644 --- a/tests/unittests/helpers.c +++ b/tests/unittests/helpers.c @@ -15,17 +15,9 @@ void create_config_dir(void **state) { setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); - gchar *xdg_config = files_get_xdg_config_home(); - - GString *profanity_dir = g_string_new(xdg_config); - g_string_append(profanity_dir, "/profanity"); - - if (!mkdir_recursive(profanity_dir->str)) { + if (!mkdir_recursive("./tests/files/xdg_config_home/profanity")) { assert_true(FALSE); } - - g_free(xdg_config); - g_string_free(profanity_dir, TRUE); } void remove_config_dir(void **state) @@ -37,17 +29,9 @@ void remove_config_dir(void **state) void create_data_dir(void **state) { setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1); - gchar *xdg_data = files_get_xdg_data_home(); - - GString *profanity_dir = g_string_new(xdg_data); - g_string_append(profanity_dir, "/profanity"); - - if (!mkdir_recursive(profanity_dir->str)) { + if (!mkdir_recursive("./tests/files/xdg_data_home/profanity")) { assert_true(FALSE); } - - g_free(xdg_data); - g_string_free(profanity_dir, TRUE); } void remove_data_dir(void **state) From cbfc34f7c3acdb4063b7f4d552c89cebc2eafdbf Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 24 Jul 2016 22:32:39 +0100 Subject: [PATCH 7/7] Remove debug on plugin win create --- src/plugins/c_api.c | 1 - src/plugins/python_api.c | 1 - 2 files changed, 2 deletions(-) diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index 066f0f56..67116d31 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -218,7 +218,6 @@ static void c_api_win_create(const char *filename, char *tag, void(*callback)(char *tag, char *line)) { char *plugin_name = _c_plugin_name(filename); - log_debug("Win create %s for %s", tag, plugin_name); WindowWrapper *wrapper = malloc(sizeof(WindowWrapper)); wrapper->func = callback; diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 13b1f665..c5a1ef80 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -540,7 +540,6 @@ python_api_win_create(PyObject *self, PyObject *args) char *tag_str = python_str_or_unicode_to_string(tag); char *plugin_name = _python_plugin_name(); - log_debug("Win create %s for %s", tag_str, plugin_name); if (p_callback && PyCallable_Check(p_callback)) { allow_python_threads();