Clip.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. // if(g_Opt.m_bPrePendDateToClip &&
  269. // cf.m_cfType == CF_TEXT || cf.m_cfType == theApp.m_RTF_Format)
  270. // {
  271. // PrependDateToFormat(cf);
  272. // }
  273. nSize = GlobalSize(cf.m_hgData);
  274. if(nSize > 0)
  275. {
  276. if(g_Opt.m_lMaxClipSizeInBytes > 0 && nSize > g_Opt.m_lMaxClipSizeInBytes)
  277. {
  278. CString cs;
  279. cs.Format("Maximum clip size reached max size = %d, clip size = %d", g_Opt.m_lMaxClipSizeInBytes, nSize);
  280. Log(cs);
  281. oleData.Release();
  282. return false;
  283. }
  284. ASSERT(::IsValid(cf.m_hgData));
  285. m_Formats.Add(cf);
  286. m_lTotalCopySize += nSize;
  287. }
  288. else
  289. {
  290. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  291. cf.Free();
  292. }
  293. cf.m_hgData = 0; // m_Formats owns it now
  294. }
  295. }
  296. m_Time = CTime::GetCurrentTime();
  297. if(!bIsDescSet)
  298. {
  299. SetDescFromType();
  300. }
  301. // if the description was in a type that is not supported,
  302. //we have to free it since it wasn't added to m_Formats
  303. if(cfDesc.m_hgData)
  304. {
  305. cfDesc.Free();
  306. }
  307. oleData.Release();
  308. if(m_Formats.GetSize() == 0)
  309. {
  310. return false;
  311. }
  312. return true;
  313. }
  314. bool CClip::SetDescFromText(HGLOBAL hgData)
  315. {
  316. if(hgData == 0)
  317. return false;
  318. bool bRet = false;
  319. char* text = (char *) GlobalLock(hgData);
  320. long ulBufLen = GlobalSize(hgData);
  321. ASSERT(text != NULL);
  322. if(ulBufLen > g_Opt.m_bDescTextSize)
  323. {
  324. ulBufLen = g_Opt.m_bDescTextSize;
  325. }
  326. if(ulBufLen > 0)
  327. {
  328. char* buf = m_Desc.GetBuffer(ulBufLen);
  329. memcpy(buf, text, ulBufLen); // in most cases, last char == null
  330. buf[ulBufLen-1] = '\0'; // just in case not null terminated
  331. m_Desc.ReleaseBuffer(); // scans for the null
  332. bRet = m_Desc.GetLength() > 0;
  333. }
  334. //Unlock the data
  335. GlobalUnlock(hgData);
  336. return bRet;
  337. }
  338. bool CClip::SetDescFromType()
  339. {
  340. if(m_Formats.GetSize() <= 0)
  341. {
  342. return false;
  343. }
  344. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  345. return m_Desc.GetLength() > 0;
  346. }
  347. bool CClip::AddToDB(bool bCheckForDuplicates)
  348. {
  349. bool bResult;
  350. try
  351. {
  352. if(bCheckForDuplicates)
  353. {
  354. CMainTable recset;
  355. if(FindDuplicate(recset, g_Opt.m_bAllowDuplicates))
  356. {
  357. m_ID = recset.m_lID;
  358. recset.Edit();
  359. recset.m_lDate = (long) m_Time.GetTime(); // update the copy Time
  360. recset.Update();
  361. recset.Close();
  362. EmptyFormats(); // delete this clip's data from memory.
  363. return true;
  364. }
  365. if(recset.IsOpen())
  366. recset.Close();
  367. }
  368. }
  369. CATCHDAO
  370. // AddToDataTable must go first in order to assign m_DataID
  371. bResult = AddToDataTable() && AddToMainTable();
  372. if(bResult)
  373. {
  374. if(g_Opt.m_csPlaySoundOnCopy.GetLength() > 0)
  375. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  376. }
  377. // should be emptied by AddToDataTable
  378. ASSERT(m_Formats.GetSize() == 0);
  379. return bResult;
  380. }
  381. // if a duplicate exists, set recset to the duplicate and return true
  382. bool CClip::FindDuplicate(CMainTable& recset, BOOL bCheckLastOnly)
  383. {
  384. ASSERT(m_lTotalCopySize > 0);
  385. try
  386. {
  387. recset.m_strSort = "lDate DESC";
  388. if(bCheckLastOnly)
  389. {
  390. recset.Open("SELECT * FROM Main");
  391. if(recset.IsEOF())
  392. {
  393. return false;
  394. }
  395. recset.MoveFirst();
  396. // if an entry exists and they are the same size and the format data matches
  397. if(!recset.IsBOF() && !recset.IsEOF() &&
  398. m_lTotalCopySize == recset.m_lTotalCopySize &&
  399. (CompareFormatDataTo(recset.m_lDataID) == 0))
  400. {
  401. return true;
  402. }
  403. return false;
  404. }
  405. // Look for any other entries that have the same size
  406. recset.Open("SELECT * FROM Main WHERE lTotalCopySize = %d", m_lTotalCopySize);
  407. while(!recset.IsEOF())
  408. {
  409. //if there is any then look if it is an exact match
  410. if(CompareFormatDataTo(recset.m_lDataID) == 0)
  411. {
  412. return true;
  413. }
  414. recset.MoveNext();
  415. }
  416. }
  417. CATCHDAO
  418. return false;
  419. }
  420. int CClip::CompareFormatDataTo(long lDataID)
  421. {
  422. int nRet = 0;
  423. int nRecs=0, nFormats=0;
  424. CClipFormat* pFormat = NULL;
  425. try
  426. {
  427. CDataTable recset;
  428. recset.Open("SELECT * FROM Data WHERE lDataID = %d", lDataID);
  429. if( !recset.IsBOF() && !recset.IsEOF() )
  430. {
  431. // Verify the same number of saved types
  432. recset.MoveLast();
  433. nRecs = recset.GetRecordCount();
  434. }
  435. nFormats = m_Formats.GetSize();
  436. nRet = nFormats - nRecs;
  437. if(nRet != 0 || nRecs == 0)
  438. {
  439. recset.Close();
  440. return nRet;
  441. }
  442. // For each format type in the db
  443. for(recset.MoveFirst(); !recset.IsEOF(); recset.MoveNext())
  444. {
  445. pFormat = m_Formats.FindFormat(GetFormatID(recset.m_strClipBoardFormat));
  446. // Verify the format exists
  447. if(!pFormat)
  448. {
  449. recset.Close();
  450. return -1;
  451. }
  452. // Compare the size
  453. nRet = ::GlobalSize(pFormat->m_hgData) - recset.m_ooData.m_dwDataLength;
  454. if( nRet != 0 )
  455. {
  456. recset.Close();
  457. return nRet;
  458. }
  459. // Binary compare
  460. nRet = CompareGlobalHH(recset.m_ooData.m_hData, pFormat->m_hgData, recset.m_ooData.m_dwDataLength);
  461. if(nRet != 0)
  462. {
  463. recset.Close();
  464. return nRet;
  465. }
  466. }
  467. recset.Close();
  468. }
  469. CATCHDAO
  470. return 0;
  471. }
  472. // assigns m_ID
  473. bool CClip::AddToMainTable()
  474. {
  475. try
  476. {
  477. CMainTable recset;
  478. // recset.m_strSort = "lDate DESC";
  479. recset.Open("SELECT * FROM Main");
  480. long lDate = (long) m_Time.GetTime();
  481. recset.AddNew(); // overridden to set m_lID to the new autoincr number
  482. m_ID = recset.m_lID;
  483. recset.m_lDate = lDate;
  484. recset.m_strText = m_Desc;
  485. recset.m_lTotalCopySize = m_lTotalCopySize;
  486. recset.m_bIsGroup = FALSE;
  487. recset.m_lParentID = theApp.m_GroupDefaultID;
  488. VERIFY(m_DataID > 0); // AddToDataTable must be called first to assign this
  489. recset.m_lDataID = m_DataID;
  490. recset.Update();
  491. // recset.SetBookmark( recset.GetLastModifiedBookmark() );
  492. // m_ID = recset.m_lID;
  493. recset.Close();
  494. }
  495. catch(CDaoException* e)
  496. {
  497. ASSERT(FALSE);
  498. e->Delete();
  499. return false;
  500. }
  501. return true;
  502. }
  503. // Empties m_Formats as it saves them to the Data Table.
  504. bool CClip::AddToDataTable()
  505. {
  506. VERIFY( m_DataID <= 0 ); // this func will assign m_DataID
  507. try
  508. {
  509. CClipFormat* pCF;
  510. CDataTable recset;
  511. recset.Open(dbOpenTable,"Data");
  512. for(int i = m_Formats.GetSize()-1; i >= 0 ; i--)
  513. {
  514. pCF = & m_Formats.ElementAt(i);
  515. recset.AddNew(); // overridden to assign new autoincr ID to m_lID
  516. if( m_DataID <= 0 )
  517. {
  518. VERIFY( recset.m_lID > 0 );
  519. m_DataID = recset.m_lID;
  520. }
  521. recset.m_lDataID = m_DataID;
  522. recset.m_strClipBoardFormat = GetFormatName(pCF->m_cfType);
  523. // the recset takes ownership of the HGLOBAL
  524. recset.ReplaceData(pCF->m_hgData, GlobalSize(pCF->m_hgData));
  525. recset.Update();
  526. m_Formats.RemoveAt(i); // the recset now owns the global
  527. }
  528. recset.Close();
  529. return true;
  530. }
  531. CATCHDAO
  532. return false;
  533. }
  534. // changes m_Time to be later than the latest clip entry in the db
  535. // ensures that pClip's time is not older than the last clip added
  536. // old times can happen on fast copies (<1 sec).
  537. void CClip::MakeLatestTime()
  538. {
  539. long lDate;
  540. try
  541. {
  542. CMainTable recset;
  543. recset.m_strSort = "lDate DESC";
  544. recset.Open("SELECT * FROM Main");
  545. if(!recset.IsEOF())
  546. {
  547. recset.MoveFirst();
  548. lDate = (long) m_Time.GetTime();
  549. if( lDate <= recset.m_lDate )
  550. {
  551. lDate = recset.m_lDate + 1;
  552. m_Time = lDate;
  553. }
  554. }
  555. recset.Close();
  556. }
  557. CATCHDAO
  558. }
  559. // STATICS
  560. // Allocates a Global containing the requested Clip Format Data
  561. HGLOBAL CClip::LoadFormat(long lID, UINT cfType)
  562. {
  563. HGLOBAL hGlobal = 0;
  564. try
  565. {
  566. CDataTable recset;
  567. CString csSQL;
  568. csSQL.Format(
  569. "SELECT Data.* FROM Data "
  570. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  571. "WHERE Main.lID = %d "
  572. "AND Data.strClipBoardFormat = \'%s\'",
  573. lID,
  574. GetFormatName(cfType));
  575. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  576. if( !recset.IsBOF() && !recset.IsEOF() )
  577. {
  578. // create a new HGLOBAL duplicate
  579. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  580. // XOR take the recset's HGLOBAL... is this SAFE??
  581. // hGlobal = recset.TakeData();
  582. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  583. {
  584. TRACE0( GetErrorString(::GetLastError()) );
  585. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  586. ASSERT(FALSE);
  587. }
  588. }
  589. recset.Close();
  590. }
  591. CATCHDAO
  592. return hGlobal;
  593. }
  594. bool CClip::LoadFormats(long lID, CClipFormats& formats, bool bOnlyLoad_CF_TEXT)
  595. {
  596. CClipFormat cf;
  597. HGLOBAL hGlobal = 0;
  598. formats.RemoveAll();
  599. try
  600. {
  601. CDataTable recset;
  602. //Open the data table for all that have the parent id
  603. CString csSQL;
  604. csSQL.Format(
  605. "SELECT Data.* FROM Data "
  606. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  607. "WHERE Main.lID = %d", lID);
  608. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  609. while( !recset.IsEOF() )
  610. {
  611. cf.m_cfType = GetFormatID( recset.m_strClipBoardFormat );
  612. if(bOnlyLoad_CF_TEXT)
  613. {
  614. if(cf.m_cfType != CF_TEXT)
  615. {
  616. recset.MoveNext();
  617. continue;
  618. }
  619. }
  620. // create a new HGLOBAL duplicate
  621. hGlobal = NewGlobalH( recset.m_ooData.m_hData, recset.m_ooData.m_dwDataLength );
  622. // XOR take the recset's HGLOBAL... is this SAFE??
  623. // hGlobal = recset.TakeData();
  624. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  625. {
  626. TRACE0( GetErrorString(::GetLastError()) );
  627. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  628. ASSERT(FALSE);
  629. }
  630. cf.m_hgData = hGlobal;
  631. formats.Add( cf );
  632. recset.MoveNext();
  633. }
  634. cf.m_hgData = 0; // formats owns all the data
  635. recset.Close();
  636. }
  637. CATCHDAO
  638. return formats.GetSize() > 0;
  639. }
  640. void CClip::LoadTypes(long lID, CClipTypes& types)
  641. {
  642. types.RemoveAll();
  643. try
  644. {
  645. CDataTable recset;
  646. CString csSQL;
  647. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  648. csSQL.Format(
  649. "SELECT Data.* FROM Data "
  650. "INNER JOIN Main ON Main.lDataID = Data.lDataID "
  651. "WHERE Main.lID = %d", lID);
  652. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  653. while(!recset.IsEOF())
  654. {
  655. types.Add(GetFormatID( recset.m_strClipBoardFormat));
  656. recset.MoveNext();
  657. }
  658. recset.Close();
  659. }
  660. CATCHDAO
  661. }
  662. //void CClip::PrependDateToFormat(CClipFormat &cf)
  663. //{
  664. // char *cData = (char*)GlobalLock(cf.m_hgData);
  665. // if(cData)
  666. // {
  667. // CString csText(cData);
  668. //
  669. // GlobalUnlock(cf.m_hgData);
  670. //
  671. // COleDateTime Date = COleDateTime::GetCurrentTime();
  672. //
  673. // CString csDate;
  674. // CString csYear;
  675. //
  676. // csYear.Format("%d", Date.GetYear());
  677. // csYear = csYear.Mid(2);
  678. //
  679. // long lInsertPos = 0;
  680. // if(cf.m_cfType == theApp.m_RTF_Format)
  681. // lInsertPos = 6;
  682. //
  683. // csDate.Format("%d%s%s ", Date.GetDay(), GetMonthAbb(Date.GetMonth()), csYear);
  684. // csText.Insert(lInsertPos, csDate);
  685. //
  686. // char *cData = csText.GetBuffer(csText.GetLength());
  687. //
  688. // //free the old data
  689. // cf.Free();
  690. //
  691. // cf.m_hgData = NewGlobalP(cData, csText.GetLength());
  692. // csText.ReleaseBuffer();
  693. //
  694. // if(cf.m_cfType == CF_TEXT)
  695. // {
  696. // m_Desc.Insert(0, csDate);
  697. // }
  698. // }
  699. //}
  700. /*----------------------------------------------------------------------------*\
  701. CClipList
  702. \*----------------------------------------------------------------------------*/
  703. CClipList::~CClipList()
  704. {
  705. CClip* pClip;
  706. while(GetCount())
  707. {
  708. pClip = RemoveHead();
  709. DELETE_PTR( pClip );
  710. }
  711. }
  712. // returns the number of clips actually saved
  713. // while this does empty the Format Data, it does not delete the Clips.
  714. int CClipList::AddToDB(bool bLatestTime, bool bShowStatus)
  715. {
  716. int savedCount = 0;
  717. int nRemaining = 0;
  718. CClip* pClip;
  719. POSITION pos;
  720. bool bResult;
  721. nRemaining = GetCount();
  722. pos = GetHeadPosition();
  723. while(pos)
  724. {
  725. if(bShowStatus)
  726. {
  727. theApp.SetStatus(StrF("%d",nRemaining), true);
  728. nRemaining--;
  729. }
  730. pClip = GetNext(pos);
  731. ASSERT(pClip);
  732. if(bLatestTime)
  733. pClip->MakeLatestTime();
  734. bResult = pClip->AddToDB();
  735. if( bResult )
  736. savedCount++;
  737. }
  738. if(bShowStatus)
  739. theApp.SetStatus(NULL, true);
  740. return savedCount;
  741. }
  742. const CClipList& CClipList::operator=(const CClipList &cliplist)
  743. {
  744. POSITION pos;
  745. CClip* pClip;
  746. pos = cliplist.GetHeadPosition();
  747. while(pos)
  748. {
  749. pClip = cliplist.GetNext(pos);
  750. ASSERT(pClip);
  751. CClip *pNewClip = new CClip;
  752. if(pNewClip)
  753. {
  754. *pNewClip = *pClip;
  755. AddTail(pNewClip);
  756. }
  757. }
  758. return *this;
  759. }