ProcessCopy.cpp 28 KB

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