From 92948abcf133b115964de682601cf53ea5e61780 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 7 Apr 2014 04:42:10 -0400 Subject: [PATCH] implement basic user settings --- build/Makefile.am | 4 +- configure.ac | 2 +- misc/toxic.conf | 8 ++++ src/chat.c | 3 ++ src/friendlist.c | 7 +++- src/groupchat.c | 8 +++- src/log.c | 6 ++- src/main.c | 21 ++++++++++ src/misc_tools.c | 5 ++- src/prompt.c | 10 +++++ src/settings.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 43 +++++++++++++++++++ 12 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 misc/toxic.conf create mode 100644 src/settings.c create mode 100644 src/settings.h diff --git a/build/Makefile.am b/build/Makefile.am index d3940e1..4bc008d 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -31,7 +31,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/file_senders.c \ $(top_srcdir)/src/file_senders.h \ $(top_srcdir)/src/line_info.c \ - $(top_srcdir)/src/line_info.h + $(top_srcdir)/src/line_info.h \ + $(top_srcdir)/src/settings.c \ + $(top_srcdir)/src/settings.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/configure.ac b/configure.ac index 0becaaf..146833c 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.3.2], [https://tox.im/]) +AC_INIT([toxic], [0.3.3], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) diff --git a/misc/toxic.conf b/misc/toxic.conf new file mode 100644 index 0000000..e3966d3 --- /dev/null +++ b/misc/toxic.conf @@ -0,0 +1,8 @@ +# 24 or 12 hour time +time:24; + +# 1 to enable autologging, 0 to disable +autolog:0; + +# 1 to use native terminal colours, 0 to use toxic default colour theme +colour_theme:0; diff --git a/src/chat.c b/src/chat.c index 9111423..3171d05 100644 --- a/src/chat.c +++ b/src/chat.c @@ -35,6 +35,7 @@ #include "toxic_strings.h" #include "log.h" #include "line_info.h" +#include "settings.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -45,7 +46,9 @@ extern int store_data(Tox *m, char *path); extern FileSender file_senders[MAX_FILES]; extern ToxicFriend friends[MAX_FRIENDS_NUM]; + extern struct _Winthread Winthread; +extern struct user_settings *user_settings; #ifdef _SUPPORT_AUDIO #define AC_NUM_CHAT_COMMANDS 23 diff --git a/src/friendlist.c b/src/friendlist.c index ecb9054..3fa1c0f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -35,6 +35,7 @@ #include "friendlist.h" #include "misc_tools.h" #include "line_info.h" +#include "settings.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -48,6 +49,8 @@ static int num_selected = 0; static int num_friends = 0; extern struct _Winthread Winthread; +extern struct user_settings *user_settings; + ToxicFriend friends[MAX_FRIENDS_NUM]; static int friendlist_index[MAX_FRIENDS_NUM] = {0}; @@ -89,7 +92,8 @@ static void update_friend_last_online(int32_t num, uint64_t timestamp) friends[num].last_online.tm = *localtime(×tamp); /* if the format changes make sure TIME_STR_SIZE is the correct size */ - strftime(friends[num].last_online.hour_min_str, TIME_STR_SIZE, "%I:%M %p", + const char *t = user_settings->time == TIME_24 ? "%H:%M" : "%I:%M %p"; + strftime(friends[num].last_online.hour_min_str, TIME_STR_SIZE, t, &friends[num].last_online.tm); } @@ -178,6 +182,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort) friends[i].online = false; friends[i].status = TOX_USERSTATUS_NONE; friends[i].namelength = tox_get_name(m, num, friends[i].name); + friends[i].logging_on = (bool) user_settings->autolog == AUTOLOG_ON; tox_get_client_id(m, num, friends[i].pub_key); update_friend_last_online(i, tox_get_last_online(m, i)); diff --git a/src/groupchat.c b/src/groupchat.c index 4d24182..efea2b4 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -36,6 +36,7 @@ #include "toxic_strings.h" #include "log.h" #include "line_info.h" +#include "settings.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); @@ -43,6 +44,8 @@ extern int store_data(Tox *m, char *path); static GroupChat groupchats[MAX_WINDOWS_NUM]; static int max_groupchat_index = 0; +extern struct user_settings *user_settings; + /* temporary until group chats have unique commands */ extern const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE]; @@ -657,8 +660,11 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) memset(ctx->log, 0, sizeof(struct chatlog)); line_info_init(ctx->hst); - print_groupchat_help(self); + + if (user_settings->autolog == AUTOLOG_ON) + log_enable(self->name, NULL, ctx->log); + execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); wmove(self->window, y-CURS_Y_OFFSET, 0); diff --git a/src/log.c b/src/log.c index 4a42a03..ed46505 100644 --- a/src/log.c +++ b/src/log.c @@ -32,6 +32,9 @@ #include "toxic_windows.h" #include "misc_tools.h" #include "log.h" +#include "settings.h" + +extern struct user_settings *user_settings; /* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) @@ -96,8 +99,9 @@ void write_to_log(const uint8_t *msg, uint8_t *name, struct chatlog *log, bool e else snprintf(name_frmt, sizeof(name_frmt), "%s:", name); + const char *t = user_settings->time == TIME_24 ? "%Y/%m/%d [%H:%M:%S]" : "%Y/%m/%d [%I:%M:%S %p]"; uint8_t s[MAX_STR_SIZE]; - strftime(s, MAX_STR_SIZE, "%Y/%m/%d [%H:%M:%S]", get_time()); + strftime(s, MAX_STR_SIZE, t, get_time()); fprintf(log->file,"%s %s %s\n", s, name_frmt, msg); uint64_t curtime = get_unix_time(); diff --git a/src/main.c b/src/main.c index 9cc4796..ed814f7 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ #include "misc_tools.h" #include "file_senders.h" #include "line_info.h" +#include "settings.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -81,6 +82,7 @@ ToxWindow *prompt = NULL; static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ struct _Winthread Winthread; +struct user_settings *user_settings = NULL; void on_window_resize(int sig) { @@ -414,6 +416,7 @@ void exit_toxic(Tox *m) free(prompt->chatwin->log); free(prompt->chatwin->hst); free(prompt->chatwin); + free(user_settings); tox_kill(m); #ifdef _SUPPORT_AUDIO terminate_audio(); @@ -502,6 +505,17 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + /* init user_settings struct and load settings from conf file */ + user_settings = malloc(sizeof(struct user_settings)); + + if (user_settings == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + int settings_err = settings_load(user_settings, NULL); + prompt = init_windows(m); /* create new thread for ncurses stuff */ @@ -545,6 +559,13 @@ int main(int argc, char *argv[]) line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + /* + if (settings_err == -1) { + msg = "Failed to load user settings"; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } + */ + sort_friendlist_index(); prompt_init_statusbar(prompt, m); diff --git a/src/misc_tools.c b/src/misc_tools.c index 0363f1d..53c42ce 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -31,8 +31,10 @@ #include "toxic_windows.h" #include "misc_tools.h" +#include "settings.h" extern ToxWindow *prompt; +extern struct user_settings *user_settings; static uint64_t current_unix_time; @@ -57,7 +59,8 @@ struct tm *get_time(void) void get_time_str(uint8_t *buf) { - strftime(buf, TIME_STR_SIZE, "[%H:%M:%S] ", get_time()); + const char *t = user_settings->time == TIME_24 ? "[%H:%M:%S] " : "[%I:%M:%S %p] "; + strftime(buf, TIME_STR_SIZE, t, get_time()); } /* XXX: FIX */ diff --git a/src/prompt.c b/src/prompt.c index 943cbf7..5280e61 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -34,12 +34,15 @@ #include "toxic_strings.h" #include "log.h" #include "line_info.h" +#include "settings.h" uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; uint8_t num_frnd_requests = 0; extern ToxWindow *prompt; struct _Winthread Winthread; +extern struct user_settings *user_settings; + /* Array of global command names used for tab completion. */ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = { { "/accept" }, @@ -489,6 +492,13 @@ static void prompt_onInit(ToxWindow *self, Tox *m) memset(ctx->hst, 0, sizeof(struct history)); line_info_init(ctx->hst); + + if (user_settings->autolog == AUTOLOG_ON) { + uint8_t myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, myid); + log_enable(self->name, myid, ctx->log); + } + execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE); wmove(ctx->history, y2-1, 2); diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..c798cae --- /dev/null +++ b/src/settings.c @@ -0,0 +1,103 @@ +/* settings.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic 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. + * + * Toxic 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 Toxic. If not, see . + * + */ + +#include +#include + +#include "toxic_windows.h" +#include "configdir.h" +#include "settings.h" + +static void uset_autolog(struct user_settings *s, int val); +static void uset_time(struct user_settings *s, int val); +static void uset_colours(struct user_settings *s, int val); + +struct { + const char *name; + void (*func)(struct user_settings *s, int val); +} user_settings_list[] = { + { "autolog", uset_autolog }, + { "time", uset_time }, + { "colour_theme", uset_colours }, +}; + +static void uset_autolog(struct user_settings *s, int val) +{ + /* default off if invalid value */ + s->autolog = val == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF; +} + +static void uset_time(struct user_settings *s, int val) +{ + /* default to 24 hour time if invalid value */ + s->time = val == TIME_12 ? TIME_12 : TIME_24; +} + +static void uset_colours(struct user_settings *s, int val) +{ + /* use default toxic colours if invalid value */ + s->colour_theme = val == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS; +} + +int settings_load(struct user_settings *s, char *path) +{ + char *user_config_dir = get_user_config_dir(); + FILE *fp = NULL; + + if (path) { + fp = fopen(path, "r"); + } else { + char dflt_path[MAX_STR_SIZE]; + snprintf(dflt_path, sizeof(dflt_path), "%s%stoxic.conf", user_config_dir, CONFIGDIR); + fp = fopen(dflt_path, "r"); + } + + free(user_config_dir); + + if (fp == NULL) + return -1; + + char line[MAX_STR_SIZE]; + + while (fgets(line, sizeof(line), fp)) { + if (line[0] == '#' || !line[0]) + continue; + + char *name = strtok(line, ":"); + char *val_s = strtok(NULL, ";"); + + if (name == NULL || val_s == NULL) + continue; + + int val = atoi(val_s); + int i; + + for (i = 0; i < NUM_SETTINGS; ++i) { + if (!strcmp(user_settings_list[i].name, name)) { + (user_settings_list[i].func)(s, val); + break; + } + } + } + + return 0; +} diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..66eaabc --- /dev/null +++ b/src/settings.h @@ -0,0 +1,43 @@ +/* settings.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic 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. + * + * Toxic 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 Toxic. If not, see . + * + */ + +#define NUM_SETTINGS 3 + +/* holds user setting values */ +struct user_settings { + int autolog; /* boolean */ + int time; /* 12 or 24 */ + int colour_theme; /* boolean (0 for default toxic colours) */ +}; + +enum { + AUTOLOG_OFF = 0, + AUTOLOG_ON = 1, + + TIME_24 = 24, + TIME_12 = 12, + + NATIVE_COLS = 1, + DFLT_COLS = 0, +}; + +int settings_load(struct user_settings *s, char *path);