ProcessCopy.cpp 27 KB

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