ProcessCopy.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. // ProcessCopy.cpp: implementation of the CProcessCopy class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "ProcessCopy.h"
  7. #include "DatabaseUtilities.h"
  8. #include ".\processcopy.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. /*----------------------------------------------------------------------------*\
  15. COleDataObjectEx
  16. \*----------------------------------------------------------------------------*/
  17. HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
  18. {
  19. HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
  20. if( hGlobal )
  21. return hGlobal;
  22. // The data isn't in global memory, so try getting an IStream interface to it.
  23. STGMEDIUM stg;
  24. if( !GetData(cfFormat, &stg) )
  25. {
  26. ASSERT(0); // does this ever happen?
  27. return 0;
  28. }
  29. switch(stg.tymed)
  30. {
  31. case TYMED_HGLOBAL:
  32. hGlobal = stg.hGlobal;
  33. break;
  34. case TYMED_ISTREAM:
  35. {
  36. UINT uDataSize;
  37. LARGE_INTEGER li;
  38. ULARGE_INTEGER uli;
  39. li.HighPart = li.LowPart = 0;
  40. if ( SUCCEEDED( stg.pstm->Seek ( li, STREAM_SEEK_END, &uli )))
  41. {
  42. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, uli.LowPart );
  43. void* pv = GlobalLock(hGlobal);
  44. stg.pstm->Seek(li, STREAM_SEEK_SET, NULL);
  45. HRESULT result = stg.pstm->Read(pv, uli.LowPart, (PULONG)&uDataSize);
  46. GlobalUnlock(hGlobal);
  47. if( FAILED(result) )
  48. hGlobal = GlobalFree(hGlobal);
  49. }
  50. break; // case TYMED_ISTREAM
  51. }
  52. } // end switch
  53. ReleaseStgMedium(&stg);
  54. return hGlobal;
  55. }
  56. /*----------------------------------------------------------------------------*\
  57. CClipFormat - holds the data of one clip format.
  58. \*----------------------------------------------------------------------------*/
  59. /*----------------------------------------------------------------------------*\
  60. CClipFormats - holds an array of CClipFormat
  61. \*----------------------------------------------------------------------------*/
  62. // returns a pointer to the CClipFormat in this array which matches the given type
  63. // or NULL if that type doesn't exist in this array.
  64. CClipFormat* CClipFormats::FindFormat( UINT cfType )
  65. {
  66. CClipFormat* pCF;
  67. int count = GetCount();
  68. for( int i=0; i < count; i++ )
  69. {
  70. pCF = &GetAt(i);
  71. if( pCF->m_cfType == cfType )
  72. return pCF;
  73. }
  74. return NULL;
  75. }
  76. /*----------------------------------------------------------------------------*\
  77. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  78. \*----------------------------------------------------------------------------*/
  79. CClip::CClip() : m_ID(0), m_lTotalCopySize(0)
  80. {}
  81. CClip::~CClip()
  82. {
  83. int count = m_Formats.GetCount();
  84. // in proper handling, m_Formats should be empty
  85. ASSERT( count == 0 );
  86. EmptyFormats();
  87. }
  88. void CClip::Clear()
  89. {
  90. m_ID = 0;
  91. m_Time = 0;
  92. m_Desc = "";
  93. m_lTotalCopySize = 0;
  94. EmptyFormats();
  95. }
  96. void CClip::EmptyFormats()
  97. {
  98. // free global memory in m_Formats
  99. for( int i = m_Formats.GetCount()-1; i >= 0; i-- )
  100. {
  101. m_Formats[i].Free();
  102. m_Formats.RemoveAt( i );
  103. }
  104. }
  105. // Adds a new Format to this Clip by copying the given data.
  106. bool CClip::AddFormat( CLIPFORMAT cfType, void* pData, UINT nLen )
  107. {
  108. ASSERT( pData && nLen );
  109. HGLOBAL hGlobal = ::NewGlobalP( pData, nLen );
  110. ASSERT( hGlobal );
  111. // update the Clip statistics
  112. m_Time = m_Time.GetCurrentTime();
  113. m_lTotalCopySize += nLen;
  114. if( !SetDescFromText( hGlobal ) )
  115. SetDescFromType();
  116. CClipFormat format(cfType,hGlobal);
  117. CClipFormat *pFormat;
  118. pFormat = m_Formats.FindFormat(cfType);
  119. // if the format type already exists as part of this clip, replace the data
  120. if( pFormat )
  121. {
  122. pFormat->Free();
  123. pFormat->m_hgData = format.m_hgData;
  124. }
  125. else
  126. m_Formats.Add(format);
  127. format.m_hgData = 0; // now owned by m_Formats
  128. return true;
  129. }
  130. #define EXIT_LoadFromClipboard(ret) { oleData.Release(); g_bCopyingClipboard = false; return(ret); }
  131. bool g_bCopyingClipboard = false; // for debugging reentrance
  132. // Fills this CClip with the contents of the clipboard.
  133. bool CClip::LoadFromClipboard( CClipTypes* pClipTypes )
  134. {
  135. COleDataObjectEx oleData;
  136. CClipTypes defaultTypes;
  137. CClipTypes* pTypes = pClipTypes;
  138. //ASSERT( !g_bCopyingClipboard );
  139. // For some reason, this can actually happen with *very* fast copies.
  140. // This is probably due to the OLE functions processing messages.
  141. // This *might* be able to be avoided by directly using the win32 Clipboard API
  142. // If this does happen, we just ignore the request by returning failure.
  143. if( g_bCopyingClipboard )
  144. return false;
  145. g_bCopyingClipboard = true;
  146. // m_Formats should be empty when this is called.
  147. ASSERT( m_Formats.GetCount() == 0 );
  148. //Attach to the clipboard
  149. if( !oleData.AttachClipboard() )
  150. {
  151. ASSERT(0); // does this ever happen?
  152. EXIT_LoadFromClipboard(false);
  153. }
  154. oleData.EnsureClipboardObject();
  155. // If the data is supposed to be private, then return
  156. if( oleData.IsDataAvailable( theApp.m_cfIgnoreClipboard ) )
  157. EXIT_LoadFromClipboard( false );
  158. // if no types were given, get only the first (most important) type.
  159. // (subsequent types could be synthetic due to automatic type conversions)
  160. if( pTypes == NULL || pTypes->GetCount() == 0 )
  161. {
  162. ASSERT(0); // this feature is not currently used... it is an error if it is.
  163. FORMATETC formatEtc;
  164. oleData.BeginEnumFormats();
  165. oleData.GetNextFormat(&formatEtc);
  166. defaultTypes.Add( formatEtc.cfFormat );
  167. pTypes = &defaultTypes;
  168. }
  169. // reset copy stats
  170. m_lTotalCopySize = 0;
  171. m_Desc = "[Ditto Error] !!BAD DESCRIPTION!!";
  172. // Get Description String
  173. // NOTE: We make sure that the description always corresponds to the
  174. // data saved by using the exact same globalmem instance as the source
  175. // for both... i.e. we only fetch the description format type once.
  176. CClipFormat cfDesc;
  177. bool bIsDescSet = false;
  178. cfDesc.m_cfType = CF_TEXT;
  179. if( oleData.IsDataAvailable( cfDesc.m_cfType ) )
  180. {
  181. cfDesc.m_hgData = oleData.GetGlobalData( cfDesc.m_cfType );
  182. bIsDescSet = SetDescFromText( cfDesc.m_hgData );
  183. }
  184. // Get global data for each supported type on the clipboard
  185. UINT nSize;
  186. CClipFormat cf;
  187. int numTypes = pTypes->GetCount();
  188. for(int i = 0; i < numTypes; i++)
  189. {
  190. cf.m_cfType = pTypes->GetAt(i);
  191. // is this the description we already fetched?
  192. if( cf.m_cfType == cfDesc.m_cfType )
  193. {
  194. cf = cfDesc;
  195. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  196. }
  197. else if( !oleData.IsDataAvailable(cf.m_cfType) )
  198. continue;
  199. else
  200. cf.m_hgData = oleData.GetGlobalData( cf.m_cfType );
  201. if( cf.m_hgData )
  202. {
  203. nSize = GlobalSize( cf.m_hgData );
  204. if( nSize > 0 )
  205. {
  206. m_Formats.Add( cf );
  207. m_lTotalCopySize += nSize;
  208. }
  209. else
  210. {
  211. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  212. cf.Free();
  213. }
  214. cf.m_hgData = 0; // m_Formats owns it now
  215. }
  216. }
  217. m_Time = CTime::GetCurrentTime();
  218. if( !bIsDescSet )
  219. SetDescFromType();
  220. // if the description was in a type that is not supported,
  221. // we have to free it since it wasn't added to m_Formats
  222. if( cfDesc.m_hgData )
  223. cfDesc.Free();
  224. if( m_Formats.GetSize() == 0 )
  225. EXIT_LoadFromClipboard( false );
  226. EXIT_LoadFromClipboard( true );
  227. }
  228. bool CClip::SetDescFromText( HGLOBAL hgData )
  229. {
  230. if( hgData == 0 )
  231. return false;
  232. bool bRet = false;
  233. char* text = (char *) GlobalLock(hgData);
  234. ULONG ulBufLen = GlobalSize(hgData);
  235. ASSERT( text != NULL );
  236. if( ulBufLen > LENGTH_OF_TEXT_SNIPET )
  237. ulBufLen = LENGTH_OF_TEXT_SNIPET;
  238. if( ulBufLen > 0 )
  239. {
  240. char* buf = m_Desc.GetBuffer(ulBufLen);
  241. memcpy(buf, text, ulBufLen); // in most cases, last char == null
  242. buf[ulBufLen-1] = '\0'; // just in case not null terminated
  243. m_Desc.ReleaseBuffer(); // scans for the null
  244. bRet = m_Desc.GetLength() > 0;
  245. }
  246. //Unlock the data
  247. GlobalUnlock(hgData);
  248. return bRet;
  249. }
  250. bool CClip::SetDescFromType()
  251. {
  252. if( m_Formats.GetCount() <= 0 )
  253. return false;
  254. m_Desc = GetFormatName( m_Formats[0].m_cfType );
  255. return m_Desc.GetLength() > 0;
  256. }
  257. bool CClip::AddToDB()
  258. {
  259. bool bResult;
  260. try
  261. {
  262. CMainTable recset;
  263. if( FindDuplicate( recset, g_Opt.m_bAllowDuplicates ) )
  264. {
  265. m_ID = recset.m_lID;
  266. recset.Edit();
  267. recset.m_lDate = (long) m_Time.GetTime(); // update the copy Time
  268. recset.Update();
  269. recset.Close();
  270. EmptyFormats(); // delete this clip's data from memory.
  271. return true;
  272. }
  273. if( recset.IsOpen() )
  274. recset.Close();
  275. }
  276. CATCHDAO
  277. bResult = AddToMainTable() && AddToDataTable();
  278. // should be emptied by AddToDataTable
  279. ASSERT( m_Formats.GetCount() == 0 );
  280. return bResult;
  281. }
  282. // if a duplicate exists, set recset to the duplicate and return true
  283. bool CClip::FindDuplicate( CMainTable& recset, BOOL bCheckLastOnly )
  284. {
  285. ASSERT( m_lTotalCopySize > 0 );
  286. try
  287. {
  288. recset.m_strSort = "lDate DESC";
  289. if( bCheckLastOnly )
  290. {
  291. recset.Open("SELECT * FROM Main");
  292. recset.MoveFirst();
  293. // if an entry exists and they are the same size and the format data matches
  294. if( !recset.IsBOF() && !recset.IsEOF() &&
  295. m_lTotalCopySize == recset.m_lTotalCopySize &&
  296. (CompareFormatDataTo(recset.m_lID) == 0) )
  297. { return true; }
  298. return false;
  299. }
  300. // Look for any other entries that have the same size
  301. recset.Open("SELECT * FROM Main WHERE lTotalCopySize = %d", m_lTotalCopySize);
  302. while( !recset.IsEOF() )
  303. {
  304. //if there is any then look if it is an exact match
  305. if( CompareFormatDataTo(recset.m_lID) == 0 )
  306. return true;
  307. recset.MoveNext();
  308. }
  309. }
  310. CATCHDAO
  311. return false;
  312. }
  313. int CClip::CompareFormatDataTo( long lID )
  314. {
  315. int nRet = 0;
  316. int nRecs, nFormats;
  317. CClipFormat* pFormat = NULL;
  318. try
  319. {
  320. CDataTable recset;
  321. recset.Open("SELECT * FROM Data WHERE lParentID = %d", lID);
  322. // Verify the same number of saved types
  323. recset.MoveLast();
  324. nRecs = recset.GetRecordCount();
  325. nFormats = m_Formats.GetCount();
  326. nRet = nFormats - nRecs;
  327. if( nRet != 0 )
  328. { recset.Close(); return nRet; }
  329. // For each format type in the db
  330. for( recset.MoveFirst(); !recset.IsEOF(); recset.MoveNext() )
  331. {
  332. pFormat = m_Formats.FindFormat( GetFormatID(recset.m_strClipBoardFormat) );
  333. // Verify the format exists
  334. if( !pFormat )
  335. { recset.Close(); return -1; }
  336. // Compare the size
  337. nRet = ::GlobalSize(pFormat->m_hgData) - recset.m_ooData.m_dwDataLength;
  338. if( nRet != 0 )
  339. { recset.Close(); return nRet; }
  340. // Binary compare
  341. nRet = CompareGlobalHH( recset.m_ooData.m_hData,
  342. pFormat->m_hgData,
  343. recset.m_ooData.m_dwDataLength );
  344. if( nRet != 0 )
  345. { recset.Close(); return nRet; }
  346. }
  347. recset.Close();
  348. }
  349. CATCHDAO
  350. return 0;
  351. }
  352. // assigns m_ID
  353. bool CClip::AddToMainTable()
  354. {
  355. long lDate;
  356. try
  357. {
  358. CMainTable recset;
  359. // recset.m_strSort = "lDate DESC";
  360. recset.Open("SELECT * FROM Main");
  361. lDate = (long) m_Time.GetTime();
  362. recset.AddNew();
  363. recset.m_lDate = lDate;
  364. recset.m_strType = GetFormatName( m_Formats[0].m_cfType );
  365. recset.m_strText = m_Desc;
  366. recset.m_lTotalCopySize = m_lTotalCopySize;
  367. recset.Update();
  368. recset.MoveLast();
  369. m_ID = recset.m_lID;
  370. recset.Close();
  371. }
  372. catch(CDaoException* e)
  373. {
  374. ASSERT(FALSE);
  375. e->Delete();
  376. return false;
  377. }
  378. return true;
  379. }
  380. // Empties m_Formats as it saves them to the Data Table.
  381. bool CClip::AddToDataTable()
  382. {
  383. ASSERT( m_ID != 0 );
  384. try
  385. {
  386. CDataTable recset;
  387. CClipFormat* pCF;
  388. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Data" ,NULL);
  389. for( int i = m_Formats.GetCount()-1; i >= 0 ; i-- )
  390. {
  391. pCF = & m_Formats.GetAt(i);
  392. recset.AddNew();
  393. recset.m_lParentID = m_ID;
  394. recset.m_strClipBoardFormat = GetFormatName( pCF->m_cfType );
  395. // the recset takes ownership of the HGLOBAL
  396. recset.ReplaceData( pCF->m_hgData, GlobalSize(pCF->m_hgData) );
  397. recset.Update();
  398. m_Formats.RemoveAt( i ); // the recset now owns the global
  399. }
  400. return true;
  401. }
  402. CATCHDAO
  403. return false;
  404. }
  405. // changes m_Time to be later than the latest clip entry in the db
  406. // ensures that pClip's time is not older than the last clip added
  407. // old times can happen on fast copies (<1 sec).
  408. void CClip::MakeLatestTime()
  409. {
  410. long lDate;
  411. try
  412. {
  413. CMainTable recset;
  414. recset.m_strSort = "lDate DESC";
  415. recset.Open("SELECT * FROM Main");
  416. recset.MoveFirst();
  417. lDate = (long) m_Time.GetTime();
  418. if( lDate <= recset.m_lDate )
  419. {
  420. lDate = recset.m_lDate + 1;
  421. m_Time = lDate;
  422. }
  423. recset.Close();
  424. }
  425. CATCHDAO
  426. }
  427. // STATICS
  428. // deletes from both Main and Data Tables
  429. BOOL CClip::Delete( int id )
  430. {
  431. CString csMainSQL;
  432. CString csDataSQL;
  433. BOOL bRet = FALSE;
  434. csMainSQL.Format( "DELETE FROM Main WHERE lID = %d", id );
  435. csDataSQL.Format( "DELETE FROM Data WHERE lParentID = %d", id );
  436. try
  437. {
  438. theApp.EnsureOpenDB();
  439. theApp.m_pDatabase->Execute(csMainSQL, dbFailOnError);
  440. theApp.m_pDatabase->Execute(csDataSQL, dbFailOnError);
  441. bRet = TRUE;
  442. }
  443. catch(CDaoException* e)
  444. {
  445. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  446. e->Delete();
  447. }
  448. return bRet;
  449. }
  450. BOOL CClip::Delete( ARRAY& IDs )
  451. {
  452. int count = IDs.GetSize();
  453. if(count <= 0)
  454. return FALSE;
  455. BOOL bRet = TRUE;
  456. // delete one at a time rather than all in one query
  457. // in order to avoid the "query too large" error for large deletes
  458. for( int i=0; i < count && bRet; i++ )
  459. bRet = bRet && Delete( IDs[i] );
  460. return bRet;
  461. }
  462. BOOL CClip::DeleteAll()
  463. {
  464. BOOL bRet = FALSE;
  465. try
  466. {
  467. theApp.EnsureOpenDB();
  468. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  469. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  470. bRet = TRUE;
  471. }
  472. catch(CDaoException* e)
  473. {
  474. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  475. e->Delete();
  476. }
  477. return bRet;
  478. }
  479. // Allocates a Global containing the requested Clip Format Data
  480. HGLOBAL CClip::LoadFormat( long lID, UINT cfType )
  481. {
  482. HGLOBAL hGlobal = 0;
  483. try
  484. {
  485. CDataTable recset;
  486. //Open the data table for all that have the parent id and format
  487. CString csSQL;
  488. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d AND strClipBoardFormat = \'%s\'", lID, GetFormatName(cfType));
  489. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  490. if( !recset.IsBOF() && !recset.IsEOF() )
  491. {
  492. // create a new HGLOBAL duplicate
  493. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  494. // XOR take the recset's HGLOBAL... is this SAFE???
  495. // hGlobal = recset.TakeData();
  496. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  497. {
  498. TRACE0( GetErrorString(::GetLastError()) );
  499. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  500. ASSERT(FALSE);
  501. }
  502. }
  503. recset.Close();
  504. }
  505. CATCHDAO
  506. return hGlobal;
  507. }
  508. bool CClip::LoadFormats( long lID, CClipFormats& formats )
  509. {
  510. CClipFormat cf;
  511. HGLOBAL hGlobal = 0;
  512. formats.RemoveAll();
  513. try
  514. {
  515. CDataTable recset;
  516. //Open the data table for all that have the parent id
  517. CString csSQL;
  518. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID);
  519. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  520. while( !recset.IsEOF() )
  521. {
  522. // create a new HGLOBAL duplicate
  523. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  524. // XOR take the recset's HGLOBAL... is this SAFE???
  525. // hGlobal = recset.TakeData();
  526. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  527. {
  528. TRACE0( GetErrorString(::GetLastError()) );
  529. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  530. ASSERT(FALSE);
  531. }
  532. cf.m_cfType = GetFormatID( recset.m_strClipBoardFormat );
  533. cf.m_hgData = hGlobal;
  534. formats.Add( cf );
  535. recset.MoveNext();
  536. }
  537. cf.m_hgData = 0; // formats owns all the data
  538. recset.Close();
  539. }
  540. CATCHDAO
  541. return formats.GetCount() > 0;
  542. }
  543. void CClip::LoadTypes( long lID, CClipTypes& types )
  544. {
  545. types.RemoveAll();
  546. try
  547. {
  548. CDataTable recset;
  549. //Open the data table for all that have the parent id
  550. CString csSQL;
  551. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID );
  552. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  553. while( !recset.IsEOF() )
  554. {
  555. types.Add( GetFormatID( recset.m_strClipBoardFormat ) );
  556. recset.MoveNext();
  557. }
  558. recset.Close();
  559. }
  560. CATCHDAO
  561. }
  562. /*----------------------------------------------------------------------------*\
  563. CClipList
  564. \*----------------------------------------------------------------------------*/
  565. CClipList::~CClipList()
  566. {
  567. CClip* pClip;
  568. while( GetCount() )
  569. {
  570. pClip = RemoveHead();
  571. DELETE_PTR( pClip );
  572. }
  573. }
  574. // returns the number of clips actually saved
  575. // while this does empty the Format Data, it does not delete the Clips.
  576. int CClipList::AddToDB( bool bLatestTime, bool bShowStatus )
  577. {
  578. int savedCount = 0;
  579. int nRemaining = 0;
  580. CClip* pClip;
  581. POSITION pos;
  582. bool bResult;
  583. nRemaining = GetCount();
  584. pos = GetHeadPosition();
  585. while( pos )
  586. {
  587. if( bShowStatus )
  588. {
  589. theApp.SetStatus( StrF("%d",nRemaining) );
  590. nRemaining--;
  591. }
  592. pClip = GetNext( pos );
  593. ASSERT( pClip );
  594. if( bLatestTime )
  595. pClip->MakeLatestTime();
  596. bResult = pClip->AddToDB();
  597. if( bResult )
  598. savedCount++;
  599. }
  600. if( bShowStatus )
  601. theApp.SetStatus();
  602. return savedCount;
  603. }
  604. /*----------------------------------------------------------------------------*\
  605. CClipboardViewer
  606. \*----------------------------------------------------------------------------*/
  607. IMPLEMENT_DYNAMIC(CClipboardViewer, CWnd)
  608. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  609. //{{AFX_MSG_MAP(CClipboardViewer)
  610. ON_WM_CREATE()
  611. ON_WM_CHANGECBCHAIN()
  612. ON_WM_DRAWCLIPBOARD()
  613. ON_WM_TIMER()
  614. //}}AFX_MSG_MAP
  615. ON_MESSAGE(WM_RECONNECT_TO_COPY_CHAIN, OnReconnectToCopyChain)
  616. ON_MESSAGE(WM_IS_TOP_VIEWER, OnGetIsTopViewer)
  617. ON_WM_DESTROY()
  618. END_MESSAGE_MAP()
  619. /////////////////////////////////////////////////////////////////////////////
  620. // CClipboardViewer construction/destruction
  621. CClipboardViewer::CClipboardViewer( CCopyThread* pHandler )
  622. {
  623. m_hNextClipboardViewer = 0;
  624. m_bCalling_SetClipboardViewer = false;
  625. m_lReconectCount = 0;
  626. m_bIsConnected = false;
  627. m_pHandler = pHandler;
  628. ASSERT(m_pHandler);
  629. }
  630. CClipboardViewer::~CClipboardViewer()
  631. {
  632. }
  633. void CClipboardViewer::Create()
  634. {
  635. CString strParentClass = AfxRegisterWndClass(0);
  636. CWnd::CreateEx(0, strParentClass, "Ditto Clipboard Viewer", 0, -1, -1, 0, 0, 0, 0);
  637. }
  638. // connects as a clipboard viewer
  639. void CClipboardViewer::Connect()
  640. {
  641. ASSERT( ::IsWindow(m_hWnd) );
  642. if( m_bIsConnected )
  643. return;
  644. //Set up the clip board viewer
  645. m_bCalling_SetClipboardViewer = true;
  646. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  647. m_bCalling_SetClipboardViewer = false;
  648. m_bIsConnected = true;
  649. }
  650. // disconnects as a clipboard viewer
  651. void CClipboardViewer::Disconnect()
  652. {
  653. if( !m_bIsConnected )
  654. return;
  655. ASSERT( ::IsWindow(m_hWnd) );
  656. CWnd::ChangeClipboardChain( m_hNextClipboardViewer );
  657. m_hNextClipboardViewer = 0;
  658. m_bIsConnected = false;
  659. }
  660. /////////////////////////////////////////////////////////////////////////////
  661. // CClipboardViewer message handlers
  662. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  663. {
  664. if (CWnd::OnCreate(lpCreateStruct) == -1)
  665. return -1;
  666. // is it important for us to be the top viewer?
  667. // SetTimer(TIMER_CHECK_TOP_LEVEL_VIEWER, ONE_MINUTE, 0);
  668. //Set up the clip board viewer
  669. Connect();
  670. return 0;
  671. }
  672. void CClipboardViewer::OnDestroy()
  673. {
  674. Disconnect();
  675. CWnd::OnDestroy();
  676. }
  677. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  678. {
  679. // If the next window is closing, repair the chain.
  680. if(m_hNextClipboardViewer == hWndRemove)
  681. {
  682. m_hNextClipboardViewer = hWndAfter;
  683. }
  684. // Otherwise, pass the message to the next link.
  685. else if (m_hNextClipboardViewer != NULL)
  686. {
  687. ::SendMessage ( m_hNextClipboardViewer, WM_CHANGECBCHAIN,
  688. (WPARAM) hWndRemove, (LPARAM) hWndAfter );
  689. }
  690. }
  691. //Message that the clipboard data has changed
  692. void CClipboardViewer::OnDrawClipboard()
  693. {
  694. // don't process the event when we first attach
  695. if( m_pHandler && !m_bCalling_SetClipboardViewer )
  696. m_pHandler->OnClipboardChange();
  697. // pass the event to the next Clipboard viewer in the chain
  698. if( m_hNextClipboardViewer != NULL )
  699. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  700. }
  701. void CClipboardViewer::OnTimer(UINT nIDEvent)
  702. {
  703. switch(nIDEvent)
  704. {
  705. case TIMER_CHECK_TOP_LEVEL_VIEWER:
  706. {
  707. if( GetClipboardViewer() != this )
  708. {
  709. OnReconnectToCopyChain(0, 0);
  710. m_lReconectCount++;
  711. if(m_lReconectCount > 10)
  712. KillTimer(TIMER_CHECK_TOP_LEVEL_VIEWER);
  713. }
  714. break;
  715. }
  716. }
  717. CWnd::OnTimer(nIDEvent);
  718. }
  719. LRESULT CClipboardViewer::OnReconnectToCopyChain(WPARAM wParam, LPARAM lParam)
  720. {
  721. if( GetClipboardViewer() != this )
  722. {
  723. Disconnect();
  724. Connect();
  725. return TRUE;
  726. }
  727. return FALSE;
  728. }
  729. LRESULT CClipboardViewer::OnGetIsTopViewer(WPARAM wParam, LPARAM lParam)
  730. {
  731. return (GetClipboardViewer() == this);
  732. }
  733. /*----------------------------------------------------------------------------*\
  734. CCopyConfig
  735. \*----------------------------------------------------------------------------*/
  736. /*----------------------------------------------------------------------------*\
  737. CCopyThread
  738. \*----------------------------------------------------------------------------*/
  739. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  740. CCopyThread::CCopyThread()
  741. {
  742. m_bQuit = false;
  743. m_bAutoDelete = false;
  744. m_bConfigChanged = false;
  745. m_pClips = new CClipList;
  746. m_pClipboardViewer = new CClipboardViewer(this);
  747. ::InitializeCriticalSection(&m_CS);
  748. }
  749. CCopyThread::~CCopyThread()
  750. {
  751. m_LocalConfig.DeleteTypes();
  752. m_SharedConfig.DeleteTypes();
  753. DELETE_PTR( m_pClipboardViewer );
  754. if( m_pClips )
  755. ASSERT( m_pClips->GetCount() == 0 );
  756. DELETE_PTR( m_pClips );
  757. ::DeleteCriticalSection(&m_CS);
  758. }
  759. // perform and per-thread initialization here
  760. BOOL CCopyThread::InitInstance()
  761. {
  762. ASSERT( ::GetCurrentThreadId() == m_nThreadID );
  763. SetThreadName(m_nThreadID, "COPY");
  764. // the window is created within this thread and therefore uses its message queue
  765. m_pClipboardViewer->Create();
  766. return TRUE;
  767. }
  768. // perform any per-thread cleanup here
  769. int CCopyThread::ExitInstance()
  770. {
  771. ASSERT( m_bQuit ); // make sure we intended to quit
  772. m_pClipboardViewer->Disconnect();
  773. return CWinThread::ExitInstance();
  774. }
  775. // Called within Copy Thread:
  776. void CCopyThread::OnClipboardChange()
  777. {
  778. SyncConfig(); // synchronize with the main thread's copy configuration
  779. // if we are told not to copy on change, then we have nothing to do.
  780. if( !m_LocalConfig.m_bCopyOnChange )
  781. return;
  782. CClip* pClip = new CClip;
  783. bool bResult = pClip->LoadFromClipboard( m_LocalConfig.m_pSupportedTypes );
  784. if( !bResult )
  785. {
  786. delete pClip;
  787. return; // error
  788. }
  789. AddToClips( pClip );
  790. if( m_LocalConfig.m_bAsyncCopy )
  791. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  792. else
  793. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  794. }
  795. void CCopyThread::SyncConfig()
  796. {
  797. // atomic read
  798. if( m_bConfigChanged )
  799. {
  800. CClipTypes* pTypes = NULL;
  801. Hold();
  802. pTypes = m_LocalConfig.m_pSupportedTypes;
  803. m_LocalConfig = m_SharedConfig;
  804. // NULL means that it shouldn't have been sync'ed
  805. if( m_SharedConfig.m_pSupportedTypes == NULL )
  806. { // let m_LocalConfig keep its types
  807. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  808. pTypes = NULL; // nothing to delete
  809. }
  810. else
  811. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  812. Release();
  813. // delete old types
  814. if( pTypes )
  815. delete pTypes;
  816. }
  817. }
  818. void CCopyThread::AddToClips( CClip* pClip )
  819. {
  820. Hold();
  821. if( !m_pClips )
  822. m_pClips = new CClipList;
  823. m_pClips->AddTail( pClip ); // m_pClips now owns pClip
  824. Release();
  825. }
  826. // Shared (use thread-safe access functions below)
  827. // Called within Main thread:
  828. CClipList* CCopyThread::GetClips()
  829. {
  830. CClipList* pRet;
  831. CClipList* pClips = new CClipList;
  832. Hold();
  833. pRet = m_pClips;
  834. m_pClips = pClips;
  835. Release();
  836. return pRet;
  837. }
  838. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  839. {
  840. CClipTypes* pTemp;
  841. Hold();
  842. pTemp = m_SharedConfig.m_pSupportedTypes;
  843. m_SharedConfig.m_pSupportedTypes = pTypes;
  844. m_bConfigChanged = true;
  845. Release();
  846. if( pTemp )
  847. delete pTemp;
  848. }
  849. HWND CCopyThread::SetClipHandler( HWND hWnd )
  850. {
  851. HWND hRet;
  852. Hold();
  853. hRet = m_SharedConfig.m_hClipHandler;
  854. m_SharedConfig.m_hClipHandler = hWnd;
  855. m_bConfigChanged = (hRet != hWnd);
  856. Release();
  857. return hRet;
  858. }
  859. HWND CCopyThread::GetClipHandler()
  860. {
  861. HWND hRet;
  862. Hold();
  863. hRet = m_SharedConfig.m_hClipHandler;
  864. Release();
  865. return hRet;
  866. }
  867. bool CCopyThread::SetCopyOnChange( bool bVal )
  868. {
  869. bool bRet;
  870. Hold();
  871. bRet = m_SharedConfig.m_bCopyOnChange;
  872. m_SharedConfig.m_bCopyOnChange = bVal;
  873. m_bConfigChanged = (bRet != bVal);
  874. Release();
  875. return bRet;
  876. }
  877. bool CCopyThread::GetCopyOnChange()
  878. {
  879. bool bRet;
  880. Hold();
  881. bRet = m_SharedConfig.m_bCopyOnChange;
  882. Release();
  883. return bRet;
  884. }
  885. bool CCopyThread::SetAsyncCopy( bool bVal )
  886. {
  887. bool bRet;
  888. Hold();
  889. bRet = m_SharedConfig.m_bAsyncCopy;
  890. m_SharedConfig.m_bAsyncCopy = bVal;
  891. m_bConfigChanged = (bRet != bVal);
  892. Release();
  893. return bRet;
  894. }
  895. bool CCopyThread::GetAsyncCopy()
  896. {
  897. bool bRet;
  898. Hold();
  899. bRet = m_SharedConfig.m_bAsyncCopy;
  900. Release();
  901. return bRet;
  902. }
  903. void CCopyThread::Init( CCopyConfig cfg )
  904. {
  905. ASSERT( m_LocalConfig.m_pSupportedTypes == NULL );
  906. m_LocalConfig = m_SharedConfig = cfg;
  907. // let m_LocalConfig own the m_pSupportedTypes
  908. m_SharedConfig.m_pSupportedTypes = NULL;
  909. }
  910. bool CCopyThread::Quit()
  911. {
  912. m_bQuit = true;
  913. m_pClipboardViewer->PostMessage( WM_QUIT );
  914. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  915. }
  916. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  917. END_MESSAGE_MAP()
  918. // CCopyThread message handlers
  919. /*----------------------------------------------------------------------------*\
  920. CProcessCopy
  921. \*----------------------------------------------------------------------------*/
  922. CProcessCopy::CProcessCopy()
  923. {
  924. }
  925. CProcessCopy::~CProcessCopy()
  926. {
  927. }