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.

315 lines
12 KiB

  1. //==========================================================================
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright 1998 - 1999 Microsoft Corporation. All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------
  11. #ifndef _HANDER_IMPL_
  12. #define _HANDER_IMPL_
  13. // itemList used keeping track of items to sync.
  14. typedef struct _tagHANDLERITEM_SYNCSTATUS
  15. {
  16. GENERICITEM genericItem;
  17. SYNCMGRITEMID ItemID;
  18. BOOL fCancelled; // set if cancel comes in after called to PrePrepareForSync.
  19. BOOL fSynchronizing; // set when actually performing a sync.
  20. BOOL fSynchronizeComplete; // set when actually performing a sync.
  21. BOOL fUnresolvedConflicts; // conflict occured during sync and were not resolved
  22. DWORD dwSyncMgrResultStatus; // result of sync on item.
  23. } HANDLERITEM_SYNCSTATUS;
  24. typedef HANDLERITEM_SYNCSTATUS *LPHANDLERITEM_SYNCSTATUS;
  25. // file object used to keep track of items.
  26. typedef struct _tagFILEOBJECT
  27. {
  28. GENERICITEM genericItem;
  29. TCHAR fName[MAX_PATH];
  30. BOOL fDirectory;
  31. BOOL fLastUpdateValid;
  32. FILETIME ftLastUpdate;
  33. LPGENERICITEMLIST pChildList;
  34. } FILEOBJECT;
  35. typedef FILEOBJECT *LPFILEOBJECT;
  36. typedef GENERICITEMLIST FILEOBJECTLIST;
  37. typedef LPGENERICITEMLIST LPFILEOBJECTLIST;
  38. class CSyncMgrHandler : public ISyncMgrSynchronize, public CLockHandler
  39. {
  40. private:
  41. DWORD m_dwSyncFlags;
  42. DWORD m_cRef;
  43. LPSYNCMGRSYNCHRONIZECALLBACK m_pSyncMgrSynchronizeCallback;
  44. LPGENERICITEMLIST m_pItemsToSyncList;
  45. HANDLE m_phThread; // handle to worker thread
  46. HWND m_hwndWorker; // hwnd of WorkerThread hwnd.
  47. HWND m_hwndHandler; // hwnd of window on same thread as handler.
  48. BOOL m_fSynchronizing; // flag set when actually synchronizing data.
  49. BOOL m_fPrepareForSync; // flag set when actually synchronizing data.
  50. BOOL m_fStopped; // indicates if a StopSetItemStatus has come in.
  51. public:
  52. CSyncMgrHandler();
  53. ~CSyncMgrHandler();
  54. /* IUnknown */
  55. STDMETHODIMP QueryInterface(REFIID, LPVOID FAR *);
  56. STDMETHODIMP_(ULONG) AddRef();
  57. STDMETHODIMP_(ULONG) Release();
  58. /* ISyncMgrSynchronize methods */
  59. STDMETHODIMP Initialize(DWORD dwReserved,DWORD dwSyncFlags,
  60. DWORD cbCookie,const BYTE *lpCooke);
  61. STDMETHODIMP GetHandlerInfo(LPSYNCMGRHANDLERINFO *ppSyncMgrHandlerInfo);
  62. STDMETHODIMP EnumSyncMgrItems(ISyncMgrEnumItems **ppenumOffineItems);
  63. STDMETHODIMP GetItemObject(REFSYNCMGRITEMID ItemID,REFIID riid,void** ppv);
  64. STDMETHODIMP ShowProperties(HWND hWndParent,REFSYNCMGRITEMID ItemID);
  65. STDMETHODIMP SetProgressCallback(ISyncMgrSynchronizeCallback *lpCallBack);
  66. STDMETHODIMP PrepareForSync(ULONG cbNumItems,SYNCMGRITEMID* pItemIDs,HWND hWndParent,
  67. DWORD dwReserved);
  68. STDMETHODIMP Synchronize(HWND hWndParent);
  69. STDMETHODIMP SetItemStatus(REFSYNCMGRITEMID ItemID,DWORD dwSyncMgrStatus);
  70. STDMETHODIMP ShowError(HWND hWndParent,REFSYNCMGRERRORID ErrorID);
  71. // methods called on worker thread.
  72. void ShowPropertiesCall(HWND hWndParent,REFSYNCMGRITEMID ItemID);
  73. void PrepareForSyncCall(ULONG cbNumItems,SYNCMGRITEMID* pItemIDs,HWND hWndParent,
  74. DWORD dwReserved);
  75. void SynchronizeCall(HWND hWndParent);
  76. void ShowErrorCall(HWND hWndParent,REFSYNCMGRERRORID ErrorID);
  77. private:
  78. STDMETHODIMP CreateWorkerThread();
  79. LPSYNCMGRSYNCHRONIZECALLBACK GetProgressCallback();
  80. void Progress(ISyncMgrSynchronizeCallback *lpCallBack,REFSYNCMGRITEMID pItemID,
  81. UINT mask,TCHAR *pStatusText,DWORD dwStatusType,
  82. int iProgValue,int iMaxValue);
  83. void ProgressSetItemStatusType(ISyncMgrSynchronizeCallback *lpCallBack,
  84. REFSYNCMGRITEMID pItemID,DWORD dwSyncMgrStatus);
  85. void ProgressSetItemStatusText(ISyncMgrSynchronizeCallback *lpCallBack,
  86. REFSYNCMGRITEMID pItemID,TCHAR *pStatusText);
  87. void ProgressSetItemProgValue(ISyncMgrSynchronizeCallback *lpCallBack,
  88. REFSYNCMGRITEMID pItemID,int iProgValue);
  89. void ProgressSetItemProgMaxValue(ISyncMgrSynchronizeCallback *lpCallBack,
  90. REFSYNCMGRITEMID pItemID,int iProgMaxValue);
  91. void LogError(ISyncMgrSynchronizeCallback *lpCallBack,REFSYNCMGRITEMID pItemID,
  92. DWORD dwErrorLevel,TCHAR *lpErrorText,DWORD mask,DWORD dwSyncMgrErrorFlags,
  93. SYNCMGRERRORID ErrorID);
  94. void LogError(ISyncMgrSynchronizeCallback *lpCallBack,REFSYNCMGRITEMID pItemID,
  95. DWORD dwErrorLevel,TCHAR *lpErrorText);
  96. void LogError(ISyncMgrSynchronizeCallback *lpCallBack,
  97. DWORD dwErrorLevel,TCHAR *lpErrorText);
  98. // helper methods specific to this handler.
  99. LPFILEOBJECTLIST CreateDirFileListFromPath(TCHAR *pDirName,BOOL fRecursive);
  100. LPFILEOBJECTLIST GetFilesForDir(TCHAR *pDirName,BOOL fRecursive);
  101. void ReleaseFileObjectList(LPFILEOBJECTLIST pfObjList,BOOL fRecursive);
  102. ULONG CountNumberOfFilesInList(LPFILEOBJECTLIST pfObjList,BOOL fRecursive);
  103. LPFILEOBJECT FindFileItemWithName(LPFILEOBJECTLIST pDir,TCHAR *pfName);
  104. BOOL CopyFileFullPath(TCHAR *pszFile1,TCHAR *pszFile2);
  105. void CopyFiles( LPHANDLERITEM_SYNCSTATUS pHandlerItemID,
  106. LPHANDLERITEMSETTINGS pHandlerSyncItem,
  107. LPSYNCMGRSYNCHRONIZECALLBACK pCallback,
  108. DWORD *pdwCurProgValue,TCHAR *pszDir1,LPFILEOBJECTLIST pDir1,
  109. TCHAR *pszDir2);
  110. void ReconcileDir(HWND hWndParent,LPHANDLERITEM_SYNCSTATUS pHandlerItemID,
  111. LPHANDLERITEMSETTINGS pHandlerSyncItem,
  112. LPSYNCMGRSYNCHRONIZECALLBACK pCallback,
  113. DWORD *pdwCurProgValue,
  114. TCHAR *pszDir1,LPFILEOBJECTLIST pDir1,
  115. TCHAR *pszDir2,LPFILEOBJECTLIST pDir2);
  116. void SyncDirs(HWND hWndParent,LPHANDLERITEM_SYNCSTATUS pHandlerItemID,
  117. LPHANDLERITEMSETTINGS pHandlerSyncItem);
  118. friend LRESULT CALLBACK HandlerThreadWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
  119. };
  120. // wrapper object for Callback so can send any callback messges back to
  121. // the main thread we were created on to conform to COM standards
  122. class CCallbackWrapper: public ISyncMgrSynchronizeCallback
  123. {
  124. public:
  125. CCallbackWrapper(HWND hwndCallback);
  126. ~CCallbackWrapper();
  127. //IUnknown methods
  128. STDMETHODIMP QueryInterface(REFIID, LPVOID FAR *);
  129. STDMETHODIMP_(ULONG) AddRef();
  130. STDMETHODIMP_(ULONG) Release();
  131. // Callback methods.
  132. STDMETHODIMP Progress(REFSYNCMGRITEMID ItemID,LPSYNCMGRPROGRESSITEM lpSyncProgressItem);
  133. STDMETHODIMP PrepareForSyncCompleted(HRESULT hr);
  134. STDMETHODIMP SynchronizeCompleted(HRESULT hr);
  135. STDMETHODIMP ShowPropertiesCompleted(HRESULT hr);
  136. STDMETHODIMP ShowErrorCompleted(HRESULT hr,ULONG cbNumItems,SYNCMGRITEMID *pItemIDs);
  137. STDMETHODIMP EnableModeless(BOOL fEnable);
  138. STDMETHODIMP LogError(DWORD dwErrorLevel,const WCHAR *lpcErrorText,LPSYNCMGRLOGERRORINFO lpSyncLogError);
  139. STDMETHODIMP DeleteLogError(REFSYNCMGRERRORID ErrorID,DWORD dwReserved);
  140. STDMETHODIMP EstablishConnection( WCHAR const * lpwszConnection, DWORD dwReserved);
  141. private:
  142. ULONG m_cRef;
  143. HWND m_hwndCallback;
  144. };
  145. // CSyncMgrHandler creates a worker thread in ::Initialize to use for async syncmgrCalls.
  146. // the following are delclarations for the worker thread.
  147. // structure passed in CreateThread.
  148. typedef struct _tagWorkerThreadArgs
  149. {
  150. /* [in ] */ HANDLE hEvent; // Event to wait on for new thread to be created.
  151. /* [in ] */ CSyncMgrHandler *pThis; // point to class worker thread for
  152. /* [out] */ HWND hwnd; // on return code of NOERROR contains workerThread hwnd.
  153. /* [out] */ HRESULT hr; // return code of thread.
  154. } WorkerThreadArgs;
  155. DWORD WINAPI WorkerThread( LPVOID lpArg );
  156. // structures and Wnd messages for posting to Worker Threads hwnd.
  157. typedef struct _tagPREPAREFORSYNCMSG
  158. {
  159. ULONG cbNumItems;
  160. HWND hWndParent;
  161. DWORD dwReserved;
  162. SYNCMGRITEMID* pItemIDs;
  163. } PREPAREFORSYNCMSG;
  164. typedef struct _tagSYNCHRONIZEMSG
  165. {
  166. HWND hWndParent;
  167. } SYNCHRONIZEMSG;
  168. typedef struct _tagSHOWPROPERTIESMSG
  169. {
  170. HWND hWndParent;
  171. SYNCMGRITEMID ItemID;
  172. } SHOWPROPERTIESMSG;
  173. typedef struct _tagSHOWERRORMSG
  174. {
  175. HWND hWndParent;
  176. SYNCMGRERRORID ErrorID;
  177. } SHOWERRORMSG;
  178. typedef struct _tagPROGRESSMSG
  179. {
  180. SYNCMGRITEMID ItemID;
  181. LPSYNCMGRPROGRESSITEM lpSyncProgressItem;
  182. } PROGRESSMSG;
  183. typedef struct _tagPREPAREFORSYNCCOMPLETEDMSG
  184. {
  185. HRESULT hr;
  186. } PREPAREFORSYNCCOMPLETEDMSG;
  187. typedef struct _tagSYNCHRONIZECOMPLETEDMSG
  188. {
  189. HRESULT hr;
  190. } SYNCHRONIZECOMPLETEDMSG;
  191. typedef struct _tagSHOWPROPERTIESCOMPLETEDMSG
  192. {
  193. HRESULT hr;
  194. } SHOWPROPERTIESCOMPLETEDMSG;
  195. typedef struct _tagSHOWERRORCOMPLETEDMSG
  196. {
  197. HRESULT hr;
  198. ULONG cbNumItems;
  199. SYNCMGRITEMID *pItemIDs;
  200. } SHOWERRORCOMPLETEDMSG;
  201. typedef struct _tagENABLEMODELESSMSG
  202. {
  203. BOOL fEnable;
  204. } ENABLEMODELESSMSG;
  205. typedef struct _tagLOGERRORMSG
  206. {
  207. DWORD dwErrorLevel;
  208. const WCHAR *lpcErrorText;
  209. LPSYNCMGRLOGERRORINFO lpSyncLogError;
  210. } LOGERRORMSG;
  211. typedef struct _tagDELETELOGERRORMSG
  212. {
  213. SYNCMGRERRORID ErrorID;
  214. DWORD dwReserved;
  215. } DELETELOGERRORMSG;
  216. typedef struct _tagESTABLISHCONNECTIONMSG
  217. {
  218. WCHAR const * lpwszConnection;
  219. DWORD dwReserved;
  220. } ESTABLISHCONNECTIONMSG;
  221. typedef struct _tagMETHODARGS
  222. {
  223. DWORD dwWorkerMsg;
  224. BOOL fAsync; // indicates this is an async call.
  225. HRESULT hr; // only valid for synchronouse calls
  226. union
  227. {
  228. PREPAREFORSYNCMSG PrepareForSyncMsg;
  229. SYNCHRONIZEMSG SynchronizeMsg;
  230. SHOWPROPERTIESMSG ShowPropertiesMsg;
  231. SHOWERRORMSG ShowErrorMsg;
  232. PROGRESSMSG ProgressMsg;
  233. PREPAREFORSYNCCOMPLETEDMSG PrepareForSyncCompletedMsg;
  234. SYNCHRONIZECOMPLETEDMSG SynchronizeCompletedMsg;
  235. SHOWPROPERTIESCOMPLETEDMSG ShowPropertiesCompletedMsg;
  236. SHOWERRORCOMPLETEDMSG ShowErrorCompletedMsg;
  237. ENABLEMODELESSMSG EnableModelessMsg;
  238. LOGERRORMSG LogErrorMsg;
  239. DELETELOGERRORMSG DeleteLogErrorMsg;
  240. ESTABLISHCONNECTIONMSG EstablishConnectionMsg;
  241. };
  242. } METHODARGS; // list of possible member for calling ThreadWndProc
  243. // definitions for handler messages
  244. #define WM_WORKERMSG_SHOWPROPERTIES (WM_USER+1)
  245. #define WM_WORKERMSG_PREPFORSYNC (WM_USER+2)
  246. #define WM_WORKERMSG_SYNCHRONIZE (WM_USER+3)
  247. #define WM_WORKERMSG_SHOWERROR (WM_USER+4)
  248. #define WM_WORKERMSG_RELEASE (WM_USER+5)
  249. // worker messages for Callback wrapper
  250. #define WM_WORKERMSG_PROGRESS (WM_USER+20)
  251. #define WM_WORKERMSG_PREPAREFORSYNCCOMPLETED (WM_USER+21)
  252. #define WM_WORKERMSG_SYNCHRONIZECOMPLETED (WM_USER+22)
  253. #define WM_WORKERMSG_SHOWPROPERTIESCOMPLETED (WM_USER+23)
  254. #define WM_WORKERMSG_SHOWERRORCOMPLETED (WM_USER+24)
  255. #define WM_WORKERMSG_ENABLEMODELESS (WM_USER+25)
  256. #define WM_WORKERMSG_LOGERROR (WM_USER+26)
  257. #define WM_WORKERMSG_DELETELOGERROR (WM_USER+27)
  258. #define WM_WORKERMSG_ESTABLISHCONNECTION (WM_USER+28)
  259. #define DWL_THREADWNDPROCCLASS 0 // window long offset to MsgService Hwnd this ptr.
  260. #define SZ_SAMPLESYNCMGRHANDLERWNDCLASS TEXT("Sample SyncMgr HandlerThread hwnd")
  261. #define SZ_SAMPLESYNCMGRWORKERWNDCLASS TEXT("Sample SyncMgr WorkThread hwnd")
  262. LRESULT CALLBACK HandlerThreadWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
  263. LRESULT CALLBACK WorkerThreadWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
  264. BOOL RegisterHandlerWndClasses(void);
  265. #endif // #define _HANDER_IMPL_