Passed
Push — settings-service ( 7097d2...6957f0 )
by Matias
12:25
created

SettingsService::getShowNotGrouped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2020 Matias De lellis <[email protected]>
5
 *
6
 * @author Matias De lellis <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\FaceRecognition\Service;
26
27
use OCA\FaceRecognition\AppInfo\Application;
28
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
29
30
use OCP\IConfig;
31
32
class SettingsService {
33
34
	/*
35
	 * Settings keys and default values.
36
	 */
37
38
	/** Current Model used to analyze */
39
	const CURRENT_MODEL_KEY = 'model';
40
	/* Default values is taked from AddDefaultFaceModel */
41
42
	/** Sensitivity used to clustering */
43
	const SENSITIVITY_KEY = 'sensitivity';
44
	const MINIMUM_SENSITIVITY = '0.4';
45
	const DEFAULT_SENSITIVITY = '0.5';
46
	const MAXIMUM_SENSITIVITY = '0.6';
47
48
	/** Minimum confidence used to try to clustring faces */
49
	const MINIMUM_CONFIDENCE_KEY = 'min-confidence';
50
	const MINIMUM_MINIMUM_CONFIDENCE = '0.0';
51
	const DEFAULT_MINIMUM_CONFIDENCE = '0.9';
52
	const MAXIMUM_MINIMUM_CONFIDENCE = '1.0';
53
54
	/** Memory limit suggested for analysis */
55
	const MEMORY_LIMITS_KEY = "memory-limits";
56
	const MINIMUM_MEMORY_LIMITS = 1 * 1024 * 1024 * 1024;
57
	const DEFAULT_MEMORY_LIMITS = '-1'; // It is dynamically configured according to hardware
58
	const MAXIMUM_MEMORY_LIMITS = 4 * 1024 * 1024 * 1024;
59
60
	/** Show single persons on clustes view */
61
	const SHOW_NOT_GROUPED_KEY = 'show-not-grouped';
62
	const DEFAULT_SHOW_NOT_GROUPED = 'false';
63
64
	/** User setting what indicates if has the analysis enabled */
65
	const USER_ENABLED_KEY = 'enabled';
66
	const DEFAULT_USER_ENABLED = 'false';
67
68
	/** User setting that remember last images checked */
69
	const STALE_IMAGES_LAST_CHECKED_KEY = 'stale_images_last_checked';
70
	const DEFAULT_STALE_IMAGES_LAST_CHECKED = '0';
71
72
	/** Define if for some reason need remove old images */
73
	const STALE_IMAGES_REMOVAL_NEEDED_KEY = 'stale_images_removal_needed';
74
	const DEFAULT_STALE_IMAGES_REMOVAL_NEEDED = 'false';
75
76
	/** User setting that indicate when scan finished */
77
	const FULL_IMAGE_SCAN_DONE_KEY = 'full_image_scan_done';
78
	const DEFAULT_FULL_IMAGE_SCAN_DONE = 'false';
79
80
	/** User setting that indicate that need to recreate clusters */
81
	const USER_RECREATE_CLUSTERS_KEY = 'recreate-clusters';
82
	const DEFAULT_USER_RECREATE_CLUSTERS = 'false';
83
84
	/** Hidden setting that allows to analyze shared files */
85
	const HANDLE_SHARED_FILES_KEY = 'handle-shared-files';
86
	const DEFAULT_HANDLE_SHARED_FILES = 'false';
87
88
89
	/**
90
	 * SettingsService
91
	 */
92
93
	/** @var IConfig Config */
94
	private $config;
95
96
	/**  @var string|null */
97
	private $userId;
98
99
	/**
100
	 * @param IConfig $config
101
	 * @param string $userId
102
	 */
103 1
	public function __construct(IConfig $config,
104
	                            $userId)
105
	{
106 1
		$this->config = $config;
107 1
		$this->userId = $userId;
108 1
	}
109
110
	/*
111
	 * User settings.
112
	 */
113
	public function getUserEnabled ($userId = null): bool {
114
		$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED);
115
		return ($enabled === 'true');
116
	}
117
118
	public function setUserEnabled (bool $enabled, $userId = null) {
119
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false");
120
	}
121
122
	public function getUserFullScanDone ($userId = null): bool {
123
		$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE);
124
		return ($fullScanDone === 'true');
125
	}
126
127
	public function setUserFullScanDone (bool $fullScanDone, $userId = null) {
128
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false");
129
	}
130
131
	public function getNeedRemoveStaleImages ($userId = null): bool {
132
		$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED);
133
		return ($needRemoval === 'true');
134
	}
135
136
	public function setNeedRemoveStaleImages (bool $needRemoval, $userId = null) {
137
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false");
138
	}
139
140
	public function getNeedRecreateClusters ($userId = null): bool {
141
		$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS);
142
		return ($needRecreate === 'true');
143
	}
144
145
	public function setNeedRecreateClusters (bool $needRecreate, $userId = null) {
146
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false");
147
	}
148
149
	/*
150
	 * Admin and process settings.
151
	 */
152
	public function getCurrentFaceModel(): int {
153
		return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
154
	}
155
156
	public function setCurrentFaceModel($model) {
157
		$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, $model);
158
	}
159
160
	public function getSensitivity(): float {
161
		return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY));
162
	}
163
164
	public function setSensitivity($sensitivity) {
165
		$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity);
166
	}
167
168
	public function getMinimumConfidence(): float {
169
		return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE));
170
	}
171
172
	public function setMinimumConfidence($confidence) {
173
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence);
174
	}
175
176
	public function getMemoryLimits(): int {
177
		return intval($this->config->getAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, self::DEFAULT_MEMORY_LIMITS));
178
	}
179
180
	public function setMemoryLimits(int $memoryLimits) {
181
		$this->config->setAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, strval($memoryLimits));
182
	}
183
184
	public function getShowNotGrouped(): bool {
185
		$show = $this->config->getAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, self::DEFAULT_SHOW_NOT_GROUPED);
186
		return ($show === 'true');
187
	}
188
189
	public function setShowNotGrouped(bool $show) {
190
		$this->config->setAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, $show ? "true" : "false");
191
	}
192
193
	/**
194
	 * The next settings are advanced preferences that are not available in gui.
195
	 * See: https://github.com/matiasdelellis/facerecognition/wiki/Settings#hidden-settings
196
	 */
197
	public function getHandleSharedFiles() {
198
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_SHARED_FILES_KEY, self::DEFAULT_HANDLE_SHARED_FILES);
199
		return ($handle === 'true');
200
	}
201
202
}
203