Clip.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // ProcessCopy.cpp: implementation of the CProcessCopy class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "Clip.h"
  7. #include "DatabaseUtilities.h"
  8. #include <Mmsystem.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. CClipFormat::CClipFormat(CLIPFORMAT cfType, HGLOBAL hgData)
  77. {
  78. m_cfType = cfType;
  79. m_hgData = hgData;
  80. bDeleteData = true;
  81. }
  82. CClipFormat::~CClipFormat()
  83. {
  84. Free();
  85. }
  86. void CClipFormat::Clear()
  87. {
  88. m_cfType = 0;
  89. m_hgData = 0;
  90. }
  91. void CClipFormat::Free()
  92. {
  93. if(bDeleteData)
  94. {
  95. if(m_hgData)
  96. {
  97. m_hgData = ::GlobalFree( m_hgData );
  98. m_hgData = NULL;
  99. }
  100. }
  101. }
  102. /*----------------------------------------------------------------------------*\
  103. CClipFormats - holds an array of CClipFormat
  104. \*----------------------------------------------------------------------------*/
  105. // returns a pointer to the CClipFormat in this array which matches the given type
  106. // or NULL if that type doesn't exist in this array.
  107. CClipFormat* CClipFormats::FindFormat(UINT cfType)
  108. {
  109. CClipFormat* pCF;
  110. int count = GetSize();
  111. for(int i=0; i < count; i++)
  112. {
  113. pCF = &ElementAt(i);
  114. if(pCF->m_cfType == cfType)
  115. return pCF;
  116. }
  117. return NULL;
  118. }
  119. /*----------------------------------------------------------------------------*\
  120. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  121. \*----------------------------------------------------------------------------*/
  122. CClip::CClip() :
  123. m_ID(0),
  124. m_DataID(0),
  125. m_lTotalCopySize(0)
  126. {
  127. }
  128. CClip::~CClip()
  129. {
  130. EmptyFormats();
  131. }
  132. void CClip::Clear()
  133. {
  134. m_ID = 0;
  135. m_Time = 0;
  136. m_Desc = "";
  137. m_lTotalCopySize = 0;
  138. m_DataID = 0;
  139. EmptyFormats();
  140. }
  141. const CClip& CClip::operator=(const CClip &clip)
  142. {
  143. const CClipFormat* pCF;
  144. m_ID = clip.m_ID;
  145. m_DataID = clip.m_DataID;
  146. m_Time = clip.m_Time;
  147. m_lTotalCopySize = clip.m_lTotalCopySize;
  148. int nCount = clip.m_Formats.GetSize();
  149. for(int i = 0; i < nCount; i++)
  150. {
  151. pCF = &clip.m_Formats.GetData()[i];
  152. LPVOID pvData = GlobalLock(pCF->m_hgData);
  153. if(pvData)
  154. {
  155. AddFormat(pCF->m_cfType, pvData, GlobalSize(pCF->m_hgData));
  156. }
  157. GlobalUnlock(pCF->m_hgData);
  158. }
  159. //Set this after since in could get the wrong description in AddFormat
  160. m_Desc = clip.m_Desc;
  161. return *this;
  162. }
  163. void CClip::EmptyFormats()
  164. {
  165. // free global memory in m_Formats
  166. for(int i = m_Formats.GetSize()-1; i >= 0; i--)
  167. {
  168. m_Formats[i].Free();
  169. m_Formats.RemoveAt( i );
  170. }
  171. }
  172. // Adds a new Format to this Clip by copying the given data.
  173. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen)
  174. {
  175. ASSERT(pData && nLen);
  176. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  177. ASSERT(hGlobal);
  178. // update the Clip statistics
  179. m_Time = m_Time.GetCurrentTime();
  180. m_lTotalCopySize += nLen;
  181. if(!SetDescFromText(hGlobal))
  182. SetDescFromType();
  183. CClipFormat format(cfType,hGlobal);
  184. CClipFormat *pFormat;
  185. pFormat = m_Formats.FindFormat(cfType);
  186. // if the format type already exists as part of this clip, replace the data
  187. if(pFormat)
  188. {
  189. pFormat->Free();
  190. pFormat->m_hgData = format.m_hgData;
  191. }
  192. else
  193. {
  194. m_Formats.Add(format);
  195. }
  196. format.m_hgData = 0; // now owned by m_Formats
  197. return true;
  198. }
  199. // Fills this CClip with the contents of the clipboard.
  200. bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
  201. {
  202. COleDataObjectEx oleData;
  203. CClipTypes defaultTypes;
  204. CClipTypes* pTypes = pClipTypes;
  205. // m_Formats should be empty when this is called.
  206. ASSERT(m_Formats.GetSize() == 0);
  207. // If the data is supposed to be private, then return
  208. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  209. {
  210. return false;
  211. }
  212. //Attach to the clipboard
  213. if(!oleData.AttachClipboard())
  214. {
  215. ASSERT(0); // does this ever happen?
  216. return false;
  217. }
  218. oleData.EnsureClipboardObject();
  219. // if no types were given, get only the first (most important) type.
  220. // (subsequent types could be synthetic due to automatic type conversions)
  221. if(pTypes == NULL || pTypes->GetSize() == 0)
  222. {
  223. ASSERT(0); // this feature is not currently used... it is an error if it is.
  224. FORMATETC formatEtc;
  225. oleData.BeginEnumFormats();
  226. oleData.GetNextFormat(&formatEtc);
  227. defaultTypes.Add(formatEtc.cfFormat);
  228. pTypes = &defaultTypes;
  229. }
  230. // reset copy stats
  231. m_lTotalCopySize = 0;
  232. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  233. // Get Description String
  234. // NOTE: We make sure that the description always corresponds to the
  235. // data saved by using the exact same globalmem instance as the source
  236. // for both... i.e. we only fetch the description format type once.
  237. CClipFormat cfDesc;
  238. bool bIsDescSet = false;
  239. cfDesc.m_cfType = CF_TEXT;
  240. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  241. {
  242. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  243. bIsDescSet = SetDescFromText(cfDesc.m_hgData);
  244. }
  245. // Get global data for each supported type on the clipboard
  246. UINT nSize;
  247. CClipFormat cf;
  248. int numTypes = pTypes->GetSize();
  249. for(int i = 0; i < numTypes; i++)
  250. {
  251. cf.m_cfType = pTypes->ElementAt(i);
  252. // is this the description we already fetched?
  253. if(cf.m_cfType == cfDesc.m_cfType)
  254. {
  255. cf = cfDesc;
  256. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  257. }
  258. else if(!oleData.IsDataAvailable(cf.m_cfType))
  259. {
  260. continue;
  261. }
  262. else
  263. {
  264. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  265. }
  266. if(cf.m_hgData)
  267. {
  268. nSize = GlobalSize(cf.m_hgData);
  269. if(nSize > 0)
  270. {
  271. if(g_Opt.m_lMaxClipSizeInBytes > 0 && nSize > g_Opt.m_lMaxClipSizeInBytes)
  272. {
  273. CString cs;
  274. cs.Format("Maximum clip size reached max size = %d, clip size = %d", g_Opt.m_lMaxClipSizeInBytes, nSize);
  275. Log(cs);
  276. oleData.Release();
  277. return false;
  278. }
  279. ASSERT(::IsValid(cf.m_hgData));
  280. m_Formats.Add(cf);
  281. m_lTotalCopySize += nSize;
  282. }
  283. else
  284. {
  285. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  286. cf.Free();
  287. }
  288. cf.m_hgData = 0; // m_Formats owns it now
  289. }
  290. }
  291. m_Time = CTime::GetCurrentTime();
  292. if(!bIsDescSet)
  293. {
  294. SetDescFromType();
  295. }
  296. // if the description was in a type that is not supported,
  297. //we have to free it since it wasn't added to m_Formats
  298. if(cfDesc.m_hgData)
  299. {
  300. cfDesc.Free();
  301. }
  302. oleData.Release();
  303. if(m_Formats.GetSize() == 0)
  304. {
  305. return false;
  306. }
  307. return true;
  308. }
  309. bool CClip::SetDescFromText(HGLOBAL hgData)
  310. {
  311. if(hgData == 0)
  312. return false;
  313. bool bRet = false;
  314. char* text = (char *) GlobalLock(hgData);
  315. long ulBufLen = GlobalSize(hgData);
  316. ASSERT(text != NULL);
  317. if(ulBufLen > g_Opt.m_bDescTextSize)
  318. {
  319. ulBufLen = g_Opt.m_bDescTextSize;
  320. }
  321. if(ulBufLen > 0)
  322. {
  323. char* buf = m_Desc.GetBuffer(ulBufLen);
  324. memcpy(buf, text, ulBufLen); // in most cases, last char == null
  325. buf[ulBufLen-1] = '\0'; // just in case not null terminated
  326. m_Desc.ReleaseBuffer(); // scans for the null
  327. bRet = m_Desc.GetLength() > 0;
  328. }
  329. //Unlock the data
  330. GlobalUnlock(hgData);
  331. return bRet;
  332. }
  333. bool CClip::SetDescFromType()
  334. {
  335. if(m_Formats.GetSize() <= 0)
  336. {
  337. return false;
  338. }
  339. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  340. return m_Desc.GetLength() > 0;
  341. }
  342. bool CClip::AddToDB(bool bCheckForDuplicates)
  343. {
  344. bool bResult;
  345. try
  346. {
  347. if(bCheckForDuplicates)
  348. {
  349. CMainTable recset;
  350. if(FindDuplicate(recset, g_Opt.m_bAllowDuplicates))
  351. {
  352. m_ID = recset.m_lID;
  353. recset.Edit();
  354. recset.m_lDate = (long) m_Time.GetTime(); // update the copy Time
  355. recset.Update();
  356. recset.Close();
  357. EmptyFormats(); // delete this clip's data from memory.
  358. return true;
  359. }
  360. if(recset.IsOpen())
  361. recset.Close();
  362. }
  363. }
  364. CATCHDAO
  365. // AddToDataTable must go first in order to assign m_DataID
  366. bResult = AddToDataTable() && AddToMainTable();
  367. if(bResult)
  368. {
  369. if(g_Opt.m_csPlaySoundOnCopy.GetLength() > 0)
  370. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  371. }
  372. // should be emptied by AddToDataTable
  373. ASSERT(m_Formats.GetSize() == 0);
  374. return bResult;
  375. }
  376. // if a duplicate exists, set recset to the duplicate and return true
  377. bool CClip::FindDuplicate(CMainTable& recset, BOOL bCheckLastOnly)
  378. {
  379. ASSERT(m_lTotalCopySize > 0);
  380. try
  381. {
  382. recset.m_strSort = "lDate DESC";
  383. if(bCheckLastOnly)
  384. {
  385. recset.Open("SELECT * FROM Main");
  386. if(recset.IsEOF())
  387. {
  388. return false;
  389. }
  390. recset.MoveFirst();
  391. // if an entry exists and they are the same size and the format data matches
  392. if(!recset.IsBOF() && !recset.IsEOF() &&
  393. m_lTotalCopySize == recset.m_lTotalCopySize &&
  394. (CompareFormatDataTo(recset.m_lDataID) == 0))
  395. {
  396. return true;
  397. }
  398. return false;
  399. }
  400. // Look for any other entries that have the same size
  401. recset.Open("SELECT * FROM Main WHERE lTotalCopySize = %d", m_lTotalCopySize);
  402. while(!recset.IsEOF())
  403. {
  404. //if there is any then look if it is an exact match
  405. if(CompareFormatDataTo(recset.m_lDataID) == 0)
  406. {
  407. return true;
  408. }
  409. recset.MoveNext();
  410. }
  411. }
  412. CATCHDAO
  413. return false;
  414. }
  415. int CClip::CompareFormatDataTo(long lDataID)
  416. {
  417. int nRet = 0;
  418. int nRecs=0, nFormats=0;
  419. CClipFormat* pFormat = NULL;
  420. try
  421. {
  422. CDataTable recset;
  423. recset.Open("SELECT * FROM Data WHERE lDataID = %d", lDataID);
  424. if( !recset.IsBOF() && !recset.IsEOF() )
  425. {
  426. // Verify the same number of saved types
  427. recset.MoveLast();
  428. nRecs = recset.GetRecordCount();
  429. }
  430. nFormats = m_Formats.GetSize();
  431. nRet = nFormats - nRecs;
  432. if(nRet != 0 || nRecs == 0)
  433. {
  434. recset.Close();
  435. return nRet;
  436. }
  437. // For each format type in the db
  438. for(recset.MoveFirst(); !recset.IsEOF(); recset.MoveNext())
  439. {
  440. pFormat = m_Formats.FindFormat(GetFormatID(recset.m_strClipBoardFormat));
  441. // Verify the format exists
  442. if(!pFormat)
  443. {
  444. recset.Close();
  445. return -1;
  446. }
  447. // Compare the size
  448. nRet = ::GlobalSize(pFormat->m_hgData) - recset.m_ooData.m_dwDataLength;
  449. if( nRet != 0 )
  450. {
  451. recset.Close();
  452. return nRet;
  453. }
  454. // Binary compare
  455. nRet = CompareGlobalHH(recset.m_ooData.m_hData, pFormat->m_hgData, recset.m_ooData.m_dwDataLength);
  456. if(nRet != 0)
  457. {
  458. recset.Close();
  459. return nRet;
  460. }
  461. }
  462. recset.Close();
  463. }
  464. CATCHDAO
  465. return 0;
  466. }
  467. // assigns m_ID
  468. bool CClip::AddToMainTable()
  469. {
  470. try
  471. {
  472. CMainTable recset;
  473. // recset.m_strSort = "lDate DESC";
  474. recset.Open("SELECT * FROM Main");
  475. long 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. }