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.

338 lines
8.4 KiB

  1. #include "general.h"
  2. #include "parseinf.h"
  3. #include <mluisupp.h>
  4. ///////////////////////////////////////////////////////////////////////////////
  5. // For retriving data from a CONTROLDATA struct
  6. UINT GetTotalNumOfFiles(LPCONTROLPIDL lpcpidl)
  7. {
  8. return (lpcpidl != NULL ? lpcpidl->ci.cTotalFiles : 0);
  9. }
  10. DWORD GetSizeSaved(LPCONTROLPIDL lpcpidl)
  11. {
  12. return (lpcpidl != NULL ? lpcpidl->ci.dwTotalSizeSaved : 0);
  13. }
  14. BOOL GetSizeSaved(LPCONTROLPIDL pcpidl, LPTSTR lpszBuf)
  15. {
  16. Assert(pcpidl != NULL);
  17. Assert(lpszBuf != NULL);
  18. if (pcpidl == NULL || lpszBuf == NULL)
  19. return FALSE;
  20. DWORD dwTotal = GetSizeSaved(pcpidl);
  21. if (dwTotal > 0)
  22. {
  23. TCHAR szSize[20];
  24. TCHAR szBuf[MAX_KILOBYTE_ABBREV_LEN + 1];
  25. dwTotal = (dwTotal < 1024 ? 1024 : dwTotal);
  26. wsprintf(szSize, "%d", (dwTotal / 1024));
  27. // insert commas to separate groups of digits
  28. int nLen = lstrlen(szSize);
  29. int i = 0, j = (nLen <= 3 ? nLen : (nLen % 3));
  30. TCHAR *pCh = szSize + j;
  31. for (; i < j; i++)
  32. lpszBuf[i] = szSize[i];
  33. for (; *pCh != '\0'; i++, pCh++)
  34. {
  35. if (((pCh - szSize) % 3 == j) && (i > 0))
  36. lpszBuf[i++] = ',';
  37. lpszBuf[i] = *pCh;
  38. }
  39. lpszBuf[i] = '\0';
  40. MLLoadString(IDS_KILOBYTE_ABBREV, szBuf, MAX_KILOBYTE_ABBREV_LEN);
  41. lstrcat(lpszBuf, szBuf);
  42. }
  43. else
  44. {
  45. lstrcpy(lpszBuf, g_szUnknownData);
  46. }
  47. return TRUE;
  48. }
  49. UINT GetStatus(LPCONTROLPIDL pcpidl)
  50. {
  51. return (pcpidl != NULL ? pcpidl->ci.dwStatus : STATUS_CTRL_UNKNOWN);
  52. }
  53. BOOL GetStatus(LPCONTROLPIDL pcpidl, LPTSTR lpszBuf, int nBufSize)
  54. {
  55. Assert(pcpidl != NULL);
  56. Assert(lpszBuf != NULL);
  57. if (pcpidl == NULL || lpszBuf == NULL)
  58. return FALSE;
  59. switch (GetStatus(pcpidl))
  60. {
  61. case STATUS_CTRL_UNKNOWN:
  62. MLLoadString(IDS_STATUS_UNKNOWN, lpszBuf, nBufSize);
  63. break;
  64. case STATUS_CTRL_INSTALLED:
  65. MLLoadString(IDS_STATUS_INSTALLED, lpszBuf, nBufSize);
  66. break;
  67. case STATUS_CTRL_SHARED:
  68. MLLoadString(IDS_STATUS_SHARED, lpszBuf, nBufSize);
  69. break;
  70. case STATUS_CTRL_DAMAGED:
  71. MLLoadString(IDS_STATUS_DAMAGED, lpszBuf, nBufSize);
  72. break;
  73. case STATUS_CTRL_UNPLUGGED:
  74. MLLoadString(IDS_STATUS_UNPLUGGED, lpszBuf, nBufSize);
  75. break;
  76. default:
  77. lstrcpy(lpszBuf, g_szUnknownData);
  78. }
  79. return TRUE;
  80. }
  81. BOOL GetTimeInfo(LPCONTROLPIDL lpcpidl, int nFlag, FILETIME* lpTime)
  82. {
  83. Assert(lpcpidl != NULL && lpTime != NULL);
  84. if (lpcpidl == NULL || lpTime == NULL)
  85. return FALSE;
  86. BOOL fResult = TRUE;
  87. switch (nFlag)
  88. {
  89. case SI_CREATION:
  90. *lpTime = lpcpidl->ci.timeCreation;
  91. break;
  92. case SI_LASTACCESS:
  93. *lpTime = lpcpidl->ci.timeLastAccessed;
  94. break;
  95. default:
  96. lpTime->dwLowDateTime = lpTime->dwLowDateTime = 0;
  97. fResult = FALSE;
  98. }
  99. return fResult;
  100. }
  101. LPCTSTR GetStringInfo(LPCONTROLPIDL lpcpidl, int nFlag)
  102. {
  103. switch (nFlag)
  104. {
  105. case SI_CONTROL:
  106. return (lpcpidl != NULL ? lpcpidl->ci.szName : NULL);
  107. case SI_LOCATION:
  108. return (lpcpidl != NULL ? lpcpidl->ci.szFile : NULL);
  109. case SI_VERSION:
  110. return (lpcpidl != NULL ? lpcpidl->ci.szVersion : NULL);
  111. case SI_CLSID:
  112. return (lpcpidl != NULL ? lpcpidl->ci.szCLSID : NULL);
  113. case SI_CREATION:
  114. return (lpcpidl != NULL ? lpcpidl->ci.szCreation : NULL);
  115. case SI_LASTACCESS:
  116. return (lpcpidl != NULL ? lpcpidl->ci.szLastAccess : NULL);
  117. case SI_TYPELIBID:
  118. return (lpcpidl != NULL ? lpcpidl->ci.szTypeLibID : NULL);
  119. case SI_CODEBASE:
  120. return (lpcpidl != NULL ? lpcpidl->ci.szCodeBase : NULL);
  121. }
  122. return NULL;
  123. }
  124. BOOL GetDependentFile(
  125. LPCONTROLPIDL lpcpidl,
  126. UINT iFile,
  127. LPTSTR lpszFile,
  128. DWORD *pdwSize)
  129. {
  130. if (lpszFile == NULL ||
  131. pdwSize == NULL ||
  132. iFile >= GetTotalNumOfFiles(lpcpidl))
  133. {
  134. return FALSE;
  135. }
  136. DEPENDENTFILEINFO UNALIGNED *pInfo = &(lpcpidl->ci.dependentFile);
  137. lstrcpy(lpszFile, (pInfo + iFile)->szFile);
  138. *pdwSize = (pInfo + iFile)->dwSize;
  139. return TRUE;
  140. }
  141. void GetContentBools( LPCONTROLPIDL lpcpidl, BOOL *pbHasActiveX, BOOL *pbHasJava )
  142. {
  143. if ( lpcpidl != NULL )
  144. {
  145. *pbHasActiveX = lpcpidl->ci.dwHasActiveX != 0;
  146. *pbHasJava = lpcpidl->ci.dwHasJava != 0;
  147. }
  148. else
  149. {
  150. *pbHasActiveX = *pbHasJava = FALSE;
  151. }
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////
  154. // Other helper functions
  155. void GenerateEvent(
  156. LONG lEventId,
  157. LPITEMIDLIST pidlFolder,
  158. LPITEMIDLIST pidlIn,
  159. LPITEMIDLIST pidlNewIn)
  160. {
  161. LPITEMIDLIST pidl = ILCombine(pidlFolder, pidlIn);
  162. if (pidl)
  163. {
  164. if (pidlNewIn)
  165. {
  166. LPITEMIDLIST pidlNew = ILCombine(pidlFolder, pidlNewIn);
  167. if (pidlNew)
  168. {
  169. SHChangeNotify(lEventId, SHCNF_IDLIST, pidl, pidlNew);
  170. ILFree(pidlNew);
  171. }
  172. }
  173. else
  174. {
  175. SHChangeNotify(lEventId, SHCNF_IDLIST, pidl, NULL);
  176. }
  177. SHChangeNotifyHandleEvents();
  178. ILFree(pidl);
  179. }
  180. }
  181. HICON GetDefaultOCIcon(LPCONTROLPIDL lpcpidl)
  182. {
  183. DWORD idIcon = IDI_DEFAULTOCXICON;
  184. if ( lpcpidl->ci.dwIsDistUnit )
  185. {
  186. if ( lpcpidl->ci.dwHasJava )
  187. {
  188. if ( lpcpidl->ci.dwHasActiveX )
  189. idIcon = IDI_DEFAULTMIXEDICON;
  190. else
  191. idIcon = IDI_DEFAULTJAVAICON;
  192. }
  193. }
  194. return LoadIcon(g_hInst, MAKEINTRESOURCE(idIcon));
  195. }
  196. HCURSOR StartWaitCur()
  197. {
  198. HCURSOR hCur = LoadCursor(NULL, IDC_WAIT);
  199. return (hCur != NULL ? SetCursor(hCur) : NULL);
  200. }
  201. void EndWaitCur(HCURSOR hCurOld)
  202. {
  203. if (hCurOld != NULL)
  204. SetCursor(hCurOld);
  205. }
  206. // The place to get the # of days before a control becomes expired.
  207. const LPCTSTR g_lpszKeyExpire = TEXT("SOFTWARE\\Microsoft\\Windows"
  208. "\\CurrentVersion\\Internet Settings\\ActiveX Cache\\Expire");
  209. const LPCTSTR g_szValueExpire = TEXT("DaysBeforeExpire");
  210. const LPCTSTR g_szValueAutoExpire = TEXT("DaysBeforeAutoExpire");
  211. ULONG g_nDaysGeneral = 0;
  212. ULONG g_nDaysAuto = 0;
  213. void GetDaysBeforeExpire(ULONG *pnDays, BOOL fGeneral)
  214. {
  215. HKEY hkey;
  216. DWORD dwSize = sizeof(ULONG);
  217. LONG lResult;
  218. ASSERT(pnDays != NULL);
  219. if ( fGeneral && g_nDaysGeneral )
  220. {
  221. *pnDays = g_nDaysGeneral;
  222. return;
  223. }
  224. else if ( !fGeneral && g_nDaysAuto )
  225. {
  226. *pnDays = g_nDaysAuto;
  227. return;
  228. }
  229. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_lpszKeyExpire, 0, KEY_READ,
  230. &hkey);
  231. if (lResult == ERROR_SUCCESS) {
  232. lResult = RegQueryValueEx(hkey, (fGeneral ? g_szValueExpire : g_szValueAutoExpire), NULL, NULL,
  233. (LPBYTE)pnDays, &dwSize);
  234. RegCloseKey(hkey);
  235. }
  236. if (lResult != ERROR_SUCCESS)
  237. *pnDays = (fGeneral ? DEFAULT_DAYS_BEFORE_EXPIRE : DEFAULT_DAYS_BEFORE_AUTOEXPIRE);
  238. if ( fGeneral )
  239. g_nDaysGeneral = *pnDays;
  240. else
  241. g_nDaysAuto = *pnDays;
  242. }
  243. void GetDaysBeforeExpireGeneral(ULONG *pnDays)
  244. {
  245. GetDaysBeforeExpire(pnDays, TRUE);
  246. }
  247. void GetDaysBeforeExpireAuto(ULONG *pnDays)
  248. {
  249. GetDaysBeforeExpire(pnDays, FALSE);
  250. }
  251. HRESULT WINAPI RemoveControlByHandle2(
  252. HANDLE hControlHandle,
  253. BOOL bForceRemove, /* = FALSE */
  254. BOOL bSilent)
  255. {
  256. CCacheItem *pci = (CCacheItem *)hControlHandle;
  257. CoFreeUnusedLibraries();
  258. return pci->RemoveFiles( pci->m_szTypeLibID, bForceRemove, pci->ItemType() == CCacheDistUnit::s_dwType, bSilent );
  259. }
  260. HRESULT WINAPI RemoveControlByName2(
  261. LPCTSTR lpszFile,
  262. LPCTSTR lpszCLSID,
  263. LPCTSTR lpszTypeLibID,
  264. BOOL bForceRemove, /* = FALSE */
  265. DWORD dwIsDistUnit, /* = FALSE */
  266. BOOL bSilent)
  267. {
  268. if (lpszFile == NULL || lpszCLSID == NULL)
  269. return HRESULT_FROM_WIN32(ERROR_BAD_ARGUMENTS);
  270. CoFreeUnusedLibraries();
  271. HRESULT hr = S_OK;
  272. CParseInf parseInf;
  273. if (!dwIsDistUnit)
  274. {
  275. hr = parseInf.DoParse(lpszFile, lpszCLSID);
  276. }
  277. else
  278. {
  279. hr = parseInf.DoParseDU(lpszFile, lpszCLSID);
  280. }
  281. if (SUCCEEDED(hr))
  282. {
  283. hr = parseInf.RemoveFiles(lpszTypeLibID, bForceRemove, dwIsDistUnit, bSilent);
  284. }
  285. return hr;
  286. }