Index: src/ch/AppHelper.cpp =================================================================== diff -u -rc435ab507c8b8280264188b49e9ada56d46c0261 -r8068e0c351055554340ac9755d1bc846893bf2b8 --- src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision c435ab507c8b8280264188b49e9ada56d46c0261) +++ src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision 8068e0c351055554340ac9755d1bc846893bf2b8) @@ -37,225 +37,111 @@ // single-instance protection m_bFirstInstance=true; - m_hMutex=NULL; + m_hMutex=nullptr; } CAppHelper::~CAppHelper() { if (m_hMutex) ReleaseMutex(m_hMutex); - delete [] m_pszProgramPath; delete [] m_pszProgramName; } -// inits mutex app protection -void CAppHelper::InitProtection() -{ - m_hMutex=CreateMutex(NULL, TRUE, CH_MUTEX_NAME); - m_bFirstInstance=(m_hMutex != NULL && GetLastError() != ERROR_ALREADY_EXISTS); -} - -// retrieves application path void CAppHelper::RetrievePaths() { // try to find '\\' in path to see if this is only exe name or fully qualified path -#ifdef _UNICODE - TCHAR* pszArgv = __wargv[0]; -#else - TCHAR* pszArgv = __argv[0]; -#endif + TCHAR* pszArgv = __wargv[ 0 ]; - TCHAR* pszName=_tcsrchr(pszArgv, _T('\\')); - if (pszName != NULL) + TCHAR* pszName = _tcsrchr(pszArgv, _T('\\')); + if(pszName != nullptr) { // copy name - m_pszProgramName=new TCHAR[_tcslen(pszName+1)+1]; - _tcscpy(m_pszProgramName, pszName+1); - - // path - UINT uiSize=(UINT)(pszName-pszArgv); - m_pszProgramPath=new TCHAR[uiSize+1]; - _tcsncpy(m_pszProgramPath, pszArgv, uiSize); - m_pszProgramPath[uiSize]=_T('\0'); + m_pszProgramName = new TCHAR[ _tcslen(pszName + 1) + 1 ]; + _tcscpy(m_pszProgramName, pszName + 1); } else { // copy name - m_pszProgramName=new TCHAR[_tcslen(pszArgv)+1]; + m_pszProgramName = new TCHAR[ _tcslen(pszArgv) + 1 ]; _tcscpy(m_pszProgramName, pszArgv); - - // path - TCHAR szPath[_MAX_PATH]; - UINT uiSize=GetCurrentDirectory(_MAX_PATH, szPath); - _tcscat(szPath, _T("\\")); - m_pszProgramPath=new TCHAR[uiSize+2]; - _tcsncpy(m_pszProgramPath, szPath, uiSize+2); } } +// inits mutex app protection +void CAppHelper::InitProtection() +{ + m_hMutex=CreateMutex(nullptr, TRUE, CH_MUTEX_NAME); + m_bFirstInstance=(m_hMutex != nullptr && GetLastError() != ERROR_ALREADY_EXISTS); +} + void CAppHelper::RetrieveAppInfo() { m_pszAppName = _T(PRODUCT_NAME); m_pszAppNameVer = PRODUCT_FULL_VERSION_T; m_pszAppVersion = _T(PRODUCT_VERSION); } -// internal func - safe getting special folder locations -UINT CAppHelper::GetFolderLocation(int iFolder, PTSTR pszBuffer) +bool CAppHelper::GetProgramDataPath(CString& rStrPath) { - LPITEMIDLIST piid; - HRESULT h=SHGetSpecialFolderLocation(NULL, iFolder, &piid); - if (!SUCCEEDED(h)) - return false; + if(IsInPortableMode()) + rStrPath = m_pathProcessor.GetProgramPath(); + else + { + rStrPath = m_pathProcessor.GetAppDataPath(); + rStrPath += L"\\Copy Handler"; - // get path - BOOL bRes=SHGetPathFromIDList(piid, pszBuffer); + // make sure to create the required directories if they does not exist + if(!CreateDirectory(rStrPath, nullptr) && GetLastError() != ERROR_ALREADY_EXISTS) + return false; + } - // free piid - LPMALLOC lpm; - if (!SUCCEEDED(SHGetMalloc(&lpm))) - return 0; + // create directory for tasks + if(!CreateDirectory(rStrPath + _T("\\Tasks"), nullptr) && GetLastError() != ERROR_ALREADY_EXISTS) + return false; - lpm->Free((void*)piid); - lpm->Release(); + return true; +} - // check for error - if (!bRes) - return 0; - - // strip the last '\\' - UINT uiLen=(UINT)_tcslen(pszBuffer); - if (pszBuffer[uiLen-1] == _T('\\')) - { - pszBuffer[uiLen-1]=_T('\0'); - return uiLen-1; - } - else - return uiLen; +CString CAppHelper::ExpandPath(CString strPath) +{ + return m_pathProcessor.ExpandPath(strPath); } -// expands given path -PTSTR CAppHelper::ExpandPath(PTSTR pszString) +CString CAppHelper::GetProgramPath() const { - // check if there is need to perform all these checkings - if (pszString[0] != _T('<')) - return pszString; + return m_pathProcessor.GetProgramPath(); +} - TCHAR szStr[_MAX_PATH]; - szStr[0]=_T('\0'); - - // search for string to replace - // _T(""), _T(""), _T(""), _T(""), _T(""), - // _T(""), _T("") - if (_tcsnicmp(pszString, _T(""), 9) == 0) +bool CAppHelper::IsInPortableMode() +{ + if(!m_optPortableMode.is_initialized()) { - // get windows path - _tcsncpy(szStr, m_pszProgramPath ? m_pszProgramPath : _t(""), _MAX_PATH); - szStr[_MAX_PATH - 1] = _T('\0'); - _tcsncat(szStr, pszString+9, _MAX_PATH - _tcslen(szStr)); - szStr[_MAX_PATH - 1] = _T('\0'); + // check if the ch.ini exists in the program's directory - it is the only way we can determine portable mode + CString strPortableCfgPath = CString(m_pathProcessor.GetProgramPath()) + _T("\\ch.xml"); + if(GetFileAttributes(strPortableCfgPath) == INVALID_FILE_ATTRIBUTES) + m_optPortableMode = false; + else + m_optPortableMode = true; } - else if (_tcsnicmp(pszString, _T(""), 9) == 0) - { - // get windows path - UINT uiSize=GetWindowsDirectory(szStr, _MAX_PATH); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+9, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - else if (_tcsnicmp(pszString, _T(""), 6) == 0) // temp dir - { - // get windows path - UINT uiSize=GetTempPath(_MAX_PATH, szStr); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+6, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - else if (_tcsnicmp(pszString, _T(""), 8) == 0) // system - { - // get windows path - UINT uiSize=GetSystemDirectory(szStr, _MAX_PATH); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+8, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - else if (_tcsnicmp(pszString, _T(""), 9) == 0) // app data - { - // get windows path - UINT uiSize=GetFolderLocation(CSIDL_LOCAL_APPDATA, szStr); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+9, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - else if (_tcsnicmp(pszString, _T(""), 9) == 0) // desktop - { - // get windows path - UINT uiSize=GetFolderLocation(CSIDL_DESKTOPDIRECTORY, szStr); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+9, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - else if (_tcsnicmp(pszString, _T(""), 10) == 0) // personal... - { - // get windows path - UINT uiSize=GetFolderLocation(CSIDL_PERSONAL, szStr); - if (szStr[uiSize-1] == _T('\\')) - szStr[uiSize-1]=_T('\0'); - _tcsncat(szStr, pszString+10, _MAX_PATH - uiSize); - szStr[_MAX_PATH - 1] = _T('\0'); - } - // copy to src string - if (szStr[0] != _T('\0')) - _tcscpy(pszString, szStr); - - return pszString; + return m_optPortableMode.get(); } -bool CAppHelper::GetProgramDataPath(CString& rStrPath) -{ - HRESULT hResult = SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, rStrPath.GetBufferSetLength(_MAX_PATH)); - rStrPath.ReleaseBuffer(); - if(FAILED(hResult)) - return false; - - if(rStrPath.Right(1) != _T('\\')) - rStrPath += _T('\\'); - - // make sure to create the required directories if they does not exist - rStrPath += _T("Copy Handler"); - if(!CreateDirectory(rStrPath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) - return false; - - // create directory for tasks -// rStrPath += _T("\\Tasks"); - if(!CreateDirectory(rStrPath + _T("\\Tasks"), NULL) && GetLastError() != ERROR_ALREADY_EXISTS) - return false; - - return true; -} - bool CAppHelper::SetAutorun(bool bEnable) { // check the current key value (to avoid irritating messages from some firewall software) - HKEY hkeyRun = NULL; - LSTATUS lStatus = ERROR_SUCCESS; + HKEY hkeyRun = nullptr; CString strValue; CString strKey; DWORD dwType = REG_SZ; DWORD dwCount = _MAX_PATH * sizeof(TCHAR); - lStatus = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_QUERY_VALUE, &hkeyRun); + LSTATUS lStatus = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_QUERY_VALUE, &hkeyRun); if(lStatus != ERROR_SUCCESS) return false; - lStatus = RegQueryValueEx(hkeyRun, m_pszAppName, NULL, &dwType, (BYTE*)strValue.GetBufferSetLength(_MAX_PATH), &dwCount); + lStatus = RegQueryValueEx(hkeyRun, m_pszAppName, nullptr, &dwType, (BYTE*)strValue.GetBufferSetLength(_MAX_PATH), &dwCount); RegCloseKey(hkeyRun); if(lStatus != ERROR_SUCCESS && lStatus != ERROR_FILE_NOT_FOUND) @@ -272,7 +158,7 @@ return true; // format the data to be written to registry - strKey.Format(_T("%s\\%s"), m_pszProgramPath, m_pszProgramName); + strKey.Format(_T("%s\\%s"), (PCTSTR)m_pathProcessor.GetProgramPath(), m_pszProgramName); } else { @@ -282,7 +168,7 @@ if(bEnable) { // key exists in registry, check if the value is correct - strKey.Format(_T("%s\\%s"), m_pszProgramPath, m_pszProgramName); + strKey.Format(_T("%s\\%s"), (PCTSTR)m_pathProcessor.GetProgramPath(), m_pszProgramName); if(strValue.CompareNoCase(strKey) == 0) return true;