ClipIds.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "ClipIds.h"
  4. #include "tinyxml\tinyxml.h"
  5. #include "shared/TextConvert.h"
  6. #include "Clip_ImportExport.h"
  7. #include "CF_HDropAggregator.h"
  8. #include "CF_UnicodeTextAggregator.h"
  9. #include "CF_TextAggregator.h"
  10. #include "richtextaggregator.h"
  11. #include "htmlformataggregator.h"
  12. #include "Popup.h"
  13. // allocate an HGLOBAL of the given Format Type representing these Clip IDs.
  14. HGLOBAL CClipIDs::Render(UINT cfType)
  15. {
  16. INT_PTR count = GetSize();
  17. if(count <= 0)
  18. {
  19. return 0;
  20. }
  21. if(count == 1)
  22. {
  23. return CClip::LoadFormat(ElementAt(0), cfType);
  24. }
  25. CStringA SepA = CTextConvert::ConvertToChar(g_Opt.GetMultiPasteSeparator());
  26. CStringW SepW = CTextConvert::ConvertToUnicode(g_Opt.GetMultiPasteSeparator());
  27. if(cfType == CF_TEXT)
  28. {
  29. CCF_TextAggregator CFText(SepA);
  30. if(AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse, false))
  31. {
  32. return CFText.GetHGlobal();
  33. }
  34. }
  35. else if(cfType == CF_UNICODETEXT)
  36. {
  37. CCF_UnicodeTextAggregator CFUnicodeText(SepW);
  38. if(AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse, false))
  39. {
  40. return CFUnicodeText.GetHGlobal();
  41. }
  42. }
  43. else if(cfType == CF_HDROP)
  44. {
  45. CCF_HDropAggregator HDrop;
  46. if(AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse, false))
  47. {
  48. return HDrop.GetHGlobal();
  49. }
  50. }
  51. else if(cfType == theApp.m_HTML_Format)
  52. {
  53. CHTMLFormatAggregator Html(SepA);
  54. if(AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse, false))
  55. {
  56. return Html.GetHGlobal();
  57. }
  58. }
  59. else if(cfType == theApp.m_RTFFormat)
  60. {
  61. CRichTextAggregator RichText(SepA);
  62. if(AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse, false))
  63. {
  64. return RichText.GetHGlobal();
  65. }
  66. }
  67. return NULL;
  68. }
  69. void CClipIDs::GetTypes(CClipTypes& types)
  70. {
  71. INT_PTR count = GetSize();
  72. types.RemoveAll();
  73. if(count == 1)
  74. {
  75. CClip::LoadTypes(ElementAt(0), types);
  76. }
  77. else if(count > 1)
  78. {
  79. //Add the types that are common across all paste ids
  80. long lCount;
  81. CMap<CLIPFORMAT, CLIPFORMAT, long, long> RenderTypes;
  82. for(int nIDPos = 0; nIDPos < count; nIDPos++)
  83. {
  84. CClipTypes CurrTypes;
  85. CClip::LoadTypes(ElementAt(nIDPos), CurrTypes);
  86. INT_PTR typeCount = CurrTypes.GetSize();
  87. for(int type = 0; type < typeCount; type++)
  88. {
  89. lCount = 0;
  90. if(nIDPos == 0 || RenderTypes.Lookup(CurrTypes[type], lCount) == TRUE)
  91. {
  92. lCount++;
  93. RenderTypes.SetAt(CurrTypes[type], lCount);
  94. }
  95. }
  96. }
  97. CLIPFORMAT Format;
  98. POSITION pos = RenderTypes.GetStartPosition();
  99. while(pos)
  100. {
  101. RenderTypes.GetNextAssoc(pos, Format, lCount);
  102. if(lCount == count)
  103. {
  104. types.Add(Format);
  105. }
  106. }
  107. //If there were no common types add the first clip
  108. if(types.GetSize() <= 0)
  109. {
  110. CClip::LoadTypes(ElementAt(0), types);
  111. }
  112. }
  113. }
  114. bool CClipIDs::AggregateData(IClipAggregator &Aggregator, UINT cfType, BOOL bReverse, bool textOnly)
  115. {
  116. CString csSQL;
  117. LPWSTR Text = NULL;
  118. int nTextSize = 0;
  119. INT_PTR numIDs = GetSize();
  120. bool bRet = false;
  121. try
  122. {
  123. INT_PTR nIndex;
  124. for(int i=0; i < numIDs; i++)
  125. {
  126. nIndex = i;
  127. if(bReverse)
  128. {
  129. nIndex = numIDs - i - 1;
  130. }
  131. CString sqlCF_HDROP = _T("");
  132. if (textOnly &&
  133. cfType == CF_UNICODETEXT || cfType == CF_TEXT)
  134. {
  135. sqlCF_HDROP.Format(_T("OR Data.strClipBoardFormat = '%s'"), GetFormatName(CF_HDROP));
  136. }
  137. csSQL.Format(_T("SELECT * FROM Data ")
  138. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  139. _T("WHERE (Data.strClipBoardFormat = '%s'")
  140. _T(" %s) ")
  141. _T("AND Main.lID = %d"),
  142. GetFormatName(cfType),
  143. sqlCF_HDROP,
  144. ElementAt(nIndex));
  145. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  146. if(q.eof() == false)
  147. {
  148. int nDataLen = 0;
  149. LPVOID pData = (LPVOID)q.getBlobField(_T("ooData"), nDataLen);
  150. if(pData == NULL)
  151. {
  152. continue;
  153. }
  154. if(Aggregator.AddClip(pData, nDataLen, (int)i, (int)numIDs, GetFormatID(q.getStringField(_T("strClipBoardFormat")))))
  155. {
  156. bRet |= true;
  157. }
  158. }
  159. else
  160. {
  161. bRet |= false;
  162. }
  163. }
  164. }
  165. CATCH_SQLITE_EXCEPTION
  166. catch(...)
  167. {
  168. }
  169. return bRet;
  170. }
  171. // Blindly Moves IDs into the lParentID Group sequentially with the given order
  172. BOOL CClipIDs::MoveTo(long lParentID, double dFirst, double dIncrement)
  173. {
  174. try
  175. {
  176. int count = (int)GetSize();
  177. Log(StrF(_T("MoveTo, Start, Size: %d, ParentId: %d"), count, lParentID));
  178. for(int i = count-1; i >= 0; i--)
  179. {
  180. CString sql;
  181. if(lParentID > 0)
  182. {
  183. sql = StrF(_T("UPDATE Main SET lParentID = %d, clipGroupOrder = %f WHERE lID = %d AND lID <> %d;"),
  184. lParentID,
  185. CClip::GetNewOrder(lParentID, ElementAt(i)),
  186. ElementAt(i),
  187. lParentID);
  188. }
  189. else
  190. {
  191. sql = StrF(_T("UPDATE Main SET lParentID = %d WHERE lID = %d AND lID <> %d;"),
  192. lParentID,
  193. ElementAt(i),
  194. lParentID);
  195. }
  196. int ret = theApp.m_db.execDMLEx(sql);
  197. Log(StrF(_T("MoveTo, Sql Ret: %d, SQL: %s"), ret, sql));
  198. }
  199. }
  200. CATCH_SQLITE_EXCEPTION
  201. return (TRUE);
  202. }
  203. // Empties this array and fills it with the elements of the given group ID
  204. BOOL CClipIDs::LoadElementsOf(int groupId)
  205. {
  206. SetSize(0);
  207. try
  208. {
  209. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lParentID = %d"), groupId);
  210. while(q.eof() == false)
  211. {
  212. Add(q.getIntField(_T("lID")));
  213. q.nextRow();
  214. }
  215. }
  216. CATCH_SQLITE_EXCEPTION
  217. return GetSize() > 0;
  218. }
  219. // Creates copies (duplicates) of all items in this array and assigns the
  220. // lParentID of the copies to the given "lParentID" group.
  221. // - if lParentID <= 0, then the copies have the same parent as the source
  222. // - pCopies is filled with the corresponding duplicate IDs.
  223. // - pAddNewTable and pSeekTable are used for more efficient recursion.
  224. // - the primary overhead for recursion is one ID array per level deep.
  225. // an alternative design would be to have one CMainTable per level deep,
  226. // but I thought that might be too costly, so I implemented it this way.
  227. BOOL CClipIDs::CopyTo(int parentId)
  228. {
  229. INT_PTR count = GetSize();
  230. if(count == 0)
  231. return TRUE;
  232. try
  233. {
  234. theApp.m_db.execDML(_T("begin transaction;"));
  235. for(int i = 0; i < count; i++)
  236. {
  237. int nID = ElementAt(i);
  238. CClip clip;
  239. if(clip.LoadMainTable(nID))
  240. {
  241. if(clip.LoadFormats(nID))
  242. {
  243. clip.MakeLatestOrder();
  244. clip.m_shortCut = 0;
  245. clip.m_parentId = parentId;
  246. clip.m_csQuickPaste = "";
  247. if(clip.AddToDB(false) == false)
  248. {
  249. Log(_T("failed to add copy to database"));
  250. }
  251. }
  252. }
  253. }
  254. theApp.m_db.execDML(_T("commit transaction;"));
  255. }
  256. CATCH_SQLITE_EXCEPTION
  257. return TRUE;
  258. }
  259. BOOL CClipIDs::DeleteIDs(bool fromClipWindow, CppSQLite3DB& db)
  260. {
  261. CPopup status(0, 0, ::GetForegroundWindow());
  262. bool bAllowShow;
  263. bAllowShow = IsAppWnd(::GetForegroundWindow());
  264. BOOL bRet = TRUE;
  265. INT_PTR count = GetSize();
  266. int batchCount = 25;
  267. Log(StrF(_T("Begin delete clips, Count: %d from Window: %d"), count, fromClipWindow));
  268. if(count <= 0)
  269. return FALSE;
  270. try
  271. {
  272. CString sql = _T("DELETE FROM Main where lId in(");
  273. CString sqlIn = _T("");
  274. CString workingString = _T("Deleting clips, building query statement");
  275. INT_PTR startIndex = 0;
  276. INT_PTR index = 0;
  277. if(bAllowShow)
  278. {
  279. status.Show(workingString);
  280. }
  281. for(index = 0; index < count; index++)
  282. {
  283. int clipId = ElementAt(index);
  284. if(clipId <= 0)
  285. continue;
  286. Log(StrF(_T("Delete clip Id: %d"), clipId));
  287. bool cont = false;
  288. bool bGroup = false;
  289. {
  290. CppSQLite3Query q = db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), clipId);
  291. cont = !q.eof();
  292. if(cont)
  293. {
  294. bGroup = q.getIntField(_T("bIsGroup")) > 0;
  295. }
  296. }
  297. if(cont)
  298. {
  299. if(bGroup)
  300. {
  301. db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), clipId);
  302. }
  303. if(sqlIn.GetLength() > 0)
  304. {
  305. sqlIn += ", ";
  306. }
  307. sqlIn += StrF(_T("%d"), clipId);
  308. }
  309. if(index > 0 &&
  310. (index % batchCount) == 0)
  311. {
  312. if(bAllowShow)
  313. {
  314. status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
  315. }
  316. startIndex = index;
  317. db.execDMLEx(sql + sqlIn + _T(")"));
  318. sqlIn = "";
  319. bRet = TRUE;
  320. if(bAllowShow)
  321. {
  322. status.Show(workingString);
  323. }
  324. }
  325. if(fromClipWindow == false)
  326. {
  327. theApp.OnDeleteID(clipId);
  328. }
  329. }
  330. if(sqlIn.GetLength() > 0)
  331. {
  332. if(bAllowShow)
  333. {
  334. status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
  335. }
  336. db.execDMLEx(sql + sqlIn + _T(")"));
  337. bRet = TRUE;
  338. }
  339. }
  340. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  341. Log(StrF(_T("End delete clips, Count: %d"), count));
  342. return bRet;
  343. }
  344. BOOL CClipIDs::CreateExportSqliteDB(CppSQLite3DB &db)
  345. {
  346. BOOL bRet = FALSE;
  347. try
  348. {
  349. db.execDML(_T("CREATE TABLE Main(")
  350. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  351. _T("lVersion INTEGER, ")
  352. _T("mText TEXT);"));
  353. db.execDML(_T("CREATE TABLE Data(")
  354. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  355. _T("lParentID INTEGER, ")
  356. _T("strClipBoardFormat TEXT, ")
  357. _T("lOriginalSize INTEGER, ")
  358. _T("ooData BLOB);"));
  359. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  360. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  361. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  362. _T("BEGIN\n")
  363. _T("DELETE FROM Data WHERE lParentID = old.lID;\n")
  364. _T("END\n"));
  365. bRet = TRUE;
  366. }
  367. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  368. return bRet;
  369. }
  370. BOOL CClipIDs::Export(CString csFilePath)
  371. {
  372. INT_PTR count = GetSize();
  373. if(count == 0)
  374. return TRUE;
  375. BOOL bRet = FALSE;
  376. if(FileExists(csFilePath) && DeleteFile(csFilePath) == FALSE)
  377. {
  378. Log(StrF(_T("Export::Error deleting the file %s"), csFilePath));
  379. return FALSE;
  380. }
  381. try
  382. {
  383. CppSQLite3DB db;
  384. db.open(csFilePath);
  385. if(CreateExportSqliteDB(db) == FALSE)
  386. return FALSE;
  387. for(int i = 0; i < count; i++)
  388. {
  389. int nID = ElementAt(i);
  390. CClip_ImportExport clip;
  391. if(clip.LoadMainTable(nID))
  392. {
  393. if(clip.LoadFormats(nID))
  394. {
  395. clip.ExportToSqliteDB(db);
  396. bRet = TRUE;
  397. }
  398. }
  399. }
  400. db.close();
  401. }
  402. CATCH_SQLITE_EXCEPTION
  403. return bRet;
  404. }