Skip to content

Commit c891d16

Browse files
committed
fix: follow rime/librime#806, migrate to path
1 parent a9aeda0 commit c891d16

File tree

8 files changed

+88
-85
lines changed

8 files changed

+88
-85
lines changed

RimeWithWeasel/RimeWithWeasel.cpp

+37-28
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include <StringAlgorithm.hpp>
55
#include <WeaselUtility.h>
66
#include <WeaselVersion.h>
7+
8+
#include <boost/filesystem.hpp>
9+
#include <map>
710
#include <regex>
811
#include <rime_api.h>
9-
#include <map>
1012

1113
#define TRANSPARENT_COLOR 0x00000000
1214
#define ARGB2ABGR(value) ((value & 0xff000000) | ((value & 0x000000ff) << 16) | (value & 0x0000ff00) | ((value & 0x00ff0000) >> 16))
@@ -70,10 +72,12 @@ void _RefreshTrayIcon(const UINT session_id, const std::function<void()> _Update
7072
void RimeWithWeaselHandler::_Setup()
7173
{
7274
RIME_STRUCT(RimeTraits, weasel_traits);
73-
weasel_traits.shared_data_dir = weasel_shared_data_dir();
74-
weasel_traits.user_data_dir = weasel_user_data_dir();
75+
std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8);
76+
std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8);
77+
weasel_traits.shared_data_dir = shared_dir.c_str();
78+
weasel_traits.user_data_dir = user_dir.c_str();
7579
weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir;
76-
std::string distribution_name(wstring_to_string(WEASEL_IME_NAME, CP_UTF8));
80+
std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8);
7781
weasel_traits.distribution_name = distribution_name.c_str();
7882
weasel_traits.distribution_code_name = WEASEL_CODE_NAME;
7983
weasel_traits.distribution_version = WEASEL_VERSION;
@@ -155,7 +159,7 @@ UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer, EatLine eat)
155159
DLOG(INFO) << "Add session: created session_id = " << session_id;
156160
_ReadClientInfo(session_id, buffer);
157161

158-
m_session_status_map[session_id] = SesstionStatus();
162+
m_session_status_map[session_id] = SessionStatus();
159163
m_session_status_map[session_id].style = m_base_style;
160164

161165
RIME_STRUCT(RimeStatus, status);
@@ -512,23 +516,28 @@ void RimeWithWeaselHandler::_UpdateUI(UINT session_id)
512516
m_option_name.clear();
513517
}
514518

515-
void _LoadIconSettingFromSchema(RimeConfig& config, char *buffer, const int& BUF_SIZE,
516-
const char* key1, const char* key2, const std::wstring& user_dir, const std::wstring& shared_dir, std::wstring& value)
519+
std::wstring _LoadIconSettingFromSchema(RimeConfig& config,
520+
const char* key1,
521+
const char* key2,
522+
const boost::filesystem::path& user_dir,
523+
const boost::filesystem::path& shared_dir)
517524
{
518-
memset(buffer, '\0', (BUF_SIZE+1));
519-
if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) || (key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) {
520-
std::wstring tmp = string_to_wstring(buffer, CP_UTF8);
521-
DWORD dwAttrib = GetFileAttributes((user_dir + L"\\" + tmp).c_str());
522-
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
523-
dwAttrib = GetFileAttributes((shared_dir + L"\\" + tmp).c_str());
524-
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)))
525-
value = L"";
526-
else
527-
value = (shared_dir + L"\\" + tmp);
525+
const int BUF_SIZE = 255;
526+
char buffer[BUF_SIZE + 1];
527+
memset(buffer, '\0', (BUF_SIZE + 1));
528+
if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) ||
529+
(key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) {
530+
std::wstring resource = string_to_wstring(buffer, CP_UTF8);
531+
DWORD dwAttrib = GetFileAttributes((user_dir / resource).c_str());
532+
if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
533+
return (user_dir / resource).wstring();
534+
}
535+
dwAttrib = GetFileAttributes((shared_dir / resource).c_str());
536+
if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
537+
return (shared_dir / resource).wstring();
528538
}
529-
else value = user_dir + L"\\" + tmp;
530539
}
531-
else value = L"";
540+
return L"";
532541
}
533542

