From 95ce2ef9207fa86f60e212c1bc54a81008b09a36 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 1 Jul 2018 00:23:16 +0300 Subject: [PATCH] Implement dekstop notifications on Windows (#290) --- .gitignore | 1 + CMakeLists.txt | 12 ++++++++- src/notifications/ManagerWin.cpp | 45 ++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f5d113b..0b2b05e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ build tags .clang_complete +*wintoastlib* # C++ objects and libs diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b01e97..e45fa0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -323,7 +323,17 @@ if (APPLE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation") set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm) elseif (WIN32) - set(SRC_FILES ${SRC_FILES} src/notifications/ManagerWin.cpp) + file(DOWNLOAD + "https://raw.githubusercontent.com/mohabouje/WinToast/41ed1c58d5dce0ee9c01dbdeac05be45358d4f57/src/wintoastlib.cpp" + ${PROJECT_SOURCE_DIR}/src/wintoastlib.cpp + EXPECTED_HASH SHA256=1A1A7CE41C1052B12946798F4A6C67CE1FAD209C967F5ED4D720B173527E2073) + + file(DOWNLOAD + "https://raw.githubusercontent.com/mohabouje/WinToast/41ed1c58d5dce0ee9c01dbdeac05be45358d4f57/src/wintoastlib.h" + ${PROJECT_SOURCE_DIR}/include/wintoastlib.h + EXPECTED_HASH SHA256=b4481023c5782733795838be22bf1a75f45d87458cd4d9a5a75f664a146eea11) + + set(SRC_FILES ${SRC_FILES} src/notifications/ManagerWin.cpp src/wintoastlib.cpp) else () set(SRC_FILES ${SRC_FILES} src/notifications/ManagerLinux.cpp) endif () diff --git a/src/notifications/ManagerWin.cpp b/src/notifications/ManagerWin.cpp index a913128..7503e85 100644 --- a/src/notifications/ManagerWin.cpp +++ b/src/notifications/ManagerWin.cpp @@ -1,7 +1,48 @@ #include "notifications/Manager.h" +#include "wintoastlib.h" + +using namespace WinToastLib; + +class CustomHandler : public IWinToastHandler +{ +public: + void toastActivated() const {} + void toastActivated(int) const {} + void toastFailed() const { std::wcout << L"Error showing current toast" << std::endl; } + void toastDismissed(WinToastDismissalReason) const {} +}; + +namespace { +bool isInitialized = false; + +void +init() +{ + isInitialized = true; + + WinToast::instance()->setAppName(L"Nheko"); + WinToast::instance()->setAppUserModelId(WinToast::configureAUMI(L"nheko", L"nheko")); + if (!WinToast::instance()->initialize()) + std::wcout << "Your system in not compatible with toast notifications\n"; +} +} void -NotificationsManager::postNotification(const QString &, const QString &, const QString &) +NotificationsManager::postNotification(const QString &room, const QString &user, const QString &msg) { - // TODO: To be implemented + if (!isInitialized) + init(); + + auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02); + if (room != user) + templ.setTextField(QString("%1 - %2").arg(user).arg(room).toStdWString(), + WinToastTemplate::FirstLine); + else + templ.setTextField(QString("%1").arg(user).toStdWString(), + WinToastTemplate::FirstLine); + templ.setTextField(QString("%1").arg(msg).toStdWString(), WinToastTemplate::SecondLine); + // TODO: implement room or user avatar + // templ.setImagePath(L"C:/example.png"); + + WinToast::instance()->showToast(templ, new CustomHandler()); }