1+ /*
2+ * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
9+ * Software is furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+ * DEALINGS IN THE SOFTWARE.
21+ *
22+ */
23+
24+
25+ /*global define, $, localStorage, brackets, console */
26+
27+ /**
28+ * Generates the fully configured preferences systems used throughout Brackets. This is intended
29+ * to be essentially private implementation that can be overridden for tests.
30+ */
31+ define ( function ( require , exports , module ) {
32+ "use strict" ;
33+
34+ var PreferencesBase = require ( "./PreferencesBase" ) ,
35+ Async = require ( "utils/Async" ) ,
36+
37+ // The SETTINGS_FILENAME is used with a preceding "." within user projects
38+ SETTINGS_FILENAME = "brackets.json" ,
39+ STATE_FILENAME = "state.json" ,
40+
41+ // User-level preferences
42+ userPrefFile = brackets . app . getApplicationSupportDirectory ( ) + "/" + SETTINGS_FILENAME ;
43+
44+ /**
45+ * A deferred object which is used to indicate PreferenceManager readiness during the start-up.
46+ * @private
47+ * @type {$.Deferred }
48+ */
49+ var _prefManagerReadyDeferred = new $ . Deferred ( ) ;
50+
51+ /**
52+ * A boolean property indicating if the user scope configuration file is malformed.
53+ */
54+ var userScopeCorrupt = false ;
55+
56+ function isUserScopeCorrupt ( ) {
57+ return userScopeCorrupt ;
58+ }
59+
60+ /**
61+ * Promises to add scopes. Used at init time only.
62+ * @private
63+ * @type {Array.<$.Promise> }
64+ */
65+ var _addScopePromises = [ ] ;
66+
67+ var manager = new PreferencesBase . PreferencesSystem ( ) ;
68+ manager . pauseChangeEvents ( ) ;
69+
70+ // Create a Project scope
71+ var projectStorage = new PreferencesBase . FileStorage ( undefined , true ) ,
72+ projectScope = new PreferencesBase . Scope ( projectStorage ) ,
73+ projectPathLayer = new PreferencesBase . PathLayer ( ) ;
74+
75+ projectScope . addLayer ( projectPathLayer ) ;
76+
77+ var userScopeLoading = manager . addScope ( "user" , new PreferencesBase . FileStorage ( userPrefFile , true ) ) ;
78+
79+ _addScopePromises . push ( userScopeLoading ) ;
80+
81+ // Set up the .brackets.json file handling
82+ userScopeLoading
83+ . fail ( function ( err ) {
84+ _addScopePromises . push ( manager . addScope ( "user" , new PreferencesBase . MemoryStorage ( ) , {
85+ before : "default"
86+ } ) ) ;
87+
88+ if ( err . name && err . name === "ParsingError" ) {
89+ userScopeCorrupt = true ;
90+ }
91+ } )
92+ . always ( function ( ) {
93+ _addScopePromises . push ( manager . addScope ( "project" , projectScope , {
94+ before : "user"
95+ } ) ) ;
96+
97+ // Session Scope is for storing prefs in memory only but with the highest precedence.
98+ _addScopePromises . push ( manager . addScope ( "session" , new PreferencesBase . MemoryStorage ( ) ) ) ;
99+
100+ Async . waitForAll ( _addScopePromises )
101+ . always ( function ( ) {
102+ _prefManagerReadyDeferred . resolve ( ) ;
103+ } ) ;
104+ } ) ;
105+
106+ // "State" is stored like preferences but it is not generally intended to be user-editable.
107+ // It's for more internal, implicit things like window size, working set, etc.
108+ var stateManager = new PreferencesBase . PreferencesSystem ( ) ;
109+ var userStateFile = brackets . app . getApplicationSupportDirectory ( ) + "/" + STATE_FILENAME ;
110+ var smUserScope = new PreferencesBase . Scope ( new PreferencesBase . FileStorage ( userStateFile , true ) ) ;
111+ var stateProjectLayer = new PreferencesBase . ProjectLayer ( ) ;
112+ smUserScope . addLayer ( stateProjectLayer ) ;
113+ var smUserScopeLoading = stateManager . addScope ( "user" , smUserScope ) ;
114+
115+ // Semi-Public API. Use this at your own risk. The public API is in PreferencesManager.
116+ exports . manager = manager ;
117+ exports . projectStorage = projectStorage ;
118+ exports . projectPathLayer = projectPathLayer ;
119+ exports . userScopeLoading = userScopeLoading ;
120+ exports . stateManager = stateManager ;
121+ exports . stateProjectLayer = stateProjectLayer ;
122+ exports . smUserScopeLoading = smUserScopeLoading ;
123+ exports . userPrefFile = userPrefFile ;
124+ exports . isUserScopeCorrupt = isUserScopeCorrupt ;
125+ exports . managerReady = _prefManagerReadyDeferred . promise ( ) ;
126+ exports . STATE_FILENAME = STATE_FILENAME ;
127+ exports . SETTINGS_FILENAME = SETTINGS_FILENAME ;
128+ } ) ;
0 commit comments