doc_and_tools/tools/apps/hg-helper/DlgPageAdvConfig.cpp

527 lines
15 KiB
C++

// DlgIndicator.cpp: 实现文件
//
#include "DlgPageAdvConfig.h"
#include "resource.h"
#include "ini_file.h"
#include <file/file_util.h>
#include <coding/coding.h>
#pragma warning(disable: 4996)
namespace config
{
INTER_MODULE_CALLBACK(got_str)
{
*(std::string*)param += std::string(data, len);
return inter_module_data::SET_RESULT_CONTINUE;
}
INTER_MODULE_CALLBACK(got_wstr)
{
*(std::wstring*)param += std::wstring((const wchar_t*)data, len / 2);
return inter_module_data::SET_RESULT_CONTINUE;
}
std::wstring get_log_method(const wchar_t* cfg_file)
{
wchar_t buf[256] = { 0 };
GetPrivateProfileStringW(L"log", L"type", L"file", buf, _countof(buf) - 1, cfg_file);
return buf;
}
void set_log_method(const wchar_t* cfg_file, const wchar_t* val)
{
WritePrivateProfileStringW(L"log", L"type", val, cfg_file);
}
std::wstring get_log_level(const wchar_t* cfg_file)
{
wchar_t buf[256] = { 0 };
GetPrivateProfileStringW(L"log", L"level", L"all", buf, _countof(buf) - 1, cfg_file);
return buf;
}
void set_log_level(const wchar_t* cfg_file, const wchar_t* val)
{
WritePrivateProfileStringW(L"log", L"level", val, cfg_file);
}
std::wstring get_log_file(const wchar_t* cfg_file)
{
wchar_t buf[256] = { 0 };
GetPrivateProfileStringW(L"log", L"path", L"", buf, _countof(buf) - 1, cfg_file);
return buf;
}
void set_log_file(const wchar_t* cfg_file, const wchar_t* val)
{
WritePrivateProfileStringW(L"log", L"path", val, cfg_file);
}
int get_twain_log_level(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"log", L"twain-level", 1, cfg_file);
}
void set_twain_log_level(const wchar_t* cfg_file, int val)
{
WritePrivateProfileStringW(L"log", L"twain-level", std::to_wstring(val).c_str(), cfg_file);
}
bool get_twain_log_triples(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"twain-app", L"log-all-triple", 0, cfg_file) == 1;
}
void set_twain_log_triples(const wchar_t* cfg_file, bool val)
{
WritePrivateProfileStringW(L"twain-app", L"log-all-triple", std::to_wstring(val).c_str(), cfg_file);
}
int get_twain_notify_close_policy(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"twain-app", L"notify-close", 0, cfg_file);
}
void set_twain_notify_close_policy(const wchar_t* cfg_file, int val)
{
WritePrivateProfileStringW(L"twain-app", L"notify-close", std::to_wstring(val).c_str(), cfg_file);
}
bool get_dump_images(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"dump", L"dumpusb", 0, cfg_file) == 1;
}
void set_dump_images(const wchar_t* cfg_file, bool val)
{
WritePrivateProfileStringW(L"dump", L"dumpusb", std::to_wstring(val).c_str(), cfg_file);
}
std::wstring get_dump_img_path(const wchar_t* cfg_file)
{
wchar_t buf[256] = { 0 };
GetPrivateProfileStringW(L"dump", L"usb_path", L"", buf, _countof(buf) - 1, cfg_file);
return buf;
}
void set_dump_img_path(const wchar_t* cfg_file, const wchar_t* val)
{
WritePrivateProfileStringW(L"dump", L"usb_path", val, cfg_file);
}
std::wstring get_tmp_img_path(const wchar_t* cfg_file)
{
wchar_t buf[256] = { 0 };
GetPrivateProfileStringW(L"paths", L"final_img", L"", buf, _countof(buf) - 1, cfg_file);
return buf;
}
void set_tmp_img_path(const wchar_t* cfg_file, const wchar_t* val)
{
WritePrivateProfileStringW(L"paths", L"final_img", val, cfg_file);
}
bool get_opencv_adv(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"cpu", L"advanced", 1, cfg_file) == 1;
}
void set_opencv_adv(const wchar_t* cfg_file, bool val)
{
WritePrivateProfileStringW(L"cpu", L"advanced", std::to_wstring(val).c_str(), cfg_file);
}
int get_memory_max(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"mem", L"max_img", 1000, cfg_file);
}
void set_memory_max(const wchar_t* cfg_file, int val)
{
WritePrivateProfileStringW(L"mem", L"max_img", std::to_wstring(val).c_str(), cfg_file);
}
int get_notify_close_method(const wchar_t* cfg_file)
{
return GetPrivateProfileIntW(L"twain-app", L"notify-close", NOTIFY_AUTO, cfg_file);
}
void set_notify_close_method(const wchar_t* cfg_file, int val)
{
WritePrivateProfileStringW(L"twain-app", L"notify-close", std::to_wstring(val).c_str(), cfg_file);
}
void get_read_eof_processes(const wchar_t* cfg_file, std::vector<readeof>& procs)
{
simple_ini ini;
std::vector<KEYVAL> vals;
ini.load(dlg_base::u2m(cfg_file, CP_ACP).c_str());
ini.get("read_eof", vals);
for (auto& v : vals)
{
readeof eof;
eof.name = dlg_base::m2u(v.key.c_str(), CP_ACP);
eof.ret_eof = v.val == "1";
procs.push_back(eof);
}
}
void set_read_eof_processes(const wchar_t* cfg_file, const wchar_t* name, bool eof)
{
WritePrivateProfileStringW(L"read_eof", name, std::to_wstring(eof).c_str(), cfg_file);
}
};
dlg_adv_cfg::dlg_adv_cfg(HWND parent) : dlg_base(parent, IDD_ADV_CFG)
{
create();
}
dlg_adv_cfg::~dlg_adv_cfg()
{
}
BOOL dlg_adv_cfg::handle_message(UINT msg, WPARAM wp, LPARAM lp)
{
wchar_t text[40] = { 0 };
BOOL ret = TRUE;
switch (msg)
{
case WM_INITDIALOG:
on_init_dlg();
UpdateWindow(hwnd());
break;
case WM_COMMAND:
handle_command(HIWORD(wp), LOWORD(wp), (HWND)lp);
break;
case WM_NOTIFY:
ret = on_notify((int)wp, (LPNMHDR)lp);
break;
default:
ret = FALSE;
}
return ret;
}
void dlg_adv_cfg::handle_command(WORD code, WORD id, HANDLE ctrl)
{
if (code == EN_KILLFOCUS)
{
on_lost_focus((HWND)ctrl);
}
else if (code == CBN_SELCHANGE)
{
on_combox_sel_changed((HWND)ctrl);
}
else if (code == BN_CLICKED)
{
on_button_clicked((HWND)ctrl, id);
}
}
BOOL dlg_adv_cfg::on_notify(int ctrl_id, LPNMHDR pnmh)
{
BOOL ret = TRUE;
if (pnmh->hwndFrom == get_item(IDC_COMBO_METHOD))
{
if (pnmh->code == TCN_SELCHANGING)
ret = FALSE;
else if (pnmh->code == TCN_SELCHANGE)
EnableWindow(get_item(IDC_EDIT_LOG_FILE), dlg_base::get_combox_cur_sel(get_item(IDC_COMBO_METHOD)) == 2);
}
return ret;
}
void dlg_adv_cfg::on_init_dlg(void)
{
std::wstring path(dlg_base::m2u(getenv("LOCALAPPDATA"), CP_ACP)),
cfg(path + L"\\config\\debug.cfg");
HWND lst = get_item(IDC_LIST1);
LVCOLUMNW col;
int ind = 0;
ListView_SetExtendedListViewStyle(lst, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.pszText = (wchar_t*)L"\u8FDB\u7A0B\u540D\u79F0";
col.cx = 100;
ListView_InsertColumn(lst, ind++, &col);
col.pszText = (wchar_t*)L"EOF";
col.cx = 40;
ListView_InsertColumn(lst, ind++, &col);
path += L"\\HuagoScan\\";
cfg = path + L"\\config\\debug.cfg";
SetDlgItemTextW(hwnd(), IDC_EDIT_CFG_PATH, cfg.c_str());
HWND oem = get_item(IDC_COMBO_OEM);
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"HuagoScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"HanvonScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"LanxumScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"CumtennScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"MicrotekScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"UniScan");
SendMessageW(oem, CB_ADDSTRING, 0, (LPARAM)L"NeuScan");
SendMessageW(oem, CB_SETCURSEL, 0, 0);
init();
}
void dlg_adv_cfg::on_lost_focus(HWND wnd)
{
// check if EDIT control ...
std::wstring cfg(get_item_text(IDC_EDIT_CFG_PATH));
if (wnd == get_item(IDC_EDIT_LOG_FILE))
{
std::wstring text(get_item_text(IDC_EDIT_LOG_FILE));
if (text.find(L"%") == std::wstring::npos)
{
config::set_log_file(cfg.c_str(), text.c_str());
init();
}
}
else if (wnd == get_item(IDC_EDIT_MEM_MAX))
{
std::wstring text(get_item_text(IDC_EDIT_MEM_MAX));
config::set_memory_max(cfg.c_str(), _wtoi(text.c_str()));
init();
}
else if (wnd == get_item(IDC_EDIT_IMGS_PATH))
{
std::wstring text(get_item_text(IDC_EDIT_IMGS_PATH));
config::set_dump_img_path(cfg.c_str(), text.c_str());
init();
}
else if (wnd == get_item(IDC_EDIT_IMG_PATH))
{
std::wstring text(get_item_text(IDC_EDIT_IMG_PATH));
config::set_tmp_img_path(cfg.c_str(), text.c_str());
init();
}
else if (wnd == get_item(IDC_EDIT_TWAIN_LEVEL))
{
std::wstring text(get_item_text(IDC_EDIT_TWAIN_LEVEL));
config::set_twain_log_level(cfg.c_str(), _wtoi(text.c_str()));
init();
}
else if (wnd == get_item(IDC_EDIT_EOF))
{
std::wstring text(get_item_text(IDC_EDIT_EOF)), first(L"[read_eof]\r\n");
size_t pos = text.find(first), cnt = 0;
if (pos != std::wstring::npos)
{
text.erase(0, pos + first.length());
while (text.length())
{
pos = text.find(L"\r\n");
if (pos == std::wstring::npos)
{
first = text;
text = L"";
}
else
{
first = text.substr(0, pos);
text.erase(0, pos + 2);
}
pos = first.find(L"=");
if (pos != std::wstring::npos)
{
config::set_read_eof_processes(cfg.c_str(), first.substr(0, pos).c_str(), first.substr(pos + 1) == L"1");
cnt++;
}
}
if (cnt)
init();
}
}
}
void dlg_adv_cfg::on_combox_sel_changed(HWND wnd)
{
std::wstring cfg(get_item_text(IDC_EDIT_CFG_PATH));
int sel = dlg_base::get_combox_cur_sel(wnd);
if (wnd == get_item(IDC_COMBO_METHOD))
{
const wchar_t* val[] = {L"none", L"console", L"file"};
config::set_log_method(cfg.c_str(), val[sel]);
init();
wnd = get_item(IDC_EDIT_LOG_FILE);
EnableWindow(wnd, sel == 2);
}
else if (wnd == get_item(IDC_COMBO_LEVEL))
{
const wchar_t* val[] = { L"all", L"debug", L"warning", L"fatal"};
config::set_log_level(cfg.c_str(), val[sel]);
init();
}
else if (wnd == get_item(IDC_COMBO_NOTIFY_CLOSE))
{
config::set_notify_close_method(cfg.c_str(), sel);
init();
}
else if (wnd == get_item(IDC_COMBO_OEM))
{
STR_PARENT_FOLDER(cfg);
STR_PARENT_FOLDER(cfg);
STR_PARENT_FOLDER(cfg);
cfg += L"\\" + get_item_text(IDC_COMBO_OEM) + L"\\config\\debug.cfg";
STR_SIMPLIFY_PATH(cfg);
SetDlgItemTextW(hwnd(), IDC_EDIT_CFG_PATH, cfg.c_str());
init();
}
}
void dlg_adv_cfg::on_button_clicked(HWND wnd, WORD id)
{
std::wstring cfg(get_item_text(IDC_EDIT_CFG_PATH));
int chk = dlg_base::get_button_check(wnd);
if (id == IDC_CHECK_OPENCV)
{
config::set_opencv_adv(cfg.c_str(), chk == BST_CHECKED);
init();
}
else if (id == IDC_CHECK_OUT_IMGS)
{
config::set_dump_images(cfg.c_str(), chk == BST_CHECKED);
init();
EnableWindow(get_item(IDC_EDIT_IMGS_PATH), chk == BST_CHECKED);
}
else if (id == IDC_CHECK_RECORD_MSGS)
{
config::set_twain_log_triples(cfg.c_str(), chk == BST_CHECKED);
init();
}
}
void dlg_adv_cfg::init(void)
{
std::wstring cfg(dlg_base::get_item_text(IDC_EDIT_CFG_PATH));
HWND ctrl = get_item(IDC_COMBO_METHOD);
load_from_file(cfg.c_str());
while (SendMessageW(ctrl, CB_DELETESTRING, 0, 0) != CB_ERR);
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u4E0D\u8F93\u51FA\u65E5\u5FD7");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u8F93\u51FA\u5230\u63A7\u5236\u53F0");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u8F93\u51FA\u5230\u6587\u4EF6");
if (log_method_ == L"file")
SendMessageW(ctrl, CB_SETCURSEL, 2, 0);
else if (log_method_ == L"console")
SendMessageW(ctrl, CB_SETCURSEL, 1, 0);
else
SendMessageW(ctrl, CB_SETCURSEL, 0, 0);
ctrl = get_item(IDC_COMBO_LEVEL);
while (SendMessageW(ctrl, CB_DELETESTRING, 0, 0) != CB_ERR);
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u6240\u6709\u65E5\u5FD7");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u8C03\u8BD5\u4FE1\u606F\u53CA\u4EE5\u4E0A");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u8B66\u544A\u4FE1\u606F\u53CA\u4EE5\u4E0A");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u81F4\u547D\u9519\u8BEF\u53CA\u4EE5\u4E0A");
if (log_method_ == L"fatal")
SendMessageW(ctrl, CB_SETCURSEL, 3, 0);
else if (log_method_ == L"warning")
SendMessageW(ctrl, CB_SETCURSEL, 2, 0);
else if (log_method_ == L"debug")
SendMessageW(ctrl, CB_SETCURSEL, 1, 0);
else
SendMessageW(ctrl, CB_SETCURSEL, 0, 0);
ctrl = get_item(IDC_COMBO_NOTIFY_CLOSE);
while (SendMessageW(ctrl, CB_DELETESTRING, 0, 0) != CB_ERR);
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u81EA\u52A8\u6A21\u5F0F");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"\u76F4\u63A5\u8C03\u7528");
SendMessageW(ctrl, CB_ADDSTRING, 0, (LPARAM)L"eventProcess\u8C03\u7528");
if (notify_close_ < 0 || notify_close_ > NOTIFY_NONE)
notify_close_ = NOTIFY_AUTO;
SendMessageW(ctrl, CB_SETCURSEL, notify_close_, 0);
SetDlgItemInt(hwnd(), IDC_EDIT_TWAIN_LEVEL, twain_log_level_, FALSE);
SendMessage(get_item(IDC_CHECK_RECORD_MSGS), BM_SETCHECK, twain_log_all_triples_ ? (WPARAM)BST_CHECKED : (WPARAM)BST_UNCHECKED, 0);
SendMessage(get_item(IDC_CHECK_OUT_IMGS), BM_SETCHECK, dump_images_ ? (WPARAM)BST_CHECKED : (WPARAM)BST_UNCHECKED, 0);
EnableWindow(get_item(IDC_EDIT_IMGS_PATH), dump_images_);
SetDlgItemTextW(hwnd(), IDC_EDIT_LOG_FILE, log_file_.c_str());
SetDlgItemTextW(hwnd(), IDC_EDIT_IMG_PATH, tmp_img_path_.c_str());
SetDlgItemTextW(hwnd(), IDC_EDIT_IMGS_PATH, dump_img_path_.c_str());
HWND list_ = get_item(IDC_LIST1);
std::wstring eof(L"[read_eof]\r\n");
ListView_DeleteAllItems(list_);
for (auto& v : eof_procs_)
{
LVITEMW item = { 0 };
int ind = ListView_GetItemCount(list_);
item.mask = LVIF_TEXT;
item.iItem = ListView_GetItemCount(list_);
item.pszText = &v.name[0];
ind = ListView_InsertItem(list_, &item);
ListView_SetItemText(list_, ind, 1, v.ret_eof ? (wchar_t*)L"true" : (wchar_t*)L"false");
eof += v.name + L"=" + std::to_wstring(v.ret_eof) + L"\r\n";
}
SetDlgItemTextW(hwnd(), IDC_EDIT_EOF, eof.c_str());
SetDlgItemInt(hwnd(), IDC_EDIT_MEM_MAX, max_mem_, FALSE);
SendMessage(get_item(IDC_CHECK_OPENCV), BM_SETCHECK, enable_opencv_adv_ ? (WPARAM)BST_CHECKED : (WPARAM)BST_UNCHECKED, 0);
}
void dlg_adv_cfg::load_from_file(const wchar_t* file)
{
log_method_ = config::get_log_method(file);
log_level_ = config::get_log_level(file);
log_file_ = config::get_log_file(file);
if (log_file_.empty())
{
log_file_ = file;
STR_PARENT_FOLDER(log_file_);
STR_PARENT_FOLDER(log_file_);
log_file_ += L"\\log\\%APPNAME%.log";
}
twain_log_level_ = config::get_twain_log_level(file);
twain_log_all_triples_ = config::get_twain_log_triples(file);
notify_close_ = config::get_notify_close_method(file);
tmp_img_path_ = config::get_tmp_img_path(file);
if (tmp_img_path_.empty())
{
tmp_img_path_ = file;
STR_PARENT_FOLDER(tmp_img_path_);
STR_PARENT_FOLDER(tmp_img_path_);
tmp_img_path_ += L"\\imgs\\";
STR_SIMPLIFY_PATH(tmp_img_path_);
}
dump_images_ = config::get_dump_images(file);
dump_img_path_ = config::get_dump_img_path(file);
if (dump_img_path_.empty())
dump_img_path_ = tmp_img_path_;
max_mem_ = config::get_memory_max(file);
enable_opencv_adv_ = config::get_opencv_adv(file);
debug_cfg_path_ = file;
config::get_read_eof_processes(file, eof_procs_);
std::string raw("");
std::wstring unic(L"");
file_util::load_file(file, config::got_str, &raw);
coding_util::bom::to_unicode(raw.c_str(), raw.length(), config::got_wstr, &unic);
SetDlgItemTextW(hwnd(), IDC_EDIT_CONTENT, unic.c_str());
}
void dlg_adv_cfg::save_to_file(const wchar_t* file)
{
std::wstring cont(get_item_text(IDC_EDIT_CONTENT));
std::string bom(""), utf8("");
coding_util::unicode_2_utf8(cont.c_str(), config::got_str, &utf8);
coding_util::bom::from_utf8(utf8.c_str(), utf8.length(), config::got_str, &bom);
file_util::save_2_file(bom.c_str(), bom.length(), file);
}