dvutil.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 12k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. // Functions stolen from shdocvwutil.cpp
  2. #include "stdafx.h"
  3. #pragma hdrstop
  4. //#include "resource.h"
  5. //#include "..ids.h"
  6. //#include "deskstat.h"
  7. //#include "dutil.h"
  8. //#include <webcheck.h>
  9. //#include "sdspatch.h"
  10. //#include "dsubscri.h"
  11. #include <mluisupp.h>
  12. // Helper function to convert Ansi string to allocated BSTR
  13. #ifndef UNICODE
  14. BSTR AllocBStrFromString(LPTSTR psz)
  15. {
  16.     OLECHAR wsz[INFOTIPSIZE];  // assumes INFOTIPSIZE number of chars max
  17.     SHAnsiToUnicode(psz, wsz, ARRAYSIZE(wsz));
  18.     return SysAllocString(wsz);
  19. }
  20. #endif
  21. #ifdef POSTSPLIT
  22. //+-----------------------------------------------------------------
  23. //
  24. // Helper function for getting the TopLeft point of an element from
  25. //      mshtml.dll, and have the point reported in inside relative
  26. //      coordinates (inside margins, borders and padding.)
  27. //-----------------------------------------------------------------
  28. HRESULT CSSOM_TopLeft(IHTMLElement * pIElem, POINT * ppt) 
  29. {
  30.     HRESULT       hr = E_FAIL;
  31.     IHTMLStyle    *pistyle;
  32.     if (SUCCEEDED(pIElem->get_style(&pistyle)) && pistyle) {
  33.         VARIANT var = {0};
  34.         if (SUCCEEDED(pistyle->get_top(&var)) && var.bstrVal) {
  35.             ppt->y = StrToIntW(var.bstrVal);
  36.             VariantClear(&var);
  37.             if (SUCCEEDED(pistyle->get_left(&var)) && var.bstrVal) {
  38.                 ppt->x = StrToIntW(var.bstrVal);
  39.                 VariantClear(&var);
  40.                 hr = S_OK;
  41.             }
  42.         }
  43.         pistyle->Release();
  44.     }
  45.     return hr;
  46. }
  47. HRESULT GetHTMLElementStrMember(IHTMLElement *pielem, LPTSTR pszName, DWORD cchSize, BSTR bstrMember)
  48. {
  49.     HRESULT hr;
  50.     VARIANT var = {0};
  51.     if (!pielem)
  52.         hr = E_INVALIDARG;
  53.     else if (SUCCEEDED(hr = pielem->getAttribute(bstrMember, TRUE, &var)))
  54.     {
  55.         if ((VT_BSTR == var.vt) && (var.bstrVal))
  56.         {
  57. #ifdef UNICODE          
  58.             StrCpyNW(pszName, (LPCWSTR)var.bstrVal, cchSize);
  59. #else // UNICODE
  60.             SHUnicodeToAnsi((BSTR)var.bstrVal, pszName, cchSize);
  61. #endif // UNICODE
  62.         }
  63.         else
  64.             hr = E_FAIL; // Try VariantChangeType?????
  65.         VariantClear(&var);
  66.     }
  67.     return hr;
  68. }
  69. /******************************************************************
  70.     FUNCTION: IElemCheckForExistingSubscription()
  71.     RETURN VALUE:
  72.     S_OK    - if the IHTMLElement points to a TAG that has a "subscribed_url" property
  73.               that is subscribed. 
  74.     S_FALSE - if the IHTMLElement points to a TAG that has a
  75.               "subscribed_url" property but the URL is not subscribed.
  76.     E_FAIL  - if the IHTMLElement points to a TAG that does not
  77.               have a  "subscribed_url" property.
  78. ******************************************************************/
  79. HRESULT IElemCheckForExistingSubscription(IHTMLElement *pielem)
  80. {
  81.     HRESULT hr = E_FAIL;
  82.     TCHAR szHTMLElementName[MAX_URL_STRING];
  83.     if (!pielem)
  84.         return E_INVALIDARG;
  85.     if (SUCCEEDED(GetHTMLElementStrMember(pielem, szHTMLElementName, SIZECHARS(szHTMLElementName), (BSTR)(s_sstrSubSRCMember.wsz))))
  86.         hr = (CheckForExistingSubscription(szHTMLElementName) ? S_OK : S_FALSE);
  87.     return hr;
  88. }
  89. HRESULT IElemCloseDesktopComp(IHTMLElement *pielem)
  90. {
  91.     HRESULT hr;
  92.     TCHAR szHTMLElementID[MAX_URL_STRING];
  93.     ASSERT(pielem);
  94.     if (pielem &&
  95.         SUCCEEDED(hr = GetHTMLElementStrMember(pielem, szHTMLElementID, SIZECHARS(szHTMLElementID), (BSTR)(s_sstrIDMember.wsz))))
  96.     {
  97.         hr = UpdateComponentFlags(szHTMLElementID, COMP_CHECKED | COMP_UNCHECKED, COMP_UNCHECKED) ? S_OK : E_FAIL;
  98.         ASSERT(SUCCEEDED(hr));
  99.         if (SUCCEEDED(hr))
  100.             REFRESHACTIVEDESKTOP();
  101.     }
  102.     return hr;
  103. }
  104. HRESULT IElemGetSubscriptionsDialog(IHTMLElement *pielem, HWND hwnd)
  105. {
  106.     HRESULT hr;
  107.     TCHAR szHTMLElementName[MAX_URL_STRING];
  108.     ASSERT(pielem);
  109.     if (SUCCEEDED(hr = GetHTMLElementStrMember(pielem, szHTMLElementName, SIZECHARS(szHTMLElementName), (BSTR)(s_sstrSubSRCMember.wsz))))
  110.     {
  111.         ASSERT(CheckForExistingSubscription(szHTMLElementName)); // We should not have gotten this far.
  112.         hr = ShowSubscriptionProperties(szHTMLElementName, hwnd);
  113.     }
  114.     return hr;
  115. }
  116. HRESULT IElemSubscribeDialog(IHTMLElement *pielem, HWND hwnd)
  117. {
  118.     HRESULT hr;
  119.     TCHAR szHTMLElementName[MAX_URL_STRING];
  120.     ASSERT(pielem);
  121.     hr = GetHTMLElementStrMember(pielem, szHTMLElementName, SIZECHARS(szHTMLElementName), (BSTR)(s_sstrSubSRCMember.wsz));
  122.     if (SUCCEEDED(hr))
  123.     {
  124.         ASSERT(!CheckForExistingSubscription(szHTMLElementName)); // We should not have gotten this far.
  125.         hr = CreateSubscriptionsWizard(SUBSTYPE_DESKTOPURL, szHTMLElementName, NULL, hwnd);
  126.     }
  127.     return hr;
  128. }
  129. HRESULT IElemUnsubscribe(IHTMLElement *pielem)
  130. {
  131.     HRESULT hr;
  132.     TCHAR szHTMLElementName[MAX_URL_STRING];
  133.     ASSERT(pielem);
  134.     hr = GetHTMLElementStrMember(pielem, szHTMLElementName, SIZECHARS(szHTMLElementName), (BSTR)(s_sstrSubSRCMember.wsz));
  135.     if (SUCCEEDED(hr))
  136.     {
  137.         ASSERT(CheckForExistingSubscription(szHTMLElementName)); // We should not have gotten this far.
  138.         hr = DeleteFromSubscriptionList(szHTMLElementName) ? S_OK : S_FALSE;
  139.     }
  140.     return hr;
  141. }
  142. HRESULT IElemUpdate(IHTMLElement *pielem)
  143. {
  144.     HRESULT hr;
  145.     TCHAR szHTMLElementName[MAX_URL_STRING];
  146.     ASSERT(pielem);
  147.     hr = GetHTMLElementStrMember(pielem, szHTMLElementName, SIZECHARS(szHTMLElementName), (BSTR)(s_sstrSubSRCMember.wsz));
  148.     if (SUCCEEDED(hr))
  149.     {
  150.         ASSERT(CheckForExistingSubscription(szHTMLElementName)); // We should not have gotten this far.
  151.         hr = UpdateSubscription(szHTMLElementName) ? S_OK : S_FALSE;
  152.     }
  153.     return hr;
  154. }
  155. HRESULT ShowSubscriptionProperties(LPCTSTR pszUrl, HWND hwnd)
  156. {
  157.     HRESULT hr;
  158.     ISubscriptionMgr *psm;
  159.     if (SUCCEEDED(hr = CoCreateInstance(CLSID_SubscriptionMgr, NULL,
  160.                           CLSCTX_INPROC_SERVER, IID_ISubscriptionMgr,
  161.                           (void**)&psm)))
  162.     {
  163.         WCHAR wzURL[MAX_URL_STRING];
  164.         LPCWSTR pwzURL = wzURL;
  165. #ifndef UNICODE
  166.         SHAnsiToUnicode(pszUrl, wzURL, ARRAYSIZE(wzURL));
  167. #else // UNICODE
  168.         pwzURL = pszUrl;
  169. #endif // UNICODE
  170.         hr = psm->ShowSubscriptionProperties(pwzURL, hwnd);
  171.         psm->Release();
  172.     }
  173.     return hr;
  174. }
  175. HRESULT CreateSubscriptionsWizard(SUBSCRIPTIONTYPE subType, LPCTSTR pszUrl, SUBSCRIPTIONINFO *pInfo, HWND hwnd)
  176. {
  177.     HRESULT hr;
  178.     ISubscriptionMgr *psm;
  179.     if (SUCCEEDED(hr = CoCreateInstance(CLSID_SubscriptionMgr, NULL,
  180.                           CLSCTX_INPROC_SERVER, IID_ISubscriptionMgr,
  181.                           (void**)&psm)))
  182.     {
  183.         WCHAR wzURL[MAX_URL_STRING];
  184.         LPCWSTR pwzURL = wzURL;
  185. #ifndef UNICODE
  186.         SHAnsiToUnicode(pszUrl, wzURL, ARRAYSIZE(wzURL));
  187. #else // UNICODE
  188.         pwzURL = pszUrl;
  189. #endif // UNICODE
  190.         hr = psm->CreateSubscription(hwnd, pwzURL, pwzURL, CREATESUBS_ADDTOFAVORITES, subType, pInfo);
  191.         psm->UpdateSubscription(pwzURL);
  192.         psm->Release();
  193.     }
  194.     return hr;
  195. }
  196. HRESULT ShowComponentSettings(void)
  197. {
  198.     TCHAR szCompSettings[MAX_PATH];
  199.     MLLoadString(IDS_DESKTOPWEBSETTINGS, szCompSettings, ARRAYSIZE(szCompSettings));
  200.     SHRunControlPanel(szCompSettings, NULL);
  201.     return S_OK;
  202. }
  203. // return an aliased pointer to the pidl in this variant (that is why it is const)
  204. // see VariantToIDList
  205. LPCITEMIDLIST VariantToConstIDList(const VARIANT *pv)
  206. {
  207.     if (pv == NULL)
  208.         return NULL;
  209.     LPCITEMIDLIST pidl = NULL;
  210.     VARIANT v;
  211.     if (pv->vt == (VT_BYREF | VT_VARIANT) && pv->pvarVal)
  212.         v = *pv->pvarVal;
  213.     else
  214.         v = *pv;
  215.     switch (v.vt)
  216.     {
  217.     case VT_UI4:
  218.         // HACK in process case, avoid the use of this if possible
  219.         // BUGBUG raymondc Sundown this is still wrong
  220.         pidl = (LPCITEMIDLIST)(UINT_PTR)v.ullVal;
  221.         ASSERT(pidl);
  222.         break;
  223.     case VT_ARRAY | VT_UI1:
  224.         pidl = (LPCITEMIDLIST)v.parray->pvData;   // alias: PIDL encoded
  225.         break;
  226.     case VT_DISPATCH | VT_BYREF:
  227.         if (v.ppdispVal == NULL)
  228.             break;
  229.         v.pdispVal = *v.ppdispVal;
  230.         // fall through...
  231.     case VT_DISPATCH:
  232.         CSDFldrItem *pcfi;
  233.         if (v.pdispVal && SUCCEEDED(v.pdispVal->QueryInterface(IID_ICSDFolderItem, (void **)&pcfi)))
  234.         {
  235.             pidl = pcfi->Pidl();    // return alias!
  236.             pcfi->Release();
  237.         }
  238.         break;
  239.     }
  240.     return pidl;
  241. }
  242. // ALLOCATES pidl from a variant, client should free this
  243. // see VariantToConstIDList
  244. LPITEMIDLIST VariantToIDList(const VARIANT *pv)
  245. {
  246.     LPITEMIDLIST pidl = NULL;
  247.     VARIANT v;
  248.     
  249.     if (!pv)
  250.         return NULL;
  251.     if (pv->vt == (VT_BYREF | VT_VARIANT) && pv->pvarVal)
  252.         v = *pv->pvarVal;
  253.     else
  254.         v = *pv;
  255.     switch (v.vt)
  256.     {
  257.     case VT_I2:
  258.         v.lVal = (long)v.iVal;
  259.         // Fall through
  260.     case VT_I4:
  261.         pidl = SHCloneSpecialIDList(HWND_DESKTOP, v.lVal, TRUE);
  262.         break;
  263.     case VT_BSTR:
  264.         pidl = ILCreateFromPathW(v.bstrVal);
  265.         break;
  266.     default:
  267.         // use above code
  268.         LPCITEMIDLIST pidlToCopy = VariantToConstIDList(pv);
  269.         if (pidlToCopy)
  270.             pidl = ILClone(pidlToCopy);
  271.         break;
  272.     }
  273.     return pidl;
  274. }
  275. BOOL GetInfoTip(IShellFolder* psf, LPCITEMIDLIST pidl, LPTSTR pszText, int cchTextMax)
  276. {
  277.     BOOL fRet = FALSE;
  278.     *pszText = 0;   // empty for failure
  279.     if (pidl)
  280.     {
  281.         IQueryInfo *pqi;
  282.         if (SUCCEEDED(psf->GetUIObjectOf(NULL, 1, &pidl, IID_IQueryInfo, NULL, (void**)&pqi)))
  283.         {
  284.             WCHAR *pwszTip;
  285.             pqi->GetInfoTip(0, &pwszTip);
  286.             if (pwszTip)
  287.             {
  288.                 fRet = TRUE;
  289.                 SHUnicodeToTChar(pwszTip, pszText, cchTextMax);
  290.                 SHFree(pwszTip);
  291.             }
  292.             pqi->Release();
  293.         }
  294.     }
  295.     return fRet;
  296. }
  297. BSTR StrRetToBStr(LPCITEMIDLIST pidl, STRRET *pstr)
  298. {
  299.     OLECHAR wszPath[MAX_PATH];
  300.     switch (pstr->uType)
  301.     {
  302.     case STRRET_WSTR:
  303.         StrRetToUnicode(wszPath, ARRAYSIZE(wszPath), pidl, pstr);
  304.         break;
  305.     case STRRET_OFFSET:
  306.         SHAnsiToUnicode((LPSTR)pidl + pstr->uOffset, wszPath, ARRAYSIZE(wszPath));
  307.         break;
  308.     case STRRET_CSTR:
  309.         SHAnsiToUnicode(pstr->cStr, wszPath, ARRAYSIZE(wszPath));
  310.         break;
  311.     default:
  312.         wszPath[0] = '';
  313.     }
  314.     return SysAllocString(wszPath);
  315. }
  316. DWORD StrRetToUnicode(LPWSTR wzPath, DWORD cbPathLen, LPCITEMIDLIST pidl, STRRET *pstr)
  317. {
  318.     DWORD cchResult = 0;
  319.     if (cbPathLen)
  320.     {
  321.         switch (pstr->uType)
  322.         {
  323.         case STRRET_WSTR:
  324.             {
  325.                 OLECHAR * pwsz = pstr->pOleStr;
  326.                 StrCpyNW(wzPath, pwsz, cbPathLen);
  327.                 cchResult = lstrlenW(wzPath);
  328.                 SHFree(pstr->pOleStr);
  329.             }
  330.             break;
  331.         case STRRET_OFFSET:
  332.             cchResult = SHAnsiToUnicode((LPSTR)pidl + pstr->uOffset, wzPath, cbPathLen);
  333.             break;
  334.         case STRRET_CSTR:
  335.             cchResult = SHAnsiToUnicode(pstr->cStr, wzPath, cbPathLen);
  336.             break;
  337.         default:
  338.             wzPath[0] = L'';
  339.             cchResult = 1;
  340.         }
  341.     }
  342.     return cchResult;
  343. }
  344. #ifndef UNICODE
  345. BSTR SysAllocStringA(LPCSTR pszAnsiStr)
  346. {
  347.     OLECHAR FAR * bstrOut = NULL;
  348.     LPWSTR pwzTemp;
  349.     UINT cchSize = (lstrlenA(pszAnsiStr) + 2);  // Count of characters
  350.     if (!pszAnsiStr)
  351.         return NULL;    // What the hell do you expect?
  352.     pwzTemp = (LPWSTR) LocalAlloc(LPTR, cchSize * sizeof(WCHAR));
  353.     if (pwzTemp)
  354.     {
  355.         SHAnsiToUnicode(pszAnsiStr, pwzTemp, cchSize);
  356.         bstrOut = SysAllocString(pwzTemp);
  357.         LocalFree(pwzTemp);
  358.     }
  359.     return bstrOut;
  360. }
  361. #endif
  362. #if 0 // BUGBUG - g_dfs.DefRevCount - raymondc - incomplete
  363. void SaveDefaultFolderSettings()
  364. {
  365.     HKEY hkCabStreams;
  366.     g_dfs.dwDefRevCount++;
  367.     if (RegOpenKey(g_hkeyExplorer, c_szStreams, &hkCabStreams) == ERROR_SUCCESS)
  368.     {
  369.         RegSetValueEx(hkCabStreams, TEXT("Settings"), 0L, REG_BINARY, (LPBYTE)&g_dfs, SIZEOF(g_dfs));
  370.         RegCloseKey(hkCabStreams);
  371.     }
  372. }
  373. #else
  374. void SaveDefaultFolderSettings() // BUGBUG raymondc
  375. {
  376. }
  377. #endif
  378. #endif // POSTSPLIT