Leaked source code of windows server 2003
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.

593 lines
13 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: jobpages.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 3/5/1996 RaviR Created
  15. //
  16. //____________________________________________________________________________
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include <mstask.h>
  20. #include "defines.h"
  21. #include "..\inc\resource.h"
  22. #include "..\inc\common.hxx"
  23. #include "..\folderui\dbg.h"
  24. #include "..\folderui\macros.h"
  25. #include "..\folderui\util.hxx"
  26. #include "..\rc.h"
  27. #include "shared.hxx"
  28. #include "schedui.hxx"
  29. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
  30. //void LogIt(WCHAR* pLine)
  31. //{
  32. // FILE* pFile;
  33. // pFile = fopen("C:\\hmh.log", "a+");
  34. //
  35. // if (pFile)
  36. // {
  37. // fputws(pLine, pFile);
  38. // fflush(pFile);
  39. // fclose(pFile);
  40. // }
  41. //}
  42. //void LogFunnyString(WCHAR* pLine, UINT count)
  43. //{
  44. // FILE* pFile;
  45. // pFile = fopen("C:\\hmh.log", "a+");
  46. //
  47. // if (pFile)
  48. // {
  49. // fputws(L"Funny file name:\n\t",pFile);
  50. // for(UINT i = 0; i < count; i++)
  51. // fputwc(pLine[i], pFile);
  52. // fputws(L"\n",pFile);
  53. //
  54. // fflush(pFile);
  55. // fclose(pFile);
  56. // }
  57. //}
  58. //
  59. // extern EXTERN_C
  60. //
  61. extern HINSTANCE g_hInstance;
  62. //
  63. // Local constants
  64. //
  65. TCHAR const FAR c_szNULL[] = TEXT("");
  66. TCHAR const FAR c_szStubWindowClass[] = TEXT("JobPropWnd");
  67. #define STUBM_SETDATA (WM_USER + 1)
  68. #define STUBM_GETDATA (WM_USER + 2)
  69. #define JF_PROPSHEET_STUB_CLASS 78345
  70. LRESULT
  71. CALLBACK
  72. StubWndProc(
  73. HWND hWnd,
  74. UINT iMessage,
  75. WPARAM wParam,
  76. LPARAM lParam)
  77. {
  78. switch(iMessage)
  79. {
  80. case STUBM_SETDATA:
  81. SetWindowLongPtr(hWnd, 0, wParam);
  82. return TRUE;
  83. case STUBM_GETDATA:
  84. return GetWindowLongPtr(hWnd, 0);
  85. default:
  86. return DefWindowProc(hWnd, iMessage, wParam, lParam);
  87. }
  88. }
  89. HWND I_CreateStubWindow(void)
  90. {
  91. WNDCLASS wndclass;
  92. if (!GetClassInfo(g_hInstance, c_szStubWindowClass, &wndclass))
  93. {
  94. wndclass.style = 0;
  95. wndclass.lpfnWndProc = StubWndProc;
  96. wndclass.cbClsExtra = 0;
  97. wndclass.cbWndExtra = sizeof(PVOID) * 2;
  98. wndclass.hInstance = g_hInstance;
  99. wndclass.hIcon = NULL;
  100. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  101. wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  102. wndclass.lpszMenuName = NULL;
  103. wndclass.lpszClassName = c_szStubWindowClass;
  104. if (!RegisterClass(&wndclass))
  105. return NULL;
  106. }
  107. return CreateWindowEx(WS_EX_TOOLWINDOW, c_szStubWindowClass, c_szNULL,
  108. WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0,
  109. NULL, NULL, g_hInstance, NULL);
  110. }
  111. HANDLE
  112. StuffStubWindow(
  113. HWND hwnd,
  114. LPTSTR pszFile)
  115. {
  116. DWORD dwProcId;
  117. HANDLE hSharedFile;
  118. UINT uiFileSize;
  119. uiFileSize = (lstrlen(pszFile) + 1) * sizeof(TCHAR);
  120. GetWindowThreadProcessId(hwnd, &dwProcId);
  121. hSharedFile = SCHEDAllocShared(NULL, sizeof(int)+uiFileSize, dwProcId);
  122. if (hSharedFile)
  123. {
  124. LPBYTE lpb = (LPBYTE)SCHEDLockShared(hSharedFile, dwProcId);
  125. if (lpb)
  126. {
  127. *(int *)lpb = JF_PROPSHEET_STUB_CLASS;
  128. CopyMemory(lpb+sizeof(int), pszFile, uiFileSize);
  129. SCHEDUnlockShared(lpb);
  130. SendMessage(hwnd, STUBM_SETDATA, (WPARAM)hSharedFile, 0);
  131. return hSharedFile;
  132. }
  133. SCHEDFreeShared(hSharedFile, dwProcId);
  134. }
  135. return NULL;
  136. }
  137. HWND
  138. FindOtherStub(
  139. LPTSTR pszFile)
  140. {
  141. HWND hwnd;
  142. //
  143. // BUGBUG using getwindow in a loop is not safe. this code should
  144. // use EnumWindows instead. From win32 sdk:
  145. //
  146. // "[EnumWindows] is more reliable than calling the GetWindow function in
  147. // a loop. An application that calls GetWindow to perform this task risks
  148. // being caught in an infinite loop or referencing a handle to a window
  149. // that has been destroyed."
  150. //
  151. for (hwnd = FindWindow(c_szStubWindowClass, NULL);
  152. hwnd != NULL;
  153. hwnd = GetWindow(hwnd, GW_HWNDNEXT))
  154. {
  155. TCHAR szClass[80];
  156. // find stub windows only
  157. GetClassName(hwnd, szClass, ARRAYSIZE(szClass));
  158. if (lstrcmpi(szClass, c_szStubWindowClass) == 0)
  159. {
  160. int iClass;
  161. HANDLE hSharedFile;
  162. DWORD dwProcId;
  163. LPTSTR pszTemp;
  164. GetWindowThreadProcessId(hwnd, &dwProcId);
  165. hSharedFile = (HANDLE)SendMessage(hwnd, STUBM_GETDATA, 0, 0);
  166. if (hSharedFile)
  167. {
  168. LPBYTE lpb;
  169. lpb = (LPBYTE)SCHEDLockShared(hSharedFile, dwProcId);
  170. pszTemp = (LPTSTR) (lpb + sizeof(int));
  171. if (lpb)
  172. {
  173. iClass = *(int *)lpb;
  174. if (iClass == JF_PROPSHEET_STUB_CLASS &&
  175. lstrcmpi(pszTemp, pszFile) == 0)
  176. {
  177. SCHEDUnlockShared(lpb);
  178. return hwnd;
  179. }
  180. SCHEDUnlockShared(lpb);
  181. }
  182. }
  183. }
  184. }
  185. return NULL;
  186. }
  187. STDMETHODIMP
  188. I_GetTaskPage(
  189. ITask * pIJob,
  190. TASKPAGE tpType,
  191. BOOL fPersistChanges,
  192. HPROPSHEETPAGE * phPage)
  193. {
  194. TRACE_FUNCTION(I_GetTaskPage);
  195. HRESULT hr = S_OK;
  196. LPOLESTR polestr = NULL;
  197. do
  198. {
  199. //
  200. // Ensure that the object has a file name.
  201. //
  202. IPersistFile * ppf = NULL;
  203. hr = pIJob->QueryInterface(IID_IPersistFile, (void **)&ppf);
  204. CHECK_HRESULT(hr);
  205. BREAK_ON_FAIL(hr);
  206. hr = ppf->GetCurFile(&polestr);
  207. CHECK_HRESULT(hr);
  208. BREAK_ON_FAIL(hr);
  209. ppf->Release();
  210. if (hr == S_FALSE)
  211. {
  212. hr = STG_E_NOTFILEBASEDSTORAGE;
  213. CHECK_HRESULT(hr);
  214. break;
  215. }
  216. //
  217. // Establish if this task exists within a task's folder.
  218. //
  219. LPTSTR ptszJobPath;
  220. ptszJobPath = polestr;
  221. if (ptszJobPath == NULL)
  222. {
  223. hr = E_OUTOFMEMORY;
  224. CHECK_HRESULT(hr);
  225. break;
  226. }
  227. //
  228. // Get the page
  229. //
  230. switch (tpType)
  231. {
  232. case TASKPAGE_TASK:
  233. {
  234. hr = GetGeneralPage(pIJob, ptszJobPath, fPersistChanges, phPage);
  235. CHECK_HRESULT(hr);
  236. break;
  237. }
  238. case TASKPAGE_SCHEDULE:
  239. hr = GetSchedulePage(pIJob, ptszJobPath, fPersistChanges, phPage);
  240. CHECK_HRESULT(hr);
  241. break;
  242. case TASKPAGE_SETTINGS:
  243. hr = GetSettingsPage(pIJob, ptszJobPath, fPersistChanges, phPage);
  244. CHECK_HRESULT(hr);
  245. break;
  246. default:
  247. hr = E_INVALIDARG;
  248. CHECK_HRESULT(hr);
  249. break;
  250. }
  251. } while (0);
  252. if (polestr != NULL)
  253. {
  254. CoTaskMemFree(polestr);
  255. }
  256. return hr;
  257. }
  258. HRESULT
  259. DisplayJobProperties(
  260. LPDATAOBJECT pdtobj)
  261. {
  262. TRACE_FUNCTION(DisplayJobProperties);
  263. HRESULT hr = S_OK;
  264. HANDLE hSharedFile = NULL;
  265. HWND hwnd = NULL;
  266. ITask * pIJob = NULL;
  267. do
  268. {
  269. //
  270. // Extract the job name from the data object.
  271. //
  272. STGMEDIUM stgm;
  273. FORMATETC fmte = {CF_HDROP, (DVTARGETDEVICE *)NULL,
  274. DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  275. hr = pdtobj->GetData(&fmte, &stgm);
  276. CHECK_HRESULT(hr);
  277. BREAK_ON_FAIL(hr);
  278. TCHAR szFile[MAX_PATH + 1];
  279. UINT cchRet = DragQueryFile((HDROP)stgm.hGlobal, 0, szFile, MAX_PATH + 1);
  280. ReleaseStgMedium(&stgm);
  281. if (cchRet == 0)
  282. {
  283. return E_FAIL;
  284. }
  285. //
  286. // See if the property page for this job is already being displayed.
  287. //
  288. if (NULL != (hwnd = FindOtherStub(szFile)))
  289. {
  290. SwitchToThisWindow(GetLastActivePopup(hwnd), TRUE);
  291. break;
  292. }
  293. else
  294. {
  295. hwnd = I_CreateStubWindow();
  296. hSharedFile = StuffStubWindow(hwnd, szFile);
  297. }
  298. //
  299. // Bind to the ITask interface.
  300. //
  301. hr = JFCreateAndLoadTask(NULL, szFile, &pIJob);
  302. // if (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
  303. // {
  304. // LogIt(L"Got a path not found error\n");
  305. // LogFunnyString(szFile, cchRet);
  306. // }
  307. // hack: sometimes we get a corrupted directory name
  308. // let's see if we can't djinn up a proper name instead...
  309. if (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
  310. {
  311. WCHAR* pFoundName = PathFindFileName(szFile);
  312. WCHAR trialPath[MAX_PATH +1];
  313. WCHAR* pTasksFolder;
  314. if (SUCCEEDED(GetTasksFolder(&pTasksFolder)))
  315. {
  316. StringCchCopy(trialPath, MAX_PATH +1, pTasksFolder);
  317. StringCchCat(trialPath, MAX_PATH +1, L"\\");
  318. StringCchCat(trialPath, MAX_PATH +1, pFoundName);
  319. if (SUCCEEDED(hr = JFCreateAndLoadTask(NULL, trialPath, &pIJob)))
  320. StringCchCopy(szFile, MAX_PATH +1, trialPath);
  321. delete[] pTasksFolder;
  322. }
  323. }
  324. BREAK_ON_FAIL(hr);
  325. LPTSTR pName = PathFindFileName(szFile);
  326. LPTSTR pExt = PathFindExtension(pName);
  327. if (pExt)
  328. {
  329. *pExt = TEXT('\0');
  330. }
  331. //
  332. // Add the pages.
  333. //
  334. HPROPSHEETPAGE ahpage[MAX_PROP_PAGES];
  335. PROPSHEETHEADER psh;
  336. SecureZeroMemory(&psh, sizeof(psh));
  337. psh.dwSize = sizeof(PROPSHEETHEADER);
  338. psh.dwFlags = PSH_DEFAULT;
  339. psh.hwndParent = hwnd;
  340. psh.hInstance = g_hInstance;
  341. psh.pszCaption = pName;
  342. psh.phpage = ahpage;
  343. hr = AddGeneralPage(psh, pIJob);
  344. CHECK_HRESULT(hr);
  345. hr = AddSchedulePage(psh, pIJob);
  346. CHECK_HRESULT(hr);
  347. hr = AddSettingsPage(psh, pIJob);
  348. CHECK_HRESULT(hr);
  349. hr = AddSecurityPage(psh, pdtobj);
  350. CHECK_HRESULT(hr);
  351. if (psh.nPages == 0)
  352. {
  353. DEBUG_OUT((DEB_USER1, "No pages to display.\n"));
  354. hr = E_FAIL;
  355. break;
  356. }
  357. PropertySheet(&psh);
  358. } while (0);
  359. if (pIJob != NULL)
  360. {
  361. pIJob->Release();
  362. }
  363. SCHEDFreeShared(hSharedFile, GetCurrentProcessId());
  364. if (hwnd)
  365. {
  366. DestroyWindow(hwnd);
  367. }
  368. return hr;
  369. }
  370. HRESULT
  371. DisplayJobProperties(
  372. LPTSTR pszJob,
  373. ITask * pIJob)
  374. {
  375. Win4Assert(pszJob != NULL);
  376. Win4Assert(pIJob != NULL);
  377. HRESULT hr = S_OK;
  378. PROPSHEETHEADER psh;
  379. SecureZeroMemory(&psh, sizeof(psh));
  380. do
  381. {
  382. //
  383. // Determine the job name.
  384. //
  385. TCHAR szName[MAX_PATH + 1];
  386. StringCchCopy(szName, MAX_PATH + 1, PathFindFileName(pszJob));
  387. LPTSTR pExt = PathFindExtension(szName);
  388. if (pExt)
  389. {
  390. *pExt = TEXT('\0');
  391. }
  392. //
  393. // Add the pages.
  394. //
  395. HPROPSHEETPAGE ahpage[MAX_PROP_PAGES];
  396. psh.dwSize = sizeof(PROPSHEETHEADER);
  397. psh.dwFlags = PSH_DEFAULT;
  398. psh.hwndParent = I_CreateStubWindow();
  399. psh.hInstance = g_hInstance;
  400. psh.pszCaption = szName;
  401. psh.phpage = ahpage;
  402. hr = AddGeneralPage(psh, pIJob);
  403. CHECK_HRESULT(hr);
  404. hr = AddSchedulePage(psh, pIJob);
  405. CHECK_HRESULT(hr);
  406. hr = AddSettingsPage(psh, pIJob);
  407. CHECK_HRESULT(hr);
  408. if (psh.nPages == 0)
  409. {
  410. DEBUG_OUT((DEB_USER1, "No pages to display.\n"));
  411. hr = E_FAIL;
  412. break;
  413. }
  414. PropertySheet(&psh);
  415. } while (0);
  416. if (psh.hwndParent)
  417. {
  418. DestroyWindow(psh.hwndParent);
  419. }
  420. return hr;
  421. }
  422. HRESULT
  423. GetJobPath(
  424. ITask * pIJob,
  425. LPTSTR * ppszJobPath)
  426. {
  427. HRESULT hr = S_OK;
  428. LPOLESTR polestr = NULL;
  429. IPersistFile * ppf = NULL;
  430. do
  431. {
  432. //
  433. // Get the object name
  434. //
  435. hr = pIJob->QueryInterface(IID_IPersistFile, (void **)&ppf);
  436. CHECK_HRESULT(hr);
  437. BREAK_ON_FAIL(hr);
  438. hr = ppf->GetCurFile(&polestr);
  439. CHECK_HRESULT(hr);
  440. BREAK_ON_FAIL(hr);
  441. LPTSTR pszJobPath = NULL;
  442. pszJobPath = NewDupString(polestr);
  443. CoTaskMemFree(polestr);
  444. if (pszJobPath == NULL)
  445. {
  446. hr = E_OUTOFMEMORY;
  447. CHECK_HRESULT(hr);
  448. break;
  449. }
  450. *ppszJobPath = pszJobPath;
  451. } while (0);
  452. if (ppf != NULL)
  453. {
  454. ppf->Release();
  455. }
  456. return hr;
  457. }