2022-11-27 21:08:34 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-12-02 23:18:13 +01:00
|
|
|
"os"
|
2022-11-27 21:08:34 +01:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// UserConfig
|
2024-01-05 17:01:36 +01:00
|
|
|
SavePassword bool `json:"save_password,omitempty"`
|
|
|
|
SavedPassword string `json:"saved_password,omitempty"`
|
|
|
|
PasswordTip string `json:"password_tip,omitempty"`
|
|
|
|
PasswordTipUnlock string `json:"password_tip_unlock,omitempty"`
|
2022-11-27 21:08:34 +01:00
|
|
|
// Technical
|
2023-01-08 22:57:17 +01:00
|
|
|
IdentityFile string `json:"identity_file,omitempty"`
|
2022-11-27 21:08:34 +01:00
|
|
|
StoragePath string `json:"storage_path,omitempty"`
|
|
|
|
MaxIdsPerUser int `json:"max_ids_per_user,omitempty"`
|
|
|
|
MsgDbRollingPeriod int `json:"msg_db_rolling_period,omitempty"`
|
2022-12-18 21:52:03 +01:00
|
|
|
Chunksize int64 `json:"chunksize,omitempty"`
|
2023-02-15 22:08:17 +01:00
|
|
|
DbSize int `json:"db_size,omitempty"`
|
2023-11-27 00:31:43 +01:00
|
|
|
UserAgent string `json:"user_agent,omitempty"`
|
2024-04-20 22:29:22 +02:00
|
|
|
// Network
|
|
|
|
ServerPollInterval int `json:"server_poll_interval,omitempty"`
|
|
|
|
HttpTimeOut int `json:"http_timeout,omitempty"`
|
|
|
|
HttpLongPoll int `json:"http_long_poll,omitempty"`
|
2022-11-27 21:08:34 +01:00
|
|
|
// GUI
|
2023-09-27 17:16:23 +02:00
|
|
|
LastOpenChat string `json:"last_open_chat,omitempty"`
|
|
|
|
SoundNotificationEnable bool `json:"sound_notification_enable,omitempty"`
|
|
|
|
DefaultNotificationSound string `json:"default_notification_sound,omitempty"`
|
|
|
|
NotificationVibeEnable bool `json:"notification_vibe_enable,omitempty"`
|
|
|
|
DefaultNotificationVibe string `json:"default_notification_vibe,omitempty"`
|
|
|
|
NotificationLEDEnable bool `json:"notification_led_enable,omitempty"`
|
|
|
|
DefaultNotificationLEDColor string `json:"default_notification_led_color,omitempty"`
|
|
|
|
VisualNotificationEnable bool `json:"visual_notification_enable,omitempty"`
|
|
|
|
VisualNotificationModes string `json:"visual_notifiaction_modes,omitempty"`
|
|
|
|
PrivateChatNotificationsEnable bool `json:"private_chat_notifiactions_enable,omitempty"`
|
|
|
|
GroupChatNotificationsEnable bool `json:"group_chat_notifiactions_enable,omitempty"`
|
|
|
|
ChannelNotificationsEnable bool `json:"channel_notifications_enable,omitempty"`
|
|
|
|
Language string `json:"language,omitempty"`
|
|
|
|
Theme string `json:"theme,omitempty"`
|
2023-09-27 22:17:44 +02:00
|
|
|
FingerprintEnable bool `json:"fingerprint_enable,omitempty"`
|
|
|
|
ShowFavoriteContacts bool `json:"show_favorite_contacts,omitempty"`
|
2023-11-06 22:44:21 +01:00
|
|
|
NightModeEnable bool `json:"night_mode_enable,omitempty"`
|
2023-09-27 17:16:23 +02:00
|
|
|
|
2023-02-15 22:08:17 +01:00
|
|
|
// Debug
|
|
|
|
DbSuffix string `json:"db_suffix,omitempty"`
|
2023-01-08 22:57:17 +01:00
|
|
|
|
2022-11-27 21:08:34 +01:00
|
|
|
// Inner
|
2024-01-05 17:01:36 +01:00
|
|
|
memoryPassword string
|
|
|
|
additionalPasswords []string
|
|
|
|
me *Identity
|
2022-11-27 21:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var instance *Config
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
func GetConfig() *Config {
|
|
|
|
once.Do(func() {
|
|
|
|
instance = &Config{}
|
|
|
|
})
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Load(filename string) error {
|
2022-12-02 23:18:13 +01:00
|
|
|
data, err := os.ReadFile(filename)
|
2022-11-27 21:08:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-23 22:31:11 +02:00
|
|
|
// override values if not set or wrong
|
|
|
|
if c.HttpTimeOut <= 0 {
|
|
|
|
c.HttpTimeOut = 10
|
|
|
|
c.Save(filename)
|
|
|
|
}
|
|
|
|
if c.HttpLongPoll <= 1 {
|
|
|
|
c.HttpLongPoll = 300
|
|
|
|
c.Save(filename)
|
|
|
|
}
|
2022-11-27 21:08:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Save(filename string) error {
|
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 17:01:36 +01:00
|
|
|
os.WriteFile(filename, data, 0600)
|
2022-11-27 21:08:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) SetMemPass(pass string) {
|
|
|
|
c.memoryPassword = pass
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetMemPass() string {
|
|
|
|
return c.memoryPassword
|
|
|
|
}
|
2023-01-07 00:39:05 +01:00
|
|
|
|
2023-02-17 22:30:13 +01:00
|
|
|
func (c *Config) GetIdentity() *Identity {
|
|
|
|
return c.me
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) SetIdentity(id *Identity) {
|
|
|
|
c.me = id
|
|
|
|
}
|
|
|
|
|
2023-01-07 00:39:05 +01:00
|
|
|
func (c *Config) SaveIdentity() error {
|
|
|
|
return c.me.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Clean() {
|
|
|
|
c.additionalPasswords = []string{}
|
|
|
|
}
|