From 4b2b8bc2739dcee76dcae737085c811ad89f7cc0 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 26 Sep 2019 03:13:36 +0300 Subject: [PATCH] filesystem unicode refactoring: many fixes and improvements for #121 --- contrib/epee/include/file_io_utils.h | 18 +++++----- contrib/epee/include/string_tools.h | 38 +++++--------------- src/gui/qt-daemon/application/mainwindow.cpp | 22 ++++++------ src/gui/qt-daemon/main.cpp | 10 +++++- 4 files changed, 36 insertions(+), 52 deletions(-) diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h index d7a35e8b..b79db5b5 100644 --- a/contrib/epee/include/file_io_utils.h +++ b/contrib/epee/include/file_io_utils.h @@ -272,19 +272,18 @@ namespace file_io_utils template bool save_string_to_file_throw(const t_string& path_to_file, const std::string& str) { - //std::ofstream fstream; - boost::filesystem::ofstream fstream; - fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fstream.open(convert_utf8_to_wstring_if_needed(path_to_file), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); - fstream << str; - fstream.close(); - return true; + //std::ofstream fstream; + boost::filesystem::ofstream fstream; + fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fstream.open(convert_utf8_to_wstring_if_needed(path_to_file), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); + fstream << str; + fstream.close(); + return true; } template bool save_string_to_file(const t_string& path_to_file, const std::string& str) { - try { return save_string_to_file_throw(path_to_file, str); @@ -301,7 +300,7 @@ namespace file_io_utils } template - bool load_file_to_string(const t_string& path_to_file, std::string& target_str) + bool load_file_to_string(const t_string& path_to_file, std::string& target_str) { try { @@ -326,7 +325,6 @@ namespace file_io_utils fstream.close(); return true; } - catch (...) { return false; diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index e1cb977f..8cd9af86 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -42,6 +42,7 @@ #include #include "warnings.h" #include "auto_val_init.h" +#include "string_coding.h" #ifndef OUT @@ -536,38 +537,15 @@ POP_WARNINGS return module_folder; } //---------------------------------------------------------------------------- -#ifdef _WIN32 - inline std::string get_current_module_path() + inline bool set_module_name_and_folder(const std::string& path_to_process_) { - char pname [5000] = {0}; - GetModuleFileNameA( NULL, pname, sizeof(pname)); - pname[sizeof(pname)-1] = 0; //be happy ;) - return pname; - } -#endif - //---------------------------------------------------------------------------- - inline bool set_module_name_and_folder(const std::string& path_to_process_) - { - std::string path_to_process = path_to_process_; - boost::system::error_code ec; - path_to_process = boost::filesystem::canonical(path_to_process, ec).string(); - #ifdef _WIN32 - path_to_process = get_current_module_path(); - #endif - std::string::size_type a = path_to_process.rfind( '\\' ); - if(a == std::string::npos ) - { - a = path_to_process.rfind( '/' ); - } - if ( a != std::string::npos ) - { - get_current_module_name() = path_to_process.substr(a+1, path_to_process.size()); - get_current_module_folder() = path_to_process.substr(0, a); - return true; - }else - return false; + boost::filesystem::path path(epee::string_encoding::utf8_to_wstring(path_to_process_)); - } + get_current_module_folder() = epee::string_encoding::wstring_to_utf8(path.parent_path().wstring()); + get_current_module_name() = epee::string_encoding::wstring_to_utf8(path.filename().wstring()); + + return true; + } //---------------------------------------------------------------------------- inline bool trim_left(std::string& str) { diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 06c3340e..7d5a834c 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -316,12 +316,12 @@ bool MainWindow::load_app_config() CATCH_ENTRY2(false); } -bool MainWindow::init(const std::string& htmlPath) +bool MainWindow::init(const std::string& html_path) { TRY_ENTRY(); //QtWebEngine::initialize(); - init_tray_icon(htmlPath); - set_html_path(htmlPath); + init_tray_icon(html_path); + set_html_path(html_path); m_backend.subscribe_to_core_events(this); @@ -354,7 +354,7 @@ void MainWindow::on_menu_show() CATCH_ENTRY2(void()); } -void MainWindow::init_tray_icon(const std::string& htmlPath) +void MainWindow::init_tray_icon(const std::string& html_path) { TRY_ENTRY(); if (!QSystemTrayIcon::isSystemTrayAvailable()) @@ -384,14 +384,14 @@ void MainWindow::init_tray_icon(const std::string& htmlPath) //setup icon #ifdef TARGET_OS_MAC - m_normal_icon_path = htmlPath + "/files/app22macos.png"; // X11 tray icon size is 22x22 - m_blocked_icon_path = htmlPath + "/files/app22macos_blocked.png"; // X11 tray icon size is 22x22 + m_normal_icon_path = html_path + "/files/app22macos.png"; // X11 tray icon size is 22x22 + m_blocked_icon_path = html_path + "/files/app22macos_blocked.png"; // X11 tray icon size is 22x22 #else - m_normal_icon_path = htmlPath + "/files/app22windows.png"; // X11 tray icon size is 22x22 - m_blocked_icon_path = htmlPath + "/files/app22windows_blocked.png"; // X11 tray icon size + m_normal_icon_path = html_path + "/files/app22windows.png"; // X11 tray icon size is 22x22 + m_blocked_icon_path = html_path + "/files/app22windows_blocked.png"; // X11 tray icon size #endif //setWindowIcon(QIcon(iconPath.c_str())); - QIcon qi(m_normal_icon_path.c_str()); + QIcon qi( QString::fromWCharArray(epee::string_encoding::utf8_to_wstring(m_normal_icon_path).c_str()) ); qi.setIsMask(true); m_tray_icon->setIcon(qi); m_tray_icon->setToolTip(CURRENCY_NAME_BASE); @@ -411,7 +411,7 @@ void MainWindow::bool_toggle_icon(const QString& param) else path = m_normal_icon_path; - QIcon qi(path.c_str()); + QIcon qi( QString::fromWCharArray(epee::string_encoding::utf8_to_wstring(path).c_str()) ); qi.setIsMask(true); m_tray_icon->setIcon(qi); CATCH_ENTRY2(void()); @@ -771,7 +771,7 @@ bool MainWindow::set_html_path(const std::string& path) TRY_ENTRY(); //init_tray_icon(path); #ifdef _MSC_VER - QString url = QString::fromUtf8(epee::string_encoding::convert_ansii_to_utf8(path).c_str()) + "/index.html"; + QString url = QString::fromUtf8(path.c_str()) + "/index.html"; load_file(url); #else // load_file(QString((std::string("file://") + path + "/index.html").c_str())); diff --git a/src/gui/qt-daemon/main.cpp b/src/gui/qt-daemon/main.cpp index e5aca25a..28e7a564 100644 --- a/src/gui/qt-daemon/main.cpp +++ b/src/gui/qt-daemon/main.cpp @@ -42,8 +42,16 @@ int main(int argc, char *argv[]) #endif +#ifdef WIN32 + WCHAR sz_file_name[MAX_PATH + 1] = L""; + ::GetModuleFileNameW(NULL, sz_file_name, MAX_PATH + 1); + std::string path_to_process_utf8 = epee::string_encoding::wstring_to_utf8(sz_file_name); +#else + std::string path_to_process_utf8 = argv[0]; +#endif + TRY_ENTRY(); - epee::string_tools::set_module_name_and_folder(argv[0]); + epee::string_tools::set_module_name_and_folder(path_to_process_utf8); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #ifdef _MSC_VER #if _MSC_VER >= 1910