2023-08-12 01:57:04 +00:00
|
|
|
|
#include "twainui.h"
|
2023-05-04 03:13:47 +00:00
|
|
|
|
#include <QMessageBox>
|
2023-05-19 03:05:59 +00:00
|
|
|
|
#include <string>
|
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-07-04 05:37:46 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
|
extern HGEvent g_event;
|
|
|
|
|
extern HGThread g_hThread;
|
2023-07-04 05:37:46 +00:00
|
|
|
|
extern Manager *g_manager;
|
2023-05-10 12:30:07 +00:00
|
|
|
|
|
2023-07-05 13:54:19 +00:00
|
|
|
|
static gb::scanner_cfg* scanner_cfg_ = nullptr;
|
|
|
|
|
|
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-07-11 02:11:37 +00:00
|
|
|
|
#ifdef HG_CMP_MSC
|
2023-05-10 12:30:07 +00:00
|
|
|
|
bool ownApplication = false;
|
|
|
|
|
ownApplication = QMfcApp::pluginInstance(g_hInst);
|
2023-07-11 02:11:37 +00:00
|
|
|
|
#else
|
|
|
|
|
bool ownApplication = true;
|
|
|
|
|
int argc = 0;
|
|
|
|
|
(void)new QApplication(argc, 0);
|
|
|
|
|
#endif
|
2023-05-10 12:30:07 +00:00
|
|
|
|
if (ownApplication)
|
|
|
|
|
{
|
|
|
|
|
qApp->setQuitOnLastWindowClosed(false);
|
|
|
|
|
|
|
|
|
|
g_manager = new Manager;
|
|
|
|
|
HGBase_SetEvent(g_event);
|
|
|
|
|
qApp->exec();
|
2023-08-08 03:47:10 +00:00
|
|
|
|
delete g_manager;
|
|
|
|
|
g_manager = nullptr;
|
2023-05-10 12:30:07 +00:00
|
|
|
|
delete qApp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-21 07:41:13 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
static void Init()
|
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-10-26 11:09:44 +00:00
|
|
|
|
else
|
2023-05-10 12:30:07 +00:00
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
if (NULL == g_manager)
|
|
|
|
|
{
|
|
|
|
|
g_manager = new Manager;
|
|
|
|
|
//g_manager->moveToThread(qApp->thread());
|
|
|
|
|
}
|
2023-05-10 12:30:07 +00:00
|
|
|
|
}
|
2023-04-23 01:25:52 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
assert(nullptr != g_manager);
|
2023-04-20 09:49:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
int choose_scanner(const std::vector<DEVQUEUI> &devs)
|
2023-04-20 09:49:48 +00:00
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-07-05 15:55:20 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showDeviceSelect(devs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *apply_current_config(const char *dev_name, SANE_Handle device, LPSANEAPI api) // this function only called after scanner open
|
|
|
|
|
{
|
|
|
|
|
Init();
|
2023-07-05 15:55:20 +00:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-05 13:54:19 +00:00
|
|
|
|
std::string fileName = std::string(cfgpath) + devName + ".cfg";
|
|
|
|
|
gb::sane_config_schm* schm = nullptr;
|
|
|
|
|
std::string name("");
|
|
|
|
|
|
|
|
|
|
if (scanner_cfg_)
|
|
|
|
|
scanner_cfg_->release();
|
|
|
|
|
scanner_cfg_ = new gb::scanner_cfg();
|
|
|
|
|
scanner_cfg_->load_file(fileName.c_str());
|
|
|
|
|
schm = scanner_cfg_->get_scheme();
|
|
|
|
|
hg_settingdialog::apply_scheme(device, api, schm);
|
|
|
|
|
if (schm)
|
|
|
|
|
{
|
|
|
|
|
name = schm->get_scheme_name();
|
|
|
|
|
schm->release();
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-07-05 14:25:29 +00:00
|
|
|
|
name = QObject::tr("default_setting").toStdString();
|
2023-07-05 13:54:19 +00:00
|
|
|
|
|
|
|
|
|
char* cfgn = new char[name.length() + 4];
|
|
|
|
|
strcpy(cfgn, name.c_str());
|
|
|
|
|
|
|
|
|
|
return cfgn;
|
|
|
|
|
/*/
|
2023-05-22 09:41:44 +00:00
|
|
|
|
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-07-05 13:54:19 +00:00
|
|
|
|
//////////////*////////////////////////////////////////////
|
|
|
|
|
}
|
|
|
|
|
int apply_given_config(const char* content, SANE_Handle device, LPSANEAPI api) // 应用指定的配置,content为配置数据流,返回0表示成功
|
|
|
|
|
{
|
|
|
|
|
gb::scanner_cfg* cfg = new gb::scanner_cfg();
|
|
|
|
|
gb::sane_config_schm* schm = nullptr;
|
|
|
|
|
|
|
|
|
|
cfg->load_mem(content, true);
|
|
|
|
|
schm = cfg->get_scheme();
|
|
|
|
|
hg_settingdialog::apply_scheme(device, api, schm);
|
|
|
|
|
if (schm)
|
|
|
|
|
schm->release();
|
|
|
|
|
cfg->release();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 功能: 获取配置文件内容
|
|
|
|
|
//
|
|
|
|
|
// 参数: dev_name - 设备名称
|
|
|
|
|
//
|
|
|
|
|
// name - 指定的文件内容,NULL表示获取默认方案的配置文件内容
|
|
|
|
|
//
|
|
|
|
|
// 返回: 配置文件内容内存,调用twain_ui_free释放
|
|
|
|
|
char* get_config_content(const char* dev_name, const char* name)
|
|
|
|
|
{
|
|
|
|
|
char* buf = nullptr;
|
|
|
|
|
|
|
|
|
|
if (scanner_cfg_)
|
|
|
|
|
{
|
|
|
|
|
std::string text(scanner_cfg_->to_text(true));
|
|
|
|
|
buf = new char[text.length() + 4];
|
|
|
|
|
strcpy(buf, text.c_str());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
buf = new char[20];
|
|
|
|
|
strcpy(buf, "{}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf;
|
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-11-29 06:39:11 +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, std::function<void(int, void*, int)> *notify)
|
2023-04-20 09:49:48 +00:00
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-04-20 09:49:48 +00:00
|
|
|
|
|
2023-11-29 06:39:11 +00:00
|
|
|
|
return g_manager->showSettingUi(device, parent, api, devName, scanner_cfg_, with_scan, callback, notify);
|
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-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-05-08 02:10:48 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showProgressUi(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-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-05-10 12:30:07 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showMessageBoxUi(parent, event, msg, flag);
|
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
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-05-12 06:39:19 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showTwainSrcUi(vds, count, defDsName, parent, ds);
|
2023-05-12 06:39:19 +00:00
|
|
|
|
}
|
2023-05-23 09:40:43 +00:00
|
|
|
|
|
2023-08-12 07:30:33 +00:00
|
|
|
|
int show_srclist_ui(const char **manuNames, const char **sanePaths, HGWindow parent, HGDll *dll,
|
|
|
|
|
SANEAPI* saneApi, char *manuName, unsigned int maxLen)
|
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-08-12 07:30:33 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showSaneSrcUi(manuNames, sanePaths, parent, dll, saneApi, manuName, maxLen);
|
2023-08-12 07:30:33 +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();
|
2023-08-08 03:47:10 +00:00
|
|
|
|
|
|
|
|
|
if (nullptr == g_hThread)
|
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
//g_manager->moveToThread(QThread::currentThread());
|
|
|
|
|
delete g_manager;
|
|
|
|
|
g_manager = nullptr;
|
2023-08-08 03:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-25 01:47:41 +00:00
|
|
|
|
#if 0
|
2023-08-08 03:47:10 +00:00
|
|
|
|
QApplication::exit();
|
|
|
|
|
HGBase_CloseThread(g_hThread);
|
|
|
|
|
g_hThread = NULL;
|
|
|
|
|
HGBase_DestroyEvent(g_event);
|
|
|
|
|
g_event = NULL;
|
2023-08-25 01:47:41 +00:00
|
|
|
|
#endif
|
2023-08-08 03:47:10 +00:00
|
|
|
|
}
|
2023-05-24 06:40:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 09:40:43 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-08-25 08:57:03 +00:00
|
|
|
|
|
|
|
|
|
void pump_ui_message(void* reserved)
|
|
|
|
|
{
|
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
}
|
2023-10-08 07:19:23 +00:00
|
|
|
|
|
|
|
|
|
int abnormal_image(SANE_Image *img, HGWindow parent)
|
|
|
|
|
{
|
2023-10-26 11:09:44 +00:00
|
|
|
|
Init();
|
2023-10-08 07:19:23 +00:00
|
|
|
|
|
2023-10-26 11:09:44 +00:00
|
|
|
|
return g_manager->showAbnormalImgUi(img);
|
2023-10-08 07:19:23 +00:00
|
|
|
|
}
|