Passed
Push — settings-service ( 7803f5...566350 )
by Matias
04:05
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
	/** User setting that indicate that is forced to create clusters */
85
	const FORCE_CREATE_CLUSTERS_KEY = 'force-create-clusters';
86
	const DEFAULT_FORCE_CREATE_CLUSTERS = 'false';
87
88
	/** Hidden setting that allows to analyze shared files */
89
	const HANDLE_SHARED_FILES_KEY = 'handle-shared-files';
90
	const DEFAULT_HANDLE_SHARED_FILES = 'false';
91
92
	/** Hidden setting that indicate minimum large of image to analyze */
93
	const MINIMUM_IMAGE_SIZE_KEY = 'min_image_size';
94
	const DEFAULT_MINIMUM_IMAGE_SIZE = '512';
95
96
	/** Hidden setting that indicate maximum area of image to analyze */
97
	const MAXIMUM_IMAGE_AREA_KEY = 'max_image_area';
98
	const DEFAULT_MAXIMUM_IMAGE_AREA = '0';
99
100
101
	/**
102
	 * SettingsService
103
	 */
104
105
	/** @var IConfig Config */
106
	private $config;
107
108
	/**  @var string|null */
109
	private $userId;
110
111
	/**
112
	 * @param IConfig $config
113
	 * @param string $userId
114
	 */
115
	public function __construct(IConfig $config,
116
	                            $userId)
117
	{
118
		$this->config = $config;
119
		$this->userId = $userId;
120
	}
121
122
	/*
123
	 * User settings.
124
	 */
125
	public function getUserEnabled ($userId = null): bool {
126
		$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED);
127
		return ($enabled === 'true');
128
	}
129
130
	public function setUserEnabled (bool $enabled, $userId = null) {
131
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false");
132
	}
133
134
	public function getUserFullScanDone ($userId = null): bool {
135
		$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE);
136
		return ($fullScanDone === 'true');
137
	}
138
139
	public function setUserFullScanDone (bool $fullScanDone, $userId = null) {
140
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false");
141
	}
142
143
	public function getNeedRemoveStaleImages ($userId = null): bool {
144
		$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED);
145
		return ($needRemoval === 'true');
146
	}
147
148
	public function setNeedRemoveStaleImages (bool $needRemoval, $userId = null) {
149
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false");
150
	}
151
152
	public function getNeedRecreateClusters ($userId = null): bool {
153
		$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS);
154
		return ($needRecreate === 'true');
155
	}
156
157
	public function setNeedRecreateClusters (bool $needRecreate, $userId = null) {
158
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false");
159
	}
160
161
	public function getForceCreateClusters ($userId = null): bool {
162
		$forceCreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, self::DEFAULT_FORCE_CREATE_CLUSTERS);
163
		return ($forceCreate === 'true');
164
	}
165
166
	public function setForceCreateClusters (bool $forceCreate, $userId = null) {
167
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, $forceCreate ? "true" : "false");
168
	}
169
170
	/*
171
	 * Admin and process settings.
172
	 */
173
	public function getCurrentFaceModel(): int {
174
		return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
175
	}
176
177
	public function setCurrentFaceModel($model) {
178
		$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, $model);
179
	}
180
181
	public function getSensitivity(): float {
182
		return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY));
183
	}
184
185
	public function setSensitivity($sensitivity) {
186
		$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity);
187
	}
188
189
	public function getMinimumConfidence(): float {
190
		return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE));
191
	}
192
193
	public function setMinimumConfidence($confidence) {
194
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence);
195
	}
196
197
	public function getMemoryLimits(): int {
198
		return intval($this->config->getAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, self::DEFAULT_MEMORY_LIMITS));
199
	}
200
201
	public function setMemoryLimits(int $memoryLimits) {
202
		$this->config->setAppValue(Application::APP_NAME, self::MEMORY_LIMITS_KEY, strval($memoryLimits));
203
	}
204
205
	public function getShowNotGrouped(): bool {
206
		$show = $this->config->getAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, self::DEFAULT_SHOW_NOT_GROUPED);
207
		return ($show === 'true');
208
	}
209
210
	public function setShowNotGrouped(bool $show) {
211
		$this->config->setAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, $show ? "true" : "false");
212
	}
213
214
	/**
215
	 * The next settings are advanced preferences that are not available in gui.
216
	 * See: https://github.com/matiasdelellis/facerecognition/wiki/Settings#hidden-settings
217
	 */
218
	public function getHandleSharedFiles(): bool {
219
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_SHARED_FILES_KEY, self::DEFAULT_HANDLE_SHARED_FILES);
220
		return ($handle === 'true');
221
	}
222
223
	public function getMinimumImageSize(): int {
224
		return intval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_IMAGE_SIZE_KEY, self::DEFAULT_MINIMUM_IMAGE_SIZE));
225
	}
226
227
	public function getMaximumImageArea(): int {
228
		return intval($this->config->getAppValue(Application::APP_NAME, self::MAXIMUM_IMAGE_AREA_KEY, self::DEFAULT_MAXIMUM_IMAGE_AREA));
229
	}
230
231
}
232