ProcessCopy.cpp 28 KB

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