cwtch-ui/lib/settings.dart

425 lines
14 KiB
Dart
Raw Normal View History

2021-06-24 23:10:45 +00:00
import 'dart:collection';
import 'dart:ui';
import 'dart:core';
2021-12-07 01:40:10 +00:00
import 'package:cwtch/themes/cwtch.dart';
2021-06-24 23:10:45 +00:00
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
2021-12-07 01:40:10 +00:00
import 'themes/opaque.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-06-24 23:10:45 +00:00
const TapirGroupsExperiment = "tapir-groups-experiment";
2021-10-29 23:37:02 +00:00
const ServerManagementExperiment = "servers-experiment";
2021-09-21 21:57:40 +00:00
const FileSharingExperiment = "filesharing";
2021-12-14 21:33:30 +00:00
const ImagePreviewsExperiment = "filesharing-images";
const ClickableLinksExperiment = "clickable-links";
const FormattingExperiment = "message-formatting";
2021-06-24 23:10:45 +00:00
enum DualpaneMode {
Single,
Dual1to2,
Dual1to4,
CopyPortrait,
}
enum NotificationPolicy {
Mute,
OptIn,
DefaultAll,
}
enum NotificationContent {
SimpleEvent,
ContactInfo,
}
2021-06-24 23:10:45 +00:00
/// Settings govern the *Globally* relevant settings like Locale, Theme and Experiments.
/// We also provide access to the version information here as it is also accessed from the
/// Settings Pane.
class Settings extends ChangeNotifier {
Locale locale;
late PackageInfo packageInfo;
OpaqueThemeType theme;
2021-06-24 23:10:45 +00:00
// explicitly set experiments to false until told otherwise...
bool experimentsEnabled = false;
HashMap<String, bool> experiments = HashMap.identity();
DualpaneMode _uiColumnModePortrait = DualpaneMode.Single;
DualpaneMode _uiColumnModeLandscape = DualpaneMode.CopyPortrait;
NotificationPolicy _notificationPolicy = NotificationPolicy.DefaultAll;
NotificationContent _notificationContent = NotificationContent.SimpleEvent;
2021-06-24 23:10:45 +00:00
bool blockUnknownConnections = false;
2021-09-14 16:05:07 +00:00
bool streamerMode = false;
2021-12-14 21:33:30 +00:00
String _downloadPath = "";
2021-06-24 23:10:45 +00:00
bool _allowAdvancedTorConfig = false;
bool _useCustomTorConfig = false;
String _customTorConfig = "";
int _socksPort = -1;
int _controlPort = -1;
String _customTorAuth = "";
2022-01-18 21:17:27 +00:00
bool _useTorCache = false;
String _torCacheDir = "";
bool _useSemanticDebugger = false;
2022-01-18 21:17:27 +00:00
String get torCacheDir => _torCacheDir;
set useSemanticDebugger(bool newval) {
this._useSemanticDebugger = newval;
notifyListeners();
}
bool get useSemanticDebugger => _useSemanticDebugger;
void setTheme(String themeId, String mode) {
theme = getTheme(themeId, mode);
2021-06-24 23:10:45 +00:00
notifyListeners();
}
/// Get access to the current theme.
OpaqueThemeType current() {
return theme;
}
/// isExperimentEnabled can be used to safely check whether a particular
/// experiment is enabled
bool isExperimentEnabled(String experiment) {
if (this.experimentsEnabled) {
if (this.experiments.containsKey(experiment)) {
// We now know it cannot be null...
return this.experiments[experiment]! == true;
}
}
2022-06-22 19:56:15 +00:00
// If message formatting has not explicitly been turned off, then
// turn it on by default.
if (experiment == FormattingExperiment) {
return true;
}
2021-06-24 23:10:45 +00:00
return false;
}
/// Called by the event bus. When new settings are loaded from a file the JSON will
/// be sent to the function and new settings will be instantiated based on the contents.
handleUpdate(dynamic settings) {
// Set Theme and notify listeners
this.setTheme(settings["Theme"], settings["ThemeMode"] ?? mode_dark);
2021-06-24 23:10:45 +00:00
// Set Locale and notify listeners
switchLocale(Locale(settings["Locale"]));
2021-09-14 20:48:25 +00:00
blockUnknownConnections = settings["BlockUnknownConnections"] ?? false;
streamerMode = settings["StreamerMode"] ?? false;
2021-06-24 23:10:45 +00:00
// Decide whether to enable Experiments
2021-09-14 20:48:25 +00:00
experimentsEnabled = settings["ExperimentsEnabled"] ?? false;
2021-06-24 23:10:45 +00:00
// Set the internal experiments map. Casting from the Map<dynamic, dynamic> that we get from JSON
experiments = new HashMap<String, bool>.from(settings["Experiments"]);
// single pane vs dual pane preferences
_uiColumnModePortrait = uiColumnModeFromString(settings["UIColumnModePortrait"]);
_uiColumnModeLandscape = uiColumnModeFromString(settings["UIColumnModeLandscape"]);
_notificationPolicy = notificationPolicyFromString(settings["NotificationPolicy"]);
_notificationContent = notificationContentFromString(settings["NotificationContent"]);
2021-12-17 01:04:29 +00:00
// auto-download folder
2021-12-14 21:33:30 +00:00
_downloadPath = settings["DownloadPath"] ?? "";
// allow a custom tor config
_allowAdvancedTorConfig = settings["AllowAdvancedTorConfig"] ?? false;
_useCustomTorConfig = settings["UseCustomTorrc"] ?? false;
_customTorConfig = settings["CustomTorrc"] ?? "";
_socksPort = settings["CustomSocksPort"] ?? -1;
_controlPort = settings["CustomControlPort"] ?? -1;
2022-01-18 21:17:27 +00:00
_useTorCache = settings["UseTorCache"] ?? false;
_torCacheDir = settings["TorCacheDir"] ?? "";
2021-06-24 23:10:45 +00:00
// Push the experimental settings to Consumers of Settings
notifyListeners();
}
/// Initialize the Package Version information
initPackageInfo() {
PackageInfo.fromPlatform().then((PackageInfo newPackageInfo) {
packageInfo = newPackageInfo;
notifyListeners();
});
}
/// Switch the Locale of the App
switchLocale(Locale newLocale) {
locale = newLocale;
notifyListeners();
}
2021-09-14 16:05:07 +00:00
setStreamerMode(bool newSteamerMode) {
streamerMode = newSteamerMode;
notifyListeners();
}
2021-06-24 23:10:45 +00:00
/// Block Unknown Connections will autoblock connections if they authenticate with public key not in our contacts list.
/// This is one of the best tools we have to combat abuse, while it isn't ideal it does allow a user to curate their contacts
/// list without being bothered by spurious requests (either permanently, or as a short term measure).
/// Note: This is not an *appear offline* setting which would explicitly close the listen port, rather than simply auto disconnecting unknown attempts.
forbidUnknownConnections() {
blockUnknownConnections = true;
notifyListeners();
}
/// Allow Unknown Connections will allow new contact requires from unknown public keys
/// See above for more information.
allowUnknownConnections() {
blockUnknownConnections = false;
notifyListeners();
}
/// Turn Experiments On, this will also have the side effect of enabling any
/// Experiments that have been previously activated.
enableExperiments() {
experimentsEnabled = true;
notifyListeners();
}
/// Turn Experiments Off. This will disable **all** active experiments.
/// Note: This will not set the preference for individual experiments, if experiments are enabled
/// any experiments that were active previously will become active again unless they are explicitly disabled.
disableExperiments() {
experimentsEnabled = false;
notifyListeners();
}
/// Turn on a specific experiment.
enableExperiment(String key) {
experiments.update(key, (value) => true, ifAbsent: () => true);
notifyListeners();
}
/// Turn off a specific experiment
disableExperiment(String key) {
experiments.update(key, (value) => false, ifAbsent: () => false);
notifyListeners();
}
DualpaneMode get uiColumnModePortrait => _uiColumnModePortrait;
2021-06-24 23:10:45 +00:00
set uiColumnModePortrait(DualpaneMode newval) {
this._uiColumnModePortrait = newval;
notifyListeners();
}
DualpaneMode get uiColumnModeLandscape => _uiColumnModeLandscape;
2021-06-24 23:10:45 +00:00
set uiColumnModeLandscape(DualpaneMode newval) {
this._uiColumnModeLandscape = newval;
notifyListeners();
}
NotificationPolicy get notificationPolicy => _notificationPolicy;
set notificationPolicy(NotificationPolicy newpol) {
this._notificationPolicy = newpol;
notifyListeners();
}
NotificationContent get notificationContent => _notificationContent;
set notificationContent(NotificationContent newcon) {
this._notificationContent = newcon;
notifyListeners();
}
2021-06-24 23:10:45 +00:00
List<int> uiColumns(bool isLandscape) {
var m = (!isLandscape || uiColumnModeLandscape == DualpaneMode.CopyPortrait) ? uiColumnModePortrait : uiColumnModeLandscape;
2021-06-30 20:59:52 +00:00
switch (m) {
case DualpaneMode.Single:
return [1];
case DualpaneMode.Dual1to2:
return [1, 2];
case DualpaneMode.Dual1to4:
return [1, 4];
2021-06-24 23:10:45 +00:00
}
print("impossible column configuration: portrait/$uiColumnModePortrait landscape/$uiColumnModeLandscape");
return [1];
}
static List<DualpaneMode> uiColumnModeOptions(bool isLandscape) {
2021-06-30 20:59:52 +00:00
if (isLandscape)
return [
DualpaneMode.CopyPortrait,
DualpaneMode.Single,
DualpaneMode.Dual1to2,
DualpaneMode.Dual1to4,
];
else
return [DualpaneMode.Single, DualpaneMode.Dual1to2, DualpaneMode.Dual1to4];
2021-06-24 23:10:45 +00:00
}
static DualpaneMode uiColumnModeFromString(String m) {
2021-06-30 20:59:52 +00:00
switch (m) {
case "DualpaneMode.Single":
return DualpaneMode.Single;
case "DualpaneMode.Dual1to2":
return DualpaneMode.Dual1to2;
case "DualpaneMode.Dual1to4":
return DualpaneMode.Dual1to4;
case "DualpaneMode.CopyPortrait":
return DualpaneMode.CopyPortrait;
2021-06-24 23:10:45 +00:00
}
print("Error: ui requested translation of column mode [$m] which doesn't exist");
return DualpaneMode.Single;
}
static String uiColumnModeToString(DualpaneMode m, BuildContext context) {
2021-06-30 20:59:52 +00:00
switch (m) {
case DualpaneMode.Single:
return AppLocalizations.of(context)!.settingUIColumnSingle;
2021-06-30 20:59:52 +00:00
case DualpaneMode.Dual1to2:
return AppLocalizations.of(context)!.settingUIColumnDouble12Ratio;
2021-06-30 20:59:52 +00:00
case DualpaneMode.Dual1to4:
return AppLocalizations.of(context)!.settingUIColumnDouble14Ratio;
2021-06-30 20:59:52 +00:00
case DualpaneMode.CopyPortrait:
return AppLocalizations.of(context)!.settingUIColumnOptionSame;
2021-06-24 23:10:45 +00:00
}
}
static NotificationPolicy notificationPolicyFromString(String? np) {
switch (np) {
case "NotificationPolicy.Mute":
return NotificationPolicy.Mute;
case "NotificationPolicy.OptIn":
return NotificationPolicy.OptIn;
case "NotificationPolicy.OptOut":
return NotificationPolicy.DefaultAll;
}
return NotificationPolicy.DefaultAll;
}
static NotificationContent notificationContentFromString(String? nc) {
switch (nc) {
case "NotificationContent.SimpleEvent":
return NotificationContent.SimpleEvent;
case "NotificationContent.ContactInfo":
return NotificationContent.ContactInfo;
}
return NotificationContent.SimpleEvent;
}
static String notificationPolicyToString(NotificationPolicy np, BuildContext context) {
switch (np) {
2022-02-14 19:03:28 +00:00
case NotificationPolicy.Mute:
return AppLocalizations.of(context)!.notificationPolicyMute;
case NotificationPolicy.OptIn:
return AppLocalizations.of(context)!.notificationPolicyOptIn;
case NotificationPolicy.DefaultAll:
return AppLocalizations.of(context)!.notificationPolicyDefaultAll;
}
}
static String notificationContentToString(NotificationContent nc, BuildContext context) {
switch (nc) {
2022-02-14 19:03:28 +00:00
case NotificationContent.SimpleEvent:
return AppLocalizations.of(context)!.notificationContentSimpleEvent;
case NotificationContent.ContactInfo:
return AppLocalizations.of(context)!.notificationContentContactInfo;
}
}
2021-12-19 02:09:18 +00:00
// checks experiment settings and file extension for image previews
// (ignores file size; if the user manually accepts the file, assume it's okay to preview)
bool shouldPreview(String path) {
var lpath = path.toLowerCase();
2022-01-10 20:28:12 +00:00
return isExperimentEnabled(ImagePreviewsExperiment) &&
(lpath.endsWith(".jpg") || lpath.endsWith(".jpeg") || lpath.endsWith(".png") || lpath.endsWith(".gif") || lpath.endsWith(".webp") || lpath.endsWith(".bmp"));
2021-12-19 02:09:18 +00:00
}
2021-12-14 21:33:30 +00:00
String get downloadPath => _downloadPath;
2021-12-14 21:33:30 +00:00
set downloadPath(String newval) {
_downloadPath = newval;
notifyListeners();
}
bool get allowAdvancedTorConfig => _allowAdvancedTorConfig;
set allowAdvancedTorConfig(bool torConfig) {
_allowAdvancedTorConfig = torConfig;
notifyListeners();
}
2022-01-18 21:17:27 +00:00
bool get useTorCache => _useTorCache;
2022-01-18 21:17:27 +00:00
set useTorCache(bool useTorCache) {
_useTorCache = useTorCache;
notifyListeners();
}
// Settings / Gettings for setting the custom tor config..
String get torConfig => _customTorConfig;
set torConfig(String torConfig) {
_customTorConfig = torConfig;
notifyListeners();
}
int get socksPort => _socksPort;
set socksPort(int newSocksPort) {
_socksPort = newSocksPort;
notifyListeners();
}
int get controlPort => _controlPort;
set controlPort(int controlPort) {
_controlPort = controlPort;
notifyListeners();
}
// Setters / Getters for toggling whether the app should use a custom tor config
bool get useCustomTorConfig => _useCustomTorConfig;
set useCustomTorConfig(bool useCustomTorConfig) {
_useCustomTorConfig = useCustomTorConfig;
notifyListeners();
}
2021-06-24 23:10:45 +00:00
/// Construct a default settings object.
Settings(this.locale, this.theme);
/// Convert this Settings object to a JSON representation for serialization on the
/// event bus.
dynamic asJson() {
return {
"Locale": this.locale.languageCode,
"Theme": theme.theme,
"ThemeMode": theme.mode,
2021-06-24 23:10:45 +00:00
"PreviousPid": -1,
"BlockUnknownConnections": blockUnknownConnections,
"NotificationPolicy": _notificationPolicy.toString(),
"NotificationContent": _notificationContent.toString(),
2021-09-14 16:05:07 +00:00
"StreamerMode": streamerMode,
2021-06-24 23:10:45 +00:00
"ExperimentsEnabled": this.experimentsEnabled,
"Experiments": experiments,
"StateRootPane": 0,
"FirstTime": false,
"UIColumnModePortrait": uiColumnModePortrait.toString(),
"UIColumnModeLandscape": uiColumnModeLandscape.toString(),
2021-12-14 21:33:30 +00:00
"DownloadPath": _downloadPath,
"AllowAdvancedTorConfig": _allowAdvancedTorConfig,
"CustomTorRc": _customTorConfig,
"UseCustomTorrc": _useCustomTorConfig,
"CustomSocksPort": _socksPort,
"CustomControlPort": _controlPort,
"CustomAuth": _customTorAuth,
2022-01-18 21:17:27 +00:00
"UseTorCache": _useTorCache,
"TorCacheDir": _torCacheDir
2021-06-24 23:10:45 +00:00
};
}
}