Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme committed Oct 22, 2024
1 parent e7ee0e3 commit 4464007
Show file tree
Hide file tree
Showing 9 changed files with 3,394 additions and 1 deletion.
3,002 changes: 3,002 additions & 0 deletions resources/locales.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/app/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type AppConfig = {
* Default: true on macOS, false otherwise.
*/
monochromeTrayIcon: boolean
/**
* List of languages to use for spell checking in addition to the system language.
* Default: [], use system preferences
*/
spellCheckLanguages: string[]

// ----------------
// Privacy settings
Expand All @@ -66,6 +71,7 @@ const defaultAppConfig: AppConfig = {
theme: 'default',
systemTitleBar: isLinux(),
monochromeTrayIcon: isMac(),
spellCheckLanguages: [],
}

/** Local cache of the config file mixed with the default values */
Expand Down
154 changes: 154 additions & 0 deletions src/app/__AppConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { join } from 'node:path'
import { readFile, writeFile } from 'node:fs/promises'
import { app } from 'electron'
import { isLinux, isMac } from '../shared/os.utils.js'

const APP_CONFIG_FILE_NAME = 'config.json'

// Windows: C:\Users\<username>\AppData\Roaming\Nextcloud Talk\config.json
// Linux: ~/.config/Nextcloud Talk/config.json (or $XDG_CONFIG_HOME)
// macOS: ~/Library/Application Support/Nextcloud Talk/config.json
const APP_CONFIG_FILE_PATH = join(app.getPath('userData'), APP_CONFIG_FILE_NAME)

/**
* Application level config. Applied to all accounts and persist on re-login.
* Stored in the application data directory.
*/
export type AppConfig = {
// ----------------
// General settings
// ----------------

/** Whether the app should run on startup. Not implemented */
runOnStartup: boolean

// -------------------
// Appearance settings
// -------------------

/** Whether to use a monochrome tray icon. By default, true only on Mac. Not implemented */
monochromeTrayIcon: boolean
/** The scale factor for the app. By default, 1. Not implemented */
scale: number
/** List of languages to use for spell checking in addition to the system language. By default, none. Not implemented */
spellCheckLanguages: string[]

// ----------------
// Privacy settings
// ----------------

/** Whether to show message previews in notifications. By default, true. Not implemented */
showMessagePreviewInNotifications: boolean

// ----------------------
// Notifications settings
// ----------------------
/**
* Whether to play a sound when a notification is received
* - always: always play sound
* - respect-dnd: play sound only if user status isn't Do-Not-Disturb [default]
* - never: disable notification sound
* Not implemented
*/
playSound: 'always' | 'respect-dnd' | 'never'
}

/**
* Get the default config
*/
const defaultAppConfig: AppConfig = {
runOnStartup: false,
theme: 'default',
systemTitleBar: isLinux(),
monochromeTrayIcon: isMac(),
scale: 1,
spellCheckLanguages: [],
showMessagePreviewInNotifications: true,
playSound: 'respect-dnd',
}

/** Local cache of the config file mixed with the default values */
const appConfig: Partial<AppConfig> = {}
/** Whether the application config has been read from the config file and ready to use */
let initialized = false

/**
* Read the application config from the file
*/
async function readAppConfigFile(): Promise<Partial<AppConfig>> {
try {
const content = await readFile(APP_CONFIG_FILE_PATH, 'utf-8')
return JSON.parse(content)
} catch (error) {
if (error instanceof Error && 'code' in error && error.code !== 'ENOENT') {
console.error('Failed to read the application config file', error)
}
// No file or invalid file - no custom config
return {}
}
}

/**
* Write the application config to the config file
* @param config - The config to write
*/
async function writeAppConfigFile(config: Partial<AppConfig>) {
try {
// Format for readability
const content = JSON.stringify(config, null, 2)
await writeFile(APP_CONFIG_FILE_PATH, content)
} catch (error) {
console.error('Failed to write the application config file', error)
throw error
}
}

/**
* Load the application config into the application memory
*/
export async function loadAppConfig() {
const config = await readAppConfigFile()
Object.assign(appConfig, config)
initialized = true
}

export function getAppConfig(): AppConfig
export function getAppConfig<T extends keyof AppConfig>(key?: T): AppConfig[T]
/**
* Get an application config value
* @param key - The config key to get
* @return - If key is provided, the value of the key. Otherwise, the full config
*/
export function getAppConfig<T extends keyof AppConfig>(key?: T): AppConfig | AppConfig[T] {
if (!initialized) {
throw new Error('The application config is not initialized yet')
}

const config = Object.assign({}, defaultAppConfig, appConfig)

if (key) {
return config[key]
}

return config
}

/**
* Set an application config value
* @param key - Settings key to set
* @param value - Value to set or undefined to reset to the default value
* @return Promise<AppConfig> - The full settings after the change
*/
export async function setAppConfig<K extends keyof AppConfig>(key: K, value?: AppConfig[K]) {
if (value !== undefined) {
appConfig[key] = value
} else {
delete appConfig[key]
}
await writeAppConfigFile(appConfig)
}
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const path = require('node:path')
const { app, dialog, BrowserWindow, ipcMain, desktopCapturer, systemPreferences, shell } = require('electron')
const { app, dialog, BrowserWindow, ipcMain, desktopCapturer, systemPreferences, shell, session } = require('electron')
const { setupMenu } = require('./app/app.menu.js')
const { setupReleaseNotificationScheduler } = require('./app/githubReleaseNotification.service.js')
const { enableWebRequestInterceptor, disableWebRequestInterceptor } = require('./app/webRequestInterceptor.js')
Expand Down Expand Up @@ -61,6 +61,7 @@ ipcMain.handle('app:getSystemL10n', () => ({
locale: app.getLocale().replace('-', '_'),
language: app.getPreferredSystemLanguages()[0].replace('-', '_'),
}))
ipcMain.handle('app:getAvailableSpellCheckerLanguages', () => session.defaultSession.availableSpellCheckerLanguages)
ipcMain.handle('app:enableWebRequestInterceptor', (event, ...args) => enableWebRequestInterceptor(...args))
ipcMain.handle('app:disableWebRequestInterceptor', (event, ...args) => disableWebRequestInterceptor(...args))
ipcMain.handle('app:setBadgeCount', async (event, count) => app.setBadgeCount(count))
Expand Down
6 changes: 6 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ const TALK_DESKTOP = {
* @return {Promise<{ locale: string, language: string }>}
*/
getSystemL10n: () => ipcRenderer.invoke('app:getSystemL10n'),
/**
* Get available spell checker languages
*
* @return {Promise<string[]>}
*/
getAvailableSpellCheckerLanguages: () => ipcRenderer.invoke('app:getAvailableSpellCheckerLanguages'),
/**
* Enable web request intercepting
*
Expand Down
31 changes: 31 additions & 0 deletions src/talk/renderer/Settings/DesktopSettingsSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { useAppConfigValue } from './useAppConfigValue.ts'
import { useNcSelectModel } from '../composables/useNcSelectModel.ts'
import { useAppConfig } from './appConfig.store.ts'
import { storeToRefs } from 'pinia'
import locales from '../../../../resources/locales.json'
import { computed, ref } from "vue";

const { isRelaunchRequired } = storeToRefs(useAppConfig())

Expand All @@ -28,6 +30,31 @@ const themeOption = useNcSelectModel(theme, themeOptions)

const systemTitleBar = useAppConfigValue('systemTitleBar')
const monochromeTrayIcon = useAppConfigValue('monochromeTrayIcon')
const spellCheckLanguages = useAppConfigValue('spellCheckLanguages')
const availableSpellCheckLanguages = ref<Set<string>>(new Set())

window.TALK_DESKTOP.getAvailableSpellCheckerLanguages().then((languages: string[]) => {
availableSpellCheckLanguages.value = new Set(languages.map((code) => code.replaceAll('_', '-')))
console.log('availableSpellCheckLanguages', languages)
})

const langToSelectOption = (code: string) => ({ label: locales.find(locale => locale), value: code }))
}

const spellCheckLanguagesOptions = computed(() => [
{ label: t('talk_desktop', 'System default'), value: 'default' },
...locales
.filter(({ code }) => availableSpellCheckLanguages.value.has(code))
.map(({ code, name }) => ({ label: name, value: code })),
])
const selectedSpellCheckLanguagesOption = computed(() => {
get() {
return
},
set(value) {

}
})

/**
* Restart the app
Expand Down Expand Up @@ -66,6 +93,10 @@ function relaunch() {
<NcCheckboxRadioSwitch :checked.sync="systemTitleBar" type="switch">
{{ t('talk_desktop', 'Use system title bar') }}
</NcCheckboxRadioSwitch>

<SettingsSelect v-model="spellCheckLanguagesOption" :options="spellCheckLanguagesOptions" searchable tags>
{{ t('talk_desktop', 'Spell check languages') }}
</SettingsSelect>
</SettingsSubsection>
</div>
</template>
Expand Down
3 changes: 3 additions & 0 deletions src/talk/renderer/Settings/components/SettingsSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const model = computed({

<script lang="ts">
export default {
inheritAttrs: false,

model: {
prop: 'modelValue',
event: 'update:modelValue',
Expand All @@ -47,6 +49,7 @@ export default {
:options="options"
:clearable="false"
:searchable="false"
v-bind="$attrs"
label-outside />
</label>
</template>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<script setup lang="ts">
import { t } from '@nextcloud/l10n'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcListItem from '@nextcloud/vue/dist/Components/NcListItem.js'
import IconAccountMultiplePlus from 'vue-material-design-icons/AccountMultiplePlus.vue'
import IconDelete from 'vue-material-design-icons/Delete.vue'
import SettingsSubsection from './SettingsSubsection.vue'
</script>

<template>
<SettingsSubsection>
<ul>
<NcListItem name="Grigorii K. Shartsev" details="nextcloud.local">
<template #icon>
<NcAvatar user="shgkme" />
</template>
<template #subname>
[email protected]
</template>
<template #actions>
<NcActionButton>
<template #icon>
<IconDelete :size="20" />
</template>
{{ t('talk_desktop', 'Remove') }}
</NcActionButton>
</template>
</NcListItem>
<NcListItem name="admin" details="stable30.local">
<template #icon>
<NcAvatar user="admin" />
</template>
<template #subname>
[email protected]
</template>
<template #actions>
<NcActionButton>
<template #icon>
<IconDelete :size="20" />
</template>
{{ t('talk_desktop', 'Remove') }}
</NcActionButton>
</template>
</NcListItem>
</ul>
<NcButton wide>
<template #icon>
<IconAccountMultiplePlus :size="20" />
</template>
{{ t('talk_desktop', 'Log into a new account') }}
</NcButton>
</SettingsSubsection>
</template>
Loading

0 comments on commit 4464007

Please sign in to comment.