ProcessCopy.cpp 28 KB

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