Exemplo n.º 1
0
void COptionsGeneral::OnGetPath() 
{
	OPENFILENAME	FileName;
	TCHAR			szFileName[400];
	TCHAR			szDir[400];

	memset(&FileName, 0, sizeof(FileName));
	memset(szFileName, 0, sizeof(szFileName));
	memset(&szDir, 0, sizeof(szDir));
	FileName.lStructSize = sizeof(FileName);
	FileName.lpstrTitle = _T("Open Database");
	FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
	FileName.nMaxFile = 400;
	FileName.lpstrFile = szFileName;
	FileName.lpstrInitialDir = szDir;
	FileName.lpstrFilter = _T("Ditto Databases (*.db; *.mdb)\0*.db;*.mdb\0\0");
	FileName.lpstrDefExt = _T("db");

	if(GetOpenFileName(&FileName) == 0)
		return;

	CString csPath(FileName.lpstrFile);
	CPath path(FileName.lpstrFile);

	if(path.GetExtension() == _T("mdb"))
	{
		CString cs;

		cs.Format(_T("The database '%s' must be converted to a Sqlite Database (Version 3 format).\n\nConvert database?"), FileName.lpstrFile);
		if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDNO)
			return;

		CString csNewDBPath = path.RemoveExtension();

		//Make sure the db name is unique
		CString csTempName;
		csTempName.Format(_T("%s.db"), csNewDBPath);
		int i = 1;
		while(FileExists(csTempName))
		{
			csTempName.Format(_T("%s_%d.db"), csNewDBPath, i);
			i++;
		}
		csNewDBPath = csTempName;
		
		CreateDB(csNewDBPath);

		CAccessToSqlite Convert;
		if(Convert.ConvertDatabase(csNewDBPath, FileName.lpstrFile))
		{
			csPath = csNewDBPath;
		}
		else
		{
			MessageBox(_T("Error converting database."), _T("Ditto"), MB_OK);
			DeleteFile(csNewDBPath);
			return;
		}
	}

	if(FileExists(csPath))
	{
		if(ValidDB(csPath) == FALSE)
		{
			MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
			m_ePath.SetFocus();
		}
		else
		{
			m_ePath.SetWindowText(csPath);	
		}
	}
	else
	{
		m_ePath.SetWindowText(csPath);
	}
}
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 && CGetSetOptions::GetIsPortableDitto() == 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();

		nsPath::CPath FullPath(csDBPath);
		CString csPath = FullPath.GetPath().GetStr();
		if(csPath.IsEmpty() == false && FileExists(csDBPath) == FALSE)
		{
			CreateDirectory(csPath, NULL);
		}

		// -- 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;
}