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.

463 lines
11 KiB

  1. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  2. //
  3. // utils.cpp
  4. //
  5. // Misc routines.
  6. //
  7. // History:
  8. //
  9. // 6/25/97 tnoonan Created.
  10. //
  11. ////////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Includes
  14. //
  15. #include "stdinc.h"
  16. #include "cdfidl.h"
  17. #include "persist.h"
  18. #include "cdfview.h"
  19. #include "xmlutil.h"
  20. #include "bindstcb.h"
  21. #include "dll.h"
  22. #include "resource.h"
  23. #include "chanapi.h"
  24. #include <mluisupp.h>
  25. typedef struct _tagDialogData {
  26. LPCWSTR pszwURL;
  27. IXMLDocument* pIXMLDocument;
  28. int nProgress;
  29. } DIALOGDATA;
  30. HRESULT
  31. GetURLFromIni(
  32. LPCTSTR pszPath,
  33. BSTR* pbstrURL
  34. )
  35. {
  36. ASSERT(pszPath);
  37. ASSERT(pbstrURL);
  38. HRESULT hr = E_FAIL;
  39. LPTSTR szFile = TSTR_INI_FILE;
  40. LPTSTR szSection = TSTR_INI_SECTION;
  41. LPTSTR szKey = TSTR_INI_URL;
  42. TCHAR szURL[INTERNET_MAX_URL_LENGTH];
  43. TCHAR szPath[MAX_PATH];
  44. StrCpyN(szPath, pszPath, ARRAYSIZE(szPath) - ARRAYSIZE(TSTR_INI_FILE));
  45. StrCat(szPath, szFile);
  46. if (GetPrivateProfileString(szSection, szKey, TEXT(""), szURL,
  47. INTERNET_MAX_URL_LENGTH, szPath))
  48. {
  49. WCHAR wszURL[INTERNET_MAX_URL_LENGTH];
  50. if (SHTCharToUnicode(szURL, wszURL, ARRAYSIZE(wszURL)))
  51. {
  52. *pbstrURL = SysAllocString(wszURL);
  53. hr = S_OK;
  54. }
  55. }
  56. return hr;
  57. }
  58. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  59. //
  60. // *** Name ***
  61. //
  62. //
  63. // Description:
  64. //
  65. //
  66. // Parameters:
  67. //
  68. //
  69. // Return:
  70. //
  71. //
  72. // Comments:
  73. //
  74. //
  75. ////////////////////////////////////////////////////////////////////////////////
  76. HRESULT
  77. GetNameAndURLAndSubscriptionInfo(
  78. LPCTSTR pszPath,
  79. BSTR* pbstrName,
  80. BSTR* pbstrURL,
  81. SUBSCRIPTIONINFO* psi
  82. )
  83. {
  84. ASSERT(pszPath);
  85. ASSERT(pbstrName);
  86. ASSERT(pbstrURL);
  87. HRESULT hr;
  88. *pbstrName = NULL;
  89. *pbstrURL = NULL;
  90. CCdfView* pCCdfView = new CCdfView;
  91. if (pCCdfView)
  92. {
  93. WCHAR wszPath[MAX_PATH];
  94. if (SHTCharToUnicode(pszPath, wszPath, ARRAYSIZE(wszPath)))
  95. {
  96. hr = pCCdfView->Load(wszPath, 0);
  97. if (SUCCEEDED(hr))
  98. {
  99. IXMLDocument* pIXMLDocument;
  100. hr = pCCdfView->ParseCdf(NULL, &pIXMLDocument, PARSE_LOCAL);
  101. if (SUCCEEDED(hr))
  102. {
  103. ASSERT(pIXMLDocument);
  104. IXMLElement* pIXMLElement;
  105. LONG nIndex;
  106. hr = XML_GetFirstChannelElement(pIXMLDocument,
  107. &pIXMLElement, &nIndex);
  108. if (SUCCEEDED(hr))
  109. {
  110. ASSERT(pIXMLElement);
  111. *pbstrName = XML_GetAttribute(pIXMLElement, XML_TITLE);
  112. if (*pbstrName && 0 == **pbstrName)
  113. {
  114. SysFreeString(*pbstrName);
  115. *pbstrName = NULL;
  116. }
  117. *pbstrURL = XML_GetAttribute(pIXMLElement, XML_SELF);
  118. if (*pbstrURL && 0 == **pbstrURL)
  119. {
  120. SysFreeString(*pbstrName);
  121. *pbstrURL = NULL;
  122. }
  123. if (psi)
  124. XML_GetSubscriptionInfo(pIXMLElement, psi);
  125. pIXMLElement->Release();
  126. }
  127. pIXMLDocument->Release();
  128. }
  129. }
  130. }
  131. if (NULL == *pbstrName)
  132. {
  133. TCHAR szName[MAX_PATH];
  134. WCHAR wszName[MAX_PATH];
  135. StrCpyN(szName, pszPath, ARRAYSIZE(szName));
  136. PathStripPath(szName);
  137. if (SHTCharToUnicode(szName, wszName, ARRAYSIZE(wszName)))
  138. *pbstrName = SysAllocString(wszName);
  139. }
  140. if (NULL == *pbstrURL)
  141. {
  142. GetURLFromIni(pszPath, pbstrURL);
  143. }
  144. hr = (NULL != *pbstrName) && (NULL != **pbstrName) &&
  145. (NULL != *pbstrURL) && (NULL != **pbstrURL) ? S_OK : E_FAIL;
  146. if (FAILED(hr))
  147. {
  148. if (NULL != *pbstrName)
  149. {
  150. SysFreeString(*pbstrName);
  151. *pbstrName = NULL;
  152. }
  153. if (NULL != *pbstrURL)
  154. {
  155. SysFreeString(*pbstrURL);
  156. *pbstrURL = NULL;
  157. }
  158. }
  159. pCCdfView->Release();
  160. }
  161. else
  162. {
  163. hr = E_OUTOFMEMORY;
  164. }
  165. return hr;
  166. }
  167. int CDFMessageBox(HWND hwnd, UINT idTextFmt, UINT idCaption, UINT uType, ...)
  168. {
  169. TCHAR szCaption[80];
  170. TCHAR szTextFmt[256];
  171. LPTSTR pszText;
  172. int result;
  173. va_list va;
  174. va_start(va, uType);
  175. MLLoadString(idTextFmt, szTextFmt, ARRAYSIZE(szTextFmt));
  176. MLLoadString(idCaption, szCaption, ARRAYSIZE(szCaption));
  177. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  178. szTextFmt, 0, 0, (LPTSTR)&pszText, 0, &va);
  179. result = MessageBox(hwnd, pszText, szCaption, uType);
  180. LocalFree((HLOCAL)pszText);
  181. return result;
  182. }
  183. INT_PTR RefreshDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  184. {
  185. BOOL fRet = TRUE;
  186. DIALOGDATA* pdd = (DIALOGDATA*) GetWindowLongPtr(hDlg, DWLP_USER);
  187. switch (msg)
  188. {
  189. case WM_INITDIALOG:
  190. SetWindowLongPtr(hDlg, DWLP_USER, lParam);
  191. pdd = (DIALOGDATA*)lParam;
  192. IMoniker* pIMoniker;
  193. if (SUCCEEDED(CreateURLMoniker(NULL, pdd->pszwURL, &pIMoniker)))
  194. {
  195. ASSERT(pIMoniker);
  196. IBindCtx* pIBindCtx;
  197. IBindStatusCallback* pIBindStatusCallback =
  198. (IBindStatusCallback*) new CBindStatusCallback2(hDlg);
  199. if (pIBindStatusCallback)
  200. {
  201. if (SUCCEEDED(CreateBindCtx(0, &pIBindCtx)) )
  202. {
  203. ASSERT(pIBindCtx);
  204. if (SUCCEEDED(RegisterBindStatusCallback(pIBindCtx,
  205. pIBindStatusCallback,
  206. NULL, 0)))
  207. {
  208. IPersistMoniker* pIPersistMoniker;
  209. if (SUCCEEDED(pdd->pIXMLDocument->QueryInterface(
  210. IID_IPersistMoniker,
  211. (void**)&pIPersistMoniker)))
  212. {
  213. ASSERT(pIPersistMoniker);
  214. pIPersistMoniker->Load(FALSE, pIMoniker, pIBindCtx,
  215. 0);
  216. pIPersistMoniker->Release();
  217. }
  218. }
  219. pIBindCtx->Release();
  220. }
  221. pIBindStatusCallback->Release();
  222. }
  223. pIMoniker->Release();
  224. }
  225. Animate_Open(GetDlgItem(hDlg, IDC_DOWNLOADANIMATE), IDA_DOWNLOAD);
  226. Animate_Play(GetDlgItem(hDlg, IDC_DOWNLOADANIMATE), 0, -1, -1);
  227. //SendMessage(GetDlgItem(hDlg, IDC_DOWNLOADPROGRESS), PBM_SETRANGE32, 0, 100);
  228. pdd->nProgress = 0;
  229. break;
  230. case WM_COMMAND:
  231. switch (LOWORD(wParam))
  232. {
  233. case IDCANCEL:
  234. EndDialog(hDlg, FALSE);
  235. break;
  236. case DOWNLOAD_PROGRESS:
  237. SendMessage(GetDlgItem(hDlg, IDC_DOWNLOADPROGRESS), PBM_DELTAPOS,
  238. pdd->nProgress += 2, 0);
  239. break;
  240. case DOWNLOAD_COMPLETE:
  241. if (lParam)
  242. {
  243. SendMessage(GetDlgItem(hDlg, IDC_DOWNLOADPROGRESS), PBM_SETPOS,
  244. 100, 0);
  245. XML_DownloadImages(pdd->pIXMLDocument);
  246. }
  247. EndDialog(hDlg, lParam);
  248. break;
  249. }
  250. break;
  251. case WM_CLOSE:
  252. EndDialog(hDlg, FALSE);
  253. break;
  254. case WM_DESTROY:
  255. break;
  256. default:
  257. fRet = FALSE;
  258. }
  259. return fRet;
  260. }
  261. BOOL DownloadCdfUI(HWND hwnd, LPCWSTR pszwURL, IXMLDocument* pIXMLDocument)
  262. {
  263. BOOL fRet = FALSE;
  264. DIALOGDATA dd;
  265. dd.pszwURL = pszwURL;
  266. dd.pIXMLDocument = pIXMLDocument;
  267. if (hwnd)
  268. {
  269. DWORD dwCacheCount = g_dwCacheCount;
  270. INT_PTR nRes = DialogBoxParam(MLGetHinst(),
  271. (LPWSTR)MAKEINTRESOURCE(IDD_CHANNELREFRESH),
  272. hwnd,
  273. RefreshDlgProc,
  274. (LPARAM)&dd);
  275. if (-1 == nRes)
  276. {
  277. int err = GetLastError();
  278. }
  279. else if (TRUE == nRes)
  280. {
  281. TCHAR szURL[INTERNET_MAX_URL_LENGTH];
  282. if (SHUnicodeToTChar(pszwURL, szURL, ARRAYSIZE(szURL)))
  283. {
  284. FILETIME ftLastMod;
  285. URLGetLastModTime(szURL, &ftLastMod);
  286. Cache_EnterWriteLock();
  287. Cache_RemoveItem(szURL);
  288. if (SUCCEEDED(Cache_AddItem(szURL, pIXMLDocument, PARSE_NET,
  289. ftLastMod, dwCacheCount)))
  290. fRet = TRUE;
  291. Cache_LeaveWriteLock();
  292. }
  293. BSTR bstrSSUrl;
  294. if (SUCCEEDED(XML_GetScreenSaverURL(pIXMLDocument, &bstrSSUrl)))
  295. {
  296. Channel_WriteScreenSaverURL(pszwURL, bstrSSUrl);
  297. SysFreeString(bstrSSUrl);
  298. }
  299. }
  300. }
  301. return fRet;
  302. }
  303. // Checks if global state is offline
  304. // Stolen from webcheck utils.cpp
  305. BOOL IsGlobalOffline(void)
  306. {
  307. DWORD dwState = 0,
  308. dwSize = sizeof(DWORD);
  309. BOOL fRet = FALSE;
  310. if (InternetQueryOption(NULL, INTERNET_OPTION_CONNECTED_STATE, &dwState, &dwSize))
  311. {
  312. if (dwState & INTERNET_STATE_DISCONNECTED_BY_USER)
  313. fRet = TRUE;
  314. }
  315. return fRet;
  316. }
  317. void SetGlobalOffline(BOOL fOffline)
  318. {
  319. INTERNET_CONNECTED_INFO ci;
  320. memset(&ci, 0, sizeof(ci));
  321. if (fOffline)
  322. {
  323. ci.dwConnectedState = INTERNET_STATE_DISCONNECTED_BY_USER;
  324. ci.dwFlags = ISO_FORCE_DISCONNECTED;
  325. }
  326. else
  327. {
  328. ci.dwConnectedState = INTERNET_STATE_CONNECTED;
  329. }
  330. InternetSetOption(NULL, INTERNET_OPTION_CONNECTED_STATE, &ci, sizeof(ci));
  331. }
  332. //
  333. // Can the given url be subscribed?
  334. //
  335. BOOL
  336. CanSubscribe(
  337. LPCWSTR pwszURL
  338. )
  339. {
  340. ASSERT(pwszURL);
  341. BOOL fRet = FALSE;
  342. TCHAR szURL[INTERNET_MAX_URL_LENGTH];
  343. if (SHUnicodeToTChar(pwszURL, szURL, ARRAYSIZE(szURL)))
  344. {
  345. TCHAR szCanonicalURL[INTERNET_MAX_URL_LENGTH];
  346. DWORD dwSize = ARRAYSIZE(szCanonicalURL);
  347. URL_COMPONENTS uc = {0};
  348. uc.dwStructSize = sizeof(URL_COMPONENTS);
  349. if (InternetCanonicalizeUrl(szURL, szCanonicalURL, &dwSize, ICU_DECODE)
  350. &&
  351. InternetCrackUrl(szCanonicalURL, 0, 0, &uc)
  352. &&
  353. ((INTERNET_SCHEME_HTTP == uc.nScheme) ||
  354. (INTERNET_SCHEME_HTTPS == uc.nScheme)))
  355. {
  356. fRet = TRUE;
  357. }
  358. }
  359. return fRet;
  360. }