ProcessCopy.cpp 26 KB

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