1// ======================================================================
2//	File:		KCOperationID.h
3//
4//	OperationID must be registered so that a test script knows how to
5//	construct an appropriate KCOperation class
6//
7//	Copyright:	Copyright (c) 2000,2003 Apple Computer, Inc. All Rights Reserved.
8//
9//	Change History (most recent first):
10//
11//		 <1>	2/22/00	em		Created.
12// ======================================================================
13#ifndef __KC_OPERATION_ID__
14#define __KC_OPERATION_ID__
15#include <stdio.h>
16enum eKCOperationID {
17	OpID_Unknown = -1,
18	OpID_COp_KCGetKeychainManagerVersion = 0,
19	OpID_COp_KeychainManagerAvailable,//1
20	OpID_COp_KCMakeKCRefFromFSRef,//2
21	OpID_COp_KCMakeKCRefFromFSSpec,//3
22	OpID_COp_KCMakeKCRefFromAlias,//4
23	OpID_COp_KCMakeAliasFromKCRef,//5
24	OpID_COp_KCReleaseKeychain,//6
25	OpID_COp_KCUnlockNoUI,//7
26	OpID_COp_KCUnlock,//8
27	OpID_COp_KCUnlockWithInfo,//9
28	OpID_COp_KCLock,//10
29	OpID_COp_KCLockNoUI,//11
30	OpID_COp_KCGetDefaultKeychain,//12
31	OpID_COp_KCSetDefaultKeychain,//13
32	OpID_COp_KCCreateKeychain,//14
33	OpID_COp_KCCreateKeychainNoUI,//15
34	OpID_COp_KCGetStatus,//16
35	OpID_COp_KCChangeSettingsNoUI,//17
36	OpID_COp_KCGetKeychain,//18
37	OpID_COp_KCGetKeychainName,//19
38	OpID_COp_KCChangeSettings,//20
39	OpID_COp_KCCountKeychains,//21
40	OpID_COp_KCGetIndKeychain,//22
41	OpID_COp_KCAddCallback,//23
42	OpID_COp_KCRemoveCallback,//24
43	OpID_COp_KCSetInteractionAllowed,//25
44	OpID_COp_KCIsInteractionAllowed,//26
45	OpID_COp_KCAddAppleSharePassword,//27
46	OpID_COp_KCFindAppleSharePassword,//28
47	OpID_COp_KCAddInternetPassword,//29
48	OpID_COp_KCAddInternetPasswordWithPath,//30
49	OpID_COp_KCFindInternetPassword,//31
50	OpID_COp_KCFindInternetPasswordWithPath,//32
51	OpID_COp_KCAddGenericPassword,//33
52	OpID_COp_KCFindGenericPassword,//34
53	OpID_COp_KCNewItem,//35
54	OpID_COp_KCSetAttribute,//36
55	OpID_COp_KCGetAttribute,//37
56	OpID_COp_KCSetData,//38
57	OpID_COp_KCGetData,//39
58	OpID_COp_KCGetDataNoUI,//40
59	OpID_COp_KCAddItem,//41
60	OpID_COp_KCAddItemNoUI,//42
61	OpID_COp_KCDeleteItem,//43
62	OpID_COp_KCDeleteItemNoUI,//44
63	OpID_COp_KCUpdateItem,//45
64	OpID_COp_KCReleaseItem,//46
65	OpID_COp_KCCopyItem,//47
66	OpID_COp_KCFindFirstItem,//48
67	OpID_COp_KCFindNextItem,//49
68	OpID_COp_KCReleaseSearch,//50
69	OpID_COp_KCFindX509Certificates,//51
70	OpID_COp_KCChooseCertificate,//52
71	OpID_COp_kcunlock,//53
72	OpID_COp_kccreatekeychain,//54
73	OpID_COp_kcgetkeychainname,//55
74	OpID_COp_kcaddapplesharepassword,//56
75	OpID_COp_kcfindapplesharepassword,//57
76	OpID_COp_kcaddinternetpassword,//58
77	OpID_COp_kcaddinternetpasswordwithpath,//59
78	OpID_COp_kcfindinternetpassword,//60
79	OpID_COp_kcfindinternetpasswordwithpath,//61
80	OpID_COp_kcaddgenericpassword,//62
81	OpID_COp_kcfindgenericpassword,//63
82	OpID_COp_KCLogin,//64
83	OpID_COp_KCLogout,//65
84	OpID_COp_KCChangeLoginPassword,//66
85	OpID_NumOperations
86};
87#define	IS_VALID_OPERATIONID(aID)	(aID >= 0 && aID < OpID_NumOperations)
88
89typedef	void *	(*tClassCreatorFunc)(void *inClient);
90typedef struct tOperationInfo{
91	const char *		name;
92	tClassCreatorFunc	func;
93} tOperationInfo;
94// ���������������������������������������������������������������������������
95// 	� COpRegister
96// ���������������������������������������������������������������������������
97class COpRegister {
98public:
99	static void *				CreateOperation(
100		 							eKCOperationID			inID,
101									void					*inClient)
102		 						{
103		 							RegisterAll();
104                                    if(IS_VALID_OPERATIONID(inID)){
105                                        if(sOperationInfoTbl[inID].func)
106                                            return (sOperationInfoTbl[inID].func)(inClient);
107									}
108                                    return NULL;
109								}
110
111	static const char *			GetOperationName(
112		 							eKCOperationID			inID)
113		 						{
114		 							RegisterAll();
115                                    if(IS_VALID_OPERATIONID(inID))
116                                        return sOperationInfoTbl[inID].name;
117                                    else
118                                        return "INVALID OPERATION ID";
119		 						}
120
121	static void					RegisterAll();
122	static void					RegisterOne(
123		 							eKCOperationID			inID,
124		 							const char *			inName,
125		 							tClassCreatorFunc		inFunc = NULL)
126		 						{
127		 							sOperationInfoTbl[inID].name = inName;
128		 							sOperationInfoTbl[inID].func = inFunc;
129		 						}
130protected:
131	static bool					sRegistered;
132	static tOperationInfo		sOperationInfoTbl[OpID_NumOperations];
133};
134template <class T>
135class TOpRegister{
136public:
137	static T *					Create(
138									void	*inClient)
139								{
140									T	*aOperation = new T;
141									if(aOperation) aOperation->SetClient(inClient);
142									return aOperation;
143								}
144	static void					RegisterOne(
145		 							eKCOperationID			inID,
146		 							const char *			inName)
147		 						{
148		 							COpRegister::RegisterOne(inID, inName, (tClassCreatorFunc)Create);
149		 						}
150};
151
152										// Relax notations so one can avoid typing long names
153#define KC_CLASS(funcname)				COp_## funcname
154#define KC_OP_ID(funcname)				OpID_COp_## funcname
155#define Register(funcname)				TOpRegister<KC_CLASS(funcname)>::RegisterOne(KC_OP_ID(funcname), (const char*)#funcname "")
156#define OPERATION_ID(funcname)			enum{ operation_ID = KC_OP_ID(funcname) };\
157										eKCOperationID	GetID(){ return (eKCOperationID)operation_ID; };
158
159#endif	// __KC_OPERATION_ID__
160