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.

601 lines
15 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998.
  5. //
  6. // File: schdsync.cpp
  7. //
  8. // Contents: SyncMgr AutoSync class
  9. //
  10. // Classes: CSchedSyncPage
  11. //
  12. // Notes:
  13. //
  14. // History: 14-Nov-97 SusiA Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "precomp.h"
  18. DWORD StartScheduler();
  19. extern HINSTANCE g_hmodThisDll; // Handle to this DLL itself.
  20. extern OSVERSIONINFOA g_OSVersionInfo; // osVersionInfo,
  21. extern LANGID g_LangIdSystem; // LangId of system we are running on.
  22. #define UNLIMITED_SCHEDULE_COUNT 50 //Review: What is a reasonable amount of shcedules to grab at a time
  23. #define MAX_APPEND_STRING_LEN 32
  24. //+-------------------------------------------------------------------------------
  25. //
  26. // FUNCTION: BOOL CSchedSyncPage::Initialize()
  27. //
  28. // PURPOSE: initialization for the autosync page
  29. //
  30. // RETURN VALUE: return TRUE if we process it ok.
  31. //
  32. //+-------------------------------------------------------------------------------
  33. BOOL CSchedSyncPage::Initialize()
  34. {
  35. //initialize the item list
  36. HWND hwndList = GetDlgItem(m_hwnd,IDC_SCHEDLIST);
  37. TCHAR pszColumnTitle[MAX_PATH + 1];
  38. LV_COLUMN columnInfo;
  39. HIMAGELIST himage;
  40. INT iItem = -1;
  41. UINT ImageListflags;
  42. LoadString(m_hinst, IDS_SCHEDULE_COLUMN_TITLE,pszColumnTitle, MAX_PATH );
  43. ListView_SetExtendedListViewStyle(hwndList, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
  44. ImageListflags = ILC_COLOR | ILC_MASK;
  45. if (IsHwndRightToLeft(m_hwnd))
  46. {
  47. ImageListflags |= ILC_MIRROR;
  48. }
  49. // create an imagelist
  50. himage = ImageList_Create( GetSystemMetrics(SM_CXSMICON),
  51. GetSystemMetrics(SM_CYSMICON),ImageListflags,5,20);
  52. if (himage)
  53. {
  54. ListView_SetImageList(hwndList,himage,LVSIL_SMALL);
  55. }
  56. HICON hIcon = LoadIcon(m_hinst,MAKEINTRESOURCE(IDI_TASKSCHED));
  57. if (hIcon)
  58. {
  59. m_iDefaultIconImageIndex = ImageList_AddIcon(himage,hIcon);
  60. }
  61. else
  62. {
  63. m_iDefaultIconImageIndex = -1;
  64. }
  65. // Insert the Proper columns
  66. columnInfo.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
  67. columnInfo.fmt = LVCFMT_LEFT;
  68. columnInfo.cx = 328;
  69. columnInfo .pszText =pszColumnTitle;
  70. columnInfo.cchTextMax = lstrlen(pszColumnTitle) + 1;
  71. columnInfo.iSubItem = 0;
  72. ListView_InsertColumn(hwndList,0,&columnInfo);
  73. if (FAILED(InitializeScheduleAgent()))
  74. {
  75. return FALSE;
  76. }
  77. ShowAllSchedules();
  78. ShowWindow(m_hwnd, /* nCmdShow */ SW_SHOWNORMAL );
  79. UpdateWindow(m_hwnd);
  80. return TRUE;
  81. }
  82. //+-------------------------------------------------------------------------------
  83. //
  84. // FUNCTION: BOOL CSchedSyncPage::InitializeScheduleAgent()
  85. //
  86. // PURPOSE: initialization for the ISyncSCheduleMgr
  87. //
  88. // RETURN VALUE: return the appropriate HRESULT.
  89. //
  90. //+-------------------------------------------------------------------------------
  91. HRESULT CSchedSyncPage::InitializeScheduleAgent()
  92. {
  93. HRESULT hr;
  94. LPUNKNOWN lpUnk;
  95. m_pISyncSchedMgr = NULL;
  96. hr = CoCreateInstance(CLSID_SyncMgr,NULL,CLSCTX_ALL,
  97. IID_ISyncScheduleMgr,(void **) &lpUnk);
  98. if (NOERROR == hr)
  99. {
  100. hr = lpUnk->QueryInterface(IID_ISyncScheduleMgr,
  101. (void **) &m_pISyncSchedMgr);
  102. lpUnk->Release();
  103. }
  104. return hr;
  105. }
  106. //+-------------------------------------------------------------------------------
  107. //
  108. // FUNCTION: BOOL CSchedSyncPage::ShowAllSchedules()
  109. //
  110. // PURPOSE: initialization for the schedsync page
  111. //
  112. // RETURN VALUE: return TRUE if we process it ok.
  113. //
  114. //+-------------------------------------------------------------------------------
  115. BOOL CSchedSyncPage::ShowAllSchedules()
  116. {
  117. HRESULT hr;
  118. BOOL fResult = FALSE;
  119. IEnumSyncSchedules *pEnum = NULL;
  120. int iItem = -1;
  121. DWORD dwFetched;
  122. HWND hwndListView = GetDlgItem(m_hwnd,IDC_SCHEDLIST);
  123. SYNCSCHEDULECOOKIE SyncScheduleCookie;
  124. if (!m_pISyncSchedMgr || !hwndListView)
  125. {
  126. goto errRtn;
  127. }
  128. //First clear out the list
  129. FreeAllSchedules();
  130. ListView_DeleteAllItems(hwndListView);
  131. if (FAILED(hr = m_pISyncSchedMgr->EnumSyncSchedules(&pEnum)))
  132. {
  133. goto errRtn;
  134. }
  135. while(S_OK == pEnum->Next(1,&SyncScheduleCookie, &dwFetched))
  136. {
  137. ISyncSchedule *pISyncSched;
  138. WCHAR pwszName[MAX_PATH + 1];
  139. DWORD dwSize = MAX_PATH;
  140. LV_ITEM lvItem;
  141. //get the ISyncSched for this schedule
  142. if (FAILED(hr = m_pISyncSchedMgr->OpenSchedule(&SyncScheduleCookie,
  143. 0,&pISyncSched)))
  144. {
  145. //can't find this one in the registry - move on.
  146. continue;
  147. }
  148. //Get and convert the schedules friendly name
  149. if (FAILED(hr = pISyncSched->GetScheduleName(&dwSize,pwszName)))
  150. {
  151. goto errRtn;
  152. }
  153. ++iItem;
  154. ZeroMemory(&lvItem, sizeof(lvItem));
  155. lvItem.mask = LVIF_TEXT | LVIF_PARAM;
  156. lvItem.pszText = pwszName;
  157. if (m_iDefaultIconImageIndex >= 0)
  158. {
  159. lvItem.mask |= LVIF_IMAGE;
  160. lvItem.iImage = m_iDefaultIconImageIndex;
  161. }
  162. //Save the ISyncSched pointer in the list view data
  163. lvItem.lParam = (LPARAM)pISyncSched;
  164. //add the item to the list
  165. ListView_InsertItem(hwndListView, &lvItem);
  166. }
  167. if (iItem != -1)
  168. {
  169. ListView_SetItemState(hwndListView, 0,
  170. LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED );
  171. }
  172. fResult = TRUE;
  173. errRtn:
  174. if (pEnum)
  175. {
  176. pEnum->Release();
  177. }
  178. return fResult;
  179. }
  180. //+-------------------------------------------------------------------------------
  181. //
  182. // FUNCTION: BOOL CSchedSyncPage::FreeAllSchedules()
  183. //
  184. // PURPOSE: free the schedules for the schedsync page
  185. //
  186. // RETURN VALUE: return TRUE if we process it ok.
  187. //
  188. //+-------------------------------------------------------------------------------
  189. BOOL CSchedSyncPage::FreeAllSchedules()
  190. {
  191. int iItem;
  192. int iItemCount;
  193. HWND hwndListView = GetDlgItem(m_hwnd,IDC_SCHEDLIST);
  194. iItemCount = ListView_GetItemCount(hwndListView);
  195. for(iItem = 0; iItem < iItemCount; iItem++)
  196. {
  197. ISyncSchedule *pISyncSched;
  198. LV_ITEM lvItem;
  199. ZeroMemory(&lvItem, sizeof(lvItem));
  200. lvItem.mask = LVIF_PARAM;
  201. lvItem.iItem = iItem;
  202. ListView_GetItem(hwndListView, &lvItem);
  203. pISyncSched = (ISyncSchedule *) lvItem.lParam;
  204. if (pISyncSched)
  205. {
  206. DWORD cRefs;
  207. cRefs = pISyncSched->Release();
  208. Assert(0 == cRefs);
  209. }
  210. }
  211. return TRUE;
  212. }
  213. //-----------------------------------------------------------------------------
  214. //
  215. // FUNCTION: CSchedSyncPage::OnNotify(HWND hwnd, int idFrom, LPNMHDR pnmhdr)
  216. //
  217. // PURPOSE: Handle the various notification messages dispatched from schedule
  218. // page
  219. //
  220. //-----------------------------------------------------------------------------
  221. BOOL CSchedSyncPage::OnNotify(HWND hwnd, int idFrom, LPNMHDR pnmhdr)
  222. {
  223. if (IDC_SCHEDLIST == idFrom)
  224. {
  225. switch (pnmhdr->code)
  226. {
  227. case LVN_ITEMCHANGED:
  228. {
  229. NM_LISTVIEW *pnmv = (NM_LISTVIEW FAR *) pnmhdr;
  230. if ( (pnmv->uChanged == LVIF_STATE) &&
  231. ((pnmv->uNewState ^ pnmv->uOldState) & LVIS_SELECTED))
  232. {
  233. BOOL fEnable = FALSE;
  234. if (pnmv->uNewState & LVIS_SELECTED)
  235. {
  236. fEnable = TRUE;
  237. }
  238. SetButtonState(IDC_SCHEDREMOVE,fEnable);
  239. SetButtonState(IDC_SCHEDEDIT,fEnable);
  240. return TRUE;
  241. }
  242. }
  243. break;
  244. case NM_DBLCLK:
  245. {
  246. LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW) pnmhdr;
  247. EditTask(lpnmlv->iItem);
  248. }
  249. break;
  250. case NM_RETURN:
  251. {
  252. LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW) pnmhdr;
  253. EditTask(lpnmlv->iItem);
  254. }
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. return FALSE;
  261. }
  262. BOOL CSchedSyncPage::SetButtonState(int nIDDlgItem,BOOL fEnabled)
  263. {
  264. BOOL fResult = FALSE;
  265. HWND hwndCtrl = GetDlgItem(m_hwnd,nIDDlgItem);
  266. HWND hwndFocus = NULL;
  267. if (hwndCtrl)
  268. {
  269. if (!fEnabled) // don't bother getting focus if not disabling.
  270. {
  271. hwndFocus = GetFocus();
  272. }
  273. fResult = EnableWindow(GetDlgItem(m_hwnd,nIDDlgItem),fEnabled);
  274. // if control had the focus. and now it doesn't then tab to the
  275. // next control
  276. if (hwndFocus == hwndCtrl
  277. && !fEnabled)
  278. {
  279. SetFocus(GetDlgItem(m_hwnd,IDC_SCHEDADD)); // if need to change focus set to add.
  280. }
  281. }
  282. return fResult;
  283. }
  284. BOOL CSchedSyncPage::OnCommand(HWND hDlg, WORD wNotifyCode, WORD wID, HWND hwndCtl)
  285. {
  286. BOOL bResult = FALSE;
  287. if (BN_CLICKED == wNotifyCode) // allrespond to clicked
  288. {
  289. switch (wID)
  290. {
  291. case IDC_SCHEDADD:
  292. {
  293. StartScheduleWizard();
  294. HWND hwndList = GetDlgItem(hDlg, IDC_SCHEDLIST);
  295. BOOL fEnable = ListView_GetSelectedCount(hwndList)? TRUE: FALSE;
  296. SetButtonState(IDC_SCHEDEDIT,fEnable);
  297. SetButtonState(IDC_SCHEDREMOVE,fEnable);
  298. }
  299. break;
  300. case IDC_SCHEDREMOVE:
  301. {
  302. HWND hwndList = GetDlgItem(hDlg, IDC_SCHEDLIST);
  303. int iItem = ListView_GetSelectionMark(hwndList);
  304. RemoveTask(iItem);
  305. BOOL fEnable = ListView_GetSelectedCount(hwndList)? TRUE: FALSE;
  306. SetButtonState(IDC_SCHEDEDIT,fEnable);
  307. SetButtonState(IDC_SCHEDREMOVE,fEnable);
  308. }
  309. break;
  310. case IDC_SCHEDEDIT:
  311. {
  312. HWND hwndList = GetDlgItem(hDlg, IDC_SCHEDLIST);
  313. int iItem = ListView_GetSelectionMark(hwndList);
  314. EditTask(iItem);
  315. BOOL fEnable = ListView_GetSelectedCount(hwndList)? TRUE: FALSE;
  316. SetButtonState(IDC_SCHEDEDIT,fEnable);
  317. SetButtonState(IDC_SCHEDREMOVE,fEnable);
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. return bResult;
  325. }
  326. //+-------------------------------------------------------------------------------
  327. //
  328. // FUNCTION: BOOL CSchedSyncPage::EditTask(int iItem)
  329. //
  330. // PURPOSE: edits the selected task
  331. //
  332. // RETURN VALUE: return TRUE if we process it ok.
  333. //
  334. //+-------------------------------------------------------------------------------
  335. BOOL CSchedSyncPage::EditTask(int iItem)
  336. {
  337. LV_ITEM lvItem;
  338. WCHAR pwszScheduleName[MAX_PATH + 1];
  339. DWORD dwSize = MAX_PATH;
  340. ZeroMemory(&lvItem, sizeof(lvItem));
  341. lvItem.mask = LVIF_PARAM;
  342. lvItem.iItem = iItem;
  343. ListView_GetItem(GetDlgItem(m_hwnd, IDC_SCHEDLIST), &lvItem);
  344. if (lvItem.iItem != -1)
  345. {
  346. ISyncSchedule *pISyncSched = (ISyncSchedule *) lvItem.lParam;
  347. //Start on the scheduled items page
  348. pISyncSched->EditSyncSchedule(m_hwnd, 0);
  349. pISyncSched->GetScheduleName(&dwSize, pwszScheduleName);
  350. ListView_SetItemText( GetDlgItem(m_hwnd,IDC_SCHEDLIST), iItem, 0,pwszScheduleName);
  351. }
  352. return TRUE;
  353. }
  354. //+-------------------------------------------------------------------------------
  355. //
  356. // FUNCTION: BOOL CSchedSyncPage::RemoveTask(int iItem)
  357. //
  358. // PURPOSE: removes the selected task
  359. //
  360. // RETURN VALUE: return TRUE if we process it ok.
  361. //
  362. //+-------------------------------------------------------------------------------
  363. // Review - Why can't we just alloc what we need instead of eating up the stack.
  364. BOOL CSchedSyncPage::RemoveTask(int iItem)
  365. {
  366. TCHAR ptszScheduleName[MAX_PATH + 1];
  367. WCHAR pwszScheduleName[MAX_PATH + 1];
  368. TCHAR szFmt[MAX_PATH];
  369. TCHAR szTitle[MAX_PATH];
  370. TCHAR szStr[MAX_PATH];
  371. SYNCSCHEDULECOOKIE SyncSchedCookie;
  372. DWORD dwSize = MAX_PATH;
  373. if (!m_pISyncSchedMgr)
  374. {
  375. return FALSE;
  376. }
  377. LV_ITEM lvItem;
  378. ZeroMemory(&lvItem, sizeof(lvItem));
  379. lvItem.mask = LVIF_PARAM;
  380. lvItem.iItem = iItem;
  381. ListView_GetItem(GetDlgItem(m_hwnd, IDC_SCHEDLIST), &lvItem);
  382. if (lvItem.iItem == -1)
  383. {
  384. return FALSE;
  385. }
  386. ISyncSchedule *pISyncSched = (ISyncSchedule *) lvItem.lParam;
  387. if (NULL == pISyncSched)
  388. {
  389. return FALSE;
  390. }
  391. if (NOERROR != pISyncSched->GetScheduleName(&dwSize,pwszScheduleName))
  392. {
  393. *ptszScheduleName = TEXT('\0');
  394. dwSize = 0;
  395. }
  396. else
  397. {
  398. ConvertString(ptszScheduleName,pwszScheduleName,MAX_PATH);
  399. }
  400. // make sure user really wants to delete this schedule
  401. LoadString(g_hmodThisDll, IDS_CONFIRMSCHEDDELETE_TITLE, szTitle, ARRAYLEN(szTitle));
  402. LoadString(g_hmodThisDll, IDS_CONFIRMSCHEDDELETE_TEXT, szFmt, ARRAYLEN(szFmt));
  403. wsprintf(szStr, szFmt,ptszScheduleName);
  404. if (IDNO == MessageBox(m_hwnd,szStr,szTitle,MB_YESNO | MB_ICONQUESTION))
  405. {
  406. return FALSE;
  407. }
  408. dwSize = MAX_PATH;
  409. //Get the Cookie from the schedule
  410. if (FAILED(((LPSYNCSCHEDULE)pISyncSched)->GetScheduleGUIDName
  411. (&dwSize,ptszScheduleName)))
  412. {
  413. return FALSE;
  414. }
  415. ptszScheduleName[GUIDSTR_MAX] = NULL;
  416. ConvertString(pwszScheduleName,ptszScheduleName,MAX_PATH);
  417. GUIDFromString(pwszScheduleName, &SyncSchedCookie);
  418. //release this pISyncSched
  419. pISyncSched->Release();
  420. m_pISyncSchedMgr->RemoveSchedule(&SyncSchedCookie);
  421. HWND hwndList = GetDlgItem(m_hwnd, IDC_SCHEDLIST);
  422. ListView_DeleteItem(hwndList, iItem);
  423. UpdateWindow(hwndList);
  424. return TRUE;
  425. }
  426. //+-------------------------------------------------------------------------------
  427. //
  428. // FUNCTION: StartScheduleWizard(HINSTANCE hinst)
  429. //
  430. // PURPOSE: Display the Onestop schedsync wizard
  431. //
  432. //
  433. //--------------------------------------------------------------------------------
  434. BOOL CSchedSyncPage::StartScheduleWizard()
  435. {
  436. SCODE sc;
  437. ISyncSchedule *pISyncSched;
  438. SYNCSCHEDULECOOKIE SyncSchedCookie = GUID_NULL;
  439. if (!m_pISyncSchedMgr)
  440. {
  441. return FALSE;
  442. }
  443. if (S_OK == (sc = m_pISyncSchedMgr->LaunchScheduleWizard(
  444. m_hwnd,
  445. 0,
  446. &SyncSchedCookie,
  447. &pISyncSched)))
  448. {
  449. TCHAR ptszBuf[MAX_PATH + 1];
  450. WCHAR pwszName[MAX_PATH + 1];
  451. DWORD dwSize = MAX_PATH;
  452. LV_ITEM lvItem;
  453. pISyncSched->GetScheduleName(&dwSize, pwszName);
  454. ConvertString(ptszBuf,pwszName,MAX_PATH);
  455. ZeroMemory(&lvItem, sizeof(lvItem));
  456. lvItem.mask = LVIF_TEXT | LVIF_PARAM;
  457. lvItem.pszText = ptszBuf;
  458. if (m_iDefaultIconImageIndex >= 0)
  459. {
  460. lvItem.mask |= LVIF_IMAGE;
  461. lvItem.iImage = m_iDefaultIconImageIndex;
  462. }
  463. //Save the ISyncSched pointer in the list view data
  464. lvItem.lParam = (LPARAM)pISyncSched;
  465. //add the item to the list
  466. ListView_InsertItem(GetDlgItem(m_hwnd,IDC_SCHEDLIST), &lvItem);
  467. return TRUE;
  468. }
  469. return FALSE;
  470. }