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.

376 lines
8.2 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: util.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 1/8/1996 RaviR Created (Copied from BruceFo's lmshare)
  15. //
  16. //____________________________________________________________________________
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include "dbg.h"
  20. #include <mstask.h> // Necessary for util.hxx
  21. #include "jobidl.hxx"
  22. #include "util.hxx"
  23. #include "resource.h"
  24. #include "commdlg.h"
  25. #include "..\inc\sadat.hxx"
  26. extern HINSTANCE g_hInstance;
  27. HRESULT
  28. GetSchSvcState(
  29. DWORD &dwCurrState);
  30. //___________________________________________________________________________
  31. //___________________________________________________________________________
  32. //___________________________________________________________________________
  33. //___________________________________________________________________________
  34. //
  35. // Menu merging routines
  36. //___________________________________________________________________________
  37. //___________________________________________________________________________
  38. //___________________________________________________________________________
  39. //___________________________________________________________________________
  40. HMENU
  41. LoadPopupMenu(
  42. HINSTANCE hinst,
  43. UINT id)
  44. {
  45. HMENU hmParent = LoadMenu(hinst, MAKEINTRESOURCE(id));
  46. if (NULL == hmParent)
  47. {
  48. return NULL;
  49. }
  50. HMENU hmPopup = GetSubMenu(hmParent, 0);
  51. RemoveMenu(hmParent, 0, MF_BYPOSITION);
  52. DestroyMenu(hmParent);
  53. return hmPopup;
  54. }
  55. HMENU
  56. UtGetMenuFromID(
  57. HMENU hmMain,
  58. UINT uID
  59. )
  60. {
  61. MENUITEMINFO mii;
  62. mii.cbSize = sizeof(mii);
  63. mii.fMask = MIIM_SUBMENU;
  64. mii.cch = 0; // just in case
  65. if (!GetMenuItemInfo(hmMain, uID, FALSE, &mii))
  66. {
  67. return NULL;
  68. }
  69. return mii.hSubMenu;
  70. }
  71. int
  72. UtMergePopupMenus(
  73. HMENU hmMain,
  74. HMENU hmMerge,
  75. int idCmdFirst,
  76. int idCmdLast)
  77. {
  78. int i;
  79. int idTemp;
  80. int idMax = idCmdFirst;
  81. for (i = GetMenuItemCount(hmMerge) - 1; i >= 0; --i)
  82. {
  83. MENUITEMINFO mii;
  84. mii.cbSize = sizeof(mii);
  85. mii.fMask = MIIM_ID | MIIM_SUBMENU;
  86. mii.cch = 0; // just in case
  87. if (!GetMenuItemInfo(hmMerge, i, TRUE, &mii))
  88. {
  89. continue;
  90. }
  91. idTemp = Shell_MergeMenus(
  92. UtGetMenuFromID(hmMain, mii.wID),
  93. mii.hSubMenu,
  94. 0,
  95. idCmdFirst,
  96. idCmdLast,
  97. MM_ADDSEPARATOR | MM_SUBMENUSHAVEIDS);
  98. if (idMax < idTemp)
  99. {
  100. idMax = idTemp;
  101. }
  102. }
  103. return idMax;
  104. }
  105. void
  106. UtMergeMenu(
  107. HINSTANCE hinst,
  108. UINT idMainMerge,
  109. UINT idPopupMerge,
  110. LPQCMINFO pqcm)
  111. {
  112. HMENU hmMerge;
  113. UINT idMax = pqcm->idCmdFirst;
  114. UINT idTemp;
  115. if (idMainMerge
  116. && (hmMerge = LoadPopupMenu(hinst, idMainMerge)) != NULL)
  117. {
  118. idMax = Shell_MergeMenus(
  119. pqcm->hmenu,
  120. hmMerge,
  121. pqcm->indexMenu,
  122. pqcm->idCmdFirst,
  123. pqcm->idCmdLast,
  124. MM_SUBMENUSHAVEIDS);
  125. DestroyMenu(hmMerge);
  126. }
  127. if (idPopupMerge
  128. && (hmMerge = LoadMenu(hinst, MAKEINTRESOURCE(idPopupMerge))) != NULL)
  129. {
  130. idTemp = UtMergePopupMenus(
  131. pqcm->hmenu,
  132. hmMerge,
  133. pqcm->idCmdFirst,
  134. pqcm->idCmdLast);
  135. if (idMax < idTemp)
  136. {
  137. idMax = idTemp;
  138. }
  139. DestroyMenu(hmMerge);
  140. }
  141. pqcm->idCmdFirst = idMax;
  142. }
  143. //+--------------------------------------------------------------------------
  144. //
  145. // Function: ContainsTemplateObject
  146. //
  147. // Synopsis: Return TRUE if [apidl] contains a CJobID object which is
  148. // marked as a template object.
  149. //
  150. // Arguments: [cidl] - number of item id lists in [apidl]
  151. // [apidl] - array of item id lists, each of which can contain
  152. // only one itemid.
  153. //
  154. // History: 5-09-1997 DavidMun Created
  155. //
  156. //---------------------------------------------------------------------------
  157. BOOL
  158. ContainsTemplateObject(
  159. UINT cidl,
  160. LPCITEMIDLIST *apidl)
  161. {
  162. UINT i;
  163. for (i = 0; i < cidl; i++)
  164. {
  165. if (JF_IsValidID(apidl[i]))
  166. {
  167. if (((PJOBID) apidl[i])->IsTemplate())
  168. {
  169. return TRUE;
  170. }
  171. }
  172. else
  173. {
  174. DEBUG_OUT((DEB_WARN, "ContainsTemplateObject: item %u not jobid\n", i));
  175. }
  176. }
  177. return FALSE;
  178. }
  179. //____________________________________________________________________________
  180. //
  181. // Function: EnsureUniquenessOfFileName
  182. //
  183. // Synopsis: Internal function. pszFile's buffer size assumed to be
  184. // greater than MAX_PATH.
  185. //
  186. // Arguments: [pszFile] -- IN
  187. //
  188. // Returns: void
  189. //
  190. // History: 2/8/1996 RaviR Created
  191. //
  192. //____________________________________________________________________________
  193. void
  194. EnsureUniquenessOfFileName(
  195. LPTSTR pszFile)
  196. {
  197. Win4Assert( NULL != pszFile );
  198. int iPostFix = 2;
  199. LPTSTR pszName = PathFindFileName(pszFile);
  200. LPTSTR pszExt = PathFindExtension(pszName);
  201. TCHAR szBufExt[10];
  202. lstrcpy(szBufExt, pszExt);
  203. int lenUpToExt = (int)(pszExt - pszFile); // lstrlen(pszFile) - lstrlen(pszExt)
  204. Win4Assert(lenUpToExt >= 0);
  205. //
  206. // Ensure uniqueness of the file
  207. //
  208. while (1)
  209. {
  210. HANDLE hFile = CreateFile(pszFile, GENERIC_READ,
  211. FILE_SHARE_READ, NULL, OPEN_EXISTING,
  212. FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  213. if (hFile == INVALID_HANDLE_VALUE)
  214. {
  215. // No file with this name exists. So this name is unique.
  216. break;
  217. }
  218. else
  219. {
  220. CloseHandle(hFile);
  221. // post fix a number to make the file name unique
  222. TCHAR szBufPostFix[10];
  223. wsprintf(szBufPostFix, TEXT(" %d"), iPostFix++);
  224. lstrcpy(&pszFile[lenUpToExt], szBufPostFix);
  225. lstrcat(&pszFile[lenUpToExt], szBufExt);
  226. }
  227. }
  228. }
  229. //+--------------------------------------------------------------------------
  230. //
  231. // Function: CheckSaDat
  232. //
  233. // Synopsis: Update or create the sa.dat file if it is missing or its
  234. // waitable timer support flag disagrees with what the system
  235. // reports.
  236. //
  237. // Arguments: [tszFolderPath] - path to tasks folder
  238. //
  239. // History: 11-12-1997 DavidMun Created
  240. //
  241. // Notes: Call only with [tszFolderPath] a path on the local machine.
  242. //
  243. //---------------------------------------------------------------------------
  244. VOID
  245. CheckSaDat(
  246. LPCTSTR tszFolderPath)
  247. {
  248. HRESULT hr;
  249. DWORD dwVersion;
  250. BYTE bSvcFlags;
  251. BYTE bPlatformId;
  252. hr = SADatGetData(tszFolderPath,
  253. &dwVersion,
  254. &bPlatformId,
  255. &bSvcFlags);
  256. BOOL fNeedUpdate = FALSE;
  257. if (SUCCEEDED(hr))
  258. {
  259. BOOL fSaDatTimersFlag = bSvcFlags & SA_DAT_SVCFLAG_RESUME_TIMERS;
  260. BOOL fTimers = ResumeTimersSupported();
  261. if (fSaDatTimersFlag && !fTimers || !fSaDatTimersFlag && fTimers)
  262. {
  263. fNeedUpdate = TRUE;
  264. }
  265. }
  266. else
  267. {
  268. fNeedUpdate = TRUE;
  269. }
  270. if (fNeedUpdate)
  271. {
  272. DWORD dwState;
  273. hr = GetSchSvcState(dwState);
  274. if (SUCCEEDED(hr))
  275. {
  276. BOOL fRunning = (dwState != SERVICE_STOPPED &&
  277. dwState != SERVICE_STOP_PENDING);
  278. SADatCreate(tszFolderPath, fRunning);
  279. }
  280. }
  281. }
  282. #if DBG==1
  283. LPTSTR DbgGetTimeStr(FILETIME &ft)
  284. {
  285. SYSTEMTIME st;
  286. FileTimeToSystemTime(&ft, &st);
  287. return DbgGetTimeStr(st);
  288. }
  289. LPTSTR DbgGetTimeStr(SYSTEMTIME &st)
  290. {
  291. static TCHAR s_szTimeStamp[20]; // space for time & date in format below
  292. wsprintf(s_szTimeStamp,
  293. TEXT("%02d:%02d:%02d %d/%02d/%d"),
  294. st.wHour,
  295. st.wMinute,
  296. st.wSecond,
  297. st.wMonth,
  298. st.wDay,
  299. st.wYear);
  300. return s_szTimeStamp;
  301. }
  302. #endif