Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
11 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. extern TCHAR szRemoteName[];
  7. BOOL OnOpen(HWND hwndApp,HWND hwndList)
  8. {
  9. OPENFILENAME ofn;
  10. TCHAR szFilter[SMALLBUF];
  11. TCHAR szOpenTitle[SMALLBUF];
  12. TCHAR szFilename[MAX_PATH+1]=TEXT("");
  13. TCHAR szDefExt[SMALLBUF];
  14. if (dwAppState & AS_FILEDIRTY) {
  15. if (!QueryForSave(hwndApp,hwndList)) return TRUE; // user cancelled
  16. }
  17. // have to load the openfile filter in 2 stages, because the string
  18. // contains a terminating character and LoadString won't load the
  19. // whole thing in one go
  20. memset(szFilter,0,ARRAYSIZE(szFilter) * sizeof(TCHAR));
  21. LoadSz(IDS_FILEFILTER1,szFilter,ARRAYSIZE(szFilter));
  22. LoadSz(IDS_FILEFILTER2,szFilter+lstrlen(szFilter)+1,ARRAYSIZE(szFilter)-
  23. (lstrlen(szFilter)-1));
  24. LoadSz(IDS_OPENTITLE,szOpenTitle,ARRAYSIZE(szOpenTitle));
  25. LoadSz(IDS_FILEFILTER2,szDefExt,ARRAYSIZE(szDefExt));
  26. memset(&ofn,0,sizeof(ofn));
  27. ofn.lStructSize = sizeof(ofn);
  28. ofn.hwndOwner = hwndApp;
  29. ofn.hInstance = ghInst;
  30. ofn.lpstrFilter = szFilter;
  31. ofn.lpstrFile = szFilename;
  32. ofn.nMaxFile = ARRAYSIZE(szFilename);
  33. ofn.lpstrTitle = szOpenTitle;
  34. ofn.lpstrDefExt = szDefExt;
  35. ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST
  36. | OFN_SHAREAWARE | OFN_HIDEREADONLY;
  37. if (!GetOpenFileName(&ofn)) return TRUE;
  38. return OnOpen_W(hwndApp,hwndList,szFilename);
  39. }
  40. BOOL OnOpen_W(HWND hwndApp,HWND hwndList,TCHAR * pszFilename)
  41. {
  42. BOOL fRet;
  43. TCHAR szStatusText[SMALLBUF+MAX_PATH+1];
  44. if (dwAppState & AS_FILELOADED) {
  45. // Free dirty file and free users
  46. RemoveAllUsers(hwndList);
  47. }
  48. // make status text a la "loading <filename>..."
  49. wsprintf(szStatusText,LoadSz(IDS_LOADING,szSmallBuf,ARRAYSIZE(szSmallBuf)),
  50. pszFilename);
  51. SetStatusText(szStatusText);
  52. dwAppState |= AS_POLICYFILE;
  53. fRet=LoadFile(pszFilename,hwndApp,hwndList,TRUE);
  54. SetStatusText(NULL);
  55. if (!fRet) {
  56. dwAppState &= ~AS_POLICYFILE;
  57. return FALSE;
  58. }
  59. lstrcpy(szDatFilename,pszFilename);
  60. dwAppState |= AS_FILELOADED | AS_FILEHASNAME | AS_POLICYFILE;
  61. dwAppState &= (~AS_CANOPENTEMPLATE & ~AS_LOCALREGISTRY & ~AS_REMOTEREGISTRY);
  62. EnableMenuItems(hwndApp,dwAppState);
  63. SetTitleBar(hwndApp,szDatFilename);
  64. ListView_SetItemState(hwndList,0,LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
  65. AddFileShortcut(GetSubMenu(GetMenu(hwndApp),0),pszFilename);
  66. SetStatusItemCount(hwndList);
  67. EnableWindow(hwndList, TRUE);
  68. SetFocus (hwndList);
  69. return TRUE;
  70. }
  71. BOOL OnSave(HWND hwndApp,HWND hwndList)
  72. {
  73. BOOL fRet;
  74. TCHAR szStatusText[SMALLBUF+MAX_PATH+1];
  75. TCHAR szStatusTextOld[255];
  76. if (!(dwAppState & AS_FILEHASNAME))
  77. return OnSaveAs(hwndApp,hwndList);
  78. GetStatusText(szStatusTextOld,ARRAYSIZE(szStatusTextOld));
  79. if (dwAppState & (AS_LOCALREGISTRY | AS_REMOTEREGISTRY)) {
  80. SetStatusText(LoadSz(IDS_SAVINGREGISTRY,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  81. fRet = SaveToRegistry(hwndApp,hwndList);
  82. } else {
  83. // make status text a la "saving <filename>..."
  84. wsprintf(szStatusText,LoadSz(IDS_SAVING,szSmallBuf,ARRAYSIZE(szSmallBuf)),
  85. szDatFilename);
  86. SetStatusText(szStatusText);
  87. fRet = SaveFile(szDatFilename,hwndApp,hwndList);
  88. }
  89. SetStatusText(szStatusTextOld);
  90. if (!fRet)
  91. return FALSE;
  92. dwAppState &= ~AS_FILEDIRTY;
  93. EnableMenuItems(hwndApp,dwAppState);
  94. if (szDatFilename[0] != TEXT('\0')) {
  95. SetTitleBar(hwndApp,szDatFilename);
  96. }
  97. return TRUE;
  98. }
  99. BOOL OnSaveAs(HWND hwndApp,HWND hwndList)
  100. {
  101. OPENFILENAME ofn;
  102. OFSTRUCT ofs;
  103. TCHAR szFilter[SMALLBUF];
  104. TCHAR szFilename[MAX_PATH+1]=TEXT("");
  105. TCHAR szStatusText[SMALLBUF+MAX_PATH+1];
  106. TCHAR szStatusTextOld[255];
  107. TCHAR szDefExt[SMALLBUF];
  108. UINT uRet;
  109. // should never get here if editing a registry (makes no sense
  110. // to "save as"), but check just for safety's sake
  111. if (dwAppState & (AS_LOCALREGISTRY | AS_REMOTEREGISTRY))
  112. return FALSE;
  113. // have to load the openfile filter in 2 stages, because the string
  114. // contains a terminating character and LoadString won't load the
  115. // whole thing in one go
  116. memset(szFilter,0,ARRAYSIZE(szFilter) * sizeof(TCHAR));
  117. LoadSz(IDS_FILEFILTER1,szFilter,ARRAYSIZE(szFilter));
  118. LoadSz(IDS_FILEFILTER2,szFilter+lstrlen(szFilter)+1,ARRAYSIZE(szFilter)-
  119. (lstrlen(szFilter)-1));
  120. LoadSz(IDS_FILEFILTER2,szDefExt,ARRAYSIZE(szDefExt));
  121. memset(&ofn,0,sizeof(ofn));
  122. ofn.lStructSize = sizeof(ofn);
  123. ofn.hwndOwner = hwndApp;
  124. ofn.hInstance = ghInst;
  125. ofn.lpstrFilter = szFilter;
  126. ofn.lpstrFile = szFilename;
  127. ofn.nMaxFile = ARRAYSIZE(szFilename);
  128. ofn.lpstrDefExt = szDefExt;
  129. ofn.lpstrTitle = NULL;
  130. ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT
  131. | OFN_SHAREAWARE | OFN_HIDEREADONLY;
  132. if (!GetSaveFileName(&ofn)) {
  133. return FALSE;
  134. }
  135. // delete the file if it already exists
  136. OpenFile(szFilename,&ofs,OF_DELETE);
  137. if ((uRet=CreateHiveFile(szFilename)) != ERROR_SUCCESS) {
  138. MsgBox(hwndApp,IDS_ErrREGERR_CANTSAVE,MB_OK,MB_ICONSTOP);
  139. return FALSE;
  140. }
  141. GetStatusText(szStatusTextOld,ARRAYSIZE(szStatusTextOld));
  142. // make status text a la "saving <filename>..."
  143. wsprintf(szStatusText,LoadSz(IDS_SAVING,szSmallBuf,ARRAYSIZE(szSmallBuf)),
  144. szDatFilename);
  145. SetStatusText(szStatusText);
  146. if (!SaveFile(szFilename,hwndApp,hwndList)) {
  147. return FALSE;
  148. }
  149. SetStatusText(szStatusTextOld);
  150. lstrcpy(szDatFilename,szFilename);
  151. dwAppState |= AS_FILEHASNAME;
  152. dwAppState &= ~AS_FILEDIRTY;
  153. EnableMenuItems(hwndApp,dwAppState);
  154. SetTitleBar(hwndApp,szDatFilename);
  155. AddFileShortcut(GetSubMenu(GetMenu(hwndApp),0),szFilename);
  156. return TRUE;
  157. }
  158. BOOL OnNew(HWND hwndApp,HWND hwndList)
  159. {
  160. if (dwAppState & AS_FILEDIRTY) {
  161. if (!QueryForSave(hwndApp,hwndList)) return TRUE; // user cancelled
  162. }
  163. if (dwAppState & AS_FILELOADED) {
  164. // Free dirty file and free users
  165. RemoveAllUsers(hwndList);
  166. }
  167. dwAppState |= AS_FILELOADED | AS_FILEDIRTY | AS_POLICYFILE;
  168. dwAppState &= (~AS_CANOPENTEMPLATE & ~AS_LOCALREGISTRY & ~AS_REMOTEREGISTRY
  169. & ~AS_FILEHASNAME);
  170. if (!AddDefaultUsers(hwndList)) {
  171. MsgBox(hwndApp,IDS_ErrOUTOFMEMORY,MB_ICONEXCLAMATION,MB_OK);
  172. dwAppState |= AS_CANOPENTEMPLATE;
  173. dwAppState &= (~AS_FILELOADED & ~AS_FILEDIRTY & ~AS_POLICYFILE);
  174. return FALSE;
  175. }
  176. EnableMenuItems(hwndApp,dwAppState);
  177. lstrcpy(szDatFilename,szNull);
  178. SetTitleBar(hwndApp,szDatFilename);
  179. ListView_SetItemState(hwndList, 0,LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
  180. SetStatusItemCount(hwndList);
  181. EnableWindow(hwndList, TRUE);
  182. SetFocus (hwndList);
  183. return TRUE;
  184. }
  185. BOOL OnOpenRegistry(HWND hwndApp,HWND hwndList,BOOL fDisplayErrors)
  186. {
  187. BOOL fRet;
  188. if (dwAppState & AS_FILEDIRTY) {
  189. if (!QueryForSave(hwndApp,hwndList)) return TRUE; // user cancelled
  190. }
  191. if (dwAppState & AS_FILELOADED) {
  192. // Free dirty file and free users
  193. RemoveAllUsers(hwndList);
  194. }
  195. dwAppState |= AS_LOCALREGISTRY;
  196. SetStatusText(LoadSz(IDS_READINGREGISTRY,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  197. fRet=LoadFromRegistry(hwndApp,hwndList,fDisplayErrors);
  198. SetStatusText(NULL);
  199. if (!fRet) {
  200. dwAppState &= ~AS_LOCALREGISTRY;
  201. return FALSE;
  202. }
  203. lstrcpy(szDatFilename,szNull);
  204. dwAppState |= AS_FILELOADED | AS_FILEHASNAME | AS_LOCALREGISTRY;
  205. dwAppState &= (~AS_CANOPENTEMPLATE & ~AS_POLICYFILE & ~AS_REMOTEREGISTRY);
  206. EnableMenuItems(hwndApp,dwAppState);
  207. SetTitleBar(hwndApp,NULL);
  208. ListView_SetItemState(hwndList,0,LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
  209. EnableWindow(hwndList, TRUE);
  210. SetFocus (hwndList);
  211. return TRUE;
  212. }
  213. BOOL OnClose(HWND hwndApp,HWND hwndList)
  214. {
  215. if (!(dwAppState & AS_FILELOADED))
  216. return FALSE;
  217. if (dwAppState & AS_FILEDIRTY) {
  218. if (!QueryForSave(hwndApp,hwndList)) return TRUE; // user cancelled
  219. }
  220. // disconnect if closing remote registry
  221. if (dwAppState & AS_REMOTEREGISTRY)
  222. OnDisconnect(hwndApp);
  223. RemoveAllUsers(hwndList);
  224. lstrcpy(szDatFilename,szNull);
  225. dwAppState &= (~AS_FILELOADED & ~AS_FILEHASNAME & ~AS_FILEDIRTY
  226. & ~AS_LOCALREGISTRY & ~AS_REMOTEREGISTRY & ~AS_POLICYFILE);
  227. dwAppState |= AS_CANOPENTEMPLATE;
  228. EnableMenuItems(hwndApp,dwAppState);
  229. SetTitleBar(hwndApp,NULL);
  230. SetStatusText(NULL);
  231. EnableWindow(hwndList, FALSE);
  232. return TRUE;
  233. }
  234. BOOL QueryForSave(HWND hwndApp,HWND hwndList)
  235. {
  236. TCHAR szTmpFilename[MAX_PATH+1];
  237. UINT uRet;
  238. if (dwAppState & AS_LOCALREGISTRY) {
  239. uRet = (UINT) MsgBox(hwndApp,IDS_QUERYSAVEREGISTRY,
  240. MB_ICONINFORMATION,MB_YESNOCANCEL);
  241. } else if (dwAppState & AS_REMOTEREGISTRY) {
  242. uRet = (UINT) MsgBoxParam(hwndApp,IDS_QUERYSAVEREMOTEREGISTRY,
  243. szRemoteName,MB_ICONINFORMATION,MB_YESNOCANCEL);
  244. } else {
  245. if (lstrlen(szDatFilename))
  246. lstrcpy(szTmpFilename,szDatFilename);
  247. else lstrcpy(szTmpFilename,LoadSz(IDS_UNTITLED,szSmallBuf,
  248. ARRAYSIZE(szSmallBuf)));
  249. uRet = (UINT) MsgBoxParam(hwndApp,IDS_QUERYSAVE,szTmpFilename,
  250. MB_ICONINFORMATION,MB_YESNOCANCEL);
  251. }
  252. switch (uRet) {
  253. case IDCANCEL:
  254. return FALSE;
  255. case IDNO:
  256. return TRUE;
  257. case IDYES:
  258. default:
  259. return OnSave(hwndApp,hwndList);
  260. }
  261. }
  262. UINT CreateHiveFile(TCHAR * pszFilename)
  263. {
  264. UINT uRet;
  265. HKEY hkeyHive=NULL,hkeyUsers=NULL,hkeyWorkstations=NULL;
  266. MyRegDeleteKey(HKEY_CURRENT_USER,TEXT("AdminConfigData"));
  267. if ( (uRet=RegCreateKey(HKEY_CURRENT_USER,TEXT("AdminConfigData"),&hkeyHive)) !=
  268. ERROR_SUCCESS ||
  269. (uRet=RegCreateKey(hkeyHive,szUSERS,&hkeyUsers)) !=
  270. ERROR_SUCCESS ||
  271. (uRet=RegCreateKey(hkeyHive,szWORKSTATIONS,&hkeyWorkstations)) !=
  272. ERROR_SUCCESS) {
  273. if (hkeyWorkstations) RegCloseKey(hkeyWorkstations);
  274. if (hkeyUsers) RegCloseKey(hkeyUsers);
  275. if (hkeyHive) RegCloseKey(hkeyHive);
  276. return uRet;
  277. }
  278. RegCloseKey(hkeyUsers);
  279. RegCloseKey(hkeyWorkstations);
  280. uRet=MyRegSaveKey(hkeyHive,pszFilename);
  281. RegCloseKey(hkeyHive);
  282. MyRegDeleteKey(HKEY_CURRENT_USER,TEXT("AdminConfigData"));
  283. if (uRet == ERROR_SUCCESS) {
  284. RegFlushKey(HKEY_CURRENT_USER);
  285. SetFileAttributes(pszFilename,FILE_ATTRIBUTE_ARCHIVE);
  286. }
  287. return uRet;
  288. }
  289. BOOL OnOpenTemplate(HWND hwndOwner,HWND hwndApp)
  290. {
  291. BOOL fRet;
  292. if (fRet=GetATemplateFile(hwndOwner)) {
  293. dwAppState |= AS_CANHAVEDOCUMENT;
  294. }
  295. EnableMenuItems(hwndApp,dwAppState);
  296. return fRet;
  297. }
  298. // adds the special prefixes "**del." and "**soft." if writing to a policy file,
  299. // and VF_DELETE/VF_SOFT flags are set
  300. VOID PrependValueName(TCHAR * pszValueName,DWORD dwFlags,TCHAR * pszNewValueName,
  301. UINT cbNewValueName)
  302. {
  303. UINT nValueNameLen = lstrlen(pszValueName);
  304. lstrcpy(pszNewValueName,szNull);
  305. if (cbNewValueName < nValueNameLen) // check length of buffer, just in case
  306. return;
  307. // prepend special prefixes for "delete" or "soft" values, if
  308. // we're writing to a policy file
  309. if ((dwAppState & AS_POLICYFILE) && !(dwCmdLineFlags & CLF_DIALOGMODE)) {
  310. if ((dwFlags & VF_DELETE) && (cbNewValueName > nValueNameLen +
  311. ARRAYSIZE(szDELETEPREFIX))) {
  312. lstrcpy(pszNewValueName,szDELETEPREFIX);
  313. } else if ((dwFlags & VF_SOFT) && (cbNewValueName > nValueNameLen +
  314. ARRAYSIZE(szSOFTPREFIX))) {
  315. lstrcpy(pszNewValueName,szSOFTPREFIX);
  316. }
  317. }
  318. lstrcat(pszNewValueName,pszValueName);
  319. }