summaryrefslogtreecommitdiffstats
path: root/private/windows/gina/policy/poledit/infmgr.c
blob: 53d3f13f6e31bab964d982f8d0050c81c14b1f8e (plain) (blame)
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
//*********************************************************************
//*                  Microsoft Windows                               **
//*            Copyright(c) Microsoft Corp., 1993                    **
//*********************************************************************

#include "admincfg.h"

BOOL fInfLoaded = FALSE;

BOOL OpenTemplateDlg(HWND hWnd,TCHAR * szFilename,UINT cbFileName);

BOOL GetATemplateFile(HWND hWnd)
{
	TCHAR szTmpFilename[MAX_PATH+1];

	// user cancelled or didn't select a file
	if (!OpenTemplateDlg(hWnd,szTmpFilename,ARRAYSIZE(szTmpFilename)))
		return FALSE;

	SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_ADDSTRING,0,(LPARAM) szTmpFilename);

	return TRUE;
}

UINT LoadTemplatesFromDlg(HWND hWnd)
{
	UINT uRet;
	TCHAR szFilename[MAX_PATH+1];
	UINT i=0;
    DWORD dwUsed, dwSize;

	UnloadTemplates();	// unload previous files, if any
	PrepareToLoadTemplates();

    while (LB_ERR != SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_GETTEXT,
	         i,(LPARAM) szFilename))
	{

		uRet = LoadTemplateFile(hWnd,szFilename);

		if (uRet != ERROR_SUCCESS) {
			if (uRet != ERROR_ALREADY_DISPLAYED) {
				DisplayStandardError(hWnd,szFilename,IDS_ErrOPENTEMPLATE,uRet);
			}

			return uRet;
		}

		i++;
    }


    // Fill up the template file buffer.
    i=0;
    dwUsed = 0;
    while (LB_ERR != SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_GETTEXT,
	         i,(LPARAM) szFilename))
    {
        dwSize = lstrlen(szFilename)+1;
		if (!(pbufTemplates=ResizeBuffer(pbufTemplates,hBufTemplates,dwUsed+dwSize+4,&dwBufTemplates)))
				return ERROR_NOT_ENOUGH_MEMORY;

		lstrcpy(pbufTemplates+dwUsed,szFilename);
		dwUsed += dwSize;
		i++;
	}

	// doubly null-terminate the buffer... safe to do this because we
	// tacked on the extra "+4" in the ResizeBuffer calls above
	*(pbufTemplates+dwUsed) = TEXT('\0');

	return ERROR_SUCCESS;
}

UINT LoadTemplates(HWND hWnd)
{
	UINT uRet;
    TCHAR *szFilename;
	UINT i=0;

	if (!(*pbufTemplates))
	   return ERROR_FILE_NOT_FOUND;  //Nothing to load.
	
	UnloadTemplates();	// unload previous file, if any
	PrepareToLoadTemplates();

    szFilename = pbufTemplates;
	while (*szFilename)
	{
		uRet = LoadTemplateFile(hWnd,szFilename);

		if (uRet != ERROR_SUCCESS) {
			if (uRet != ERROR_ALREADY_DISPLAYED) {
				DisplayStandardError(hWnd,szFilename,IDS_ErrOPENTEMPLATE,uRet);
			}

			return uRet;
		}
		szFilename += lstrlen(szFilename)+1;
	}

	return ERROR_SUCCESS;
}