534543
void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const std::string& schema_id)
@@ -541,7 +550,7 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s
541550
_UpdateShowNotifications(&config);
542551
m_ui->style() = m_base_style;
543552
_UpdateUIStyle(&config, m_ui, false);
544-
SesstionStatus& session_status = m_session_status_map[session_id];
553+
SessionStatus& session_status = m_session_status_map[session_id];
545554
session_status.style = m_ui->style();
546555
// load schema color style config
547556
memset(buffer, '\0', sizeof(buffer));
@@ -583,12 +592,12 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s
583592
}
584593
// load schema icon start
585594
{
586-
std::wstring user_dir = string_to_wstring(weasel_user_data_dir());
587-
std::wstring shared_dir = string_to_wstring(weasel_shared_data_dir());
588-
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/icon", "schema/zhung_icon", user_dir, shared_dir, session_status.style.current_zhung_icon);
589-
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/ascii_icon", NULL, user_dir, shared_dir, session_status.style.current_ascii_icon);
590-
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/full_icon", NULL, user_dir, shared_dir, session_status.style.current_full_icon);
591-
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/half_icon", NULL, user_dir, shared_dir, session_status.style.current_half_icon);
595+
auto user_dir = WeaselUserDataPath();
596+
auto shared_dir = WeaselSharedDataPath();
597+
session_status.style.current_zhung_icon = _LoadIconSettingFromSchema(config, "schema/icon", "schema/zhung_icon", user_dir, shared_dir);
598+
session_status.style.current_ascii_icon = _LoadIconSettingFromSchema(config, "schema/ascii_icon", NULL, user_dir, shared_dir);
599+
session_status.style.current_full_icon = _LoadIconSettingFromSchema(config, "schema/full_icon", NULL, user_dir, shared_dir);
600+
session_status.style.current_half_icon = _LoadIconSettingFromSchema(config, "schema/half_icon", NULL, user_dir, shared_dir);
592601
}
593602
// load schema icon end
594603
RimeConfigClose(&config);
@@ -602,7 +611,7 @@ void RimeWithWeaselHandler::_LoadAppInlinePreeditSet(UINT session_id, bool ignor
602611
if(!ignore_app_name && m_last_app_name == app_name)
603612
return;
604613
m_last_app_name = app_name;
605-
SesstionStatus& session_status = m_session_status_map[session_id];
614+
SessionStatus& session_status = m_session_status_map[session_id];
606615
bool inline_preedit = session_status.style.inline_preedit;
607616
if (!app_name.empty())
608617
{
@@ -712,7 +721,7 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat)
712721
std::set<std::string> actions;
713722
std::list<std::string> messages;
714723

715-
SesstionStatus& session_status = m_session_status_map[session_id];
724+
SessionStatus& session_status = m_session_status_map[session_id];
716725
RIME_STRUCT(RimeCommit, commit);
717726
if (RimeGetCommit(session_id, &commit))
718727
{

RimeWithWeasel/WeaselUtility.cpp

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
#include "stdafx.h"
2+
#include <boost/filesystem.hpp>
23
#include <string>
4+
#include <WeaselUtility.h>
35

4-
std::wstring WeaselUserDataPath() {
5-
WCHAR path[MAX_PATH] = {0};
6+
namespace fs = boost::filesystem;
7+
8+
fs::path WeaselUserDataPath() {
9+
WCHAR _path[MAX_PATH] = {0};
610
const WCHAR KEY[] = L"Software\\Rime\\Weasel";
711
HKEY hKey;
812
LSTATUS ret = RegOpenKey(HKEY_CURRENT_USER, KEY, &hKey);
913
if (ret == ERROR_SUCCESS)
1014
{
11-
DWORD len = sizeof(path);
15+
DWORD len = sizeof(_path);
1216
DWORD type = 0;
1317
DWORD data = 0;
14-
ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)path, &len);
18+
ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)_path, &len);
1519
RegCloseKey(hKey);
16-
if (ret == ERROR_SUCCESS && type == REG_SZ && path[0])
20+
if (ret == ERROR_SUCCESS && type == REG_SZ && _path[0])
1721
{
18-
return path;
22+
return fs::path(_path);
1923
}
2024
}
2125
// default location
22-
ExpandEnvironmentStringsW(L"%AppData%\\Rime", path, _countof(path));
23-
return path;
24-
}
25-
26-
const char* weasel_shared_data_dir() {
27-
static char path[MAX_PATH] = {0};
28-
GetModuleFileNameA(NULL, path, _countof(path));
29-
std::string str_path(path);
30-
size_t k = str_path.find_last_of("/\\");
31-
strcpy_s(path + k + 1, _countof(path) - (k + 1), "data");
32-
return path;
26+
ExpandEnvironmentStringsW(L"%AppData%\\Rime", _path, _countof(_path));
27+
return fs::path(_path);
3328
}
3429

