2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
22 #ifndef _SECURITY_DLDBLISTCFPREF_H_
23 #define _SECURITY_DLDBLISTCFPREF_H_
25 #include <Security/cfutilities.h>
26 #include <CoreFoundation/CFDictionary.h>
27 #include <CoreFoundation/CFPreferences.h>
28 #include <Security/DLDBList.h>
29 #include <Security/cssmdb.h>
31 #include <CoreFoundation/CFNumber.h>
32 #include <CoreFoundation/CFDate.h>
38 class DLDbListCFPref
: public CssmClient::DLDbList
41 DLDbListCFPref(CFStringRef theDLDbListKey
=NULL
,CFStringRef prefsDomain
=NULL
);
45 CssmClient::DLDbList
& list() { return *this; } // eventually, it should check mod dates of CFPrefs file, etc.
47 static DLDbIdentifier
cfDictionaryRefToDLDbIdentifier(CFDictionaryRef theDict
);
48 static CFDictionaryRef
dlDbIdentifierToCFDictionaryRef(const DLDbIdentifier
& dldbIdentifier
);
49 bool revert(bool force
);
50 void clearDefaultKeychain();
52 static string
ExpandTildesInPath(const string
&inPath
);
53 static string
StripPathStuff(const string
&inPath
);
54 static string
AbbreviatedPath(const string
&inPath
);
55 static string
HomeDir();
58 // Private member variables
59 CFStringRef mPrefsDomain
;
60 CFStringRef mDLDbListKey
;
62 // Private member functions
65 CFAbsoluteTime mPrefsTimeStamp
;
72 T
cfref() const { return reinterpret_cast<T
>(CFTypeRef(mRef
)); }
75 CCFValue(CFTypeRef ref
) : mRef(ref
) {}
76 CCFValue
&operator =(CFTypeRef ref
) { mRef
= ref
; return *this; }
78 CCFValue
&operator = (bool value
)
80 mRef
= value
?kCFBooleanTrue
:kCFBooleanFalse
;
85 CCFValue &operator = (const string &value) { string(value); return *this; }
87 void string(const string &value, CFStringEncoding encoding=kCFStringEncodingMacRoman)
89 mRef = CFStringCreate();
90 CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
91 if (!mRef) throw std::bad_alloc;
96 bool hasValue() const { return mRef
; }
100 if (!mRef
) return false;
101 if (::CFGetTypeID(mRef
) != ::CFBooleanGetTypeID())
102 throw std::logic_error("wrong type in property list");
104 return ::CFBooleanGetValue(cfref
<CFBooleanRef
>());
107 operator sint32() const
110 if (::CFGetTypeID(mRef
) != ::CFNumberGetTypeID())
111 throw std::logic_error("wrong type in property list");
114 ::CFNumberGetValue(cfref
<CFNumberRef
>(),kCFNumberSInt32Type
,&val
);
118 operator uint32() const { return uint32(sint32(*this)); }
120 operator const string() const { return getString(); }
122 const string
getString(CFStringEncoding encoding
=kCFStringEncodingMacRoman
) const
125 throw std::logic_error("missing string in property list");
126 if (::CFGetTypeID(mRef
) != ::CFStringGetTypeID())
127 throw std::logic_error("wrong type in property list");
129 const char *tmpStr
=::CFStringGetCStringPtr(cfref
<CFStringRef
>(),encoding
);
132 CFIndex maxLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfref
<CFStringRef
>()), encoding
);
133 auto_array
<char> buffer(maxLen
+ 1);
135 if (!::CFStringGetCString(cfref
<CFStringRef
>(),buffer
.get(),maxLen
+ 1,encoding
))
136 throw std::logic_error("could not convert string from property list");
139 return string(tmpStr
?tmpStr
:"");
141 return string(tmpStr
?tmpStr
:"");
144 CFCopyRef
<CFTypeRef
>mRef
;
147 } // end namespace Security
149 #endif /* !_SECURITY_DLDBLISTCFPREF_H_ */