Skip to content

Commit

Permalink
Support custom user data directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jul 21, 2012
1 parent b667b33 commit 27c96cf
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 73 deletions.
33 changes: 2 additions & 31 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stdafx.h"
#include <RimeWithWeasel.h>
#include <WeaselUtility.h>
#include <WeaselVersion.h>
#include <windows.h>
#include <list>
#include <set>
#include <string>
Expand All @@ -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;
}

Expand All @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="WeaselUtility.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\RimeWithWeasel.h" />
<ClInclude Include="..\include\WeaselUtility.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WeaselUtility.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\RimeWithWeasel.h">
Expand All @@ -32,6 +35,9 @@
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\WeaselUtility.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
Expand Down
53 changes: 53 additions & 0 deletions RimeWithWeasel/WeaselUtility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "stdafx.h"
#include <string>

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;
}
1 change: 1 addition & 0 deletions RimeWithWeasel/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "targetver.h"

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>

#pragma warning(disable : 4819)

Expand Down
26 changes: 1 addition & 25 deletions WeaselDeployer/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "DictManagementDialog.h"
#include <WeaselCommon.h>
#include <WeaselIPC.h>
#include <WeaselUtility.h>
#include <WeaselVersion.h>
#pragma warning(disable: 4005)
#pragma warning(disable: 4995)
Expand All @@ -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()
{
}
Expand Down
14 changes: 8 additions & 6 deletions WeaselDeployer/DictManagementDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "stdafx.h"
#include "DictManagementDialog.h"
#include "Configurator.h"
#include <WeaselUtility.h>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>

DictManagementDialog::DictManagementDialog(rime::Deployer* deployer)
: mgr_(deployer)
Expand Down Expand Up @@ -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);
}
}
Expand Down
9 changes: 3 additions & 6 deletions WeaselDeployer/WeaselDeployer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// WeaselDeployer.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <WeaselUtility.h>
#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;
}

Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions WeaselDeployer/WeaselDeployer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<ClInclude Include="WeaselDeployer.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\RimeWithWeasel\WeaselUtility.cpp" />
<ClCompile Include="Configurator.cpp" />
<ClCompile Include="DictManagementDialog.cpp" />
<ClCompile Include="stdafx.cpp">
Expand Down
3 changes: 3 additions & 0 deletions WeaselDeployer/WeaselDeployer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<ClCompile Include="DictManagementDialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\RimeWithWeasel\WeaselUtility.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="WeaselDeployer.rc">
Expand Down
2 changes: 1 addition & 1 deletion WeaselIME/WeaselIME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions WeaselServer/WeaselServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <WeaselIPC.h>
#include <WeaselUI.h>
#include <RimeWithWeasel.h>
#include <WeaselUtility.h>
#include <winsparkle.h>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
// 防止服务进程开启输入法
Expand Down
12 changes: 12 additions & 0 deletions include/WeaselUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <string>

// 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();
1 change: 1 addition & 0 deletions weasel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 27c96cf

Please sign in to comment.