35-
const char* weasel_user_data_dir() {
36-
static char path[MAX_PATH] = {0};
37-
// Windows wants multi-byte file paths in native encoding
38-
WideCharToMultiByte(CP_ACP, 0, WeaselUserDataPath().c_str(), -1, path, _countof(path) - 1, NULL, NULL);
39-
return path;
30+
fs::path WeaselSharedDataPath() {
31+
wchar_t _path[MAX_PATH] = {0};
32+
GetModuleFileNameW(NULL, _path, _countof(_path));
33+
return fs::path(_path).remove_filename().append("data");
4034
}
4135

4236
std::string GetCustomResource(const char *name, const char *type)

WeaselDeployer/Configurator.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
#include <rime_api.h>
1414
#include <rime_levers_api.h>
1515
#pragma warning(default: 4005)
16+
#include <boost/filesystem.hpp>
1617
#include <fstream>
1718
#include "WeaselDeployer.h"
1819

1920
static void CreateFileIfNotExist(std::string filename)
2021
{
21-
std::string user_data_dir = weasel_user_data_dir();
22-
std::wstring filepathw = string_to_wstring(user_data_dir) + L"\\" + string_to_wstring(filename);
23-
DWORD dwAttrib = GetFileAttributes(filepathw.c_str());
22+
boost::filesystem::path file_path = WeaselUserDataPath() / string_to_wstring(filename, CP_UTF8);
23+
DWORD dwAttrib = GetFileAttributes(file_path.c_str());
2424
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)))
2525
{
26-
std::wofstream o(filepathw, std::ios::app);
26+
std::wofstream o(file_path.c_str(), std::ios::app);
2727
o.close();
2828
}
2929
}
@@ -36,14 +36,13 @@ Configurator::Configurator()
3636
void Configurator::Initialize()
3737
{
3838
RIME_STRUCT(RimeTraits, weasel_traits);
39-
weasel_traits.shared_data_dir = weasel_shared_data_dir();
40-
weasel_traits.user_data_dir = weasel_user_data_dir();
39+
std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8);
40+
std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8);
41+
weasel_traits.shared_data_dir = shared_dir.c_str();
42+
weasel_traits.user_data_dir = user_dir.c_str();
4143
weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir;
42-
const int len = 20;
43-
char utf8_str[len];
44-
memset(utf8_str, 0, sizeof(utf8_str));
45-
WideCharToMultiByte(CP_UTF8, 0, WEASEL_IME_NAME, -1, utf8_str, len - 1, NULL, NULL);
46-
weasel_traits.distribution_name = utf8_str;
44+
std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8);
45+
weasel_traits.distribution_name = distribution_name.c_str();
4746
weasel_traits.distribution_code_name = WEASEL_CODE_NAME;
4847
weasel_traits.distribution_version = WEASEL_VERSION;
4948
weasel_traits.app_name = "rime.weasel";

WeaselServer/WeaselServerApp.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "stdafx.h"
22
#include "WeaselServerApp.h"
3+
#include <boost/filesystem.hpp>
34

45
WeaselServerApp::WeaselServerApp()
56
: m_handler(std::make_unique<RimeWithWeaselHandler>(&m_ui))
@@ -44,12 +45,12 @@ int WeaselServerApp::Run()
4445

4546
void WeaselServerApp::SetupMenuHandlers()
4647
{
47-
std::wstring dir(install_dir());
48+
boost::filesystem::path dir = install_dir();
4849
m_server.AddMenuHandler(ID_WEASELTRAY_QUIT, [this] { return m_server.Stop() == 0; });
49-
m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/deploy")));
50-
m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring()));
51-
m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/dict")));
52-
m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/sync")));
50+
m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/deploy")));
51+
m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring()));
52+
m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/dict")));
53+
m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/sync")));
5354
m_server.AddMenuHandler(ID_WEASELTRAY_WIKI, std::bind(open, L"https://rime.im/docs/"));
5455
m_server.AddMenuHandler(ID_WEASELTRAY_HOMEPAGE, std::bind(open, L"https://rime.im/"));
5556
m_server.AddMenuHandler(ID_WEASELTRAY_FORUM, std::bind(open, L"https://rime.im/discuss/"));

