summaryrefslogtreecommitdiff
path: root/NSCmpts/ActionEngine.h
blob: 301ed818bcea7d374ebc14e7df2169d64d831141 (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
// ActionEngine.h: interface for the CActionEngine class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_)
#define AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

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

////////////////////////////////////////////////////////////////////////
// Hooks onto any object to provide reference count services

template<class TBase>
class counted_obj
{
public:
	counted_obj() 
		{ m_lCount = 0; }
	long addref() 
		{ return ++m_lCount; }
	long release()
	{
		long lCount = --m_lCount;

		if(lCount <= 0)
			delete (TBase*)this;

		return lCount;
	}
protected:
	long m_lCount;
};

////////////////////////////////////////////////////////////////////////
// Forward Declarations

class CAction;				// Action processed by the Action Engine
class CActionError;			// Error thrown by an Action
class CActionEngine;		// The Engine

/////////////////////////////////////////////////////////////////////////
// Abstract class: An Action for the Action Engine

class CAction
	: public counted_obj<CAction>
{
public:
	CAction() {};
	virtual ~CAction() {};

	// Does the actual action
	virtual bool Do(CActionEngine* pEngine, HWND hwndUpdates) 
		throw(CActionError) = 0;
	// Retry after an error
	virtual bool IsRetryable() const throw() = 0;

	// Try and fix after there's an error
	virtual void Fix(HRESULT hr) throw(CActionError) = 0;
	virtual bool IsFixable(HRESULT hr) const throw() = 0;

protected:
	// Helper function for implementations
	// Updates the dialog
	void Update(HWND hwndUpdates, LPCTSTR szDesc);
	void Update(HWND hwndUpdates, LPCTSTR szStatus, LPCTSTR szDesc);
};



//////////////////////////////////////////////////////////////////////
// An Error thrown by an Action Object and caught by Action Engine
// With built in formatting
//
// %e	Error Text
// %f	File Name
// %F	Full File Path

class CActionError
{
// Constructors
public:
	// These two get the error message from the HRESULT
	CActionError(HRESULT hr, const string& sMessage, 
				 const file_path& path = file_path());
	CActionError(HRESULT hr, UINT nID, 
				 const file_path& path = file_path());

	// These two ask you to supply the error message
	CActionError(const string& sError, HRESULT hr, UINT nID, 
		         const file_path& path = file_path());
	CActionError(const string& sError, HRESULT hr, const string& sMessage, 
		         const file_path& path = file_path());

	string m_sDesc;	// Formatted Error Text
	HRESULT m_hRes;	// Original HRESULT

// Helper Functions
protected:
	void Construct(HRESULT hr, string sMessage, const file_path& path);
	void Construct(const string& sError, HRESULT hr, string sMessage, 
				   const file_path& path);
};

/////////////////////////////////////////////////////////////////////////
// The Engine that processes CAction objects 
// Derives from a vector of Action pointers

typedef std::vector<CAction*> action_array;

class CActionEngine  
	: protected action_array
{
// Construction
public:
	CActionEngine() {};
	CActionEngine(CAction* pFirstAction)
		{ push_back(pFirstAction); }
	virtual ~CActionEngine() 
		{ RemoveAll(); };

// Actions
public:
	// These start engine in current thread
	HRESULT Start(CAction* pFirstAction);
	HRESULT Start();

	// Start engine in another thread and return immediately
	HANDLE StartThread();

// Set Properties
public:
	void SetUpdates(HWND hwndUpdates = NULL, HWND hwndErrors = NULL);

// A few array management functions for the outside world
public:
	UINT Add(CAction* pAction);
	UINT Insert(CAction* pAction, UINT nIndex);
	void Remove(UINT nIndex);
	void RemoveAll(CAction* pKeep = NULL);
	CAction* At(UINT nIndex);

protected:
	HWND m_hwndUpdates;		// Window to Send Updates to
    HWND m_hwndErrors;		// Window to Send Errors to

	// Used to start in a separate thread
	static DWORD CALLBACK ThreadProc(LPVOID lpParam);
};

void Lower(string& s);
#endif // !defined(AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_)