Clip.cpp 54 KB

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