1// Copyright 2023 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package setting
5
6import (
7 "sync"
8
9 "forgejo.org/modules/log"
10 "forgejo.org/modules/setting/config"
11)
12
13type PictureStruct struct {
14 DisableGravatar *config.Value[bool]
15 EnableFederatedAvatar *config.Value[bool]
16}
17
18type OpenWithEditorApp struct {
19 DisplayName string
20 OpenURL string
21}
22
23type OpenWithEditorAppsType []OpenWithEditorApp
24
25func (t OpenWithEditorAppsType) ToTextareaString() string {
26 ret := ""
27 for _, app := range t {
28 ret += app.DisplayName + " = " + app.OpenURL + "\n"
29 }
30 return ret
31}
32
33func DefaultOpenWithEditorApps() OpenWithEditorAppsType {
34 return OpenWithEditorAppsType{
35 {
36 DisplayName: "VS Code",
37 OpenURL: "vscode://vscode.git/clone?url={url}",
38 },
39 {
40 DisplayName: "VSCodium",
41 OpenURL: "vscodium://vscode.git/clone?url={url}",
42 },
43 {
44 DisplayName: "Intellij IDEA",
45 OpenURL: "jetbrains://idea/checkout/git?idea.required.plugins.id=Git4Idea&checkout.repo={url}",
46 },
47 }
48}
49
50type RepositoryStruct struct {
51 OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
52}
53
54type ConfigStruct struct {
55 Picture *PictureStruct
56 Repository *RepositoryStruct
57}
58
59var (
60 defaultConfig *ConfigStruct
61 defaultConfigOnce sync.Once
62)
63
64func initDefaultConfig() {
65 config.SetCfgSecKeyGetter(&cfgSecKeyGetter{})
66 defaultConfig = &ConfigStruct{
67 Picture: &PictureStruct{
68 DisableGravatar: config.ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "DISABLE_GRAVATAR"}),
69 EnableFederatedAvatar: config.ValueJSON[bool]("picture.enable_federated_avatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "ENABLE_FEDERATED_AVATAR"}),
70 },
71 Repository: &RepositoryStruct{
72 OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
73 },
74 }
75}
76
77func Config() *ConfigStruct {
78 defaultConfigOnce.Do(initDefaultConfig)
79 return defaultConfig
80}
81
82type cfgSecKeyGetter struct{}
83
84func (c cfgSecKeyGetter) GetValue(sec, key string) (v string, has bool) {
85 if key == "" {
86 return "", false
87 }
88 cfgSec, err := CfgProvider.GetSection(sec)
89 if err != nil {
90 log.Error("Unable to get config section: %q", sec)
91 return "", false
92 }
93 cfgKey := ConfigSectionKey(cfgSec, key)
94 if cfgKey == nil {
95 return "", false
96 }
97 return cfgKey.Value(), true
98}