ProcessCopy.cpp 28 KB

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