Skip to content

Commit

Permalink
fix: follow rime/librime#806, migrate to path
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Feb 8, 2024
1 parent 853d9b4 commit 6acd154
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 85 deletions.
65 changes: 37 additions & 28 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <StringAlgorithm.hpp>
#include <WeaselUtility.h>
#include <WeaselVersion.h>

#include <boost/filesystem.hpp>
#include <map>
#include <regex>
#include <rime_api.h>
#include <map>

#define TRANSPARENT_COLOR 0x00000000
#define ARGB2ABGR(value) ((value & 0xff000000) | ((value & 0x000000ff) << 16) | (value & 0x0000ff00) | ((value & 0x00ff0000) >> 16))
Expand Down Expand Up @@ -70,10 +72,12 @@ void _RefreshTrayIcon(const UINT session_id, const std::function<void()> _Update
void RimeWithWeaselHandler::_Setup()
{
RIME_STRUCT(RimeTraits, weasel_traits);
weasel_traits.shared_data_dir = weasel_shared_data_dir();
weasel_traits.user_data_dir = weasel_user_data_dir();
std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8);
std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8);
weasel_traits.shared_data_dir = shared_dir.c_str();
weasel_traits.user_data_dir = user_dir.c_str();
weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir;
std::string distribution_name(wstring_to_string(WEASEL_IME_NAME, CP_UTF8));
std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8);
weasel_traits.distribution_name = distribution_name.c_str();
weasel_traits.distribution_code_name = WEASEL_CODE_NAME;
weasel_traits.distribution_version = WEASEL_VERSION;
Expand Down Expand Up @@ -155,7 +159,7 @@ UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer, EatLine eat)
DLOG(INFO) << "Add session: created session_id = " << session_id;
_ReadClientInfo(session_id, buffer);

m_session_status_map[session_id] = SesstionStatus();
m_session_status_map[session_id] = SessionStatus();
m_session_status_map[session_id].style = m_base_style;

RIME_STRUCT(RimeStatus, status);
Expand Down Expand Up @@ -512,23 +516,28 @@ void RimeWithWeaselHandler::_UpdateUI(UINT session_id)
m_option_name.clear();
}

void _LoadIconSettingFromSchema(RimeConfig& config, char *buffer, const int& BUF_SIZE,
const char* key1, const char* key2, const std::wstring& user_dir, const std::wstring& shared_dir, std::wstring& value)
std::wstring _LoadIconSettingFromSchema(RimeConfig& config,
const char* key1,
const char* key2,
const boost::filesystem::path& user_dir,
const boost::filesystem::path& shared_dir)
{
memset(buffer, '\0', (BUF_SIZE+1));
if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) || (key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) {
std::wstring tmp = string_to_wstring(buffer, CP_UTF8);
DWORD dwAttrib = GetFileAttributes((user_dir + L"\\" + tmp).c_str());
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
dwAttrib = GetFileAttributes((shared_dir + L"\\" + tmp).c_str());
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)))
value = L"";
else
value = (shared_dir + L"\\" + tmp);
const int BUF_SIZE = 255;
char buffer[BUF_SIZE + 1];
memset(buffer, '\0', (BUF_SIZE + 1));
if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) ||
(key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) {
std::wstring resource = string_to_wstring(buffer, CP_UTF8);
DWORD dwAttrib = GetFileAttributes((user_dir / resource).c_str());
if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
return (user_dir / resource).wstring();
}
dwAttrib = GetFileAttributes((shared_dir / resource).c_str());
if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
return (shared_dir / resource).wstring();
}
else value = user_dir + L"\\" + tmp;
}
else value = L"";
return L"";
}

