ProcessCopy.cpp 25 KB

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