|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { join } from 'node:path' |
| 7 | +import { readFile, writeFile } from 'node:fs/promises' |
| 8 | +import { app } from 'electron' |
| 9 | +import { isLinux, isMac } from '../shared/os.utils.js' |
| 10 | + |
| 11 | +const APP_CONFIG_FILE_NAME = 'config.json' |
| 12 | + |
| 13 | +// Windows: C:\Users\<username>\AppData\Roaming\Nextcloud Talk\config.json |
| 14 | +// Linux: ~/.config/Nextcloud Talk/config.json (or $XDG_CONFIG_HOME) |
| 15 | +// macOS: ~/Library/Application Support/Nextcloud Talk/config.json |
| 16 | +const APP_CONFIG_FILE_PATH = join(app.getPath('userData'), APP_CONFIG_FILE_NAME) |
| 17 | + |
| 18 | +/** |
| 19 | + * Application level config. Applied to all accounts and persist on re-login. |
| 20 | + * Stored in the application data directory. |
| 21 | + */ |
| 22 | +export type AppConfig = { |
| 23 | + // ---------------- |
| 24 | + // General settings |
| 25 | + // ---------------- |
| 26 | + |
| 27 | + /** Whether the app should run on startup. Not implemented */ |
| 28 | + runOnStartup: boolean |
| 29 | + |
| 30 | + // ------------------- |
| 31 | + // Appearance settings |
| 32 | + // ------------------- |
| 33 | + |
| 34 | + /** Whether to use a monochrome tray icon. By default, true only on Mac. Not implemented */ |
| 35 | + monochromeTrayIcon: boolean |
| 36 | + /** The scale factor for the app. By default, 1. Not implemented */ |
| 37 | + scale: number |
| 38 | + /** List of languages to use for spell checking in addition to the system language. By default, none. Not implemented */ |
| 39 | + spellCheckLanguages: string[] |
| 40 | + |
| 41 | + // ---------------- |
| 42 | + // Privacy settings |
| 43 | + // ---------------- |
| 44 | + |
| 45 | + /** Whether to show message previews in notifications. By default, true. Not implemented */ |
| 46 | + showMessagePreviewInNotifications: boolean |
| 47 | + |
| 48 | + // ---------------------- |
| 49 | + // Notifications settings |
| 50 | + // ---------------------- |
| 51 | + /** |
| 52 | + * Whether to play a sound when a notification is received |
| 53 | + * - always: always play sound |
| 54 | + * - respect-dnd: play sound only if user status isn't Do-Not-Disturb [default] |
| 55 | + * - never: disable notification sound |
| 56 | + * Not implemented |
| 57 | + */ |
| 58 | + playSound: 'always' | 'respect-dnd' | 'never' |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Get the default config |
| 63 | + */ |
| 64 | +const defaultAppConfig: AppConfig = { |
| 65 | + runOnStartup: false, |
| 66 | + theme: 'default', |
| 67 | + systemTitleBar: isLinux(), |
| 68 | + monochromeTrayIcon: isMac(), |
| 69 | + scale: 1, |
| 70 | + spellCheckLanguages: [], |
| 71 | + showMessagePreviewInNotifications: true, |
| 72 | + playSound: 'respect-dnd', |
| 73 | +} |
| 74 | + |
| 75 | +/** Local cache of the config file mixed with the default values */ |
| 76 | +const appConfig: Partial<AppConfig> = {} |
| 77 | +/** Whether the application config has been read from the config file and ready to use */ |
| 78 | +let initialized = false |
| 79 | + |
| 80 | +/** |
| 81 | + * Read the application config from the file |
| 82 | + */ |
| 83 | +async function readAppConfigFile(): Promise<Partial<AppConfig>> { |
| 84 | + try { |
| 85 | + const content = await readFile(APP_CONFIG_FILE_PATH, 'utf-8') |
| 86 | + return JSON.parse(content) |
| 87 | + } catch (error) { |
| 88 | + if (error instanceof Error && 'code' in error && error.code !== 'ENOENT') { |
| 89 | + console.error('Failed to read the application config file', error) |
| 90 | + } |
| 91 | + // No file or invalid file - no custom config |
| 92 | + return {} |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Write the application config to the config file |
| 98 | + * @param config - The config to write |
| 99 | + */ |
| 100 | +async function writeAppConfigFile(config: Partial<AppConfig>) { |
| 101 | + try { |
| 102 | + // Format for readability |
| 103 | + const content = JSON.stringify(config, null, 2) |
| 104 | + await writeFile(APP_CONFIG_FILE_PATH, content) |
| 105 | + } catch (error) { |
| 106 | + console.error('Failed to write the application config file', error) |
| 107 | + throw error |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Load the application config into the application memory |
| 113 | + */ |
| 114 | +export async function loadAppConfig() { |
| 115 | + const config = await readAppConfigFile() |
| 116 | + Object.assign(appConfig, config) |
| 117 | + initialized = true |
| 118 | +} |
| 119 | + |
| 120 | +export function getAppConfig(): AppConfig |
| 121 | +export function getAppConfig<T extends keyof AppConfig>(key?: T): AppConfig[T] |
| 122 | +/** |
| 123 | + * Get an application config value |
| 124 | + * @param key - The config key to get |
| 125 | + * @return - If key is provided, the value of the key. Otherwise, the full config |
| 126 | + */ |
| 127 | +export function getAppConfig<T extends keyof AppConfig>(key?: T): AppConfig | AppConfig[T] { |
| 128 | + if (!initialized) { |
| 129 | + throw new Error('The application config is not initialized yet') |
| 130 | + } |
| 131 | + |
| 132 | + const config = Object.assign({}, defaultAppConfig, appConfig) |
| 133 | + |
| 134 | + if (key) { |
| 135 | + return config[key] |
| 136 | + } |
| 137 | + |
| 138 | + return config |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Set an application config value |
| 143 | + * @param key - Settings key to set |
| 144 | + * @param value - Value to set or undefined to reset to the default value |
| 145 | + * @return Promise<AppConfig> - The full settings after the change |
| 146 | + */ |
| 147 | +export async function setAppConfig<K extends keyof AppConfig>(key: K, value?: AppConfig[K]) { |
| 148 | + if (value !== undefined) { |
| 149 | + appConfig[key] = value |
| 150 | + } else { |
| 151 | + delete appConfig[key] |
| 152 | + } |
| 153 | + await writeAppConfigFile(appConfig) |
| 154 | +} |
0 commit comments