ClipIds.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "ClipIds.h"
  4. #include "tinyxml.h"
  5. #include "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. // allocate an HGLOBAL of the given Format Type representing these Clip IDs.
  13. HGLOBAL CClipIDs::Render(UINT cfType)
  14. {
  15. int count = GetSize();
  16. if(count <= 0)
  17. {
  18. return 0;
  19. }
  20. if(count == 1)
  21. {
  22. return CClip::LoadFormat(ElementAt(0), cfType);
  23. }
  24. CStringA SepA = CTextConvert::ConvertToChar(g_Opt.GetMultiPasteSeparator());
  25. CStringW SepW = CTextConvert::ConvertToUnicode(g_Opt.GetMultiPasteSeparator());
  26. if(cfType == CF_TEXT)
  27. {
  28. CCF_TextAggregator CFText(SepA);
  29. if(AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop))
  30. {
  31. return CFText.GetHGlobal();
  32. }
  33. }
  34. else if(cfType == CF_UNICODETEXT)
  35. {
  36. CCF_UnicodeTextAggregator CFUnicodeText(SepW);
  37. if(AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop))
  38. {
  39. return CFUnicodeText.GetHGlobal();
  40. }
  41. }
  42. else if(cfType == CF_HDROP)
  43. {
  44. CCF_HDropAggregator HDrop;
  45. if(AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop))
  46. {
  47. return HDrop.GetHGlobal();
  48. }
  49. }
  50. else if(cfType == theApp.m_HTML_Format)
  51. {
  52. CHTMLFormatAggregator Html(SepA);
  53. if(AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop))
  54. {
  55. return Html.GetHGlobal();
  56. }
  57. }
  58. else if(cfType == theApp.m_RTFFormat)
  59. {
  60. CRichTextAggregator RichText(SepA);
  61. if(AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop))
  62. {
  63. return RichText.GetHGlobal();
  64. }
  65. }
  66. return NULL;
  67. }
  68. void CClipIDs::GetTypes(CClipTypes& types)
  69. {
  70. int IDCount = GetSize();
  71. types.RemoveAll();
  72. if(IDCount == 1)
  73. {
  74. CClip::LoadTypes(ElementAt(0), types);
  75. }
  76. else if(IDCount > 1)
  77. {
  78. //Add the types that are common accross all paste ids
  79. long lCount;
  80. CMap<CLIPFORMAT, CLIPFORMAT, long, long> RenderTypes;
  81. for(int nIDPos = 0; nIDPos < IDCount; nIDPos++)
  82. {
  83. CClipTypes CurrTypes;
  84. CClip::LoadTypes(ElementAt(nIDPos), CurrTypes);
  85. int nTypeCount = CurrTypes.GetSize();
  86. for(int nType = 0; nType < nTypeCount; nType++)
  87. {
  88. lCount = 0;
  89. if(nIDPos == 0 || RenderTypes.Lookup(CurrTypes[nType], lCount) == TRUE)
  90. {
  91. lCount++;
  92. RenderTypes.SetAt(CurrTypes[nType], lCount);
  93. }
  94. }
  95. }
  96. CLIPFORMAT Format;
  97. POSITION pos = RenderTypes.GetStartPosition();
  98. while(pos)
  99. {
  100. RenderTypes.GetNextAssoc(pos, Format, lCount);
  101. if(lCount == IDCount)
  102. {
  103. types.Add(Format);
  104. }
  105. }
  106. //If there were no common types add the first clip
  107. if(types.GetSize() <= 0)
  108. {
  109. CClip::LoadTypes(ElementAt(0), types);
  110. }
  111. }
  112. }
  113. bool CClipIDs::AggregateData(IClipAggregator &Aggregator, UINT cfType, BOOL bReverse)
  114. {
  115. CString csSQL;
  116. LPWSTR Text = NULL;
  117. int nTextSize = 0;
  118. int numIDs = GetSize();
  119. int* pIDs = GetData();
  120. bool bRet = false;
  121. try
  122. {
  123. int 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. pIDs[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, i, numIDs))
  147. {
  148. bRet = true;
  149. }
  150. }
  151. }
  152. }
  153. CATCH_SQLITE_EXCEPTION
  154. catch(...)
  155. {
  156. }
  157. return bRet;
  158. }
  159. //----------------------------------------------
  160. // ELEMENT (Clip or Group) MANAGEMENT FUNCTIONS
  161. //----------------------------------------------
  162. // returns the address of the given id in this array or NULL.
  163. long* CClipIDs::FindID(long lID)
  164. {
  165. int count = GetSize();
  166. long* pID = (long*) GetData();
  167. for(int i=0; i < count; i++)
  168. {
  169. if(*pID == lID)
  170. {
  171. return pID;
  172. }
  173. pID++;
  174. }
  175. return NULL;
  176. }
  177. // Blindly Moves IDs into the lParentID Group sequentially with the given order
  178. BOOL CClipIDs::MoveTo(long lParentID, double dFirst, double dIncrement)
  179. {
  180. try
  181. {
  182. int nCount = GetSize();
  183. for(int i = 0; i < nCount; i++)
  184. {
  185. theApp.m_db.execDMLEx(_T("UPDATE Main SET lParentID = %d, ")
  186. _T("lDontAutoDelete = %d WHERE lID = %d AND lID <> %d;"),
  187. lParentID,
  188. (long)CTime::GetCurrentTime().GetTime(),
  189. ElementAt(i),
  190. lParentID);
  191. }
  192. }
  193. CATCH_SQLITE_EXCEPTION
  194. return (TRUE);
  195. }
  196. // Empties this array and fills it with the elements of the given group ID
  197. BOOL CClipIDs::LoadElementsOf(long lGroupID)
  198. {
  199. SetSize(0);
  200. try
  201. {
  202. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lParentID = %d"), lGroupID);
  203. while(q.eof() == false)
  204. {
  205. Add(q.getIntField(_T("lID")));
  206. q.nextRow();
  207. }
  208. }
  209. CATCH_SQLITE_EXCEPTION
  210. return GetSize();
  211. }
  212. // Creates copies (duplicates) of all items in this array and assigns the
  213. // lParentID of the copies to the given "lParentID" group.
  214. // - if lParentID <= 0, then the copies have the same parent as the source
  215. // - pCopies is filled with the corresponding duplicate IDs.
  216. // - pAddNewTable and pSeekTable are used for more efficient recursion.
  217. // - the primary overhead for recursion is one ID array per level deep.
  218. // an alternative design would be to have one CMainTable per level deep,
  219. // but I thought that might be too costly, so I implemented it this way.
  220. BOOL CClipIDs::CopyTo(long lParentID)
  221. {
  222. int count = GetSize();
  223. if(count == 0)
  224. return TRUE;
  225. try
  226. {
  227. theApp.m_db.execDML(_T("begin transaction;"));
  228. for(int i = 0; i < count; i++)
  229. {
  230. int nID = ElementAt(i);
  231. CClip clip;
  232. if(clip.LoadMainTable(nID))
  233. {
  234. if(clip.LoadFormats(nID))
  235. {
  236. clip.MakeLatestTime();
  237. clip.m_lShortCut = 0;
  238. clip.m_lParent = lParentID;
  239. clip.m_csQuickPaste = "";
  240. if(clip.AddToDB(false) == false)
  241. {
  242. Log(_T("failed to add copy to database"));
  243. }
  244. }
  245. }
  246. }
  247. theApp.m_db.execDML(_T("commit transaction;"));
  248. }
  249. CATCH_SQLITE_EXCEPTION
  250. return TRUE;
  251. }
  252. BOOL CClipIDs::DeleteIDs()
  253. {
  254. CPopup status(0, 0, ::GetForegroundWindow());
  255. bool bAllowShow;
  256. bAllowShow = IsAppWnd(::GetForegroundWindow());
  257. BOOL bRet = TRUE;
  258. int count = GetSize();
  259. if(count <= 0)
  260. return FALSE;
  261. try
  262. {
  263. theApp.m_db.execDML(_T("begin transaction;"));
  264. for(int i=0; i < count; i++)
  265. {
  266. if(bAllowShow)
  267. status.Show(StrF(_T("Deleting %d out of %d..."), i+1, count));
  268. if(i && i % 50)
  269. {
  270. theApp.m_db.execDML(_T("commit transaction;"));
  271. theApp.m_db.execDML(_T("begin transaction;"));
  272. }
  273. bRet = bRet && DeleteID(ElementAt(i));
  274. }
  275. theApp.m_db.execDML(_T("commit transaction;"));
  276. }
  277. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  278. return bRet;
  279. }
  280. BOOL CClipIDs::CreateExportSqliteDB(CppSQLite3DB &db)
  281. {
  282. BOOL bRet = FALSE;
  283. try
  284. {
  285. db.execDML(_T("CREATE TABLE Main(")
  286. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  287. _T("lVersion INTEGER, ")
  288. _T("mText TEXT);"));
  289. db.execDML(_T("CREATE TABLE Data(")
  290. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  291. _T("lParentID INTEGER, ")
  292. _T("strClipBoardFormat TEXT, ")
  293. _T("lOriginalSize INTEGER, ")
  294. _T("ooData BLOB);"));
  295. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  296. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  297. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  298. _T("BEGIN\n")
  299. _T("DELETE FROM Data WHERE lParentID = old.lID;\n")
  300. _T("END\n"));
  301. bRet = TRUE;
  302. }
  303. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  304. return bRet;
  305. }
  306. BOOL CClipIDs::Export(CString csFilePath)
  307. {
  308. int count = GetSize();
  309. if(count == 0)
  310. return TRUE;
  311. BOOL bRet = FALSE;
  312. if(FileExists(csFilePath) && DeleteFile(csFilePath) == FALSE)
  313. {
  314. Log(StrF(_T("Export::Error deleting the file %s"), csFilePath));
  315. return FALSE;
  316. }
  317. try
  318. {
  319. CppSQLite3DB db;
  320. db.open(csFilePath);
  321. if(CreateExportSqliteDB(db) == FALSE)
  322. return FALSE;
  323. for(int i = 0; i < count; i++)
  324. {
  325. int nID = ElementAt(i);
  326. CClip_ImportExport clip;
  327. if(clip.LoadMainTable(nID))
  328. {
  329. if(clip.LoadFormats(nID))
  330. {
  331. clip.ExportToSqliteDB(db);
  332. bRet = TRUE;
  333. }
  334. }
  335. }
  336. db.close();
  337. }
  338. CATCH_SQLITE_EXCEPTION
  339. return bRet;
  340. }