summaryrefslogtreecommitdiff
path: root/NSCmpts/ActionEngine.cpp
blob: 59673b1fbabe8e6f4609851df043f4673ffeb0a6 (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
// ActionEngine.cpp: implementation of the CActionEngine class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ActionEngine.h"

#include "NSMessages.h"
#include <appmisc.h>

//////////////////////////////////////////////////////////////////////
// CActionEngine

// Starts with only this action
HRESULT CActionEngine::Start(CAction* pFirstAction)
{
	ASSERT(pFirstAction);
	clear();
	pFirstAction->addref();
	push_back(pFirstAction);
	return Start();
}

///////////////////////////////////////////////////////////////////////
// Starts the Actual Processing

HRESULT CActionEngine::Start()
{
	HRESULT hr = S_OK;
	CAction* pAction = NULL;
	UINT nDone = 0;

	// Keep going while there are actions to process
	while(size())
	{
		// Get current action
		pAction = at(0);
		ASSERT(pAction);

		try
		{
			// Do Action
			pAction->Do(this, m_hwndUpdates);

		}
		catch(CActionError& e)
		{
			if(m_hwndErrors)
			{
				// Send Error to Window
				NS_ERROR_DATA nsErr;
				nsErr.hRes = e.m_hRes;
				nsErr.szDesc = e.m_sDesc;
				nsErr.lParam = (LPARAM)pAction;

				::SendMessage(m_hwndErrors, NSM_ERROR, NULL, (LPARAM)&nsErr);
			}

			hr = E_FAIL;
		}

		// Update the Window
		nDone++;

		if(m_hwndUpdates)
		{
			// Send Update Message
			NS_UPDATE_DATA nsUpd;
			nsUpd.lCur = nDone;
			nsUpd.lTot = nDone + size();
			nsUpd.szMessage1 = NULL;
			nsUpd.szMessage2 = NULL;

			// If message returns false then cancel
			if(!::SendMessage(m_hwndUpdates, NSM_UPDATE, NULL, (LPARAM)&nsUpd))
			{
				hr = E_ABORT;
				break;
			}
		}

		// Remove action from array and release it
		erase(begin());
		pAction->release();
	}

	return hr;
}

/////////////////////////////////////////////////////////////////////
// Starts the Engine in another thread
HANDLE CActionEngine::StartThread()
{
	DWORD dwThreadID = NULL;
	return (HANDLE)CreateThread(NULL, 0, ThreadProc,	
							    (LPVOID)this, 0, &dwThreadID);
}

DWORD CALLBACK CActionEngine::ThreadProc(LPVOID lpParam)
{
	// Need to Initialize for this thread
	// BUG: This didn't work in Win95 (non-OSR2) because
	// only have CoInitialize
	// HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
	HRESULT hr = CoInitialize(NULL);

	ASSERT(lpParam);

	CActionEngine* pEngine = (CActionEngine*)lpParam;
	HRESULT hRet = pEngine->Start();

	if(SUCCEEDED(hr))
		CoUninitialize();

	// Return the result of the engine
	// as our thread exit code
	return hRet;
}

// Sets the pointers for the update windows
void CActionEngine::SetUpdates(HWND hwndUpdates /*= NULL*/, 
							   HWND hwndErrors /*= NULL*/)
{
	m_hwndUpdates = hwndUpdates;
	m_hwndErrors = hwndErrors;
}


///////////////////////////////////////////////////////////////
// Array Management Stuff

UINT CActionEngine::Add(CAction* pAction)
{ 
	ASSERT(pAction);
	pAction->addref();
	push_back(pAction); 
	return size(); 
}

UINT CActionEngine::Insert(CAction* pAction, UINT nIndex)
{ 
	ASSERT(pAction);
	ASSERT(nIndex < size());
	pAction->addref();
	insert(begin() + nIndex, pAction);
	return nIndex;
}

void CActionEngine::Remove(UINT nIndex)
{
	ASSERT(nIndex < size());
	CAction* pAction = at(nIndex);
	erase(begin() + nIndex);
	pAction->release();
}

void CActionEngine::RemoveAll(CAction* pKeep /*= NULL*/)
{
	if(pKeep)
		pKeep->addref();

	while(size())
	{
		CAction* pAction = at(0);
		pAction->release();
		erase(begin());		
	}

	if(pKeep)
	{
		Add(pKeep);
		pKeep->release();
	}
}

CAction* CActionEngine::At(UINT nIndex)
{
	ASSERT(nIndex < size());
	return at(nIndex);
}





////////////////////////////////////////////////////////////////////////
// CAction Helper Function
////////////////////////////////////////////////////////////////////////

void CAction::Update(HWND hwndUpdates, LPCTSTR szDesc)
{
	// Send a message with only the string valid
	NS_UPDATE_DATA nsUpd;
	nsUpd.lCur = -1;
	nsUpd.lTot = -1;
	nsUpd.szMessage1 = NULL;
	nsUpd.szMessage2 = szDesc;

	::SendMessage(hwndUpdates, NSM_UPDATE, NULL, (LPARAM)&nsUpd);
}

void CAction::Update(HWND hwndUpdates, LPCTSTR szStatus, LPCTSTR szDesc)
{
	// Send a message with only the string valid
	NS_UPDATE_DATA nsUpd;
	nsUpd.lCur = -1;
	nsUpd.lTot = -1;
	nsUpd.szMessage1 = szStatus;
	nsUpd.szMessage2 = szDesc;

	::SendMessage(hwndUpdates, NSM_UPDATE, NULL, (LPARAM)&nsUpd);
}



//////////////////////////////////////////////////////////////////////
// CActionError Construction Helper Functions

inline CActionError::CActionError(HRESULT hr, const string& sMessage, 
						   const file_path& path /*= file_path()*/)
{
	Construct(hr, sMessage, path); 
}

CActionError::CActionError(HRESULT hr, UINT nID, 
						   const file_path& path /*= file_path()*/)
{ 
	string sTemp; 
	sTemp.load_string(nID); 
	Construct(hr, sTemp, path);
}

CActionError::CActionError(const string& sError, HRESULT hr, UINT nID, 
						   const file_path& path /*= file_path()*/)
{
	string sTemp;
	sTemp.load_string(nID);
	Construct(sError, hr, sTemp, path);
}

inline CActionError::CActionError(const string& sError, HRESULT hr, const string& sMessage, 
						   const file_path& path /*= file_path()*/)
{ 
	Construct(sError, hr, sMessage, path); 
}


void CActionError::Construct(const string& sError, HRESULT hr, string sMessage, const file_path& path)
{
	// Replace with custom message before passing on
	sMessage.replace(_T("%e"), sError);
	Construct(hr, sMessage, path);
}

void CActionError::Construct(HRESULT hr, string sMessage, const file_path& path)
{
	m_hRes = hr;

	// If there's a need then replace
	if(sMessage.find(_T("%e")) != string::npos)
		sMessage.replace(_T("%e"), FormatHR(m_hRes));

	// Same here
	if(path.valid())
	{
		sMessage.replace(_T("%f"), path.file());
		sMessage.replace(_T("%F"), path);
	}

	m_sDesc = sMessage;
}

void Lower(string& s)
{
	s.make_lower();
}