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.

380 lines
8.5 KiB

  1. /*
  2. * isnewshk.cpp - INewShortcutHook implementation for URL class.
  3. */
  4. #include "priv.h"
  5. #include "ishcut.h"
  6. #include "resource.h"
  7. #include <mluisupp.h>
  8. #pragma warning(disable:4100) /* "unreferenced formal parameter" warning */
  9. STDMETHODIMP
  10. Intshcut::SetReferent(
  11. LPCTSTR pcszReferent,
  12. HWND hwndParent)
  13. {
  14. HRESULT hr;
  15. TCHAR szURL[MAX_URL_STRING];
  16. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  17. ASSERT(IS_VALID_STRING_PTR(pcszReferent, -1));
  18. ASSERT(IS_VALID_HANDLE(hwndParent, WND));
  19. hr = IURLQualify(pcszReferent, UQF_IGNORE_FILEPATHS | UQF_GUESS_PROTOCOL, szURL, NULL, NULL);
  20. if (SUCCEEDED(hr))
  21. {
  22. hr = ValidateURL(szURL);
  23. if (hr == S_OK)
  24. hr = SetURL(szURL, 0);
  25. }
  26. if (S_OK != hr)
  27. {
  28. ASSERT(FAILED(hr));
  29. // Massage result
  30. switch (hr)
  31. {
  32. case URL_E_INVALID_SYNTAX:
  33. case URL_E_UNREGISTERED_PROTOCOL:
  34. hr = S_FALSE;
  35. break;
  36. default:
  37. break;
  38. }
  39. TraceMsg(TF_INTSHCUT, "Intshcut::SetReferent(): Failed to set referent to %s.",
  40. pcszReferent);
  41. }
  42. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  43. return(hr);
  44. }
  45. #pragma warning(default:4100) /* "unreferenced formal parameter" warning */
  46. STDMETHODIMP Intshcut::GetReferent(PTSTR pszReferent, int cchReferent)
  47. {
  48. HRESULT hr = InitProp();
  49. if (SUCCEEDED(hr))
  50. {
  51. TCHAR szURL[INTERNET_MAX_URL_LENGTH];
  52. hr = m_pprop->GetProp(PID_IS_URL, szURL, SIZECHARS(szURL));
  53. if (S_OK == hr)
  54. {
  55. if (lstrlen(szURL) < cchReferent)
  56. {
  57. StrCpyN(pszReferent, szURL, cchReferent);
  58. hr = S_OK;
  59. }
  60. else
  61. hr = E_FAIL;
  62. }
  63. else
  64. hr = S_FALSE;
  65. if (hr != S_OK)
  66. {
  67. if (cchReferent > 0)
  68. *pszReferent = '\0';
  69. }
  70. }
  71. return hr;
  72. }
  73. STDMETHODIMP Intshcut::SetFolder(LPCTSTR pcszFolder)
  74. {
  75. HRESULT hr;
  76. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  77. ASSERT(PathIsDirectory(pcszFolder));
  78. if (Str_SetPtr(&m_pszFolder, pcszFolder))
  79. {
  80. hr = S_OK;
  81. TraceMsg(TF_INTSHCUT, "Intshcut::SetFolder(): Set folder to %s.",
  82. m_pszFolder);
  83. }
  84. else
  85. hr = E_OUTOFMEMORY;
  86. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  87. return(hr);
  88. }
  89. STDMETHODIMP
  90. Intshcut::GetFolder(
  91. LPTSTR pszFolder,
  92. int cchFolder)
  93. {
  94. HRESULT hr;
  95. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  96. ASSERT(IS_VALID_WRITE_BUFFER(pszFolder, TCHAR, cchFolder));
  97. if (m_pszFolder)
  98. {
  99. if (lstrlen(m_pszFolder) < cchFolder)
  100. {
  101. StrCpyN(pszFolder, m_pszFolder, cchFolder);
  102. hr = S_OK;
  103. TraceMsg(TF_INTSHCUT, "Intshcut::GetFolder(): Returning folder %s.",
  104. pszFolder);
  105. }
  106. else
  107. hr = E_FAIL;
  108. }
  109. else
  110. hr = S_FALSE;
  111. if (hr != S_OK)
  112. {
  113. if (cchFolder > 0)
  114. *pszFolder = '\0';
  115. }
  116. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  117. ASSERT((hr == S_OK &&
  118. IS_VALID_STRING_PTR(pszFolder, -1) &&
  119. EVAL(lstrlen(pszFolder) < cchFolder)) ||
  120. ((hr == S_FALSE ||
  121. hr == E_FAIL) &&
  122. EVAL(! cchFolder ||
  123. ! *pszFolder)));
  124. return(hr);
  125. }
  126. STDMETHODIMP
  127. Intshcut::GetName(
  128. LPTSTR pszName,
  129. int cchBuf)
  130. {
  131. HRESULT hr = E_FAIL;
  132. TCHAR rgchShortName[MAX_PATH];
  133. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  134. ASSERT(IS_VALID_WRITE_BUFFER(pszName, TCHAR, cchBuf));
  135. hr = E_FAIL;
  136. if (MLLoadString(IDS_SHORT_NEW_INTSHCUT, rgchShortName, SIZECHARS(rgchShortName)))
  137. {
  138. TCHAR rgchLongName[MAX_PATH];
  139. if (MLLoadString(IDS_NEW_INTSHCUT, rgchLongName, SIZECHARS(rgchLongName)))
  140. {
  141. TCHAR rgchCurDir[MAX_PATH];
  142. LPCTSTR pcszFolderToUse;
  143. // Use current directory if m_pszFolder has not been set.
  144. pcszFolderToUse = m_pszFolder;
  145. if (! pcszFolderToUse)
  146. {
  147. if (GetCurrentDirectory(SIZECHARS(rgchCurDir), rgchCurDir) > 0)
  148. pcszFolderToUse = rgchCurDir;
  149. }
  150. if (pcszFolderToUse)
  151. {
  152. TCHAR rgchUniqueName[MAX_PATH];
  153. if (PathYetAnotherMakeUniqueName(rgchUniqueName, pcszFolderToUse,
  154. rgchShortName, rgchLongName))
  155. {
  156. PTSTR pszFileName;
  157. PTSTR pszRemoveExt;
  158. pszFileName = (PTSTR)PathFindFileName(rgchUniqueName);
  159. pszRemoveExt = (PTSTR)PathFindExtension(pszFileName);
  160. *pszRemoveExt = '\0';
  161. if (lstrlen(pszFileName) < cchBuf)
  162. {
  163. StrCpyN(pszName, pszFileName, cchBuf);
  164. hr = S_OK;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. if (hr == S_OK)
  171. TraceMsg(TF_INTSHCUT, "Intshcut::GetName(): Returning %s.", pszName);
  172. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  173. ASSERT((hr == S_OK &&
  174. IS_VALID_STRING_PTR(pszName, -1) &&
  175. EVAL(lstrlen(pszName) < cchBuf)) ||
  176. (hr == E_FAIL &&
  177. (! cchBuf ||
  178. ! *pszName)));
  179. return(hr);
  180. }
  181. STDMETHODIMP
  182. Intshcut::GetExtension(
  183. LPTSTR pszExtension,
  184. int cchBufMax)
  185. {
  186. HRESULT hr;
  187. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  188. ASSERT(IS_VALID_WRITE_BUFFER(pszExtension, TCHAR, cchBufMax));
  189. if (SIZECHARS(TEXT(".url")) < cchBufMax)
  190. {
  191. StrCpyN(pszExtension, TEXT(".url"), cchBufMax);
  192. hr = S_OK;
  193. TraceMsg(TF_INTSHCUT, "Intshcut::GetExtension(): Returning extension %s.",
  194. pszExtension);
  195. }
  196. else
  197. {
  198. if (cchBufMax > 0)
  199. *pszExtension = '\0';
  200. hr = E_FAIL;
  201. }
  202. ASSERT(IS_VALID_STRUCT_PTR(this, CIntshcut));
  203. ASSERT((hr == S_OK &&
  204. IS_VALID_STRING_PTR(pszExtension, -1) &&
  205. EVAL(lstrlen(pszExtension) < cchBufMax)) ||
  206. (hr == E_FAIL &&
  207. EVAL(! cchBufMax ||
  208. ! *pszExtension)));
  209. return(hr);
  210. }
  211. // Ansi versions. Needed for W9x
  212. STDMETHODIMP
  213. Intshcut::SetReferent(
  214. LPCSTR pcszReferent,
  215. HWND hwndParent)
  216. {
  217. HRESULT hr;
  218. WCHAR szReferent[MAX_URL_STRING];
  219. ASSERT(lstrlenA(pcszReferent) + 1 < ARRAYSIZE(szReferent));
  220. SHAnsiToUnicode(pcszReferent, szReferent, ARRAYSIZE(szReferent));
  221. hr = SetReferent(szReferent, hwndParent);
  222. return hr;
  223. }
  224. STDMETHODIMP Intshcut::GetReferent(PSTR pszReferent, int cchReferent)
  225. {
  226. HRESULT hr;
  227. WCHAR szReferent[MAX_URL_STRING];
  228. ASSERT(cchReferent <= ARRAYSIZE(szReferent));
  229. hr = GetReferent(szReferent, ARRAYSIZE(szReferent));
  230. if (SUCCEEDED(hr))
  231. SHUnicodeToAnsi(szReferent, pszReferent, cchReferent);
  232. return hr;
  233. }
  234. STDMETHODIMP Intshcut::SetFolder(LPCSTR pcszFolder)
  235. {
  236. HRESULT hr;
  237. WCHAR szFolder[MAX_PATH];
  238. ASSERT(lstrlenA(pcszFolder) + 1 < ARRAYSIZE(szFolder))
  239. SHAnsiToUnicode(pcszFolder, szFolder, ARRAYSIZE(szFolder));
  240. hr = SetFolder(szFolder);
  241. return(hr);
  242. }
  243. STDMETHODIMP
  244. Intshcut::GetFolder(
  245. LPSTR pszFolder,
  246. int cchFolder)
  247. {
  248. HRESULT hr;
  249. WCHAR szFolder[MAX_PATH];
  250. ASSERT(cchFolder <= ARRAYSIZE(szFolder));
  251. hr = GetFolder(szFolder, ARRAYSIZE(szFolder));
  252. if (SUCCEEDED(hr))
  253. SHUnicodeToAnsi(szFolder, pszFolder, cchFolder);
  254. return hr;
  255. }
  256. STDMETHODIMP
  257. Intshcut::GetName(
  258. LPSTR pszName,
  259. int cchBuf)
  260. {
  261. HRESULT hr;
  262. WCHAR szName[MAX_PATH];
  263. ASSERT(cchBuf <= ARRAYSIZE(szName));
  264. hr = GetName(szName, ARRAYSIZE(szName));
  265. if (SUCCEEDED(hr))
  266. SHUnicodeToAnsi(szName, pszName, cchBuf);
  267. return hr;
  268. }
  269. STDMETHODIMP
  270. Intshcut::GetExtension(
  271. LPSTR pszExtension,
  272. int cchBufMax)
  273. {
  274. HRESULT hr;
  275. WCHAR szExtension[MAX_PATH];
  276. ASSERT(cchBufMax<= ARRAYSIZE(szExtension));
  277. hr = GetExtension(szExtension, ARRAYSIZE(szExtension));
  278. if (SUCCEEDED(hr))
  279. SHUnicodeToAnsi(szExtension, pszExtension, cchBufMax);
  280. return hr;
  281. }