diff --git a/plugins/windows-notification/api/include/converter.hpp b/plugins/windows-notification/api/include/converter.hpp index 743b8011..0eb1488d 100644 --- a/plugins/windows-notification/api/include/converter.hpp +++ b/plugins/windows-notification/api/include/converter.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include +#include -std::wstring char_to_wstr(const gchar* str); -char* wstr_to_char(const std::wstring& wstr); \ No newline at end of file +std::wstring sview_to_wstr(const std::string_view str); +char* wsview_to_char(const std::wstring_view wstr); \ No newline at end of file diff --git a/plugins/windows-notification/api/src/converter.cpp b/plugins/windows-notification/api/src/converter.cpp index 93b85479..f97cd4f3 100644 --- a/plugins/windows-notification/api/src/converter.cpp +++ b/plugins/windows-notification/api/src/converter.cpp @@ -3,7 +3,7 @@ #include "converter.hpp" // Convert a wide Unicode string to an UTF8 string -std::string wstr_to_str(const std::wstring& wstr) +std::string wstr_to_str(const std::wstring_view wstr) { if(wstr.empty()) { @@ -15,7 +15,7 @@ std::string wstr_to_str(const std::wstring& wstr) return strTo; } -char* wstr_to_char(const std::wstring& wstr) +char* wsview_to_char(const std::wstring_view wstr) { if(wstr.empty()) { @@ -28,7 +28,7 @@ char* wstr_to_char(const std::wstring& wstr) } // Convert an UTF8 string to a wide Unicode String -std::wstring std_to_wstr(const std::string &str) +std::wstring sview_to_wstr(const std::string_view str) { if(str.empty()) { @@ -38,16 +38,4 @@ std::wstring std_to_wstr(const std::string &str) std::wstring wstrTo(final_size, 0); MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), wstrTo.data(), final_size); return wstrTo; -} - -std::wstring char_to_wstr(const char* str) // TODO: how to be safe from non-null terminated strings? -{ - if(str == nullptr) - { - return std::wstring(); - } - int final_size = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), nullptr, 0); - std::wstring wstrTo(final_size, 0); - MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), wstrTo.data(), final_size); - return wstrTo; } \ No newline at end of file diff --git a/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp b/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp index 2d20e759..00cad570 100644 --- a/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp +++ b/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -147,7 +148,7 @@ winrtWindowsUINotificationsToastNotification* winrt_windows_ui_notifications_toa auto ret = static_cast(g_object_new (WINRT_TYPE_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION, NULL)); winrt::Windows::Data::Xml::Dom::XmlDocument xmlDoc; - xmlDoc.LoadXml(char_to_wstr(doc)); + xmlDoc.LoadXml(sview_to_wstr(doc)); winrt_windows_ui_notifications_toast_notification_set_internal(ret, winrt::Windows::UI::Notifications::ToastNotification{ xmlDoc }); return ret; } @@ -170,7 +171,7 @@ void winrt_windows_ui_notifications_toast_notification_set_Tag(winrtWindowsUINot { g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); - winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag(char_to_wstr(value)); + winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag(sview_to_wstr(value)); } /** @@ -185,14 +186,14 @@ char* winrt_windows_ui_notifications_toast_notification_get_Tag(winrtWindowsUINo { g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE); - return wstr_to_char(std::wstring(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag())); + return wsview_to_char(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag()); } void winrt_windows_ui_notifications_toast_notification_set_Group(winrtWindowsUINotificationsToastNotification* self, const char* value) { g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); - winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group(char_to_wstr(value)); + winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group(sview_to_wstr(value)); } /** @@ -207,7 +208,7 @@ char* winrt_windows_ui_notifications_toast_notification_get_Group(winrtWindowsUI { g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE); - return wstr_to_char(std::wstring(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group())); + return wsview_to_char(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group()); } winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackActivated callback, void* context, void(*free)(void*)) @@ -248,7 +249,7 @@ winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(win } std::cout << "Notification activated!" << std::endl; - priv->activated.callback(wstr_to_char(arguments.data()), nullptr, user_input.size(), priv->activated.context); + priv->activated.callback(wsview_to_char(arguments.data()), nullptr /* user_input */ , 0 /* user_input.size() */, priv->activated.context); }); priv->activated.token = winrt_event_token_new_from_token(&token); return priv->activated.token; diff --git a/plugins/windows-notification/api/src/shortcutcreator.cpp b/plugins/windows-notification/api/src/shortcutcreator.cpp index 356fea48..2ef8c6fc 100644 --- a/plugins/windows-notification/api/src/shortcutcreator.cpp +++ b/plugins/windows-notification/api/src/shortcutcreator.cpp @@ -150,7 +150,7 @@ extern "C" { gboolean TryCreateShortcut(const gchar* aumid) { - auto result = char_to_wstr(aumid); + auto result = sview_to_wstr(aumid); if (result.empty()) { return FALSE; diff --git a/plugins/windows-notification/api/src/win32.cpp b/plugins/windows-notification/api/src/win32.cpp index 840d3416..15612581 100644 --- a/plugins/windows-notification/api/src/win32.cpp +++ b/plugins/windows-notification/api/src/win32.cpp @@ -6,22 +6,22 @@ std::optional GetCurrentModulePath() { - std::array exePath; + std::wstring exePath(MAX_PATH, 0); auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size()); if (charWritten > 0) { - return std::wstring(exePath.data()); + return exePath; } return std::nullopt; } std::optional GetShortcutPath() { - std::array shortcutPath; + std::wstring shortcutPath(MAX_PATH, 0); auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size()); if (charWritten > 0) { - return std::wstring(shortcutPath.data()); + return shortcutPath; } return std::nullopt; } @@ -49,7 +49,7 @@ extern "C" gboolean SetAppModelID(const gchar* aumid) { - auto result = char_to_wstr(aumid); + auto result = sview_to_wstr(aumid); if (result.empty()) { return FALSE; diff --git a/plugins/windows-notification/src/plugin.vala b/plugins/windows-notification/src/plugin.vala index a5704fd8..3ba8049f 100644 --- a/plugins/windows-notification/src/plugin.vala +++ b/plugins/windows-notification/src/plugin.vala @@ -27,7 +27,7 @@ public class Plugin : RootInterface, Object { { var m = new winrt.Windows.UI.Notifications.ToastNotification("Test"); - var token = m.Activated(() => { + var token = m.Activated((c, d) => { var i = 2; }); m.RemoveActivatedAction(token);