Skip to content

Commit

Permalink
Implement messageBox function for script API in qimageuploader
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Mar 7, 2024
1 parent 3387f2c commit f70b590
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 129 deletions.
11 changes: 6 additions & 5 deletions Dist/create_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def write_json_header(jsonfile, json_builds_file_name, source_dir, version_heade
json_builds_data = {

}
last_commit_hash = json_builds_data.get("last_commit_hash");
last_commit_hash = json_builds_data.get("last_commit_hash")

json_builds_data["last_commit_hash"] = version_header_defines['IU_COMMIT_HASH']

Expand Down Expand Up @@ -438,14 +438,15 @@ def modify_update_file(component_name, filepath, version_header_defines, json_da
shutil.copyfile("../Source/versioninfo.h.dist", VERSION_HEADER_FILE)

version_file_abs_path = os.path.abspath(VERSION_HEADER_FILE)
version_header_defines = generate_version_header(VERSION_HEADER_FILE, True)
generate_version_header(VERSION_HEADER_FILE, True)
repo_dir_abs = os.path.abspath(repo_dir)
shutil.copyfile(VERSION_HEADER_FILE, repo_dir + "/Source/" + VERSION_HEADER_FILE)
app_ver = version_header_defines["IU_APP_VER"]
build_number = version_header_defines["IU_BUILD_NUMBER"]

dist_directory = os.path.dirname(os.path.realpath(__file__))
#with cwd(repo_dir):
generate_version_header(repo_dir_abs + "/Source/" + VERSION_HEADER_FILE, False)
version_header_defines = generate_version_header(repo_dir_abs + "/Source/" + VERSION_HEADER_FILE, False)
app_ver = version_header_defines["IU_APP_VER"]
build_number = version_header_defines["IU_BUILD_NUMBER"]

proc = subprocess.run("wsl -e /bin/bash generate_mo.sh", cwd=repo_dir_abs + "/Lang/")
if proc.returncode !=0:
Expand Down
1 change: 1 addition & 0 deletions Source/CLI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_executable(CLI
ConsoleScriptDialogProvider.cpp
ConsoleScriptDialogProvider.h
../Core/Settings/CliSettings.cpp
ConsoleScriptFunctionsImpl.cpp
${RESOURCE_LIST}
)

Expand Down
41 changes: 40 additions & 1 deletion Source/CLI/ConsoleScriptDialogProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "ConsoleScriptDialogProvider.h"

#include <iostream>
#include <map>

#include "Core/Network/NetworkClient.h"
#include "Core/Utils/DesktopUtils.h"
#include "Core/Utils/CoreUtils.h"
#include "Core/Utils/ConsoleUtils.h"
#include <iostream>
#include "Core/Utils/StringUtils.h"

std::string ConsoleScriptDialogProvider::askUserCaptcha(NetworkClient* nm, const std::string& url) {
std::lock_guard<std::mutex> guard(ConsoleUtils::instance()->getOutputMutex());
Expand All @@ -27,3 +30,39 @@ std::string ConsoleScriptDialogProvider::inputDialog(const std::string& text, co
std::cin >> result;
return result;
}

std::string ConsoleScriptDialogProvider::messageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) {
std::cerr << "----------";
#ifdef _WIN32
std::wcerr << IuCoreUtils::Utf8ToWstring(title);
#else
std::cerr << IuCoreUtils::Utf8ToSystemLocale(title);
#endif
std::cerr << "----------" << std::endl;
#ifdef _WIN32
std::wcerr << IuCoreUtils::Utf8ToWstring(message) << std::endl;;
#else
std::cerr << IuCoreUtils::Utf8ToSystemLocale(message) << std::endl;;
#endif
if (buttons.empty() || buttons == "OK") {
getc(stdin);
return "OK";
}
else {
std::vector<std::string> tokens;
std::map<char, std::string> buttonsMap;
IuStringUtils::Split(buttons, "_", tokens, 10);
for (int i = 0; i < tokens.size(); i++) {
if (i != 0) {
std::cerr << "/";
}
buttonsMap[tokens[i][0]] = tokens[i];
std::cerr << "(" << tokens[i][0] << ")" << IuStringUtils::toLower(tokens[i]).c_str() + 1;
}
std::cerr << ": ";
char res;
std::cin >> res;
res = toupper(res);
return buttonsMap[res];
}
}
7 changes: 4 additions & 3 deletions Source/CLI/ConsoleScriptDialogProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

class ConsoleScriptDialogProvider : public IDialogProvider {
public:
virtual std::string askUserCaptcha(NetworkClient *nm, const std::string& url) override;
virtual std::string inputDialog(const std::string& text, const std::string& defaultValue) override;
std::string askUserCaptcha(NetworkClient *nm, const std::string& url) override;
std::string inputDialog(const std::string& text, const std::string& defaultValue) override;
std::string ConsoleScriptDialogProvider::messageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) override;
};
#endif
#endif
16 changes: 16 additions & 0 deletions Source/CLI/ConsoleScriptFunctionsImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "Core/Scripting/API/ScriptFunctionsImpl.h"

#include "Core/Utils/StringUtils.h"

namespace ScriptAPI::Impl {

std::string GetAppLanguageImpl() {
return "en";
}

std::string GetAppLocaleImpl() {
return "en_US";
}

}
4 changes: 2 additions & 2 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})

