ProcessCopy.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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(g_Opt.m_csPlaySoundOnCopy.GetLength() > 0)
  352. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  353. // should be emptied by AddToDataTable
  354. ASSERT( m_Formats.GetSize() == 0 );
  355. return bResult;
  356. }
  357. // if a duplicate exists, set recset to the duplicate and return true
  358. bool CClip::FindDuplicate( CMainTable& recset, BOOL bCheckLastOnly )
  359. {
  360. ASSERT( m_lTotalCopySize > 0 );
  361. try
  362. {
  363. recset.m_strSort = "lDate DESC";
  364. if( bCheckLastOnly )
  365. {
  366. recset.Open("SELECT * FROM Main");
  367. if(recset.IsEOF())
  368. return false;
  369. recset.MoveFirst();
  370. // if an entry exists and they are the same size and the format data matches
  371. if( !recset.IsBOF() && !recset.IsEOF() &&
  372. m_lTotalCopySize == recset.m_lTotalCopySize &&
  373. (CompareFormatDataTo(recset.m_lDataID) == 0) )
  374. { return true; }
  375. return false;
  376. }
  377. // Look for any other entries that have the same size
  378. recset.Open("SELECT * FROM Main WHERE lTotalCopySize = %d", m_lTotalCopySize);
  379. while( !recset.IsEOF() )
  380. {
  381. //if there is any then look if it is an exact match
  382. if( CompareFormatDataTo(recset.m_lDataID) == 0 )
  383. return true;
  384. recset.MoveNext();
  385. }
  386. }
  387. CATCHDAO
  388. return false;
  389. }
  390. int CClip::CompareFormatDataTo( long lDataID )
  391. {
  392. int nRet = 0;
  393. int nRecs=0, nFormats=0;
  394. CClipFormat* pFormat = NULL;
  395. try
  396. {
  397. CDataTable recset;
  398. recset.Open("SELECT * FROM Data WHERE lDataID = %d", lDataID);
  399. if( !recset.IsBOF() && !recset.IsEOF() )
  400. {
  401. // Verify the same number of saved types
  402. recset.MoveLast();
  403. nRecs = recset.GetRecordCount();
  404. }
  405. nFormats = m_Formats.GetSize();
  406. nRet = nFormats - nRecs;
  407. if( nRet != 0 || nRecs == 0 )
  408. { recset.Close(); return nRet; }
  409. // For each format type in the db
  410. for( recset.MoveFirst(); !recset.IsEOF(); recset.MoveNext() )
  411. {
  412. pFormat = m_Formats.FindFormat( GetFormatID(recset.m_strClipBoardFormat) );
  413. // Verify the format exists
  414. if( !pFormat )
  415. { recset.Close(); return -1; }
  416. // Compare the size
  417. nRet = ::GlobalSize(pFormat->m_hgData) - recset.m_ooData.m_dwDataLength;
  418. if( nRet != 0 )
  419. { recset.Close(); return nRet; }
  420. // Binary compare
  421. nRet = CompareGlobalHH( recset.m_ooData.m_hData,
  422. pFormat->m_hgData,
  423. recset.m_ooData.m_dwDataLength );
  424. if( nRet != 0 )
  425. { recset.Close(); return nRet; }
  426. }
  427. recset.Close();
  428. }
  429. CATCHDAO
  430. return 0;
  431. }
  432. // assigns m_ID
  433. bool CClip::AddToMainTable()
  434. {
  435. long lDate;
  436. try
  437. {
  438. CMainTable recset;
  439. // recset.m_strSort = "lDate DESC";
  440. recset.Open("SELECT * FROM Main");
  441. lDate = (long) m_Time.GetTime();
  442. recset.AddNew(); // overridden to set m_lID to the new autoincr number
  443. m_ID = recset.m_lID;
  444. recset.m_lDate = lDate;
  445. recset.m_strText = m_Desc;
  446. recset.m_lTotalCopySize = m_lTotalCopySize;
  447. recset.m_bIsGroup = FALSE;
  448. recset.m_lParentID = theApp.m_GroupDefaultID;
  449. VERIFY( m_DataID > 0 ); // AddToDataTable must be called first to assign this
  450. recset.m_lDataID = m_DataID;
  451. recset.Update();
  452. // recset.SetBookmark( recset.GetLastModifiedBookmark() );
  453. // m_ID = recset.m_lID;
  454. recset.Close();
  455. }
  456. catch(CDaoException* e)
  457. {
  458. ASSERT(FALSE);
  459. e->Delete();
  460. return false;
  461. }
  462. return true;
  463. }
  464. // Empties m_Formats as it saves them to the Data Table.
  465. bool CClip::AddToDataTable()
  466. {
  467. VERIFY( m_DataID <= 0 ); // this func will assign m_DataID
  468. try
  469. {
  470. CClipFormat* pCF;
  471. CDataTable recset;
  472. recset.Open(dbOpenTable,"Data");
  473. for( int i = m_Formats.GetSize()-1; i >= 0 ; i-- )
  474. {
  475. pCF = & m_Formats.ElementAt(i);
  476. recset.AddNew(); // overridden to assign new autoincr ID to m_lID
  477. if( m_DataID <= 0 )
  478. {
  479. VERIFY( recset.m_lID > 0 );
  480. m_DataID = recset.m_lID;
  481. }
  482. recset.m_lDataID = m_DataID;
  483. recset.m_strClipBoardFormat = GetFormatName( pCF->m_cfType );
  484. // the recset takes ownership of the HGLOBAL
  485. recset.ReplaceData( pCF->m_hgData, GlobalSize(pCF->m_hgData) );
  486. recset.Update();
  487. m_Formats.RemoveAt( i ); // the recset now owns the global
  488. }
  489. recset.Close();
  490. return true;
  491. }
  492. CATCHDAO
  493. return false;
  494. }
  495. // changes m_Time to be later than the latest clip entry in the db
  496. // ensures that pClip's time is not older than the last clip added
  497. // old times can happen on fast copies (<1 sec).
  498. void CClip::MakeLatestTime()
  499. {
  500. long lDate;
  501. try
  502. {
  503. CMainTable recset;
  504. recset.m_strSort = "lDate DESC";
  505. recset.Open("SELECT * FROM Main");
  506. if(!recset.IsEOF())
  507. {
  508. recset.MoveFirst();
  509. lDate = (long) m_Time.GetTime();
  510. if( lDate <= recset.m_lDate )
  511. {
  512. lDate = recset.m_lDate + 1;
  513. m_Time = lDate;
  514. }
  515. }
  516. recset.Close();
  517. }
  518. CATCHDAO
  519. }
  520. // STATICS
  521. // Allocates a Global containing the requested Clip Format Data
  522. HGLOBAL CClip::LoadFormat( long lID, UINT cfType )
  523. {
  524. HGLOBAL hGlobal = 0;
  525. try
  526. {
  527. CDataTable recset;
  528. CString csSQL;
  529. csSQL.Format(
  530. "SELECT Data.* FROM Data "
  531. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  532. "WHERE Main.lID = %d "
  533. "AND Data.strClipBoardFormat = \'%s\'",
  534. lID,
  535. GetFormatName(cfType));
  536. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  537. if( !recset.IsBOF() && !recset.IsEOF() )
  538. {
  539. // create a new HGLOBAL duplicate
  540. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  541. // XOR take the recset's HGLOBAL... is this SAFE??
  542. // hGlobal = recset.TakeData();
  543. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  544. {
  545. TRACE0( GetErrorString(::GetLastError()) );
  546. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  547. ASSERT(FALSE);
  548. }
  549. }
  550. recset.Close();
  551. }
  552. CATCHDAO
  553. return hGlobal;
  554. }
  555. bool CClip::LoadFormats( long lID, CClipFormats& formats, bool bOnlyLoad_CF_TEXT)
  556. {
  557. CClipFormat cf;
  558. HGLOBAL hGlobal = 0;
  559. formats.RemoveAll();
  560. try
  561. {
  562. BOOL bShiftIsDown = ((GetKeyState(VK_SHIFT) & 0x8000) || bOnlyLoad_CF_TEXT);
  563. CDataTable recset;
  564. //Open the data table for all that have the parent id
  565. CString csSQL;
  566. csSQL.Format(
  567. "SELECT Data.* FROM Data "
  568. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  569. "WHERE Main.lID = %d", lID);
  570. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  571. while( !recset.IsEOF() )
  572. {
  573. cf.m_cfType = GetFormatID( recset.m_strClipBoardFormat );
  574. if(bShiftIsDown)
  575. {
  576. if(cf.m_cfType != CF_TEXT)
  577. {
  578. recset.MoveNext();
  579. continue;
  580. }
  581. }
  582. // create a new HGLOBAL duplicate
  583. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  584. // XOR take the recset's HGLOBAL... is this SAFE??
  585. // hGlobal = recset.TakeData();
  586. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  587. {
  588. TRACE0( GetErrorString(::GetLastError()) );
  589. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  590. ASSERT(FALSE);
  591. }
  592. cf.m_hgData = hGlobal;
  593. formats.Add( cf );
  594. recset.MoveNext();
  595. }
  596. cf.m_hgData = 0; // formats owns all the data
  597. recset.Close();
  598. }
  599. CATCHDAO
  600. return formats.GetSize() > 0;
  601. }
  602. void CClip::LoadTypes( long lID, CClipTypes& types )
  603. {
  604. types.RemoveAll();
  605. try
  606. {
  607. CDataTable recset;
  608. CString csSQL;
  609. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  610. csSQL.Format(
  611. "SELECT Data.* FROM Data "
  612. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  613. "WHERE Main.lID = %d", lID);
  614. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  615. while( !recset.IsEOF() )
  616. {
  617. types.Add( GetFormatID( recset.m_strClipBoardFormat ) );
  618. recset.MoveNext();
  619. }
  620. recset.Close();
  621. }
  622. CATCHDAO
  623. }
  624. /*----------------------------------------------------------------------------*\
  625. CClipList
  626. \*----------------------------------------------------------------------------*/
  627. CClipList::~CClipList()
  628. {
  629. CClip* pClip;
  630. while( GetCount() )
  631. {
  632. pClip = RemoveHead();
  633. DELETE_PTR( pClip );
  634. }
  635. }
  636. // returns the number of clips actually saved
  637. // while this does empty the Format Data, it does not delete the Clips.
  638. int CClipList::AddToDB( bool bLatestTime, bool bShowStatus )
  639. {
  640. int savedCount = 0;
  641. int nRemaining = 0;
  642. CClip* pClip;
  643. POSITION pos;
  644. bool bResult;
  645. nRemaining = GetCount();
  646. pos = GetHeadPosition();
  647. while( pos )
  648. {
  649. if( bShowStatus )
  650. {
  651. theApp.SetStatus( StrF("%d",nRemaining), true );
  652. nRemaining--;
  653. }
  654. pClip = GetNext( pos );
  655. ASSERT( pClip );
  656. if( bLatestTime )
  657. pClip->MakeLatestTime();
  658. bResult = pClip->AddToDB();
  659. if( bResult )
  660. savedCount++;
  661. }
  662. if( bShowStatus )
  663. theApp.SetStatus(NULL, true);
  664. return savedCount;
  665. }
  666. const CClipList& CClipList::operator=(const CClipList &cliplist)
  667. {
  668. POSITION pos;
  669. CClip* pClip;
  670. pos = cliplist.GetHeadPosition();
  671. while(pos)
  672. {
  673. pClip = cliplist.GetNext(pos);
  674. ASSERT(pClip);
  675. CClip *pNewClip = new CClip;
  676. if(pNewClip)
  677. {
  678. *pNewClip = *pClip;
  679. AddTail(pNewClip);
  680. }
  681. }
  682. return *this;
  683. }
  684. /*----------------------------------------------------------------------------*\
  685. CClipboardViewer
  686. \*----------------------------------------------------------------------------*/
  687. IMPLEMENT_DYNAMIC(CClipboardViewer, CWnd)
  688. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  689. //{{AFX_MSG_MAP(CClipboardViewer)
  690. ON_WM_CREATE()
  691. ON_WM_CHANGECBCHAIN()
  692. ON_WM_DRAWCLIPBOARD()
  693. ON_WM_TIMER()
  694. //}}AFX_MSG_MAP
  695. ON_MESSAGE(WM_CV_GETCONNECT, OnCVGetConnect)
  696. ON_MESSAGE(WM_CV_SETCONNECT, OnCVSetConnect)
  697. ON_MESSAGE(WM_CV_IS_CONNECTED, OnCVIsConnected)
  698. ON_WM_DESTROY()
  699. END_MESSAGE_MAP()
  700. /////////////////////////////////////////////////////////////////////////////
  701. // CClipboardViewer construction/destruction
  702. CClipboardViewer::CClipboardViewer( CCopyThread* pHandler )
  703. {
  704. m_hNextClipboardViewer = 0;
  705. m_bCalling_SetClipboardViewer = false;
  706. m_lReconectCount = 0;
  707. m_bIsConnected = false;
  708. m_bConnect = false;
  709. m_pHandler = pHandler;
  710. ASSERT(m_pHandler);
  711. m_bPinging = false;
  712. m_bPingSuccess = false;
  713. }
  714. CClipboardViewer::~CClipboardViewer()
  715. {
  716. }
  717. void CClipboardViewer::Create()
  718. {
  719. CString strParentClass = AfxRegisterWndClass(0);
  720. CWnd::CreateEx(0, strParentClass, "Ditto Clipboard Viewer", 0, -1, -1, 0, 0, 0, 0);
  721. SetConnect( true );
  722. }
  723. // connects as a clipboard viewer
  724. void CClipboardViewer::Connect()
  725. {
  726. // if( m_bIsConnected )
  727. // return;
  728. ASSERT( ::IsWindow(m_hWnd) );
  729. //Set up the clip board viewer
  730. m_bCalling_SetClipboardViewer = true;
  731. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  732. m_bCalling_SetClipboardViewer = false;
  733. m_bIsConnected = SendPing();
  734. // verify that we are in the chain every minute
  735. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE, 0);
  736. }
  737. // disconnects as a clipboard viewer
  738. void CClipboardViewer::Disconnect()
  739. {
  740. // if( !m_bIsConnected )
  741. // return;
  742. ASSERT( ::IsWindow(m_hWnd) );
  743. KillTimer(TIMER_ENSURE_VIEWER_IN_CHAIN);
  744. CWnd::ChangeClipboardChain( m_hNextClipboardViewer );
  745. m_hNextClipboardViewer = 0;
  746. m_bIsConnected = false;
  747. }
  748. bool CClipboardViewer::SendPing()
  749. {
  750. HWND hWnd;
  751. bool bResult = false;
  752. hWnd = ::GetClipboardViewer();
  753. // if there is a chain
  754. if( ::IsWindow(hWnd) )
  755. {
  756. m_bPingSuccess = false;
  757. m_bPinging = true;
  758. ::SendMessage( hWnd, WM_DRAWCLIPBOARD, 0, 0 );
  759. m_bPinging = false;
  760. bResult = m_bPingSuccess;
  761. }
  762. m_bIsConnected = bResult;
  763. return bResult;
  764. }
  765. bool CClipboardViewer::EnsureConnected()
  766. {
  767. if( !SendPing() )
  768. Connect();
  769. return m_bIsConnected;
  770. }
  771. // puts format "Clipboard Viewer Ignore" on the clipboard
  772. void CClipboardViewer::SetCVIgnore()
  773. {
  774. if( ::OpenClipboard( m_hWnd ) )
  775. {
  776. ::SetClipboardData( theApp.m_cfIgnoreClipboard, NULL );
  777. ::CloseClipboard();
  778. }
  779. }
  780. void CClipboardViewer::SetConnect( bool bConnect )
  781. {
  782. m_bConnect = bConnect;
  783. if( m_bConnect )
  784. EnsureConnected();
  785. else
  786. Disconnect();
  787. }
  788. /////////////////////////////////////////////////////////////////////////////
  789. // CClipboardViewer message handlers
  790. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  791. {
  792. if (CWnd::OnCreate(lpCreateStruct) == -1)
  793. return -1;
  794. //Set up the clip board viewer
  795. Connect();
  796. return 0;
  797. }
  798. void CClipboardViewer::OnDestroy()
  799. {
  800. Disconnect();
  801. CWnd::OnDestroy();
  802. }
  803. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  804. {
  805. // If the next window is closing, repair the chain.
  806. if(m_hNextClipboardViewer == hWndRemove)
  807. {
  808. m_hNextClipboardViewer = hWndAfter;
  809. }
  810. // Otherwise, pass the message to the next link.
  811. else if (m_hNextClipboardViewer != NULL)
  812. {
  813. if(m_hNextClipboardViewer != m_hWnd)
  814. {
  815. ::SendMessage ( m_hNextClipboardViewer, WM_CHANGECBCHAIN,
  816. (WPARAM) hWndRemove, (LPARAM) hWndAfter );
  817. }
  818. else
  819. {
  820. m_hNextClipboardViewer = NULL;
  821. }
  822. }
  823. }
  824. //Message that the clipboard data has changed
  825. void CClipboardViewer::OnDrawClipboard()
  826. {
  827. if( m_bPinging )
  828. {
  829. m_bPingSuccess = true;
  830. return;
  831. }
  832. // don't process the event when we first attach
  833. if( m_pHandler && !m_bCalling_SetClipboardViewer )
  834. {
  835. if( !::IsClipboardFormatAvailable( theApp.m_cfIgnoreClipboard ) )
  836. m_pHandler->OnClipboardChange();
  837. }
  838. // pass the event to the next Clipboard viewer in the chain
  839. if( m_hNextClipboardViewer != NULL )
  840. {
  841. if(m_hNextClipboardViewer != m_hWnd)
  842. {
  843. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  844. }
  845. else
  846. {
  847. m_hNextClipboardViewer = NULL;
  848. }
  849. }
  850. }
  851. void CClipboardViewer::OnTimer(UINT nIDEvent)
  852. {
  853. switch(nIDEvent)
  854. {
  855. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  856. EnsureConnected();
  857. break;
  858. }
  859. CWnd::OnTimer(nIDEvent);
  860. }
  861. LRESULT CClipboardViewer::OnCVGetConnect(WPARAM wParam, LPARAM lParam)
  862. {
  863. return GetConnect();
  864. }
  865. LRESULT CClipboardViewer::OnCVSetConnect(WPARAM wParam, LPARAM lParam)
  866. {
  867. SetConnect( wParam != FALSE ); // convert to bool
  868. return TRUE;
  869. }
  870. LRESULT CClipboardViewer::OnCVIsConnected(WPARAM wParam, LPARAM lParam)
  871. {
  872. return SendPing();
  873. }
  874. /*----------------------------------------------------------------------------*\
  875. CCopyConfig
  876. \*----------------------------------------------------------------------------*/
  877. /*----------------------------------------------------------------------------*\
  878. CCopyThread
  879. \*----------------------------------------------------------------------------*/
  880. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  881. CCopyThread::CCopyThread()
  882. {
  883. m_bQuit = false;
  884. m_bAutoDelete = false;
  885. m_bConfigChanged = false;
  886. m_pClips = new CClipList;
  887. m_pClipboardViewer = new CClipboardViewer(this);
  888. ::InitializeCriticalSection(&m_CS);
  889. }
  890. CCopyThread::~CCopyThread()
  891. {
  892. m_LocalConfig.DeleteTypes();
  893. m_SharedConfig.DeleteTypes();
  894. DELETE_PTR( m_pClipboardViewer );
  895. if( m_pClips )
  896. ASSERT( m_pClips->GetCount() == 0 );
  897. DELETE_PTR( m_pClips );
  898. ::DeleteCriticalSection(&m_CS);
  899. }
  900. // perform and per-thread initialization here
  901. BOOL CCopyThread::InitInstance()
  902. {
  903. ASSERT( ::GetCurrentThreadId() == m_nThreadID );
  904. SetThreadName(m_nThreadID, "COPY");
  905. // the window is created within this thread and therefore uses its message queue
  906. m_pClipboardViewer->Create();
  907. return TRUE;
  908. }
  909. // perform any per-thread cleanup here
  910. int CCopyThread::ExitInstance()
  911. {
  912. ASSERT( m_bQuit ); // make sure we intended to quit
  913. m_pClipboardViewer->Disconnect();
  914. return CWinThread::ExitInstance();
  915. }
  916. // Called within Copy Thread:
  917. void CCopyThread::OnClipboardChange()
  918. {
  919. SyncConfig(); // synchronize with the main thread's copy configuration
  920. // if we are told not to copy on change, then we have nothing to do.
  921. if( !m_LocalConfig.m_bCopyOnChange )
  922. return;
  923. CClip* pClip = new CClip;
  924. bool bResult = pClip->LoadFromClipboard( m_LocalConfig.m_pSupportedTypes );
  925. if( !bResult )
  926. {
  927. delete pClip;
  928. return; // error
  929. }
  930. AddToClips( pClip );
  931. if( m_LocalConfig.m_bAsyncCopy )
  932. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  933. else
  934. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  935. }
  936. void CCopyThread::SyncConfig()
  937. {
  938. // atomic read
  939. if( m_bConfigChanged )
  940. {
  941. CClipTypes* pTypes = NULL;
  942. Hold();
  943. pTypes = m_LocalConfig.m_pSupportedTypes;
  944. m_LocalConfig = m_SharedConfig;
  945. // NULL means that it shouldn't have been sync'ed
  946. if( m_SharedConfig.m_pSupportedTypes == NULL )
  947. { // let m_LocalConfig keep its types
  948. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  949. pTypes = NULL; // nothing to delete
  950. }
  951. else
  952. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  953. Release();
  954. // delete old types
  955. if( pTypes )
  956. delete pTypes;
  957. }
  958. }
  959. void CCopyThread::AddToClips( CClip* pClip )
  960. {
  961. Hold();
  962. if( !m_pClips )
  963. m_pClips = new CClipList;
  964. m_pClips->AddTail( pClip ); // m_pClips now owns pClip
  965. Release();
  966. }
  967. // Shared (use thread-safe access functions below)
  968. // Called within Main thread:
  969. bool CCopyThread::IsClipboardViewerConnected()
  970. {
  971. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  972. return ::SendMessage( m_pClipboardViewer->m_hWnd, WM_CV_IS_CONNECTED, 0, 0 ) != FALSE;
  973. }
  974. bool CCopyThread::GetConnectCV()
  975. {
  976. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  977. return ::SendMessage( m_pClipboardViewer->m_hWnd, WM_CV_GETCONNECT, 0, 0 ) != FALSE;
  978. }
  979. void CCopyThread::SetConnectCV( bool bConnect )
  980. {
  981. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  982. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_CV_SETCONNECT, bConnect, 0 );
  983. }
  984. CClipList* CCopyThread::GetClips()
  985. {
  986. CClipList* pRet;
  987. CClipList* pClips = new CClipList;
  988. Hold();
  989. pRet = m_pClips;
  990. m_pClips = pClips;
  991. Release();
  992. return pRet;
  993. }
  994. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  995. {
  996. CClipTypes* pTemp;
  997. Hold();
  998. pTemp = m_SharedConfig.m_pSupportedTypes;
  999. m_SharedConfig.m_pSupportedTypes = pTypes;
  1000. m_bConfigChanged = true;
  1001. Release();
  1002. if( pTemp )
  1003. delete pTemp;
  1004. }
  1005. HWND CCopyThread::SetClipHandler( HWND hWnd )
  1006. {
  1007. HWND hRet;
  1008. Hold();
  1009. hRet = m_SharedConfig.m_hClipHandler;
  1010. m_SharedConfig.m_hClipHandler = hWnd;
  1011. m_bConfigChanged = (hRet != hWnd);
  1012. Release();
  1013. return hRet;
  1014. }
  1015. HWND CCopyThread::GetClipHandler()
  1016. {
  1017. HWND hRet;
  1018. Hold();
  1019. hRet = m_SharedConfig.m_hClipHandler;
  1020. Release();
  1021. return hRet;
  1022. }
  1023. bool CCopyThread::SetCopyOnChange( bool bVal )
  1024. {
  1025. bool bRet;
  1026. Hold();
  1027. bRet = m_SharedConfig.m_bCopyOnChange;
  1028. m_SharedConfig.m_bCopyOnChange = bVal;
  1029. m_bConfigChanged = (bRet != bVal);
  1030. Release();
  1031. return bRet;
  1032. }
  1033. bool CCopyThread::GetCopyOnChange()
  1034. {
  1035. bool bRet;
  1036. Hold();
  1037. bRet = m_SharedConfig.m_bCopyOnChange;
  1038. Release();
  1039. return bRet;
  1040. }
  1041. bool CCopyThread::SetAsyncCopy( bool bVal )
  1042. {
  1043. bool bRet;
  1044. Hold();
  1045. bRet = m_SharedConfig.m_bAsyncCopy;
  1046. m_SharedConfig.m_bAsyncCopy = bVal;
  1047. m_bConfigChanged = (bRet != bVal);
  1048. Release();
  1049. return bRet;
  1050. }
  1051. bool CCopyThread::GetAsyncCopy()
  1052. {
  1053. bool bRet;
  1054. Hold();
  1055. bRet = m_SharedConfig.m_bAsyncCopy;
  1056. Release();
  1057. return bRet;
  1058. }
  1059. void CCopyThread::Init( CCopyConfig cfg )
  1060. {
  1061. ASSERT( m_LocalConfig.m_pSupportedTypes == NULL );
  1062. m_LocalConfig = m_SharedConfig = cfg;
  1063. // let m_LocalConfig own the m_pSupportedTypes
  1064. m_SharedConfig.m_pSupportedTypes = NULL;
  1065. }
  1066. bool CCopyThread::Quit()
  1067. {
  1068. m_bQuit = true;
  1069. m_pClipboardViewer->PostMessage( WM_QUIT );
  1070. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  1071. }
  1072. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  1073. END_MESSAGE_MAP()
  1074. // CCopyThread message handlers
  1075. /*----------------------------------------------------------------------------*\
  1076. CProcessCopy
  1077. \*----------------------------------------------------------------------------*/
  1078. CProcessCopy::CProcessCopy()
  1079. {
  1080. }
  1081. CProcessCopy::~CProcessCopy()
  1082. {
  1083. }