Clip.cpp 54 KB

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