1
0

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