ClipIds.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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))
  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))
  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))
  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))
  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))
  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)
  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. csSQL.Format(_T("SELECT * FROM Data ")
  132. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  133. _T("WHERE Data.strClipBoardFormat = '%s' ")
  134. _T("AND Main.lID = %d"),
  135. GetFormatName(cfType),
  136. ElementAt(nIndex));
  137. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  138. if(q.eof() == false)
  139. {
  140. int nDataLen = 0;
  141. LPVOID pData = (LPVOID)q.getBlobField(_T("ooData"), nDataLen);
  142. if(pData == NULL)
  143. {
  144. continue;
  145. }
  146. if(Aggregator.AddClip(pData, nDataLen, (int)i, (int)numIDs))
  147. {
  148. bRet = true;
  149. }
  150. }
  151. }
  152. }
  153. CATCH_SQLITE_EXCEPTION
  154. catch(...)
  155. {
  156. }
  157. return bRet;
  158. }
  159. // Blindly Moves IDs into the lParentID Group sequentially with the given order
  160. BOOL CClipIDs::MoveTo(long lParentID, double dFirst, double dIncrement)
  161. {
  162. try
  163. {
  164. int count = (int)GetSize();
  165. Log(StrF(_T("MoveTo, Start, Size: %d, ParentId: %d"), count, lParentID));
  166. for(int i = count-1; i >= 0; i--)
  167. {
  168. CString sql;
  169. if(lParentID > 0)
  170. {
  171. sql = StrF(_T("UPDATE Main SET lParentID = %d, clipGroupOrder = %f WHERE lID = %d AND lID <> %d;"),
  172. lParentID,
  173. CClip::GetNewOrder(lParentID, ElementAt(i)),
  174. ElementAt(i),
  175. lParentID);
  176. }
  177. else
  178. {
  179. sql = StrF(_T("UPDATE Main SET lParentID = %d WHERE lID = %d AND lID <> %d;"),
  180. lParentID,
  181. ElementAt(i),
  182. lParentID);
  183. }
  184. int ret = theApp.m_db.execDMLEx(sql);
  185. Log(StrF(_T("MoveTo, Sql Ret: %d, SQL: %s"), ret, sql));
  186. }
  187. }
  188. CATCH_SQLITE_EXCEPTION
  189. return (TRUE);
  190. }
  191. // Empties this array and fills it with the elements of the given group ID
  192. BOOL CClipIDs::LoadElementsOf(int groupId)
  193. {
  194. SetSize(0);
  195. try
  196. {
  197. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lParentID = %d"), groupId);
  198. while(q.eof() == false)
  199. {
  200. Add(q.getIntField(_T("lID")));
  201. q.nextRow();
  202. }
  203. }
  204. CATCH_SQLITE_EXCEPTION
  205. return GetSize() > 0;
  206. }
  207. // Creates copies (duplicates) of all items in this array and assigns the
  208. // lParentID of the copies to the given "lParentID" group.
  209. // - if lParentID <= 0, then the copies have the same parent as the source
  210. // - pCopies is filled with the corresponding duplicate IDs.
  211. // - pAddNewTable and pSeekTable are used for more efficient recursion.
  212. // - the primary overhead for recursion is one ID array per level deep.
  213. // an alternative design would be to have one CMainTable per level deep,
  214. // but I thought that might be too costly, so I implemented it this way.
  215. BOOL CClipIDs::CopyTo(int parentId)
  216. {
  217. INT_PTR count = GetSize();
  218. if(count == 0)
  219. return TRUE;
  220. try
  221. {
  222. theApp.m_db.execDML(_T("begin transaction;"));
  223. for(int i = 0; i < count; i++)
  224. {
  225. int nID = ElementAt(i);
  226. CClip clip;
  227. if(clip.LoadMainTable(nID))
  228. {
  229. if(clip.LoadFormats(nID))
  230. {
  231. clip.MakeLatestOrder();
  232. clip.m_shortCut = 0;
  233. clip.m_parentId = parentId;
  234. clip.m_csQuickPaste = "";
  235. if(clip.AddToDB(false) == false)
  236. {
  237. Log(_T("failed to add copy to database"));
  238. }
  239. }
  240. }
  241. }
  242. theApp.m_db.execDML(_T("commit transaction;"));
  243. }
  244. CATCH_SQLITE_EXCEPTION
  245. return TRUE;
  246. }
  247. BOOL CClipIDs::DeleteIDs(bool fromClipWindow, CppSQLite3DB& db)
  248. {
  249. CPopup status(0, 0, ::GetForegroundWindow());
  250. bool bAllowShow;
  251. bAllowShow = IsAppWnd(::GetForegroundWindow());
  252. BOOL bRet = TRUE;
  253. INT_PTR count = GetSize();
  254. int batchCount = 25;
  255. Log(StrF(_T("Begin delete clips, Count: %d from Window: %d"), count, fromClipWindow));
  256. if(count <= 0)
  257. return FALSE;
  258. try
  259. {
  260. CString sql = _T("DELETE FROM Main where lId in(");
  261. CString sqlIn = _T("");
  262. CString workingString = _T("Deleting clips, building query statement");
  263. INT_PTR startIndex = 0;
  264. INT_PTR index = 0;
  265. if(bAllowShow)
  266. {
  267. status.Show(workingString);
  268. }
  269. for(index = 0; index < count; index++)
  270. {
  271. int clipId = ElementAt(index);
  272. if(clipId <= 0)
  273. continue;
  274. Log(StrF(_T("Delete clip Id: %d"), clipId));
  275. bool cont = false;
  276. bool bGroup = false;
  277. {
  278. CppSQLite3Query q = db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), clipId);
  279. cont = !q.eof();
  280. if(cont)
  281. {
  282. bGroup = q.getIntField(_T("bIsGroup")) > 0;
  283. }
  284. }
  285. if(cont)
  286. {
  287. if(bGroup)
  288. {
  289. db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), clipId);
  290. }
  291. if(sqlIn.GetLength() > 0)
  292. {
  293. sqlIn += ", ";
  294. }
  295. sqlIn += StrF(_T("%d"), clipId);
  296. }
  297. if(index > 0 &&
  298. (index % batchCount) == 0)
  299. {
  300. if(bAllowShow)
  301. {
  302. status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
  303. }
  304. startIndex = index;
  305. db.execDMLEx(sql + sqlIn + _T(")"));
  306. sqlIn = "";
  307. bRet = TRUE;
  308. if(bAllowShow)
  309. {
  310. status.Show(workingString);
  311. }
  312. }
  313. if(fromClipWindow == false)
  314. {
  315. theApp.OnDeleteID(clipId);
  316. }
  317. }
  318. if(sqlIn.GetLength() > 0)
  319. {
  320. if(bAllowShow)
  321. {
  322. status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
  323. }
  324. db.execDMLEx(sql + sqlIn + _T(")"));
  325. bRet = TRUE;
  326. }
  327. }
  328. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  329. Log(StrF(_T("End delete clips, Count: %d"), count));
  330. return bRet;
  331. }
  332. BOOL CClipIDs::CreateExportSqliteDB(CppSQLite3DB &db)
  333. {
  334. BOOL bRet = FALSE;
  335. try
  336. {
  337. db.execDML(_T("CREATE TABLE Main(")
  338. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  339. _T("lVersion INTEGER, ")
  340. _T("mText TEXT);"));
  341. db.execDML(_T("CREATE TABLE Data(")
  342. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  343. _T("lParentID INTEGER, ")
  344. _T("strClipBoardFormat TEXT, ")
  345. _T("lOriginalSize INTEGER, ")
  346. _T("ooData BLOB);"));
  347. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  348. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  349. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  350. _T("BEGIN\n")
  351. _T("DELETE FROM Data WHERE lParentID = old.lID;\n")
  352. _T("END\n"));
  353. bRet = TRUE;
  354. }
  355. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  356. return bRet;
  357. }
  358. BOOL CClipIDs::Export(CString csFilePath)
  359. {
  360. INT_PTR count = GetSize();
  361. if(count == 0)
  362. return TRUE;
  363. BOOL bRet = FALSE;
  364. if(FileExists(csFilePath) && DeleteFile(csFilePath) == FALSE)
  365. {
  366. Log(StrF(_T("Export::Error deleting the file %s"), csFilePath));
  367. return FALSE;
  368. }
  369. try
  370. {
  371. CppSQLite3DB db;
  372. db.open(csFilePath);
  373. if(CreateExportSqliteDB(db) == FALSE)
  374. return FALSE;
  375. for(int i = 0; i < count; i++)
  376. {
  377. int nID = ElementAt(i);
  378. CClip_ImportExport clip;
  379. if(clip.LoadMainTable(nID))
  380. {
  381. if(clip.LoadFormats(nID))
  382. {
  383. clip.ExportToSqliteDB(db);
  384. bRet = TRUE;
  385. }
  386. }
  387. }
  388. db.close();
  389. }
  390. CATCH_SQLITE_EXCEPTION
  391. return bRet;
  392. }