ProcessCopy.cpp 26 KB

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