if (MSVC)
add_definitions("/MP")
# add_definitions("/MP")
add_compile_options(/wd4091) # disable warning C4091: 'typedef ': ignored on left of '*' when no variable is declared
endif()

Expand Down Expand Up @@ -411,4 +411,4 @@ if(IU_BUILD_QIMAGEUPLOADER)
add_subdirectory(qimageuploader)
endif()
#enable_testing()
add_subdirectory(Tests)
add_subdirectory(Tests)
6 changes: 3 additions & 3 deletions Source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ if (IU_ENABLE_WEBVIEW2)
target_link_libraries(iucore PUBLIC WebView2LoaderStatic.lib version.lib)
endif()

if(MSVC)
target_compile_options(iucore PRIVATE "/MP")
endif()
#if(MSVC)
# target_compile_options(iucore PRIVATE "/MP")
#endif()

set_target_properties(iucore PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/)
104 changes: 5 additions & 99 deletions Source/Core/Scripting/API/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "Core/i18n/Translator.h"
#include "Core/Settings/BasicSettings.h"
#include "Core/Utils/SystemUtils.h"
#include "ScriptFunctionsImpl.h"

using namespace Sqrat;

Expand Down Expand Up @@ -85,7 +86,7 @@ Sqrat::Table GetAppVersion() {
return res;
}

const std::string GetCurrentScriptFileName()
std::string GetCurrentScriptFileName()
{
return GetScriptName(GetCurrentThreadVM());
}
Expand Down Expand Up @@ -183,7 +184,6 @@ std::string InputDialog(const std::string& text, const std::string& defaultValue
return ServiceLocator::instance()->dialogProvider()->inputDialog(text, defaultValue);
}


std::string GetFileMimeType(const std::string& filename)
{
return IuCoreUtils::GetFileMimeType(filename);
Expand Down Expand Up @@ -265,94 +265,8 @@ void DebugMessage(const std::string& msg, bool isResponseBody) {
ServiceLocator::instance()->uploadErrorHandler()->DebugMessage(msg,isResponseBody);
}

std::string MessageBox( const std::string& message, const std::string &title,const std::string& buttons , const std::string& type) {
#if defined(_WIN32) && !defined(IU_CLI)
UINT uButtons = MB_OK;
if ( buttons == "ABORT_RETRY_IGNORE") {
uButtons = MB_ABORTRETRYIGNORE;
} else if ( buttons == "CANCEL_TRY_CONTINUE") {
uButtons = MB_CANCELTRYCONTINUE;
} else if ( buttons == "OK_CANCEL") {
uButtons = MB_OKCANCEL;
} else if ( buttons == "RETRY_CANCEL") {
uButtons = MB_RETRYCANCEL;
} else if ( buttons == "YES_NO") {
uButtons = MB_YESNO;
}else if ( buttons == "YES_NO_CANCEL") {
uButtons = MB_YESNOCANCEL;
}
UINT icon = 0;
if ( type == "EXCLAMATION") {
icon = MB_ICONEXCLAMATION;
} else if ( type == "WARNING") {
icon = MB_ICONWARNING;
} else if ( type == "INFORMATION") {
icon = MB_ICONINFORMATION;
} else if ( type == "QUESTION") {
icon = MB_ICONQUESTION;
} else if ( type == "ERROR") {
icon = MB_ICONERROR;
}
int res = ::MessageBox(GetActiveWindow(), IuCoreUtils::Utf8ToWstring(message).c_str(), IuCoreUtils::Utf8ToWstring(title).c_str(), uButtons |icon );
if ( res == IDABORT ) {
return "ABORT";
} else if ( res == IDCANCEL ) {
return "CANCEL";
} else if ( res == IDCONTINUE ) {
return "CONTINUE";
} else if ( res == IDIGNORE ) {
return "IGNORE";
} else if ( res == IDNO ) {
return "NO";
} else if ( res == IDOK ) {
return "OK";
} else if ( res == IDYES ) {
return "YES";
}
else if ( res == IDRETRY ) {
return "TRY";
} else if ( res == IDTRYAGAIN ) {
return "TRY";
}
return "";

#else
std::cerr<<"----------";
#ifdef _WIN32
std::wcerr<<IuCoreUtils::Utf8ToWstring(title);
#else
std::cerr<<IuCoreUtils::Utf8ToSystemLocale(title);
#endif
std::cerr<<"----------"<<std::endl;
#ifdef _WIN32
std::wcerr<<IuCoreUtils::Utf8ToWstring(message)<<std::endl;;
#else
std::cerr<<IuCoreUtils::Utf8ToSystemLocale(message)<<std::endl;;
#endif
if ( buttons.empty() || buttons == "OK") {
getc(stdin);
return "OK";
} else {


std::vector<std::string> tokens;
std::map<char,std::string> buttonsMap;
IuStringUtils::Split(buttons,"_", tokens,10);
for(int i = 0; i< tokens.size(); i++ ) {
if ( i !=0 ) {
std::cerr<< "/";
}
buttonsMap[tokens[i][0]] = tokens[i];
std::cerr<< "("<<tokens[i][0]<<")"<<IuStringUtils::toLower(tokens[i]).c_str()+1;
}
std::cerr<<": ";
char res;
std::cin >> res;
res = toupper(res);
return buttonsMap[res];
}

#endif
std::string MessageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) {
return ServiceLocator::instance()->dialogProvider()->messageBox(message, title, buttons, type);
}

void parseJSONObj(const Json::Value& root, Sqrat::Array& obj);
Expand Down Expand Up @@ -504,15 +418,7 @@ int64_t ScriptGetFileSize(const std::string& filename) {
}

std::string GetAppLanguage() {
#ifndef IU_CLI
ITranslator* translator = ServiceLocator::instance()->translator();
if ( !translator ) {
LOG(ERROR) << "No translator set";
} else {
return translator->getCurrentLanguage();
}
#endif
return "en";
return Impl::GetAppLanguageImpl();
}

std::string GetAppLocale() {
Expand Down
8 changes: 8 additions & 0 deletions Source/Core/Scripting/API/ScriptFunctionsImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>

namespace ScriptAPI::Impl{
std::string GetAppLanguageImpl();
std::string GetAppLocaleImpl();
}
4 changes: 3 additions & 1 deletion Source/Core/Scripting/DialogProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class IDialogProvider {
virtual ~IDialogProvider() = default;
virtual std::string askUserCaptcha(NetworkClient *nm, const std::string& url) = 0;
virtual std::string inputDialog(const std::string& text, const std::string& defaultValue) = 0;
virtual std::string messageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) = 0;

std::mutex& getMutex() { return dialogMutex_; }
protected:
std::mutex dialogMutex_;
};

#endif
#endif
54 changes: 53 additions & 1 deletion Source/Func/WtlScriptDialogProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,56 @@ std::string WtlScriptDialogProvider::inputDialog(const std::string& text, const
return W2U(dlg.getValue());
}
return {};
}
}

