Clip.cpp 54 KB

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