| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 | 
							- #include "stdafx.h"
 
- #include "CP_Main.h"
 
- #include ".\clip_importexport.h"
 
- #include "shared/TextConvert.h"
 
- #include "sqlite/CppSQLite3.h"
 
- #include "zlib/zlib.h"
 
- #include "Misc.h"
 
- #define CURRENT_EXPORT_VERSION 1
 
- CClip_ImportExport::CClip_ImportExport(void) :
 
- 	m_importCount(0)
 
- {
 
- }
 
- CClip_ImportExport::~CClip_ImportExport(void)
 
- {
 
- }
 
- bool CClip_ImportExport::ExportToSqliteDB(CppSQLite3DB &db)
 
- {
 
- 	bool bRet = false;
 
- 	try
 
- 	{
 
- 		//Add to Main Table
 
- 		m_Desc.Replace(_T("'"), _T("''"));
 
- 		db.execDMLEx(_T("insert into Main values(NULL, %d, '%s');"), CURRENT_EXPORT_VERSION, m_Desc);
 
- 		long lId = (long)db.lastRowId();
 
- 		//Add to Data table
 
- 		CClipFormat* pCF;
 
- 		CppSQLite3Statement stmt = db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?, ?);"));
 
- 		for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
 
- 		{
 
- 			pCF = & m_Formats.ElementAt(i);
 
- 			stmt.bind(1, lId);
 
- 			stmt.bind(2, GetFormatName(pCF->m_cfType));
 
- 			INT_PTR originalSize = GlobalSize(pCF->m_hgData);
 
- 			stmt.bind(3, (int)originalSize);
 
- 			const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
 
- 			if(Data)
 
- 			{
 
- 				//First compress the data
 
- 				INT_PTR zippedSize = compressBound((ULONG)originalSize);
 
- 				Bytef *pZipped = new Bytef[zippedSize];
 
- 				if(pZipped)
 
- 				{				
 
- 					INT_PTR zipReturn = compress(pZipped, (uLongf *)&zippedSize, (const Bytef *)Data, (ULONG)originalSize);
 
- 					if(zipReturn == Z_OK)
 
- 					{
 
- 						stmt.bind(4, pZipped, (int)zippedSize);
 
- 					}
 
- 					delete []pZipped;
 
- 					pZipped = NULL;
 
- 				}
 
- 			}
 
- 			GlobalUnlock(pCF->m_hgData);
 
- 			stmt.execDML();
 
- 			stmt.reset();
 
- 			m_Formats.RemoveAt(i);
 
- 		}
 
- 		bRet = true;
 
- 	}
 
- 	CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
 
- 	return bRet;
 
- }
 
- bool CClip_ImportExport::ImportFromSqliteDB(CppSQLite3DB &db, bool bAddToDB, bool bPutOnClipboard)
 
- {
 
- 	bool bRet = false;
 
- 	CStringA csCF_TEXT;
 
- 	CStringW csCF_UNICODETEXT;
 
- 	try
 
- 	{
 
- 		CppSQLite3Query q = db.execQuery(_T("Select * from Main order by lId DESC"));
 
- 		while(q.eof() == false)
 
- 		{
 
- 			Clear();
 
- 			int nVersion = q.getIntField(_T("lVersion"));
 
- 			if(nVersion == 1)
 
- 			{
 
- 				if(ImportFromSqliteV1(db, q))
 
- 				{
 
- 					if(bAddToDB)
 
- 					{
 
- 						MakeLatestOrder();
 
- 						AddToDB(true);
 
- 						bRet = true;
 
- 					}
 
- 					else if(bPutOnClipboard)
 
- 					{
 
- 						bRet = true;
 
- 					}
 
- 				}
 
- 			}
 
- 			m_importCount++;
 
- 			//If putting on the clipboard and there are multiple
 
- 			//then append cf_text and cf_unicodetext
 
- 			if(bPutOnClipboard)
 
- 			{
 
- 				Append_CF_TEXT_AND_CF_UNICODETEXT(csCF_TEXT, csCF_UNICODETEXT);
 
- 			}
 
- 			q.nextRow();
 
- 		}
 
- 		if(bRet && bAddToDB)
 
- 		{
 
- 			theApp.RefreshView();
 
- 		}
 
- 		else if(bRet && m_importCount == 1 && bPutOnClipboard)
 
- 		{
 
- 			PlaceFormatsOnclipboard();
 
- 		}
 
- 		else if(bRet && bPutOnClipboard)
 
- 		{
 
- 			PlaceCF_TEXT_AND_CF_UNICODETEXT_OnClipboard(csCF_TEXT, csCF_UNICODETEXT);
 
- 		}
 
- 	}
 
- 	CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
 
- 	return bRet;
 
- }
 
