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.

749 lines
23 KiB

  1. /*****************************************************************************
  2. *
  3. * account.cpp -
  4. *
  5. *
  6. *****************************************************************************/
  7. #include "priv.h"
  8. #include "account.h"
  9. #include "passwordapi.h"
  10. #include <richedit.h>
  11. #include <regapix.h>
  12. #include <commctrl.h>
  13. #define SZ_REGKEY_SEPARATOR TEXT("\\")
  14. // Server Level Login Attributes
  15. #define ATTRIB_NONE 0x00000000
  16. #define ATTRIB_LOGIN_ANONYMOUSLY 0x00000001
  17. #define ATTRIB_SAVE_USERNAME 0x00000002
  18. #define ATTRIB_SAVE_PASSWORD 0x00000004
  19. #define ATTRIB_DEFAULT (ATTRIB_LOGIN_ANONYMOUSLY | ATTRIB_SAVE_USERNAME)
  20. /*****************************************************************************\
  21. FUNCTION: _GetAccountKey
  22. DESCRIPTION:
  23. \*****************************************************************************/
  24. HRESULT CAccounts::_GetAccountKey(LPCTSTR pszServer, LPTSTR pszKey, DWORD cchKeySize)
  25. {
  26. HRESULT hr = S_OK;
  27. StrCpyN(pszKey, SZ_REGKEY_FTPFOLDER_ACCOUNTS, cchKeySize);
  28. StrCatBuff(pszKey, pszServer, cchKeySize);
  29. return hr;
  30. }
  31. /*****************************************************************************\
  32. FUNCTION: _GetUserAccountKey
  33. DESCRIPTION:
  34. \*****************************************************************************/
  35. HRESULT CAccounts::_GetUserAccountKey(LPCTSTR pszServer, LPCTSTR pszUserName, LPTSTR pszKey, DWORD cchKeySize)
  36. {
  37. TCHAR szUserNameEscaped[MAX_PATH];
  38. HRESULT hr = _GetAccountKey(pszServer, pszKey, cchKeySize);
  39. EscapeString(pszUserName, szUserNameEscaped, ARRAYSIZE(szUserNameEscaped));
  40. StrCatBuff(pszKey, SZ_REGKEY_SEPARATOR, cchKeySize);
  41. StrCatBuff(pszKey, szUserNameEscaped, cchKeySize);
  42. return hr;
  43. }
  44. /*****************************************************************************\
  45. FUNCTION: GetUserName
  46. DESCRIPTION:
  47. \*****************************************************************************/
  48. HRESULT CAccounts::GetUserName(LPCTSTR pszServer, LPTSTR pszUserName, DWORD cchUserName)
  49. {
  50. HRESULT hr = E_FAIL;
  51. TCHAR szKey[MAXIMUM_SUB_KEY_LENGTH];
  52. DWORD dwType = REG_SZ;
  53. DWORD cbSize = cchUserName * sizeof(TCHAR);
  54. hr = _GetAccountKey(pszServer, szKey, ARRAYSIZE(szKey));
  55. if (EVAL(SUCCEEDED(hr)))
  56. {
  57. if (ERROR_SUCCESS != SHGetValue(HKEY_CURRENT_USER, szKey, SZ_REGVALUE_DEFAULT_USER, &dwType, pszUserName, &cbSize))
  58. hr = E_FAIL;
  59. }
  60. return hr;
  61. }
  62. /*****************************************************************************\
  63. FUNCTION: _LoadLoginAttributes
  64. DESCRIPTION:
  65. \*****************************************************************************/
  66. HRESULT CAccounts::_LoadLoginAttributes(DWORD * pdwLoginAttribs)
  67. {
  68. HRESULT hr = E_FAIL;
  69. TCHAR szKey[MAXIMUM_SUB_KEY_LENGTH];
  70. DWORD dwType = REG_DWORD;
  71. DWORD cbSize = sizeof(*pdwLoginAttribs);
  72. // TODO: Walk the tree so these are read from the correct place.
  73. ASSERT(pdwLoginAttribs);
  74. hr = _GetAccountKey(m_pszServer, szKey, ARRAYSIZE(szKey));
  75. if (EVAL(SUCCEEDED(hr)))
  76. {
  77. // Do we also want to check on a per user basis?
  78. if ((ERROR_SUCCESS != SHGetValue(HKEY_CURRENT_USER, szKey, SZ_REGKEY_LOGIN_ATTRIBS, &dwType, pdwLoginAttribs, &cbSize)) ||
  79. (ERROR_SUCCESS != SHGetValue(HKEY_CURRENT_USER, SZ_REGKEY_FTPFOLDER_ACCOUNTS, SZ_REGKEY_LOGIN_ATTRIBS, &dwType, pdwLoginAttribs, &cbSize)))
  80. {
  81. hr = E_FAIL;
  82. }
  83. }
  84. return hr;
  85. }
  86. /*****************************************************************************\
  87. FUNCTION: _SaveLoginAttributes
  88. DESCRIPTION:
  89. \*****************************************************************************/
  90. HRESULT CAccounts::_SaveLoginAttributes(LPCTSTR pszServer, DWORD dwLoginAttribs)
  91. {
  92. HRESULT hr = E_FAIL;
  93. TCHAR szKey[MAXIMUM_SUB_KEY_LENGTH];
  94. // TODO: Walk the tree so these are saved to the correct place.
  95. hr = _GetAccountKey(pszServer, szKey, ARRAYSIZE(szKey));
  96. if (EVAL(SUCCEEDED(hr)))
  97. {
  98. if (!EVAL(ERROR_SUCCESS == SHSetValue(HKEY_CURRENT_USER, szKey, SZ_REGKEY_LOGIN_ATTRIBS, REG_DWORD, &dwLoginAttribs, sizeof(dwLoginAttribs))) ||
  99. !EVAL(ERROR_SUCCESS == SHSetValue(HKEY_CURRENT_USER, SZ_REGKEY_FTPFOLDER_ACCOUNTS, SZ_REGKEY_LOGIN_ATTRIBS, REG_DWORD, &dwLoginAttribs, sizeof(dwLoginAttribs))))
  100. {
  101. hr = E_FAIL;
  102. }
  103. }
  104. return hr;
  105. }
  106. /*****************************************************************************\
  107. FUNCTION: GetPassword
  108. DESCRIPTION:
  109. Update m_pszUser with pszUserName and get the password if we are allowed
  110. to. pszPassword is optional.
  111. \*****************************************************************************/
  112. HRESULT CAccounts::GetPassword(LPCTSTR pszServer, LPCTSTR pszUserName, LPTSTR pszPassword, DWORD cchPassword)
  113. {
  114. HRESULT hr = E_NOTIMPL;
  115. DWORD dwLogAttribs = 0;
  116. Str_SetPtr((LPTSTR *) &m_pszServer, pszServer);
  117. Str_SetPtr((LPTSTR *) &m_pszUser, pszUserName);
  118. _LoadLoginAttributes(&dwLogAttribs);
  119. hr = _LoadDefaultPassword((dwLogAttribs & ATTRIB_SAVE_PASSWORD));
  120. if (pszPassword)
  121. {
  122. pszPassword[0] = 0; // Incase this password isn't stored yet.
  123. if (SUCCEEDED(hr))
  124. StrCpyN(pszPassword, m_pszPassword, cchPassword);
  125. }
  126. return hr;
  127. }
  128. /*****************************************************************************\
  129. FUNCTION: _GetPassword
  130. DESCRIPTION:
  131. Always get a password even if persist is off.
  132. \*****************************************************************************/
  133. HRESULT CAccounts::_GetPassword(LPCTSTR pszServer, LPCTSTR pszUserName, LPTSTR pszPassword, DWORD cchPassword)
  134. {
  135. HRESULT hr = E_NOTIMPL;
  136. pszPassword[0] = 0; // Incase this password isn't stored yet.
  137. #ifdef FEATURE_SAVE_PASSWORD
  138. TCHAR wzKey[MAX_URL_STRING];
  139. wnsprintfW(wzKey, ARRAYSIZE(wzKey), L"ftp://%ls@%ls", pszUserName, pszServer);
  140. hr = GetCachedCredentials(wzKey, pszPassword, cchPassword);
  141. #endif // FEATURE_SAVE_PASSWORD
  142. return hr;
  143. }
  144. /*****************************************************************************\
  145. FUNCTION: _UserChangeSelect
  146. DESCRIPTION:
  147. \*****************************************************************************/
  148. HRESULT CAccounts::_UserChangeSelect(HWND hDlg, BOOL fSelectChange)
  149. {
  150. HRESULT hr = S_OK;
  151. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  152. HWND hwndComboBox = GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME);
  153. // SelectChange requires we get the text thru ComboBox_GetLBText because
  154. // it's not in GetWindowText yet. KILLFOCUS requires we get it from
  155. // GetWindowText because nothing is selected.
  156. szUser[0] = 0;
  157. if (fSelectChange)
  158. {
  159. if (ARRAYSIZE(szUser) > ComboBox_GetLBTextLen(hwndComboBox, ComboBox_GetCurSel(hwndComboBox)))
  160. ComboBox_GetLBText(hwndComboBox, ComboBox_GetCurSel(hwndComboBox), szUser);
  161. }
  162. else
  163. GetWindowText(hwndComboBox, szUser, ARRAYSIZE(szUser));
  164. if (szUser[0])
  165. {
  166. GetPassword(m_pszServer, szUser, NULL, 0);
  167. SetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG1), m_pszPassword);
  168. }
  169. return hr;
  170. }
  171. /*****************************************************************************\
  172. FUNCTION: _SaveUserName
  173. DESCRIPTION:
  174. \*****************************************************************************/
  175. HRESULT CAccounts::_SaveUserName(HWND hDlg)
  176. {
  177. HRESULT hr = S_OK;
  178. TCHAR szKey[MAXIMUM_SUB_KEY_LENGTH];
  179. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  180. GetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME), szUser, ARRAYSIZE(szUser));
  181. Str_SetPtr((LPTSTR *) &m_pszUser, szUser);
  182. // Always save the user name
  183. hr = _GetAccountKey(m_pszServer, szKey, ARRAYSIZE(szKey));
  184. if (EVAL(SUCCEEDED(hr)))
  185. {
  186. if (!EVAL(ERROR_SUCCESS == SHSetValue(HKEY_CURRENT_USER, szKey, SZ_REGVALUE_DEFAULT_USER, REG_SZ, szUser, (lstrlen(szUser) + 1) * sizeof(TCHAR))))
  187. hr = E_FAIL;
  188. hr = _GetUserAccountKey(m_pszServer, m_pszUser, szKey, ARRAYSIZE(szKey));
  189. if (EVAL(SUCCEEDED(hr)))
  190. SHSetValue(HKEY_CURRENT_USER, szKey, TEXT(""), REG_SZ, TEXT(""), sizeof(TEXT("")));
  191. }
  192. return hr;
  193. }
  194. /*****************************************************************************\
  195. FUNCTION: _SavePassword
  196. DESCRIPTION:
  197. \*****************************************************************************/
  198. HRESULT CAccounts::_SavePassword(HWND hDlg, LPCTSTR pszUser, BOOL fPersist)
  199. {
  200. HRESULT hr = S_OK;
  201. TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
  202. TCHAR wzKey[MAX_URL_STRING];
  203. GetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG1), szPassword, ARRAYSIZE(szPassword));
  204. Str_SetPtr((LPTSTR *) &m_pszPassword, szPassword);
  205. #ifdef FEATURE_SAVE_PASSWORD
  206. if (fPersist)
  207. {
  208. wnsprintfW(wzKey, ARRAYSIZE(wzKey), L"ftp://%ls@%ls", pszUser, m_pszServer);
  209. hr = SetCachedCredentials(wzKey, szPassword);
  210. }
  211. #endif // FEATURE_SAVE_PASSWORD
  212. return hr;
  213. }
  214. /*****************************************************************************\
  215. FUNCTION: _SetLoginType
  216. DESCRIPTION:
  217. \*****************************************************************************/
  218. HRESULT CAccounts::_SetLoginType(HWND hDlg, BOOL fLoginAnnonymously)
  219. {
  220. ////// The "Annonymous" section
  221. // Set the Radio Button
  222. CheckDlgButton(hDlg, IDC_LOGINDLG_ANONYMOUS_CBOX, (fLoginAnnonymously ? BST_CHECKED : BST_UNCHECKED));
  223. // Disable or Enable applicable items
  224. if (fLoginAnnonymously)
  225. {
  226. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME), SW_HIDE);
  227. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME_ANON), SW_SHOW);
  228. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG1), SW_HIDE);
  229. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2), SW_SHOW);
  230. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_LABEL_DLG1), SW_HIDE);
  231. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_LABEL_DLG2), SW_SHOW);
  232. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_NOTES_DLG1), SW_HIDE);
  233. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_NOTES_DLG2), SW_SHOW);
  234. // Hide "Save Password" in Anonymous mode.
  235. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_SAVE_PASSWORD), SW_HIDE);
  236. }
  237. else
  238. {
  239. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME), SW_SHOW);
  240. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME_ANON), SW_HIDE);
  241. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG1), SW_SHOW);
  242. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2), SW_HIDE);
  243. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_LABEL_DLG1), SW_SHOW);
  244. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_LABEL_DLG2), SW_HIDE);
  245. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_NOTES_DLG1), SW_SHOW);
  246. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_NOTES_DLG2), SW_HIDE);
  247. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_SAVE_PASSWORD), SW_SHOW);
  248. }
  249. if (fLoginAnnonymously) // Select all the text.
  250. {
  251. int iStart = 0;
  252. int iEnd = -1;
  253. SendMessage(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2), EM_GETSEL, (WPARAM) &iStart, (LPARAM) &iEnd);
  254. }
  255. SetFocus(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2));
  256. return S_OK;
  257. }
  258. /*****************************************************************************\
  259. FUNCTION: _PopulateUserNameDropDown
  260. DESCRIPTION:
  261. \*****************************************************************************/
  262. HRESULT CAccounts::_PopulateUserNameDropDown(HWND hDlg, LPCTSTR pszServer)
  263. {
  264. HRESULT hr = S_OK;
  265. HWND hwndUserComboBox = GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME);
  266. if (EVAL(hwndUserComboBox))
  267. {
  268. TCHAR szKey[MAXIMUM_SUB_KEY_LENGTH];
  269. TCHAR szDefaultUser[INTERNET_MAX_USER_NAME_LENGTH];
  270. szDefaultUser[0] = 0;
  271. hr = _GetAccountKey(pszServer, szKey, ARRAYSIZE(szKey));
  272. if (EVAL(SUCCEEDED(hr)))
  273. {
  274. HKEY hKey;
  275. SendMessage(hwndUserComboBox, CB_RESETCONTENT, 0, 0); // Empty the contents.
  276. if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER, szKey, &hKey))
  277. {
  278. TCHAR szUser[INTERNET_MAX_USER_NAME_LENGTH];
  279. DWORD dwIndex = 0;
  280. while (ERROR_SUCCESS == RegEnumKey(hKey, dwIndex++, szUser, ARRAYSIZE(szUser)))
  281. {
  282. UnEscapeString(NULL, szUser, ARRAYSIZE(szUser));
  283. SendMessage(hwndUserComboBox, CB_ADDSTRING, NULL, (LPARAM) szUser);
  284. }
  285. RegCloseKey(hKey);
  286. }
  287. SendMessage(hwndUserComboBox, CB_SETCURSEL, 0, 0);
  288. }
  289. if (!m_pszUser[0])
  290. GetUserName(pszServer, szDefaultUser, ARRAYSIZE(szDefaultUser));
  291. if (CB_ERR == SendMessage(hwndUserComboBox, CB_FINDSTRINGEXACT, 0, (LPARAM) (m_pszUser ? m_pszUser : szDefaultUser)))
  292. SendMessage(hwndUserComboBox, CB_ADDSTRING, NULL, (LPARAM) (m_pszUser ? m_pszUser : szDefaultUser));
  293. SetWindowText(hwndUserComboBox, (m_pszUser ? m_pszUser : szDefaultUser));
  294. }
  295. return hr;
  296. }
  297. /*****************************************************************************\
  298. FUNCTION: _LoadDefaultPassword
  299. DESCRIPTION:
  300. \*****************************************************************************/
  301. HRESULT CAccounts::_LoadDefaultPassword(BOOL fLoadPersisted)
  302. {
  303. TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
  304. HRESULT hr = S_FALSE;
  305. if (fLoadPersisted)
  306. hr = _GetPassword(m_pszServer, m_pszUser, szPassword, ARRAYSIZE(szPassword));
  307. else
  308. szPassword[0] = 0;
  309. Str_SetPtr((LPTSTR *) &m_pszPassword, szPassword);
  310. return hr;
  311. }
  312. /*****************************************************************************\
  313. FUNCTION: _LoadMessage
  314. DESCRIPTION:
  315. \*****************************************************************************/
  316. HRESULT CAccounts::_LoadMessage(HWND hDlg)
  317. {
  318. // if it's allowed, we need to load the anonymous email. This needs to be
  319. // be hard coded in English because that's how FTP works.
  320. SetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_USERNAME_ANON), TEXT("Anonymous"));
  321. if (LOGINFLAGS_ANON_LOGINJUSTFAILED & m_dwLoginFlags)
  322. {
  323. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_NORMAL), SW_HIDE);
  324. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_USERREJECT), SW_HIDE);
  325. }
  326. else if (LOGINFLAGS_USER_LOGINJUSTFAILED & m_dwLoginFlags)
  327. {
  328. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_ANONREJECT), SW_HIDE);
  329. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_NORMAL), SW_HIDE);
  330. }
  331. else
  332. {
  333. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_ANONREJECT), SW_HIDE);
  334. ShowWindow(GetDlgItem(hDlg, IDC_LOGINDLG_MESSAGE_USERREJECT), SW_HIDE);
  335. }
  336. return S_OK;
  337. }
  338. /*****************************************************************************\
  339. FUNCTION: _LoadEMailName
  340. DESCRIPTION:
  341. \*****************************************************************************/
  342. HRESULT CAccounts::_LoadEMailName(HWND hDlg)
  343. {
  344. TCHAR szEmailName[MAX_PATH];
  345. DWORD dwType = REG_SZ;
  346. DWORD cbSize = sizeof(szEmailName);
  347. if (ERROR_SUCCESS == SHGetValue(HKEY_CURRENT_USER, SZ_REGKEY_INTERNET_SETTINGS, SZ_REGKEY_EMAIL_NAME, &dwType, szEmailName, &cbSize))
  348. SetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2), szEmailName);
  349. return S_OK;
  350. }
  351. /*****************************************************************************\
  352. FUNCTION: _SaveEMailName
  353. DESCRIPTION:
  354. \*****************************************************************************/
  355. HRESULT CAccounts::_SaveEMailName(HWND hDlg)
  356. {
  357. HRESULT hr = E_FAIL;
  358. TCHAR szEmailName[MAX_PATH];
  359. if (GetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG2), szEmailName, ARRAYSIZE(szEmailName)))
  360. {
  361. if (ERROR_SUCCESS == SHSetValue(HKEY_CURRENT_USER, SZ_REGKEY_INTERNET_SETTINGS, SZ_REGKEY_EMAIL_NAME, REG_SZ, szEmailName, (lstrlen(szEmailName) + 1) * sizeof(TCHAR)))
  362. hr = S_OK;
  363. }
  364. return hr;
  365. }
  366. /*****************************************************************************\
  367. FUNCTION: _InitDialog
  368. DESCRIPTION:
  369. \*****************************************************************************/
  370. BOOL CAccounts::_InitDialog(HWND hDlg)
  371. {
  372. DWORD dwLogAttribs = ATTRIB_SAVE_USERNAME;
  373. BOOL fSucceeded = SetProp(hDlg, SZ_ACCOUNT_PROP, this);
  374. ASSERT(fSucceeded);
  375. // Init the dialog controls
  376. _LoadMessage(hDlg); // Load Message
  377. _LoadLoginAttributes(&dwLogAttribs);
  378. if (m_dwLoginFlags & LOGINFLAGS_ANON_ISDEFAULT) // Do we want to login anonymously?
  379. dwLogAttribs |= ATTRIB_LOGIN_ANONYMOUSLY; // Yes.
  380. CheckDlgButton(hDlg, IDC_LOGINDLG_ANONYMOUS_CBOX, (m_dwLoginFlags & LOGINFLAGS_ANON_ISDEFAULT));
  381. SetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_FTPSERVER), m_pszServer);
  382. _SetLoginType(hDlg, ATTRIB_LOGIN_ANONYMOUSLY & dwLogAttribs);
  383. _LoadEMailName(hDlg);
  384. _PopulateUserNameDropDown(hDlg, m_pszServer);
  385. _LoadDefaultPassword((dwLogAttribs & ATTRIB_SAVE_PASSWORD));
  386. SetWindowText(GetDlgItem(hDlg, IDC_LOGINDLG_PASSWORD_DLG1), m_pszPassword);
  387. #ifdef FEATURE_SAVE_PASSWORD
  388. if (S_OK == InitCredentialPersist())
  389. CheckDlgButton(hDlg, IDC_LOGINDLG_SAVE_PASSWORD, (dwLogAttribs & ATTRIB_SAVE_PASSWORD));
  390. else
  391. EnableWindow(GetDlgItem(hDlg, IDC_LOGINDLG_SAVE_PASSWORD), FALSE);
  392. #endif // FEATURE_SAVE_PASSWORD
  393. return TRUE;
  394. }
  395. /*****************************************************************************\
  396. FUNCTION: _SaveDialogData
  397. DESCRIPTION:
  398. \*****************************************************************************/
  399. BOOL CAccounts::_SaveDialogData(HWND hDlg)
  400. {
  401. DWORD dwLogAttribs = ATTRIB_NONE;
  402. if (IsDlgButtonChecked(hDlg, IDC_LOGINDLG_ANONYMOUS_CBOX))
  403. m_dwLoginFlags |= LOGINFLAGS_ANON_ISDEFAULT;
  404. else
  405. m_dwLoginFlags &= ~LOGINFLAGS_ANON_ISDEFAULT;
  406. // Alway save user name
  407. dwLogAttribs |= ATTRIB_SAVE_USERNAME;
  408. _SaveUserName(hDlg);
  409. #ifdef FEATURE_SAVE_PASSWORD
  410. if (IsDlgButtonChecked(hDlg, IDC_LOGINDLG_SAVE_PASSWORD))
  411. dwLogAttribs |= ATTRIB_SAVE_PASSWORD;
  412. if (!(m_dwLoginFlags & LOGINFLAGS_ANON_ISDEFAULT))
  413. _SavePassword(hDlg, m_pszUser, (dwLogAttribs & ATTRIB_SAVE_PASSWORD));
  414. #endif // FEATURE_SAVE_PASSWORD
  415. _SaveLoginAttributes(m_pszServer, dwLogAttribs);
  416. // Init the dialog controls
  417. _SaveEMailName(hDlg);
  418. return TRUE;
  419. }
  420. LRESULT CAccounts::_OnCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
  421. {
  422. LRESULT lResult = 0;
  423. UINT idc = GET_WM_COMMAND_ID(wParam, lParam);
  424. switch (idc)
  425. {
  426. case IDOK:
  427. if (m_hDlg == hDlg) // (IDOK)
  428. {
  429. _SaveDialogData(hDlg);
  430. EndDialog(hDlg, TRUE);
  431. lResult = 1;
  432. }
  433. break;
  434. case IDCANCEL:
  435. EndDialog(hDlg, FALSE);
  436. lResult = 1;
  437. break;
  438. case IDC_LOGINDLG_ANONYMOUS_CBOX:
  439. _SetLoginType(hDlg, IsDlgButtonChecked(hDlg, IDC_LOGINDLG_ANONYMOUS_CBOX));
  440. lResult = 1;
  441. break;
  442. case IDC_LOGINDLG_USERNAME:
  443. {
  444. UINT uCmd = GET_WM_COMMAND_CMD(wParam, lParam);
  445. switch (uCmd)
  446. {
  447. case CBN_SELCHANGE:
  448. case CBN_KILLFOCUS:
  449. _UserChangeSelect(hDlg, (CBN_SELCHANGE == uCmd));
  450. lResult = 1;
  451. break;
  452. }
  453. }
  454. break;
  455. }
  456. return lResult;
  457. }
  458. LRESULT CAccounts::_OnNotify(HWND hDlg, NMHDR * pNMHdr, int idControl)
  459. {
  460. BOOL fHandled = 1; // Not handled (WM_COMMAND seems to be different)
  461. if (pNMHdr)
  462. {
  463. switch (pNMHdr->code)
  464. {
  465. case NM_RETURN:
  466. case NM_CLICK:
  467. {
  468. PNMLINK pNMLink = (PNMLINK) pNMHdr;
  469. if (!StrCmpW(pNMLink->item.szID, L"idWebDAV"))
  470. {
  471. TCHAR szCommand[MAX_PATH];
  472. LoadString(HINST_THISDLL, IDS_LOGIN_WEBDAVLINK, szCommand, ARRAYSIZE(szCommand));
  473. HrShellExecute(hDlg, NULL, szCommand, NULL, NULL, SW_NORMAL);
  474. }
  475. break;
  476. }
  477. };
  478. }
  479. return fHandled;
  480. }
  481. /*****************************************************************************\
  482. FUNCTION: _LoginDialogProc
  483. DESCRIPTION:
  484. \*****************************************************************************/
  485. INT_PTR CALLBACK CAccounts::_LoginDialogProc(HWND hDlg, UINT wm, WPARAM wParam, LPARAM lParam)
  486. {
  487. LRESULT lResult = 0;
  488. switch (wm)
  489. {
  490. case WM_INITDIALOG:
  491. {
  492. CAccounts * pThis = (CAccounts *) lParam;
  493. pThis->m_hDlg = hDlg;
  494. lResult = pThis->_InitDialog(hDlg);
  495. break;
  496. }
  497. case WM_COMMAND:
  498. {
  499. CAccounts * pThis = (CAccounts *)GetProp(hDlg, SZ_ACCOUNT_PROP);
  500. if (EVAL(pThis))
  501. lResult = pThis->_OnCommand(hDlg, wParam, lParam);
  502. break;
  503. }
  504. case WM_NOTIFY:
  505. {
  506. CAccounts * pThis = (CAccounts *)GetProp(hDlg, SZ_ACCOUNT_PROP);
  507. if (pThis)
  508. {
  509. lResult = pThis->_OnNotify(hDlg, (NMHDR *)lParam, (int) wParam);
  510. }
  511. break;
  512. }
  513. };
  514. return lResult;
  515. }
  516. /*****************************************************************************\
  517. FUNCTION: GetAccountUrl
  518. DESCRIPTION:
  519. \*****************************************************************************/
  520. HRESULT CAccounts::DisplayLoginDialog(HWND hwnd, DWORD dwLoginFlags, LPCTSTR pszServer, LPTSTR pszUserName, DWORD cchUserNameSize, LPTSTR pszPassword, DWORD cchPasswordSize)
  521. {
  522. HRESULT hr = S_OK;
  523. HRESULT hrOle = SHCoInitialize();
  524. INITCOMMONCONTROLSEX icex = {0};
  525. icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  526. icex.dwICC = (ICC_USEREX_CLASSES | ICC_STANDARD_CLASSES | ICC_LINK_CLASS);
  527. InitCommonControlsEx(&icex); // Init the Link control for the login dialog
  528. ASSERT(hwnd && pszServer[0]);
  529. if (TEXT('\0') == pszUserName[0])
  530. hr = GetUserName(pszServer, pszUserName, cchUserNameSize);
  531. Str_SetPtr((LPTSTR *) &m_pszServer, pszServer);
  532. Str_SetPtr((LPTSTR *) &m_pszUser, pszUserName);
  533. Str_SetPtr((LPTSTR *) &m_pszPassword, pszPassword);
  534. m_dwLoginFlags = dwLoginFlags;
  535. if (DialogBoxParam(HINST_THISDLL, MAKEINTRESOURCE(IDD_LOGINDLG), hwnd, _LoginDialogProc, (LPARAM)this))
  536. {
  537. StrCpyN(pszUserName, ((m_dwLoginFlags & LOGINFLAGS_ANON_ISDEFAULT) ? TEXT("") : m_pszUser), cchUserNameSize);
  538. StrCpyN(pszPassword, ((m_dwLoginFlags & LOGINFLAGS_ANON_ISDEFAULT) ? TEXT("") : m_pszPassword), cchPasswordSize);
  539. hr = S_OK;
  540. }
  541. else
  542. hr = S_FALSE;
  543. if (SUCCEEDED(hrOle))
  544. {
  545. SHCoUninitialize(hrOle);
  546. }
  547. return hr;
  548. }
  549. /****************************************************\
  550. Constructor
  551. \****************************************************/
  552. CAccounts::CAccounts()
  553. {
  554. DllAddRef();
  555. // NOTE: We may be put on the stack, so we will not
  556. // automatically have our member variables inited.
  557. m_pszServer = 0;
  558. m_pszUser = 0;
  559. m_pszPassword = 0;
  560. LEAK_ADDREF(LEAK_CAccount);
  561. }
  562. /****************************************************\
  563. Destructor
  564. \****************************************************/
  565. CAccounts::~CAccounts()
  566. {
  567. Str_SetPtr((LPTSTR *) &m_pszServer, NULL);
  568. Str_SetPtr((LPTSTR *) &m_pszUser, NULL);
  569. Str_SetPtr((LPTSTR *) &m_pszPassword, NULL);
  570. DllRelease();
  571. LEAK_DELREF(LEAK_CAccount);
  572. }