ProcessCopy.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. if(!recset.IsEOF())
  417. {
  418. recset.MoveFirst();
  419. lDate = (long) m_Time.GetTime();
  420. if( lDate <= recset.m_lDate )
  421. {
  422. lDate = recset.m_lDate + 1;
  423. m_Time = lDate;
  424. }
  425. }
  426. recset.Close();
  427. }
  428. CATCHDAO
  429. }
  430. // STATICS
  431. // deletes from both Main and Data Tables
  432. BOOL CClip::Delete( int id )
  433. {
  434. CString csMainSQL;
  435. CString csDataSQL;
  436. BOOL bRet = FALSE;
  437. csMainSQL.Format( "DELETE FROM Main WHERE lID = %d", id );
  438. csDataSQL.Format( "DELETE FROM Data WHERE lParentID = %d", id );
  439. try
  440. {
  441. theApp.EnsureOpenDB();
  442. theApp.m_pDatabase->Execute(csMainSQL, dbFailOnError);
  443. theApp.m_pDatabase->Execute(csDataSQL, dbFailOnError);
  444. bRet = TRUE;
  445. }
  446. catch(CDaoException* e)
  447. {
  448. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  449. e->Delete();
  450. }
  451. return bRet;
  452. }
  453. BOOL CClip::Delete( ARRAY& IDs )
  454. {
  455. int count = IDs.GetSize();
  456. if(count <= 0)
  457. return FALSE;
  458. BOOL bRet = TRUE;
  459. // delete one at a time rather than all in one query
  460. // in order to avoid the "query too large" error for large deletes
  461. for( int i=0; i < count && bRet; i++ )
  462. bRet = bRet && Delete( IDs[i] );
  463. return bRet;
  464. }
  465. BOOL CClip::DeleteAll()
  466. {
  467. BOOL bRet = FALSE;
  468. try
  469. {
  470. theApp.EnsureOpenDB();
  471. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  472. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  473. bRet = TRUE;
  474. }
  475. catch(CDaoException* e)
  476. {
  477. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  478. e->Delete();
  479. }
  480. return bRet;
  481. }
  482. // Allocates a Global containing the requested Clip Format Data
  483. HGLOBAL CClip::LoadFormat( long lID, UINT cfType )
  484. {
  485. HGLOBAL hGlobal = 0;
  486. try
  487. {
  488. CDataTable recset;
  489. //Open the data table for all that have the parent id and format
  490. CString csSQL;
  491. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d AND strClipBoardFormat = \'%s\'", lID, GetFormatName(cfType));
  492. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  493. if( !recset.IsBOF() && !recset.IsEOF() )
  494. {
  495. // create a new HGLOBAL duplicate
  496. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  497. // XOR take the recset's HGLOBAL... is this SAFE???
  498. // hGlobal = recset.TakeData();
  499. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  500. {
  501. TRACE0( GetErrorString(::GetLastError()) );
  502. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  503. ASSERT(FALSE);
  504. }
  505. }
  506. recset.Close();
  507. }
  508. CATCHDAO
  509. return hGlobal;
  510. }
  511. bool CClip::LoadFormats( long lID, CClipFormats& formats )
  512. {
  513. CClipFormat cf;
  514. HGLOBAL hGlobal = 0;
  515. formats.RemoveAll();
  516. try
  517. {
  518. CDataTable recset;
  519. //Open the data table for all that have the parent id
  520. CString csSQL;
  521. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID);
  522. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  523. while( !recset.IsEOF() )
  524. {
  525. // create a new HGLOBAL duplicate
  526. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  527. // XOR take the recset's HGLOBAL... is this SAFE???
  528. // hGlobal = recset.TakeData();
  529. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  530. {
  531. TRACE0( GetErrorString(::GetLastError()) );
  532. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  533. ASSERT(FALSE);
  534. }
  535. cf.m_cfType = GetFormatID( recset.m_strClipBoardFormat );
  536. cf.m_hgData = hGlobal;
  537. formats.Add( cf );
  538. recset.MoveNext();
  539. }
  540. cf.m_hgData = 0; // formats owns all the data
  541. recset.Close();
  542. }
  543. CATCHDAO
  544. return formats.GetCount() > 0;
  545. }
  546. void CClip::LoadTypes( long lID, CClipTypes& types )
  547. {
  548. types.RemoveAll();
  549. try
  550. {
  551. CDataTable recset;
  552. //Open the data table for all that have the parent id
  553. CString csSQL;
  554. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID );
  555. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  556. while( !recset.IsEOF() )
  557. {
  558. types.Add( GetFormatID( recset.m_strClipBoardFormat ) );
  559. recset.MoveNext();
  560. }
  561. recset.Close();
  562. }
  563. CATCHDAO
  564. }
  565. /*----------------------------------------------------------------------------*\
  566. CClipList
  567. \*----------------------------------------------------------------------------*/
  568. CClipList::~CClipList()
  569. {
  570. CClip* pClip;
  571. while( GetCount() )
  572. {
  573. pClip = RemoveHead();
  574. DELETE_PTR( pClip );
  575. }
  576. }
  577. // returns the number of clips actually saved
  578. // while this does empty the Format Data, it does not delete the Clips.
  579. int CClipList::AddToDB( bool bLatestTime, bool bShowStatus )
  580. {
  581. int savedCount = 0;
  582. int nRemaining = 0;
  583. CClip* pClip;
  584. POSITION pos;
  585. bool bResult;
  586. nRemaining = GetCount();
  587. pos = GetHeadPosition();
  588. while( pos )
  589. {
  590. if( bShowStatus )
  591. {
  592. theApp.SetStatus( StrF("%d",nRemaining) );
  593. nRemaining--;
  594. }
  595. pClip = GetNext( pos );
  596. ASSERT( pClip );
  597. if( bLatestTime )
  598. pClip->MakeLatestTime();
  599. bResult = pClip->AddToDB();
  600. if( bResult )
  601. savedCount++;
  602. }
  603. if( bShowStatus )
  604. theApp.SetStatus();
  605. return savedCount;
  606. }
  607. /*----------------------------------------------------------------------------*\
  608. CClipboardViewer
  609. \*----------------------------------------------------------------------------*/
  610. IMPLEMENT_DYNAMIC(CClipboardViewer, CWnd)
  611. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  612. //{{AFX_MSG_MAP(CClipboardViewer)
  613. ON_WM_CREATE()
  614. ON_WM_CHANGECBCHAIN()
  615. ON_WM_DRAWCLIPBOARD()
  616. ON_WM_TIMER()
  617. //}}AFX_MSG_MAP
  618. ON_MESSAGE(WM_RECONNECT_TO_COPY_CHAIN, OnReconnectToCopyChain)
  619. ON_MESSAGE(WM_IS_TOP_VIEWER, OnGetIsTopViewer)
  620. ON_WM_DESTROY()
  621. END_MESSAGE_MAP()
  622. /////////////////////////////////////////////////////////////////////////////
  623. // CClipboardViewer construction/destruction
  624. CClipboardViewer::CClipboardViewer( CCopyThread* pHandler )
  625. {
  626. m_hNextClipboardViewer = 0;
  627. m_bCalling_SetClipboardViewer = false;
  628. m_lReconectCount = 0;
  629. m_bIsConnected = false;
  630. m_pHandler = pHandler;
  631. ASSERT(m_pHandler);
  632. }
  633. CClipboardViewer::~CClipboardViewer()
  634. {
  635. }
  636. void CClipboardViewer::Create()
  637. {
  638. CString strParentClass = AfxRegisterWndClass(0);
  639. CWnd::CreateEx(0, strParentClass, "Ditto Clipboard Viewer", 0, -1, -1, 0, 0, 0, 0);
  640. }
  641. // connects as a clipboard viewer
  642. void CClipboardViewer::Connect()
  643. {
  644. ASSERT( ::IsWindow(m_hWnd) );
  645. if( m_bIsConnected )
  646. return;
  647. //Set up the clip board viewer
  648. m_bCalling_SetClipboardViewer = true;
  649. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  650. m_bCalling_SetClipboardViewer = false;
  651. m_bIsConnected = true;
  652. }
  653. // disconnects as a clipboard viewer
  654. void CClipboardViewer::Disconnect()
  655. {
  656. if( !m_bIsConnected )
  657. return;
  658. ASSERT( ::IsWindow(m_hWnd) );
  659. CWnd::ChangeClipboardChain( m_hNextClipboardViewer );
  660. m_hNextClipboardViewer = 0;
  661. m_bIsConnected = false;
  662. }
  663. /////////////////////////////////////////////////////////////////////////////
  664. // CClipboardViewer message handlers
  665. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  666. {
  667. if (CWnd::OnCreate(lpCreateStruct) == -1)
  668. return -1;
  669. // is it important for us to be the top viewer?
  670. // SetTimer(TIMER_CHECK_TOP_LEVEL_VIEWER, ONE_MINUTE, 0);
  671. //Set up the clip board viewer
  672. Connect();
  673. return 0;
  674. }
  675. void CClipboardViewer::OnDestroy()
  676. {
  677. Disconnect();
  678. CWnd::OnDestroy();
  679. }
  680. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  681. {
  682. // If the next window is closing, repair the chain.
  683. if(m_hNextClipboardViewer == hWndRemove)
  684. {
  685. m_hNextClipboardViewer = hWndAfter;
  686. }
  687. // Otherwise, pass the message to the next link.
  688. else if (m_hNextClipboardViewer != NULL)
  689. {
  690. ::SendMessage ( m_hNextClipboardViewer, WM_CHANGECBCHAIN,
  691. (WPARAM) hWndRemove, (LPARAM) hWndAfter );
  692. }
  693. }
  694. //Message that the clipboard data has changed
  695. void CClipboardViewer::OnDrawClipboard()
  696. {
  697. // don't process the event when we first attach
  698. if( m_pHandler && !m_bCalling_SetClipboardViewer )
  699. m_pHandler->OnClipboardChange();
  700. // pass the event to the next Clipboard viewer in the chain
  701. if( m_hNextClipboardViewer != NULL )
  702. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  703. }
  704. void CClipboardViewer::OnTimer(UINT nIDEvent)
  705. {
  706. switch(nIDEvent)
  707. {
  708. case TIMER_CHECK_TOP_LEVEL_VIEWER:
  709. {
  710. if( GetClipboardViewer() != this )
  711. {
  712. OnReconnectToCopyChain(0, 0);
  713. m_lReconectCount++;
  714. if(m_lReconectCount > 10)
  715. KillTimer(TIMER_CHECK_TOP_LEVEL_VIEWER);
  716. }
  717. break;
  718. }
  719. }
  720. CWnd::OnTimer(nIDEvent);
  721. }
  722. LRESULT CClipboardViewer::OnReconnectToCopyChain(WPARAM wParam, LPARAM lParam)
  723. {
  724. if( GetClipboardViewer() != this )
  725. {
  726. Disconnect();
  727. Connect();
  728. return TRUE;
  729. }
  730. return FALSE;
  731. }
  732. LRESULT CClipboardViewer::OnGetIsTopViewer(WPARAM wParam, LPARAM lParam)
  733. {
  734. return (GetClipboardViewer() == this);
  735. }
  736. /*----------------------------------------------------------------------------*\
  737. CCopyConfig
  738. \*----------------------------------------------------------------------------*/
  739. /*----------------------------------------------------------------------------*\
  740. CCopyThread
  741. \*----------------------------------------------------------------------------*/
  742. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  743. CCopyThread::CCopyThread()
  744. {
  745. m_bQuit = false;
  746. m_bAutoDelete = false;
  747. m_bConfigChanged = false;
  748. m_pClips = new CClipList;
  749. m_pClipboardViewer = new CClipboardViewer(this);
  750. ::InitializeCriticalSection(&m_CS);
  751. }
  752. CCopyThread::~CCopyThread()
  753. {
  754. m_LocalConfig.DeleteTypes();
  755. m_SharedConfig.DeleteTypes();
  756. DELETE_PTR( m_pClipboardViewer );
  757. if( m_pClips )
  758. ASSERT( m_pClips->GetCount() == 0 );
  759. DELETE_PTR( m_pClips );
  760. ::DeleteCriticalSection(&m_CS);
  761. }
  762. // perform and per-thread initialization here
  763. BOOL CCopyThread::InitInstance()
  764. {
  765. ASSERT( ::GetCurrentThreadId() == m_nThreadID );
  766. SetThreadName(m_nThreadID, "COPY");
  767. // the window is created within this thread and therefore uses its message queue
  768. m_pClipboardViewer->Create();
  769. return TRUE;
  770. }
  771. // perform any per-thread cleanup here
  772. int CCopyThread::ExitInstance()
  773. {
  774. ASSERT( m_bQuit ); // make sure we intended to quit
  775. m_pClipboardViewer->Disconnect();
  776. return CWinThread::ExitInstance();
  777. }
  778. // Called within Copy Thread:
  779. void CCopyThread::OnClipboardChange()
  780. {
  781. SyncConfig(); // synchronize with the main thread's copy configuration
  782. // if we are told not to copy on change, then we have nothing to do.
  783. if( !m_LocalConfig.m_bCopyOnChange )
  784. return;
  785. CClip* pClip = new CClip;
  786. bool bResult = pClip->LoadFromClipboard( m_LocalConfig.m_pSupportedTypes );
  787. if( !bResult )
  788. {
  789. delete pClip;
  790. return; // error
  791. }
  792. AddToClips( pClip );
  793. if( m_LocalConfig.m_bAsyncCopy )
  794. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  795. else
  796. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  797. }
  798. void CCopyThread::SyncConfig()
  799. {
  800. // atomic read
  801. if( m_bConfigChanged )
  802. {
  803. CClipTypes* pTypes = NULL;
  804. Hold();
  805. pTypes = m_LocalConfig.m_pSupportedTypes;
  806. m_LocalConfig = m_SharedConfig;
  807. // NULL means that it shouldn't have been sync'ed
  808. if( m_SharedConfig.m_pSupportedTypes == NULL )
  809. { // let m_LocalConfig keep its types
  810. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  811. pTypes = NULL; // nothing to delete
  812. }
  813. else
  814. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  815. Release();
  816. // delete old types
  817. if( pTypes )
  818. delete pTypes;
  819. }
  820. }
  821. void CCopyThread::AddToClips( CClip* pClip )
  822. {
  823. Hold();
  824. if( !m_pClips )
  825. m_pClips = new CClipList;
  826. m_pClips->AddTail( pClip ); // m_pClips now owns pClip
  827. Release();
  828. }
  829. // Shared (use thread-safe access functions below)
  830. // Called within Main thread:
  831. CClipList* CCopyThread::GetClips()
  832. {
  833. CClipList* pRet;
  834. CClipList* pClips = new CClipList;
  835. Hold();
  836. pRet = m_pClips;
  837. m_pClips = pClips;
  838. Release();
  839. return pRet;
  840. }
  841. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  842. {
  843. CClipTypes* pTemp;
  844. Hold();
  845. pTemp = m_SharedConfig.m_pSupportedTypes;
  846. m_SharedConfig.m_pSupportedTypes = pTypes;
  847. m_bConfigChanged = true;
  848. Release();
  849. if( pTemp )
  850. delete pTemp;
  851. }
  852. HWND CCopyThread::SetClipHandler( HWND hWnd )
  853. {
  854. HWND hRet;
  855. Hold();
  856. hRet = m_SharedConfig.m_hClipHandler;
  857. m_SharedConfig.m_hClipHandler = hWnd;
  858. m_bConfigChanged = (hRet != hWnd);
  859. Release();
  860. return hRet;
  861. }
  862. HWND CCopyThread::GetClipHandler()
  863. {
  864. HWND hRet;
  865. Hold();
  866. hRet = m_SharedConfig.m_hClipHandler;
  867. Release();
  868. return hRet;
  869. }
  870. bool CCopyThread::SetCopyOnChange( bool bVal )
  871. {
  872. bool bRet;
  873. Hold();
  874. bRet = m_SharedConfig.m_bCopyOnChange;
  875. m_SharedConfig.m_bCopyOnChange = bVal;
  876. m_bConfigChanged = (bRet != bVal);
  877. Release();
  878. return bRet;
  879. }
  880. bool CCopyThread::GetCopyOnChange()
  881. {
  882. bool bRet;
  883. Hold();
  884. bRet = m_SharedConfig.m_bCopyOnChange;
  885. Release();
  886. return bRet;
  887. }
  888. bool CCopyThread::SetAsyncCopy( bool bVal )
  889. {
  890. bool bRet;
  891. Hold();
  892. bRet = m_SharedConfig.m_bAsyncCopy;
  893. m_SharedConfig.m_bAsyncCopy = bVal;
  894. m_bConfigChanged = (bRet != bVal);
  895. Release();
  896. return bRet;
  897. }
  898. bool CCopyThread::GetAsyncCopy()
  899. {
  900. bool bRet;
  901. Hold();
  902. bRet = m_SharedConfig.m_bAsyncCopy;
  903. Release();
  904. return bRet;
  905. }
  906. void CCopyThread::Init( CCopyConfig cfg )
  907. {
  908. ASSERT( m_LocalConfig.m_pSupportedTypes == NULL );
  909. m_LocalConfig = m_SharedConfig = cfg;
  910. // let m_LocalConfig own the m_pSupportedTypes
  911. m_SharedConfig.m_pSupportedTypes = NULL;
  912. }
  913. bool CCopyThread::Quit()
  914. {
  915. m_bQuit = true;
  916. m_pClipboardViewer->PostMessage( WM_QUIT );
  917. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  918. }
  919. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  920. END_MESSAGE_MAP()
  921. // CCopyThread message handlers
  922. /*----------------------------------------------------------------------------*\
  923. CProcessCopy
  924. \*----------------------------------------------------------------------------*/
  925. CProcessCopy::CProcessCopy()
  926. {
  927. }
  928. CProcessCopy::~CProcessCopy()
  929. {
  930. }