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.

342 lines
8.2 KiB

  1. /*
  2. * newshk.cpp - INewShortcutHook implementation for URL class.
  3. */
  4. /* Headers
  5. **********/
  6. #include "project.hpp"
  7. #pragma hdrstop
  8. #include "resource.h"
  9. #include <mluisupp.h>
  10. extern "C"{
  11. extern BOOL lPathYetAnotherMakeUniqueNameA(LPSTR,LPCSTR,LPCSTR,LPCSTR);
  12. }
  13. /********************************** Methods **********************************/
  14. #pragma warning(disable:4100) /* "unreferenced formal parameter" warning */
  15. HRESULT STDMETHODCALLTYPE InternetShortcut::SetReferent(PCSTR pcszReferent,
  16. HWND hwndParent)
  17. {
  18. HRESULT hr;
  19. PSTR pszTranslatedURL;
  20. DebugEntry(InternetShortcut::SetReferent);
  21. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  22. ASSERT(IS_VALID_STRING_PTR(pcszReferent, CSTR));
  23. ASSERT(IS_VALID_HANDLE(hwndParent, WND));
  24. hr = TranslateURL(pcszReferent, TRANSLATEURL_FL_GUESS_PROTOCOL,
  25. &pszTranslatedURL);
  26. if (SUCCEEDED(hr))
  27. {
  28. PCSTR pcszURLToUse;
  29. pcszURLToUse = (hr == S_OK) ? pszTranslatedURL : pcszReferent;
  30. hr = ValidateURL(pcszURLToUse);
  31. if (hr == S_OK)
  32. hr = SetURL(pcszURLToUse, 0);
  33. if (pszTranslatedURL)
  34. {
  35. LocalFree(pszTranslatedURL);
  36. pszTranslatedURL = NULL;
  37. }
  38. }
  39. if (hr == S_OK)
  40. TRACE_OUT(("InternetShortcut::SetReferent(): Set referent %s as URL %s.",
  41. pcszReferent,
  42. m_pszURL));
  43. else
  44. {
  45. ASSERT(FAILED(hr));
  46. switch (hr)
  47. {
  48. case URL_E_INVALID_SYNTAX:
  49. case URL_E_UNREGISTERED_PROTOCOL:
  50. hr = S_FALSE;
  51. break;
  52. default:
  53. break;
  54. }
  55. TRACE_OUT(("InternetShortcut::SetReferent(): Failed to set referent to %s.",
  56. pcszReferent));
  57. }
  58. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  59. DebugExitHRESULT(InternetShortcut::SetReferent, hr);
  60. return(hr);
  61. }
  62. #pragma warning(default:4100) /* "unreferenced formal parameter" warning */
  63. HRESULT STDMETHODCALLTYPE InternetShortcut::GetReferent(PSTR pszReferent,
  64. int ncReferentBufLen)
  65. {
  66. HRESULT hr;
  67. DebugEntry(InternetShortcut::GetReferent);
  68. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  69. ASSERT(IS_VALID_WRITE_BUFFER_PTR(pszReferent, STR, ncReferentBufLen));
  70. if (m_pszURL)
  71. {
  72. if (lstrlen(m_pszURL) < ncReferentBufLen)
  73. {
  74. lstrcpy(pszReferent, m_pszURL);
  75. hr = S_OK;
  76. TRACE_OUT(("InternetShortcut::GetReferent(): Returning referent %s.",
  77. pszReferent));
  78. }
  79. else
  80. hr = E_FAIL;
  81. }
  82. else
  83. hr = S_FALSE;
  84. if (hr != S_OK)
  85. {
  86. if (ncReferentBufLen > 0)
  87. *pszReferent = '\0';
  88. }
  89. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  90. ASSERT((hr == S_OK &&
  91. IS_VALID_STRING_PTR(pszReferent, STR) &&
  92. EVAL(lstrlen(pszReferent) < ncReferentBufLen)) ||
  93. ((hr == S_FALSE ||
  94. hr == E_FAIL) &&
  95. EVAL(! ncReferentBufLen ||
  96. ! *pszReferent)));
  97. DebugExitHRESULT(InternetShortcut::GetReferent, hr);
  98. return(hr);
  99. }
  100. HRESULT STDMETHODCALLTYPE InternetShortcut::SetFolder(PCSTR pcszFolder)
  101. {
  102. HRESULT hr;
  103. PSTR pszFolderCopy;
  104. DebugEntry(InternetShortcut::SetFolder);
  105. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  106. ASSERT(IsPathDirectory(pcszFolder));
  107. // (+ 1) for null terminator.
  108. pszFolderCopy = new(char[lstrlen(pcszFolder) + 1]);
  109. if (pszFolderCopy)
  110. {
  111. lstrcpy(pszFolderCopy, pcszFolder);
  112. if (m_pszFolder)
  113. delete m_pszFolder;
  114. m_pszFolder = pszFolderCopy;
  115. hr = S_OK;
  116. TRACE_OUT(("InternetShortcut::SetFolder(): Set folder to %s.",
  117. m_pszFolder));
  118. }
  119. else
  120. hr = E_OUTOFMEMORY;
  121. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  122. DebugExitHRESULT(InternetShortcut::SetFolder, hr);
  123. return(hr);
  124. }
  125. HRESULT STDMETHODCALLTYPE InternetShortcut::GetFolder(PSTR pszFolder,
  126. int ncFolderBufLen)
  127. {
  128. HRESULT hr;
  129. DebugEntry(InternetShortcut::GetFolder);
  130. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  131. ASSERT(IS_VALID_WRITE_BUFFER_PTR(pszFolder, STR, ncFolderBufLen));
  132. if (m_pszFolder)
  133. {
  134. if (lstrlen(m_pszFolder) < ncFolderBufLen)
  135. {
  136. lstrcpy(pszFolder, m_pszFolder);
  137. hr = S_OK;
  138. TRACE_OUT(("InternetShortcut::GetFolder(): Returning folder %s.",
  139. pszFolder));
  140. }
  141. else
  142. hr = E_FAIL;
  143. }
  144. else
  145. hr = S_FALSE;
  146. if (hr != S_OK)
  147. {
  148. if (ncFolderBufLen > 0)
  149. *pszFolder = '\0';
  150. }
  151. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  152. ASSERT((hr == S_OK &&
  153. IS_VALID_STRING_PTR(pszFolder, STR) &&
  154. EVAL(lstrlen(pszFolder) < ncFolderBufLen)) ||
  155. ((hr == S_FALSE ||
  156. hr == E_FAIL) &&
  157. EVAL(! ncFolderBufLen ||
  158. ! *pszFolder)));
  159. DebugExitHRESULT(InternetShortcut::GetFolder, hr);
  160. return(hr);
  161. }
  162. HRESULT STDMETHODCALLTYPE InternetShortcut::GetName(PSTR pszName,
  163. int ncNameBufLen)
  164. {
  165. HRESULT hr = E_FAIL;
  166. char rgchShortName[MAX_PATH_LEN];
  167. DebugEntry(InternetShortcut::GetName);
  168. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  169. ASSERT(IS_VALID_WRITE_BUFFER_PTR(pszName, STR, ncNameBufLen));
  170. hr = E_FAIL;
  171. if (MLLoadStringA(IDS_SHORT_NEW_INTERNET_SHORTCUT,
  172. rgchShortName, sizeof(rgchShortName)))
  173. {
  174. char rgchLongName[MAX_PATH_LEN];
  175. if (MLLoadStringA(IDS_NEW_INTERNET_SHORTCUT,
  176. rgchLongName, sizeof(rgchLongName)))
  177. {
  178. char rgchCurDir[MAX_PATH_LEN];
  179. PCSTR pcszFolderToUse;
  180. // Use current directory if m_pszFolder has not been set.
  181. pcszFolderToUse = m_pszFolder;
  182. if (! pcszFolderToUse)
  183. {
  184. if (GetCurrentDirectory(sizeof(rgchCurDir), rgchCurDir) > 0)
  185. pcszFolderToUse = rgchCurDir;
  186. }
  187. if (pcszFolderToUse)
  188. {
  189. char rgchUniqueName[MAX_PATH_LEN];
  190. if (lPathYetAnotherMakeUniqueNameA(rgchUniqueName, pcszFolderToUse,
  191. rgchShortName, rgchLongName))
  192. {
  193. PSTR pszFileName;
  194. PSTR pszRemoveExt;
  195. pszFileName = (PSTR)ExtractFileName(rgchUniqueName);
  196. pszRemoveExt = (PSTR)ExtractExtension(pszFileName);
  197. *pszRemoveExt = '\0';
  198. if (lstrlen(pszFileName) < ncNameBufLen)
  199. {
  200. lstrcpy(pszName, pszFileName);
  201. hr = S_OK;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. if (hr == S_OK) {
  208. TRACE_OUT(("InternetShortcut::GetName(): Returning %s.",
  209. pszName));
  210. }
  211. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  212. ASSERT((hr == S_OK &&
  213. IS_VALID_STRING_PTR(pszName, STR) &&
  214. EVAL(lstrlen(pszName) < ncNameBufLen)) ||
  215. (hr == E_FAIL &&
  216. (! ncNameBufLen ||
  217. ! *pszName)));
  218. DebugExitHRESULT(InternetShortcut::GetName, hr);
  219. return(hr);
  220. }
  221. HRESULT STDMETHODCALLTYPE InternetShortcut::GetExtension(PSTR pszExtension,
  222. int ncExtensionBufLen)
  223. {
  224. HRESULT hr;
  225. DebugEntry(InternetShortcut::GetExtension);
  226. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  227. ASSERT(IS_VALID_WRITE_BUFFER_PTR(pszExtension, STR, ncExtensionBufLen));
  228. if (lstrlen(g_cszURLExt) < ncExtensionBufLen)
  229. {
  230. lstrcpy(pszExtension, g_cszURLExt);
  231. hr = S_OK;
  232. TRACE_OUT(("InternetShortcut::GetExtension(): Returning extension %s.",
  233. pszExtension));
  234. }
  235. else
  236. {
  237. if (ncExtensionBufLen > 0)
  238. *pszExtension = '\0';
  239. hr = E_FAIL;
  240. }
  241. ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  242. ASSERT((hr == S_OK &&
  243. IS_VALID_STRING_PTR(pszExtension, STR) &&
  244. EVAL(lstrlen(pszExtension) < ncExtensionBufLen)) ||
  245. (hr == E_FAIL &&
  246. EVAL(! ncExtensionBufLen ||
  247. ! *pszExtension)));
  248. DebugExitHRESULT(InternetShortcut::GetExtension, hr);
  249. return(hr);
  250. }