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