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.

435 lines
9.6 KiB

  1. /****************************************************************************
  2. *
  3. * Microsoft Confidential
  4. * Copyright (c) Microsoft Corporation 1994
  5. * All rights reserved
  6. *
  7. ***************************************************************************/
  8. #ifdef UNIX_FEATURE_ALIAS
  9. #undef UNICODE
  10. #include "inetcplp.h"
  11. #include "shalias.h"
  12. #include "mluisupp.h"
  13. static TCHAR g_szAliasKey[] = TEXT("Software\\Microsoft\\Internet Explorer\\Unix\\Alias");
  14. // Member function definitions for CAlias
  15. CAlias::CAlias( TCHAR * name )
  16. {
  17. m_alias = (TCHAR *) LocalAlloc( LPTR, (lstrlen(name) + 1)*sizeof(TCHAR) );
  18. StrCpy( m_alias, name );
  19. m_szurl = NULL;
  20. m_fDirty= TRUE;
  21. }
  22. CAlias::~CAlias()
  23. {
  24. if( m_alias ) LocalFree( m_alias );
  25. if( m_szurl ) LocalFree( m_szurl );
  26. }
  27. CAlias::Load()
  28. {
  29. if(m_alias)
  30. {
  31. HKEY hKey;
  32. TCHAR aliasKey[MAX_PATH], buffer[MAX_PATH];
  33. StrCpy( aliasKey, g_szAliasKey);
  34. StrCat( aliasKey, TEXT("\\"));
  35. StrCat( aliasKey, m_alias );
  36. HRESULT lResult = RegOpenKeyExA(
  37. HKEY_CURRENT_USER,
  38. aliasKey,
  39. 0,
  40. KEY_QUERY_VALUE | KEY_READ,
  41. &hKey);
  42. if( lResult == ERROR_SUCCESS )
  43. {
  44. DWORD dwLen = MAX_PATH;
  45. if (RegQueryValue( hKey, NULL, buffer, (long *)&dwLen )
  46. == ERROR_SUCCESS )
  47. {
  48. m_szurl = (TCHAR *)LocalAlloc( LPTR, (dwLen+1)*sizeof(TCHAR));
  49. StrCpy(m_szurl, buffer);
  50. }
  51. RegCloseKey( hKey );
  52. }
  53. else
  54. return FALSE;
  55. }
  56. return TRUE;
  57. }
  58. CAlias::Save()
  59. {
  60. HRESULT lResult;
  61. HKEY hKey;
  62. TCHAR aliasKey[MAX_PATH], buffer[MAX_PATH];
  63. StrCpy( aliasKey, g_szAliasKey);
  64. StrCat( aliasKey, TEXT("\\"));
  65. StrCat( aliasKey, m_alias );
  66. lResult = RegOpenKeyEx(
  67. HKEY_CURRENT_USER,
  68. aliasKey,
  69. 0,
  70. KEY_QUERY_VALUE| KEY_WRITE,
  71. &hKey);
  72. if( lResult != ERROR_SUCCESS )
  73. {
  74. lResult = RegCreateKey(
  75. HKEY_CURRENT_USER,
  76. aliasKey,
  77. &hKey);
  78. }
  79. if( lResult == ERROR_SUCCESS )
  80. {
  81. DWORD dwType = REG_SZ;
  82. DWORD dwLen = (lstrlen(m_szurl)+1)*sizeof(TCHAR);
  83. RegSetValue( hKey, NULL, dwType, m_szurl, dwLen );
  84. RegCloseKey( hKey);
  85. }
  86. else
  87. return FALSE;
  88. m_fDirty = FALSE;
  89. return TRUE;
  90. }
  91. CAlias::Delete()
  92. {
  93. TCHAR aliasKey[MAX_PATH], buffer[MAX_PATH];
  94. StrCpy( aliasKey, g_szAliasKey);
  95. StrCat( aliasKey, TEXT("\\"));
  96. StrCat( aliasKey, m_alias );
  97. RegDeleteKey( HKEY_CURRENT_USER, aliasKey );
  98. return TRUE;
  99. }
  100. #ifdef DEBUG
  101. CAlias::Print()
  102. {
  103. if( m_alias ) printf( m_alias );
  104. printf(",");
  105. if( m_szurl ) printf( m_szurl );
  106. printf("\n");
  107. return TRUE;
  108. }
  109. #endif
  110. STDAPI_(BOOL) FreeAliases( HDPA aliasListIn )
  111. {
  112. if(aliasListIn)
  113. {
  114. int aliasCount = DPA_GetPtrCount( aliasListIn );
  115. for(int i=0; i<aliasCount; i++ )
  116. {
  117. CAlias * ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, i );
  118. if(ptr) delete ptr;
  119. }
  120. return TRUE;
  121. }
  122. return FALSE;
  123. }
  124. STDAPI_(BOOL) AddAliasToList( HDPA aliasListIn, LPTSTR aliasIn, LPTSTR szurl, HWND hwnd )
  125. {
  126. int index;
  127. CAlias * ptr;
  128. TCHAR alias[ MAX_ALIAS_LENGTH + 1 ];
  129. TCHAR achTemp[MAX_ALIAS_LENGTH], achTemp2[MAX_ALIAS_LENGTH];
  130. if (aliasListIn && aliasIn && lstrlen(aliasIn)<MAX_ALIAS_LENGTH)
  131. {
  132. StrCpy( alias, aliasIn );
  133. EatSpaces( alias );
  134. if((index = FindAliasIndex( aliasListIn, alias )) != -1)
  135. {
  136. if( !hwnd )
  137. return FALSE;
  138. MLLoadShellLangString(IDS_ERROR_ALIAS_ALREADY_EXISTS,
  139. achTemp, sizeof(achTemp));
  140. MLLoadShellLangString(IDS_TITLE_ALIASADD,
  141. achTemp2, sizeof(achTemp2));
  142. if(hwnd && MessageBox(hwnd, achTemp, achTemp2 , MB_YESNO|MB_ICONQUESTION) != IDYES)
  143. return FALSE;
  144. ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, index );
  145. SetAliasInfo( ptr, NULL, szurl );
  146. return TRUE;
  147. }
  148. else
  149. {
  150. ptr = new CAlias( alias );
  151. SetAliasInfo( ptr, NULL, szurl );
  152. DPA_InsertPtr(aliasListIn, 0x7FFF, (LPVOID)ptr);
  153. return TRUE;
  154. }
  155. }
  156. return FALSE;
  157. }
  158. STDAPI_(BOOL) SaveAliases( HDPA aliasListIn )
  159. {
  160. // Save the currently changed aliases
  161. if( aliasListIn )
  162. {
  163. int count = DPA_GetPtrCount( aliasListIn );
  164. for(int i=0;i<count;i++)
  165. {
  166. CAlias * pAlias = (CAlias *)DPA_FastGetPtr( aliasListIn, i );
  167. if(pAlias && pAlias->m_fDirty)
  168. {
  169. pAlias->Save();
  170. }
  171. }
  172. return TRUE;
  173. }
  174. return FALSE;
  175. }
  176. STDAPI_(BOOL) LoadAliases( HDPA aliasListIn )
  177. {
  178. HKEY hKey, hKeyAlias;
  179. int index = 0;
  180. DWORD dwLen = MAX_PATH;
  181. if(aliasListIn)
  182. FreeAliases( aliasListIn );
  183. TCHAR * buffer = (TCHAR *)LocalAlloc( LPTR, (MAX_PATH+1)*sizeof(TCHAR) );
  184. LONG lResult = RegOpenKeyExA(
  185. HKEY_CURRENT_USER,
  186. g_szAliasKey,
  187. 0,
  188. KEY_QUERY_VALUE | KEY_READ,
  189. &hKeyAlias);
  190. if( lResult == ERROR_SUCCESS )
  191. {
  192. while( buffer )
  193. {
  194. dwLen = MAXPATH;
  195. if( RegEnumKeyEx( hKeyAlias, index, buffer, &dwLen,
  196. NULL, NULL, NULL, NULL )
  197. == ERROR_NO_MORE_ITEMS ) break;
  198. {
  199. CAlias * ptr = new CAlias( buffer );
  200. if(ptr->Load())
  201. {
  202. DPA_InsertPtr(aliasListIn, 0x7FFF, (LPVOID)ptr);
  203. ptr->m_fDirty = FALSE;
  204. }
  205. else
  206. delete ptr;
  207. index++;
  208. }
  209. }
  210. if(buffer) LocalFree( buffer );
  211. RegCloseKey( hKeyAlias );
  212. }
  213. return TRUE;
  214. }
  215. #ifdef DEBUG
  216. STDAPI_(BOOL) PrintAliases( HDPA aliasListIn )
  217. {
  218. printf("Listing Aliases:\n");
  219. if( !aliasListIn ) return FALSE;
  220. int aliasCount = DPA_GetPtrCount( aliasListIn );
  221. for(int i=0; i<aliasCount; i++ )
  222. {
  223. CAlias * ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, i );
  224. ptr->Print();
  225. }
  226. return TRUE;
  227. }
  228. #endif
  229. STDAPI_(INT) FindAliasIndex(HDPA aliasListIn , LPTSTR alias)
  230. {
  231. if( ! aliasListIn ) return -1;
  232. int aliasCount = DPA_GetPtrCount( aliasListIn );
  233. for(int i = 0; i< aliasCount;i++ )
  234. {
  235. CAlias * ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, i );
  236. if( !StrCmpI(alias, ptr->m_alias) )
  237. return i;
  238. }
  239. return -1;
  240. }
  241. STDAPI_(BOOL) FindAliasByURL(HDPA aliasListIn , LPTSTR szurl, LPTSTR aliasIn, INT cchAlias)
  242. {
  243. if( ! aliasListIn ) return FALSE;
  244. int aliasCount = DPA_GetPtrCount( aliasListIn );
  245. for(int i = 0; i< aliasCount;i++ )
  246. {
  247. CAlias * ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, i );
  248. if( !StrCmp(szurl, ptr->m_szurl) )
  249. {
  250. StrCpy( aliasIn, ptr->m_alias );
  251. return TRUE;
  252. }
  253. }
  254. return FALSE;
  255. }
  256. STDAPI_(BOOL) GetURLForAlias(HDPA aliasListIn, LPTSTR alias, LPTSTR szUrl, int cchUrl )
  257. {
  258. int index = -1;
  259. if(!aliasListIn || !alias || !szUrl || cchUrl <= 0 ) return FALSE;
  260. // ENTERCRITICAL;
  261. if( (index = FindAliasIndex(aliasListIn, alias) ) != -1 )
  262. {
  263. CAlias * ptr = (CAlias *)DPA_FastGetPtr( aliasListIn, index );
  264. StrCpy( szUrl, ptr->m_szurl );
  265. }
  266. Done:
  267. // LEAVECRITICAL;
  268. return (index != -1);
  269. }
  270. STDAPI_(LPCTSTR) GetAliasName( CAlias * ptr )
  271. {
  272. if( ptr )
  273. return ptr->m_alias;
  274. return NULL;
  275. }
  276. STDAPI_(LPCTSTR) GetAliasUrl( CAlias * ptr )
  277. {
  278. if( ptr )
  279. return ptr->m_szurl;
  280. return NULL;
  281. }
  282. STDAPI_(LPVOID) CreateAlias( LPTSTR str )
  283. {
  284. return new CAlias(str);
  285. }
  286. STDAPI_(VOID) DestroyAlias( CAlias * ptr )
  287. {
  288. if( ptr ) delete ptr;
  289. }
  290. STDAPI_(BOOL) SetAliasInfo( CAlias * ptr, TCHAR * alias, TCHAR * url )
  291. {
  292. if( ptr )
  293. {
  294. if(alias)
  295. {
  296. if( ptr->m_alias ) LocalFree( ptr->m_alias );
  297. ptr->m_alias = DuplicateString( alias );
  298. ptr->m_fDirty= TRUE;
  299. }
  300. if(url)
  301. {
  302. if( ptr->m_szurl ) LocalFree( ptr->m_szurl );
  303. ptr->m_szurl = DuplicateString( url );
  304. ptr->m_fDirty= TRUE;
  305. }
  306. }
  307. }
  308. #ifdef UNICODE
  309. STDAPI_(BOOL) FindAliasByURLA(HDPA aliasListIn , LPTSTR szurl, LPTSTR aliasIn, INT cchAlias)
  310. {
  311. WCHAR szwurl[MAX_URL_STRING];
  312. WCHAR aliasw[MAX_ALIAS_LENGTH];
  313. if( !szurl || !aliasIn || !aliasListIn )
  314. return FALSE;
  315. SHAnsiToUnicode( aliasIn, alias
  316. }
  317. STDAPI_(BOOL) AddAliasToListA( HDPA aliasListIn, LPTSTR aliasIn, LPTSTR szurl, HWND hwnd )
  318. {
  319. return FALSE;
  320. }
  321. #else
  322. TCHAR *DuplicateString( TCHAR *orig )
  323. {
  324. TCHAR * newStr;
  325. if( !orig ) return NULL;
  326. newStr = (TCHAR *)LocalAlloc( LPTR, (lstrlen(orig) + 1)*sizeof(TCHAR));
  327. if(newStr) StrCpy( newStr, orig );
  328. return newStr;
  329. }
  330. TCHAR *EatSpaces( TCHAR * str )
  331. {
  332. if( !str ) return NULL;
  333. TCHAR *ptr = str, *tmpStr = DuplicateString( str );
  334. TCHAR *tmpPtr = tmpStr;
  335. while( *tmpStr )
  336. {
  337. if(*tmpStr == TEXT(' ') || *tmpStr == TEXT('\t') ||
  338. *tmpStr == TEXT('\n') || *tmpStr == TEXT('\r') ||
  339. // Remove special characters.
  340. (int)(*tmpStr) >= 127)
  341. tmpStr++;
  342. else
  343. *ptr++ = *tmpStr++;
  344. }
  345. *ptr = TEXT('\0');
  346. LocalFree( tmpPtr );
  347. return str;
  348. }
  349. #endif /* UNICODE */
  350. #endif /* UNIX_FEATURE_ALIAS */