123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- // DatabaseUtilites.cpp: implementation of the CDatabaseUtilites class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CP_Main.h"
- #include "DatabaseUtilities.h"
- #include "ProcessPaste.h"
- #include <io.h>
- #include "AccessToSqlite.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- BOOL CreateBackup(CString csPath)
- {
- CString csOriginal;
- int count = 0;
- // create a backup of the existing database
- do
- {
- count++;
- csOriginal = csPath + StrF(_T(".%03d"), count);
- // in case of some weird infinite loop
- if( count > 50 )
- {
- ASSERT(0);
- return FALSE;
- }
- } while( !::CopyFile(csPath, csOriginal, TRUE));
-
- return TRUE;
- }
- CString GetDBName()
- {
- return CGetSetOptions::GetDBPath();
- }
- CString GetOLDDefaultDBName()
- {
- CString csDefaultPath;
- LPMALLOC pMalloc;
- if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
- {
- LPITEMIDLIST pidlPrograms;
- SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
- TCHAR string[MAX_PATH];
- SHGetPathFromIDList(pidlPrograms, string);
- pMalloc->Free(pidlPrograms);
- pMalloc->Release();
- csDefaultPath = string;
- csDefaultPath += "\\Ditto\\";
- csDefaultPath += "DittoDB.mdb";
- }
- return csDefaultPath;
- }
- CString GetDefaultDBName()
- {
- CString csDefaultPath = _T("c:\\program files\\Ditto\\");
- if(g_Opt.m_bU3)
- {
- csDefaultPath = CGetSetOptions::GetPath(PATH_DATABASE);
- }
- else
- {
- LPMALLOC pMalloc;
-
- if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
- {
- LPITEMIDLIST pidlPrograms;
-
- SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
-
- TCHAR string[MAX_PATH];
- SHGetPathFromIDList(pidlPrograms, string);
-
- pMalloc->Free(pidlPrograms);
- pMalloc->Release();
-
- csDefaultPath = string;
- }
- FIX_CSTRING_PATH(csDefaultPath);
- csDefaultPath += "Ditto\\";
- }
- if(FileExists(csDefaultPath) == FALSE)
- CreateDirectory(csDefaultPath, NULL);
- CString csTempName = csDefaultPath + "Ditto.db";
- int i = 1;
- while(FileExists(csTempName))
- {
- csTempName.Format(_T("%sDitto_%d.db"), csDefaultPath, i);
- i++;
- }
- csDefaultPath = csTempName;
-
- return csDefaultPath;
- }
- BOOL CheckDBExists(CString csDBPath)
- {
- //If this is the first time running this version then convert the old database to the new db
- if(csDBPath.IsEmpty() && g_Opt.m_bU3 == false)
- {
- csDBPath = GetDefaultDBName();
- if(FileExists(csDBPath) == FALSE)
- {
- CString csOldDB = CGetSetOptions::GetDBPathOld();
- if(csOldDB.IsEmpty())
- {
- csOldDB = GetOLDDefaultDBName();
- }
- if(FileExists(csOldDB))
- {
- //create the new sqlite db
- CreateDB(csDBPath);
- CAccessToSqlite Convert;
- Convert.ConvertDatabase(csDBPath, csOldDB);
- }
- }
- }
- BOOL bRet = FALSE;
- if(FileExists(csDBPath) == FALSE)
- {
- csDBPath = GetDefaultDBName();
- // -- create a new one
- bRet = CreateDB(csDBPath);
- }
- else
- {
- if(ValidDB(csDBPath) == FALSE)
- {
- //Db existed but was bad
- CString csMarkAsBad;
-
- csMarkAsBad = csDBPath;
- csMarkAsBad.Replace(_T("."), _T("_BAD."));
-
- CString csPath = GetDefaultDBName();
-
- CString cs;
- cs.Format(_T("%s \"%s\",\n")
- _T("%s \"%s\",\n")
- _T("%s,\n")
- _T("\"%s\""),
- theApp.m_Language.GetString("Database_Format", "Unrecognized Database Format"),
- csDBPath,
- theApp.m_Language.GetString("File_Renamed", "the file will be renamed"),
- csMarkAsBad,
- theApp.m_Language.GetString("New_Database", "and a new database will be created"),
- csPath);
-
- AfxMessageBox(cs);
-
- CFile::Rename(csDBPath, csMarkAsBad);
- csDBPath = csPath;
-
- bRet = CreateDB(csDBPath);
- }
- else
- bRet = TRUE;
- }
- if(bRet)
- {
- bRet = OpenDatabase(csDBPath);
- }
-
- return bRet;
- }
- BOOL OpenDatabase(CString csDB)
- {
- try
- {
- theApp.m_db.close();
- theApp.m_db.open(csDB);
- CGetSetOptions::SetDBPath(csDB);
- return TRUE;
- }
- CATCH_SQLITE_EXCEPTION
- return FALSE;
- }
- BOOL ValidDB(CString csPath, BOOL bUpgrade)
- {
- try
- {
- CppSQLite3DB db;
- db.open(csPath);
- db.execQuery(_T("SELECT lID, lDate, mText, lShortCut, lDontAutoDelete, ")
- _T("CRC, bIsGroup, lParentID, QuickPasteText ")
- _T("FROM Main"));
- db.execQuery(_T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data"));
- db.execQuery(_T("SELECT lID, TypeText FROM Types"));
- //This was added later so try to add each time and catch the exception here
- try
- {
- db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
- _T("BEGIN\n")
- _T("DELETE FROM Data WHERE lParentID = old.lID;\n")
- _T("END\n"));
- }
- catch(CppSQLite3Exception& e)
- {
- e.errorCode();
- }
- }
- CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
- return TRUE;
- }
- BOOL CreateDB(CString csPath)
- {
- try
- {
- CppSQLite3DB db;
- db.open(csPath);
- db.execDML(_T("CREATE TABLE Main(")
- _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
- _T("lDate INTEGER, ")
- _T("mText TEXT, ")
- _T("lShortCut INTEGER, ")
- _T("lDontAutoDelete INTEGER, ")
- _T("CRC INTEGER, ")
- _T("bIsGroup INTEGER, ")
- _T("lParentID INTEGER, ")
- _T("QuickPasteText TEXT);"));
- db.execDML(_T("CREATE TABLE Data(")
- _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
- _T("lParentID INTEGER, ")
- _T("strClipBoardFormat TEXT, ")
- _T("ooData BLOB);"));
- db.execDML(_T("CREATE TABLE Types(")
- _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
- _T("TypeText TEXT)"));
- db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
- db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
- db.execDML(_T("CREATE INDEX Main_Date on Main(lDate DESC)"));
- db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
- _T("BEGIN\n")
- _T("DELETE FROM Data WHERE lParentID = old.lID;\n")
- _T("END\n"));
- db.close();
- }
- CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
- return TRUE;
- }
- BOOL CompactDatabase()
- {
- // if(!theApp.CloseDB())
- // return FALSE;
- //
- // CString csDBName = GetDBName();
- // CString csTempDBName = csDBName;
- // csTempDBName.Replace(".mdb", "TempDBName.mdb");
- //
- // //Compact the database
- // try
- // {
- // CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
- // }
- // catch(CDaoException* e)
- // {
- // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- // DeleteFile(csTempDBName);
- // e->Delete();
- // return FALSE;
- // }
- // catch(CMemoryException* e)
- // {
- // AfxMessageBox("Memory Exception");
- // DeleteFile(csTempDBName);
- // e->Delete();
- // return FALSE;
- // }
- //
- // //Since compacting the database creates a new db delete the old one and replace it
- // //with the compacted db
- // if(DeleteFile(csDBName))
- // {
- // try
- // {
- // CFile::Rename(csTempDBName, csDBName);
- // }
- // catch(CFileException *e)
- // {
- // e->ReportError();
- // e->Delete();
- // return FALSE;
- // }
- // }
- // else
- // AfxMessageBox("Error Compacting Database");
-
- return TRUE;
- }
- BOOL RepairDatabase()
- {
- // if(!theApp.CloseDB())
- // return FALSE;
-
- // try
- // {
- // CDaoWorkspace::RepairDatabase(GetDBName());
- // }
- // catch(CDaoException *e)
- // {
- // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- // e->Delete();
- // return FALSE;
- // }
-
- return TRUE;
- }
- BOOL RemoveOldEntries()
- {
- try
- {
- if(CGetSetOptions::GetCheckForMaxEntries())
- {
- long lMax = CGetSetOptions::GetMaxEntries();
- if(lMax >= 0)
- {
- CClipIDs IDs;
-
- CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, lShortCut, lDontAutoDelete FROM Main ORDER BY lDate DESC LIMIT -1 OFFSET %d"), lMax);
- while(q.eof() == false)
- {
- //Only delete entries that have no shortcut and don't have the flag set
- if(q.getIntField(_T("lShortCut")) == 0 && q.getIntField(_T("lDontAutoDelete")) == 0)
- IDs.Add(q.getIntField(_T("lID")));
- q.nextRow();
- }
- IDs.DeleteIDs();
- }
- }
-
- if(CGetSetOptions::GetCheckForExpiredEntries())
- {
- long lExpire = CGetSetOptions::GetExpiredEntries();
-
- if(lExpire)
- {
- CTime now = CTime::GetCurrentTime();
- now -= CTimeSpan(lExpire, 0, 0, 0);
-
- CClipIDs IDs;
-
- CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main ")
- _T("WHERE lDate < %d AND ")
- _T("lShortCut = 0 AND lDontAutoDelete = 0"), now.GetTime());
- while(q.eof() == false)
- {
- IDs.Add(q.getIntField(_T("lID")));
- q.nextRow();
- }
-
- IDs.DeleteIDs();
- }
- }
- }
- CATCH_SQLITE_EXCEPTION
-
- return TRUE;
- }
- BOOL EnsureDirectory(CString csPath)
- {
- TCHAR drive[_MAX_DRIVE];
- TCHAR dir[_MAX_DIR];
- TCHAR fname[_MAX_FNAME];
- TCHAR ext[_MAX_EXT];
-
- SPLITPATH(csPath, drive, dir, fname, ext);
-
- CString csDir(drive);
- csDir += dir;
-
- if(FileExists(csDir) == FALSE)
- {
- if(CreateDirectory(csDir, NULL))
- return TRUE;
- }
- else
- return TRUE;
-
- return FALSE;
- }
- // BOOL RunZippApp(CString csCommandLine)
- // {
- // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
- // FIX_CSTRING_PATH(csLocalPath);
- //
- // CString csZippApp = GETENV(_T("U3_DEVICE_EXEC_PATH"));
- // FIX_CSTRING_PATH(csZippApp);
- // csZippApp += "7za.exe";
- //
- // csZippApp += " ";
- // csZippApp += csCommandLine;
- //
- // Log(csZippApp);
- //
- // STARTUPINFO StartupInfo;
- // PROCESS_INFORMATION ProcessInformation;
- //
- // ZeroMemory(&StartupInfo, sizeof(StartupInfo));
- // StartupInfo.cb = sizeof(StartupInfo);
- // ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
- //
- // StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
- // StartupInfo.wShowWindow = SW_HIDE;
- //
- // BOOL bRet = CreateProcess(NULL, csZippApp.GetBuffer(csZippApp.GetLength()), NULL, NULL, FALSE,
- // CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, csLocalPath,
- // &StartupInfo, &ProcessInformation);
- //
- // if(bRet)
- // {
- // WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
- //
- // DWORD dwExitCode;
- // GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
- //
- // CString cs;
- // cs.Format(_T("Exit code from unzip = %d"), dwExitCode);
- // Log(cs);
- //
- // if(dwExitCode != 0)
- // {
- // bRet = FALSE;
- // }
- // }
- // else
- // {
- // bRet = FALSE;
- // Log(_T("Create Process Failed"));
- // }
- //
- // csZippApp.ReleaseBuffer();
- //
- // return bRet;
- // }
- // BOOL CopyDownDatabase()
- // {
- // BOOL bRet = FALSE;
- //
- // CString csZippedPath = GETENV(_T("U3_APP_DATA_PATH"));
- // FIX_CSTRING_PATH(csZippedPath);
- //
- // CString csUnZippedPath = csZippedPath;
- // csUnZippedPath += "Ditto.db";
- //
- // csZippedPath += "Ditto.7z";
- //
- // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
- // FIX_CSTRING_PATH(csLocalPath);
- //
- // if(FileExists(csZippedPath))
- // {
- // CString csCommandLine;
- //
- // //e = extract
- // //surround command line arguments with quotes
- // //-aoa = overight files with extracted files
- //
- // csCommandLine += "e ";
- // csCommandLine += "\"";
- // csCommandLine += csZippedPath;
- // csCommandLine += "\"";
- // csCommandLine += " -o";
- // csCommandLine += "\"";
- // csCommandLine += csLocalPath;
- // csCommandLine += "\"";
- // csCommandLine += " -aoa";
- //
- // bRet = RunZippApp(csCommandLine);
- //
- // csLocalPath += "Ditto.db";
- // }
- // else if(FileExists(csUnZippedPath))
- // {
- // csLocalPath += "Ditto.db";
- // bRet = CopyFile(csUnZippedPath, csLocalPath, FALSE);
- // }
- //
- // if(FileExists(csLocalPath) == FALSE)
- // {
- // Log(_T("Failed to copy files from device zip file"));
- // }
- //
- // g_Opt.nLastDbWriteTime = GetLastWriteTime(csLocalPath);
- //
- // return bRet;
- // }
- //BOOL CopyUpDatabase()
- //{
- // CStringA csZippedPath = "C:\\";//getenv("U3_APP_DATA_PATH");
- // FIX_CSTRING_PATH(csZippedPath);
- // csZippedPath += "Ditto.zip";
- // CStringA csLocalPath = GetDBName();//getenv("U3_HOST_EXEC_PATH");
- // //FIX_CSTRING_PATH(csLocalPath);
- // //csLocalPath += "Ditto.db";
- //
- // CZipper Zip;
- //
- // if(Zip.OpenZip(csZippedPath))
- // {
- // Zip.AddFileToZip(csLocalPath);
- // }
- //
- // return TRUE;
- //}
|