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.

1047 lines
25 KiB

  1. //*******************************************************************************************
  2. //
  3. // Filename : folder.cpp
  4. //
  5. // CAB Files Shell Extension
  6. //
  7. // Copyright (c) 1994 - 1997 Microsoft Corporation. All rights reserved
  8. //
  9. //*******************************************************************************************
  10. #include "pch.h"
  11. #include "ccstock.h"
  12. #include "thisdll.h"
  13. #include "thisguid.h"
  14. #include "ntquery.h"
  15. #include "varutil.h"
  16. #include "folder.h"
  17. #include "enum.h"
  18. #include "menu.h"
  19. #include "dataobj.h"
  20. #include "cabitms.h"
  21. #include "resource.h"
  22. STDAPI StringToStrRet(LPCTSTR pszName, STRRET *pStrRet)
  23. {
  24. #ifdef UNICODE
  25. pStrRet->uType = STRRET_WSTR;
  26. return SHStrDup(pszName, &pStrRet->pOleStr);
  27. #else
  28. pStrRet->uType = STRRET_CSTR;
  29. lstrcpyn(pStrRet->cStr, pszName, ARRAYSIZE(pStrRet->cStr));
  30. return NOERROR;
  31. #endif
  32. }
  33. STDMETHODIMP CCabFolder::QueryInterface(REFIID riid, void **ppv)
  34. {
  35. if (CLSID_CabFolder == riid)
  36. {
  37. // yuck - dataobject uses this when loading us from a stream:
  38. // NOTE: we are doing an AddRef() in this case
  39. *ppv = (CCabFolder*) this;
  40. AddRef();
  41. return S_OK;
  42. }
  43. else
  44. {
  45. static const QITAB qit[] = {
  46. QITABENT(CCabFolder, IShellFolder2),
  47. QITABENTMULTI(CCabFolder, IShellFolder, IShellFolder2),
  48. QITABENT(CCabFolder, IPersistFolder2),
  49. QITABENTMULTI(CCabFolder, IPersistFolder, IPersistFolder2),
  50. QITABENTMULTI(CCabFolder, IPersist, IPersistFolder2),
  51. QITABENT(CCabFolder, IShellFolderViewCB),
  52. { 0 },
  53. };
  54. return QISearch(this, qit, riid, ppv);
  55. }
  56. }
  57. STDMETHODIMP_(ULONG) CCabFolder::AddRef(void)
  58. {
  59. return(m_cRef.AddRef());
  60. }
  61. STDMETHODIMP_(ULONG) CCabFolder::Release(void)
  62. {
  63. if (!m_cRef.Release())
  64. {
  65. delete this;
  66. return(0);
  67. }
  68. return(m_cRef.GetRef());
  69. }
  70. STDMETHODIMP CCabFolder::ParseDisplayName(HWND hwnd, LPBC pbc, LPOLESTR pszDisplayName, ULONG *pchEaten, LPITEMIDLIST *ppidl, ULONG *pdwAttributes)
  71. {
  72. return E_NOTIMPL;
  73. }
  74. //**********************************************************************
  75. //
  76. // Purpose:
  77. //
  78. // Creates an item enumeration object
  79. // (an IEnumIDList interface) that can be used to
  80. // enumerate the contents of a folder.
  81. //
  82. // Parameters:
  83. //
  84. // HWND hwndOwner - handle to the owner window
  85. // DWORD grFlags - flags about which items to include
  86. // IEnumIDList **ppenumIDList - address that receives IEnumIDList
  87. // interface pointer
  88. //********************************************************************
  89. STDMETHODIMP CCabFolder::EnumObjects(HWND hwnd, DWORD grfFlags, IEnumIDList **ppenumIDList)
  90. {
  91. HRESULT hres;
  92. CEnumCabObjs *pce = new CEnumCabObjs(this, grfFlags);
  93. if (pce)
  94. {
  95. hres = pce->QueryInterface(IID_IEnumIDList, (void **)ppenumIDList);
  96. }
  97. else
  98. {
  99. *ppenumIDList = NULL;
  100. hres = E_OUTOFMEMORY;
  101. }
  102. return hres;
  103. }
  104. STDMETHODIMP CCabFolder::BindToObject(LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void **ppvObj)
  105. {
  106. return E_NOTIMPL;
  107. }
  108. STDMETHODIMP CCabFolder::BindToStorage(LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void ** ppvObj)
  109. {
  110. return E_NOTIMPL;
  111. }
  112. //**********************************************************************
  113. //
  114. // CCabFolder::CompareIDs
  115. //
  116. // Purpose:
  117. //
  118. // Determines the relative ordering of two file
  119. // objects or folders, given their item identifier lists
  120. //
  121. // Parameters:
  122. //
  123. // LPARAM lParam - type of comparison
  124. // LPCITEMIDLIST pidl1 - address to ITEMIDLIST
  125. // LPCITEMIDLIST pidl2 - address to ITEMIDLIST
  126. //
  127. //
  128. // Comments:
  129. //
  130. //
  131. //********************************************************************
  132. STDMETHODIMP CCabFolder::CompareIDs(LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
  133. {
  134. LPCABITEM pit1 = (LPCABITEM)pidl1;
  135. LPCABITEM pit2 = (LPCABITEM)pidl2;
  136. short nCmp = 0;
  137. LPCWSTR pszName1, pszName2;
  138. WSTR_ALIGNED_STACK_COPY(&pszName1, pit1->szName);
  139. WSTR_ALIGNED_STACK_COPY(&pszName2, pit2->szName);
  140. switch (lParam)
  141. {
  142. case CV_COL_NAME:
  143. break;
  144. case CV_COL_SIZE:
  145. if (pit1->dwFileSize < pit2->dwFileSize)
  146. {
  147. nCmp = -1;
  148. }
  149. else if (pit1->dwFileSize > pit2->dwFileSize)
  150. {
  151. nCmp = 1;
  152. }
  153. break;
  154. case CV_COL_TYPE:
  155. {
  156. STRRET srName1, srName2;
  157. GetTypeOf(pit1, &srName1);
  158. GetTypeOf(pit2, &srName2);
  159. #ifdef UNICODE
  160. if (srName1.pOleStr && srName2.pOleStr)
  161. {
  162. nCmp = (SHORT)lstrcmp(srName1.pOleStr, srName2.pOleStr);
  163. }
  164. else
  165. {
  166. if (srName1.pOleStr)
  167. {
  168. nCmp = 1;
  169. }
  170. else
  171. {
  172. nCmp = -1;
  173. }
  174. }
  175. if (srName1.pOleStr)
  176. {
  177. CoTaskMemFree(srName1.pOleStr);
  178. }
  179. if (srName2.pOleStr)
  180. {
  181. CoTaskMemFree(srName2.pOleStr);
  182. }
  183. #else // UNICODE
  184. nCmp = (SHORT)lstrcmp(srName1.cStr, srName2.cStr);
  185. #endif // UNICODE
  186. break;
  187. }
  188. case CV_COL_MODIFIED:
  189. if (pit1->uFileDate < pit2->uFileDate)
  190. {
  191. nCmp = -1;
  192. }
  193. else if (pit1->uFileDate > pit2->uFileDate)
  194. {
  195. nCmp = 1;
  196. }
  197. else if (pit1->uFileTime < pit2->uFileTime)
  198. {
  199. nCmp = -1;
  200. }
  201. else if (pit1->uFileTime > pit2->uFileTime)
  202. {
  203. nCmp = 1;
  204. }
  205. break;
  206. case CV_COL_PATH:
  207. if (pit1->cPathChars == 0)
  208. {
  209. if (pit2->cPathChars != 0)
  210. {
  211. nCmp = -1;
  212. }
  213. }
  214. else if (pit2->cPathChars == 0)
  215. {
  216. nCmp = 1;
  217. }
  218. else if (pit1->cPathChars <= pit2->cPathChars)
  219. {
  220. nCmp = (short) StrCmpN(pszName1, pszName2, pit1->cPathChars-1);
  221. if ((nCmp == 0) && (pit1->cPathChars < pit2->cPathChars))
  222. {
  223. nCmp = -1;
  224. }
  225. }
  226. else
  227. {
  228. nCmp = (short) StrCmpN(pszName1, pszName2, pit2->cPathChars-1);
  229. if (nCmp == 0)
  230. {
  231. nCmp = 1;
  232. }
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. if (nCmp != 0)
  239. {
  240. return ResultFromShort(nCmp);
  241. }
  242. return ResultFromShort(lstrcmpi(pszName1 + pit1->cPathChars, pszName2 + pit2->cPathChars));
  243. }
  244. STDMETHODIMP CCabFolder::MessageSFVCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
  245. {
  246. return E_NOTIMPL;
  247. }
  248. //**********************************************************************
  249. //
  250. // CCabFolder::CreateViewObject
  251. //
  252. // Purpose:
  253. //
  254. // IShellbrowser calls this to create a ShellView
  255. // object
  256. //
  257. // Parameters:
  258. //
  259. // HWND hwndOwner -
  260. //
  261. // REFIID riid - interface ID
  262. //
  263. // void ** ppvObj - pointer to the Shellview object
  264. //
  265. // Return Value:
  266. //
  267. // NOERROR
  268. // E_OUTOFMEMORY
  269. // E_NOINTERFACE
  270. //
  271. //
  272. // Comments:
  273. //
  274. // ShellBrowser interface calls this to request the ShellFolder
  275. // to create a ShellView object
  276. //
  277. //********************************************************************
  278. STDMETHODIMP CCabFolder::CreateViewObject(HWND hwndOwner, REFIID riid, void **ppvObj)
  279. {
  280. HRESULT hr;
  281. if (riid == IID_IShellView)
  282. {
  283. SFV_CREATE sfvc = { 0 };
  284. sfvc.cbSize = sizeof(sfvc);
  285. sfvc.pshf = this;
  286. sfvc.psfvcb = this;
  287. hr = SHCreateShellFolderView(&sfvc, (IShellView **)ppvObj);
  288. }
  289. else
  290. {
  291. *ppvObj = NULL;
  292. hr = E_NOINTERFACE;
  293. }
  294. return hr;
  295. }
  296. // **************************************************************************************
  297. //
  298. // CCabFolder::GetAttributesOf
  299. //
  300. // Purpose
  301. //
  302. // Retrieves attributes of one of more file objects
  303. //
  304. // Parameters:
  305. //
  306. // UINT cidl - number of file objects
  307. // LPCITEMIDLIST *apidl - pointer to array of ITEMIDLIST
  308. // ULONG *rgfInOut - array of values that specify file object
  309. // attributes
  310. //
  311. //
  312. // Return Value:
  313. //
  314. // NOERROR
  315. //
  316. // Comments
  317. //
  318. // ***************************************************************************************
  319. STDMETHODIMP CCabFolder::GetAttributesOf(UINT cidl, LPCITEMIDLIST *apidl, ULONG *rgfInOut)
  320. {
  321. *rgfInOut &= SFGAO_CANCOPY;
  322. return NOERROR;
  323. }
  324. // **************************************************************************************
  325. //
  326. // CCabFolder::GetUIObjectOf
  327. //
  328. // Purpose
  329. //
  330. // Returns an interface that can be used to carry out actions on
  331. // the specified file objects or folders
  332. //
  333. // Parameters:
  334. //
  335. // HWND hwndOwner - handle of the Owner window
  336. //
  337. // UINT cidl - Number of file objects
  338. //
  339. // LPCITEMIDLIST *apidl - array of file object pidls
  340. //
  341. // REFIID - Identifier of interface to return
  342. //
  343. // UINT * prgfInOut - reserved
  344. //
  345. // void **ppvObj - address that receives interface pointer
  346. //
  347. // Return Value:
  348. //
  349. // E_INVALIDARG
  350. // E_NOINTERFACE
  351. // E_OUTOFMEMORY
  352. //
  353. // Comments
  354. // ***************************************************************************************
  355. STDMETHODIMP CCabFolder::GetUIObjectOf(HWND hwndOwner, UINT cidl, LPCITEMIDLIST *apidl,
  356. REFIID riid, UINT *prgfInOut, void **ppv)
  357. {
  358. *ppv = NULL;
  359. HRESULT hr = E_OUTOFMEMORY;
  360. if (riid == IID_IExtractIcon)
  361. {
  362. if (cidl != 1)
  363. {
  364. hr = E_INVALIDARG;
  365. }
  366. else
  367. {
  368. LPCABITEM pci = (LPCABITEM)*apidl;
  369. LPCWSTR pszName;
  370. WSTR_ALIGNED_STACK_COPY(&pszName, pci->szName);
  371. hr = SHCreateFileExtractIconW(pszName, pci->uFileAttribs, riid, ppv);
  372. }
  373. }
  374. else if (riid == IID_IContextMenu)
  375. {
  376. if (cidl < 1)
  377. {
  378. hr = E_INVALIDARG;
  379. }
  380. else
  381. {
  382. CCabItemMenu *pcim = new CCabItemMenu(hwndOwner, this, (LPCABITEM *)apidl, cidl);
  383. if (pcim)
  384. {
  385. pcim->AddRef(); // weak 0-based refcount
  386. hr = pcim->QueryInterface(riid, ppv);
  387. pcim->Release();
  388. }
  389. }
  390. }
  391. else if (riid == IID_IDataObject)
  392. {
  393. if (cidl < 1)
  394. {
  395. hr = E_INVALIDARG;
  396. }
  397. else
  398. {
  399. CCabObj *pco = new CCabObj(hwndOwner, this, (LPCABITEM *)apidl, cidl);
  400. if (pco)
  401. {
  402. pco->AddRef(); // weak 0-based refcount
  403. hr = pco->QueryInterface(riid, ppv);
  404. pco->Release();
  405. }
  406. }
  407. }
  408. else
  409. {
  410. hr = E_NOINTERFACE;
  411. }
  412. return hr;
  413. }
  414. //*****************************************************************************
  415. //
  416. // CCabFolder::GetDisplayNameOf
  417. //
  418. // Purpose:
  419. // Retrieves the display name for the specified file object or
  420. // subfolder.
  421. //
  422. //
  423. // Parameters:
  424. //
  425. // LPCITEMIDLIST pidl - pidl of the file object
  426. // DWORD dwReserved - Value of the type of display name to
  427. // return
  428. // LPSTRRET lpName - address holding the name returned
  429. //
  430. //
  431. // Comments:
  432. //
  433. //*****************************************************************************
  434. STDMETHODIMP CCabFolder::GetDisplayNameOf(LPCITEMIDLIST pidl, DWORD dwReserved, LPSTRRET lpName)
  435. {
  436. LPCABITEM pit = (LPCABITEM)pidl;
  437. GetNameOf(pit, lpName);
  438. return NOERROR;
  439. }
  440. STDMETHODIMP CCabFolder::SetNameOf(HWND hwndOwner, LPCITEMIDLIST pidl, LPCOLESTR lpszName, DWORD dwRes, LPITEMIDLIST *ppidlOut)
  441. {
  442. return E_NOTIMPL;
  443. }
  444. DEFINE_SCID(SCID_TYPE, PSGUID_STORAGE, PID_STG_STORAGETYPE);
  445. DEFINE_SCID(SCID_NAME, PSGUID_STORAGE, PID_STG_NAME);
  446. DEFINE_SCID(SCID_SIZE, PSGUID_STORAGE, PID_STG_SIZE);
  447. DEFINE_SCID(SCID_WRITETIME, PSGUID_STORAGE, PID_STG_WRITETIME);
  448. // the CV_COL_PATH column doesn't map to any well-known SCID types.
  449. // since nobody will need to get a hold of the data, just refer to a bogus
  450. // SCID. all this has to do is work within FindSCID.
  451. #define PSGUID_CabFolder {0x0CD7A5C0L, 0x9F37, 0x11CE, 0xAE, 0x65, 0x08, 0x00, 0x2B, 0x2E, 0x12, 0x62} // CLSID_CabFolder
  452. DEFINE_SCID(SCID_BOGUS, PSGUID_CabFolder, 0);
  453. struct _CVCOLINFO
  454. {
  455. UINT iColumn;
  456. UINT iTitle;
  457. UINT cchCol;
  458. UINT iFmt;
  459. const SHCOLUMNID* pscid;
  460. } s_aCVColInfo[] = {
  461. {CV_COL_NAME, IDS_CV_COL_NAME, 20, LVCFMT_LEFT, &SCID_NAME},
  462. {CV_COL_SIZE, IDS_CV_COL_SIZE, 10, LVCFMT_RIGHT, &SCID_SIZE},
  463. {CV_COL_TYPE, IDS_CV_COL_TYPE, 20, LVCFMT_LEFT, &SCID_TYPE},
  464. {CV_COL_MODIFIED, IDS_CV_COL_MODIFIED, 20, LVCFMT_LEFT, &SCID_WRITETIME},
  465. {CV_COL_PATH, IDS_CV_COL_PATH, 30, LVCFMT_LEFT, &SCID_BOGUS},
  466. };
  467. STDMETHODIMP CCabFolder::GetDetailsOf(LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
  468. {
  469. LPCABITEM pit = (LPCABITEM)pidl;
  470. TCHAR szTemp[MAX_PATH];
  471. if (iColumn >= CV_COL_MAX)
  472. {
  473. return E_NOTIMPL;
  474. }
  475. psd->str.uType = STRRET_CSTR;
  476. psd->str.cStr[0] = '\0';
  477. if (!pit)
  478. {
  479. TCHAR szTitle[MAX_PATH];
  480. LoadString(g_ThisDll.GetInstance(), s_aCVColInfo[iColumn].iTitle, szTitle, ARRAYSIZE(szTitle));
  481. StringToStrRet(szTitle, &(psd->str));
  482. psd->fmt = s_aCVColInfo[iColumn].iFmt;
  483. psd->cxChar = s_aCVColInfo[iColumn].cchCol;
  484. return S_OK;
  485. }
  486. HRESULT hr = S_OK;
  487. switch (iColumn)
  488. {
  489. case CV_COL_NAME:
  490. GetNameOf(pit, &psd->str);
  491. break;
  492. case CV_COL_PATH:
  493. GetPathOf(pit, &psd->str);
  494. break;
  495. case CV_COL_SIZE:
  496. {
  497. ULARGE_INTEGER ullSize = {pit->dwFileSize, 0};
  498. StrFormatKBSize(ullSize.QuadPart, szTemp, ARRAYSIZE(szTemp));
  499. StringToStrRet(szTemp, &(psd->str));
  500. break;
  501. }
  502. case CV_COL_TYPE:
  503. GetTypeOf(pit, &psd->str);
  504. break;
  505. case CV_COL_MODIFIED:
  506. {
  507. FILETIME ft, uft;
  508. if (DosDateTimeToFileTime(pit->uFileDate, pit->uFileTime, &ft) &&
  509. LocalFileTimeToFileTime(&ft, &uft)) // Apply timezone
  510. {
  511. SHFormatDateTime(&uft, NULL, szTemp, ARRAYSIZE(szTemp));
  512. StringToStrRet(szTemp, &(psd->str));
  513. }
  514. else
  515. {
  516. hr = E_FAIL;
  517. }
  518. }
  519. break;
  520. }
  521. return hr;
  522. }
  523. STDMETHODIMP CCabFolder::MapColumnToSCID(UINT iCol, SHCOLUMNID *pscid)
  524. {
  525. HRESULT hr;
  526. ZeroMemory(pscid, sizeof(*pscid));
  527. if (iCol < ARRAYSIZE(s_aCVColInfo))
  528. {
  529. *pscid = *s_aCVColInfo[iCol].pscid;
  530. hr = S_OK;
  531. }
  532. else
  533. hr = E_INVALIDARG;
  534. return hr;
  535. }
  536. STDAPI_(int) FindSCID(const _CVCOLINFO* pcol, UINT nCols, const SHCOLUMNID* pscid)
  537. {
  538. for (UINT i = 0; i < nCols; i++)
  539. {
  540. if (IsEqualSCID(*pscid, *pcol[i].pscid))
  541. return (int)i;
  542. }
  543. return -1;
  544. }
  545. STDMETHODIMP CCabFolder::GetDetailsEx(LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
  546. {
  547. HRESULT hr = E_FAIL;
  548. LPCABITEM pit = (LPCABITEM)pidl;
  549. int iCol = FindSCID(s_aCVColInfo, ARRAYSIZE(s_aCVColInfo), pscid);
  550. if (iCol >= 0)
  551. {
  552. switch (iCol)
  553. {
  554. case CV_COL_SIZE:
  555. pv->ullVal = pit->dwFileSize;
  556. pv->vt = VT_UI8;
  557. hr = S_OK;
  558. break;
  559. case CV_COL_MODIFIED:
  560. if (DosDateTimeToVariantTime(pit->uFileDate, pit->uFileTime, &pv->date))
  561. {
  562. pv->vt = VT_DATE;
  563. hr = S_OK;
  564. }
  565. break;
  566. default:
  567. {
  568. SHELLDETAILS sd;
  569. // Note that GetDetailsOf expects a relative pidl, since it is passed the SF itself.
  570. // The columnid includes the absolute pidl, though.z
  571. hr = GetDetailsOf(pidl, iCol, &sd);
  572. if (SUCCEEDED(hr))
  573. {
  574. hr = InitVariantFromStrRet(&sd.str, pidl, pv);
  575. }
  576. }
  577. }
  578. }
  579. return hr;
  580. }
  581. // *** IPersist methods ***
  582. STDMETHODIMP CCabFolder::GetClassID(CLSID *pclsid)
  583. {
  584. *pclsid = CLSID_CabFolder;
  585. return NOERROR;
  586. }
  587. // IPersistFolder
  588. STDMETHODIMP CCabFolder::Initialize(LPCITEMIDLIST pidl)
  589. {
  590. if (m_pidlHere)
  591. {
  592. ILFree(m_pidlHere);
  593. }
  594. m_pidlHere = ILClone(pidl); // copy the pidl
  595. return m_pidlHere ? S_OK : E_OUTOFMEMORY;
  596. }
  597. HRESULT CCabFolder::GetCurFolder(LPITEMIDLIST *ppidl)
  598. {
  599. if (m_pidlHere)
  600. {
  601. *ppidl = ILClone(m_pidlHere);
  602. return *ppidl ? NOERROR : E_OUTOFMEMORY;
  603. }
  604. *ppidl = NULL;
  605. return S_FALSE; // success but empty
  606. }
  607. //*****************************************************************************
  608. //
  609. // CCabFolder::CreateIDList
  610. //
  611. // Purpose:
  612. //
  613. // Creates an item identifier list for the objects in the namespace
  614. //
  615. //
  616. //*****************************************************************************
  617. LPITEMIDLIST CCabFolder::CreateIDList(LPCTSTR pszName, DWORD dwFileSize,
  618. UINT uFileDate, UINT uFileTime, UINT uFileAttribs)
  619. {
  620. // We'll assume no name is longer than MAX_PATH
  621. // Note the terminating NULL is already in the sizeof(CABITEM)
  622. BYTE bBuf[sizeof(CABITEM) + (sizeof(TCHAR) * MAX_PATH) + sizeof(WORD)];
  623. CABITEM *pci = (CABITEM*)bBuf;
  624. UINT uNameLen = lstrlen(pszName);
  625. if (uNameLen >= MAX_PATH)
  626. {
  627. uNameLen = MAX_PATH;
  628. }
  629. pci->wSize = (WORD)(sizeof(CABITEM) + (sizeof(TCHAR) * uNameLen));
  630. pci->dwFileSize = dwFileSize;
  631. pci->uFileDate = (USHORT)uFileDate;
  632. pci->uFileTime = (USHORT)uFileTime;
  633. pci->uFileAttribs = (USHORT)uFileAttribs & (FILE_ATTRIBUTE_READONLY|
  634. FILE_ATTRIBUTE_HIDDEN |
  635. FILE_ATTRIBUTE_SYSTEM |
  636. FILE_ATTRIBUTE_ARCHIVE);
  637. lstrcpynW(pci->szName, pszName, uNameLen+1);
  638. pci->cPathChars = 0;
  639. LPCTSTR psz = pszName;
  640. while (*psz)
  641. {
  642. if ((*psz == TEXT(':')) || (*psz == TEXT('/')) || (*psz == TEXT('\\')))
  643. {
  644. pci->cPathChars = (USHORT)(psz - pszName) + 1;
  645. }
  646. psz = CharNext(psz);
  647. }
  648. // Terminate the IDList
  649. *(WORD *)(((LPSTR)pci)+pci->wSize) = 0;
  650. return(ILClone((LPCITEMIDLIST)pci));
  651. }
  652. //*****************************************************************************
  653. //
  654. // CCabFolder::GetPath
  655. //
  656. // Purpose:
  657. //
  658. // Get the Path for the current pidl
  659. //
  660. // Parameters:
  661. //
  662. // LPSTR szPath - return pointer for path string
  663. //
  664. // Comments:
  665. //
  666. //*****************************************************************************
  667. BOOL CCabFolder::GetPath(LPTSTR szPath)
  668. {
  669. if (!m_pidlHere || !SHGetPathFromIDList(m_pidlHere, szPath))
  670. {
  671. *szPath = TEXT('\0');
  672. return FALSE;
  673. }
  674. #ifdef UNICODE
  675. // NOTE: we use GetShortPathName() to avoid losing characters during the
  676. // UNICODE->ANSI->UNICODE roundtrip while calling FDICopy()
  677. // NOTE: It is valid for GetShortPathName()'s src and dest pointers to be the same
  678. // If this fails, we'll just ignore the error and try to use the long path name
  679. GetShortPathName(szPath, szPath, MAX_PATH);
  680. #endif // UNICODE
  681. return(TRUE);
  682. }
  683. void CCabFolder::GetNameOf(LPCABITEM pit, LPSTRRET lpName)
  684. {
  685. #ifdef UNICODE
  686. lpName->uType = STRRET_WSTR;
  687. lpName->pOleStr = NULL;
  688. #else
  689. lpName->uType = STRRET_CSTR;
  690. lpName->cStr[0] = '\0';
  691. #endif
  692. LPCWSTR pszName;
  693. WSTR_ALIGNED_STACK_COPY(&pszName, pit->szName);
  694. SHFILEINFO sfi;
  695. if (SHGetFileInfo(pszName + pit->cPathChars, 0, &sfi, sizeof(sfi),
  696. SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME))
  697. {
  698. StringToStrRet(sfi.szDisplayName, lpName);
  699. }
  700. }
  701. void CCabFolder::GetPathOf(LPCABITEM pit, LPSTRRET lpName)
  702. {
  703. WCHAR szPath[MAX_PATH];
  704. ualstrcpynW(szPath, pit->szName, ARRAYSIZE(szPath));
  705. szPath[pit->cPathChars] = TEXT('\0');
  706. StringToStrRet(szPath, lpName);
  707. }
  708. void CCabFolder::GetTypeOf(LPCABITEM pit, LPSTRRET lpName)
  709. {
  710. #ifdef UNICODE
  711. lpName->uType = STRRET_WSTR;
  712. lpName->pOleStr = NULL;
  713. #else
  714. lpName->uType = STRRET_CSTR;
  715. lpName->cStr[0] = '\0';
  716. #endif
  717. LPCWSTR pszName;
  718. WSTR_ALIGNED_STACK_COPY(&pszName, pit->szName);
  719. SHFILEINFO sfi;
  720. if (SHGetFileInfo(pszName + pit->cPathChars, 0, &sfi, sizeof(sfi),
  721. SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME))
  722. {
  723. StringToStrRet(sfi.szTypeName, lpName);
  724. }
  725. }
  726. //*****************************************************************************
  727. //
  728. // CCabFolder::EnumToList
  729. //
  730. // Purpose:
  731. //
  732. // This notify callback is called by the FDI routines. It adds the
  733. // file object from the cab file to the list.
  734. //
  735. // Parameters:
  736. //
  737. //
  738. //
  739. // Comments:
  740. //
  741. //*****************************************************************************
  742. void CALLBACK CCabFolder::EnumToList(LPCTSTR pszFile, DWORD dwSize, UINT date,
  743. UINT time, UINT attribs, LPARAM lParam)
  744. {
  745. CCabFolder *pThis = (CCabFolder *)lParam;
  746. pThis->m_lItems.AddItem(pszFile, dwSize, date, time, attribs);
  747. }
  748. HRESULT CCabFolder::InitItems()
  749. {
  750. switch (m_lItems.GetState())
  751. {
  752. case CCabItemList::State_Init:
  753. return NOERROR;
  754. case CCabItemList::State_OutOfMem:
  755. return E_OUTOFMEMORY;
  756. case CCabItemList::State_UnInit:
  757. default:
  758. break;
  759. }
  760. // Force the list to initialize
  761. m_lItems.InitList();
  762. TCHAR szHere[MAX_PATH];
  763. // the m_pidl has been set to current dir
  764. // get the path to the current directory
  765. if (!GetPath(szHere))
  766. {
  767. return(E_UNEXPECTED);
  768. }
  769. CCabItems ciHere(szHere);
  770. if (!ciHere.EnumItems(EnumToList, (LPARAM)this))
  771. {
  772. return(E_UNEXPECTED);
  773. }
  774. return NOERROR;
  775. }
  776. HRESULT CabFolder_CreateInstance(REFIID riid, void **ppvObj)
  777. {
  778. HRESULT hres;
  779. *ppvObj = NULL;
  780. HINSTANCE hCabinetDll = LoadLibrary(TEXT("CABINET.DLL"));
  781. if (hCabinetDll)
  782. {
  783. FreeLibrary(hCabinetDll);
  784. CCabFolder *pfolder = new CCabFolder;
  785. if (pfolder)
  786. hres = pfolder->QueryInterface(riid, ppvObj);
  787. else
  788. hres = E_OUTOFMEMORY;
  789. }
  790. else
  791. hres = E_UNEXPECTED;
  792. return hres;
  793. }
  794. UINT CCabItemList::GetState()
  795. {
  796. if (m_uStep == 0)
  797. {
  798. if (m_dpaList)
  799. {
  800. return(State_Init);
  801. }
  802. return(State_OutOfMem);
  803. }
  804. return(State_UnInit);
  805. }
  806. BOOL CCabItemList::StoreItem(LPITEMIDLIST pidl)
  807. {
  808. if (pidl)
  809. {
  810. if (InitList() && DPA_InsertPtr(m_dpaList, 0x7fff, (LPSTR)pidl)>=0)
  811. {
  812. return(TRUE);
  813. }
  814. ILFree(pidl);
  815. }
  816. CleanList();
  817. return FALSE;
  818. }
  819. BOOL CCabItemList::AddItems(LPCABITEM *apit, UINT cpit)
  820. {
  821. for (UINT i=0; i<cpit; ++i)
  822. {
  823. if (!StoreItem(ILClone((LPCITEMIDLIST)apit[i])))
  824. {
  825. return FALSE;
  826. }
  827. }
  828. return(TRUE);
  829. }
  830. BOOL CCabItemList::AddItem(LPCTSTR pszName, DWORD dwFileSize,
  831. UINT uFileDate, UINT uFileTime, UINT uFileAttribs)
  832. {
  833. return(StoreItem(CCabFolder::CreateIDList(pszName, dwFileSize, uFileDate, uFileTime,
  834. uFileAttribs)));
  835. }
  836. int CCabItemList::FindInList(LPCTSTR pszName, DWORD dwFileSize,
  837. UINT uFileDate, UINT uFileTime, UINT uFileAttribs)
  838. {
  839. // TODO: Linear search for now; binary later
  840. for (int i=DPA_GetPtrCount(m_dpaList)-1; i>=0; --i)
  841. {
  842. LPCABITEM pcab = (*this)[i];
  843. // all guys in the dpa are WORD-aligned but copy out anyway.
  844. LPCWSTR pszNameCopy;
  845. WSTR_ALIGNED_STACK_COPY(&pszNameCopy, pcab->szName);
  846. if ((lstrcmpi(pszName, pszNameCopy) == 0) &&
  847. (pcab->dwFileSize == dwFileSize) &&
  848. (pcab->uFileDate == uFileDate) &&
  849. (pcab->uFileTime == uFileTime) &&
  850. (pcab->uFileAttribs == uFileAttribs))
  851. {
  852. break;
  853. }
  854. }
  855. return(i);
  856. }
  857. BOOL CCabItemList::InitList()
  858. {
  859. switch (GetState())
  860. {
  861. case State_Init:
  862. return(TRUE);
  863. case State_OutOfMem:
  864. return FALSE;
  865. case State_UnInit:
  866. default:
  867. m_dpaList = DPA_Create(m_uStep);
  868. m_uStep = 0;
  869. return(InitList());
  870. }
  871. }
  872. void CCabItemList::CleanList()
  873. {
  874. if (m_uStep != 0)
  875. {
  876. m_dpaList = NULL;
  877. m_uStep = 0;
  878. return;
  879. }
  880. if (!m_dpaList)
  881. {
  882. return;
  883. }
  884. for (int i=DPA_GetPtrCount(m_dpaList)-1; i>=0; --i)
  885. {
  886. ILFree((LPITEMIDLIST)((*this)[i]));
  887. }
  888. DPA_Destroy(m_dpaList);
  889. m_dpaList = NULL;
  890. }