33namespace OCA \Notes \Service ;
44
55use OCP \IConfig ;
6+ use OCP \IL10N ;
7+ use OCP \Files \IRootFolder ;
68
79class SettingsService {
810
911 private $ config ;
12+ private $ l10n ;
1013 private $ root ;
1114
1215 /* Default values */
13- private $ defaults = [
14- 'notesPath ' => 'Notes ' ,
15- 'fileSuffix ' => '.txt ' ,
16- ];
16+ private $ defaults ;
1717
1818 public function __construct (
19- IConfig $ config
19+ IConfig $ config ,
20+ IL10N $ l10n ,
21+ IRootFolder $ root
2022 ) {
2123 $ this ->config = $ config ;
24+ $ this ->l10n = $ l10n ;
25+ $ this ->root = $ root ;
26+ $ this ->defaults = [
27+ 'fileSuffix ' => '.txt ' ,
28+ 'notesPath ' => function (string $ uid ) {
29+ return $ this ->getDefaultNotesPath ($ uid );
30+ },
31+ ];
32+ }
33+
34+ private function getDefaultNotesPath (string $ uid ) : string {
35+ $ defaultFolder = 'Notes ' ;
36+ $ defaultExists = $ this ->root ->getUserFolder ($ uid )->nodeExists ($ defaultFolder );
37+ if ($ defaultExists ) {
38+ return $ defaultFolder ;
39+ } else {
40+ return $ this ->l10n ->t ($ defaultFolder );
41+ }
2242 }
2343
2444 /**
@@ -39,15 +59,23 @@ public function set(string $uid, array $settings) : void {
3959
4060 public function getAll (string $ uid ) : \stdClass {
4161 $ settings = json_decode ($ this ->config ->getUserValue ($ uid , 'notes ' , 'settings ' ));
42- if (is_object ($ settings )) {
43- // use default for empty settings
44- foreach ($ this ->defaults as $ name => $ defaultValue ) {
45- if (!property_exists ($ settings , $ name ) || empty ($ settings ->{$ name })) {
62+ if (!is_object ($ settings )) {
63+ $ settings = new \stdClass ();
64+ }
65+ // use default for empty settings
66+ $ toBeSaved = false ;
67+ foreach ($ this ->defaults as $ name => $ defaultValue ) {
68+ if (!property_exists ($ settings , $ name ) || empty ($ settings ->{$ name })) {
69+ if (is_callable ($ defaultValue )) {
70+ $ settings ->{$ name } = $ defaultValue ($ uid );
71+ $ toBeSaved = true ;
72+ } else {
4673 $ settings ->{$ name } = $ defaultValue ;
4774 }
4875 }
49- } else {
50- $ settings = (object )$ this ->defaults ;
76+ }
77+ if ($ toBeSaved ) {
78+ $ this ->set ($ uid , (array ) $ settings );
5179 }
5280 return $ settings ;
5381 }
0 commit comments