XTPCalendarResource.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:13k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPCalendarResource.cpp: implementation of the CXTPCalendarResource class.
  2. //
  3. // This file is a part of the XTREME CALENDAR MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Resource.h"
  22. //#include "Common/XTPResourceManager.h"
  23. #include "Common/XTPPropExchange.h"
  24. #include "XTPTopLevelWndMsgNotifier.h"
  25. #include "XTPCalendarUtils.h"
  26. #include "XTPCalendarNotifications.h"
  27. #include "XTPCalendarDayView.h"
  28. #include "XTPCalendarWeekView.h"
  29. #include "XTPCalendarMonthView.h"
  30. #include "XTPCalendarControl.h"
  31. #include "XTPCalendarData.h"
  32. #include "XTPCalendarMemoryDataProvider.h"
  33. #include "XTPCalendarDatabaseDataProvider.h"
  34. #include "XTPCalendarCustomDataProvider.h"
  35. #include "XTPCalendarCustomProperties.h"
  36. #include "XTPCalendarRemindersManager.h"
  37. #include "XTPCalendarOccurSeriesChooseDlg.h"
  38. #include "XTPCalendarResource.h"
  39. #ifdef _DEBUG
  40. #define new DEBUG_NEW
  41. #undef THIS_FILE
  42. static char THIS_FILE[] = __FILE__;
  43. #endif
  44. #define XTP_SCHEDULES_DATA_VER 2
  45. //////////////////////////////////////////////////////////////////////
  46. IMPLEMENT_DYNCREATE(CXTPCalendarResource, CCmdTarget)
  47. IMPLEMENT_DYNCREATE(CXTPCalendarResources, CCmdTarget)
  48. IMPLEMENT_DYNAMIC(CXTPCalendarSchedule, CCmdTarget)
  49. IMPLEMENT_DYNAMIC(CXTPCalendarSchedules, CCmdTarget)
  50. CXTPCalendarSchedule::CXTPCalendarSchedule(UINT uScheduleID, LPCTSTR pcszScheduleName)
  51. {
  52. m_uScheduleID = uScheduleID;
  53. ASSERT(pcszScheduleName);
  54. if (pcszScheduleName)
  55. {
  56. m_strScheduleName = pcszScheduleName;
  57. }
  58. m_pCustomProperties = new CXTPCalendarCustomProperties();
  59. }
  60. CXTPCalendarSchedule::~CXTPCalendarSchedule()
  61. {
  62. CMDTARGET_RELEASE(m_pCustomProperties)
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. CXTPCalendarSchedules::CXTPCalendarSchedules()
  66. {
  67. }
  68. CXTPCalendarSchedules::~CXTPCalendarSchedules()
  69. {
  70. }
  71. BOOL CXTPCalendarSchedules::AddNewSchedule(LPCTSTR pcszScheduleName)
  72. {
  73. ASSERT(pcszScheduleName);
  74. if (!pcszScheduleName)
  75. {
  76. return FALSE;
  77. }
  78. UINT uNewID = _GetNextID();
  79. CXTPCalendarSchedule* pNewSch = new CXTPCalendarSchedule(uNewID, pcszScheduleName);
  80. if (pNewSch)
  81. {
  82. Add(pNewSch, FALSE);
  83. }
  84. return pNewSch != NULL;
  85. }
  86. BOOL CXTPCalendarSchedules::RemoveSchedule(UINT uScheduleID)
  87. {
  88. int nFIdx = FindIndex(uScheduleID);
  89. if (nFIdx >= 0)
  90. {
  91. RemoveAt(nFIdx);
  92. }
  93. return nFIdx >= 0;
  94. }
  95. LPCTSTR CXTPCalendarSchedules::GetScheduleName(UINT uScheduleID) const
  96. {
  97. int nFIdx = FindIndex(uScheduleID);
  98. if (nFIdx >= 0)
  99. {
  100. CXTPCalendarSchedule* pSch = GetAt(nFIdx, FALSE);
  101. if (pSch)
  102. {
  103. return pSch->GetName();
  104. }
  105. }
  106. return NULL;
  107. }
  108. void CXTPCalendarSchedules::SetScheduleName(UINT uScheduleID, LPCTSTR pcszNewName)
  109. {
  110. int nFIdx = FindIndex(uScheduleID);
  111. if (nFIdx >= 0)
  112. {
  113. CXTPCalendarSchedule* pSch = GetAt(nFIdx, FALSE);
  114. if (pSch)
  115. {
  116. pSch->SetName(pcszNewName);
  117. }
  118. }
  119. }
  120. int CXTPCalendarSchedules::FindIndex(UINT uScheduleID) const
  121. {
  122. int nCount = GetCount();
  123. for (int i = 0; i < nCount; i++)
  124. {
  125. CXTPCalendarSchedule* pSch = GetAt(i, FALSE);
  126. if (pSch && pSch->GetID() == uScheduleID)
  127. {
  128. return i;
  129. }
  130. }
  131. return -1;
  132. }
  133. UINT CXTPCalendarSchedules::_GetNextID() const
  134. {
  135. UINT uMaxID = 0;
  136. int nCount = GetCount();
  137. for (int i = 0; i < nCount; i++)
  138. {
  139. CXTPCalendarSchedule* pSch = GetAt(i, FALSE);
  140. if (pSch && uMaxID < pSch->GetID())
  141. {
  142. uMaxID = pSch->GetID();
  143. }
  144. }
  145. return uMaxID + 1;
  146. }
  147. void CXTPCalendarSchedules::DoPropExchange(CXTPPropExchange* pPX)
  148. {
  149. if (!pPX)
  150. {
  151. ASSERT(FALSE);
  152. return;
  153. }
  154. if (pPX->IsLoading())
  155. {
  156. _Load(pPX);
  157. }
  158. else
  159. {
  160. _Save(pPX);
  161. }
  162. }
  163. void CXTPCalendarSchedules::_Save(CXTPPropExchange* pPX)
  164. {
  165. if (!pPX || !pPX->IsStoring())
  166. {
  167. ASSERT(FALSE);
  168. return;
  169. }
  170. CMDTARGET_ADDREF(pPX);
  171. CXTPPropExchangeSection secSchedules(pPX); //->GetSection(_T("EventsSchedules")));
  172. secSchedules->EmptySection();
  173. long nVersion = XTP_SCHEDULES_DATA_VER;
  174. PX_Long(&secSchedules, _T("Version"), nVersion, XTP_SCHEDULES_DATA_VER);
  175. int nCount = GetCount();
  176. CXTPPropExchangeEnumeratorPtr pEnumerator(secSchedules->GetEnumerator(_T("Schedule")));
  177. POSITION posStorage = pEnumerator->GetPosition(nCount);
  178. int nSavedCount = 0;
  179. for (int i = 0; i < nCount; i++)
  180. {
  181. CXTPCalendarSchedule* pSch = GetAt(i, FALSE);
  182. ASSERT(pSch);
  183. if (!pSch)
  184. {
  185. continue;
  186. }
  187. ULONG ulID = pSch->GetID();
  188. CString strName = pSch->GetName();
  189. CXTPPropExchangeSection secSch(pEnumerator->GetNext(posStorage));
  190. PX_ULong(&secSch, _T("ID"), ulID);
  191. PX_String(&secSch, _T("Name"), strName);
  192. if (pSch->GetCustomProperties())
  193. pSch->GetCustomProperties()->DoPropExchange(&secSch);
  194. nSavedCount++;
  195. }
  196. ASSERT(nSavedCount == nCount);
  197. }
  198. void CXTPCalendarSchedules::_Load(CXTPPropExchange* pPX)
  199. {
  200. if (!pPX || !pPX->IsLoading())
  201. {
  202. ASSERT(FALSE);
  203. return;
  204. }
  205. RemoveAll();
  206. CMDTARGET_ADDREF(pPX);
  207. CXTPPropExchangeSection secSchedules(pPX); //->GetSection(_T("EventsSchedules")));
  208. long nVersion = 0;
  209. PX_Long(&secSchedules, _T("Version"), nVersion, XTP_SCHEDULES_DATA_VER);
  210. if (nVersion != XTP_SCHEDULES_DATA_VER)
  211. {
  212. TRACE(_T("ERROR! XTPCalendarSchedules: Unsupported data file version. (%d) n"), nVersion);
  213. return;
  214. }
  215. CXTPPropExchangeEnumeratorPtr pEnumerator(secSchedules->GetEnumerator(_T("Schedule")));
  216. POSITION posStorage = pEnumerator->GetPosition();
  217. while (posStorage)
  218. {
  219. ULONG ulID;
  220. CString strName;
  221. CXTPPropExchangeSection secSch(pEnumerator->GetNext(posStorage));
  222. PX_ULong(&secSch, _T("ID"), ulID, 0);
  223. PX_String(&secSch, _T("Name"), strName);
  224. CXTPCalendarSchedule* pNewSch = new CXTPCalendarSchedule(ulID, strName);
  225. if (!pNewSch)
  226. {
  227. return;
  228. }
  229. if (nVersion >= 2)
  230. {
  231. XTP_SAFE_CALL2(pNewSch, GetCustomProperties(), DoPropExchange(&secSch));
  232. }
  233. Add(pNewSch, FALSE);
  234. }
  235. }
  236. ///////////////////////////////////////////////////////////////////////////////
  237. CXTPCalendarResource::CXTPCalendarResource(CXTPCalendarControl* pCalendarCtrl)
  238. : m_pCalendarCtrl(pCalendarCtrl)
  239. {
  240. m_pDataProvider = NULL;
  241. m_bCloseDataProviderWhenDestroy = TRUE;
  242. }
  243. CXTPCalendarResource::~CXTPCalendarResource()
  244. {
  245. if (m_pDataProvider)
  246. {
  247. if (m_bCloseDataProviderWhenDestroy && m_pDataProvider->IsOpen())
  248. {
  249. m_pDataProvider->Close();
  250. }
  251. CMDTARGET_RELEASE(m_pDataProvider);
  252. }
  253. }
  254. void CXTPCalendarResource::SetDataProvider(CXTPCalendarData* pDataProvider,
  255.    BOOL bCloseDataProviderWhenDestroy)
  256. {
  257. CMDTARGET_ADDREF(pDataProvider);
  258. CMDTARGET_RELEASE(m_pDataProvider);
  259. m_pDataProvider = pDataProvider;
  260. m_bCloseDataProviderWhenDestroy = bCloseDataProviderWhenDestroy;
  261. }
  262. CXTPCalendarEventsPtr CXTPCalendarResource::RetrieveDayEvents(COleDateTime dtDay)
  263. {
  264. // get current day date
  265. dtDay = CXTPCalendarUtils::ResetTime(dtDay);
  266. //
  267. // retrieve all events for this day from the corresponding data provider
  268. //
  269. CXTPCalendarEventsPtr ptrEvents = m_pDataProvider ? m_pDataProvider->RetrieveDayEvents(dtDay) : 
  270. new CXTPCalendarEvents();
  271. FilterEventsByScheduleID(ptrEvents);
  272. return ptrEvents;
  273. }
  274. BOOL CXTPCalendarResource::ExistsScheduleID(UINT uScheduleID,
  275. BOOL bReturnTrueIfSchedulesSetEmpty)
  276. {
  277. const int nScheduleIDCount = (int)m_arScheduleIDs.GetSize();
  278. if (nScheduleIDCount == 0)
  279. {
  280. return bReturnTrueIfSchedulesSetEmpty;
  281. }
  282. // iterate all ScheduleID's
  283. for (int i = 0; i < nScheduleIDCount; i++)
  284. {
  285. UINT uScheduleID_I = m_arScheduleIDs.GetAt(i);
  286. if (uScheduleID_I == uScheduleID)
  287. {
  288. return TRUE;
  289. }
  290. }
  291. return FALSE;
  292. }
  293. void CXTPCalendarResource::FilterEventsByScheduleID(CXTPCalendarEvents* pEvents)
  294. {
  295. // filter all events by ScheduleID collection
  296. //
  297. const int nScheduleIDCount = (int)m_arScheduleIDs.GetSize();
  298. // do not filter by schedule ID if no schedules set
  299. if (nScheduleIDCount && pEvents)
  300. {
  301. const int nEventCount = pEvents->GetCount();
  302. for (int nEvent = nEventCount - 1; nEvent >= 0; nEvent--)
  303. {
  304. BOOL bRemove = TRUE;
  305. // get next event from the collection
  306. CXTPCalendarEvent* pEvent = pEvents->GetAt(nEvent, FALSE);
  307. ASSERT(pEvent);
  308. if (pEvent)
  309. {
  310. UINT uScheduleIDEvent = pEvent->GetScheduleID();
  311. bRemove = !ExistsScheduleID(uScheduleIDEvent, TRUE);
  312. }
  313. if (bRemove)
  314. {
  315. pEvents->RemoveAt(nEvent);
  316. }
  317. }
  318. }
  319. }
  320. //////////////////////////////////////////////////////////////////////////
  321. //
  322. // Resources collection
  323. //
  324. //
  325. CXTPCalendarResources::CXTPCalendarResources()
  326. {
  327. }
  328. CXTPCalendarResources::~CXTPCalendarResources()
  329. {
  330. }
  331. /////////////////////////////////////////////////////////////////////////////
  332. CXTPCalendarResourcesNf::CXTPCalendarResourcesNf()
  333. {
  334. m_pConnection = new CXTPNotifyConnection_internal();
  335. }
  336. CXTPCalendarResourcesNf::~CXTPCalendarResourcesNf()
  337. {
  338. CXTPNotifySink::UnadviseAll();
  339. CMDTARGET_RELEASE(m_pConnection);
  340. }
  341. void CXTPCalendarResourcesNf::ReBuildInternalData()
  342. {
  343. UnadviseAll();
  344. // rebuild optimized data
  345. m_arResourcesGroupedByDP.RemoveAll();
  346. int nRCCount = GetCount();
  347. for (int nRCNr = 0; nRCNr < nRCCount; nRCNr++)
  348. {
  349. CXTPCalendarResource* pRCorig = GetAt(nRCNr, FALSE);
  350. if (!pRCorig || !pRCorig->GetDataProvider() ||
  351. !pRCorig->GetSchedules())
  352. {
  353. ASSERT(FALSE);
  354. continue;
  355. }
  356. CXTPCalendarResource* pRCuniq = FindByDataProvider(&m_arResourcesGroupedByDP, pRCorig->GetDataProvider());
  357. if (!pRCuniq)
  358. {
  359. pRCuniq = new CXTPCalendarResource();
  360. if (!pRCuniq)
  361. {
  362. return;
  363. }
  364. pRCuniq->SetDataProvider(pRCorig->GetDataProvider(), FALSE);
  365. m_arResourcesGroupedByDP.Add(pRCuniq, FALSE);
  366. }
  367. int nSchCount = (int)pRCorig->GetSchedules()->GetSize();
  368. for (int j = 0; j < nSchCount; j++)
  369. {
  370. UINT nSchID = pRCorig->GetSchedules()->GetAt(j);
  371. if (!pRCuniq->ExistsScheduleID(nSchID, FALSE) )
  372. {
  373. pRCuniq->GetSchedules()->Add(nSchID);
  374. }
  375. }
  376. }
  377. //-------------------------
  378. if (!m_pConnection)
  379. {
  380. return;
  381. }
  382. // get notify IDs ---------
  383. CMap<XTP_NOTIFY_CODE, XTP_NOTIFY_CODE, int, int> mapNfIDs;
  384. int nConnCount = (int)m_pConnection->m_arrConnections.GetSize();
  385. for (int nConnNr = 0; nConnNr < nConnCount; nConnNr++)
  386. {
  387. CXTPNotifyConnection_internal::CONNECTION_DESCRIPTOR* pConnDesc =
  388. m_pConnection->m_arrConnections[nConnNr];
  389. ASSERT(pConnDesc);
  390. if (!pConnDesc)
  391. {
  392. continue;
  393. }
  394. mapNfIDs[pConnDesc->dwNotifyCode] = 1;
  395. }
  396. // advice
  397. POSITION posNfID = mapNfIDs.GetStartPosition();
  398. while (posNfID)
  399. {
  400. XTP_NOTIFY_CODE nfCode = 0;
  401. int nTmp;
  402. mapNfIDs.GetNextAssoc(posNfID, nfCode, nTmp);
  403. int nDPCount = m_arResourcesGroupedByDP.GetCount();
  404. for (int i = 0; i < nDPCount; i++)
  405. {
  406. CXTPNotifyConnection* pDPconn = XTP_SAFE_GET2(m_arResourcesGroupedByDP.GetAt(i),
  407. GetDataProvider(), GetConnection(), NULL);
  408. ASSERT(pDPconn);
  409. if (pDPconn)
  410. {
  411. CXTPNotifySink::Advise(pDPconn, nfCode);
  412. }
  413. }
  414. }
  415. }
  416. CXTPCalendarResource* CXTPCalendarResourcesNf::FindByDataProvider(
  417. CXTPCalendarResources* pResources,
  418. CXTPCalendarData* pData)
  419. {
  420. if (!pResources || !pData)
  421. {
  422. ASSERT(FALSE);
  423. return NULL;
  424. }
  425. int nCount = pResources->GetCount();
  426. for (int i = 0; i < nCount; i++)
  427. {
  428. CXTPCalendarResource* pRC = pResources->GetAt(i);
  429. ASSERT(pRC);
  430. if (pRC && pRC->GetDataProvider() == pData)
  431. {
  432. return pRC;
  433. }
  434. }
  435. return NULL;
  436. }
  437. void CXTPCalendarResourcesNf::OnEvent(XTP_NOTIFY_CODE dwNotifyCode, WPARAM wParam,
  438.   LPARAM lParam, DWORD dwFlags)
  439. {
  440. UNUSED_ALWAYS(dwFlags);
  441. if (dwNotifyCode == XTP_NC_CALENDAREVENTWASADDED ||
  442. dwNotifyCode == XTP_NC_CALENDAREVENTWASDELETED ||
  443. dwNotifyCode == XTP_NC_CALENDAREVENTWASCHANGED)
  444. {
  445. CXTPCalendarEvent* pEvent = (CXTPCalendarEvent*)lParam;
  446. ASSERT(pEvent);
  447. if (!pEvent)
  448. {
  449. return;
  450. }
  451. CXTPCalendarResource* pRC = FindByDataProvider(&m_arResourcesGroupedByDP, pEvent->GetDataProvider());
  452. UINT uSchID = pEvent->GetScheduleID();
  453. if (!pRC || !pRC->ExistsScheduleID(uSchID, TRUE))
  454. {
  455. return;
  456. }
  457. }
  458. ASSERT(GetConnection());
  459. if (GetConnection())
  460. {
  461. GetConnection()->SendEvent(dwNotifyCode, wParam, lParam, dwFlags);
  462. }
  463. }
  464. /////////////////////////////////////////////////////////////////////////////