ProcessCopy.cpp 26 KB

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