AmiTimeKeeper/val.c

430 lines
11 KiB
C
Raw Normal View History

2021-01-12 21:55:34 +00:00
/*-
2021-01-12 22:16:18 +00:00
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
2021-01-12 21:55:34 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2021-01-12 22:02:29 +00:00
*
2021-01-12 21:55:34 +00:00
*/
#include "config.h"
#include "message.h"
2021-01-12 22:02:29 +00:00
#include "setting.h"
2021-01-12 22:16:18 +00:00
#include "conv.h"
2021-01-12 21:55:34 +00:00
#include "log.h"
2021-01-12 22:16:18 +00:00
#include "mem.h"
#include "val.h"
#include "win.h"
2021-01-12 22:02:29 +00:00
#include "logmod.h"
2021-01-12 22:16:18 +00:00
#define MODULENAME "Settings"
2021-01-12 21:55:34 +00:00
static const char *settingChangedLong = "%s changed: %ld -> %ld";
2021-01-12 22:16:18 +00:00
static const char *settingChangedString = "%s changed: %s -> %s";
2021-01-12 21:55:34 +00:00
static const char *settingTooLow = "%s < %ld (too low)";
static const char *settingTooHigh = "%s > %ld (too high)";
static const char *settingGreaterThan = "%s * 2 > %s";
static void ValidateInterval(void)
{
2021-01-12 22:02:29 +00:00
if (Settings->Interval < INTERVAL_MIN)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooLow,
SettingKeys->Interval,
INTERVAL_MIN);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Interval,
Settings->Interval,
INTERVAL_MIN);
2021-01-12 22:02:29 +00:00
Settings->Interval = INTERVAL_MIN;
2021-01-12 21:55:34 +00:00
}
}
static void ValidateTimeout(void)
{
2021-01-12 22:02:29 +00:00
if (Settings->Timeout < TIMEOUT_MIN)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooLow,
SettingKeys->Timeout,
TIMEOUT_MIN);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MIN);
2021-01-12 22:02:29 +00:00
Settings->Timeout = TIMEOUT_MIN;
2021-01-12 21:55:34 +00:00
}
2021-01-12 22:02:29 +00:00
if (Settings->Timeout > Settings->Interval / 2)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingGreaterThan,
SettingKeys->Timeout,
SettingKeys->Interval);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
Settings->Interval / 2);
2021-01-12 22:02:29 +00:00
Settings->Timeout = Settings->Interval / 2;
2021-01-12 21:55:34 +00:00
}
2021-01-12 22:02:29 +00:00
if (Settings->Timeout > TIMEOUT_MAX)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooHigh,
SettingKeys->Timeout,
TIMEOUT_MAX);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MAX);
2021-01-12 22:02:29 +00:00
Settings->Timeout = TIMEOUT_MAX;
2021-01-12 21:55:34 +00:00
}
}
static void ValidatePriority(void)
{
2021-01-12 22:02:29 +00:00
if (Settings->Priority < PRIORITY_MIN)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooLow,
SettingKeys->Priority,
PRIORITY_MIN);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MIN);
2021-01-12 22:02:29 +00:00
Settings->Priority = PRIORITY_MIN;
2021-01-12 21:55:34 +00:00
}
2021-01-12 22:16:18 +00:00
else if (Settings->Priority > PRIORITY_MAX)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooHigh,
SettingKeys->Priority,
PRIORITY_MAX);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MAX);
2021-01-12 22:02:29 +00:00
Settings->Priority = PRIORITY_MAX;
2021-01-12 21:55:34 +00:00
}
}
static void ValidateThreshold(void)
{
2021-01-12 22:02:29 +00:00
if (Settings->Threshold < THRESHOLD_MIN)
2021-01-12 21:55:34 +00:00
{
2021-01-12 22:02:29 +00:00
LogInfo(settingTooLow,
SettingKeys->Threshold,
THRESHOLD_MIN);
2021-01-12 22:16:18 +00:00
LogNotice(settingChangedLong,
SettingKeys->Threshold,
Settings->Threshold,
THRESHOLD_MIN);
2021-01-12 22:02:29 +00:00
Settings->Threshold = THRESHOLD_MIN;
2021-01-12 21:55:34 +00:00
}
}
2021-01-12 22:16:18 +00:00
static bool ValidateServer(char *name)
{
return true;
}
static bool ValidatePort(char *name)
{
return true;
}
2021-01-12 21:55:34 +00:00
void SanitizeSettings(void)
{
2021-01-12 22:16:18 +00:00
LogTrace("Validating settings");
2021-01-12 21:55:34 +00:00
ValidateInterval();
ValidateTimeout();
ValidatePriority();
ValidateThreshold();
2021-01-12 22:16:18 +00:00
LogTrace("Settings validated");
}
void SetInterval(long value, long opt)
{
long oldValue = Settings->Interval;
if (Settings->Interval != value)
{
LogNotice(settingChangedLong,
SettingKeys->Interval,
Settings->Interval,
value);
Settings->Interval = value;
ValidateInterval();
}
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
Forbid();
if (WindowSettings != NULL)
{
WindowSettings->Interval = Settings->Interval;
}
Permit();
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiInterval();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && Settings->Interval != oldValue)
{
SendBrokerMessage(ATK_RESTART);
}
}
void SetTimeout(long value, long opt)
{
long oldValue = Settings->Timeout;
if (Settings->Timeout != value)
{
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
value);
Settings->Timeout = value;
ValidateTimeout();
}
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
Forbid();
if (WindowSettings != NULL)
{
WindowSettings->Timeout = Settings->Timeout;
}
Permit();
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiTimeout();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && Settings->Timeout != oldValue)
{
SendBrokerMessage(ATK_RESTART);
}
}
void SetThreshold(char *valueString, long opt)
{
unsigned long long value;
long long oldValue = Settings->Threshold;
int p = StrToLongLong(valueString, &value);
if (p != -1 && Settings->Threshold != value)
{
char newValueString[MAXLONGLONGCHARSIZE];
char oldValueString[MAXLONGLONGCHARSIZE];
LongLongToStr(Settings->Threshold, oldValueString);
LongLongToStr(value, newValueString);
LogNotice(settingChangedString,
SettingKeys->Threshold,
oldValueString,
newValueString);
Settings->Threshold = value;
ValidateThreshold();
}
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
Forbid();
if (WindowSettings != NULL)
{
WindowSettings->Threshold = Settings->Threshold;
}
Permit();
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiThreshold();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && Settings->Threshold != oldValue)
{
SendBrokerMessage(ATK_RESTART);
}
}
void SetPriority(char *valueString, long opt)
{
LONG value, oldValue = Settings->Priority;
int p = StrToLong((STRPTR)valueString, &value);
if (p != -1 && Settings->Priority != value)
{
LogNotice(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
value);
Settings->Priority = value;
ValidatePriority();
}
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
Forbid();
if (WindowSettings != NULL)
{
WindowSettings->Priority = Settings->Priority;
}
Permit();
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiCxPriority();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && Settings->Priority != oldValue)
{
SetBrokerPriority(Settings->Priority);
}
}
void SetServer(char *valueString, long opt)
{
char *oldSettingValue, *newSettingValue;
char *oldValue;
if (!ValidateServer(valueString))
return;
oldValue = StrDupSafe(Settings->DestinationAddress);
if (Stricmp((STRPTR)oldValue, (STRPTR)valueString) == 0)
{
FreeMemSafe(oldValue);
return;
}
LogNotice(settingChangedString,
SettingKeys->Threshold,
oldValue,
valueString);
FreeMemSafe(oldValue);
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
Deactivate();
}
newSettingValue = StrDupSafe(valueString);
Forbid();
oldSettingValue = Settings->DestinationAddress;
Settings->DestinationAddress = newSettingValue;
Permit();
FreeMemSafe(oldSettingValue);
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
newSettingValue = StrDupSafe(valueString);
oldSettingValue = NULL;
Forbid();
if (WindowSettings != NULL)
{
oldSettingValue = WindowSettings->DestinationAddress;
WindowSettings->DestinationAddress = newSettingValue;
}
Permit();
if (oldSettingValue == NULL)
FreeMemSafe(newSettingValue);
else
FreeMemSafe(oldSettingValue);
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiServer();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
Activate();
}
}
void SetPort(char *valueString, long opt)
{
char *oldSettingValue, *newSettingValue;
char *oldValue;
if (!ValidatePort(valueString))
return;
oldValue = StrDupSafe(Settings->DestinationPort);
if (Stricmp((STRPTR)oldValue, (STRPTR)valueString) == 0)
{
FreeMemSafe(oldValue);
return;
}
LogNotice(settingChangedString,
SettingKeys->DestinationPort,
oldValue,
valueString);
FreeMemSafe(oldValue);
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
Deactivate();
}
newSettingValue = StrDupSafe(valueString);
Forbid();
oldSettingValue = Settings->DestinationPort;
Settings->DestinationPort = newSettingValue;
Permit();
FreeMemSafe(oldSettingValue);
if ((opt & APPLY_UI_BACK) == APPLY_UI_BACK)
{
newSettingValue = StrDupSafe(valueString);
oldSettingValue = NULL;
Forbid();
if (WindowSettings != NULL)
{
oldSettingValue = WindowSettings->DestinationPort;
WindowSettings->DestinationPort = newSettingValue;
}
Permit();
if (oldSettingValue == NULL)
FreeMemSafe(newSettingValue);
else
FreeMemSafe(oldSettingValue);
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
WriteUiPort();
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
Activate();
}
2021-01-12 21:55:34 +00:00
}