|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2022 John Molakvoæ <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author John Molakvoæ <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OCA\Theming\Service; |
|
24
|
|
|
|
|
25
|
|
|
use OCA\Theming\AppInfo\Application; |
|
26
|
|
|
use OCA\Theming\ITheme; |
|
27
|
|
|
use OCA\Theming\Themes\DarkHighContrastTheme; |
|
28
|
|
|
use OCA\Theming\Themes\DarkTheme; |
|
29
|
|
|
use OCA\Theming\Themes\DefaultTheme; |
|
30
|
|
|
use OCA\Theming\Themes\DyslexiaFont; |
|
31
|
|
|
use OCA\Theming\Themes\HighContrastTheme; |
|
32
|
|
|
use OCP\IConfig; |
|
33
|
|
|
use OCP\IUser; |
|
34
|
|
|
use OCP\IUserSession; |
|
35
|
|
|
|
|
36
|
|
|
class ThemesService { |
|
37
|
|
|
private IUserSession $userSession; |
|
38
|
|
|
private IConfig $config; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ITheme[] */ |
|
41
|
|
|
private array $themesProviders; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(IUserSession $userSession, |
|
44
|
|
|
IConfig $config, |
|
45
|
|
|
DefaultTheme $defaultTheme, |
|
46
|
|
|
DarkTheme $darkTheme, |
|
47
|
|
|
HighContrastTheme $highContrastTheme, |
|
48
|
|
|
DarkHighContrastTheme $darkHighContrastTheme, |
|
49
|
|
|
DyslexiaFont $dyslexiaFont) { |
|
50
|
|
|
$this->userSession = $userSession; |
|
51
|
|
|
$this->config = $config; |
|
52
|
|
|
|
|
53
|
|
|
// Register themes |
|
54
|
|
|
$this->themesProviders = [ |
|
55
|
|
|
$defaultTheme->getId() => $defaultTheme, |
|
56
|
|
|
$darkTheme->getId() => $darkTheme, |
|
57
|
|
|
$highContrastTheme->getId() => $highContrastTheme, |
|
58
|
|
|
$darkHighContrastTheme->getId() => $darkHighContrastTheme, |
|
59
|
|
|
$dyslexiaFont->getId() => $dyslexiaFont, |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the list of all registered themes |
|
65
|
|
|
* |
|
66
|
|
|
* @return ITheme[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getThemes(): array { |
|
69
|
|
|
return $this->themesProviders; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Enable a theme for the logged-in user |
|
74
|
|
|
* |
|
75
|
|
|
* @param ITheme $theme the theme to enable |
|
76
|
|
|
* @return string[] the enabled themes |
|
77
|
|
|
*/ |
|
78
|
|
|
public function enableTheme(ITheme $theme): array { |
|
79
|
|
|
$themesIds = $this->getEnabledThemes(); |
|
80
|
|
|
|
|
81
|
|
|
// If already enabled, ignore |
|
82
|
|
|
if (in_array($theme->getId(), $themesIds)) { |
|
83
|
|
|
return $themesIds; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @var ITheme[] */ |
|
87
|
|
|
$themes = array_map(function($themeId) { |
|
88
|
|
|
return $this->getThemes()[$themeId]; |
|
89
|
|
|
}, $themesIds); |
|
90
|
|
|
|
|
91
|
|
|
// Filtering all themes with the same type |
|
92
|
|
|
$filteredThemes = array_filter($themes, function(ITheme $t) use ($theme) { |
|
93
|
|
|
return $theme->getType() === $t->getType(); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
// Retrieve IDs only |
|
97
|
|
|
/** @var string[] */ |
|
98
|
|
|
$filteredThemesIds = array_map(function(ITheme $t) { |
|
99
|
|
|
return $t->getId(); |
|
100
|
|
|
}, array_values($filteredThemes)); |
|
101
|
|
|
|
|
102
|
|
|
$enabledThemes = array_merge(array_diff($themesIds, $filteredThemesIds), [$theme->getId()]); |
|
103
|
|
|
$this->setEnabledThemes($enabledThemes); |
|
104
|
|
|
|
|
105
|
|
|
return $enabledThemes; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Disable a theme for the logged-in user |
|
110
|
|
|
* |
|
111
|
|
|
* @param ITheme $theme the theme to disable |
|
112
|
|
|
* @return string[] the enabled themes |
|
113
|
|
|
*/ |
|
114
|
|
|
public function disableTheme(ITheme $theme): array { |
|
115
|
|
|
$themesIds = $this->getEnabledThemes(); |
|
116
|
|
|
|
|
117
|
|
|
// If enabled, removing it |
|
118
|
|
|
if (in_array($theme->getId(), $themesIds)) { |
|
119
|
|
|
$enabledThemes = array_diff($themesIds, [$theme->getId()]); |
|
120
|
|
|
$this->setEnabledThemes($enabledThemes); |
|
121
|
|
|
return $enabledThemes; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $themesIds; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Check whether a theme is enabled or not |
|
129
|
|
|
* for the logged-in user |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
public function isEnabled(ITheme $theme): bool { |
|
134
|
|
|
$user = $this->userSession->getUser(); |
|
135
|
|
|
if ($user instanceof IUser) { |
|
136
|
|
|
// Using keys as it's faster |
|
137
|
|
|
$themes = $this->getEnabledThemes(); |
|
138
|
|
|
return in_array($theme->getId(), $themes); |
|
139
|
|
|
} |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the list of all enabled themes IDs |
|
145
|
|
|
* for the logged-in user |
|
146
|
|
|
* |
|
147
|
|
|
* @return string[] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getEnabledThemes(): array { |
|
150
|
|
|
$user = $this->userSession->getUser(); |
|
151
|
|
|
if ($user === null) { |
|
152
|
|
|
return []; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
|
|
return json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]')); |
|
157
|
|
|
} catch (\Exception $e) { |
|
158
|
|
|
return []; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set the list of enabled themes |
|
164
|
|
|
* for the logged-in user |
|
165
|
|
|
* |
|
166
|
|
|
* @param string[] $themes the list of enabled themes IDs |
|
167
|
|
|
*/ |
|
168
|
|
|
private function setEnabledThemes(array $themes): void { |
|
169
|
|
|
$user = $this->userSession->getUser(); |
|
170
|
|
|
$this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique(array_values($themes)))); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|