Keep your time right
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

569 lines
15 KiB

/*-
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
* 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.
*
*/
#include "config.h"
#include "message.h"
#include "setting.h"
#include "text.h"
#include "conv.h"
#include "sync.h"
#include "log.h"
#include "mem.h"
#include "val.h"
#include "win.h"
#include "tz.h"
#include "logmod.h"
#define MODULENAME "Settings"
static void ValidateInterval(void)
{
if (Settings->Interval < INTERVAL_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Interval,
INTERVAL_MIN);
LogNotice(settingChangedLong,
SettingKeys->Interval,
Settings->Interval,
INTERVAL_MIN);
Settings->Interval = INTERVAL_MIN;
}
else if (Settings->Interval > INTERVAL_MAX)
{
LogInfo(settingTooHigh,
SettingKeys->Interval,
INTERVAL_MAX);
LogNotice(settingChangedLong,
SettingKeys->Interval,
Settings->Interval,
INTERVAL_MAX);
Settings->Interval = INTERVAL_MAX;
}
}
static void ValidateTimeout(void)
{
if (Settings->Timeout < TIMEOUT_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Timeout,
TIMEOUT_MIN);
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MIN);
Settings->Timeout = TIMEOUT_MIN;
}
if (Settings->Timeout > Settings->Interval / 2)
{
LogInfo(settingGreaterThan,
SettingKeys->Timeout,
SettingKeys->Interval);
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
Settings->Interval / 2);
Settings->Timeout = Settings->Interval / 2;
}
if (Settings->Timeout > TIMEOUT_MAX)
{
LogInfo(settingTooHigh,
SettingKeys->Timeout,
TIMEOUT_MAX);
LogNotice(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MAX);
Settings->Timeout = TIMEOUT_MAX;
}
}
static void ValidatePriority(void)
{
if (Settings->Priority < PRIORITY_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Priority,
PRIORITY_MIN);
LogNotice(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MIN);
Settings->Priority = PRIORITY_MIN;
}
else if (Settings->Priority > PRIORITY_MAX)
{
LogInfo(settingTooHigh,
SettingKeys->Priority,
PRIORITY_MAX);
LogNotice(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MAX);
Settings->Priority = PRIORITY_MAX;
}
}
static void ValidateThreshold(void)
{
if (Settings->Threshold < THRESHOLD_MIN)
{
LogInfo(settingTooLow, SettingKeys->Threshold, THRESHOLD_MIN);
LogNotice(settingChangedLong, SettingKeys->Threshold,
Settings->Threshold, THRESHOLD_MIN);
Settings->Threshold = THRESHOLD_MIN;
}
}
static void ValidateTimeZoneDisplay(void)
{
if (Settings->TimeZoneDisplay < TZD_MIN)
{
LogInfo(settingTooLow, SettingKeys->TimeZoneDisplay, TZD_MIN);
LogNotice(settingChangedLong, SettingKeys->TimeZoneDisplay,
Settings->TimeZoneDisplay, TZD_MIN);
Settings->TimeZoneDisplay = TZD_MIN;
}
else if (Settings->TimeZoneDisplay > TZD_MAX)
{
LogInfo(settingTooHigh, SettingKeys->TimeZoneDisplay, TZD_MAX);
LogNotice(settingChangedLong, SettingKeys->TimeZoneDisplay,
Settings->TimeZoneDisplay, TZD_MAX);
Settings->TimeZoneDisplay = TZD_MAX;
}
}
static void ValidateTimeZoneValue(void)
{
if (Settings->TimeZoneValue < TZVALUE_MIN)
{
LogInfo(settingTooLow, SettingKeys->TimeZoneValue, TZVALUE_MIN);
LogNotice(settingChangedLong, SettingKeys->TimeZoneValue,
Settings->TimeZoneValue, TZVALUE_MIN);
Settings->TimeZoneValue = TZVALUE_MIN;
}
else if (Settings->TimeZoneValue != TZVALUE_DEF &&
Settings->TimeZoneValue > TZVALUE_MAX)
{
LogInfo(settingTooHigh, SettingKeys->TimeZoneValue, TZVALUE_MAX);
LogNotice(settingChangedLong, SettingKeys->TimeZoneValue,
Settings->TimeZoneValue, TZVALUE_MAX);
Settings->TimeZoneValue = TZVALUE_MAX;
}
if (Settings->TimeZoneValue % 60 >= 60)
{
long oldValue = Settings->TimeZoneValue;
Settings->TimeZoneValue = (Settings->TimeZoneValue / 60) * 60;
LogInfo(settingInvalid, SettingKeys->TimeZoneValue, oldValue);
LogNotice(settingChangedLong, SettingKeys->TimeZoneValue,
oldValue, Settings->TimeZoneValue);
}
}
static bool ValidateServer(char *name)
{
return true;
}
static bool ValidatePort(char *name)
{
return true;
}
void SanitizeSettings(void)
{
LogDebug(validatingSettings);
ValidateInterval();
ValidateTimeout();
ValidatePriority();
ValidateThreshold();
ValidateTimeZoneDisplay();
ValidateTimeZoneValue();
LogTrace(validatedSettings);
}
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);
bool active = SynchronizerRunning;
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && active)
{
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 && active)
{
while (SynchronizerRunning)
Delay(10);
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);
bool active = SynchronizerRunning;
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && active)
{
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 && active)
{
while (SynchronizerRunning)
Delay(10);
Activate();
}
}
void SetTimeZoneSetting(const char *settingKey, char **setting, char *valueString, long opt)
{
char *oldSettingValue, *newSettingValue;
char *oldValue;
oldValue = StrDupSafe(*setting);
if (Stricmp((STRPTR)oldValue, (STRPTR)valueString) == 0)
{
FreeMemSafe(oldValue);
return;
}
LogNotice(settingChangedString,
settingKey,
oldValue,
valueString);
FreeMemSafe(oldValue);
bool active = SynchronizerRunning;
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && active)
{
Deactivate();
}
newSettingValue = StrDupSafe(valueString);
Forbid();
oldSettingValue = *setting;
*setting = newSettingValue;
Permit();
FreeMemSafe(oldSettingValue);
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && settingKey == SettingKeys->TZ)
{
InitTimezone();
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
SendWindowMessage(ATK_TZ_CHANGED);
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME && active)
{
while (SynchronizerRunning)
Delay(10);
Activate();
}
}
void SetTimeZoneSettingLong(const char *settingKey, long *setting, char *valueString, long opt)
{
LONG value, oldValue = *setting;
int p = StrToLong((STRPTR)valueString, &value);
if (p != -1 && oldValue != value)
{
LogNotice(settingChangedLong,
settingKey,
oldValue,
value);
*setting = value;
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
if (settingKey == SettingKeys->TimeZoneValue || settingKey == SettingKeys->TimeZoneDst)
{
InitTimezone();
}
}
if ((opt & APPLY_UI_FRONT) == APPLY_UI_FRONT)
{
SendWindowMessage(ATK_TZ_CHANGED);
}
if ((opt & APPLY_RUNTIME) == APPLY_RUNTIME)
{
SendBrokerMessage(ATK_RESTART);
}
}