WeaselServer/WeaselServerApp.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55
#include <WeaselUI.h>
66
#include <RimeWithWeasel.h>
77
#include <WeaselUtility.h>
8-
#include <winsparkle.h>
8+
#include <boost/filesystem.hpp>
99
#include <functional>
1010
#include <memory>
11+
#include <winsparkle.h>
1112

1213
#include "WeaselTrayIcon.h"
1314

15+
namespace fs = boost::filesystem;
16+
1417
class WeaselServerApp {
1518
public:
16-
static bool execute(const std::wstring &cmd, const std::wstring &args)
19+
static bool execute(const fs::path &cmd, const std::wstring &args)
1720
{
1821
return (int)ShellExecuteW(NULL, NULL, cmd.c_str(), args.c_str(), NULL, SW_SHOWNORMAL) > 32;
1922
}
2023

21-
static bool explore(const std::wstring &path)
24+
static bool explore(const fs::path &path)
2225
{
23-
return (int)ShellExecuteW(NULL, L"open", L"explorer", (L"\"" + path + L"\"").c_str(), NULL, SW_SHOWNORMAL) > 32;
26+
std::wstring quoted_path(L"\"" + path.wstring() + L"\"");
27+
return (int)ShellExecuteW(NULL, L"open", L"explorer", quoted_string.c_str(), NULL, SW_SHOWNORMAL) > 32;
2428
}
2529

26-
static bool open(const std::wstring &path)
30+
static bool open(const fs::path &path)
2731
{
2832
return (int)ShellExecuteW(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
2933
}
@@ -40,14 +44,11 @@ class WeaselServerApp {
4044
return true;
4145
}
4246

43-
static std::wstring install_dir()
47+
static fs::path install_dir()
4448
{
4549
WCHAR exe_path[MAX_PATH] = { 0 };
4650
GetModuleFileNameW(GetModuleHandle(NULL), exe_path, _countof(exe_path));
47-
std::wstring dir(exe_path);
48-
size_t pos = dir.find_last_of(L"\\");
49-
dir.resize(pos);
50-
return dir;
51+
return fs::path(exe_path).remove_filename();
5152
}
5253

5354
public:

include/RimeWithWeasel.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ struct CaseInsensitiveCompare {
1919

2020
typedef std::map<std::string, bool> AppOptions;
2121
typedef std::map<std::string, AppOptions, CaseInsensitiveCompare> AppOptionsByAppName;
22-
struct SesstionStatus
22+
struct SessionStatus
2323
{
24-
SesstionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); }
24+
SessionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); }
2525
weasel::UIStyle style;
2626
RimeStatus status;
2727
bool __synced;
2828
};
29-
typedef std::map<UINT, SesstionStatus> SesstionStatusMap;
29+
typedef std::map<UINT, SessionStatus> SessionStatusMap;
3030
class RimeWithWeaselHandler :
3131
public weasel::RequestHandler
3232
{
@@ -88,7 +88,7 @@ class RimeWithWeaselHandler :
8888
static std::string m_message_value;
8989
static std::string m_message_label;
9090
static std::string m_option_name;
91-
SesstionStatusMap m_session_status_map;
91+
SessionStatusMap m_session_status_map;
9292
bool m_current_dark_mode;
9393
bool m_global_ascii_mode;
9494
int m_show_notifications_time;

include/WeaselUtility.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <boost/filesystem.hpp>
23
#include <string>
34

45
inline int utf8towcslen(const char* utf8_str, int utf8_len)
@@ -27,10 +28,8 @@ inline std::wstring getUsername() {
2728
}
2829

2930
// data directories
30-
std::wstring WeaselUserDataPath();
31-
32-
const char* weasel_shared_data_dir();
33-
const char* weasel_user_data_dir();
31+
boost::filesystem::path WeaselSharedDataPath();
32+
boost::filesystem::path WeaselUserDataPath();
3433

3534
inline BOOL IsUserDarkMode()
3635
{

librime

Submodule librime updated 136 files

0 commit comments

Comments
 (0)