Leaked source code of windows server 2003
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.

399 lines
11 KiB

  1. // mimemap1.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "afxcmn.h"
  5. #include "ISAdmin.h"
  6. #include "mimemap1.h"
  7. #include "addmime.h"
  8. #include "delmime.h"
  9. #include "editmime.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // MIMEMAP1 property page
  16. IMPLEMENT_DYNCREATE(MIMEMAP1, CGenPage)
  17. MIMEMAP1::MIMEMAP1() : CGenPage(MIMEMAP1::IDD)
  18. {
  19. //{{AFX_DATA_INIT(MIMEMAP1)
  20. // NOTE: the ClassWizard will add member initialization here
  21. //}}AFX_DATA_INIT
  22. m_rkMimeKey = NULL;
  23. m_pmeMimeMapList = NULL;
  24. }
  25. MIMEMAP1::~MIMEMAP1()
  26. {
  27. if (m_rkMimeKey != NULL)
  28. delete(m_rkMimeKey);
  29. DeleteMimeList();
  30. }
  31. void MIMEMAP1::DoDataExchange(CDataExchange* pDX)
  32. {
  33. CGenPage::DoDataExchange(pDX);
  34. //{{AFX_DATA_MAP(MIMEMAP1)
  35. DDX_Control(pDX, IDC_MIMEMAPLIST1, m_lboxMimeMapList);
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(MIMEMAP1, CGenPage)
  39. //{{AFX_MSG_MAP(MIMEMAP1)
  40. ON_BN_CLICKED(IDC_MIMEMAPADDBUTTON, OnMimemapaddbutton)
  41. ON_BN_CLICKED(IDC_MIMEMAPREMOVEBUTTON, OnMimemapremovebutton)
  42. ON_BN_CLICKED(IDC_MIMEMAPEDITBUTTON, OnMimemapeditbutton)
  43. ON_LBN_DBLCLK(IDC_MIMEMAPLIST1, OnDblclkMimemaplist1)
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. /////////////////////////////////////////////////////////////////////////////
  47. // MIMEMAP1 message handlers
  48. BOOL MIMEMAP1::OnInitDialog()
  49. {
  50. CGenPage::OnInitDialog();
  51. /*
  52. CMimeMap mimeTestMime(_T("mimetype,fileextension,imagefile,g"));
  53. CMimeMap *pmimeTestMimePtr;
  54. */
  55. CString strNextValue;
  56. BOOL bAllocationError = FALSE;
  57. int lpiTabStops[2];
  58. CRegValueIter *rviMimeKeys;
  59. DWORD err, ulRegType;
  60. lpiTabStops[0] = 58;
  61. lpiTabStops[1] = 191;
  62. m_ulMimeIndex = 0;
  63. m_lboxMimeMapList.SetTabStops(2,lpiTabStops);
  64. m_bMimeEntriesExist = FALSE;
  65. m_rkMimeKey = new CRegKey(*m_rkMainKey,_T("MimeMap"),REGISTRY_ACCESS_RIGHTS);
  66. // Anything under this key should be a mime mapping.
  67. // No way to verify that, but non-string entries are invalid
  68. // so ignore them
  69. if (m_rkMimeKey != NULL) {
  70. if (*m_rkMimeKey != NULL) {
  71. if (rviMimeKeys = new CRegValueIter(*m_rkMimeKey)) {
  72. while ((err = rviMimeKeys->Next(&strNextValue, &ulRegType)) == ERROR_SUCCESS) {
  73. if (ulRegType == REG_SZ) {
  74. if (!AddMimeEntry(strNextValue))
  75. bAllocationError = TRUE;
  76. }
  77. }
  78. delete (rviMimeKeys);
  79. }
  80. m_bMimeEntriesExist = TRUE;
  81. }
  82. }
  83. if (!m_bMimeEntriesExist) { //Can't open registry key
  84. CString strNoMimeEntriesMsg;
  85. strNoMimeEntriesMsg.LoadString(IDS_MIMENOMIMEENTRIESMSG);
  86. AfxMessageBox(strNoMimeEntriesMsg);
  87. }
  88. if (bAllocationError) { //Error adding one or more entries
  89. CString strAllocFailMsg;
  90. strAllocFailMsg.LoadString(IDS_MIMEENTRIESALLOCFAILMSG);
  91. AfxMessageBox(strAllocFailMsg);
  92. }
  93. /*
  94. strTestString = _T("mimetype,fileextension,,g");
  95. pmimeTestMimePtr = new CMimeMap(strTestString);
  96. strTestString = mimeTestMime;
  97. */
  98. // TODO: Add extra initialization here
  99. return TRUE; // return TRUE unless you set the focus to a control
  100. // EXCEPTION: OCX Property Pages should return FALSE
  101. }
  102. void MIMEMAP1::OnMimemapaddbutton()
  103. {
  104. // TODO: Add your control notification handler code here
  105. if (m_bMimeEntriesExist) {
  106. CAddMime addmimeGetInfo(this);
  107. if (addmimeGetInfo.DoModal() == IDOK) {
  108. if (AddMimeEntry(addmimeGetInfo.GetFileExtension(), addmimeGetInfo.GetMimeType(), addmimeGetInfo.GetImageFile(),
  109. addmimeGetInfo.GetGopherType())) {
  110. m_bIsDirty = TRUE;
  111. SetModified(TRUE);
  112. }
  113. else {
  114. CString strAllocFailMsg;
  115. strAllocFailMsg.LoadString(IDS_MIMEENTRYALLOCFAILMSG);
  116. AfxMessageBox(strAllocFailMsg);
  117. }
  118. }
  119. }
  120. else {
  121. CString strNoMimeEntriesMsg;
  122. strNoMimeEntriesMsg.LoadString(IDS_MIMENOMIMEENTRIESMSG);
  123. AfxMessageBox(strNoMimeEntriesMsg);
  124. }
  125. }
  126. void MIMEMAP1::OnMimemapremovebutton()
  127. {
  128. // TODO: Add your control notification handler code here
  129. if (m_bMimeEntriesExist) {
  130. int iCurSel;
  131. CDelMime delmimeGetInfo(this);
  132. if ((iCurSel = m_lboxMimeMapList.GetCurSel()) != LB_ERR) {
  133. if (delmimeGetInfo.DoModal() == IDOK) {
  134. DeleteMimeMapping(iCurSel);
  135. m_bIsDirty = TRUE;
  136. SetModified(TRUE);
  137. }
  138. }
  139. else {
  140. CString strNoHighlightMsg;
  141. strNoHighlightMsg.LoadString(IDS_NOHIGHLIGHTMSG);
  142. AfxMessageBox(strNoHighlightMsg);
  143. }
  144. }
  145. else {
  146. CString strNoMimeEntriesMsg;
  147. strNoMimeEntriesMsg.LoadString(IDS_MIMENOMIMEENTRIESMSG);
  148. AfxMessageBox(strNoMimeEntriesMsg);
  149. }
  150. }
  151. void MIMEMAP1::OnMimemapeditbutton()
  152. {
  153. // TODO: Add your control notification handler code here
  154. if (m_bMimeEntriesExist) {
  155. int iCurSel;
  156. PMIME_ENTRY pmeEditEntry;
  157. if ((iCurSel = m_lboxMimeMapList.GetCurSel()) != LB_ERR) {
  158. for (pmeEditEntry = m_pmeMimeMapList;(pmeEditEntry != NULL) &&
  159. (m_lboxMimeMapList.GetItemData(iCurSel) != pmeEditEntry->iListIndex);
  160. pmeEditEntry = pmeEditEntry->NextPtr)
  161. ;
  162. ASSERT (pmeEditEntry != NULL);
  163. CEditMime editmimeGetInfo(this,
  164. pmeEditEntry->mimeData->GetFileExtension(),
  165. pmeEditEntry->mimeData->GetMimeType(),
  166. pmeEditEntry->mimeData->GetImageFile(),
  167. pmeEditEntry->mimeData->GetGopherType());
  168. if (editmimeGetInfo.DoModal() == IDOK) {
  169. if (EditMimeMapping(iCurSel,
  170. pmeEditEntry,
  171. editmimeGetInfo.GetFileExtension(),
  172. editmimeGetInfo.GetMimeType(),
  173. editmimeGetInfo.GetImageFile(),
  174. editmimeGetInfo.GetGopherType() )) {
  175. m_bIsDirty = TRUE;
  176. SetModified(TRUE);
  177. }
  178. else {
  179. CString strEditErrorMsg;
  180. strEditErrorMsg.LoadString(IDS_MIMEEDITERRORMSG);
  181. AfxMessageBox(strEditErrorMsg);
  182. }
  183. }
  184. }
  185. else {
  186. CString strNoHighlightMsg;
  187. strNoHighlightMsg.LoadString(IDS_NOHIGHLIGHTMSG);
  188. AfxMessageBox(strNoHighlightMsg);
  189. }
  190. }
  191. else {
  192. CString strNoMimeEntriesMsg;
  193. strNoMimeEntriesMsg.LoadString(IDS_MIMENOMIMEENTRIESMSG);
  194. AfxMessageBox(strNoMimeEntriesMsg);
  195. }
  196. }
  197. void MIMEMAP1::OnDblclkMimemaplist1()
  198. {
  199. // TODO: Add your control notification handler code here
  200. OnMimemapeditbutton();
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////
  203. // Other Functions
  204. void MIMEMAP1::SaveInfo()
  205. {
  206. PMIME_ENTRY pmeSaveEntry;
  207. CString strDummyValue(_T(""));
  208. if (m_bIsDirty) {
  209. for (pmeSaveEntry = m_pmeMimeMapList;(pmeSaveEntry != NULL); pmeSaveEntry = pmeSaveEntry->NextPtr) {
  210. if (pmeSaveEntry->DeleteCurrent) {
  211. m_rkMimeKey->DeleteValue(pmeSaveEntry->mimeData->GetPrevMimeMap());
  212. pmeSaveEntry->DeleteCurrent = FALSE;
  213. }
  214. if (pmeSaveEntry->WriteNew) {
  215. m_rkMimeKey->SetValue(*(pmeSaveEntry->mimeData), strDummyValue);
  216. pmeSaveEntry->mimeData->SetPrevMimeMap();
  217. pmeSaveEntry->WriteNew = FALSE;
  218. }
  219. }
  220. }
  221. CGenPage::SaveInfo();
  222. }
  223. //This version is called for existing entries
  224. BOOL MIMEMAP1::AddMimeEntry(CString &strNewMimeMap)
  225. {
  226. PMIME_ENTRY pmeNewEntry;
  227. int iCurSel;
  228. BOOL bretcode = FALSE;
  229. if ((pmeNewEntry = new MIME_ENTRY) != NULL) {
  230. if ((pmeNewEntry->mimeData = new CMimeMap(strNewMimeMap)) != NULL) {
  231. iCurSel = m_lboxMimeMapList.AddString(pmeNewEntry->mimeData->GetDisplayString());
  232. if ((iCurSel != LB_ERR) && (iCurSel != LB_ERRSPACE)) {
  233. pmeNewEntry->DeleteCurrent = FALSE;
  234. pmeNewEntry->WriteNew = FALSE;
  235. m_lboxMimeMapList.SetItemData(iCurSel,m_ulMimeIndex);
  236. pmeNewEntry->iListIndex = m_ulMimeIndex++;
  237. pmeNewEntry->NextPtr = m_pmeMimeMapList;
  238. m_pmeMimeMapList = pmeNewEntry;
  239. bretcode = TRUE;
  240. }
  241. else {
  242. delete (pmeNewEntry->mimeData);
  243. delete (pmeNewEntry);
  244. }
  245. }
  246. else
  247. delete (pmeNewEntry);
  248. }
  249. return (bretcode);
  250. }
  251. // This version is called for new entries so set the write flag.
  252. BOOL MIMEMAP1::AddMimeEntry(LPCTSTR pchFileExtension, LPCTSTR pchMimeType, LPCTSTR pchImageFile, LPCTSTR pchGoperType)
  253. {
  254. PMIME_ENTRY pmeNewEntry;
  255. int iCurSel;
  256. BOOL bretcode = FALSE;
  257. if ((pmeNewEntry = new MIME_ENTRY) != NULL) {
  258. if ((pmeNewEntry->mimeData = new CMimeMap(pchFileExtension, pchMimeType, pchImageFile, pchGoperType)) != NULL) {
  259. iCurSel = m_lboxMimeMapList.AddString(pmeNewEntry->mimeData->GetDisplayString());
  260. if ((iCurSel != LB_ERR) && (iCurSel != LB_ERRSPACE)) {
  261. pmeNewEntry->DeleteCurrent = FALSE;
  262. pmeNewEntry->WriteNew = TRUE;
  263. m_lboxMimeMapList.SetItemData(iCurSel,m_ulMimeIndex);
  264. m_lboxMimeMapList.SetCurSel(iCurSel);
  265. pmeNewEntry->iListIndex = m_ulMimeIndex++;
  266. pmeNewEntry->NextPtr = m_pmeMimeMapList;
  267. m_pmeMimeMapList = pmeNewEntry;
  268. bretcode = TRUE;
  269. }
  270. else {
  271. delete (pmeNewEntry->mimeData);
  272. delete (pmeNewEntry);
  273. }
  274. }
  275. else
  276. delete (pmeNewEntry);
  277. }
  278. return (bretcode);
  279. }
  280. void MIMEMAP1::DeleteMimeList()
  281. {
  282. PMIME_ENTRY pmeCurEntry;
  283. while (m_pmeMimeMapList != NULL) {
  284. delete (m_pmeMimeMapList->mimeData);
  285. pmeCurEntry = m_pmeMimeMapList;
  286. m_pmeMimeMapList = m_pmeMimeMapList->NextPtr;
  287. delete (pmeCurEntry);
  288. }
  289. }
  290. void MIMEMAP1::DeleteMimeMapping(int iCurSel)
  291. {
  292. PMIME_ENTRY pmeDelEntry;
  293. for (pmeDelEntry = m_pmeMimeMapList;(pmeDelEntry != NULL) &&
  294. (m_lboxMimeMapList.GetItemData(iCurSel) != pmeDelEntry->iListIndex);
  295. pmeDelEntry = pmeDelEntry->NextPtr)
  296. ;
  297. ASSERT (pmeDelEntry != NULL);
  298. if (pmeDelEntry->mimeData->PrevMimeMapExists())
  299. pmeDelEntry->DeleteCurrent = TRUE;
  300. pmeDelEntry->WriteNew = FALSE;
  301. m_lboxMimeMapList.DeleteString(iCurSel);
  302. }
  303. BOOL MIMEMAP1::EditMimeMapping(int iCurSel,
  304. PMIME_ENTRY pmeEditEntry,
  305. LPCTSTR pchFileExtension,
  306. LPCTSTR pchMimeType,
  307. LPCTSTR pchImageFile,
  308. LPCTSTR pchGopherType)
  309. {
  310. BOOL bretcode = FALSE;
  311. pmeEditEntry->mimeData->SetFileExtension(pchFileExtension);
  312. pmeEditEntry->mimeData->SetMimeType(pchMimeType);
  313. pmeEditEntry->mimeData->SetImageFile(pchImageFile);
  314. pmeEditEntry->mimeData->SetGopherType(pchGopherType);
  315. m_lboxMimeMapList.DeleteString(iCurSel); // Delete first so memory is freed
  316. iCurSel = m_lboxMimeMapList.AddString(pmeEditEntry->mimeData->GetDisplayString());
  317. // There error case on this is incredibly rare, so don't bother saving and restoring the above fields
  318. // Just don't set flags so registry is not updated.
  319. if ((iCurSel != LB_ERR) && (iCurSel != LB_ERRSPACE)) {
  320. m_lboxMimeMapList.SetItemData(iCurSel,pmeEditEntry->iListIndex);
  321. if (pmeEditEntry->mimeData->PrevMimeMapExists())
  322. pmeEditEntry->DeleteCurrent = TRUE;
  323. pmeEditEntry->WriteNew = TRUE;
  324. bretcode = TRUE;
  325. }
  326. return (bretcode);
  327. }