Skip to content

Commit

Permalink
Handle non-ascii characters in directories like cyrillic
Browse files Browse the repository at this point in the history
  • Loading branch information
d4koon committed Jul 11, 2021
1 parent 3c34fb6 commit 287444d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 101 deletions.
27 changes: 5 additions & 22 deletions WhatsappTray/AppData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DataEntryS<SBool> AppData::StartMinimized(Data::START_MINIMIZED, false, &AppData
DataEntryS<SBool> AppData::ShowUnreadMessages(Data::SHOW_UNREAD_MESSAGES, false, &AppData::SetData);
DataEntryS<SBool> AppData::CloseToTrayWithEscape(Data::CLOSE_TO_TRAY_WITH_ESCAPE, false, &AppData::SetData);
DataEntryS<SString> AppData::WhatsappStartpath(Data::WHATSAPP_STARTPATH, std::string("%userStartmenuePrograms%\\WhatsApp\\WhatsApp.lnk"), &AppData::SetData);
DataEntryS<SString> AppData::WhatsappRoamingDirectory(Data::WHATSAPP_ROAMING_DIRECTORY, Helper::GetWindowsAppDataDirectory(), &AppData::SetData);
DataEntryS<SString> AppData::WhatsappRoamingDirectory(Data::WHATSAPP_ROAMING_DIRECTORY, Helper::GetWindowsAppDataRoamingDirectory(), &AppData::SetData);

/// Initialize the dummy-value initDone with a lambda to get a static-constructor like behavior. NOTE: The disadvantage is though that we can not control the order. For example if we want to make sure that the logger inits first.
bool AppData::initDone([]()
Expand Down Expand Up @@ -87,23 +87,6 @@ std::string AppData::GetDataOrSetDefault(DataEntry& value)
*/
std::string AppData::GetAppDataFilePath()
{
//char appDataDirectory[MAX_PATH] = { 0 };
//if (SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataDirectory) != S_OK) {
// Logger::Error(MODULE_NAME "GetAppDataFilePath() - Saving app-data failed because the path could not be received.");
// MessageBoxA(NULL, MODULE_NAME "GetAppDataFilePath() - Saving app-data failed because the path could not be received.", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
// return "";
//}

//// Create data-folder if not exists.
//strcat_s(appDataDirectory, MAX_PATH, TEXT("\\WhatsappTray\\"));
//if (CreateDirectory(appDataDirectory, NULL) == NULL) {
// // We can get here, if the folder already exists -> We don't want an error for that case ...
// if (GetLastError() != ERROR_ALREADY_EXISTS) {
// Logger::Error(MODULE_NAME "GetAppDataFilePath() - Saving app-data failed because the folder could not be created.");
// MessageBoxA(NULL, MODULE_NAME "GetAppDataFilePath() - Saving app-data failed because the folder could not be created.", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
// return "";
// }
//}
std::string appDirectory = Helper::GetApplicationDirectory();

std::string appDataFilePath(appDirectory);
Expand All @@ -122,8 +105,8 @@ std::string AppData::WhatsappStartpathGet()
Helper::Replace(path, "%userStartmenuePrograms%", Helper::GetStartMenuProgramsDirectory());
Helper::Replace(path, "%UserProfile%", Helper::GetCurrentUserDirectory());
Helper::Replace(path, "%userprofile%", Helper::GetCurrentUserDirectory());
Helper::Replace(path, "%AppData%", Helper::GetCurrentUserAppData());
Helper::Replace(path, "%appdata%", Helper::GetCurrentUserAppData());
Helper::Replace(path, "%AppData%", Helper::GetWindowsAppDataRoamingDirectory());
Helper::Replace(path, "%appdata%", Helper::GetWindowsAppDataRoamingDirectory());

return path;
}
Expand All @@ -137,8 +120,8 @@ std::string AppData::WhatsappRoamingDirectoryGet()

Helper::Replace(path, "%UserProfile%", Helper::GetCurrentUserDirectory());
Helper::Replace(path, "%userprofile%", Helper::GetCurrentUserDirectory());
Helper::Replace(path, "%AppData%", Helper::GetCurrentUserAppData());
Helper::Replace(path, "%appdata%", Helper::GetCurrentUserAppData());
Helper::Replace(path, "%AppData%", Helper::GetWindowsAppDataRoamingDirectory());
Helper::Replace(path, "%appdata%", Helper::GetWindowsAppDataRoamingDirectory());

return path;
}
92 changes: 23 additions & 69 deletions WhatsappTray/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,6 @@ std::string Helper::GetApplicationDirectory()
return applicationFilePath.substr(0, found) + "\\";
}

