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.

147 lines
4.0 KiB

  1. #include <windows.h>
  2. #include <commctrl.h>
  3. #include <stdio.h>
  4. #include <memory.h>
  5. #include <subsmgr.h>
  6. #include <mstask.h>
  7. #include <wininet.h>
  8. #include "cdfsubs.hpp"
  9. // CDFAutoSubscribe
  10. //
  11. // Create the cache entry and subscribe to the channel.
  12. //
  13. // APPCOMPAT: Right now, if you create the cache entry for the CDF, subscribing
  14. // to the channel doesn't work properly. Updating the channel will fail.
  15. //
  16. BOOL WINAPI CDFAutoSubscribe(HWND hwnd, LPSTR szFriendlyName, LPSTR szCDFUrl,
  17. LPSTR szCDFFilePath, BOOL bSilent)
  18. {
  19. BOOL bRet = FALSE;
  20. if (CreateCDFCacheEntry(szCDFUrl, szCDFFilePath)) {
  21. if (SubscribeChannel(hwnd, szFriendlyName, szCDFUrl, bSilent)) {
  22. bRet = TRUE;
  23. }
  24. }
  25. return bRet;
  26. }
  27. //
  28. // CreateCDFCacheEntry
  29. //
  30. BOOL CreateCDFCacheEntry(LPSTR szCDFUrl, LPSTR szLocalFileName)
  31. {
  32. BOOL bRet;
  33. FILETIME ftExpireTime;
  34. FILETIME ftLastMod;
  35. TCHAR achFileName[MAX_PATH];
  36. bRet = FALSE;
  37. GetSystemTimeAsFileTime(&ftLastMod);
  38. ftExpireTime.dwLowDateTime = (DWORD)0;
  39. ftExpireTime.dwHighDateTime = (DWORD)0;
  40. if (CreateUrlCacheEntry(szCDFUrl, 0, NULL, achFileName, 0)) {
  41. CopyFile(szLocalFileName, achFileName, FALSE);
  42. if (CommitUrlCacheEntry(szCDFUrl, achFileName, ftExpireTime,
  43. ftLastMod, 0, NULL, 0, NULL, 0)) {
  44. bRet = TRUE;
  45. }
  46. }
  47. return bRet;
  48. }
  49. //
  50. // SubscribeChannel
  51. //
  52. BOOL SubscribeChannel(HWND hwnd, LPSTR szName, LPSTR szURL, BOOL bSilent)
  53. {
  54. ISubscriptionMgr *pISubscriptionMgr = NULL;
  55. DWORD dwFlags = 0;
  56. BOOL bRet = FALSE;
  57. HRESULT hr = S_OK;
  58. SUBSCRIPTIONTYPE subsType = SUBSTYPE_CHANNEL;
  59. SUBSCRIPTIONINFO subsInfo;
  60. LPWSTR wzName = NULL;
  61. LPWSTR wzURL = NULL;
  62. if (FAILED(Ansi2Unicode(szName, &wzName))) {
  63. goto Exit;
  64. }
  65. if (FAILED(Ansi2Unicode(szURL, &wzURL))) {
  66. goto Exit;
  67. }
  68. hr = CoCreateInstance(CLSID_SubscriptionMgr, NULL, CLSCTX_INPROC_SERVER,
  69. IID_ISubscriptionMgr, (void **)&pISubscriptionMgr);
  70. if (FAILED(hr)) {
  71. goto Exit;
  72. }
  73. ZeroMemory(&subsInfo, sizeof(SUBSCRIPTIONINFO));
  74. subsInfo.cbSize = sizeof(SUBSCRIPTIONINFO);
  75. subsInfo.fUpdateFlags = SUBSINFO_CHANNELFLAGS | SUBSINFO_TASKFLAGS | SUBSINFO_GLEAM;
  76. subsInfo.bGleam = TRUE;
  77. subsInfo.fChannelFlags |= CHANNEL_AGENT_PRECACHE_ALL |
  78. CHANNEL_AGENT_DYNAMIC_SCHEDULE;
  79. subsInfo.fTaskFlags = TASK_FLAG_START_ONLY_IF_IDLE |
  80. TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET;
  81. dwFlags = CREATESUBS_ADDTOFAVORITES | CREATESUBS_SOFTWAREUPDATE;
  82. if (bSilent) {
  83. dwFlags |= CREATESUBS_NOUI;
  84. }
  85. hr = pISubscriptionMgr->CreateSubscription(hwnd, wzURL, wzName,
  86. dwFlags, subsType, &subsInfo);
  87. if (hr == S_OK) {
  88. bRet = TRUE;
  89. }
  90. pISubscriptionMgr->Release();
  91. Exit:
  92. if (wzName) {
  93. delete wzName;
  94. }
  95. if (wzURL) {
  96. delete wzURL;
  97. }
  98. return bRet;
  99. }
  100. // Ansi2Unicode: Convert ANSI strings to Unicode
  101. // Ripped from urlmon\download\helpers.cxx
  102. HRESULT Ansi2Unicode(const char * src, wchar_t **dest)
  103. {
  104. if ((src == NULL) || (dest == NULL))
  105. return E_INVALIDARG;
  106. // find out required buffer size and allocate it
  107. int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, -1, NULL, 0);
  108. *dest = new WCHAR [len*sizeof(WCHAR)];
  109. if (!*dest)
  110. return E_OUTOFMEMORY;
  111. // Do the actual conversion.
  112. if ((MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, -1, *dest,
  113. len*sizeof(wchar_t))) != 0)
  114. return S_OK;
  115. else
  116. return E_FAIL;
  117. }