void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const std::string& schema_id)
Expand All @@ -541,7 +550,7 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s
_UpdateShowNotifications(&config);
m_ui->style() = m_base_style;
_UpdateUIStyle(&config, m_ui, false);
SesstionStatus& session_status = m_session_status_map[session_id];
SessionStatus& session_status = m_session_status_map[session_id];
session_status.style = m_ui->style();
// load schema color style config
memset(buffer, '\0', sizeof(buffer));
Expand Down Expand Up @@ -583,12 +592,12 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s
}
// load schema icon start
{
std::wstring user_dir = string_to_wstring(weasel_user_data_dir());
std::wstring shared_dir = string_to_wstring(weasel_shared_data_dir());
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/icon", "schema/zhung_icon", user_dir, shared_dir, session_status.style.current_zhung_icon);
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/ascii_icon", NULL, user_dir, shared_dir, session_status.style.current_ascii_icon);
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/full_icon", NULL, user_dir, shared_dir, session_status.style.current_full_icon);
_LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/half_icon", NULL, user_dir, shared_dir, session_status.style.current_half_icon);
auto user_dir = WeaselUserDataPath();
auto shared_dir = WeaselSharedDataPath();
session_status.style.current_zhung_icon = _LoadIconSettingFromSchema(config, "schema/icon", "schema/zhung_icon", user_dir, shared_dir);
session_status.style.current_ascii_icon = _LoadIconSettingFromSchema(config, "schema/ascii_icon", NULL, user_dir, shared_dir);
session_status.style.current_full_icon = _LoadIconSettingFromSchema(config, "schema/full_icon", NULL, user_dir, shared_dir);
session_status.style.current_half_icon = _LoadIconSettingFromSchema(config, "schema/half_icon", NULL, user_dir, shared_dir);
}
// load schema icon end
RimeConfigClose(&config);
Expand All @@ -602,7 +611,7 @@ void RimeWithWeaselHandler::_LoadAppInlinePreeditSet(UINT session_id, bool ignor
if(!ignore_app_name && m_last_app_name == app_name)
return;
m_last_app_name = app_name;
SesstionStatus& session_status = m_session_status_map[session_id];
SessionStatus& session_status = m_session_status_map[session_id];
bool inline_preedit = session_status.style.inline_preedit;
if (!app_name.empty())
{
Expand Down Expand Up @@ -712,7 +721,7 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat)
std::set<std::string> actions;
std::list<std::string> messages;

SesstionStatus& session_status = m_session_status_map[session_id];
SessionStatus& session_status = m_session_status_map[session_id];
RIME_STRUCT(RimeCommit, commit);
if (RimeGetCommit(session_id, &commit))
{
Expand Down
38 changes: 16 additions & 22 deletions RimeWithWeasel/WeaselUtility.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
#include "stdafx.h"
#include <boost/filesystem.hpp>
#include <string>
#include <WeaselUtility.h>

std::wstring WeaselUserDataPath() {
WCHAR path[MAX_PATH] = {0};
namespace fs = boost::filesystem;

fs::path 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 len = sizeof(_path);
DWORD type = 0;
DWORD data = 0;
ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)path, &len);
ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)_path, &len);
RegCloseKey(hKey);
if (ret == ERROR_SUCCESS && type == REG_SZ && path[0])
if (ret == ERROR_SUCCESS && type == REG_SZ && _path[0])
{
return path;
return fs::path(_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_s(path + k + 1, _countof(path) - (k + 1), "data");
return path;
ExpandEnvironmentStringsW(L"%AppData%\\Rime", _path, _countof(_path));
return fs::path(_path);
}

const char* weasel_user_data_dir() {
static char path[MAX_PATH] = {0};
// Windows wants multi-byte file paths in native encoding
WideCharToMultiByte(CP_ACP, 0, WeaselUserDataPath().c_str(), -1, path, _countof(path) - 1, NULL, NULL);
return path;
fs::path WeaselSharedDataPath() {
wchar_t _path[MAX_PATH] = {0};
GetModuleFileNameW(NULL, _path, _countof(_path));
return fs::path(_path).remove_filename().append("data");
}

std::string GetCustomResource(const char *name, const char *type)
Expand Down
21 changes: 10 additions & 11 deletions WeaselDeployer/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
#include <rime_api.h>
#include <rime_levers_api.h>
#pragma warning(default: 4005)
#include <boost/filesystem.hpp>
#include <fstream>
#include "WeaselDeployer.h"

static void CreateFileIfNotExist(std::string filename)
{
std::string user_data_dir = weasel_user_data_dir();
std::wstring filepathw = string_to_wstring(user_data_dir) + L"\\" + string_to_wstring(filename);
DWORD dwAttrib = GetFileAttributes(filepathw.c_str());
boost::filesystem::path file_path = WeaselUserDataPath() / string_to_wstring(filename, CP_UTF8);
DWORD dwAttrib = GetFileAttributes(file_path.c_str());
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)))
{
std::wofstream o(filepathw, std::ios::app);
std::wofstream o(file_path.c_str(), std::ios::app);
o.close();
}
}
Expand All @@ -36,14 +36,13 @@ Configurator::Configurator()
void Configurator::Initialize()
{
RIME_STRUCT(RimeTraits, weasel_traits);
weasel_traits.shared_data_dir = weasel_shared_data_dir();
weasel_traits.user_data_dir = weasel_user_data_dir();
std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8);
std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8);
weasel_traits.shared_data_dir = shared_dir.c_str();
weasel_traits.user_data_dir = user_dir.c_str();
weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir;
const int len = 20;
char utf8_str[len];
memset(utf8_str, 0, sizeof(utf8_str));
WideCharToMultiByte(CP_UTF8, 0, WEASEL_IME_NAME, -1, utf8_str, len - 1, NULL, NULL);
weasel_traits.distribution_name = utf8_str;
std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8);
weasel_traits.distribution_name = distribution_name.c_str();
weasel_traits.distribution_code_name = WEASEL_CODE_NAME;
weasel_traits.distribution_version = WEASEL_VERSION;
weasel_traits.app_name = "rime.weasel";
Expand Down
11 changes: 6 additions & 5 deletions WeaselServer/WeaselServerApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "stdafx.h"
#include "WeaselServerApp.h"
#include <boost/filesystem.hpp>

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