std::string WtlScriptDialogProvider::messageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) {
UINT uButtons = MB_OK;
if (buttons == "ABORT_RETRY_IGNORE") {
uButtons = MB_ABORTRETRYIGNORE;
} else if (buttons == "CANCEL_TRY_CONTINUE") {
uButtons = MB_CANCELTRYCONTINUE;
} else if (buttons == "OK_CANCEL") {
uButtons = MB_OKCANCEL;
} else if (buttons == "RETRY_CANCEL") {
uButtons = MB_RETRYCANCEL;
} else if (buttons == "YES_NO") {
uButtons = MB_YESNO;
} else if (buttons == "YES_NO_CANCEL") {
uButtons = MB_YESNOCANCEL;
}
UINT icon = 0;
if (type == "EXCLAMATION") {
icon = MB_ICONEXCLAMATION;
} else if (type == "WARNING") {
icon = MB_ICONWARNING;
} else if (type == "INFORMATION") {
icon = MB_ICONINFORMATION;
} else if (type == "QUESTION") {
icon = MB_ICONQUESTION;
} else if (type == "ERROR") {
icon = MB_ICONERROR;
}
IProgramWindow* window = ServiceLocator::instance()->programWindow();

int res = ::MessageBox(window ? window->getHandle() : ::GetActiveWindow(), IuCoreUtils::Utf8ToWstring(message).c_str(), IuCoreUtils::Utf8ToWstring(title).c_str(), uButtons | icon);
if (res == IDABORT) {
return "ABORT";
} else if (res == IDCANCEL) {
return "CANCEL";
} else if (res == IDCONTINUE) {
return "CONTINUE";
} else if (res == IDIGNORE) {
return "IGNORE";
} else if (res == IDNO) {
return "NO";
} else if (res == IDOK) {
return "OK";
} else if (res == IDYES) {
return "YES";
} else if (res == IDRETRY) {
return "TRY";
} else if (res == IDTRYAGAIN) {
return "TRY";
}
return {};
}
4 changes: 3 additions & 1 deletion Source/Func/WtlScriptDialogProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class WtlScriptDialogProvider : public IDialogProvider {
public:
std::string askUserCaptcha(NetworkClient *nm, const std::string& url) override;
std::string inputDialog(const std::string& text, const std::string& defaultValue) override;
std::string messageBox(const std::string& message, const std::string& title, const std::string& buttons, const std::string& type) override;

};
#endif
#endif
Loading

0 comments on commit f70b590

Please sign in to comment.