]> git.saurik.com Git - apple/security.git/blob - Keychain/DefaultKeychain.cpp
Security-54.tar.gz
[apple/security.git] / Keychain / DefaultKeychain.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 File: DefaultKeychain.cpp
21
22 Contains: User preference for default keychain
23
24 Copyright: 2000 by Apple Computer, Inc., all rights reserved.
25
26 To Do:
27 */
28
29 #include "DefaultKeychain.h"
30
31 #include "CCallbackMgr.h"
32 #include "KCEventNotifier.h"
33 #include "Keychains.h"
34 #include "Globals.h"
35 #include "KCExceptions.h"
36
37 using namespace KeychainCore;
38 using namespace CssmClient;
39
40 DefaultKeychain::DefaultKeychain() : mPref(CFSTR("DefaultKeychain"))
41 {
42 }
43
44 // Set/Get via DLDbIdentifier
45 void DefaultKeychain::dLDbIdentifier(const DLDbIdentifier& keychainID)
46 {
47 DLDbList& theList=mPref.list();
48 if (theList.size()>0 && keychainID==theList[0]) // already the default keychain
49 return;
50 theList.clear();
51 mPref.add(keychainID); // destructor will save
52 mPref.save();
53 KCEventNotifier::PostKeychainEvent(kSecDefaultChangedEvent, keychainID);
54 defaultID = keychainID;
55 }
56
57 // unset default
58 void DefaultKeychain::unset()
59 {
60 DLDbList& theList=mPref.list();
61 theList.clear();
62 mPref.clearDefaultKeychain();
63 KCEventNotifier::PostKeychainEvent(kSecDefaultChangedEvent);
64 }
65
66 void DefaultKeychain::reload(bool force)
67 {
68 if (!defaultID || mPref.revert(force))
69 {
70 DLDbList& theList=mPref.list();
71 if (theList.size()==0)
72 MacOSError::throwMe(errSecNoDefaultKeychain);
73 defaultID = theList[0];
74 }
75 }
76
77 DLDbIdentifier DefaultKeychain::dLDbIdentifier()
78 {
79 reload();
80 return defaultID;
81 }
82
83 // Set/Get via Keychain
84 void DefaultKeychain::keychain(const Keychain& keychain)
85 {
86 DefaultKeychain::dLDbIdentifier(keychain->dLDbIdentifier()); // call the main "set" routine
87 }
88
89 Keychain DefaultKeychain::keychain() // was: GetTimedDefaultKC
90 {
91 return globals().storageManager.keychain(dLDbIdentifier());
92 }
93
94 bool DefaultKeychain::isSet()
95 {
96 return mPref.list().size() != 0;
97 }