Clip_ImportExport.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include ".\clip_importexport.h"
  4. #include "shared/TextConvert.h"
  5. #include "sqlite/CppSQLite3.h"
  6. #include "zlib/zlib.h"
  7. #include "Misc.h"
  8. #define CURRENT_EXPORT_VERSION 1
  9. CClip_ImportExport::CClip_ImportExport(void) :
  10. m_importCount(0)
  11. {
  12. }
  13. CClip_ImportExport::~CClip_ImportExport(void)
  14. {
  15. }
  16. bool CClip_ImportExport::ExportToSqliteDB(CppSQLite3DB &db)
  17. {
  18. bool bRet = false;
  19. try
  20. {
  21. //Add to Main Table
  22. m_Desc.Replace(_T("'"), _T("''"));
  23. db.execDMLEx(_T("insert into Main values(NULL, %d, '%s');"), CURRENT_EXPORT_VERSION, m_Desc);
  24. long lId = (long)db.lastRowId();
  25. //Add to Data table
  26. CClipFormat* pCF;
  27. CppSQLite3Statement stmt = db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?, ?);"));
  28. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  29. {
  30. pCF = & m_Formats.ElementAt(i);
  31. stmt.bind(1, lId);
  32. stmt.bind(2, GetFormatName(pCF->m_cfType));
  33. INT_PTR originalSize = GlobalSize(pCF->m_hgData);
  34. stmt.bind(3, (int)originalSize);
  35. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  36. if(Data)
  37. {
  38. //First compress the data
  39. INT_PTR zippedSize = compressBound((ULONG)originalSize);
  40. Bytef *pZipped = new Bytef[zippedSize];
  41. if(pZipped)
  42. {
  43. INT_PTR zipReturn = compress(pZipped, (uLongf *)&zippedSize, (const Bytef *)Data, (ULONG)originalSize);
  44. if(zipReturn == Z_OK)
  45. {
  46. stmt.bind(4, pZipped, (int)zippedSize);
  47. }
  48. delete []pZipped;
  49. pZipped = NULL;
  50. }
  51. }
  52. GlobalUnlock(pCF->m_hgData);
  53. stmt.execDML();
  54. stmt.reset();
  55. m_Formats.RemoveAt(i);
  56. }
  57. bRet = true;
  58. }
  59. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  60. return bRet;
  61. }
  62. bool CClip_ImportExport::ImportFromSqliteDB(CppSQLite3DB &db, bool bAddToDB, bool bPutOnClipboard)
  63. {
  64. bool bRet = false;
  65. CStringA csCF_TEXT;
  66. CStringW csCF_UNICODETEXT;
  67. try
  68. {
  69. CppSQLite3Query q = db.execQuery(_T("Select * from Main order by lId DESC"));
  70. while(q.eof() == false)
  71. {
  72. Clear();
  73. int nVersion = q.getIntField(_T("lVersion"));
  74. if(nVersion == 1)
  75. {
  76. if(ImportFromSqliteV1(db, q))
  77. {
  78. if(bAddToDB)
  79. {
  80. MakeLatestOrder();
  81. AddToDB(true);
  82. bRet = true;
  83. }
  84. else if(bPutOnClipboard)
  85. {
  86. bRet = true;
  87. }
  88. }
  89. }
  90. m_importCount++;
  91. //If putting on the clipboard and there are multiple
  92. //then append cf_text and cf_unicodetext
  93. if(bPutOnClipboard)
  94. {
  95. Append_CF_TEXT_AND_CF_UNICODETEXT(csCF_TEXT, csCF_UNICODETEXT);
  96. }
  97. q.nextRow();
  98. }
  99. if(bRet && bAddToDB)
  100. {
  101. theApp.RefreshView();
  102. }
  103. else if(bRet && m_importCount == 1 && bPutOnClipboard)
  104. {
  105. PlaceFormatsOnclipboard();
  106. }
  107. else if(bRet && bPutOnClipboard)
  108. {
  109. PlaceCF_TEXT_AND_CF_UNICODETEXT_OnClipboard(csCF_TEXT, csCF_UNICODETEXT);
  110. }
  111. }
  112. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  113. return bRet;
  114. }
  115. bool CClip_ImportExport::PlaceCF_TEXT_AND_CF_UNICODETEXT_OnClipboard(CStringA &csCF_TEXT, CStringW &csCF_UNICODETEXT)
  116. {
  117. bool bRet = false;
  118. if(OpenClipboard(theApp.m_MainhWnd))
  119. {
  120. EmptyClipboard();
  121. if(csCF_TEXT.IsEmpty() == FALSE)
  122. {
  123. long lLen = csCF_TEXT.GetLength();
  124. HGLOBAL hGlobal = NewGlobalP(csCF_TEXT.GetBuffer(lLen), lLen+1);
  125. csCF_TEXT.ReleaseBuffer();
  126. SetClipboardData(CF_TEXT, hGlobal);
  127. bRet = true;
  128. }
  129. if(csCF_UNICODETEXT.IsEmpty() == FALSE)
  130. {
  131. long lLen = csCF_UNICODETEXT.GetLength() * sizeof(wchar_t);
  132. HGLOBAL hGlobal = NewGlobalP(csCF_UNICODETEXT.GetBuffer(lLen), lLen+1);
  133. csCF_UNICODETEXT.ReleaseBuffer();
  134. SetClipboardData(CF_UNICODETEXT, hGlobal);
  135. bRet = true;
  136. }
  137. CloseClipboard();
  138. }
  139. else
  140. {
  141. Log(_T("Error opening clipboard"));
  142. }
  143. return bRet;
  144. }
  145. bool CClip_ImportExport::PlaceFormatsOnclipboard()
  146. {
  147. bool bRet = false;
  148. if(OpenClipboard(theApp.m_MainhWnd))
  149. {
  150. EmptyClipboard();
  151. INT_PTR formatCount = m_Formats.GetSize();
  152. for(int i = 0; i < formatCount; i++)
  153. {
  154. CClipFormat *pCF;
  155. pCF = &m_Formats.ElementAt(i);
  156. LPVOID Data = (LPVOID)GlobalLock(pCF->m_hgData);
  157. if(Data)
  158. {
  159. HGLOBAL hGlobal = NewGlobalP(Data, GlobalSize(pCF->m_hgData));
  160. if(hGlobal)
  161. {
  162. SetClipboardData(pCF->m_cfType, hGlobal);
  163. }
  164. GlobalUnlock(pCF->m_hgData);
  165. }
  166. }
  167. CloseClipboard();
  168. bRet = true;
  169. }
  170. else
  171. {
  172. Log(_T("PlaceFormatsOnclipboard::Error opening clipboard"));
  173. }
  174. return bRet;
  175. }
  176. bool CClip_ImportExport::ImportFromSqliteV1(CppSQLite3DB &db, CppSQLite3Query &qMain)
  177. {
  178. try
  179. {
  180. //Load the Main Table
  181. m_Desc = qMain.getStringField(_T("mText"));
  182. long lID = qMain.getIntField(_T("lID"));
  183. //Load the data Table
  184. CClipFormat cf;
  185. HGLOBAL hGlobal = 0;
  186. m_Formats.RemoveAll();
  187. CString csSQL;
  188. csSQL.Format(
  189. _T("SELECT Data.* FROM Data ")
  190. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  191. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), lID);
  192. CppSQLite3Query qData = db.execQuery(csSQL);
  193. while(qData.eof() == false)
  194. {
  195. cf.m_cfType = GetFormatID(qData.getStringField(_T("strClipBoardFormat")));
  196. long lOriginalSize = qData.getIntField(_T("lOriginalSize"));
  197. int nDataLen = 0;
  198. const unsigned char *cData = qData.getBlobField(_T("ooData"), nDataLen);
  199. if(cData != NULL)
  200. {
  201. Bytef *pUnZippedData = new Bytef[lOriginalSize];
  202. if(pUnZippedData)
  203. {
  204. //the data in the exported file is compressed so uncompress it now
  205. int nRet = uncompress(pUnZippedData, (uLongf *)&lOriginalSize, (Bytef *)cData, nDataLen);
  206. if(nRet == Z_OK)
  207. {
  208. cf.m_hgData = NewGlobalP(pUnZippedData, lOriginalSize);
  209. if(cf.m_hgData)
  210. {
  211. m_Formats.Add(cf);
  212. cf.m_hgData = NULL; //m_format owns m_hgData now
  213. }
  214. else
  215. {
  216. Log(StrF(_T("Error allocating NewGlobalP size = %d"), lOriginalSize));
  217. ASSERT(FALSE);
  218. }
  219. }
  220. else
  221. {
  222. Log(_T("Error uncompressing data from zlib"));
  223. ASSERT(FALSE);
  224. }
  225. delete []pUnZippedData;
  226. pUnZippedData = NULL;
  227. }
  228. else
  229. {
  230. Log(StrF(_T("Error allocating memory to unzip size = %d"), lOriginalSize));
  231. ASSERT(FALSE);
  232. }
  233. }
  234. qData.nextRow();
  235. }
  236. }
  237. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  238. return m_Formats.GetSize() > 0;
  239. }
  240. bool CClip_ImportExport::Append_CF_TEXT_AND_CF_UNICODETEXT(CStringA &csCF_TEXT, CStringW &csCF_UNICODETEXT)
  241. {
  242. bool bRet = false;
  243. CClipFormat *pCF;
  244. INT_PTR count = m_Formats.GetSize();
  245. for(int i = 0; i < count; i++)
  246. {
  247. pCF = &m_Formats.ElementAt(i);
  248. switch(pCF->m_cfType)
  249. {
  250. case CF_TEXT:
  251. {
  252. if(csCF_TEXT.IsEmpty() == FALSE)
  253. csCF_TEXT += "\r\n";
  254. csCF_TEXT += pCF->GetAsCStringA();
  255. bRet = true;
  256. }
  257. break;
  258. case CF_UNICODETEXT:
  259. {
  260. if(csCF_UNICODETEXT.IsEmpty() == FALSE)
  261. csCF_UNICODETEXT += _T("\r\n");
  262. csCF_UNICODETEXT += pCF->GetAsCString();;
  263. bRet = true;
  264. }
  265. break;
  266. }
  267. }
  268. return bRet;
  269. }