Clip.cpp 52 KB

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