ProcessCopy.cpp 29 KB

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