/**
* Untested!
*/
std::wstring Helper::ToWString(const std::string& inputString)
{
const char* inputC_String = inputString.c_str();
size_t size = inputString.length() + 1;
wchar_t* outputStringBuffer = new wchar_t[size];

size_t outSize;
errno_t convertResult = mbstowcs_s(&outSize, outputStringBuffer, size, inputC_String, size - 1);

if (convertResult != NULL) {
Logger::Error("Error in mbsrtowcs(): %d", errno);
return L"";
}

std::wstring outputString = outputStringBuffer;
delete[] outputStringBuffer;

return outputString;
}

/**
* Convert a std::wstring to a std::string.
*/
std::string Helper::ToString(const std::wstring& inputString)
{
const wchar_t* inputC_String = inputString.c_str();
size_t size = inputString.length() + 1;
char* outputStringBuffer = new char[size];

size_t outSize;
errno_t convertResult = wcstombs_s(&outSize, outputStringBuffer, size, inputC_String, size - 1);

if (convertResult != NULL) {
Logger::Error("Error in mbsrtowcs(): %d", errno);
return "";
}

std::string outputString = outputStringBuffer;
delete[] outputStringBuffer;

return outputString;
}

std::wstring Helper::Utf8ToWide(const std::string& inputString)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, inputString.c_str(), (int)inputString.size(), NULL, 0);
Expand All @@ -102,6 +56,11 @@ std::wstring Helper::Utf8ToWide(const std::string& inputString)
return wstrTo;
}

/**
* Convert a std::wstring to a std::string.
*
* NOTE: Non ascii-characters will look broken when viewed in the debugger, to see the UTF-8 representation use the watch-window and add ',s8'
*/
std::string Helper::WideToUtf8(const std::wstring& inputString)
{
int size_needed = WideCharToMultiByte(CP_UTF8, 0, inputString.c_str(), (int)inputString.size(), NULL, 0, NULL, NULL);
Expand Down Expand Up @@ -181,44 +140,39 @@ std::string Helper::GetStartMenuProgramsDirectory()
MessageBoxA(NULL, "'Start Menu\\Programs' folder not found", "WhatsappTray", MB_OK);
return NULL;
}
std::string startMenuPrograms = Helper::ToString(startMenuProgramsBuffer);
std::string startMenuPrograms = Helper::WideToUtf8(startMenuProgramsBuffer);
CoTaskMemFree(startMenuProgramsBuffer);
return startMenuPrograms;
}

std::string Helper::GetWindowsAppDataDirectory()
/**
* @brief Gets the path to C:\Users\<JohnDoe>\AppData\Roaming
*/
std::string Helper::GetWindowsAppDataRoamingDirectory()
{
char appDataDirectory[MAX_PATH] = { 0 };
if (SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataDirectory) != S_OK) {
Logger::Fatal(MODULE_NAME "Init() - Could not get the AppData-directory!");
MessageBoxA(NULL, MODULE_NAME "Init() - Fatal: Could not get the AppData-directory!", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
PWSTR appDataRoamingDirectoryWide = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &appDataRoamingDirectoryWide) != S_OK) {
Logger::Fatal(MODULE_NAME "Init() - Could not get the AppDataRoaming-directory!");
MessageBoxA(NULL, MODULE_NAME "Init() - Fatal: Could not get the AppDataRoaming-directory!", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
return "";
}
return appDataDirectory;

std::string appDataRoamingDirectory = Helper::WideToUtf8(appDataRoamingDirectoryWide);
CoTaskMemFree(appDataRoamingDirectoryWide);
return appDataRoamingDirectory;
}

