UserPreferences.st
branchjv
changeset 22109 850e85f23ff2
parent 21285 7770135c2b54
child 22135 9b225469dca3
--- a/UserPreferences.st	Fri Feb 17 10:25:31 2017 +0100
+++ b/UserPreferences.st	Wed Jun 07 22:02:36 2017 +0100
@@ -129,28 +129,40 @@
     "Created: / 06-06-2016 / 10:42:14 / cg"
 ! !
 
+!UserPreferences class methodsFor:'instance creation'!
+
+new
+    "return an initialized instance"
+
+    ^ super new initialize.
+
+    "Modified: / 07-06-2017 / 11:34:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !UserPreferences class methodsFor:'accessing'!
 
 current
     CurrentPreferences isNil ifTrue:[
         CurrentPreferences := self new.
-        self initializeDefaultsIn:CurrentPreferences.
         CurrentPreferences flyByHelpSettingChanged.
     ].
-    ^ CurrentPreferences.
+    "/ The #value message allows CurrentPrefences to be a block (or anything
+    "/ that responds to #value) that returns different preferences in different
+    "/ contexts. For example, each screen might have different user with 
+    "/ different settings.
+    ^ CurrentPreferences value.
 
     "
      CurrentPreferences := nil
     "
 
-    "Modified: / 05-02-2015 / 07:08:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 06-06-2016 / 10:42:59 / cg"
+    "Modified: / 07-06-2017 / 12:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 default
     DefaultPreferences isNil ifTrue:[
         DefaultPreferences := self new.
-        self initializeDefaultsIn:DefaultPreferences
     ].
     ^ DefaultPreferences.
 
@@ -159,6 +171,7 @@
     "
 
     "Modified: / 06-06-2016 / 10:41:45 / cg"
+    "Modified: / 07-06-2017 / 11:33:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 reset
@@ -474,9 +487,37 @@
 !UserPreferences class methodsFor:'accessing - defaults'!
 
 defaultUserSettingsFile
-    ^ (Filename usersPrivateSmalltalkDirectory) / 'settings.stx'
+    "Return default user settings file."
+
+    ^self defaultUserSettingsFileLocations first.       
+    "
+    UserPreferences defaultUserSettingsFile
+    "
 
     "Created: / 06-10-2008 / 08:27:15 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 07-06-2017 / 11:29:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 07-06-2017 / 21:47:46 / jv"
+!
+
+defaultUserSettingsFileLocations
+    "Return list of files which are probed when looking for
+     saved user settings.
+
+     The location changed over the time. To be backward 
+     compatible, return all of them. 
+    "
+    ^ {
+        (Filename usersPrivateSmalltalkDirectory / 'settings.stx') ."/ per-user settings file (new default?)
+        (Filename usersPrivateSmalltalkDirectory / 'settings.rc') . "/ for backward compatibility with jv-branch
+        (Smalltalk getSystemFileName: 'settings.stx') .             "/ an old stx default
+        (Smalltalk getSystemFileName: 'settings.rc') .              "/ for backward compatibility with jv-branch
+      }
+
+    "
+    UserPreferences defaultUserSettingsFileLocations
+    "
+
+    "Created: / 07-06-2017 / 21:46:43 / jv"
 !
 
 defaultWorkspaceDirectory
@@ -517,7 +558,92 @@
     "
 ! !
 
-!UserPreferences class methodsFor:'saving'!
+!UserPreferences class methodsFor:'load / save'!
+
+loadSettings
+    "Load and return default user settings. Probe all files specified specified
+     in #defaultUserSettingsFileLocations. First found is used, the rest is ignored.
+    "
+
+    self defaultUserSettingsFileLocations do:[:file |
+        file exists ifTrue:[ 
+            file isDirectory ifTrue:[ 
+                Logger warning: 'user settings file is actually a directory: %1' with: file pathName.
+            ] ifFalse:[
+                file isReadable ifFalse:[ 
+                    Logger warning: 'user settings file is not readable, skipping: %1' with: file pathName.
+                ] ifTrue:[ 
+                    ^ self loadSettingsFrom: file
+                ].
+            ]
+        ]
+    ].
+    ^ self new.
+
+    "
+    UserPreferences loadSettings.
+    "
+
+    "Created: / 07-06-2017 / 11:50:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 07-06-2017 / 21:50:56 / jv"
+!
+
+loadSettingsFrom: aStringOrFilename
+    "Load previously stored settings from given file. Returns loaded settings."
+
+    | prefsFile prefs currentProcess currentPrefs |
+
+    prefsFile := aStringOrFilename.
+    (prefsFile isReadable not or:[prefsFile isRegularFile not]) ifTrue:[ 
+        self error: 'File not readable or not a regular file'
+    ].
+    "/ Currently format of user preferences is actually an executable
+    "/ smalltalk code that is evaluated. That code fills in values in
+    "/ `UserPreferences current`. This way, it's not possible to load
+    "/ previously saved preferences to some other objects.
+    "/ 
+    "/ So, in order to load preferences we need to temporarily
+    "/ swap `UserPreferences current` to pristine instance, load
+    "/ preferences and then restore old preferences.
+    "/ 
+    "/ Things are more complicated since someone may access preferences
+    "/ while loading. Therefore, answer old preferences except for
+    "/ current process which is loading new ones.
+    "/ 
+    "/ What a hack!!
+    currentPrefs := CurrentPreferences.
+    currentProcess := Processor activeProcess.
+    [
+        prefs := self new.
+        CurrentPreferences := [ 
+            Processor activeProcess == currentProcess 
+                ifTrue:[ prefs ]
+                ifFalse:[ currentPrefs value ]
+        ].
+        prefsFile fileIn. 
+        prefs at:#settingsFilename put: prefsFile pathName.               
+    ] ensure:[ 
+        CurrentPreferences := currentPrefs.
+    ].
+    ^ prefs
+
+    "
+    UserPreferences loadSettingsFrom: self defaultUserSettingsFile. 
+
+    | file |
+
+    file := Filename newTemporary.
+    [
+        file writingFileDo:[:s| s nextPutLine: 'UserPreferences current at: #xxx put: #yyy' ].
+        UserPreferences loadSettingsFrom: file
+    ] ensure:[ 
+        file remove.
+    ].
+    "
+
+    "Created: / 07-06-2017 / 11:46:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 07-06-2017 / 13:25:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
 
 saveSettings:userPrefs in:fileNameOrString
     "save settings to a settings-file."
@@ -874,6 +1000,7 @@
 
 
 
+
 !UserPreferences methodsFor:'accessing-locale'!
 
 dateInputFormat
@@ -5670,6 +5797,8 @@
 ! !
 
 
+
+
 !UserPreferences methodsFor:'default settings-syntax colors'!
 
 listOfPredefinedSyntaxColoringSchemes
@@ -5899,6 +6028,16 @@
     ^ self defaultValue
 ! !
 
+!UserPreferences methodsFor:'initialization'!
+
+initialize
+    super initialize.
+    self class initializeDefaultsIn: self.
+    self beUnmodified.
+
+    "Modified: / 07-06-2017 / 11:32:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !UserPreferences methodsFor:'misc'!
 
 doesNotUnderstand:aMessage
@@ -5986,6 +6125,11 @@
     ^ '$Header$'
 !
 
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+!
+
 version_SVN
     ^ '$ Id: UserPreferences.st 10648 2011-06-23 15:55:10Z vranyj1  $'
 ! !