BOOL OpenTemplateDlg(HWND hWnd,TCHAR * szFilename,UINT cbFilename)
{
	OPENFILENAME ofn;
	TCHAR szFilter[SMALLBUF];
	TCHAR szOpenInfTitle[SMALLBUF];
	
	lstrcpy(szFilename,szNull);
	LoadSz(IDS_InfOPENTITLE,szOpenInfTitle,ARRAYSIZE(szOpenInfTitle));

	// have to load the openfile filter in 2 stages, because the string
	// contains a terminating character and LoadString won't load the
	// whole thing in one go
	memset(szFilter,0,(ARRAYSIZE(szFilter) * sizeof(TCHAR)));
	LoadSz(IDS_InfOPENFILTER1,szFilter,ARRAYSIZE(szFilter));
	LoadSz(IDS_InfOPENFILTER2,szFilter+lstrlen(szFilter)+1,ARRAYSIZE(szFilter)-
		(lstrlen(szFilter)-1));
	
	memset(&ofn,0,sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hWnd;
	ofn.hInstance = ghInst;
	ofn.lpstrFilter = szFilter;
	ofn.lpstrFile =	szFilename;
	ofn.nMaxFile = cbFilename;
	ofn.lpstrTitle = szOpenInfTitle;
	ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST
		| OFN_SHAREAWARE | OFN_HIDEREADONLY;

	return GetOpenFileName(&ofn);
}


UINT PrepareToLoadTemplates()
{
	gClassList.nUserDataItems = 0;
	gClassList.nMachineDataItems = 0;

	if (!InitializeRootTables()) 
		return ERROR_NOT_ENOUGH_MEMORY;

        return ERROR_SUCCESS;
}

UINT LoadTemplateFile(HWND hWnd,TCHAR * szFilename)
{
	HANDLE hFile;
	UINT uRet;
	TCHAR szStatusTextOld[255];
	TCHAR szStatusText[SMALLBUF+MAX_PATH+1];

	// open the specified file
	if ((hFile = CreateFile(szFilename,GENERIC_READ,FILE_SHARE_READ,
		NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)) == INVALID_HANDLE_VALUE) {
#ifdef DEBUG
		wsprintf(szDebugOut,TEXT("LoadTemplateFile: CreateFile returned %lu\r\n"),
			GetLastError());
		OutputDebugString(szDebugOut);
#endif
		return (UINT) GetLastError();
	}

	// save old status bar text
	GetStatusText(szStatusTextOld,ARRAYSIZE(szStatusTextOld));

	// set status bar text a la "loading template file <name>..."
	wsprintf(szStatusText,LoadSz(IDS_LOADINGTEMPLATE,szSmallBuf,
		ARRAYSIZE(szSmallBuf)),szFilename);
	SetStatusText(szStatusText);

	uRet=ParseTemplateFile(hWnd,hFile,szFilename);
	
	SetStatusText(szStatusTextOld);
	CloseHandle(hFile);

	if (uRet != ERROR_SUCCESS)
		FreeRootTables();
	else {
		fInfLoaded = TRUE;
		dwAppState |= AS_CANHAVEDOCUMENT;
	}
	return uRet;
}

VOID UnloadTemplates(VOID)
{
	if (fInfLoaded) {
		FreeRootTables();
		fInfLoaded = FALSE;
		dwAppState &= ~AS_CANHAVEDOCUMENT;
	}
}	


VOID EnableMainMenuItems(HWND hwndApp,BOOL fEnable)
{
	HMENU hMenu = GetMenu(hwndApp);
	UINT uFlags = MF_BYCOMMAND | (fEnable ? MF_ENABLED : MF_DISABLED);

	EnableMenuItem(GetMenu(hwndMain),IDM_NEW,uFlags);
	EnableMenuItem(GetMenu(hwndMain),IDM_OPEN,uFlags);

}

/*******************************************************************

	NAME:		GetDefaultTemplateFilename

	SYNOPSIS:	Builds a default .adm path and filename to look for

	NOTES:		Fills szFilename with "<windows dir>\INF\ADMINCFG.ADF".
				Text is loaded from resources since file and directory
				names are localizable.

********************************************************************/
DWORD GetDefaultTemplateFilename(HWND hWnd,TCHAR * pszFilename,UINT cbFileName)
{
        TCHAR szFileName[MAX_PATH];
        UINT cchFileName;
        LPTSTR lpEnd;


        //
        // Load the common adm file
        //

        GetWindowsDirectory(szFileName,MAX_PATH);
        lstrcat(szFileName,szSlash);
        lstrcat(szFileName,LoadSz(IDS_INFDIR,szSmallBuf,ARRAYSIZE(szSmallBuf)));
        lstrcat(szFileName,szSlash);
        lstrcat(szFileName,LoadSz(IDS_DEF_COMMONNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));

        cchFileName = lstrlen (szFileName) + 1;

        if (cchFileName <= cbFileName) {
            lstrcpy (pszFilename, szFileName);
            lpEnd = pszFilename + lstrlen (pszFilename) + 1;
        } else {
            *pszFilename = TEXT('\0');
            return 0;
        }


        //
        // Add on the base adm filename
        //

        GetWindowsDirectory(szFileName,MAX_PATH);
        lstrcat(szFileName,szSlash);
        lstrcat(szFileName,LoadSz(IDS_INFDIR,szSmallBuf,ARRAYSIZE(szSmallBuf)));
        lstrcat(szFileName,szSlash);

        if (g_bWinnt) {
            lstrcat(szFileName,LoadSz(IDS_DEF_NT_INFNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));
        } else {
            lstrcat(szFileName,LoadSz(IDS_DEF_INFNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));
        }

        cchFileName += lstrlen (szFileName) + 1;

        if (cchFileName < cbFileName) {
            lstrcat (lpEnd, szFileName);
        }

        return (cchFileName);
}