2014-04-07 10:42:10 +02:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-22 02:18:23 +02:00
|
|
|
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
|
|
|
#include "configdir.h"
|
2014-05-29 23:25:09 +02:00
|
|
|
|
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-06-21 01:58:00 +02:00
|
|
|
#include "device.h"
|
2014-05-29 23:25:09 +02:00
|
|
|
#endif /* _SUPPORT_AUDIO */
|
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
#include "settings.h"
|
2014-06-22 02:18:23 +02:00
|
|
|
#include "line_info.h"
|
2014-04-07 10:42:10 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_autolog(struct user_settings *s, const char *val);
|
|
|
|
static void uset_time(struct user_settings *s, const char *val);
|
|
|
|
static void uset_alerts(struct user_settings *s, const char *val);
|
|
|
|
static void uset_colours(struct user_settings *s, const char *val);
|
|
|
|
static void uset_hst_size(struct user_settings *s, const char *val);
|
|
|
|
static void uset_dwnld_path(struct user_settings *s, const char *val);
|
2014-05-29 23:25:09 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
static void uset_ain_dev(struct user_settings *s, const char *val);
|
|
|
|
static void uset_aout_dev(struct user_settings *s, const char *val);
|
|
|
|
#endif
|
2014-04-07 10:42:10 +02:00
|
|
|
|
|
|
|
struct {
|
2014-06-22 02:18:23 +02:00
|
|
|
const char *key;
|
|
|
|
void (*func)(struct user_settings *s, const char *val);
|
2014-04-19 23:58:13 +02:00
|
|
|
} user_settings_list[] = {
|
|
|
|
{ "autolog", uset_autolog },
|
2014-04-07 10:42:10 +02:00
|
|
|
{ "time", uset_time },
|
2014-04-12 03:47:09 +02:00
|
|
|
{ "disable_alerts", uset_alerts },
|
2014-04-07 10:42:10 +02:00
|
|
|
{ "colour_theme", uset_colours },
|
2014-06-22 02:18:23 +02:00
|
|
|
{ "history_size", uset_hst_size },
|
|
|
|
{ "download_path", uset_dwnld_path },
|
|
|
|
|
2014-05-29 23:25:09 +02:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-04-08 23:20:21 +02:00
|
|
|
{ "audio_in_dev", uset_ain_dev },
|
|
|
|
{ "audio_out_dev", uset_aout_dev },
|
2014-05-29 23:25:09 +02:00
|
|
|
#endif
|
2014-04-07 10:42:10 +02:00
|
|
|
};
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_autolog(struct user_settings *s, const char *val)
|
2014-04-08 23:20:21 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
/* default off if invalid value */
|
2014-06-22 02:18:23 +02:00
|
|
|
s->autolog = n == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF;
|
2014-04-07 10:42:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_time(struct user_settings *s, const char *val)
|
2014-04-07 10:42:10 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
/* default to 24 hour time if invalid value */
|
2014-06-22 02:18:23 +02:00
|
|
|
s->time = n == TIME_12 ? TIME_12 : TIME_24;
|
2014-04-07 10:42:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_alerts(struct user_settings *s, const char *val)
|
2014-04-12 03:47:09 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
2014-04-12 09:10:01 +02:00
|
|
|
/* alerts default on if invalid value */
|
2014-06-22 02:18:23 +02:00
|
|
|
s->alerts = n == ALERTS_DISABLED ? ALERTS_DISABLED : ALERTS_ENABLED;
|
2014-04-12 03:47:09 +02:00
|
|
|
}
|
2014-04-12 09:10:01 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_colours(struct user_settings *s, const char *val)
|
2014-04-07 10:42:10 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
/* use default toxic colours if invalid value */
|
2014-06-22 02:18:23 +02:00
|
|
|
s->colour_theme = n == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS;
|
2014-04-07 10:42:10 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 23:25:09 +02:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_ain_dev(struct user_settings *s, const char *val)
|
2014-04-08 23:20:21 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
|
|
|
if (n < 0 || n > MAX_DEVICES)
|
|
|
|
n = (long int) 0;
|
|
|
|
|
|
|
|
s->audio_in_dev = (long int) n;
|
2014-04-08 23:20:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_aout_dev(struct user_settings *s, const char *val)
|
2014-04-08 23:20:21 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = atoi(val);
|
|
|
|
|
|
|
|
if (n < 0 || n > MAX_DEVICES)
|
|
|
|
n = (long int) 0;
|
2014-04-08 23:20:21 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
s->audio_out_dev = (long int) n;
|
2014-04-08 23:20:21 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 23:25:09 +02:00
|
|
|
#endif /* _SUPPORT_AUDIO */
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void uset_hst_size(struct user_settings *s, const char *val)
|
|
|
|
{
|
|
|
|
int n = atoi(val);
|
|
|
|
|
|
|
|
/* if val is out of range use default history size */
|
|
|
|
s->history_size = (n > MAX_HISTORY || n < MIN_HISTORY) ? DFLT_HST_SIZE : n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uset_dwnld_path(struct user_settings *s, const char *val)
|
|
|
|
{
|
|
|
|
memset(s->download_path, 0, sizeof(s->download_path));
|
|
|
|
|
|
|
|
if (val == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int len = strlen(val);
|
|
|
|
|
|
|
|
if (len >= sizeof(s->download_path) - 2) /* leave room for null and '/' */
|
|
|
|
return;
|
|
|
|
|
|
|
|
FILE *fp = fopen(val, "r");
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
strcpy(s->download_path, val);
|
|
|
|
|
|
|
|
if (val[len - 1] != '/')
|
|
|
|
strcat(s->download_path, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_default_settings(struct user_settings *s)
|
|
|
|
{
|
|
|
|
/* see settings_values enum in settings.h for defaults */
|
|
|
|
uset_autolog(s, "0");
|
|
|
|
uset_time(s, "24");
|
|
|
|
uset_alerts(s, "0");
|
|
|
|
uset_colours(s, "0");
|
|
|
|
uset_hst_size(s, "700");
|
|
|
|
uset_dwnld_path(s, NULL);
|
|
|
|
|
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
uset_ain_dev(s, "0");
|
|
|
|
uset_aout_dev(s, "0");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
int settings_load(struct user_settings *s, char *path)
|
|
|
|
{
|
|
|
|
char *user_config_dir = get_user_config_dir();
|
|
|
|
FILE *fp = NULL;
|
2014-04-08 00:16:38 +02:00
|
|
|
char dflt_path[MAX_STR_SIZE];
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
if (path) {
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
} else {
|
|
|
|
snprintf(dflt_path, sizeof(dflt_path), "%s%stoxic.conf", user_config_dir, CONFIGDIR);
|
|
|
|
fp = fopen(dflt_path, "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(user_config_dir);
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
set_default_settings(s);
|
|
|
|
|
2014-04-20 22:42:37 +02:00
|
|
|
if (fp == NULL && !path) {
|
2014-04-08 00:16:38 +02:00
|
|
|
if ((fp = fopen(dflt_path, "w")) == NULL)
|
|
|
|
return -1;
|
2014-04-20 22:42:37 +02:00
|
|
|
} else if (fp == NULL && path) {
|
|
|
|
return -1;
|
2014-04-08 00:16:38 +02:00
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
char line[MAX_STR_SIZE];
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
|
|
if (line[0] == '#' || !line[0])
|
|
|
|
continue;
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-06-21 01:58:00 +02:00
|
|
|
const char *key = strtok(line, ":");
|
|
|
|
const char *val = strtok(NULL, ";");
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-06-21 01:58:00 +02:00
|
|
|
if (key == NULL || val == NULL)
|
2014-04-07 10:42:10 +02:00
|
|
|
continue;
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
int i;
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
for (i = 0; i < NUM_SETTINGS; ++i) {
|
2014-06-22 02:18:23 +02:00
|
|
|
if (strcmp(user_settings_list[i].key, key) == 0) {
|
|
|
|
(user_settings_list[i].func)(s, val);
|
2014-04-07 10:42:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
2014-04-08 00:16:38 +02:00
|
|
|
fclose(fp);
|
2014-04-07 10:42:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|