Clip.cpp 51 KB

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