1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2020 Julius Härtl <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author Jan C. Borchardt <[email protected]> |
9
|
|
|
* @author Julius Härtl <[email protected]> |
10
|
|
|
* @author Christopher Ng <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @license GNU AGPL version 3 or any later version |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
17
|
|
|
* License, or (at your option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU Affero General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace OCA\Theming\Service; |
29
|
|
|
|
30
|
|
|
use InvalidArgumentException; |
31
|
|
|
use OC\User\NoUserException; |
32
|
|
|
use OCA\Theming\AppInfo\Application; |
33
|
|
|
use OCA\Theming\ThemingDefaults; |
34
|
|
|
use OCP\Files\File; |
35
|
|
|
use OCP\Files\IAppData; |
36
|
|
|
use OCP\Files\IRootFolder; |
37
|
|
|
use OCP\Files\NotFoundException; |
38
|
|
|
use OCP\Files\NotPermittedException; |
39
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
40
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
41
|
|
|
use OCP\IConfig; |
42
|
|
|
use OCP\Lock\LockedException; |
43
|
|
|
use OCP\PreConditionNotMetException; |
44
|
|
|
|
45
|
|
|
class BackgroundService { |
46
|
|
|
// true when the background is bright and need dark icons |
47
|
|
|
public const THEMING_MODE_DARK = 'dark'; |
48
|
|
|
public const DEFAULT_COLOR = '#0082c9'; |
49
|
|
|
public const DEFAULT_ACCESSIBLE_COLOR = '#006aa3'; |
50
|
|
|
|
51
|
|
|
public const BACKGROUND_SHIPPED = 'shipped'; |
52
|
|
|
public const BACKGROUND_CUSTOM = 'custom'; |
53
|
|
|
public const BACKGROUND_DEFAULT = 'default'; |
54
|
|
|
public const BACKGROUND_DISABLED = 'disabled'; |
55
|
|
|
|
56
|
|
|
public const DEFAULT_BACKGROUND_IMAGE = 'kamil-porembinski-clouds.jpg'; |
57
|
|
|
public const SHIPPED_BACKGROUNDS = [ |
58
|
|
|
'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [ |
59
|
|
|
'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)', |
60
|
|
|
'attribution_url' => 'https://commons.wikimedia.org/wiki/File:%D0%A7%D0%B5%D1%88%D1%83%D0%B9%D0%BA%D0%B8_%D0%BA%D1%80%D1%8B%D0%BB%D0%B0_%D0%B1%D0%B0%D0%B1%D0%BE%D1%87%D0%BA%D0%B8.jpg', |
61
|
|
|
'primary_color' => '#a53c17', |
62
|
|
|
], |
63
|
|
|
'bernie-cetonia-aurata-take-off-composition.jpg' => [ |
64
|
|
|
'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)', |
65
|
|
|
'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg', |
66
|
|
|
'theming' => self::THEMING_MODE_DARK, |
67
|
|
|
'primary_color' => '#56633d', |
68
|
|
|
], |
69
|
|
|
'dejan-krsmanovic-ribbed-red-metal.jpg' => [ |
70
|
|
|
'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)', |
71
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/', |
72
|
|
|
'primary_color' => '#9c4236', |
73
|
|
|
], |
74
|
|
|
'eduardo-neves-pedra-azul.jpg' => [ |
75
|
|
|
'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)', |
76
|
|
|
'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg', |
77
|
|
|
'primary_color' => '#4f6071', |
78
|
|
|
], |
79
|
|
|
'european-space-agency-barents-bloom.jpg' => [ |
80
|
|
|
'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)', |
81
|
|
|
'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom', |
82
|
|
|
'primary_color' => '#396475', |
83
|
|
|
], |
84
|
|
|
'hannes-fritz-flippity-floppity.jpg' => [ |
85
|
|
|
'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)', |
86
|
|
|
'attribution_url' => 'http://hannes.photos/flippity-floppity', |
87
|
|
|
'primary_color' => '#98415a', |
88
|
|
|
], |
89
|
|
|
'hannes-fritz-roulette.jpg' => [ |
90
|
|
|
'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)', |
91
|
|
|
'attribution_url' => 'http://hannes.photos/roulette', |
92
|
|
|
'primary_color' => '#845334', |
93
|
|
|
], |
94
|
|
|
'hannes-fritz-sea-spray.jpg' => [ |
95
|
|
|
'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)', |
96
|
|
|
'attribution_url' => 'http://hannes.photos/sea-spray', |
97
|
|
|
'primary_color' => '#4f6071', |
98
|
|
|
], |
99
|
|
|
'kamil-porembinski-clouds.jpg' => [ |
100
|
|
|
'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)', |
101
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/', |
102
|
|
|
'primary_color' => self::DEFAULT_COLOR, |
103
|
|
|
], |
104
|
|
|
'bernard-spragg-new-zealand-fern.jpg' => [ |
105
|
|
|
'attribution' => 'New zealand fern (Bernard Spragg, CC0)', |
106
|
|
|
'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg', |
107
|
|
|
'primary_color' => '#316b26', |
108
|
|
|
], |
109
|
|
|
'rawpixel-pink-tapioca-bubbles.jpg' => [ |
110
|
|
|
'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)', |
111
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/', |
112
|
|
|
'theming' => self::THEMING_MODE_DARK, |
113
|
|
|
'primary_color' => '#7b4e7e', |
114
|
|
|
], |
115
|
|
|
'nasa-waxing-crescent-moon.jpg' => [ |
116
|
|
|
'attribution' => 'Waxing crescent moon (NASA, Public Domain)', |
117
|
|
|
'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon', |
118
|
|
|
'primary_color' => '#005ac1', |
119
|
|
|
], |
120
|
|
|
'tommy-chau-already.jpg' => [ |
121
|
|
|
'attribution' => 'Cityscape (Tommy Chau, CC BY)', |
122
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368', |
123
|
|
|
'primary_color' => '#6a2af4', |
124
|
|
|
], |
125
|
|
|
'tommy-chau-lion-rock-hill.jpg' => [ |
126
|
|
|
'attribution' => 'Lion rock hill (Tommy Chau, CC BY)', |
127
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246', |
128
|
|
|
'theming' => self::THEMING_MODE_DARK, |
129
|
|
|
'primary_color' => '#7f4f70', |
130
|
|
|
], |
131
|
|
|
'lali-masriera-yellow-bricks.jpg' => [ |
132
|
|
|
'attribution' => 'Yellow bricks (Lali Masriera, CC BY)', |
133
|
|
|
'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447', |
134
|
|
|
'theming' => self::THEMING_MODE_DARK, |
135
|
|
|
'primary_color' => '#7f5700', |
136
|
|
|
], |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
private IRootFolder $rootFolder; |
140
|
|
|
private IAppData $appData; |
141
|
|
|
private IConfig $config; |
142
|
|
|
private string $userId; |
143
|
|
|
private ThemingDefaults $themingDefaults; |
144
|
|
|
|
145
|
|
|
public function __construct(IRootFolder $rootFolder, |
146
|
|
|
IAppData $appData, |
147
|
|
|
IConfig $config, |
148
|
|
|
?string $userId, |
149
|
|
|
ThemingDefaults $themingDefaults) { |
150
|
|
|
if ($userId === null) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->rootFolder = $rootFolder; |
155
|
|
|
$this->config = $config; |
156
|
|
|
$this->userId = $userId; |
157
|
|
|
$this->appData = $appData; |
158
|
|
|
$this->themingDefaults = $themingDefaults; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setDefaultBackground(): void { |
162
|
|
|
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_image'); |
163
|
|
|
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $this->themingDefaults->getDefaultColorPrimary()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $path |
168
|
|
|
* @throws NotFoundException |
169
|
|
|
* @throws NotPermittedException |
170
|
|
|
* @throws LockedException |
171
|
|
|
* @throws PreConditionNotMetException |
172
|
|
|
* @throws NoUserException |
173
|
|
|
*/ |
174
|
|
|
public function setFileBackground($path): void { |
175
|
|
|
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM); |
176
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
177
|
|
|
|
178
|
|
|
/** @var File $file */ |
179
|
|
|
$file = $userFolder->get($path); |
180
|
|
|
$image = new \OCP\Image(); |
181
|
|
|
|
182
|
|
|
if ($image->loadFromFileHandle($file->fopen('r')) === false) { |
|
|
|
|
183
|
|
|
throw new InvalidArgumentException('Invalid image file'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r')); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setShippedBackground($fileName): void { |
190
|
|
|
if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) { |
191
|
|
|
throw new InvalidArgumentException('The given file name is invalid'); |
192
|
|
|
} |
193
|
|
|
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', $fileName); |
194
|
|
|
$this->setColorBackground(self::SHIPPED_BACKGROUNDS[$fileName]['primary_color']); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setColorBackground(string $color): void { |
198
|
|
|
if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) { |
199
|
|
|
throw new InvalidArgumentException('The given color is invalid'); |
200
|
|
|
} |
201
|
|
|
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $color); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function deleteBackgroundImage(): void { |
205
|
|
|
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DISABLED); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getBackground(): ?ISimpleFile { |
209
|
|
|
$background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT); |
210
|
|
|
if ($background === self::BACKGROUND_CUSTOM) { |
211
|
|
|
try { |
212
|
|
|
return $this->getAppDataFolder()->getFile('background.jpg'); |
213
|
|
|
} catch (NotFoundException | NotPermittedException $e) { |
214
|
|
|
return null; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Storing the data in appdata/theming/users/USERID |
222
|
|
|
* |
223
|
|
|
* @return ISimpleFolder |
224
|
|
|
* @throws NotPermittedException |
225
|
|
|
*/ |
226
|
|
|
private function getAppDataFolder(): ISimpleFolder { |
227
|
|
|
try { |
228
|
|
|
$rootFolder = $this->appData->getFolder('users'); |
229
|
|
|
} catch (NotFoundException $e) { |
230
|
|
|
$rootFolder = $this->appData->newFolder('users'); |
231
|
|
|
} |
232
|
|
|
try { |
233
|
|
|
return $rootFolder->getFolder($this->userId); |
234
|
|
|
} catch (NotFoundException $e) { |
235
|
|
|
return $rootFolder->newFolder($this->userId); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|