Clip.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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 "Crc32Dynamic.h"
  9. #include "sqlite\CppSQLite3.h"
  10. #include "shared/TextConvert.h"
  11. #include "zlib/zlib.h"
  12. #include "Misc.h"
  13. #include "Md5.h"
  14. #include "ChaiScriptOnCopy.h"
  15. #include "DittoChaiScript.h"
  16. #include <Mmsystem.h>
  17. #include "Path.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. /*----------------------------------------------------------------------------*\
  24. COleDataObjectEx
  25. \*----------------------------------------------------------------------------*/
  26. HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
  27. {
  28. HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
  29. if(hGlobal)
  30. {
  31. if(!::IsValid(hGlobal))
  32. {
  33. Log( StrF(
  34. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  35. GetFormatName(cfFormat) ) );
  36. ::GlobalFree( hGlobal );
  37. hGlobal = NULL;
  38. }
  39. return hGlobal;
  40. }
  41. // The data isn't in global memory, so try getting an IStream interface to it.
  42. STGMEDIUM stg;
  43. if(!GetData(cfFormat, &stg))
  44. {
  45. return 0;
  46. }
  47. switch(stg.tymed)
  48. {
  49. case TYMED_HGLOBAL:
  50. hGlobal = stg.hGlobal;
  51. break;
  52. case TYMED_ISTREAM:
  53. {
  54. UINT uDataSize;
  55. LARGE_INTEGER li;
  56. ULARGE_INTEGER uli;
  57. li.HighPart = li.LowPart = 0;
  58. if ( SUCCEEDED( stg.pstm->Seek ( li, STREAM_SEEK_END, &uli )))
  59. {
  60. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, uli.LowPart );
  61. void* pv = GlobalLock(hGlobal);
  62. stg.pstm->Seek(li, STREAM_SEEK_SET, NULL);
  63. HRESULT result = stg.pstm->Read(pv, uli.LowPart, (PULONG)&uDataSize);
  64. GlobalUnlock(hGlobal);
  65. if( FAILED(result) )
  66. hGlobal = GlobalFree(hGlobal);
  67. }
  68. break; // case TYMED_ISTREAM
  69. }
  70. } // end switch
  71. ReleaseStgMedium(&stg);
  72. if(hGlobal && !::IsValid(hGlobal))
  73. {
  74. Log( StrF(
  75. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  76. GetFormatName(cfFormat)));
  77. ::GlobalFree(hGlobal);
  78. hGlobal = NULL;
  79. }
  80. return hGlobal;
  81. }
  82. /*----------------------------------------------------------------------------*\
  83. CClipFormat - holds the data of one clip format.
  84. \*----------------------------------------------------------------------------*/
  85. CClipFormat::CClipFormat(CLIPFORMAT cfType, HGLOBAL hgData, int parentId)
  86. {
  87. m_cfType = cfType;
  88. m_hgData = hgData;
  89. m_autoDeleteData = true;
  90. m_parentId = parentId;
  91. }
  92. CClipFormat::~CClipFormat()
  93. {
  94. Free();
  95. }
  96. void CClipFormat::Clear()
  97. {
  98. m_cfType = 0;
  99. m_hgData = 0;
  100. m_dataId = -1;
  101. m_parentId = -1;
  102. }
  103. void CClipFormat::Free()
  104. {
  105. if(m_autoDeleteData)
  106. {
  107. if(m_hgData)
  108. {
  109. m_hgData = ::GlobalFree( m_hgData );
  110. m_hgData = NULL;
  111. }
  112. }
  113. }
  114. /*----------------------------------------------------------------------------*\
  115. CClipFormats - holds an array of CClipFormat
  116. \*----------------------------------------------------------------------------*/
  117. // returns a pointer to the CClipFormat in this array which matches the given type
  118. // or NULL if that type doesn't exist in this array.
  119. CClipFormat* CClipFormats::FindFormat(UINT cfType)
  120. {
  121. CClipFormat* pCF;
  122. INT_PTR count = GetSize();
  123. for(int i=0; i < count; i++)
  124. {
  125. pCF = &ElementAt(i);
  126. if(pCF->m_cfType == cfType)
  127. return pCF;
  128. }
  129. return NULL;
  130. }
  131. /*----------------------------------------------------------------------------*\
  132. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  133. \*----------------------------------------------------------------------------*/
  134. DWORD CClip::m_LastAddedCRC = 0;
  135. int CClip::m_lastAddedID = -1;
  136. CClip::CClip() :
  137. m_id(0),
  138. m_CRC(0),
  139. m_parentId(-1),
  140. m_dontAutoDelete(FALSE),
  141. m_shortCut(0),
  142. m_bIsGroup(FALSE),
  143. m_param1(0),
  144. m_clipOrder(0),
  145. m_stickyClipOrder(INVALID_STICKY),
  146. m_stickyClipGroupOrder(INVALID_STICKY),
  147. m_clipGroupOrder(0),
  148. m_globalShortCut(FALSE),
  149. m_moveToGroupShortCut(0),
  150. m_globalMoveToGroupShortCut(FALSE)
  151. {
  152. m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
  153. }
  154. CClip::~CClip()
  155. {
  156. EmptyFormats();
  157. }
  158. void CClip::Clear()
  159. {
  160. m_id = -1;
  161. m_Time = 0;
  162. m_Desc = "";
  163. m_CRC = 0;
  164. m_parentId = -1;
  165. m_dontAutoDelete = FALSE;
  166. m_shortCut = 0;
  167. m_bIsGroup = FALSE;
  168. m_csQuickPaste = "";
  169. m_param1 = 0;
  170. m_globalShortCut = FALSE;
  171. m_moveToGroupShortCut = 0;
  172. m_globalMoveToGroupShortCut = 0;
  173. EmptyFormats();
  174. }
  175. const CClip& CClip::operator=(const CClip &clip)
  176. {
  177. const CClipFormat* pCF;
  178. m_id = clip.m_id;
  179. m_Time = clip.m_Time;
  180. m_lastPasteDate = clip.m_lastPasteDate;
  181. m_CRC = clip.m_CRC;
  182. m_parentId = clip.m_parentId;
  183. m_dontAutoDelete = clip.m_dontAutoDelete;
  184. m_shortCut = clip.m_shortCut;
  185. m_bIsGroup = clip.m_bIsGroup;
  186. m_csQuickPaste = clip.m_csQuickPaste;
  187. m_moveToGroupShortCut = clip.m_moveToGroupShortCut;
  188. m_globalMoveToGroupShortCut = clip.m_globalMoveToGroupShortCut;
  189. INT_PTR nCount = clip.m_Formats.GetSize();
  190. for(int i = 0; i < nCount; i++)
  191. {
  192. pCF = &clip.m_Formats.GetData()[i];
  193. LPVOID pvData = GlobalLock(pCF->m_hgData);
  194. if(pvData)
  195. {
  196. AddFormat(pCF->m_cfType, pvData, (UINT)GlobalSize(pCF->m_hgData));
  197. }
  198. GlobalUnlock(pCF->m_hgData);
  199. }
  200. //Set this after since in could get the wrong description in AddFormat
  201. m_Desc = clip.m_Desc;
  202. return *this;
  203. }
  204. void CClip::EmptyFormats()
  205. {
  206. // free global memory in m_Formats
  207. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0; i--)
  208. {
  209. m_Formats[i].Free();
  210. m_Formats.RemoveAt(i);
  211. }
  212. }
  213. // Adds a new Format to this Clip by copying the given data.
  214. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen, bool setDesc)
  215. {
  216. ASSERT(pData && nLen);
  217. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  218. ASSERT(hGlobal);
  219. // update the Clip statistics
  220. m_Time = m_Time.GetCurrentTime();
  221. if (setDesc)
  222. {
  223. if (cfType != CF_UNICODETEXT || !SetDescFromText(hGlobal, true))
  224. SetDescFromType();
  225. }
  226. CClipFormat format(cfType,hGlobal);
  227. CClipFormat *pFormat;
  228. pFormat = m_Formats.FindFormat(cfType);
  229. // if the format type already exists as part of this clip, replace the data
  230. if(pFormat)
  231. {
  232. pFormat->Free();
  233. pFormat->m_hgData = format.m_hgData;
  234. }
  235. else
  236. {
  237. m_Formats.Add(format);
  238. }
  239. format.m_hgData = 0; // now owned by m_Formats
  240. return true;
  241. }
  242. // Fills this CClip with the contents of the clipboard.
  243. int CClip::LoadFromClipboard(CClipTypes* pClipTypes, bool checkClipboardIgnore, CString activeApp)
  244. {
  245. COleDataObjectEx oleData;
  246. CClipTypes defaultTypes;
  247. CClipTypes* pTypes = pClipTypes;
  248. // m_Formats should be empty when this is called.
  249. ASSERT(m_Formats.GetSize() == 0);
  250. // If the data is supposed to be private, then return
  251. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  252. {
  253. Log(_T("Clipboard ignore type is on the clipboard, skipping this clipboard change"));
  254. return FALSE;
  255. }
  256. //If we are saving a multi paste then delay us connecting to the clipboard
  257. //to allow the ctrl-v to do a paste
  258. if(::IsClipboardFormatAvailable(theApp.m_cfDelaySavingData))
  259. {
  260. Log(_T("Delay clipboard type is on the clipboard, delaying 1500 ms to allow ctrl-v to work"));
  261. Sleep(1500);
  262. }
  263. //Attach to the clipboard
  264. if(!oleData.AttachClipboard())
  265. {
  266. Log(_T("failed to attache to clipboard, skipping this clipboard change"));
  267. ASSERT(0); // does this ever happen?
  268. return FALSE;
  269. }
  270. oleData.EnsureClipboardObject();
  271. // if no types were given, get only the first (most important) type.
  272. // (subsequent types could be synthetic due to automatic type conversions)
  273. if(pTypes == NULL || pTypes->GetSize() == 0)
  274. {
  275. ASSERT(0); // this feature is not currently used... it is an error if it is.
  276. FORMATETC formatEtc;
  277. oleData.BeginEnumFormats();
  278. oleData.GetNextFormat(&formatEtc);
  279. defaultTypes.Add(formatEtc.cfFormat);
  280. pTypes = &defaultTypes;
  281. }
  282. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  283. // Get Description String
  284. // NOTE: We make sure that the description always corresponds to the
  285. // data saved by using the exact same globalmem instance as the source
  286. // for both... i.e. we only fetch the description format type once.
  287. CClipFormat cfDesc;
  288. bool bIsDescSet = false;
  289. cfDesc.m_cfType = CF_UNICODETEXT;
  290. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  291. {
  292. for (int i = 0; i < 10; i++)
  293. {
  294. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  295. if (cfDesc.m_hgData == NULL)
  296. {
  297. Log(StrF(_T("Tried to set description from cf_unicode, data is NULL, try: %d"), i+1));
  298. }
  299. else
  300. {
  301. break;
  302. }
  303. Sleep(10);
  304. }
  305. bIsDescSet = SetDescFromText(cfDesc.m_hgData, true);
  306. if (activeApp != _T(""))
  307. {
  308. TCHAR* text = (TCHAR *)GlobalLock(cfDesc.m_hgData);
  309. if (text != NULL)
  310. {
  311. std::wstring stringData(text);
  312. GlobalUnlock(cfDesc.m_hgData);
  313. if (g_Opt.m_regexHelper.TextMatchFilters(activeApp, stringData))
  314. {
  315. return -1;
  316. }
  317. }
  318. else
  319. {
  320. GlobalUnlock(cfDesc.m_hgData);
  321. }
  322. }
  323. Log(StrF(_T("Tried to set description from cf_unicode text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  324. }
  325. if(bIsDescSet == false)
  326. {
  327. cfDesc.m_cfType = CF_TEXT;
  328. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  329. {
  330. for (int i = 0; i < 10; i++)
  331. {
  332. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  333. if (cfDesc.m_hgData == NULL)
  334. {
  335. Log(StrF(_T("Tried to set description from cf_text, data is NULL, try: %d"), i + 1));
  336. }
  337. else
  338. {
  339. break;
  340. }
  341. Sleep(10);
  342. }
  343. bIsDescSet = SetDescFromText(cfDesc.m_hgData, false);
  344. Log(StrF(_T("Tried to set description from cf_text text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  345. }
  346. }
  347. INT_PTR nSize;
  348. CClipFormat cf;
  349. INT_PTR numTypes = pTypes->GetSize();
  350. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  351. for(int i = 0; i < numTypes; i++)
  352. {
  353. cf.m_cfType = pTypes->ElementAt(i);
  354. if (cf.m_cfType == CF_DIB &&
  355. g_Opt.m_excludeCF_DIBInExcel &&
  356. activeApp.MakeLower() == _T("excel.exe"))
  357. {
  358. continue;
  359. }
  360. BOOL bSuccess = false;
  361. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  362. // is this the description we already fetched?
  363. if(cf.m_cfType == cfDesc.m_cfType)
  364. {
  365. cf = cfDesc;
  366. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  367. }
  368. else if(!oleData.IsDataAvailable(cf.m_cfType))
  369. {
  370. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  371. continue;
  372. }
  373. else
  374. {
  375. for (int i = 0; i < 2; i++)
  376. {
  377. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  378. if (cf.m_hgData != NULL)
  379. {
  380. break;
  381. }
  382. Log(StrF(_T("Tried to get data for type: %s, data is NULL, try: %d"), GetFormatName(cf.m_cfType), i + 1));
  383. Sleep(5);
  384. }
  385. }
  386. if(cf.m_hgData)
  387. {
  388. nSize = GlobalSize(cf.m_hgData);
  389. if(nSize > 0)
  390. {
  391. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  392. {
  393. CString cs;
  394. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  395. Log(cs);
  396. oleData.Release();
  397. return -1;
  398. }
  399. ASSERT(::IsValid(cf.m_hgData));
  400. m_Formats.Add(cf);
  401. bSuccess = true;
  402. }
  403. else
  404. {
  405. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  406. cf.Free();
  407. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  408. }
  409. cf.m_hgData = 0; // m_Formats owns it now
  410. }
  411. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  412. }
  413. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  414. m_Time = CTime::GetCurrentTime();
  415. if(!bIsDescSet)
  416. {
  417. SetDescFromType();
  418. Log(StrF(_T("Setting description from type, Desc: [%s]"), m_Desc.Left(30)));
  419. }
  420. // if the description was in a type that is not supported,
  421. //we have to free it since it wasn't added to m_Formats
  422. if(cfDesc.m_hgData)
  423. {
  424. cfDesc.Free();
  425. }
  426. oleData.Release();
  427. if (!bIsDescSet &&
  428. this->m_Desc != _T(""))
  429. {
  430. std::wstring stringData(this->m_Desc);
  431. if (g_Opt.m_regexHelper.TextMatchFilters(activeApp, stringData))
  432. {
  433. return -1;
  434. }
  435. }
  436. if(m_Formats.GetSize() == 0)
  437. {
  438. Log(_T("No clip types were in supported types array"));
  439. return FALSE;
  440. }
  441. try
  442. {
  443. for (auto & listItem : g_Opt.m_copyScripts.m_list)
  444. {
  445. if (listItem.m_active)
  446. {
  447. Log(StrF(_T("Start of process copy name: %s, script: %s"), listItem.m_name, listItem.m_script));
  448. ChaiScriptOnCopy onCopy;
  449. CDittoChaiScript clipData(this, (LPCSTR)CTextConvert::ConvertToChar(activeApp));
  450. if (onCopy.ProcessScript(clipData, (LPCSTR)CTextConvert::ConvertToChar(listItem.m_script)) == false)
  451. {
  452. Log(StrF(_T("End of process copy name: %s, returned false, not saving this copy to Ditto, last Error: %s"), listItem.m_name, onCopy.m_lastError));
  453. return -1;
  454. }
  455. Log(StrF(_T("End of process copy name: %s, returned true, last Error: %s"), listItem.m_name, onCopy.m_lastError));
  456. }
  457. else
  458. {
  459. Log(StrF(_T("Script is not active, not processing name: %s, script: %s"), listItem.m_name, listItem.m_script));
  460. }
  461. }
  462. }
  463. catch (CException *ex)
  464. {
  465. TCHAR szCause[255];
  466. ex->GetErrorMessage(szCause, 255);
  467. CString cs;
  468. cs.Format(_T("save copy exception: %s"), szCause);
  469. Log(cs);
  470. }
  471. catch (...)
  472. {
  473. Log(_T("save copy exception 2"));
  474. }
  475. return TRUE;
  476. }
  477. bool CClip::SetDescFromText(HGLOBAL hgData, bool unicode)
  478. {
  479. if(hgData == 0)
  480. return false;
  481. bool bRet = false;
  482. INT_PTR bufLen = 0;
  483. if(unicode)
  484. {
  485. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  486. bufLen = GlobalSize(hgData);
  487. m_Desc = text;
  488. bRet = true;
  489. }
  490. else
  491. {
  492. char* text = (char *) GlobalLock(hgData);
  493. bufLen = GlobalSize(hgData);
  494. m_Desc = text;
  495. bRet = true;
  496. }
  497. if(bufLen > g_Opt.m_bDescTextSize)
  498. {
  499. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  500. }
  501. //Unlock the data
  502. GlobalUnlock(hgData);
  503. return bRet;
  504. }
  505. bool CClip::SetDescFromType()
  506. {
  507. INT_PTR size = m_Formats.GetSize();
  508. if(size <= 0)
  509. {
  510. return false;
  511. }
  512. int nCF_HDROPIndex = -1;
  513. for(int i = 0; i < size; i++)
  514. {
  515. if(m_Formats[i].m_cfType == CF_HDROP)
  516. {
  517. nCF_HDROPIndex = i;
  518. }
  519. }
  520. if(nCF_HDROPIndex >= 0)
  521. {
  522. using namespace nsPath;
  523. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  524. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  525. if(nNumFiles > 1)
  526. m_Desc = "Copied Files - ";
  527. else
  528. m_Desc = "Copied File - ";
  529. TCHAR file[MAX_PATH];
  530. for(int nFile = 0; nFile < nNumFiles; nFile++)
  531. {
  532. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  533. {
  534. CPath path(file);
  535. m_Desc += path.GetName();
  536. m_Desc += " - ";
  537. m_Desc += file;
  538. m_Desc += "\n";
  539. }
  540. }
  541. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  542. }
  543. else
  544. {
  545. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  546. }
  547. return m_Desc.GetLength() > 0;
  548. }
  549. bool CClip::AddToDB(bool bCheckForDuplicates)
  550. {
  551. bool bResult;
  552. try
  553. {
  554. m_CRC = GenerateCRC();
  555. if(bCheckForDuplicates &&
  556. m_parentId < 0)
  557. {
  558. int nID = FindDuplicate();
  559. if(nID >= 0)
  560. {
  561. MakeLatestOrder();
  562. MakeLatestGroupOrder();
  563. CString sql;
  564. sql.Format(_T("UPDATE Main SET clipOrder = %f where lID = %d;"),
  565. m_clipOrder, nID);
  566. int ret = theApp.m_db.execDML(sql);
  567. int groupRet = -1;
  568. if(m_parentId > -1)
  569. {
  570. sql.Format(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"),
  571. m_clipGroupOrder, nID);
  572. groupRet = theApp.m_db.execDML(sql);
  573. }
  574. m_id = nID;
  575. Log(StrF(_T("Found duplicate clip in db, Id: %d, ParentId: %d crc: %d, NewOrder: %f, GroupOrder %f, Ret: %d, GroupRet: %d, SQL: %s"),
  576. nID, m_parentId, m_CRC, m_clipOrder, m_clipGroupOrder, ret, groupRet, sql));
  577. return true;
  578. }
  579. }
  580. }
  581. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  582. bResult = false;
  583. if(AddToMainTable())
  584. {
  585. bResult = AddToDataTable();
  586. }
  587. if(bResult)
  588. {
  589. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  590. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  591. }
  592. // should be emptied by AddToDataTable
  593. //ASSERT(m_Formats.GetSize() == 0);
  594. return bResult;
  595. }
  596. // if a duplicate exists, set recset to the duplicate and return true
  597. int CClip::FindDuplicate()
  598. {
  599. try
  600. {
  601. //If they are allowing duplicates still check
  602. //the last copied item
  603. if(g_Opt.m_bAllowDuplicates)
  604. {
  605. if(m_CRC == m_LastAddedCRC)
  606. return m_lastAddedID;
  607. }
  608. else
  609. {
  610. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  611. if(q.eof() == false)
  612. {
  613. return q.getIntField(_T("lID"));
  614. }
  615. }
  616. }
  617. CATCH_SQLITE_EXCEPTION
  618. return -1;
  619. }
  620. DWORD CClip::GenerateCRC()
  621. {
  622. CClipFormat* pCF;
  623. DWORD dwCRC = 0xFFFFFFFF;
  624. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  625. if(pCrc32)
  626. {
  627. //Generate a CRC value for all copied data
  628. INT_PTR size = m_Formats.GetSize();
  629. for(int i = 0; i < size ; i++)
  630. {
  631. pCF = & m_Formats.ElementAt(i);
  632. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  633. if(Data)
  634. {
  635. if (CGetSetOptions::GetAdjustClipsForCRC())
  636. {
  637. //Try and remove known things that change in rtf (word and outlook)
  638. if (pCF->m_cfType == theApp.m_RTFFormat)
  639. {
  640. CStringA CStringData((char*)Data);
  641. //In word and outlook I was finding that data in the \\datastore section was always changing, remove this for the crc check
  642. RemoveRTFSection(CStringData, "{\\*\\datastore");
  643. //In word and outlook rsid values are always changing, remove these for the crc check
  644. DeleteParamFromRTF(CStringData, "\\rsid", true);
  645. DeleteParamFromRTF(CStringData, "\\insrsid", true);
  646. DeleteParamFromRTF(CStringData, "\\mdispDef1", false);
  647. pCrc32->GenerateCrc32((const LPBYTE)CStringData.GetBuffer(), (DWORD)CStringData.GetLength(), dwCRC);
  648. }
  649. else
  650. {
  651. //i've seen examble where the text size was 10 but the data size was 20, leading to random crc values
  652. //try and only check the crc for the actual text
  653. int dataLength = GlobalSize(pCF->m_hgData);
  654. if (pCF->m_cfType == CF_TEXT)
  655. {
  656. dataLength = min(dataLength, (strlen((char*)Data) + 1));
  657. }
  658. else if (pCF->m_cfType == CF_UNICODETEXT)
  659. {
  660. dataLength = min(dataLength, ((wcslen((wchar_t*)Data) + 1) * 2));
  661. }
  662. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)dataLength, dwCRC);
  663. }
  664. }
  665. else
  666. {
  667. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  668. }
  669. }
  670. GlobalUnlock(pCF->m_hgData);
  671. }
  672. dwCRC = ~dwCRC;
  673. delete pCrc32;
  674. }
  675. return dwCRC;
  676. }
  677. // assigns m_ID
  678. bool CClip::AddToMainTable()
  679. {
  680. try
  681. {
  682. m_Desc.Replace(_T("'"), _T("''"));
  683. m_csQuickPaste.Replace(_T("'"), _T("''"));
  684. CString cs;
  685. cs.Format(_T("INSERT into Main (lDate, mText, lShortCut, lDontAutoDelete, CRC, bIsGroup, lParentID, QuickPasteText, clipOrder, clipGroupOrder, globalShortCut, lastPasteDate, stickyClipOrder, stickyClipGroupOrder, MoveToGroupShortCut, GlobalMoveToGroupShortCut) ")
  686. _T("values(%d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d, %f, %f, %d, %d);"),
  687. (int)m_Time.GetTime(),
  688. m_Desc,
  689. m_shortCut,
  690. m_dontAutoDelete,
  691. m_CRC,
  692. m_bIsGroup,
  693. m_parentId,
  694. m_csQuickPaste,
  695. m_clipOrder,
  696. m_clipGroupOrder,
  697. m_globalShortCut,
  698. (int)CTime::GetCurrentTime().GetTime(),
  699. m_stickyClipOrder,
  700. m_stickyClipGroupOrder,
  701. m_moveToGroupShortCut,
  702. m_globalMoveToGroupShortCut);
  703. theApp.m_db.execDML(cs);
  704. m_id = (long)theApp.m_db.lastRowId();
  705. Log(StrF(_T("Added clip to main table, Id: %d, ParentId: %d Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_parentId, m_Desc, m_clipOrder, m_clipGroupOrder));
  706. m_LastAddedCRC = m_CRC;
  707. m_lastAddedID = m_id;
  708. }
  709. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  710. return true;
  711. }
  712. bool CClip::ModifyMainTable()
  713. {
  714. bool bRet = false;
  715. try
  716. {
  717. m_Desc.Replace(_T("'"), _T("''"));
  718. m_csQuickPaste.Replace(_T("'"), _T("''"));
  719. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  720. _T("mText = '%s', ")
  721. _T("lParentID = %d, ")
  722. _T("lDontAutoDelete = %d, ")
  723. _T("QuickPasteText = '%s', ")
  724. _T("clipOrder = %f, ")
  725. _T("clipGroupOrder = %f, ")
  726. _T("globalShortCut = %d, ")
  727. _T("stickyClipOrder = %f, ")
  728. _T("stickyClipGroupOrder = %f, ")
  729. _T("MoveToGroupShortCut = %d, ")
  730. _T("GlobalMoveToGroupShortCut = %d ")
  731. _T("WHERE lID = %d;"),
  732. m_shortCut,
  733. m_Desc,
  734. m_parentId,
  735. m_dontAutoDelete,
  736. m_csQuickPaste,
  737. m_clipOrder,
  738. m_clipGroupOrder,
  739. m_globalShortCut,
  740. m_stickyClipOrder,
  741. m_stickyClipGroupOrder,
  742. m_moveToGroupShortCut,
  743. m_globalMoveToGroupShortCut,
  744. m_id);
  745. bRet = true;
  746. }
  747. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  748. return bRet;
  749. }
  750. bool CClip::ModifyDescription()
  751. {
  752. bool bRet = false;
  753. try
  754. {
  755. m_Desc.Replace(_T("'"), _T("''"));
  756. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' ")
  757. _T("WHERE lID = %d;"),
  758. m_Desc,
  759. m_id);
  760. bRet = true;
  761. }
  762. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  763. return bRet;
  764. }
  765. // Empties m_Formats as it saves them to the Data Table.
  766. bool CClip::AddToDataTable()
  767. {
  768. CClipFormat* pCF;
  769. try
  770. {
  771. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  772. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  773. {
  774. pCF = &m_Formats.ElementAt(i);
  775. CString formatName = GetFormatName(pCF->m_cfType);
  776. int clipSize = 0;
  777. stmt.bind(1, m_id);
  778. stmt.bind(2, formatName);
  779. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  780. if(Data)
  781. {
  782. clipSize = (int)GlobalSize(pCF->m_hgData);
  783. stmt.bind(3, Data, clipSize);
  784. }
  785. GlobalUnlock(pCF->m_hgData);
  786. stmt.execDML();
  787. stmt.reset();
  788. pCF->m_dataId = (long)theApp.m_db.lastRowId();
  789. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dataId, m_id, formatName, clipSize));
  790. }
  791. }
  792. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  793. return true;
  794. }
  795. void CClip::MoveUp(int parentId)
  796. {
  797. try
  798. {
  799. //In a group, not a sticky
  800. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  801. {
  802. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder == -(2147483647) AND clipGroupOrder > %f ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, m_clipGroupOrder);
  803. if (q.eof() == false)
  804. {
  805. int idAbove = q.getIntField(_T("lID"));
  806. double orderAbove = q.getFloatField(_T("clipGroupOrder"));
  807. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder > %f AND stickyClipGroupOrder == -(2147483647) ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  808. if (q2.eof() == false)
  809. {
  810. int idTwoAbove = q2.getIntField(_T("lID"));
  811. double orderTwoAbove = q2.getFloatField(_T("clipGroupOrder"));
  812. m_clipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  813. }
  814. else
  815. {
  816. m_clipGroupOrder = orderAbove + 1;
  817. }
  818. }
  819. }
  820. // main group, not a sticky
  821. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  822. {
  823. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), m_clipOrder);
  824. if (q.eof() == false)
  825. {
  826. int idAbove = q.getIntField(_T("lID"));
  827. double orderAbove = q.getFloatField(_T("clipOrder"));
  828. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), orderAbove);
  829. if (q2.eof() == false)
  830. {
  831. int idTwoAbove = q2.getIntField(_T("lID"));
  832. double orderTwoAbove = q2.getFloatField(_T("clipOrder"));
  833. m_clipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  834. }
  835. else
  836. {
  837. m_clipOrder = orderAbove + 1;
  838. }
  839. }
  840. }
  841. //In a group, a sticky clip
  842. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  843. {
  844. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  845. if (q.eof() == false)
  846. {
  847. int idAbove = q.getIntField(_T("lID"));
  848. double orderAbove = q.getFloatField(_T("stickyClipGroupOrder"));
  849. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  850. if (q2.eof() == false)
  851. {
  852. int idTwoAbove = q2.getIntField(_T("lID"));
  853. double orderTwoAbove = q2.getFloatField(_T("stickyClipGroupOrder"));
  854. m_stickyClipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  855. }
  856. else
  857. {
  858. m_stickyClipGroupOrder = orderAbove + 1;
  859. }
  860. }
  861. }
  862. //not in a group, a sticky clip
  863. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  864. {
  865. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), m_stickyClipOrder);
  866. if (q.eof() == false)
  867. {
  868. int idAbove = q.getIntField(_T("lID"));
  869. double orderAbove = q.getFloatField(_T("stickyClipOrder"));
  870. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), orderAbove);
  871. if (q2.eof() == false)
  872. {
  873. int idTwoAbove = q2.getIntField(_T("lID"));
  874. double orderTwoAbove = q2.getFloatField(_T("stickyClipOrder"));
  875. m_stickyClipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  876. }
  877. else
  878. {
  879. m_stickyClipOrder = orderAbove + 1;
  880. }
  881. }
  882. }
  883. }
  884. CATCH_SQLITE_EXCEPTION
  885. }
  886. void CClip::MoveDown(int parentId)
  887. {
  888. try
  889. {
  890. //In a group, not a sticky
  891. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  892. {
  893. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, m_clipGroupOrder);
  894. if (q.eof() == false)
  895. {
  896. int idBelow = q.getIntField(_T("lID"));
  897. double orderBelow = q.getFloatField(_T("clipGroupOrder"));
  898. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  899. if (q2.eof() == false)
  900. {
  901. int idTwoBelow = q2.getIntField(_T("lID"));
  902. double orderTwoBelow = q2.getFloatField(_T("clipGroupOrder"));
  903. m_clipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  904. }
  905. else
  906. {
  907. m_clipGroupOrder = orderBelow - 1;
  908. }
  909. }
  910. }
  911. // main group, not a sticky
  912. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  913. {
  914. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), m_clipOrder);
  915. if (q.eof() == false)
  916. {
  917. int idBelow = q.getIntField(_T("lID"));
  918. double orderBelow = q.getFloatField(_T("clipOrder"));
  919. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), orderBelow);
  920. if (q2.eof() == false)
  921. {
  922. int idTwoBelow = q2.getIntField(_T("lID"));
  923. double orderTwoBelow = q2.getFloatField(_T("clipOrder"));
  924. m_clipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  925. }
  926. else
  927. {
  928. m_clipOrder = orderBelow - 1;
  929. }
  930. }
  931. }
  932. //In a group, a sticky clip
  933. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  934. {
  935. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  936. if (q.eof() == false)
  937. {
  938. int idBelow = q.getIntField(_T("lID"));
  939. double orderBelow = q.getFloatField(_T("stickyClipGroupOrder"));
  940. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  941. if (q2.eof() == false)
  942. {
  943. int idTwoBelow = q2.getIntField(_T("lID"));
  944. double orderTwoBelow = q2.getFloatField(_T("stickyClipGroupOrder"));
  945. m_stickyClipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  946. }
  947. else
  948. {
  949. m_stickyClipGroupOrder = orderBelow - 1;
  950. }
  951. }
  952. }
  953. //not in a group, a sticky clip
  954. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  955. {
  956. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), m_stickyClipOrder);
  957. if (q.eof() == false)
  958. {
  959. int idBelow = q.getIntField(_T("lID"));
  960. double orderBelow = q.getFloatField(_T("stickyClipOrder"));
  961. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), orderBelow);
  962. if (q2.eof() == false)
  963. {
  964. int idTwoBelow = q2.getIntField(_T("lID"));
  965. double orderTwoBelow = q2.getFloatField(_T("stickyClipOrder"));
  966. m_stickyClipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  967. }
  968. else
  969. {
  970. m_stickyClipOrder = orderBelow - 1;
  971. }
  972. }
  973. }
  974. }
  975. CATCH_SQLITE_EXCEPTION
  976. }
  977. void CClip::MakeStickyTop(int parentId)
  978. {
  979. if (parentId < 0)
  980. {
  981. m_stickyClipOrder = GetNewTopSticky(parentId, m_id);
  982. }
  983. else
  984. {
  985. m_stickyClipGroupOrder = GetNewTopSticky(parentId, m_id);
  986. }
  987. }
  988. void CClip::MakeStickyLast(int parentId)
  989. {
  990. if (parentId < 0)
  991. {
  992. m_stickyClipOrder = GetNewLastSticky(parentId, m_id);
  993. }
  994. else
  995. {
  996. m_stickyClipGroupOrder = GetNewLastSticky(parentId, m_id);
  997. }
  998. }
  999. bool CClip::RemoveStickySetting(int parentId)
  1000. {
  1001. bool reset = false;
  1002. if (parentId < 0)
  1003. {
  1004. if (m_stickyClipOrder != INVALID_STICKY)
  1005. {
  1006. m_stickyClipOrder = INVALID_STICKY;
  1007. reset = true;
  1008. }
  1009. }
  1010. else
  1011. {
  1012. if (m_stickyClipGroupOrder != INVALID_STICKY)
  1013. {
  1014. m_stickyClipGroupOrder = INVALID_STICKY;
  1015. reset = true;
  1016. }
  1017. }
  1018. return reset;
  1019. }
  1020. double CClip::GetNewTopSticky(int parentId, int clipId)
  1021. {
  1022. double newOrder = 1;
  1023. double existingMaxOrder = 0;
  1024. CString existingDesc = _T("");
  1025. try
  1026. {
  1027. if (parentId < 0)
  1028. {
  1029. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder DESC LIMIT 1"));
  1030. if (q.eof() == false)
  1031. {
  1032. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  1033. existingDesc = q.getStringField(_T("mText"));
  1034. newOrder = existingMaxOrder + 1;
  1035. }
  1036. }
  1037. else
  1038. {
  1039. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId);
  1040. if (q.eof() == false)
  1041. {
  1042. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1043. newOrder = existingMaxOrder + 1;
  1044. }
  1045. }
  1046. if (newOrder == 0.0)
  1047. newOrder += 1;
  1048. Log(StrF(_T("GetNewTopSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1049. }
  1050. CATCH_SQLITE_EXCEPTION
  1051. return newOrder;
  1052. }
  1053. double CClip::GetNewLastSticky(int parentId, int clipId)
  1054. {
  1055. double newOrder = 1;
  1056. double existingMaxOrder = 0;
  1057. CString existingDesc = _T("");
  1058. try
  1059. {
  1060. if (parentId < 0)
  1061. {
  1062. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder LIMIT 1"));
  1063. if (q.eof() == false)
  1064. {
  1065. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  1066. existingDesc = q.getStringField(_T("mText"));
  1067. newOrder = existingMaxOrder - 1;
  1068. }
  1069. }
  1070. else
  1071. {
  1072. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder LIMIT 1"), parentId);
  1073. if (q.eof() == false)
  1074. {
  1075. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1076. newOrder = existingMaxOrder - 1;
  1077. }
  1078. }
  1079. if (newOrder == 0.0)
  1080. newOrder -= 1;
  1081. Log(StrF(_T("GetNewLastSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1082. }
  1083. CATCH_SQLITE_EXCEPTION
  1084. return newOrder;
  1085. }
  1086. void CClip::MakeLatestOrder()
  1087. {
  1088. m_clipOrder = GetNewOrder(-1, m_id);
  1089. }
  1090. void CClip::MakeLatestGroupOrder()
  1091. {
  1092. if(m_parentId > -1)
  1093. {
  1094. m_clipGroupOrder = GetNewOrder(m_parentId, m_id);
  1095. }
  1096. }
  1097. void CClip::MakeLastOrder()
  1098. {
  1099. m_clipOrder = GetNewLastOrder(-1, m_id);
  1100. }
  1101. void CClip::MakeLastGroupOrder()
  1102. {
  1103. if (m_parentId > -1)
  1104. {
  1105. m_clipGroupOrder = GetNewLastOrder(m_parentId, m_id);
  1106. }
  1107. }
  1108. double CClip::GetNewOrder(int parentId, int clipId)
  1109. {
  1110. double newOrder = 0;
  1111. double existingMaxOrder = 0;
  1112. CString existingDesc = _T("");
  1113. try
  1114. {
  1115. if(parentId < 0)
  1116. {
  1117. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  1118. if(q.eof() == false)
  1119. {
  1120. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  1121. existingDesc = q.getStringField(_T("mText"));
  1122. newOrder = existingMaxOrder + 1;
  1123. }
  1124. }
  1125. else
  1126. {
  1127. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipGroupOrder DESC LIMIT 1"), parentId);
  1128. if(q.eof() == false)
  1129. {
  1130. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  1131. newOrder = existingMaxOrder + 1;
  1132. }
  1133. }
  1134. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1135. }
  1136. CATCH_SQLITE_EXCEPTION
  1137. return newOrder;
  1138. }
  1139. double CClip::GetNewLastOrder(int parentId, int clipId)
  1140. {
  1141. double newOrder = 0;
  1142. double existingMinOrder = 0;
  1143. CString existingDesc = _T("");
  1144. try
  1145. {
  1146. if (parentId < 0)
  1147. {
  1148. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main where clipOrder notnull ORDER BY clipOrder ASC LIMIT 1"));
  1149. if (q.eof() == false)
  1150. {
  1151. existingMinOrder = q.getFloatField(_T("clipOrder"));
  1152. existingDesc = q.getStringField(_T("mText"));
  1153. newOrder = existingMinOrder - 1;
  1154. }
  1155. }
  1156. else
  1157. {
  1158. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d AND clipGroupOrder notnull ORDER BY clipGroupOrder ASC LIMIT 1"), parentId);
  1159. if (q.eof() == false)
  1160. {
  1161. existingMinOrder = q.getFloatField(_T("clipGroupOrder"));
  1162. newOrder = existingMinOrder - 1;
  1163. }
  1164. }
  1165. Log(StrF(_T("GetLastOrder, Id: %d, parentId: %d, CurrentMin: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMinOrder, existingDesc, newOrder));
  1166. }
  1167. CATCH_SQLITE_EXCEPTION
  1168. return newOrder;
  1169. }
  1170. BOOL CClip::LoadMainTable(int id)
  1171. {
  1172. bool bRet = false;
  1173. try
  1174. {
  1175. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  1176. if(q.eof() == false)
  1177. {
  1178. m_Time = q.getIntField(_T("lDate"));
  1179. m_Desc = q.getStringField(_T("mText"));
  1180. m_CRC = q.getIntField(_T("CRC"));
  1181. m_parentId = q.getIntField(_T("lParentID"));
  1182. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  1183. m_shortCut = q.getIntField(_T("lShortCut"));
  1184. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  1185. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  1186. m_clipOrder = q.getFloatField(_T("clipOrder"));
  1187. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  1188. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  1189. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  1190. m_stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  1191. m_stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1192. m_moveToGroupShortCut = q.getIntField(_T("MoveToGroupShortCut"));
  1193. m_globalMoveToGroupShortCut = q.getIntField(_T("GlobalMoveToGroupShortCut"));
  1194. m_id = id;
  1195. bRet = true;
  1196. }
  1197. }
  1198. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  1199. return bRet;
  1200. }
  1201. // STATICS
  1202. // Allocates a Global containing the requested Clip Format Data
  1203. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  1204. {
  1205. HGLOBAL hGlobal = 0;
  1206. try
  1207. {
  1208. CString csSQL;
  1209. csSQL.Format(
  1210. _T("SELECT Data.ooData FROM Data ")
  1211. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1212. _T("WHERE Main.lID = %d ")
  1213. _T("AND Data.strClipBoardFormat = \'%s\'"),
  1214. id,
  1215. GetFormatName(cfType));
  1216. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1217. if(q.eof() == false)
  1218. {
  1219. int nDataLen = 0;
  1220. const unsigned char *cData = q.getBlobField(0, nDataLen);
  1221. if(cData == NULL)
  1222. {
  1223. return false;
  1224. }
  1225. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1226. }
  1227. }
  1228. CATCH_SQLITE_EXCEPTION
  1229. return hGlobal;
  1230. }
  1231. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT, bool includeRichTextForTextOnly)
  1232. {
  1233. DWORD startTick = GetTickCount();
  1234. CClipFormat cf;
  1235. HGLOBAL hGlobal = 0;
  1236. m_Formats.RemoveAll();
  1237. try
  1238. {
  1239. //Open the data table for all that have the parent id
  1240. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1241. //we generated it
  1242. CString csSQL;
  1243. CString textFilter = _T("");
  1244. if(bOnlyLoad_CF_TEXT)
  1245. {
  1246. textFilter = _T("(strClipBoardFormat = 'CF_TEXT' OR strClipBoardFormat = 'CF_UNICODETEXT' OR strClipBoardFormat = 'CF_HDROP'");
  1247. if(includeRichTextForTextOnly)
  1248. {
  1249. textFilter = textFilter + _T(" OR strClipBoardFormat = 'Rich Text Format') AND ");
  1250. }
  1251. else
  1252. {
  1253. textFilter = textFilter + _T(") AND ");
  1254. }
  1255. }
  1256. csSQL.Format(
  1257. _T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data ")
  1258. _T("WHERE %s lParentID = %d ORDER BY Data.lID desc"), textFilter, id);
  1259. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1260. while(q.eof() == false)
  1261. {
  1262. cf.m_dataId = q.getIntField(_T("lID"));
  1263. cf.m_parentId = q.getIntField(_T("lParentID"));
  1264. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  1265. if(bOnlyLoad_CF_TEXT)
  1266. {
  1267. if(cf.m_cfType != CF_TEXT &&
  1268. cf.m_cfType != CF_UNICODETEXT &&
  1269. cf.m_cfType != CF_HDROP &&
  1270. (cf.m_cfType != theApp.m_RTFFormat && !includeRichTextForTextOnly))
  1271. {
  1272. q.nextRow();
  1273. continue;
  1274. }
  1275. }
  1276. int nDataLen = 0;
  1277. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  1278. if(cData != NULL)
  1279. {
  1280. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1281. }
  1282. cf.m_hgData = hGlobal;
  1283. m_Formats.Add(cf);
  1284. q.nextRow();
  1285. }
  1286. // formats owns all the data
  1287. cf.m_hgData = 0;
  1288. }
  1289. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  1290. DWORD endTick = GetTickCount();
  1291. if((endTick-startTick) > 150)
  1292. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  1293. return m_Formats.GetSize() > 0;
  1294. }
  1295. void CClip::LoadTypes(int id, CClipTypes& types)
  1296. {
  1297. types.RemoveAll();
  1298. try
  1299. {
  1300. CString csSQL;
  1301. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  1302. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1303. //we generated it
  1304. csSQL.Format(
  1305. _T("SELECT strClipBoardFormat FROM Data ")
  1306. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1307. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  1308. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1309. while(q.eof() == false)
  1310. {
  1311. types.Add(GetFormatID(q.getStringField(0)));
  1312. q.nextRow();
  1313. }
  1314. }
  1315. CATCH_SQLITE_EXCEPTION
  1316. }
  1317. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  1318. {
  1319. bool bRet = false;
  1320. try
  1321. {
  1322. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  1323. DWORD CRC = GenerateCRC();
  1324. AddToDataTable();
  1325. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  1326. if(bUpdateDesc)
  1327. {
  1328. m_Desc.Replace(_T("'"), _T("''"));
  1329. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  1330. }
  1331. bRet = true;
  1332. }
  1333. CATCH_SQLITE_EXCEPTION
  1334. return bRet;
  1335. }
  1336. CStringW CClip::GetUnicodeTextFormat()
  1337. {
  1338. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_UNICODETEXT);
  1339. if(pFormat != NULL)
  1340. {
  1341. wchar_t *stringData = (wchar_t *)GlobalLock(pFormat->Data());
  1342. if(stringData != NULL)
  1343. {
  1344. CStringW string(stringData);
  1345. GlobalUnlock(pFormat->Data());
  1346. return string;
  1347. }
  1348. }
  1349. return _T("");
  1350. }
  1351. CStringA CClip::GetCFTextTextFormat()
  1352. {
  1353. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_TEXT);
  1354. if(pFormat != NULL)
  1355. {
  1356. char *stringData = (char *)GlobalLock(pFormat->Data());
  1357. if(stringData != NULL)
  1358. {
  1359. CStringA string(stringData);
  1360. GlobalUnlock(pFormat->Data());
  1361. return string;
  1362. }
  1363. }
  1364. return _T("");
  1365. }
  1366. BOOL CClip::WriteTextToFile(CString path, BOOL unicode, BOOL asci, BOOL utf8)
  1367. {
  1368. BOOL ret = false;
  1369. CFile f;
  1370. if(f.Open(path, CFile::modeWrite|CFile::modeCreate))
  1371. {
  1372. CStringW w = GetUnicodeTextFormat();
  1373. CStringA a = GetCFTextTextFormat();
  1374. if(utf8 && w != _T(""))
  1375. {
  1376. CStringA convToUtf8;
  1377. CTextConvert::ConvertToUTF8(w, convToUtf8);
  1378. byte header[2];
  1379. header[0] = 0xEF;
  1380. header[1] = 0xBB;
  1381. f.Write(&header, 2);
  1382. f.Write(convToUtf8.GetBuffer(), convToUtf8.GetLength());
  1383. ret = true;
  1384. }
  1385. else if(unicode && w != _T(""))
  1386. {
  1387. byte header[2];
  1388. header[0] = 0xFF;
  1389. header[1] = 0xFE;
  1390. f.Write(&header, 2);
  1391. f.Write(w.GetBuffer(), w.GetLength() * sizeof(wchar_t));
  1392. ret = true;
  1393. }
  1394. else if(asci && a != _T(""))
  1395. {
  1396. f.Write(a.GetBuffer(), a.GetLength());
  1397. ret = true;
  1398. }
  1399. f.Close();
  1400. }
  1401. return ret;
  1402. }
  1403. bool CClip::AddFileDataToData(CString &errorMessage)
  1404. {
  1405. INT_PTR size = m_Formats.GetSize();
  1406. if (size <= 0)
  1407. {
  1408. errorMessage = _T("No CF_HDROP formats to convert");
  1409. return false;
  1410. }
  1411. bool addedFileData = false;
  1412. int nCF_HDROPIndex = -1;
  1413. int dittoDataIndex = -1;
  1414. for (int i = 0; i < size; i++)
  1415. {
  1416. if (m_Formats[i].m_cfType == CF_HDROP)
  1417. {
  1418. nCF_HDROPIndex = i;
  1419. }
  1420. else if(m_Formats[i].m_cfType == theApp.m_DittoFileData)
  1421. {
  1422. dittoDataIndex = i;
  1423. }
  1424. }
  1425. if (nCF_HDROPIndex < 0)
  1426. {
  1427. errorMessage = _T("No CF_HDROP formats to convert");
  1428. return false;
  1429. }
  1430. else if (dittoDataIndex >= 0)
  1431. {
  1432. return false;
  1433. }
  1434. else
  1435. {
  1436. using namespace nsPath;
  1437. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  1438. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  1439. TCHAR filePath[MAX_PATH];
  1440. CString newDesc = _T("File Contents - ");
  1441. for (int nFile = 0; nFile < nNumFiles; nFile++)
  1442. {
  1443. if (DragQueryFile(drop, nFile, filePath, sizeof(filePath)) > 0)
  1444. {
  1445. CFile file;
  1446. CFileException ex;
  1447. if (file.Open(filePath, CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone, &ex))
  1448. {
  1449. ULONGLONG fileSize = file.GetLength();
  1450. int maxSize = CGetSetOptions::GetMaxFileContentsSize();
  1451. if (fileSize < maxSize)
  1452. {
  1453. CString src(filePath);
  1454. CStringA csFilePath;
  1455. CTextConvert::ConvertToUTF8(src, csFilePath);
  1456. int bufferSize = fileSize + csFilePath.GetLength() + 1 + md5StringLength + 1;;
  1457. char *pBuffer = new char[bufferSize];
  1458. if (pBuffer != NULL)
  1459. {
  1460. //data contents
  1461. //original file<null terminator>md5<null terminator>file data
  1462. memset(pBuffer, 0, bufferSize);
  1463. strncpy(pBuffer, csFilePath, csFilePath.GetLength());
  1464. CClipFormat* pCF;
  1465. //move the buffer start past the file path and md5 string
  1466. char *bufferStart = pBuffer + csFilePath.GetLength() + 1 + md5StringLength + 1;
  1467. int readBytes = file.Read(bufferStart, fileSize);
  1468. CMd5 md5;
  1469. CStringA md5String = md5.CalcMD5FromString(bufferStart, fileSize);
  1470. char *bufferMd5 = pBuffer + csFilePath.GetLength() + 1;
  1471. strncpy(bufferMd5, md5String, md5StringLength);
  1472. AddFormat(theApp.m_DittoFileData, pBuffer, bufferSize);
  1473. addedFileData = true;
  1474. newDesc += filePath;
  1475. newDesc += _T("\n");
  1476. Log(StrF(_T("Saving file contents to Ditto Database, file: %s, size: %d, md5: %s"), filePath, fileSize, md5String));
  1477. }
  1478. }
  1479. else
  1480. {
  1481. const int MAX_FILE_SIZE_BUFFER = 255;
  1482. TCHAR szFileSize[MAX_FILE_SIZE_BUFFER];
  1483. TCHAR szMaxFileSize[MAX_FILE_SIZE_BUFFER];
  1484. StrFormatByteSize(fileSize, szFileSize, MAX_FILE_SIZE_BUFFER);
  1485. StrFormatByteSize(maxSize, szMaxFileSize, MAX_FILE_SIZE_BUFFER);
  1486. errorMessage += StrF(_T("File is to large: %s, Size: %s, Max Size: %s\r\n"), filePath, szFileSize, szMaxFileSize);
  1487. }
  1488. }
  1489. else
  1490. {
  1491. TCHAR szError[200];
  1492. ex.GetErrorMessage(szError, 200);
  1493. errorMessage += StrF(_T("Error opening file: %s, Error: %s\r\n"), filePath, szError);
  1494. }
  1495. }
  1496. }
  1497. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  1498. if (addedFileData)
  1499. {
  1500. for (int i = 0; i < size; i++)
  1501. {
  1502. this->m_Formats.RemoveAt(i, 1);
  1503. }
  1504. this->m_Desc = newDesc;
  1505. if (this->ModifyDescription())
  1506. {
  1507. if (this->AddToDataTable() == FALSE)
  1508. {
  1509. errorMessage += _T("Error saving data to database.");
  1510. }
  1511. }
  1512. else
  1513. {
  1514. errorMessage += _T("Error saving main table to database.");
  1515. }
  1516. }
  1517. }
  1518. return addedFileData;
  1519. }
  1520. /*----------------------------------------------------------------------------*\
  1521. CClipList
  1522. \*----------------------------------------------------------------------------*/
  1523. CClipList::~CClipList()
  1524. {
  1525. CClip* pClip;
  1526. while(GetCount())
  1527. {
  1528. pClip = RemoveHead();
  1529. delete pClip;
  1530. }
  1531. }
  1532. // returns the number of clips actually saved
  1533. // while this does empty the Format Data, it does not delete the Clips.
  1534. int CClipList::AddToDB(bool bLatestOrder)
  1535. {
  1536. Log(_T("AddToDB - Start"));
  1537. int savedCount = 0;
  1538. CClip* pClip;
  1539. POSITION pos;
  1540. bool bResult;
  1541. INT_PTR remaining = GetCount();
  1542. pos = GetHeadPosition();
  1543. while(pos)
  1544. {
  1545. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  1546. remaining--;
  1547. pClip = GetNext(pos);
  1548. ASSERT(pClip);
  1549. if(bLatestOrder)
  1550. {
  1551. pClip->MakeLatestOrder();
  1552. pClip->MakeLatestGroupOrder();
  1553. }
  1554. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  1555. bResult = pClip->AddToDB();
  1556. if(bResult)
  1557. {
  1558. savedCount++;
  1559. }
  1560. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  1561. }
  1562. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  1563. return savedCount;
  1564. }
  1565. const CClipList& CClipList::operator=(const CClipList &cliplist)
  1566. {
  1567. POSITION pos;
  1568. CClip* pClip;
  1569. pos = cliplist.GetHeadPosition();
  1570. while(pos)
  1571. {
  1572. pClip = cliplist.GetNext(pos);
  1573. ASSERT(pClip);
  1574. CClip *pNewClip = new CClip;
  1575. if(pNewClip)
  1576. {
  1577. *pNewClip = *pClip;
  1578. AddTail(pNewClip);
  1579. }
  1580. }
  1581. return *this;
  1582. }