void WeaselServerApp::SetupMenuHandlers()
{
std::wstring dir(install_dir());
boost::filesystem::path dir = install_dir();
m_server.AddMenuHandler(ID_WEASELTRAY_QUIT, [this] { return m_server.Stop() == 0; });
m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/deploy")));
m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring()));
m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/dict")));
m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/sync")));
m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/deploy")));
m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring()));
m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/dict")));
m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/sync")));
m_server.AddMenuHandler(ID_WEASELTRAY_WIKI, std::bind(open, L"https://rime.im/docs/"));
m_server.AddMenuHandler(ID_WEASELTRAY_HOMEPAGE, std::bind(open, L"https://rime.im/"));
m_server.AddMenuHandler(ID_WEASELTRAY_FORUM, std::bind(open, L"https://rime.im/discuss/"));
Expand Down
21 changes: 11 additions & 10 deletions WeaselServer/WeaselServerApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
#include <WeaselUI.h>
#include <RimeWithWeasel.h>
#include <WeaselUtility.h>
#include <winsparkle.h>
#include <boost/filesystem.hpp>
#include <functional>
#include <memory>
#include <winsparkle.h>

#include "WeaselTrayIcon.h"

namespace fs = boost::filesystem;

class WeaselServerApp {
public:
static bool execute(const std::wstring &cmd, const std::wstring &args)
static bool execute(const fs::path &cmd, const std::wstring &args)
{
return (int)ShellExecuteW(NULL, NULL, cmd.c_str(), args.c_str(), NULL, SW_SHOWNORMAL) > 32;
}

static bool explore(const std::wstring &path)
static bool explore(const fs::path &path)
{
return (int)ShellExecuteW(NULL, L"open", L"explorer", (L"\"" + path + L"\"").c_str(), NULL, SW_SHOWNORMAL) > 32;
std::wstring quoted_path(L"\"" + path.wstring() + L"\"");
return (int)ShellExecuteW(NULL, L"open", L"explorer", quoted_path.c_str(), NULL, SW_SHOWNORMAL) > 32;
}

static bool open(const std::wstring &path)
static bool open(const fs::path &path)
{
return (int)ShellExecuteW(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
}
Expand All @@ -40,14 +44,11 @@ class WeaselServerApp {
return true;
}

static std::wstring install_dir()
static fs::path install_dir()
{
WCHAR exe_path[MAX_PATH] = { 0 };
GetModuleFileNameW(GetModuleHandle(NULL), exe_path, _countof(exe_path));
std::wstring dir(exe_path);
size_t pos = dir.find_last_of(L"\\");
dir.resize(pos);
return dir;
return fs::path(exe_path).remove_filename();
}

public:
Expand Down
8 changes: 4 additions & 4 deletions include/RimeWithWeasel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ struct CaseInsensitiveCompare {

typedef std::map<std::string, bool> AppOptions;
typedef std::map<std::string, AppOptions, CaseInsensitiveCompare> AppOptionsByAppName;
struct SesstionStatus
struct SessionStatus
{
SesstionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); }
SessionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); }
weasel::UIStyle style;
RimeStatus status;
bool __synced;
};
typedef std::map<UINT, SesstionStatus> SesstionStatusMap;
typedef std::map<UINT, SessionStatus> SessionStatusMap;
class RimeWithWeaselHandler :
public weasel::RequestHandler
{
Expand Down Expand Up @@ -88,7 +88,7 @@ class RimeWithWeaselHandler :
static std::string m_message_value;
static std::string m_message_label;
static std::string m_option_name;
SesstionStatusMap m_session_status_map;
SessionStatusMap m_session_status_map;
bool m_current_dark_mode;
bool m_global_ascii_mode;
int m_show_notifications_time;
Expand Down
7 changes: 3 additions & 4 deletions include/WeaselUtility.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <boost/filesystem.hpp>
#include <string>

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

// data directories
std::wstring WeaselUserDataPath();

const char* weasel_shared_data_dir();
const char* weasel_user_data_dir();
boost::filesystem::path WeaselSharedDataPath();
boost::filesystem::path WeaselUserDataPath();

inline BOOL IsUserDarkMode()
{
Expand Down
2 changes: 1 addition & 1 deletion librime
Submodule librime updated 136 files

0 comments on commit 6acd154

Please sign in to comment.