From 27c96cf87be7d1a9aaf61dbbc6b29413ddae2296 Mon Sep 17 00:00:00 2001 From: GONG Chen Date: Sat, 21 Jul 2012 20:19:45 +0800 Subject: [PATCH] Support custom user data directory. --- RimeWithWeasel/RimeWithWeasel.cpp | 33 +----------- RimeWithWeasel/RimeWithWeasel.vcxproj | 2 + RimeWithWeasel/RimeWithWeasel.vcxproj.filters | 6 +++ RimeWithWeasel/WeaselUtility.cpp | 53 +++++++++++++++++++ RimeWithWeasel/stdafx.h | 1 + WeaselDeployer/Configurator.cpp | 26 +-------- WeaselDeployer/DictManagementDialog.cpp | 14 ++--- WeaselDeployer/WeaselDeployer.cpp | 9 ++-- WeaselDeployer/WeaselDeployer.vcxproj | 1 + WeaselDeployer/WeaselDeployer.vcxproj.filters | 3 ++ WeaselIME/WeaselIME.cpp | 2 +- WeaselServer/WeaselServer.cpp | 6 +-- include/WeaselUtility.h | 12 +++++ weasel.sln | 1 + 14 files changed, 96 insertions(+), 73 deletions(-) create mode 100644 RimeWithWeasel/WeaselUtility.cpp create mode 100644 include/WeaselUtility.h diff --git a/RimeWithWeasel/RimeWithWeasel.cpp b/RimeWithWeasel/RimeWithWeasel.cpp index ead20fab0..fbffe01bc 100644 --- a/RimeWithWeasel/RimeWithWeasel.cpp +++ b/RimeWithWeasel/RimeWithWeasel.cpp @@ -1,7 +1,7 @@ #include "stdafx.h" #include +#include #include -#include #include #include #include @@ -16,7 +16,7 @@ static const std::string WeaselLogFilePath() { char path[MAX_PATH] = {0}; - ExpandEnvironmentStringsA("%AppData%\\Rime\\rime.log", path, _countof(path)); + ExpandEnvironmentStringsA("%TEMP%\\rime.log", path, _countof(path)); return path; } @@ -33,40 +33,11 @@ static const std::string WeaselLogFilePath() #pragma warning(default: 4996) #pragma warning(default: 4995) -const WCHAR* utf8towcs(const char* utf8_str) -{ - const int buffer_len = 4096; - static WCHAR buffer[buffer_len]; - memset(buffer, 0, sizeof(buffer)); - MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, buffer, buffer_len - 1); - return buffer; -} - -int utf8towcslen(const char* utf8_str, int utf8_len) -{ - return MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0); -} - int expand_ibus_modifier(int m) { return (m & 0xff) | ((m & 0xff00) << 16); } -static const char* weasel_shared_data_dir() { - static char path[MAX_PATH] = {0}; - GetModuleFileNameA(NULL, path, _countof(path)); - std::string str_path(path); - size_t k = str_path.find_last_of("/\\"); - strcpy(path + k + 1, "data"); - return path; -} - -static const char* weasel_user_data_dir() { - static char path[MAX_PATH] = {0}; - ExpandEnvironmentStringsA("%AppData%\\Rime", path, _countof(path)); - return path; -} - RimeWithWeaselHandler::RimeWithWeaselHandler(weasel::UI *ui) : m_ui(ui), m_active_session(0), m_disabled(true) { diff --git a/RimeWithWeasel/RimeWithWeasel.vcxproj b/RimeWithWeasel/RimeWithWeasel.vcxproj index d64eca93d..8f98fe613 100644 --- a/RimeWithWeasel/RimeWithWeasel.vcxproj +++ b/RimeWithWeasel/RimeWithWeasel.vcxproj @@ -201,9 +201,11 @@ Create Create + + diff --git a/RimeWithWeasel/RimeWithWeasel.vcxproj.filters b/RimeWithWeasel/RimeWithWeasel.vcxproj.filters index cd881d846..91998a146 100644 --- a/RimeWithWeasel/RimeWithWeasel.vcxproj.filters +++ b/RimeWithWeasel/RimeWithWeasel.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + @@ -32,6 +35,9 @@ Header Files + + Header Files + diff --git a/RimeWithWeasel/WeaselUtility.cpp b/RimeWithWeasel/WeaselUtility.cpp new file mode 100644 index 000000000..7d1c8aad6 --- /dev/null +++ b/RimeWithWeasel/WeaselUtility.cpp @@ -0,0 +1,53 @@ +#include "stdafx.h" +#include + +const WCHAR* utf8towcs(const char* utf8_str) +{ + const int buffer_len = 4096; + static WCHAR buffer[buffer_len]; + memset(buffer, 0, sizeof(buffer)); + MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, buffer, buffer_len - 1); + return buffer; +} + +int utf8towcslen(const char* utf8_str, int utf8_len) +{ + return MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0); +} + +const std::wstring WeaselUserDataPath() { + WCHAR path[MAX_PATH] = {0}; + const WCHAR KEY[] = L"Software\\Rime\\Weasel"; + HKEY hKey; + LSTATUS ret = RegOpenKey(HKEY_CURRENT_USER, KEY, &hKey); + if (ret == ERROR_SUCCESS) + { + DWORD len = sizeof(path); + DWORD type = 0; + DWORD data = 0; + ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)path, &len); + RegCloseKey(hKey); + if (ret == ERROR_SUCCESS && type == REG_SZ && path[0]) + { + return path; + } + } + // default location + ExpandEnvironmentStringsW(L"%AppData%\\Rime", path, _countof(path)); + return path; +} + +const char* weasel_shared_data_dir() { + static char path[MAX_PATH] = {0}; + GetModuleFileNameA(NULL, path, _countof(path)); + std::string str_path(path); + size_t k = str_path.find_last_of("/\\"); + strcpy(path + k + 1, "data"); + return path; +} + +const char* weasel_user_data_dir() { + static char path[MAX_PATH] = {0}; + WideCharToMultiByte(CP_UTF8, 0, WeaselUserDataPath().c_str(), -1, path, _countof(path) - 1, NULL, NULL); + return path; +} diff --git a/RimeWithWeasel/stdafx.h b/RimeWithWeasel/stdafx.h index 36065c851..d432e5b0e 100644 --- a/RimeWithWeasel/stdafx.h +++ b/RimeWithWeasel/stdafx.h @@ -8,6 +8,7 @@ #include "targetver.h" #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +#include #pragma warning(disable : 4819) diff --git a/WeaselDeployer/Configurator.cpp b/WeaselDeployer/Configurator.cpp index 5f3994420..97c627bbf 100644 --- a/WeaselDeployer/Configurator.cpp +++ b/WeaselDeployer/Configurator.cpp @@ -7,6 +7,7 @@ #include "DictManagementDialog.h" #include #include +#include #include #pragma warning(disable: 4005) #pragma warning(disable: 4995) @@ -20,31 +21,6 @@ #pragma warning(default: 4005) -const WCHAR* utf8towcs(const char* utf8_str) -{ - const int buffer_len = 1024; - static WCHAR buffer[buffer_len]; - memset(buffer, 0, sizeof(buffer)); - MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, buffer, buffer_len - 1); - return buffer; -} - -static const char* weasel_shared_data_dir() { - static char path[MAX_PATH] = {0}; - GetModuleFileNameA(NULL, path, _countof(path)); - std::string str_path(path); - size_t k = str_path.find_last_of("/\\"); - strcpy(path + k + 1, "data"); - return path; -} - -static const char* weasel_user_data_dir() { - static char path[MAX_PATH] = {0}; - ExpandEnvironmentStringsA("%AppData%\\Rime", path, _countof(path)); - return path; -} - - Configurator::Configurator() { } diff --git a/WeaselDeployer/DictManagementDialog.cpp b/WeaselDeployer/DictManagementDialog.cpp index 440fc981c..b45ee11d4 100644 --- a/WeaselDeployer/DictManagementDialog.cpp +++ b/WeaselDeployer/DictManagementDialog.cpp @@ -1,7 +1,9 @@ #include "stdafx.h" #include "DictManagementDialog.h" #include "Configurator.h" +#include #include +#include DictManagementDialog::DictManagementDialog(rime::Deployer* deployer) : mgr_(deployer) @@ -53,15 +55,15 @@ LRESULT DictManagementDialog::OnBackup(WORD, WORD code, HWND, BOOL&) { MessageBox(L"不知哪裏出錯了,未能完成操作。", L":-(", MB_OK | MB_ICONERROR); } else { - WCHAR path[MAX_PATH] = {0}; - ExpandEnvironmentStrings(L"%AppData%\\Rime\\", path, _countof(path)); - MultiByteToWideChar(CP_ACP, 0, dicts_[sel].c_str(), -1, path + wcslen(path), _countof(path) - wcslen(path)); - wcscpy(path + wcslen(path), L".userdb.kct.snapshot"); - if (_waccess(path, 0) != 0) { + boost::filesystem::wpath path(WeaselUserDataPath()); + WCHAR dict_name[100] = {0}; + MultiByteToWideChar(CP_ACP, 0, dicts_[sel].c_str(), -1, dict_name, _countof(dict_name)); + path /= std::wstring(dict_name) + L".userdb.kct.snapshot"; + if (_waccess(path.wstring().c_str(), 0) != 0) { MessageBox(L"咦,輸出的快照文件找不着了。", L":-(", MB_OK | MB_ICONERROR); } else { - std::wstring param = L"/select, \"" + std::wstring(path) + L"\""; + std::wstring param = L"/select, \"" + path.wstring() + L"\""; ShellExecute(NULL, L"open", L"explorer.exe", param.c_str(), NULL, SW_SHOWNORMAL); } } diff --git a/WeaselDeployer/WeaselDeployer.cpp b/WeaselDeployer/WeaselDeployer.cpp index 1ec8e6ec7..f03790079 100644 --- a/WeaselDeployer/WeaselDeployer.cpp +++ b/WeaselDeployer/WeaselDeployer.cpp @@ -1,13 +1,14 @@ // WeaselDeployer.cpp : Defines the entry point for the application. // #include "stdafx.h" +#include #include "WeaselDeployer.h" #include "Configurator.h" const std::string WeaselDeployerLogFilePath() { char path[MAX_PATH] = {0}; - ExpandEnvironmentStringsA("%AppData%\\Rime\\deployer.log", path, _countof(path)); + ExpandEnvironmentStringsA("%TEMP%\\deployer.log", path, _countof(path)); return path; } @@ -37,11 +38,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, ATLASSERT(SUCCEEDED(hRes)); // this should be done before any logging - { - WCHAR path[MAX_PATH] = {0}; - ExpandEnvironmentStrings(L"%AppData%\\Rime", path, _countof(path)); - CreateDirectory(path, NULL); - } + CreateDirectory(WeaselUserDataPath().c_str(), NULL); int ret = 0; HANDLE hMutex = CreateMutex(NULL, TRUE, L"WeaselDeployerExclusiveMutex"); diff --git a/WeaselDeployer/WeaselDeployer.vcxproj b/WeaselDeployer/WeaselDeployer.vcxproj index f48758d9c..5e8245aea 100644 --- a/WeaselDeployer/WeaselDeployer.vcxproj +++ b/WeaselDeployer/WeaselDeployer.vcxproj @@ -90,6 +90,7 @@ + diff --git a/WeaselDeployer/WeaselDeployer.vcxproj.filters b/WeaselDeployer/WeaselDeployer.vcxproj.filters index 883e4dfe7..efc5708b2 100644 --- a/WeaselDeployer/WeaselDeployer.vcxproj.filters +++ b/WeaselDeployer/WeaselDeployer.vcxproj.filters @@ -65,6 +65,9 @@ Source Files + + Source Files + diff --git a/WeaselIME/WeaselIME.cpp b/WeaselIME/WeaselIME.cpp index b1ceb38fd..38cac6827 100644 --- a/WeaselIME/WeaselIME.cpp +++ b/WeaselIME/WeaselIME.cpp @@ -11,7 +11,7 @@ static const std::string WeaselLogFilePath() { char path[MAX_PATH] = {0}; - ExpandEnvironmentStringsA("%AppData%\\Rime\\", path, _countof(path)); + ExpandEnvironmentStringsA("%TEMP%\\", path, _countof(path)); size_t len = 0; StringCchLengthA(path, _countof(path), &len); StringCchPrintfA(path + len, _countof(path) - len, "weasel-ime.log"); diff --git a/WeaselServer/WeaselServer.cpp b/WeaselServer/WeaselServer.cpp index afeae3a5e..c72ace11d 100644 --- a/WeaselServer/WeaselServer.cpp +++ b/WeaselServer/WeaselServer.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -85,9 +86,7 @@ int WeaselServerApp::Run() void WeaselServerApp::SetupMenuHandlers() { WCHAR exe_path[MAX_PATH] = {0}; - WCHAR user_config_path[MAX_PATH] = {0}; GetModuleFileNameW(GetModuleHandle(NULL), exe_path, _countof(exe_path)); - ExpandEnvironmentStringsW(L"%AppData%\\Rime", user_config_path, _countof(user_config_path)); std::wstring install_dir(exe_path); size_t pos = install_dir.find_last_of(L"\\"); install_dir.resize(pos); @@ -100,10 +99,9 @@ void WeaselServerApp::SetupMenuHandlers() m_server.AddMenuHandler(ID_WEASELTRAY_FORUM, boost::lambda::bind(&open, L"http://tieba.baidu.com/f?kw=rime")); m_server.AddMenuHandler(ID_WEASELTRAY_CHECKUPDATE, check_update); m_server.AddMenuHandler(ID_WEASELTRAY_INSTALLDIR, boost::lambda::bind(&explore, install_dir)); - m_server.AddMenuHandler(ID_WEASELTRAY_USERCONFIG, boost::lambda::bind(&explore, std::wstring(user_config_path))); + m_server.AddMenuHandler(ID_WEASELTRAY_USERCONFIG, boost::lambda::bind(&explore, WeaselUserDataPath())); } - int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) { // 防止服务进程开启输入法 diff --git a/include/WeaselUtility.h b/include/WeaselUtility.h new file mode 100644 index 000000000..b3be69d03 --- /dev/null +++ b/include/WeaselUtility.h @@ -0,0 +1,12 @@ +#pragma once +#include + +// UTF-8 conversion +const WCHAR* utf8towcs(const char* utf8_str); +int utf8towcslen(const char* utf8_str, int utf8_len); + +// data directories +const std::wstring WeaselUserDataPath(); + +const char* weasel_shared_data_dir(); +const char* weasel_user_data_dir(); diff --git a/weasel.sln b/weasel.sln index 19c1a79cd..79b20ae25 100644 --- a/weasel.sln +++ b/weasel.sln @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution include\WeaselCommon.h = include\WeaselCommon.h include\WeaselIPC.h = include\WeaselIPC.h include\WeaselUI.h = include\WeaselUI.h + include\WeaselUtility.h = include\WeaselUtility.h EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WeaselTSF", "WeaselTSF\WeaselTSF.vcxproj", "{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}"