@@ -12,165 +12,165 @@ |
||
12 | 12 | use OCP\IUserSession; |
13 | 13 | |
14 | 14 | class UserConfig { |
15 | - public const ALLOWED_CONFIGS = [ |
|
16 | - [ |
|
17 | - // Whether to crop the files previews or not in the files list |
|
18 | - 'key' => 'crop_image_previews', |
|
19 | - 'default' => true, |
|
20 | - 'allowed' => [true, false], |
|
21 | - ], |
|
22 | - [ |
|
23 | - // The view to start the files app in |
|
24 | - 'key' => 'default_view', |
|
25 | - 'default' => 'files', |
|
26 | - 'allowed' => ['files', 'personal'], |
|
27 | - ], |
|
28 | - [ |
|
29 | - // Whether to show the folder tree |
|
30 | - 'key' => 'folder_tree', |
|
31 | - 'default' => true, |
|
32 | - 'allowed' => [true, false], |
|
33 | - ], |
|
34 | - [ |
|
35 | - // Whether to show the files list in grid view or not |
|
36 | - 'key' => 'grid_view', |
|
37 | - 'default' => false, |
|
38 | - 'allowed' => [true, false], |
|
39 | - ], |
|
40 | - [ |
|
41 | - // Whether to show the "confirm file deletion" warning |
|
42 | - 'key' => 'show_dialog_deletion', |
|
43 | - 'default' => false, |
|
44 | - 'allowed' => [true, false], |
|
45 | - ], |
|
46 | - [ |
|
47 | - // Whether to show the "confirm file extension change" warning |
|
48 | - 'key' => 'show_dialog_file_extension', |
|
49 | - 'default' => true, |
|
50 | - 'allowed' => [true, false], |
|
51 | - ], |
|
52 | - [ |
|
53 | - // Whether to show the hidden files or not in the files list |
|
54 | - 'key' => 'show_hidden', |
|
55 | - 'default' => false, |
|
56 | - 'allowed' => [true, false], |
|
57 | - ], |
|
58 | - [ |
|
59 | - // Whether to show the mime column or not |
|
60 | - 'key' => 'show_mime_column', |
|
61 | - 'default' => false, |
|
62 | - 'allowed' => [true, false], |
|
63 | - ], |
|
64 | - [ |
|
65 | - // Whether to sort favorites first in the list or not |
|
66 | - 'key' => 'sort_favorites_first', |
|
67 | - 'default' => true, |
|
68 | - 'allowed' => [true, false], |
|
69 | - ], |
|
70 | - [ |
|
71 | - // Whether to sort folders before files in the list or not |
|
72 | - 'key' => 'sort_folders_first', |
|
73 | - 'default' => true, |
|
74 | - 'allowed' => [true, false], |
|
75 | - ], |
|
76 | - ]; |
|
77 | - protected ?IUser $user = null; |
|
15 | + public const ALLOWED_CONFIGS = [ |
|
16 | + [ |
|
17 | + // Whether to crop the files previews or not in the files list |
|
18 | + 'key' => 'crop_image_previews', |
|
19 | + 'default' => true, |
|
20 | + 'allowed' => [true, false], |
|
21 | + ], |
|
22 | + [ |
|
23 | + // The view to start the files app in |
|
24 | + 'key' => 'default_view', |
|
25 | + 'default' => 'files', |
|
26 | + 'allowed' => ['files', 'personal'], |
|
27 | + ], |
|
28 | + [ |
|
29 | + // Whether to show the folder tree |
|
30 | + 'key' => 'folder_tree', |
|
31 | + 'default' => true, |
|
32 | + 'allowed' => [true, false], |
|
33 | + ], |
|
34 | + [ |
|
35 | + // Whether to show the files list in grid view or not |
|
36 | + 'key' => 'grid_view', |
|
37 | + 'default' => false, |
|
38 | + 'allowed' => [true, false], |
|
39 | + ], |
|
40 | + [ |
|
41 | + // Whether to show the "confirm file deletion" warning |
|
42 | + 'key' => 'show_dialog_deletion', |
|
43 | + 'default' => false, |
|
44 | + 'allowed' => [true, false], |
|
45 | + ], |
|
46 | + [ |
|
47 | + // Whether to show the "confirm file extension change" warning |
|
48 | + 'key' => 'show_dialog_file_extension', |
|
49 | + 'default' => true, |
|
50 | + 'allowed' => [true, false], |
|
51 | + ], |
|
52 | + [ |
|
53 | + // Whether to show the hidden files or not in the files list |
|
54 | + 'key' => 'show_hidden', |
|
55 | + 'default' => false, |
|
56 | + 'allowed' => [true, false], |
|
57 | + ], |
|
58 | + [ |
|
59 | + // Whether to show the mime column or not |
|
60 | + 'key' => 'show_mime_column', |
|
61 | + 'default' => false, |
|
62 | + 'allowed' => [true, false], |
|
63 | + ], |
|
64 | + [ |
|
65 | + // Whether to sort favorites first in the list or not |
|
66 | + 'key' => 'sort_favorites_first', |
|
67 | + 'default' => true, |
|
68 | + 'allowed' => [true, false], |
|
69 | + ], |
|
70 | + [ |
|
71 | + // Whether to sort folders before files in the list or not |
|
72 | + 'key' => 'sort_folders_first', |
|
73 | + 'default' => true, |
|
74 | + 'allowed' => [true, false], |
|
75 | + ], |
|
76 | + ]; |
|
77 | + protected ?IUser $user = null; |
|
78 | 78 | |
79 | - public function __construct( |
|
80 | - protected IConfig $config, |
|
81 | - IUserSession $userSession, |
|
82 | - ) { |
|
83 | - $this->user = $userSession->getUser(); |
|
84 | - } |
|
79 | + public function __construct( |
|
80 | + protected IConfig $config, |
|
81 | + IUserSession $userSession, |
|
82 | + ) { |
|
83 | + $this->user = $userSession->getUser(); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the list of all allowed user config keys |
|
88 | - * @return string[] |
|
89 | - */ |
|
90 | - public function getAllowedConfigKeys(): array { |
|
91 | - return array_map(function ($config) { |
|
92 | - return $config['key']; |
|
93 | - }, self::ALLOWED_CONFIGS); |
|
94 | - } |
|
86 | + /** |
|
87 | + * Get the list of all allowed user config keys |
|
88 | + * @return string[] |
|
89 | + */ |
|
90 | + public function getAllowedConfigKeys(): array { |
|
91 | + return array_map(function ($config) { |
|
92 | + return $config['key']; |
|
93 | + }, self::ALLOWED_CONFIGS); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get the list of allowed config values for a given key |
|
98 | - * |
|
99 | - * @param string $key a valid config key |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - private function getAllowedConfigValues(string $key): array { |
|
103 | - foreach (self::ALLOWED_CONFIGS as $config) { |
|
104 | - if ($config['key'] === $key) { |
|
105 | - return $config['allowed']; |
|
106 | - } |
|
107 | - } |
|
108 | - return []; |
|
109 | - } |
|
96 | + /** |
|
97 | + * Get the list of allowed config values for a given key |
|
98 | + * |
|
99 | + * @param string $key a valid config key |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + private function getAllowedConfigValues(string $key): array { |
|
103 | + foreach (self::ALLOWED_CONFIGS as $config) { |
|
104 | + if ($config['key'] === $key) { |
|
105 | + return $config['allowed']; |
|
106 | + } |
|
107 | + } |
|
108 | + return []; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Get the default config value for a given key |
|
113 | - * |
|
114 | - * @param string $key a valid config key |
|
115 | - * @return string|bool |
|
116 | - */ |
|
117 | - private function getDefaultConfigValue(string $key) { |
|
118 | - foreach (self::ALLOWED_CONFIGS as $config) { |
|
119 | - if ($config['key'] === $key) { |
|
120 | - return $config['default']; |
|
121 | - } |
|
122 | - } |
|
123 | - return ''; |
|
124 | - } |
|
111 | + /** |
|
112 | + * Get the default config value for a given key |
|
113 | + * |
|
114 | + * @param string $key a valid config key |
|
115 | + * @return string|bool |
|
116 | + */ |
|
117 | + private function getDefaultConfigValue(string $key) { |
|
118 | + foreach (self::ALLOWED_CONFIGS as $config) { |
|
119 | + if ($config['key'] === $key) { |
|
120 | + return $config['default']; |
|
121 | + } |
|
122 | + } |
|
123 | + return ''; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Set a user config |
|
128 | - * |
|
129 | - * @param string $key |
|
130 | - * @param string|bool $value |
|
131 | - * @throws \Exception |
|
132 | - * @throws \InvalidArgumentException |
|
133 | - */ |
|
134 | - public function setConfig(string $key, $value): void { |
|
135 | - if ($this->user === null) { |
|
136 | - throw new \Exception('No user logged in'); |
|
137 | - } |
|
126 | + /** |
|
127 | + * Set a user config |
|
128 | + * |
|
129 | + * @param string $key |
|
130 | + * @param string|bool $value |
|
131 | + * @throws \Exception |
|
132 | + * @throws \InvalidArgumentException |
|
133 | + */ |
|
134 | + public function setConfig(string $key, $value): void { |
|
135 | + if ($this->user === null) { |
|
136 | + throw new \Exception('No user logged in'); |
|
137 | + } |
|
138 | 138 | |
139 | - if (!in_array($key, $this->getAllowedConfigKeys())) { |
|
140 | - throw new \InvalidArgumentException('Unknown config key'); |
|
141 | - } |
|
139 | + if (!in_array($key, $this->getAllowedConfigKeys())) { |
|
140 | + throw new \InvalidArgumentException('Unknown config key'); |
|
141 | + } |
|
142 | 142 | |
143 | - if (!in_array($value, $this->getAllowedConfigValues($key))) { |
|
144 | - throw new \InvalidArgumentException('Invalid config value'); |
|
145 | - } |
|
143 | + if (!in_array($value, $this->getAllowedConfigValues($key))) { |
|
144 | + throw new \InvalidArgumentException('Invalid config value'); |
|
145 | + } |
|
146 | 146 | |
147 | - if (is_bool($value)) { |
|
148 | - $value = $value ? '1' : '0'; |
|
149 | - } |
|
147 | + if (is_bool($value)) { |
|
148 | + $value = $value ? '1' : '0'; |
|
149 | + } |
|
150 | 150 | |
151 | - $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value); |
|
152 | - } |
|
151 | + $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * Get the current user configs array |
|
156 | - * |
|
157 | - * @return array |
|
158 | - */ |
|
159 | - public function getConfigs(): array { |
|
160 | - if ($this->user === null) { |
|
161 | - throw new \Exception('No user logged in'); |
|
162 | - } |
|
154 | + /** |
|
155 | + * Get the current user configs array |
|
156 | + * |
|
157 | + * @return array |
|
158 | + */ |
|
159 | + public function getConfigs(): array { |
|
160 | + if ($this->user === null) { |
|
161 | + throw new \Exception('No user logged in'); |
|
162 | + } |
|
163 | 163 | |
164 | - $userId = $this->user->getUID(); |
|
165 | - $userConfigs = array_map(function (string $key) use ($userId) { |
|
166 | - $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key)); |
|
167 | - // If the default is expected to be a boolean, we need to cast the value |
|
168 | - if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) { |
|
169 | - return $value === '1'; |
|
170 | - } |
|
171 | - return $value; |
|
172 | - }, $this->getAllowedConfigKeys()); |
|
164 | + $userId = $this->user->getUID(); |
|
165 | + $userConfigs = array_map(function (string $key) use ($userId) { |
|
166 | + $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key)); |
|
167 | + // If the default is expected to be a boolean, we need to cast the value |
|
168 | + if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) { |
|
169 | + return $value === '1'; |
|
170 | + } |
|
171 | + return $value; |
|
172 | + }, $this->getAllowedConfigKeys()); |
|
173 | 173 | |
174 | - return array_combine($this->getAllowedConfigKeys(), $userConfigs); |
|
175 | - } |
|
174 | + return array_combine($this->getAllowedConfigKeys(), $userConfigs); |
|
175 | + } |
|
176 | 176 | } |