- bool CClip_ImportExport::PlaceCF_TEXT_AND_CF_UNICODETEXT_OnClipboard(CStringA &csCF_TEXT, CStringW &csCF_UNICODETEXT)
 
- {
 
- 	bool bRet = false;
 
- 	if(OpenClipboard(theApp.m_MainhWnd))
 
- 	{
 
- 		EmptyClipboard();
 
- 		if(csCF_TEXT.IsEmpty() == FALSE)
 
- 		{
 
- 			long lLen = csCF_TEXT.GetLength();
 
- 			HGLOBAL hGlobal = NewGlobalP(csCF_TEXT.GetBuffer(lLen), lLen+1);
 
- 			csCF_TEXT.ReleaseBuffer();
 
- 			SetClipboardData(CF_TEXT, hGlobal);
 
- 			bRet = true;
 
- 		}
 
- 		if(csCF_UNICODETEXT.IsEmpty() == FALSE)
 
- 		{
 
- 			long lLen = csCF_UNICODETEXT.GetLength() * sizeof(wchar_t);
 
- 			HGLOBAL hGlobal = NewGlobalP(csCF_UNICODETEXT.GetBuffer(lLen), lLen+1);
 
- 			csCF_UNICODETEXT.ReleaseBuffer();
 
- 			SetClipboardData(CF_UNICODETEXT, hGlobal);
 
- 			bRet = true;
 
- 		}
 
- 		CloseClipboard();
 
- 	}
 
- 	else
 
- 	{
 
- 		Log(_T("Error opening clipboard"));
 
- 	}
 
- 	return bRet;
 
- }
 
- bool CClip_ImportExport::PlaceFormatsOnclipboard()
 
- {
 
- 	bool bRet = false;
 
- 	if(OpenClipboard(theApp.m_MainhWnd))
 
- 	{
 
- 		EmptyClipboard();
 
- 		INT_PTR formatCount = m_Formats.GetSize();
 
- 		for(int i = 0; i < formatCount; i++)
 
- 		{
 
- 			CClipFormat *pCF;
 
- 			pCF = &m_Formats.ElementAt(i);
 
- 			LPVOID Data = (LPVOID)GlobalLock(pCF->m_hgData);
 
- 			if(Data)
 
- 			{
 
- 				HGLOBAL hGlobal = NewGlobalP(Data, GlobalSize(pCF->m_hgData));			
 
- 				if(hGlobal)
 
- 				{
 
- 					SetClipboardData(pCF->m_cfType, hGlobal);
 
- 				}
 
- 				GlobalUnlock(pCF->m_hgData);
 
- 			}
 
- 		}
 
- 		CloseClipboard();
 
- 		bRet = true;
 
- 	}
 
- 	else
 
- 	{
 
- 		Log(_T("PlaceFormatsOnclipboard::Error opening clipboard"));
 
- 	}
 
- 	return bRet;
 
- }
 
- bool CClip_ImportExport::ImportFromSqliteV1(CppSQLite3DB &db, CppSQLite3Query &qMain)
 
