-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSettingsService.php
More file actions
430 lines (374 loc) · 11.8 KB
/
SettingsService.php
File metadata and controls
430 lines (374 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* This file is part of the Unsplash App
* and licensed under the AGPL.
*/
namespace OCA\Unsplash\Services;
use OCA\Unsplash\ProviderHandler\CachedProvider;
use OCA\Unsplash\ProviderHandler\Provider;
use OCA\Unsplash\ProviderHandler\ProviderDefinitions;
use OCP\Files\IAppData;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
/**
* Class SettingsService
*
* @package OCA\Unsplash\Services
*/
class SettingsService
{
const STYLE_LOGIN = 'unsplash/style/login';
const STYLE_LOGIN_HIGH_VISIBILITY = 'unsplash/style/login/highvisibility';
const STYLE_DASHBORAD = 'unsplash/style/dashborad';
const USER_STYLE_DASHBORAD = 'unsplash/style/dashborad';
const PROVIDER_SELECTED = 'unsplash/provider/selected';
const PROVIDER_DEFAULT = 'Unsplash';
const STYLE_TINT_ALLOWED = 'unsplash/style/tint';
const STYLE_STRENGHT_COLOR = 'unsplash/style/strength/color';
const STYLE_STRENGHT_BLUR = 'unsplash/style/strength/blur';
const STYLE_TINT_ALLOWED_DEFAULT = 0; //equals 30%
const STYLE_STRENGHT_COLOR_DEFAULT = 30; //equals 30%
const STYLE_STRENGHT_BLUR_DEFAULT = 0;
const STYLE_LOGIN_HIGH_VISIBILITY_DEFAULT = 0;
/**
* @var IConfig
*/
protected $config;
/**
* @var string
*/
protected $userId;
/**
* @var string
*/
protected $appName;
/**
* @var ProviderDefinitions
*/
protected $providerDefinitions;
/**
* @var \OC_Defaults
*/
private $defaults;
/**
* FaviconService constructor.
*
* @param string|null $userId
* @param $appName
* @param IConfig $config
* @param IAppData $config
* @param IClientService $clientService
* @param Defaults $defaults
* @param LoggerInterface $logger
*/
public function __construct($userId, $appName, IConfig $config, IAppData $appData, IClientService $clientService, \OC_Defaults $defaults, LoggerInterface $logger)
{
$this->config = $config;
$this->userId = $userId;
if ($this->config->getSystemValue('maintenance', false)) {
$this->userId = null;
}
$this->appName = $appName;
$this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData, $clientService);
$this->defaults = $defaults;
}
/**
* If the dashboard should be styled for this user
*
* @return bool
*/
public function getUserStyleDashboardEnabled(): bool
{
// for magic value see: https://github.com/nextcloud/server/blame/master/apps/theming/lib/Service/BackgroundService.php
$themingSettingsKey = "background";
if ($this->getNextcloudVersion() > 26) {
$themingSettingsKey = "background_image";
}
$themingAppDashboard = $this->config->getUserValue($this->userId, "theming", $themingSettingsKey, 'default');
// dont add custom css when custom image was selected
if ($themingAppDashboard == 'default') {
return $this->getServerStyleDashboardEnabled();
}
return false;
}
/**
* Todo: refactor this function to a "has dash" function that also checks wether the dashboard is actually enabled.
* and then dont show the entries.
* @return int
*/
public function getNextcloudVersion(): int
{
$version = $this->config->getSystemValue('version', '0.0.0');
$parts = explode('.', $version, 2);
return intval($parts[0]);
}
/**
* If the page dashboard be styled by default
*
* @return bool
*/
public function getServerStyleDashboardEnabled(): bool
{
return $this->config->getAppValue($this->appName, self::STYLE_DASHBORAD, 0) == 1;
}
/**
* Set if the dashboard should be styled by default
*
* @param int $styleDashboard
*/
public function setServerStyleDashboardEnabled(int $styleDashboard = 1)
{
$this->config->setAppValue($this->appName, self::STYLE_DASHBORAD, $styleDashboard);
}
/**
* If the login page should be styled by default
*
* @return bool
*/
public function getServerStyleLoginEnabled(): bool
{
return $this->config->getAppValue($this->appName, self::STYLE_LOGIN, 1) == 1;
}
/**
* Set if the login page should be styled by default
*
* @param int $styleLogin
*/
public function setServerStyleLoginEnabled(int $styleLogin = 1)
{
$this->config->setAppValue($this->appName, self::STYLE_LOGIN, $styleLogin);
}
/**
* Set the selected imageprovider, but does not use the provided string.
*
* @param string $providername
*/
public function setImageProviderSanitized(string $providername): void
{
$allProvider = $this->getAllImageProvider();
foreach ($allProvider as &$value) {
if($value == $providername) {
$this->config->setAppValue($this->appName, self::PROVIDER_SELECTED, $value);
}
}
}
/**
* Get the selected imageprovider
*
* @return string name of the provider
* @return string current provider
*/
public function getImageProvider($name): Provider
{
return $this->providerDefinitions->getProviderByName($name);
}
/**
* Get the selected imageprovider
*
* @return Provider current provider
*/
public function getSelectedImageProvider(): Provider
{
$name = $this->getImageProviderName();
return $this->providerDefinitions->getProviderByName($name);
}
/**
* Get the selected imageprovider's name
*
* @return string current provider name
*/
public function getImageProviderName(): string
{
return $this->config->getAppValue($this->appName, self::PROVIDER_SELECTED, self::PROVIDER_DEFAULT);
}
/**
* Get the selected imageprovider customization
*
* @return string current provider customization
*/
public function getImageProviderCustomization()
{
$providername = $this->getImageProviderName();
$provider = $this->providerDefinitions->getProviderByName($providername);
return $provider->getCustomSearchterms();
}
/**
* Set the selected imageprovider customization
*
* @param string $customization
*/
public function setImageProviderCustomization($customization)
{
$providername = $this->getImageProviderName();
$provider = $this->providerDefinitions->getProviderByName($providername);
$provider->setCustomSearchTerms($customization);
$this->fetchIfRequired();
}
/**
* Get all defined imageprovider
* @return array[String]
*/
public function getAllImageProvider(): array
{
return $this->providerDefinitions->getAllProviderNames();
}
/**
* Get all defined imageprovider that allow customization
*/
public function getAllCustomizableImageProvider()
{
$all = [];
foreach ($this->providerDefinitions->getAllProviderNames() as $value) {
$provider = $this->providerDefinitions->getProviderByName($value);
if ($provider->isCustomizable()) {
$all[] = $value;
}
}
return $this->providerDefinitions->getAllProviderNames();
}
/**
* Returns the URL to the custom Unsplash-path
*
* @return String
*/
public function headerbackgroundLink($size)
{
$providerName = $this->config->getAppValue($this->appName, self::PROVIDER_SELECTED, self::PROVIDER_DEFAULT);
$provider = $this->providerDefinitions->getProviderByName($providerName);
if ($provider->isCached()) {
return $provider->getCachedImageURL();
}
return $provider->getRandomImageUrl($size);
}
/**
* Get all URLs for whitelisting
*/
public function getWhitelistingUrlsForSelectedProvider()
{
$providerName = $this->config->getAppValue($this->appName, self::PROVIDER_SELECTED, self::PROVIDER_DEFAULT);
$provider = $this->providerDefinitions->getProviderByName($providerName);
return $provider->getWhitelistResourceUrls();
}
/**
* nextcloud theming main color
*
* @param String $styleUrl
*/
public function getInstanceColor()
{
return $this->config->getAppValue('theming', 'color', $this->defaults->getColorPrimary());
}
/**
* If the login page should be styled by default
*
* @return bool
*/
public function isTintEnabled(): bool
{
return $this->config->getAppValue($this->appName, self::STYLE_TINT_ALLOWED, self::STYLE_TINT_ALLOWED_DEFAULT);
}
public function setTint(int $tinting): void
{
$this->config->setAppValue($this->appName, self::STYLE_TINT_ALLOWED, $tinting);
}
/**
* color strength
*
* @param String $styleUrl
*/
public function getColorStrength()
{
return $this->config->getAppValue($this->appName, self::STYLE_STRENGHT_COLOR, self::STYLE_STRENGHT_COLOR_DEFAULT);
}
/**
* set color strength
*
* @param String $styleUrl
*/
public function setColorStrength(int $strength)
{
if ($strength > 100) {
$strength = 100;
}
if ($strength < 0) {
$strength = 0;
}
$this->config->setAppValue($this->appName, self::STYLE_STRENGHT_COLOR, $strength);
}
/**
* blur strength
*
* @param String $styleUrl
*/
public function getBlurStrength()
{
return $this->config->getAppValue($this->appName, self::STYLE_STRENGHT_BLUR, self::STYLE_STRENGHT_BLUR_DEFAULT);
}
/**
* set blur strength
*
* @param String $styleUrl
*/
public function setBlurStrength(int $strength)
{
if ($strength > 25) {
$strength = 25;
}
if ($strength < 0) {
$strength = 0;
}
$this->config->setAppValue($this->appName, self::STYLE_STRENGHT_BLUR, $strength);
}
/**
* If the login page should be styled as High Visibility for Legal reasons
*
* @return bool
*/
public function isHighVisibilityLogin(): bool
{
return $this->config->getAppValue($this->appName, self::STYLE_LOGIN_HIGH_VISIBILITY, self::STYLE_LOGIN_HIGH_VISIBILITY_DEFAULT);
}
public function setHighVisibilityLogin(int $highVisibility): void
{
$this->config->setAppValue($this->appName, self::STYLE_LOGIN_HIGH_VISIBILITY, $highVisibility);
}
/**
* Store the authentication token for the current provider
* @param string $token
*/
public function setCurrentProviderToken(string $token)
{
$provider = $this->getImageProviderName();
$this->config->setAppValue($this->appName, 'splash/provider/' . $provider . '/token', $token);
$this->fetchIfRequired();
}
private function fetchIfRequired()
{
$provider = $this->getImageProviderName();
$providerToFetch = $this->providerDefinitions->getProviderByName($provider);
if ($providerToFetch->isCached()) {
$providerToFetch->fetchCached();
}
}
/**
* Get new image for current provider
*/
public function updateCachedBackground()
{
$provider = $this->getImageProviderName();
$providerToFetch = $this->providerDefinitions->getProviderByName($provider);
if ($providerToFetch->isCached()) {
$providerToFetch->deleteCached();
$providerToFetch->fetchCached();
}
}
/**
* Wrapper to check if current provider is cached
*/
public function isCached(): bool
{
$provider = $this->getImageProviderName();
return $this->providerDefinitions->getProviderByName($provider)->isCached();
}
}