summaryrefslogtreecommitdiff
path: root/Common/ComponentArray.cpp
blob: 7bab44c47845a384aff304e21529ab8b46a0462a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// ComponentArray.cpp: implementation of the CComponentArray class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ComponentArray.h"
#include "defines.h"

#include <algorithm>
using std::find;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CComponentArray::CComponentArray() { }

CComponentArray::~CComponentArray() 
{
	UnloadComponents();
}

//////////////////////////////////////////////////////////////////
// LoadComponents from Registry (Public Function)
//
// returns number of components.

UINT CComponentArray::LoadComponents()
{
	// Can only load if empty
	ASSERT(empty());

	// Make sure List of Installed Components is Loaded
	LoadInstalledComponents();


	// Open Key
	CRegKey regKey;
	if(regKey.Create(HKEY_CURRENT_USER, NS_REG_COMPONENTS)
		!= ERROR_SUCCESS)

			return 0;

	// Enumerate Subkeys which should contain Component ID's (GetIDFromKey)
	DWORD dwCnt = 0;
	DWORD dwLoadedCnt = 0;
	string sKeyName;
	while(!RegEnumKey(regKey.m_hKey, dwCnt, sKeyName.get_buffer(MAX_PATH), MAX_PATH))
	{
		sKeyName.release_buffer();

		dwCnt++;

		// Add the Component
		if(SUCCEEDED(AddComponent(sKeyName)))
			dwLoadedCnt++;	// Increment Return Value
	}

	// Check if there's anymore that are Installed
	// But the user didn't have previously loaded (CheckOffInstalledComponent)
	while(!m_asInstalledComponents.empty())
	{
		// Add the Component
		if(SUCCEEDED(AddComponent(m_asInstalledComponents[0])))
			dwLoadedCnt++;
	}

	// Remove All NULL Spaces in array as we put each component
	// at location requested by registry
	iterator iter = begin();
	for(int nCnt = 0; nCnt < size(); nCnt++)
	{
		while((*this)[nCnt] == NULL)
			erase(begin() + nCnt);
	}

	if(empty())
		return 0;

	// Move DOS Components to End of Array
	CComponentHolder* pComponent = NULL;
	CComponentHolder* pLastComp = at(size() - 1);
	for(nCnt = 0; nCnt < size() && pComponent != pLastComp; nCnt++)
	{
		pComponent = at(nCnt);

		if(pComponent->GetType() == COMPONENT_DOS)
		{
			erase(begin() + nCnt);
			push_back(pComponent);
		}
	}

	// Return Number of Components
	return (UINT)dwLoadedCnt;
}

//////////////////////////////////////////////////////////////////
//  Loads the component into memory and adds it to array at 
//  location requested. 

HRESULT CComponentArray::AddComponent(const string& sKeyName)
{
	CComponentHolder* pComponent;
	HRESULT hr;


	// Get the Component ID from a Registry KeyName
	// eg: 'NightSecurity.ClearInetCache' would come from 'NightSecurity.ClearInetCache 2'
	string sID = GetIDFromKey(sKeyName);


	// Load and Initialize Component
	pComponent = new CComponentHolder;
	hr = pComponent->Initialize(sID, NS_REG_COMPONENTS + (_T("\\") + sKeyName));


	// If Failed then return NULL
	if(SUCCEEDED(hr)) 
	{

		// Get the Component's Position
		int nPos = pComponent->GetPos();


		// Add at nPos
		// If nPos is already used insert before
		if(nPos < size()) 
		{

			if(at(nPos) == NULL) 
				at(nPos) = pComponent;
			else 
				insert(begin() + nPos, pComponent);

		}
		else 
		{
				resize(nPos + 1, NULL);
				at(nPos) = pComponent;
		}


	}

	// Check it off from Installed List
	CheckOffInstalledComponent(sID);


	return hr;
}


/////////////////////////////////////////////////////////
// LoadInstalledComponents : Loads list of installed 
// components for access later on
// 
UINT CComponentArray::LoadInstalledComponents()
{

	// Open Key
	CRegKey regKey;
	if(regKey.Create(HKEY_LOCAL_MACHINE, NS_REG_INSTALLEDCOMPONENTS)
					!= ERROR_SUCCESS)

			return 0;



	// Enumerate Subkeys which should be the IDs of Components
	DWORD dwCnt = 0;
	string sKeyName;
	while(!RegEnumKey(regKey.m_hKey, dwCnt, sKeyName.get_buffer(MAX_PATH), MAX_PATH))
	{

		sKeyName.release_buffer();

		// Add to List
		m_asInstalledComponents.push_back(sKeyName);

		dwCnt++;
	}


	// Return Number of Components
	return (UINT)dwCnt;

}

//////////////////////////////////////////////////////////////////
// Checks off the component from the Installed Components list
// Loaded with Installed Components

bool CComponentArray::CheckOffInstalledComponent(const string& sID)
{
	int nFound = -1;

	string_array::iterator iter;
	iter = find(m_asInstalledComponents.begin(), 
				m_asInstalledComponents.end(), sID);

	// If we found one then remove it
	// Have to remove it outside of loop or we mess up the 
	// Array Index
	if(iter != m_asInstalledComponents.end()) 
	{
		m_asInstalledComponents.erase(iter);
		return true;
	}

	// Couldn't Find
	return false;
}


//////////////////////////////////////////////////////////////////
// Remove the components from memory

UINT CComponentArray::UnloadComponents()
{
	int nCnt = 0;

	while(!empty())
	{
		nCnt++;

		// First let it do it's Clean Up
		at(0)->Release();
		// Then Delete it
		delete at(0);
		// Then Remove it from Array
		erase(begin());
	}

	// Return Number of Components
	return nCnt;
}


//////////////////////////////////////////////////////////////////
// Takes a Component registry key and extracts the Program ID
// ID should come before space in Key
// eg: 'NightSecurity.ClearInetCache' would come from 'NightSecurity.ClearInetCache 2'

string CComponentArray::GetIDFromKey(const string& sKeyName)
{
	// ID should come before space in Key
	// eg: 'NightSecurity.ClearInetCache' would come from 'NightSecurity.ClearInetCache 2'

	string::size_type pos = sKeyName.find(_T(' '));

	// return first part
	// find return string::npos if fails (which also means end of string)
	return sKeyName.substr(0, pos);

}


//////////////////////////////////////////////////////////////////
// Saves each components data to the Registry

HRESULT CComponentArray::SaveComponentData()
{
	HRESULT hrRet = S_OK;

	// Ask Each Component to Save it's own Data
	iterator iter = begin();
	for(; iter != end(); iter++)
	{

		// CComponentHolder checks to make sure that the
		// Components are loaded, so we don't have to 
		// include error checking here.
		HRESULT hr = (*iter)->GetData()->Save();
		if(FAILED(hr))
			hrRet = hr;
		
	}

	return hrRet;
}