ProcessCopy.cpp 28 KB

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