- {
 
- 	try
 
- 	{
 
- 		//Load the Main Table
 
- 		m_Desc = qMain.getStringField(_T("mText"));
 
- 		long lID = qMain.getIntField(_T("lID"));
 
- 		//Load the data Table
 
- 		CClipFormat cf;
 
- 		HGLOBAL hGlobal = 0;
 
- 		m_Formats.RemoveAll();
 
- 		CString csSQL;
 
- 		csSQL.Format(
 
- 			_T("SELECT Data.* FROM Data ")
 
- 			_T("INNER JOIN Main ON Main.lID = Data.lParentID ")
 
- 			_T("WHERE Main.lID = %d ORDER BY Data.lID desc"), lID);
 
- 		CppSQLite3Query qData = db.execQuery(csSQL);
 
- 		while(qData.eof() == false)
 
- 		{
 
- 			cf.m_cfType = GetFormatID(qData.getStringField(_T("strClipBoardFormat")));
 
- 			long lOriginalSize = qData.getIntField(_T("lOriginalSize"));
 
- 			int nDataLen = 0;
 
- 			const unsigned char *cData = qData.getBlobField(_T("ooData"), nDataLen);
 
- 			if(cData != NULL)
 
- 			{
 
- 				Bytef *pUnZippedData = new Bytef[lOriginalSize];
 
- 				if(pUnZippedData)
 
- 				{
 
- 					//the data in the exported file is compressed so uncompress it now
 
- 					int nRet = uncompress(pUnZippedData, (uLongf *)&lOriginalSize, (Bytef *)cData, nDataLen);
 
- 					if(nRet == Z_OK)
 
- 					{
 
- 						cf.m_hgData = NewGlobalP(pUnZippedData, lOriginalSize);
 
- 						if(cf.m_hgData)
 
- 						{
 
- 							m_Formats.Add(cf);
 
- 							cf.m_hgData = NULL; //m_format owns m_hgData now
 
- 						}
 
- 						else
 
- 						{
 
- 							Log(StrF(_T("Error allocating NewGlobalP size = %d"), lOriginalSize));
 
- 							ASSERT(FALSE);
 
- 						}
 
- 					}
 
- 					else
 
- 					{
 
- 						Log(_T("Error uncompressing data from zlib"));
 
- 						ASSERT(FALSE);
 
- 					}
 
- 					delete []pUnZippedData;
 
- 					pUnZippedData = NULL;
 
- 				}
 
- 				else
 
- 				{
 
- 					Log(StrF(_T("Error allocating memory to unzip size = %d"), lOriginalSize));
 
- 					ASSERT(FALSE);
 
- 				}
 
- 			}
 
- 			qData.nextRow();
 
- 		}
 
- 	}
 
- 	CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
 
- 	return m_Formats.GetSize() > 0;
 
- }
 
- bool CClip_ImportExport::Append_CF_TEXT_AND_CF_UNICODETEXT(CStringA &csCF_TEXT, CStringW &csCF_UNICODETEXT)
 
- {
 
- 	bool bRet = false;
 
- 	CClipFormat *pCF;
 
- 	INT_PTR count = m_Formats.GetSize();
 
- 	for(int i = 0; i < count; i++)
 
- 	{
 
- 		pCF = &m_Formats.ElementAt(i);
 
- 		switch(pCF->m_cfType)
 
- 		{
 
- 		case CF_TEXT:
 
- 			{	
 
- 				const char *Data = (const char *)GlobalLock(pCF->m_hgData);
 
- 				if(Data)
 
- 				{
 
- 					if(csCF_TEXT.IsEmpty() == FALSE)
 
- 						csCF_TEXT += "\r\n";
 
- 					csCF_TEXT += Data;
 
- 					bRet = true;
 
- 					GlobalUnlock(pCF->m_hgData);
 
- 				}
 
- 			}
 
- 			break;
 
- 		case CF_UNICODETEXT:
 
- 			{	
 
- 				const wchar_t *Data = (const wchar_t *)GlobalLock(pCF->m_hgData);
 
- 				if(Data)
 
- 				{
 
- 					if(csCF_UNICODETEXT.IsEmpty() == FALSE)
 
- 						csCF_UNICODETEXT += _T("\r\n");
 
- 					csCF_UNICODETEXT += Data;
 
- 					bRet = true;
 
- 					GlobalUnlock(pCF->m_hgData);
 
- 				}
 
- 			}
 
- 			break;
 
- 		}
 
- 	}
 
- 	return bRet;
 
- }
 
 
  |