std::string Helper::GetCurrentUserDirectory()
{
char currentUserDirectory[MAX_PATH] = { 0 };
if (SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, currentUserDirectory) != S_OK) {
PWSTR currentUserDirectoryWide = NULL;
if (SHGetKnownFolderPath(FOLDERID_Profile, KF_FLAG_DEFAULT, NULL, &currentUserDirectoryWide) != S_OK) {
Logger::Fatal(MODULE_NAME "Init() - Could not get the CurrentUser-directory!");
MessageBoxA(NULL, MODULE_NAME "Init() - Fatal: Could not get the CurrentUser-directory!", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
return "";
}
return currentUserDirectory;
}

/**
* @brief Gets the path to C:\Users\<JohnDoe>\AppData\Roaming
*/
std::string Helper::GetCurrentUserAppData()
{
char currentUserDirectory[MAX_PATH] = { 0 };
if (SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, currentUserDirectory) != S_OK) {
Logger::Fatal(MODULE_NAME "Init() - Could not get the CurrentUser-directory!");
MessageBoxA(NULL, MODULE_NAME "Init() - Fatal: Could not get the CurrentUser-directory!", "WhatsappTray", MB_OK | MB_ICONINFORMATION);
return "";
}
std::string currentUserDirectory = Helper::WideToUtf8(currentUserDirectoryWide);
CoTaskMemFree(currentUserDirectoryWide);
return currentUserDirectory;
}

Expand Down Expand Up @@ -279,7 +233,7 @@ std::string Helper::ResolveLnk(HWND hwnd, LPCSTR lpszLinkFile)
WCHAR wsz[MAX_PATH];

// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);
MultiByteToWideChar(CP_UTF8, 0, lpszLinkFile, -1, wsz, MAX_PATH);

// Add code here to check return value from MultiByteWideChar
// for success.
Expand Down
5 changes: 1 addition & 4 deletions WhatsappTray/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class Helper

static std::string Helper::GetApplicationFilePath();
static std::string GetApplicationDirectory();
static std::wstring Helper::ToWString(const std::string& inputString);
static std::string Helper::ToString(const std::wstring& inputString);
static std::wstring Utf8ToWide(const std::string& inputString);
static std::string WideToUtf8(const std::wstring& inputString);
static bool Replace(std::string& str, const std::string& oldValue, const std::string& newValue);
static HICON GetWindowIcon(HWND hwnd);
static std::string GetProductAndVersion();
static std::string GetStartMenuProgramsDirectory();
static std::string GetWindowsAppDataDirectory();
static std::string GetWindowsAppDataRoamingDirectory();
static std::string GetCurrentUserDirectory();
static std::string GetCurrentUserAppData();
static std::string GetFilenameFromPath(std::string path);
static std::wstring GetFilenameFromPath(std::wstring path);
static std::string ResolveLnk(HWND hwnd, LPCSTR lpszLinkFile);
Expand Down
2 changes: 1 addition & 1 deletion WhatsappTray/TrayManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ HICON TrayManager::AddTextToIcon(HICON hBackgroundIcon, LPCSTR text)
pointF = PointF(-6.0f, -1.5f);
}

graphics.DrawString(Helper::ToWString(text).c_str(), -1, &font, pointF, &brush);
graphics.DrawString(Helper::Utf8ToWide(text).c_str(), -1, &font, pointF, &brush);

::SelectObject(hMemDC, hOldBmp);

Expand Down
2 changes: 1 addition & 1 deletion WhatsappTray/Version.rc2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define MAJOR 1
#define MINOR 7
#define PATCH 1
#define PATCH 2
#define IS_DEBUG 0

#define STRINGIFY(x) #x
Expand Down
6 changes: 3 additions & 3 deletions WhatsappTray/WhatsappTray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ static HWND StartWhatsapp()
fs::path appPath = Helper::GetApplicationFilePath();
auto combinedPath = appPath / waStartPath;

LogInfo("Starting WhatsApp from combinedPath:%s", combinedPath.string().c_str());
LogInfo("Starting WhatsApp from combinedPath:%s", combinedPath.u8string().c_str());

// Shorten the path by converting to absoltue path.
auto combinedPathCanonical = fs::canonical(combinedPath);
waStartPathString = combinedPathCanonical.string();
waStartPathString = combinedPathCanonical.u8string();
} else {
waStartPathString = waStartPath.string();
waStartPathString = waStartPath.u8string();
}

LogInfo("Starting WhatsApp from canonical-path:'" + waStartPathString + "'");
Expand Down
2 changes: 1 addition & 1 deletion setupBuildfile.iss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "WhatsappTray"
#define MyAppVersion "1.7.1"
#define MyAppVersion "1.7.2"
#define MyAppURL "https://github.com/D4koon/WhatsappTray"
#define MyAppExeName "WhatsappTray.exe"

Expand Down

0 comments on commit 287444d

Please sign in to comment.