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