Jamoma API  0.6.0.a19
CACFPreferences.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 //==================================================================================================
42 // Includes
43 //==================================================================================================
44 
45 // Self Include
46 #include "CACFPreferences.h"
47 
48 // PublicUtility Includes
49 #include "CACFDistributedNotification.h"
50 #include "CADebugMacros.h"
51 
52 //==================================================================================================
53 // CACFPreferences
54 //==================================================================================================
55 
56 CFPropertyListRef CACFPreferences::CopyValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost)
57 {
58  // synchronize to make sure that what's in memory matches what's on disk
59  Synchronize(inCurrentUser, inCurrentHost, false);
60 
61  CFPropertyListRef theAnswer = NULL;
62  CFStringRef theUser = inCurrentUser ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
63  CFStringRef theHost = inCurrentHost ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
64 
65  theAnswer = CFPreferencesCopyValue(inKey, kCFPreferencesAnyApplication, theUser, theHost);
66 
67  return theAnswer;
68 }
69 
70 CFStringRef CACFPreferences::CopyStringValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost)
71 {
72  CFStringRef theAnswer = NULL;
73 
74  // get the raw value
75  CFPropertyListRef theRawValue = CopyValue(inKey, inCurrentUser, inCurrentHost);
76 
77  if(theRawValue != NULL)
78  {
79  // get it's type ID and make sure it's a CFString
80  CFTypeID theTypeID = CFGetTypeID(theRawValue);
81  if(theTypeID == CFStringGetTypeID())
82  {
83  // cast the value
84  theAnswer = static_cast<CFStringRef>(theRawValue);
85  }
86  else
87  {
88  CFRelease(theRawValue);
89  DebugMessage("CACFPreferences::CopyStringValue: not a CFString");
90  }
91  }
92 
93  return theAnswer;
94 }
95 
96 CFNumberRef CACFPreferences::CopyNumberValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost)
97 {
98  CFNumberRef theAnswer = NULL;
99 
100  // get the raw value
101  CFPropertyListRef theRawValue = CopyValue(inKey, inCurrentUser, inCurrentHost);
102 
103  if(theRawValue != NULL)
104  {
105  // get it's type ID and make sure it's a CFNumber
106  CFTypeID theTypeID = CFGetTypeID(theRawValue);
107  if(theTypeID == CFNumberGetTypeID())
108  {
109  // cast the value
110  theAnswer = static_cast<CFNumberRef>(theRawValue);
111  }
112  else
113  {
114  CFRelease(theRawValue);
115  DebugMessage("CACFPreferences::CopyNumberValue: not a CFNumber");
116  }
117  }
118 
119  return theAnswer;
120 }
121 
122 CFArrayRef CACFPreferences::CopyArrayValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost)
123 {
124  CFArrayRef theAnswer = NULL;
125 
126  // get the raw value
127  CFPropertyListRef theRawValue = CopyValue(inKey, inCurrentUser, inCurrentHost);
128 
129  if(theRawValue != NULL)
130  {
131  // get it's type ID and make sure it's a CFArray
132  CFTypeID theTypeID = CFGetTypeID(theRawValue);
133  if(theTypeID == CFArrayGetTypeID())
134  {
135  // cast the value
136  theAnswer = static_cast<CFArrayRef>(theRawValue);
137  }
138  else
139  {
140  CFRelease(theRawValue);
141  DebugMessage("CACFPreferences::CopyArrayValue: not a CFArray");
142  }
143  }
144 
145  return theAnswer;
146 }
147 
148 CFDictionaryRef CACFPreferences::CopyDictionaryValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost)
149 {
150  CFDictionaryRef theAnswer = NULL;
151 
152  // get the raw value
153  CFPropertyListRef theRawValue = CopyValue(inKey, inCurrentUser, inCurrentHost);
154 
155  if(theRawValue != NULL)
156  {
157  // get it's type ID and make sure it's a CFDictionary
158  CFTypeID theTypeID = CFGetTypeID(theRawValue);
159  if(theTypeID == CFDictionaryGetTypeID())
160  {
161  // cast the value
162  theAnswer = static_cast<CFDictionaryRef>(theRawValue);
163  }
164  else
165  {
166  CFRelease(theRawValue);
167  DebugMessage("CACFPreferences::CopyDictionaryValue: not a CFDictionary");
168  }
169  }
170 
171  return theAnswer;
172 }
173 
174 void CACFPreferences::SetValue(CFStringRef inKey, CFPropertyListRef inValue, bool inCurrentUser, bool inCurrentHost, bool inSynchronize)
175 {
176  CFStringRef theUser = inCurrentUser ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
177  CFStringRef theHost = inCurrentHost ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
178  CFPreferencesSetValue(inKey, inValue, kCFPreferencesAnyApplication, theUser, theHost);
179 
180  if(inSynchronize)
181  {
182  Synchronize(inCurrentUser, inCurrentHost, true);
183  }
184 }
185 
186 void CACFPreferences::DeleteValue(CFStringRef inKey, bool inCurrentUser, bool inCurrentHost, bool inSynchronize)
187 {
188  CFStringRef theUser = inCurrentUser ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
189  CFStringRef theHost = inCurrentHost ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
190  CFPreferencesSetValue(inKey, NULL, kCFPreferencesAnyApplication, theUser, theHost);
191 
192  if(inSynchronize)
193  {
194  Synchronize(theUser, inCurrentHost, true);
195  }
196 }
197 
198 void CACFPreferences::Synchronize(bool inCurrentUser, bool inCurrentHost, bool inForce)
199 {
200  if(inForce || ArePrefsOutOfDate(inCurrentUser, inCurrentHost))
201  {
202  CFStringRef theUser = inCurrentUser ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
203  CFStringRef theHost = inCurrentHost ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
204  CFPreferencesSynchronize(kCFPreferencesAnyApplication, theUser, theHost);
205  MarkPrefsClean(inCurrentUser, inCurrentHost);
206  }
207 }
208 
209 void CACFPreferences::MarkPrefsOutOfDate(bool inCurrentUser, bool inCurrentHost)
210 {
211  if(!inCurrentUser && !inCurrentHost)
212  {
213  sAnyUserAnyHostPrefsOutOfDate = true;
214  }
215  else if(!inCurrentUser && inCurrentHost)
216  {
217  sAnyUserCurrentHostPrefsOutOfDate = true;
218  }
219  else if(inCurrentUser && !inCurrentHost)
220  {
221  sCurrentUserAnyHostPrefsOutOfDate = true;
222  }
223  else if(inCurrentUser && inCurrentHost)
224  {
225  sCurrentUserCurrentHostPrefsOutOfDate = true;
226  }
227 }
228 
229 void CACFPreferences::MarkPrefsClean(bool inCurrentUser, bool inCurrentHost)
230 {
231  if(!inCurrentUser && !inCurrentHost)
232  {
233  sAnyUserAnyHostPrefsOutOfDate = false;
234  }
235  else if(!inCurrentUser && inCurrentHost)
236  {
237  sAnyUserCurrentHostPrefsOutOfDate = false;
238  }
239  else if(inCurrentUser && !inCurrentHost)
240  {
241  sCurrentUserAnyHostPrefsOutOfDate = false;
242  }
243  else if(inCurrentUser && inCurrentHost)
244  {
245  sCurrentUserCurrentHostPrefsOutOfDate = false;
246  }
247 }
248 
249 void CACFPreferences::SendNotification(CFStringRef inName)
250 {
251  CACFDistributedNotification::PostNotification(inName, NULL, true);
252 }
253 
254 bool CACFPreferences::ArePrefsOutOfDate(bool inCurrentUser, bool inCurrentHost)
255 {
256  bool theAnswer = false;
257 
258  if(!inCurrentUser && !inCurrentHost)
259  {
260  theAnswer = sAnyUserAnyHostPrefsOutOfDate;
261  }
262  else if(!inCurrentUser && inCurrentHost)
263  {
264  theAnswer = sAnyUserCurrentHostPrefsOutOfDate;
265  }
266  else if(inCurrentUser && !inCurrentHost)
267  {
268  theAnswer = sCurrentUserAnyHostPrefsOutOfDate;
269  }
270  else if(inCurrentUser && inCurrentHost)
271  {
272  theAnswer = sCurrentUserCurrentHostPrefsOutOfDate;
273  }
274 
275  return theAnswer;
276 }
277 
278 bool CACFPreferences::sAnyUserAnyHostPrefsOutOfDate = false;
279 bool CACFPreferences::sAnyUserCurrentHostPrefsOutOfDate = false;
280 bool CACFPreferences::sCurrentUserAnyHostPrefsOutOfDate = false;
281 bool CACFPreferences::sCurrentUserCurrentHostPrefsOutOfDate = false;