2023-05-06 02:55:17 +00:00
|
|
|
#include "twain_user/twainui.h"
|
2023-05-04 03:13:47 +00:00
|
|
|
#include <QMessageBox>
|
2023-05-19 03:05:59 +00:00
|
|
|
#include <string>
|
2023-06-01 08:24:03 +00:00
|
|
|
#include <shlobj.h>
|
2023-04-20 09:49:48 +00:00
|
|
|
#include "dialog_device_select.h"
|
|
|
|
#include "dialog_progress_ui.h"
|
|
|
|
#include "hg_settingdialog.h"
|
2023-05-12 06:39:19 +00:00
|
|
|
#include "dialog_twain_source_select.h"
|
2023-04-27 09:42:50 +00:00
|
|
|
#include "device_menu.h"
|
|
|
|
#include "base/HGBase.h"
|
2023-05-10 12:30:07 +00:00
|
|
|
#include "Manager.h"
|
2023-06-02 06:34:35 +00:00
|
|
|
#include "app_cfg.h"
|
2023-04-20 09:49:48 +00:00
|
|
|
|
|
|
|
#ifdef HG_CMP_MSC
|
|
|
|
#include "qwinwidget.hpp"
|
|
|
|
extern HINSTANCE g_hInst;
|
2023-05-10 12:30:07 +00:00
|
|
|
extern HGEvent g_event;
|
|
|
|
extern Manager *g_manager;
|
|
|
|
extern HGThread g_hThread;
|
2023-04-20 09:49:48 +00:00
|
|
|
#endif
|
2023-05-10 12:30:07 +00:00
|
|
|
|
|
|
|
static void HGAPI ThreadFunc(HGThread thread, HGPointer param)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
bool ownApplication = false;
|
|
|
|
ownApplication = QMfcApp::pluginInstance(g_hInst);
|
|
|
|
if (ownApplication)
|
|
|
|
{
|
|
|
|
qApp->setQuitOnLastWindowClosed(false);
|
|
|
|
|
|
|
|
g_manager = new Manager;
|
|
|
|
HGBase_SetEvent(g_event);
|
|
|
|
qApp->exec();
|
|
|
|
delete qApp;
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 07:41:13 +00:00
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
int choose_scanner(const std::vector<DEVQUEUI> &devs)
|
|
|
|
{
|
|
|
|
if (!qApp)
|
|
|
|
{
|
|
|
|
HGBase_CreateEvent(HGTRUE, HGFALSE, &g_event);
|
|
|
|
HGBase_OpenThread(ThreadFunc, NULL, &g_hThread);
|
|
|
|
HGBase_WaitEvent(g_event);
|
|
|
|
}
|
2023-04-21 07:51:55 +00:00
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
if (NULL == g_manager)
|
|
|
|
{
|
|
|
|
g_manager = new Manager;
|
|
|
|
}
|
2023-04-23 01:25:52 +00:00
|
|
|
|
2023-05-19 10:41:49 +00:00
|
|
|
return g_manager->showDeviceSelect(nullptr == g_hThread, devs);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:24:03 +00:00
|
|
|
static HGResult GetConfigPath(HGChar* configPath, HGUInt maxLen)
|
|
|
|
{
|
|
|
|
if (NULL == configPath || 0 == maxLen)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *appName = "HuaGoScan";
|
|
|
|
#if defined(OEM_HANWANG)
|
|
|
|
appName = "HanvonScan";
|
|
|
|
#elif defined(OEM_LISICHENG)
|
|
|
|
appName = "LanxumScan";
|
|
|
|
#elif defined(OEM_CANGTIAN)
|
|
|
|
appName = "CumtennScan";
|
|
|
|
#elif defined(OEM_ZHONGJING)
|
|
|
|
appName = "MicrotekScan";
|
|
|
|
#elif defined(OEM_ZIGUANG)
|
|
|
|
appName = "UniScan";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
|
|
CHAR cfgPath[MAX_PATH] = { 0 };
|
|
|
|
BOOL ret = SHGetSpecialFolderPathA(NULL, cfgPath, CSIDL_APPDATA, FALSE);
|
|
|
|
if (!ret)
|
|
|
|
return HGBASE_ERR_FAIL;
|
|
|
|
if (cfgPath[strlen(cfgPath) - 1] != '\\')
|
|
|
|
strcat(cfgPath, "\\");
|
|
|
|
|
|
|
|
strcat(cfgPath, appName);
|
|
|
|
strcat(cfgPath, "\\Cfg\\");
|
|
|
|
#else
|
|
|
|
char cfgPath[512] = { 0 };
|
|
|
|
struct passwd* pw = getpwuid(getuid());
|
|
|
|
strcpy(cfgPath, pw->pw_dir);
|
|
|
|
if (cfgPath[strlen(cfgPath) - 1] != '/')
|
|
|
|
strcat(cfgPath, "/");
|
|
|
|
|
|
|
|
strcat(cfgPath, ".");
|
|
|
|
strcat(cfgPath, appName);
|
|
|
|
strcat(cfgPath, "/Cfg/");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (maxLen < strlen(cfgPath) + 1)
|
|
|
|
return HGBASE_ERR_FAIL;
|
|
|
|
strcpy(configPath, cfgPath);
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
char *apply_current_config(const char *dev_name, SANE_Handle device, LPSANEAPI api)
|
|
|
|
{
|
2023-04-27 09:42:50 +00:00
|
|
|
dev_que devQue;
|
|
|
|
gb::scanner_cfg *cur_cfg_ = nullptr;
|
|
|
|
gb::sane_config_schm *curScheme = nullptr;
|
|
|
|
|
|
|
|
HGChar cfgpath[512] = {0};
|
2023-06-01 08:24:03 +00:00
|
|
|
GetConfigPath(cfgpath, 512);
|
|
|
|
|
2023-04-27 09:42:50 +00:00
|
|
|
HGBase_CreateDir(cfgpath);
|
|
|
|
devQue.set_root_dir(cfgpath);
|
|
|
|
QString old = QString::fromStdString(cfgpath) + PATH_SYMBOL + "scanner.schm";
|
|
|
|
if(QFile::exists(old))
|
|
|
|
dev_que::update_old_cfg(old.toStdString().c_str());
|
|
|
|
|
2023-05-19 03:05:59 +00:00
|
|
|
int pid = 0;
|
|
|
|
api->sane_control_option_api(device, (SANE_Int)0x8853, SANE_ACTION_GET_VALUE, &pid, NULL);
|
|
|
|
|
2023-05-22 09:41:44 +00:00
|
|
|
char buf[10] = { 0 };
|
|
|
|
sprintf(buf, "%x", pid);
|
|
|
|
std::string devName = dev_name;
|
|
|
|
if (pid != 0)
|
|
|
|
{
|
|
|
|
devName = devName.substr(0, devName.find(" ")) + " " + buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
devQue.add_scanner(devName.c_str());
|
|
|
|
devQue.open_scanner(api, device, devName.c_str(), true);
|
2023-04-27 09:42:50 +00:00
|
|
|
|
|
|
|
std::string n(devQue.opened_scanner_name());
|
|
|
|
for(int i = 0; i < devQue.scanners(); ++i)
|
|
|
|
{
|
|
|
|
SCANNER s = devQue.get_at(i);
|
|
|
|
if(s.name == n)
|
|
|
|
{
|
|
|
|
cur_cfg_ = s.cfg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curScheme = cur_cfg_->get_scheme();
|
|
|
|
if(!curScheme)
|
|
|
|
curScheme = new gb::sane_config_schm();
|
|
|
|
curScheme->begin_setting();
|
|
|
|
|
|
|
|
std::string name = curScheme->get_scheme_name();
|
|
|
|
char *str = const_cast<char*>(name.c_str());
|
2023-05-26 09:25:01 +00:00
|
|
|
char *buf2 = new char[strlen(str) + 4];
|
|
|
|
memcpy(buf2, str, sizeof(str));
|
|
|
|
return buf2;
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void twain_ui_free(void *buf)
|
|
|
|
{
|
|
|
|
if (buf != nullptr)
|
|
|
|
{
|
2023-05-26 09:25:01 +00:00
|
|
|
delete[] buf;
|
2023-04-20 09:49:48 +00:00
|
|
|
buf = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 02:55:17 +00:00
|
|
|
int show_setting_ui(SANE_Handle device, HWND parent, LPSANEAPI api, const char *devName, bool with_scan, std::function<void(ui_result)> callback)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
if (!qApp)
|
|
|
|
{
|
|
|
|
HGBase_CreateEvent(HGTRUE, HGFALSE, &g_event);
|
|
|
|
HGBase_OpenThread(ThreadFunc, NULL, &g_hThread);
|
|
|
|
HGBase_WaitEvent(g_event);
|
|
|
|
}
|
2023-04-20 09:49:48 +00:00
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
if (NULL == g_manager)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
g_manager = new Manager;
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 10:41:49 +00:00
|
|
|
return g_manager->showSettingUi(nullptr == g_hThread, device, parent, api, devName, with_scan, callback);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int show_progress_ui(HWND parent, std::function<void (ui_result)> callback, std::function<void (int, void *, int)> *notify)
|
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
if (!qApp)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
HGBase_CreateEvent(HGTRUE, HGFALSE, &g_event);
|
|
|
|
HGBase_OpenThread(ThreadFunc, NULL, &g_hThread);
|
|
|
|
HGBase_WaitEvent(g_event);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
2023-05-08 02:10:48 +00:00
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
if (NULL == g_manager)
|
2023-05-08 02:10:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
g_manager = new Manager;
|
2023-05-08 02:10:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 10:41:49 +00:00
|
|
|
return g_manager->showProgressUi(nullptr == g_hThread, parent, callback, notify);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
2023-05-04 03:13:47 +00:00
|
|
|
|
|
|
|
int show_messagebox_ui(HWND parent, int event, void *msg, int flag)
|
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
if (!qApp)
|
2023-05-04 03:13:47 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
HGBase_CreateEvent(HGTRUE, HGFALSE, &g_event);
|
|
|
|
HGBase_OpenThread(ThreadFunc, NULL, &g_hThread);
|
|
|
|
HGBase_WaitEvent(g_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == g_manager)
|
|
|
|
{
|
|
|
|
g_manager = new Manager;
|
2023-05-04 03:13:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 09:54:02 +00:00
|
|
|
bool showMsgBox = getCfgValue("twain", "showMsgBox", true);
|
2023-06-02 06:34:35 +00:00
|
|
|
if (showMsgBox)
|
|
|
|
return g_manager->showMessageBoxUi(nullptr == g_hThread, parent, event, msg, flag);
|
|
|
|
|
|
|
|
return 0;
|
2023-05-04 03:13:47 +00:00
|
|
|
}
|
2023-05-12 06:39:19 +00:00
|
|
|
|
2023-05-31 01:05:33 +00:00
|
|
|
int show_twain_srclist_ui(const TW_IDENTITY *vds, HGUInt count, const HGChar *defDsName, HGWindow parent, TW_IDENTITY *ds)
|
2023-05-12 06:39:19 +00:00
|
|
|
{
|
|
|
|
if (!qApp)
|
|
|
|
{
|
|
|
|
HGBase_CreateEvent(HGTRUE, HGFALSE, &g_event);
|
|
|
|
HGBase_OpenThread(ThreadFunc, NULL, &g_hThread);
|
|
|
|
HGBase_WaitEvent(g_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == g_manager)
|
|
|
|
{
|
|
|
|
g_manager = new Manager;
|
|
|
|
}
|
|
|
|
|
2023-05-31 01:05:33 +00:00
|
|
|
return g_manager->showTwainSrcUi(nullptr == g_hThread, vds, count, defDsName, parent, ds);
|
2023-05-12 06:39:19 +00:00
|
|
|
}
|
2023-05-23 09:40:43 +00:00
|
|
|
|
|
|
|
int close_ui(int which)
|
|
|
|
{
|
|
|
|
if (NULL == g_manager)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
if (UI_INDICATOR & which)
|
2023-05-23 09:40:43 +00:00
|
|
|
{
|
|
|
|
g_manager->closeProgressUi();
|
|
|
|
}
|
2023-05-24 06:40:06 +00:00
|
|
|
if (UI_SETTING & which)
|
2023-05-23 09:40:43 +00:00
|
|
|
{
|
|
|
|
g_manager->closeSettingUi();
|
|
|
|
}
|
2023-05-24 06:40:06 +00:00
|
|
|
if (UI_MSG_BOX & which)
|
2023-05-23 09:40:43 +00:00
|
|
|
{
|
|
|
|
g_manager->closeMessageBoxUi();
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
if (which == UI_UNLOAD_MODULE)
|
|
|
|
{
|
|
|
|
g_manager->clear_functions();
|
|
|
|
//g_manager->closeManager();
|
|
|
|
|
|
|
|
//if (NULL != g_hThread)
|
|
|
|
//{
|
|
|
|
// HGBase_CloseThread(g_hThread);
|
|
|
|
// g_hThread = NULL;
|
|
|
|
// HGBase_DestroyEvent(g_event);
|
|
|
|
// g_event = NULL;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2023-05-23 09:40:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|