ProcessCopy.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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 = GetSize();
  68. for( int i=0; i < count; i++ )
  69. {
  70. pCF = &ElementAt(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.GetSize();
  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.GetSize()-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.GetSize() == 0 );
  148. // If the data is supposed to be private, then return
  149. if( ::IsClipboardFormatAvailable( theApp.m_cfIgnoreClipboard ) )
  150. EXIT_LoadFromClipboard( false );
  151. //Attach to the clipboard
  152. if( !oleData.AttachClipboard() )
  153. {
  154. ASSERT(0); // does this ever happen?
  155. EXIT_LoadFromClipboard(false);
  156. }
  157. oleData.EnsureClipboardObject();
  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->GetSize() == 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->GetSize();
  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 > g_Opt.m_bDescTextSize )
  237. ulBufLen = g_Opt.m_bDescTextSize;
  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.GetSize() <= 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.GetSize() == 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=0, nFormats=0;
  317. CClipFormat* pFormat = NULL;
  318. try
  319. {
  320. CDataTable recset;
  321. recset.Open("SELECT * FROM Data WHERE lParentID = %d", lID);
  322. if( !recset.IsBOF() && !recset.IsEOF() )
  323. {
  324. // Verify the same number of saved types
  325. recset.MoveLast();
  326. nRecs = recset.GetRecordCount();
  327. }
  328. nFormats = m_Formats.GetSize();
  329. nRet = nFormats - nRecs;
  330. if( nRet != 0 || nRecs == 0 )
  331. { recset.Close(); return nRet; }
  332. // For each format type in the db
  333. for( recset.MoveFirst(); !recset.IsEOF(); recset.MoveNext() )
  334. {
  335. pFormat = m_Formats.FindFormat( GetFormatID(recset.m_strClipBoardFormat) );
  336. // Verify the format exists
  337. if( !pFormat )
  338. { recset.Close(); return -1; }
  339. // Compare the size
  340. nRet = ::GlobalSize(pFormat->m_hgData) - recset.m_ooData.m_dwDataLength;
  341. if( nRet != 0 )
  342. { recset.Close(); return nRet; }
  343. // Binary compare
  344. nRet = CompareGlobalHH( recset.m_ooData.m_hData,
  345. pFormat->m_hgData,
  346. recset.m_ooData.m_dwDataLength );
  347. if( nRet != 0 )
  348. { recset.Close(); return nRet; }
  349. }
  350. recset.Close();
  351. }
  352. CATCHDAO
  353. return 0;
  354. }
  355. // assigns m_ID
  356. bool CClip::AddToMainTable()
  357. {
  358. long lDate;
  359. try
  360. {
  361. CMainTable recset;
  362. // recset.m_strSort = "lDate DESC";
  363. recset.Open("SELECT * FROM Main");
  364. lDate = (long) m_Time.GetTime();
  365. recset.AddNew();
  366. recset.m_lDate = lDate;
  367. recset.m_strText = m_Desc;
  368. recset.m_lTotalCopySize = m_lTotalCopySize;
  369. recset.Update();
  370. recset.MoveLast();
  371. m_ID = recset.m_lID;
  372. recset.Close();
  373. }
  374. catch(CDaoException* e)
  375. {
  376. ASSERT(FALSE);
  377. e->Delete();
  378. return false;
  379. }
  380. return true;
  381. }
  382. // Empties m_Formats as it saves them to the Data Table.
  383. bool CClip::AddToDataTable()
  384. {
  385. ASSERT( m_ID != 0 );
  386. try
  387. {
  388. CDataTable recset;
  389. CClipFormat* pCF;
  390. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Data" ,NULL);
  391. for( int i = m_Formats.GetSize()-1; i >= 0 ; i-- )
  392. {
  393. pCF = & m_Formats.ElementAt(i);
  394. recset.AddNew();
  395. recset.m_lParentID = m_ID;
  396. recset.m_strClipBoardFormat = GetFormatName( pCF->m_cfType );
  397. // the recset takes ownership of the HGLOBAL
  398. recset.ReplaceData( pCF->m_hgData, GlobalSize(pCF->m_hgData) );
  399. recset.Update();
  400. m_Formats.RemoveAt( i ); // the recset now owns the global
  401. }
  402. return true;
  403. }
  404. CATCHDAO
  405. return false;
  406. }
  407. // changes m_Time to be later than the latest clip entry in the db
  408. // ensures that pClip's time is not older than the last clip added
  409. // old times can happen on fast copies (<1 sec).
  410. void CClip::MakeLatestTime()
  411. {
  412. long lDate;
  413. try
  414. {
  415. CMainTable recset;
  416. recset.m_strSort = "lDate DESC";
  417. recset.Open("SELECT * FROM Main");
  418. if(!recset.IsEOF())
  419. {
  420. recset.MoveFirst();
  421. lDate = (long) m_Time.GetTime();
  422. if( lDate <= recset.m_lDate )
  423. {
  424. lDate = recset.m_lDate + 1;
  425. m_Time = lDate;
  426. }
  427. }
  428. recset.Close();
  429. }
  430. CATCHDAO
  431. }
  432. // STATICS
  433. // deletes from both Main and Data Tables
  434. BOOL CClip::Delete( int id )
  435. {
  436. CString csMainSQL;
  437. CString csDataSQL;
  438. BOOL bRet = FALSE;
  439. csMainSQL.Format( "DELETE FROM Main WHERE lID = %d", id );
  440. csDataSQL.Format( "DELETE FROM Data WHERE lParentID = %d", id );
  441. try
  442. {
  443. theApp.EnsureOpenDB();
  444. theApp.m_pDatabase->Execute(csMainSQL, dbFailOnError);
  445. theApp.m_pDatabase->Execute(csDataSQL, dbFailOnError);
  446. bRet = TRUE;
  447. }
  448. catch(CDaoException* e)
  449. {
  450. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  451. e->Delete();
  452. }
  453. return bRet;
  454. }
  455. BOOL CClip::Delete( ARRAY& IDs )
  456. {
  457. int count = IDs.GetSize();
  458. if(count <= 0)
  459. return FALSE;
  460. BOOL bRet = TRUE;
  461. // delete one at a time rather than all in one query
  462. // in order to avoid the "query too large" error for large deletes
  463. for( int i=0; i < count && bRet; i++ )
  464. bRet = bRet && Delete( IDs[i] );
  465. return bRet;
  466. }
  467. BOOL CClip::DeleteAll()
  468. {
  469. BOOL bRet = FALSE;
  470. try
  471. {
  472. theApp.EnsureOpenDB();
  473. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  474. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  475. bRet = TRUE;
  476. }
  477. catch(CDaoException* e)
  478. {
  479. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  480. e->Delete();
  481. }
  482. return bRet;
  483. }
  484. // Allocates a Global containing the requested Clip Format Data
  485. HGLOBAL CClip::LoadFormat( long lID, UINT cfType )
  486. {
  487. HGLOBAL hGlobal = 0;
  488. try
  489. {
  490. CDataTable recset;
  491. //Open the data table for all that have the parent id and format
  492. CString csSQL;
  493. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d AND strClipBoardFormat = \'%s\'", lID, GetFormatName(cfType));
  494. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  495. if( !recset.IsBOF() && !recset.IsEOF() )
  496. {
  497. // create a new HGLOBAL duplicate
  498. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  499. // XOR take the recset's HGLOBAL... is this SAFE???
  500. // hGlobal = recset.TakeData();
  501. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  502. {
  503. TRACE0( GetErrorString(::GetLastError()) );
  504. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  505. ASSERT(FALSE);
  506. }
  507. }
  508. recset.Close();
  509. }
  510. CATCHDAO
  511. return hGlobal;
  512. }
  513. bool CClip::LoadFormats( long lID, CClipFormats& formats )
  514. {
  515. CClipFormat cf;
  516. HGLOBAL hGlobal = 0;
  517. formats.RemoveAll();
  518. try
  519. {
  520. CDataTable recset;
  521. //Open the data table for all that have the parent id
  522. CString csSQL;
  523. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID);
  524. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  525. while( !recset.IsEOF() )
  526. {
  527. // create a new HGLOBAL duplicate
  528. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  529. // XOR take the recset's HGLOBAL... is this SAFE???
  530. // hGlobal = recset.TakeData();
  531. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  532. {
  533. TRACE0( GetErrorString(::GetLastError()) );
  534. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  535. ASSERT(FALSE);
  536. }
  537. cf.m_cfType = GetFormatID( recset.m_strClipBoardFormat );
  538. cf.m_hgData = hGlobal;
  539. formats.Add( cf );
  540. recset.MoveNext();
  541. }
  542. cf.m_hgData = 0; // formats owns all the data
  543. recset.Close();
  544. }
  545. CATCHDAO
  546. return formats.GetSize() > 0;
  547. }
  548. void CClip::LoadTypes( long lID, CClipTypes& types )
  549. {
  550. types.RemoveAll();
  551. try
  552. {
  553. CDataTable recset;
  554. //Open the data table for all that have the parent id
  555. CString csSQL;
  556. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID );
  557. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  558. while( !recset.IsEOF() )
  559. {
  560. types.Add( GetFormatID( recset.m_strClipBoardFormat ) );
  561. recset.MoveNext();
  562. }
  563. recset.Close();
  564. }
  565. CATCHDAO
  566. }
  567. /*----------------------------------------------------------------------------*\
  568. CClipList
  569. \*----------------------------------------------------------------------------*/
  570. CClipList::~CClipList()
  571. {
  572. CClip* pClip;
  573. while( GetCount() )
  574. {
  575. pClip = RemoveHead();
  576. DELETE_PTR( pClip );
  577. }
  578. }
  579. // returns the number of clips actually saved
  580. // while this does empty the Format Data, it does not delete the Clips.
  581. int CClipList::AddToDB( bool bLatestTime, bool bShowStatus )
  582. {
  583. int savedCount = 0;
  584. int nRemaining = 0;
  585. CClip* pClip;
  586. POSITION pos;
  587. bool bResult;
  588. nRemaining = GetCount();
  589. pos = GetHeadPosition();
  590. while( pos )
  591. {
  592. if( bShowStatus )
  593. {
  594. theApp.SetStatus( StrF("%d",nRemaining), true );
  595. nRemaining--;
  596. }
  597. pClip = GetNext( pos );
  598. ASSERT( pClip );
  599. if( bLatestTime )
  600. pClip->MakeLatestTime();
  601. bResult = pClip->AddToDB();
  602. if( bResult )
  603. savedCount++;
  604. }
  605. if( bShowStatus )
  606. theApp.SetStatus(NULL, true);
  607. return savedCount;
  608. }
  609. /*----------------------------------------------------------------------------*\
  610. CClipboardViewer
  611. \*----------------------------------------------------------------------------*/
  612. IMPLEMENT_DYNAMIC(CClipboardViewer, CWnd)
  613. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  614. //{{AFX_MSG_MAP(CClipboardViewer)
  615. ON_WM_CREATE()
  616. ON_WM_CHANGECBCHAIN()
  617. ON_WM_DRAWCLIPBOARD()
  618. ON_WM_TIMER()
  619. //}}AFX_MSG_MAP
  620. ON_MESSAGE(WM_CV_RECONNECT, OnCVReconnect)
  621. ON_MESSAGE(WM_CV_IS_CONNECTED, OnCVIsConnected)
  622. ON_WM_DESTROY()
  623. END_MESSAGE_MAP()
  624. /////////////////////////////////////////////////////////////////////////////
  625. // CClipboardViewer construction/destruction
  626. CClipboardViewer::CClipboardViewer( CCopyThread* pHandler )
  627. {
  628. m_hNextClipboardViewer = 0;
  629. m_bCalling_SetClipboardViewer = false;
  630. m_lReconectCount = 0;
  631. m_bIsConnected = false;
  632. m_pHandler = pHandler;
  633. ASSERT(m_pHandler);
  634. m_bPinging = false;
  635. m_bPingSuccess = false;
  636. }
  637. CClipboardViewer::~CClipboardViewer()
  638. {
  639. }
  640. void CClipboardViewer::Create()
  641. {
  642. CString strParentClass = AfxRegisterWndClass(0);
  643. CWnd::CreateEx(0, strParentClass, "Ditto Clipboard Viewer", 0, -1, -1, 0, 0, 0, 0);
  644. }
  645. // connects as a clipboard viewer
  646. void CClipboardViewer::Connect()
  647. {
  648. ASSERT( ::IsWindow(m_hWnd) );
  649. if( m_bIsConnected )
  650. return;
  651. //Set up the clip board viewer
  652. m_bCalling_SetClipboardViewer = true;
  653. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  654. m_bCalling_SetClipboardViewer = false;
  655. m_bIsConnected = SendPing();
  656. }
  657. // disconnects as a clipboard viewer
  658. void CClipboardViewer::Disconnect()
  659. {
  660. if( !m_bIsConnected )
  661. return;
  662. ASSERT( ::IsWindow(m_hWnd) );
  663. CWnd::ChangeClipboardChain( m_hNextClipboardViewer );
  664. m_hNextClipboardViewer = 0;
  665. m_bIsConnected = false;
  666. }
  667. bool CClipboardViewer::SendPing()
  668. {
  669. HWND hWnd;
  670. bool bResult = false;
  671. hWnd = ::GetClipboardViewer();
  672. // if there is a chain
  673. if( ::IsWindow(hWnd) )
  674. {
  675. m_bPingSuccess = false;
  676. m_bPinging = true;
  677. ::SendMessage( hWnd, WM_DRAWCLIPBOARD, 0, 0 );
  678. m_bPinging = false;
  679. bResult = m_bPingSuccess;
  680. }
  681. m_bIsConnected = bResult;
  682. return bResult;
  683. }
  684. bool CClipboardViewer::EnsureConnected()
  685. {
  686. if( !SendPing() )
  687. Connect();
  688. return m_bIsConnected;
  689. }
  690. // puts format "Clipboard Viewer Ignore" on the clipboard
  691. void CClipboardViewer::SetCVIgnore()
  692. {
  693. if( ::OpenClipboard( m_hWnd ) )
  694. {
  695. ::SetClipboardData( theApp.m_cfIgnoreClipboard, NULL );
  696. ::CloseClipboard();
  697. }
  698. }
  699. /////////////////////////////////////////////////////////////////////////////
  700. // CClipboardViewer message handlers
  701. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  702. {
  703. if (CWnd::OnCreate(lpCreateStruct) == -1)
  704. return -1;
  705. // verify that we are in the chain every minute
  706. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE, 0);
  707. //Set up the clip board viewer
  708. Connect();
  709. return 0;
  710. }
  711. void CClipboardViewer::OnDestroy()
  712. {
  713. Disconnect();
  714. CWnd::OnDestroy();
  715. }
  716. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  717. {
  718. // If the next window is closing, repair the chain.
  719. if(m_hNextClipboardViewer == hWndRemove)
  720. {
  721. m_hNextClipboardViewer = hWndAfter;
  722. }
  723. // Otherwise, pass the message to the next link.
  724. else if (m_hNextClipboardViewer != NULL)
  725. {
  726. ::SendMessage ( m_hNextClipboardViewer, WM_CHANGECBCHAIN,
  727. (WPARAM) hWndRemove, (LPARAM) hWndAfter );
  728. }
  729. }
  730. //Message that the clipboard data has changed
  731. void CClipboardViewer::OnDrawClipboard()
  732. {
  733. if( m_bPinging )
  734. {
  735. m_bPingSuccess = true;
  736. return;
  737. }
  738. // don't process the event when we first attach
  739. if( m_pHandler && !m_bCalling_SetClipboardViewer )
  740. {
  741. if( !::IsClipboardFormatAvailable( theApp.m_cfIgnoreClipboard ) )
  742. m_pHandler->OnClipboardChange();
  743. }
  744. // pass the event to the next Clipboard viewer in the chain
  745. if( m_hNextClipboardViewer != NULL )
  746. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  747. }
  748. void CClipboardViewer::OnTimer(UINT nIDEvent)
  749. {
  750. switch(nIDEvent)
  751. {
  752. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  753. EnsureConnected();
  754. break;
  755. }
  756. CWnd::OnTimer(nIDEvent);
  757. }
  758. LRESULT CClipboardViewer::OnCVReconnect(WPARAM wParam, LPARAM lParam)
  759. {
  760. return EnsureConnected();
  761. }
  762. LRESULT CClipboardViewer::OnCVIsConnected(WPARAM wParam, LPARAM lParam)
  763. {
  764. return SendPing();
  765. }
  766. /*----------------------------------------------------------------------------*\
  767. CCopyConfig
  768. \*----------------------------------------------------------------------------*/
  769. /*----------------------------------------------------------------------------*\
  770. CCopyThread
  771. \*----------------------------------------------------------------------------*/
  772. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  773. CCopyThread::CCopyThread()
  774. {
  775. m_bQuit = false;
  776. m_bAutoDelete = false;
  777. m_bConfigChanged = false;
  778. m_pClips = new CClipList;
  779. m_pClipboardViewer = new CClipboardViewer(this);
  780. ::InitializeCriticalSection(&m_CS);
  781. }
  782. CCopyThread::~CCopyThread()
  783. {
  784. m_LocalConfig.DeleteTypes();
  785. m_SharedConfig.DeleteTypes();
  786. DELETE_PTR( m_pClipboardViewer );
  787. if( m_pClips )
  788. ASSERT( m_pClips->GetCount() == 0 );
  789. DELETE_PTR( m_pClips );
  790. ::DeleteCriticalSection(&m_CS);
  791. }
  792. // perform and per-thread initialization here
  793. BOOL CCopyThread::InitInstance()
  794. {
  795. ASSERT( ::GetCurrentThreadId() == m_nThreadID );
  796. SetThreadName(m_nThreadID, "COPY");
  797. // the window is created within this thread and therefore uses its message queue
  798. m_pClipboardViewer->Create();
  799. return TRUE;
  800. }
  801. // perform any per-thread cleanup here
  802. int CCopyThread::ExitInstance()
  803. {
  804. ASSERT( m_bQuit ); // make sure we intended to quit
  805. m_pClipboardViewer->Disconnect();
  806. return CWinThread::ExitInstance();
  807. }
  808. bool CCopyThread::IsClipboardViewerConnected()
  809. {
  810. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  811. return ::SendMessage( m_pClipboardViewer->m_hWnd, WM_CV_IS_CONNECTED, 0, 0 ) != FALSE;
  812. }
  813. // Called within Copy Thread:
  814. void CCopyThread::OnClipboardChange()
  815. {
  816. SyncConfig(); // synchronize with the main thread's copy configuration
  817. // if we are told not to copy on change, then we have nothing to do.
  818. if( !m_LocalConfig.m_bCopyOnChange )
  819. return;
  820. CClip* pClip = new CClip;
  821. bool bResult = pClip->LoadFromClipboard( m_LocalConfig.m_pSupportedTypes );
  822. if( !bResult )
  823. {
  824. delete pClip;
  825. return; // error
  826. }
  827. AddToClips( pClip );
  828. if( m_LocalConfig.m_bAsyncCopy )
  829. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  830. else
  831. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  832. }
  833. void CCopyThread::SyncConfig()
  834. {
  835. // atomic read
  836. if( m_bConfigChanged )
  837. {
  838. CClipTypes* pTypes = NULL;
  839. Hold();
  840. pTypes = m_LocalConfig.m_pSupportedTypes;
  841. m_LocalConfig = m_SharedConfig;
  842. // NULL means that it shouldn't have been sync'ed
  843. if( m_SharedConfig.m_pSupportedTypes == NULL )
  844. { // let m_LocalConfig keep its types
  845. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  846. pTypes = NULL; // nothing to delete
  847. }
  848. else
  849. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  850. Release();
  851. // delete old types
  852. if( pTypes )
  853. delete pTypes;
  854. }
  855. }
  856. void CCopyThread::AddToClips( CClip* pClip )
  857. {
  858. Hold();
  859. if( !m_pClips )
  860. m_pClips = new CClipList;
  861. m_pClips->AddTail( pClip ); // m_pClips now owns pClip
  862. Release();
  863. }
  864. // Shared (use thread-safe access functions below)
  865. // Called within Main thread:
  866. CClipList* CCopyThread::GetClips()
  867. {
  868. CClipList* pRet;
  869. CClipList* pClips = new CClipList;
  870. Hold();
  871. pRet = m_pClips;
  872. m_pClips = pClips;
  873. Release();
  874. return pRet;
  875. }
  876. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  877. {
  878. CClipTypes* pTemp;
  879. Hold();
  880. pTemp = m_SharedConfig.m_pSupportedTypes;
  881. m_SharedConfig.m_pSupportedTypes = pTypes;
  882. m_bConfigChanged = true;
  883. Release();
  884. if( pTemp )
  885. delete pTemp;
  886. }
  887. HWND CCopyThread::SetClipHandler( HWND hWnd )
  888. {
  889. HWND hRet;
  890. Hold();
  891. hRet = m_SharedConfig.m_hClipHandler;
  892. m_SharedConfig.m_hClipHandler = hWnd;
  893. m_bConfigChanged = (hRet != hWnd);
  894. Release();
  895. return hRet;
  896. }
  897. HWND CCopyThread::GetClipHandler()
  898. {
  899. HWND hRet;
  900. Hold();
  901. hRet = m_SharedConfig.m_hClipHandler;
  902. Release();
  903. return hRet;
  904. }
  905. bool CCopyThread::SetCopyOnChange( bool bVal )
  906. {
  907. bool bRet;
  908. Hold();
  909. bRet = m_SharedConfig.m_bCopyOnChange;
  910. m_SharedConfig.m_bCopyOnChange = bVal;
  911. m_bConfigChanged = (bRet != bVal);
  912. Release();
  913. return bRet;
  914. }
  915. bool CCopyThread::GetCopyOnChange()
  916. {
  917. bool bRet;
  918. Hold();
  919. bRet = m_SharedConfig.m_bCopyOnChange;
  920. Release();
  921. return bRet;
  922. }
  923. bool CCopyThread::SetAsyncCopy( bool bVal )
  924. {
  925. bool bRet;
  926. Hold();
  927. bRet = m_SharedConfig.m_bAsyncCopy;
  928. m_SharedConfig.m_bAsyncCopy = bVal;
  929. m_bConfigChanged = (bRet != bVal);
  930. Release();
  931. return bRet;
  932. }
  933. bool CCopyThread::GetAsyncCopy()
  934. {
  935. bool bRet;
  936. Hold();
  937. bRet = m_SharedConfig.m_bAsyncCopy;
  938. Release();
  939. return bRet;
  940. }
  941. void CCopyThread::Init( CCopyConfig cfg )
  942. {
  943. ASSERT( m_LocalConfig.m_pSupportedTypes == NULL );
  944. m_LocalConfig = m_SharedConfig = cfg;
  945. // let m_LocalConfig own the m_pSupportedTypes
  946. m_SharedConfig.m_pSupportedTypes = NULL;
  947. }
  948. bool CCopyThread::Quit()
  949. {
  950. m_bQuit = true;
  951. m_pClipboardViewer->PostMessage( WM_QUIT );
  952. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  953. }
  954. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  955. END_MESSAGE_MAP()
  956. // CCopyThread message handlers
  957. /*----------------------------------------------------------------------------*\
  958. CProcessCopy
  959. \*----------------------------------------------------------------------------*/
  960. CProcessCopy::CProcessCopy()
  961. {
  962. }
  963. CProcessCopy::~CProcessCopy()
  964. {
  965. }