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.

419 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: I E X T R A C T . C P P
  7. //
  8. // Contents: IExtract implementation for CUPnPDeviceFolderExtractIcon
  9. //
  10. // Notes:
  11. //
  12. // Author: jeffspr 7 Oct 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "upsres.h"
  18. #include "upscmn.h"
  19. extern const WCHAR c_szUPnPUIDll[];
  20. static const WCHAR* c_szDllName = c_szUPnPUIDll;
  21. typedef struct
  22. {
  23. BSTR m_DeviceType ;
  24. WORD m_wResourceID;
  25. } DeviceTypeIconMAPPING ;
  26. static CONST DeviceTypeIconMAPPING g_DeviceTypeIconConvtTable[] =
  27. {
  28. {L"Camcorder" , IDI_UPNP_CAMCORDER},
  29. {L"DigitalSecurityCamera" , IDI_UPNP_DIGITAL_SECURITY_CAMERA},
  30. // {L"DisplayDevice" , IDI_UPNP_DISPLAY_DEVICE},
  31. {L"InternetGateway" , IDI_UPNP_INTERNET_GATEWAY},
  32. {L"PrinterDevice" , IDI_UPNP_PRINTER_DEVICE},
  33. {L"ScannerDevice" , IDI_UPNP_SCANNER_DEVICE},
  34. {L"AudioPlayer" , IDI_UPNP_AUDIO_PLAYER}
  35. };
  36. WORD GetDefaultDeviceIconResourceID(BSTR bstrDeviceType)
  37. {
  38. CONST int nSize = celems(g_DeviceTypeIconConvtTable);
  39. int i ;
  40. if( bstrDeviceType != NULL )
  41. {
  42. for( i = 0 ; i < nSize ; i++ )
  43. {
  44. // ignore case ??
  45. if ( wcsstr(bstrDeviceType , g_DeviceTypeIconConvtTable[i].m_DeviceType) )
  46. return g_DeviceTypeIconConvtTable[i].m_wResourceID;
  47. }
  48. }
  49. return 0 ;
  50. }
  51. CUPnPDeviceFolderExtractIcon::CUPnPDeviceFolderExtractIcon()
  52. {
  53. m_DeviceType = NULL;
  54. m_DeviceUDN = NULL;
  55. }
  56. CUPnPDeviceFolderExtractIcon::~CUPnPDeviceFolderExtractIcon()
  57. {
  58. SysFreeString(m_DeviceType);
  59. SysFreeString(m_DeviceUDN);
  60. }
  61. HRESULT CUPnPDeviceFolderExtractIcon::CreateInstance(
  62. LPCITEMIDLIST apidl,
  63. REFIID riid,
  64. void** ppv)
  65. {
  66. HRESULT hr = E_OUTOFMEMORY;
  67. CUPnPDeviceFolderExtractIcon * pObj = NULL;
  68. pObj = new CComObject <CUPnPDeviceFolderExtractIcon>;
  69. if (pObj)
  70. {
  71. // Do the standard CComCreator::CreateInstance stuff.
  72. //
  73. pObj->SetVoid (NULL);
  74. pObj->InternalFinalConstructAddRef ();
  75. hr = pObj->FinalConstruct ();
  76. pObj->InternalFinalConstructRelease ();
  77. if (SUCCEEDED(hr))
  78. {
  79. if (SUCCEEDED(hr))
  80. {
  81. hr = pObj->GetUnknown()->QueryInterface (riid, ppv);
  82. }
  83. }
  84. if (FAILED(hr))
  85. {
  86. delete pObj;
  87. }
  88. }
  89. return hr;
  90. }
  91. HRESULT CUPnPDeviceFolderExtractIcon::Initialize(LPCITEMIDLIST apidl)
  92. {
  93. HRESULT hr = S_OK;
  94. PUPNPDEVICEFOLDPIDL pupdfp = NULL;
  95. CUPnPDeviceFoldPidl udfp;
  96. PCWSTR psz = NULL;
  97. PCWSTR pszUdn = NULL;
  98. if (!FIsUPnPDeviceFoldPidl(apidl) )
  99. {
  100. hr = E_INVALIDARG;
  101. }
  102. else
  103. {
  104. pupdfp = ConvertToUPnPDevicePIDL(apidl);
  105. hr = udfp.HrInit(pupdfp);
  106. if (SUCCEEDED(hr))
  107. {
  108. psz = udfp.PszGetTypePointer();
  109. hr = HrSysAllocString(psz,&m_DeviceType);
  110. pszUdn = udfp.PszGetUDNPointer();
  111. hr = HrSysAllocString(pszUdn,&m_DeviceUDN);
  112. }
  113. }
  114. return hr;
  115. }
  116. //+---------------------------------------------------------------------------
  117. //
  118. // Member: CUPnPDeviceFolderExtractIcon::GetIconLocation
  119. //
  120. // Purpose:
  121. //
  122. // Arguments:
  123. // uFlags [in] Address of a UINT value that receives zero or a
  124. // combination of the following values:
  125. //
  126. // GIL_ASYNC The calling application supports asynchronous
  127. // retrieval of icons.
  128. // GIL_FORSHELL The icon is to be displayed in a shell folder.
  129. //
  130. // wzIconFile [out] Address of the buffer that receives the icon
  131. // location. The icon location is a null-terminated
  132. // string that identifies the file that contains
  133. // the icon.
  134. // cchMax [in] Size of the buffer that receives the icon location.
  135. // piIndex [out] Address of an INT that receives the icon index,
  136. // which further describes the icon location.
  137. // pwFlags [in] Address of a UINT value that receives zero or a
  138. // combination of the following values:
  139. //
  140. // GIL_DONTCACHE Don't cache the physical bits.
  141. // GIL_NOTFILENAME This isn't a filename/index pair. Call
  142. // IExtractIcon::Extract instead
  143. // GIL_PERCLASS (Only internal to the shell)
  144. // GIL_PERINSTANCE Each object of this class has the same icon.
  145. //
  146. //
  147. // Returns: S_OK if the function returned a valid location,
  148. // or S_FALSE if the shell should use a default icon.
  149. //
  150. // Author: jeffspr 25 Nov 1998
  151. //
  152. // Notes:
  153. //
  154. STDMETHODIMP CUPnPDeviceFolderExtractIcon::GetIconLocation(
  155. UINT uFlags,
  156. PWSTR szIconFile,
  157. UINT cchMax,
  158. int * piIndex,
  159. UINT * pwFlags)
  160. {
  161. HRESULT hr = S_OK;
  162. WORD wResourceID = 0;
  163. TraceTag(ttidShellFolderIface, "OBJ: CCFEI - IExtractIcon::GetIconLocation, flags: %d", uFlags);
  164. Assert(pwFlags);
  165. Assert(szIconFile);
  166. Assert(piIndex);
  167. #ifdef DBG
  168. // Easy way to check if certain flags are set
  169. //
  170. BOOL fAsync = (uFlags & GIL_ASYNC);
  171. BOOL fForShell = (uFlags & GIL_FORSHELL);
  172. BOOL fOpenIcon = (uFlags & GIL_OPENICON);
  173. #endif
  174. *pwFlags = GIL_PERINSTANCE ;
  175. if((wResourceID = GetDefaultDeviceIconResourceID(m_DeviceType)))
  176. {
  177. *piIndex = (-1)*wResourceID ;
  178. }
  179. else
  180. {
  181. *piIndex = 1 ;
  182. }
  183. lstrcpyW(szIconFile, c_szUPnPUIDll);
  184. return hr;
  185. }
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CUPnPDeviceFolderExtractIcon::GetIconLocation
  189. //
  190. // Purpose: ANSI wrapper for the above UNICODE GetIconLocation
  191. //
  192. // Arguments:
  193. // uFlags [] See above
  194. // szIconFile [] See above
  195. // cchMax [] See above
  196. // piIndex [] See above
  197. // pwFlags [] See above
  198. //
  199. // Returns:
  200. //
  201. // Author: jeffspr 6 Apr 1999
  202. //
  203. // Notes:
  204. //
  205. STDMETHODIMP CUPnPDeviceFolderExtractIcon::GetIconLocation(
  206. UINT uFlags,
  207. PSTR szIconFile,
  208. UINT cchMax,
  209. int * piIndex,
  210. UINT * pwFlags)
  211. {
  212. HRESULT hr = S_OK;
  213. WCHAR * pszIconFileW = new WCHAR[cchMax];
  214. if (!pszIconFileW)
  215. {
  216. hr = ERROR_OUTOFMEMORY;
  217. }
  218. else
  219. {
  220. hr = GetIconLocation(uFlags, pszIconFileW, cchMax, piIndex, pwFlags);
  221. if (SUCCEEDED(hr))
  222. {
  223. WideCharToMultiByte(CP_ACP, 0, pszIconFileW, -1, szIconFile, cchMax, NULL, NULL);
  224. }
  225. delete [] pszIconFileW;
  226. }
  227. TraceHr(ttidShellFolder, FAL, hr, FALSE, "CUPnPDeviceFolderExtractIcon::GetIconLocation(A)");
  228. return hr;
  229. }
  230. //+---------------------------------------------------------------------------
  231. //
  232. // Member: CUPnPDeviceFolderExtractIcon::Extract
  233. //
  234. // Purpose: Grab the actual icon for the caller.
  235. //
  236. // Arguments:
  237. // wzFile [] Filename from where we'll retrieve the icon
  238. // nIconIndex [] Index of the icon (though this is bogus)
  239. // phiconLarge [] Return pointer for the large icon handle
  240. // phiconSmall [] Return pointer for the small icon handle
  241. // nIconSize [] Size of the icon requested.
  242. //
  243. // Returns:
  244. //
  245. // Author: jeffspr 9 Oct 1997
  246. //
  247. // Notes:
  248. //
  249. STDMETHODIMP CUPnPDeviceFolderExtractIcon::Extract(
  250. PCWSTR wzFile,
  251. UINT nIconIndex,
  252. HICON * phiconLarge,
  253. HICON * phiconSmall,
  254. UINT nIconSize)
  255. {
  256. HRESULT hr = S_OK;
  257. int nSizeLarge = 0;
  258. int nSizeSmall = 0;
  259. TraceTag(ttidShellFolderIface, "OBJ: CCFEI - IExtractIcon::Extract");
  260. Assert(wzFile);
  261. Assert(phiconLarge);
  262. Assert(phiconSmall);
  263. nSizeLarge = LOWORD(nIconSize);
  264. nSizeSmall = HIWORD(nIconSize);
  265. TraceTag(ttidShellFolder,
  266. "Extract: %S, index: %d, nIconSize: large=%d small=%d",
  267. wzFile, nIconIndex, nSizeLarge, nSizeSmall);
  268. hr = HrLoadIcons(wzFile, nIconIndex, nSizeLarge, nSizeSmall,
  269. phiconLarge, phiconSmall);
  270. return hr;
  271. }
  272. HRESULT CUPnPDeviceFolderExtractIcon::HrLoadIcons(
  273. PCWSTR pszFile,
  274. UINT nIconIndex,
  275. int nSizeLarge,
  276. int nSizeSmall,
  277. HICON * phiconLarge,
  278. HICON * phiconSmall)
  279. {
  280. HRESULT hr = S_OK;
  281. WORD wResourceID = 0 ;
  282. wResourceID = GetDefaultDeviceIconResourceID(m_DeviceType);
  283. if(!wResourceID)
  284. wResourceID = IDI_UPNPDEVICE;
  285. if (phiconLarge)
  286. {
  287. int cx = nSizeLarge;
  288. int cy = nSizeLarge;
  289. *phiconLarge = (HICON) LoadImage(_Module.GetResourceInstance(),
  290. MAKEINTRESOURCE(wResourceID),
  291. IMAGE_ICON,
  292. cx,
  293. cy,
  294. LR_DEFAULTCOLOR);
  295. if (!*phiconLarge)
  296. {
  297. AssertSz(FALSE, "Unable to load large icon in CUPnPDeviceFolderExtractIcon::GetWizardIcons");
  298. }
  299. }
  300. if (phiconSmall)
  301. {
  302. int cx = nSizeSmall;
  303. int cy = nSizeSmall;
  304. *phiconSmall = (HICON) LoadImage(_Module.GetResourceInstance(),
  305. MAKEINTRESOURCE(wResourceID),
  306. IMAGE_ICON,
  307. cx,
  308. cy,
  309. LR_DEFAULTCOLOR);
  310. if (!*phiconSmall)
  311. {
  312. AssertSz(FALSE, "Unable to load small icon in CUPnPDeviceFolderExtractIcon::GetWizardIcons");
  313. }
  314. }
  315. return hr;
  316. }
  317. //+---------------------------------------------------------------------------
  318. //
  319. // Member: CUPnPDeviceFolderExtractIcon::Extract
  320. //
  321. // Purpose: ANSI version of the above Extract
  322. //
  323. // Arguments:
  324. // pszFile [] Filename from where we'll retrieve the icon
  325. // nIconIndex [] Index of the icon (though this is bogus)
  326. // phiconLarge [] Return pointer for the large icon handle
  327. // phiconSmall [] Return pointer for the small icon handle
  328. // nIconSize [] Size of the icon requested.
  329. //
  330. // Returns:
  331. //
  332. // Author: jeffspr 6 Apr 1999
  333. //
  334. // Notes:
  335. //
  336. STDMETHODIMP CUPnPDeviceFolderExtractIcon::Extract(
  337. PCSTR pszFile,
  338. UINT nIconIndex,
  339. HICON * phiconLarge,
  340. HICON * phiconSmall,
  341. UINT nIconSize)
  342. {
  343. HRESULT hr = S_OK;
  344. INT cch = 0;
  345. WCHAR * pszFileW = NULL;
  346. Assert(pszFile);
  347. cch = lstrlenA(pszFile) + 1;
  348. pszFileW = new WCHAR[cch];
  349. if (!pszFileW)
  350. {
  351. hr = ERROR_OUTOFMEMORY;
  352. }
  353. else
  354. {
  355. MultiByteToWideChar(CP_ACP, 0, pszFile, -1, pszFileW, cch);
  356. hr = Extract(pszFileW, nIconIndex, phiconLarge, phiconSmall, nIconSize);
  357. delete [] pszFileW;
  358. }
  359. TraceHr(ttidShellFolder, FAL, hr, FALSE, "CUPnPDeviceFolderExtractIcon::Extract(A)");
  360. return hr;
  361. }