From 79cd4ccc87c8a4f380bd29139c71c0995e565913 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Tue, 29 Dec 2015 17:40:24 +0000 Subject: Toaster: big refactoring git-svn-id: http://svn.miranda-ng.org/main/trunk@15964 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Toaster/src/services.cpp | 38 ++-------- plugins/Toaster/src/stdafx.h | 2 - plugins/Toaster/src/toast_event_handler.cpp | 111 ---------------------------- plugins/Toaster/src/toast_event_handler.h | 37 ---------- plugins/Toaster/src/toast_notification.cpp | 58 ++++++++++----- plugins/Toaster/src/toast_notification.h | 51 ++++++++++--- 6 files changed, 87 insertions(+), 210 deletions(-) delete mode 100644 plugins/Toaster/src/toast_event_handler.cpp delete mode 100644 plugins/Toaster/src/toast_event_handler.h (limited to 'plugins/Toaster/src') diff --git a/plugins/Toaster/src/services.cpp b/plugins/Toaster/src/services.cpp index 3f701b86f9..16c89edcb1 100644 --- a/plugins/Toaster/src/services.cpp +++ b/plugins/Toaster/src/services.cpp @@ -53,34 +53,17 @@ void __stdcall ShowToastNotification(void* p) } } - ToastNotification *notification = new ToastNotification(td->tszText, td->tszTitle, imagePath); - - HRESULT hr = notification->Initialize(); - if (SUCCEEDED(hr)) - { - ToastHandlerData *thd = new ToastHandlerData(); - thd->hContact = td->hContact; - thd->vPopupData = td->vPopupData; - thd->pPopupProc = td->pPopupProc; - thd->tstNotification = notification; - - notification->Show(thd); - lstNotifications.insert(notification); - } - else - { - delete notification; - } + new ToastNotification(td->tszText, td->tszTitle, imagePath, td->hContact, td->pPopupProc, td->vPopupData); } static INT_PTR GetPopupData(WPARAM wParam, LPARAM) { - return (INT_PTR)((ToastEventHandler*)wParam)->GetPluginData(); + return (INT_PTR)((ToastNotification*)wParam)->GetPluginData(); } static INT_PTR GetPopupContact(WPARAM wParam, LPARAM) { - return (INT_PTR)((ToastEventHandler*)wParam)->GetContact(); + return (INT_PTR)((ToastNotification*)wParam)->GetContact(); } static INT_PTR CreatePopup(WPARAM wParam, LPARAM) @@ -261,23 +244,18 @@ static INT_PTR ShowMessage(WPARAM wParam, LPARAM lParam) static INT_PTR HideToast(WPARAM, LPARAM lParam) { - extern LIST lstHandlers; - ToastEventHandler* handler = reinterpret_cast(lParam); - if (lstHandlers.getIndex(handler) != -1) + ToastNotification* pNotification = reinterpret_cast(lParam); + if (lstNotifications.getIndex(pNotification) != -1) { - ToastNotification* notification = static_cast(handler->GetToastNotification()); - if (lstNotifications.getIndex(notification) != -1) - { - notification->Hide(); - } + lstNotifications.remove(pNotification); } return 0; } void __stdcall HideAllToasts(void*) { mir_cslock lck(csNotifications); - for (int i = 0; i < lstNotifications.getCount(); i++) - lstNotifications[i].Hide(); + while(lstNotifications.getCount()) + lstNotifications.remove(0); } void InitServices() diff --git a/plugins/Toaster/src/stdafx.h b/plugins/Toaster/src/stdafx.h index 2088db9016..57c860a0f3 100644 --- a/plugins/Toaster/src/stdafx.h +++ b/plugins/Toaster/src/stdafx.h @@ -32,11 +32,9 @@ const wchar_t AppUserModelID[] = _T("MirandaNG"); DEFINE_PROPERTYKEY(PKEY_AppUserModel_ID, 0x9F4C2855, 0x9F79, 0x4B39, 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, 5); #define MODULE "Toaster" - #define CHECKHR(x) if (FAILED(x)) return x; #include "string_reference_wrapper.h" -#include "toast_event_handler.h" #include "toast_notification.h" #include "add_to_start_menu.h" #include "options.h" diff --git a/plugins/Toaster/src/toast_event_handler.cpp b/plugins/Toaster/src/toast_event_handler.cpp deleted file mode 100644 index 33a3c3e1c3..0000000000 --- a/plugins/Toaster/src/toast_event_handler.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "stdafx.h" - -using namespace ABI::Windows::UI::Notifications; -using namespace Microsoft::WRL; - -LIST lstHandlers(2, PtrKeySortT); - -ToastEventHandler::ToastEventHandler(_In_ ToastHandlerData *pData) : _ref(0), _thd(pData) -{ - lstHandlers.insert(this); - CallPopupProc(UM_INITPOPUP); -} - -ToastEventHandler::~ToastEventHandler() -{ - lstHandlers.remove(this); - CallPopupProc(UM_FREEPLUGINDATA); -} - -IFACEMETHODIMP_(ULONG) ToastEventHandler::AddRef() -{ - return InterlockedIncrement(&_ref); -} - -IFACEMETHODIMP_(ULONG) ToastEventHandler::Release() -{ - ULONG l = InterlockedDecrement(&_ref); - if (l == 0) delete this; - return l; -} - -IFACEMETHODIMP ToastEventHandler::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void **ppv) -{ - if (IsEqualIID(riid, IID_IUnknown)) - *ppv = (IUnknown*)(DesktopToastActivatedEventHandler*)(this); - else if (IsEqualIID(riid, __uuidof(DesktopToastActivatedEventHandler))) - *ppv = (DesktopToastActivatedEventHandler*)(this); - else if (IsEqualIID(riid, __uuidof(DesktopToastDismissedEventHandler))) - *ppv = (DesktopToastDismissedEventHandler*)(this); - else if (IsEqualIID(riid, __uuidof(DesktopToastFailedEventHandler))) - *ppv = (DesktopToastFailedEventHandler*)(this); - else *ppv = nullptr; - - if (*ppv) ((IUnknown*)*ppv)->AddRef(); - return (*ppv ? S_OK : E_NOINTERFACE); -} - -IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification*, _In_ IInspectable*) -{ - CallPopupProc(WM_COMMAND); - DestroyNotification(); - return S_OK; -} - -IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification*, _In_ IToastDismissedEventArgs* e) -{ - ToastDismissalReason tdr; - CHECKHR(e->get_Reason(&tdr)); - - switch (tdr) - { - case ToastDismissalReason_UserCanceled: - { - CallPopupProc(WM_CONTEXTMENU); - _thd->tstNotification->Hide(); - break; - } - case ToastDismissalReason_ApplicationHidden: - case ToastDismissalReason_TimedOut: - DestroyNotification(); - break; - } - - return S_OK; -} - -IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification*, _In_ IToastFailedEventArgs*) -{ - DestroyNotification(); - return S_OK; -} - -void ToastEventHandler::DestroyNotification() -{ - if (_thd->tstNotification != nullptr) - { - mir_cslock lck(csNotifications); - lstNotifications.remove(_thd->tstNotification); - } - _thd->tstNotification = nullptr; -} - -LRESULT ToastEventHandler::CallPopupProc(UINT uMsg) -{ - return (_thd->pPopupProc ? _thd->pPopupProc((HWND)this, uMsg, 0, 0) : 0); -} - -void* ToastEventHandler::GetPluginData() -{ - return _thd->vPopupData; -} - -MCONTACT ToastEventHandler::GetContact() -{ - return _thd->hContact; -} - -void* ToastEventHandler::GetToastNotification() -{ - return _thd->tstNotification; -} \ No newline at end of file diff --git a/plugins/Toaster/src/toast_event_handler.h b/plugins/Toaster/src/toast_event_handler.h deleted file mode 100644 index 2626436540..0000000000 --- a/plugins/Toaster/src/toast_event_handler.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _TOAST_EVENT_HANDLER_H_ -#define _TOAST_EVENT_HANDLER_H_ - -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastActivatedEventHandler; -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastDismissedEventHandler; -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastFailedEventHandler; - -struct ToastHandlerData; - -class ToastEventHandler : public Microsoft::WRL::Implements -{ -public: - explicit ToastEventHandler(_In_ ToastHandlerData*); - ~ToastEventHandler(); - - IFACEMETHODIMP_(ULONG) AddRef(); - IFACEMETHODIMP_(ULONG) Release(); - - IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppv); - - IFACEMETHODIMP Invoke(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ IInspectable*); - IFACEMETHODIMP Invoke(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastDismissedEventArgs*); - IFACEMETHODIMP Invoke(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastFailedEventArgs*); - - void* GetPluginData(); - MCONTACT GetContact(); - void* GetToastNotification(); - -private: - ULONG _ref; - std::unique_ptr _thd; - - void DestroyNotification(); - LRESULT CallPopupProc(UINT uMsg); -}; - -#endif //_TOAST_EVENT_HANDLER_H_ \ No newline at end of file diff --git a/plugins/Toaster/src/toast_notification.cpp b/plugins/Toaster/src/toast_notification.cpp index bc9f6a7b98..0b48c5c235 100644 --- a/plugins/Toaster/src/toast_notification.cpp +++ b/plugins/Toaster/src/toast_notification.cpp @@ -1,22 +1,34 @@ #include "stdafx.h" - +#include using namespace Microsoft::WRL; -ToastNotification::ToastNotification(_In_ wchar_t* text, _In_ wchar_t* caption, _In_ wchar_t* imagePath) - : _text(text), _caption(caption), _imagePath(imagePath) -{} - -ToastNotification::~ToastNotification() +ToastNotification::ToastNotification( + _In_ wchar_t* text, + _In_ wchar_t* caption, + _In_ wchar_t* imagePath, + MCONTACT hContact, + WNDPROC pWndProc, + void *pData ) + : _text(text), _caption(caption), _imagePath(imagePath), _hContact(hContact), _pfnPopupProc(pWndProc), _pvPopupData(pData) { + lstNotifications.insert(this); + Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), ¬ificationManager); + notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(::AppUserModelID).Get(), ¬ifier); + Create(¬ification); + + notification->add_Activated(Callback(this, &ToastNotification::OnActivate).Get(), &_ertActivated); + notification->add_Dismissed(Callback(this, &ToastNotification::OnDismiss).Get(), &_ertDismissed); + notification->add_Failed(Callback(this, &ToastNotification::OnFail).Get(), &_ertFailed); + notifier->Show(notification.Get()); + } -HRESULT ToastNotification::Initialize() +ToastNotification::~ToastNotification() { - CHECKHR(Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), ¬ificationManager)); - CHECKHR(notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(::AppUserModelID).Get(), ¬ifier)) - return Create(¬ification); + notifier->Hide(notification.Get()); } + HRESULT ToastNotification::CreateXml(_Outptr_ ABI::Windows::Data::Xml::Dom::IXmlDocument** xml) { ComPtr xmlDocument; @@ -66,19 +78,25 @@ HRESULT ToastNotification::Create(_Outptr_ ABI::Windows::UI::Notifications::IToa return factory->CreateToastNotification(xml.Get(), _notification); } -HRESULT ToastNotification::Show(_In_ ToastHandlerData* thd) +HRESULT ToastNotification::OnActivate(_In_ ABI::Windows::UI::Notifications::IToastNotification*, IInspectable*) { - ComPtr eventHandler(new ToastEventHandler(thd)); - - CHECKHR(notification->add_Activated(eventHandler.Get(), &_ertActivated)); - CHECKHR(notification->add_Dismissed(eventHandler.Get(), &_ertDismissed)); - CHECKHR(notification->add_Failed(eventHandler.Get(), &_ertFailed)); - - return notifier->Show(notification.Get()); + CallPopupProc(WM_COMMAND); + Destroy(); + return S_OK; } -HRESULT ToastNotification::Hide() +HRESULT ToastNotification::OnDismiss(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastDismissedEventArgs *e) { - return notifier->Hide(notification.Get()); + ABI::Windows::UI::Notifications::ToastDismissalReason tdr; + CHECKHR(e->get_Reason(&tdr)); + if (tdr == ABI::Windows::UI::Notifications::ToastDismissalReason_UserCanceled) + CallPopupProc(WM_CONTEXTMENU); + Destroy(); + return S_OK; } +HRESULT ToastNotification::OnFail(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastFailedEventArgs*) +{ + Destroy(); + return S_OK; +} \ No newline at end of file diff --git a/plugins/Toaster/src/toast_notification.h b/plugins/Toaster/src/toast_notification.h index 4399248848..f57d397b5b 100644 --- a/plugins/Toaster/src/toast_notification.h +++ b/plugins/Toaster/src/toast_notification.h @@ -1,11 +1,22 @@ -#ifndef _TOAST_NOTIFICATION_H_ -#define _TOAST_NOTIFICATION_H_ +#pragma once +typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastActivatedEventHandler; +typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastDismissedEventHandler; +typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastFailedEventHandler; -struct ToastHandlerData; +typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable ToastActivationHandler; +typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler; +typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastFailedEventArgs ToastFailHandler; class ToastNotification { private: + + MCONTACT _hContact; + + WNDPROC _pfnPopupProc; + void *_pvPopupData; + + wchar_t* _text; wchar_t* _caption; wchar_t* _imagePath; @@ -22,12 +33,32 @@ private: HRESULT Create(_Outptr_ ABI::Windows::UI::Notifications::IToastNotification** notification); public: - ToastNotification(_In_ wchar_t* text, _In_ wchar_t* caption = nullptr, _In_ wchar_t* imagePath = nullptr); - ~ToastNotification(); + ToastNotification(_In_ wchar_t* text, _In_ wchar_t* caption = nullptr, _In_ wchar_t* imagePath = nullptr, MCONTACT hContact = 0, WNDPROC pWndProc = nullptr, void *pData = nullptr); + ToastNotification::~ToastNotification(); - HRESULT Initialize(); - HRESULT Show(_In_ ToastHandlerData*); - HRESULT Hide(); -}; + inline void Destroy() + { + extern OBJLIST lstNotifications; + lstNotifications.remove(this); + } -#endif //_TOAST_NOTIFICATION_H_ \ No newline at end of file + HRESULT OnActivate(_In_ ABI::Windows::UI::Notifications::IToastNotification*, IInspectable* aInspectable); + HRESULT OnDismiss(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastDismissedEventArgs*); + HRESULT OnFail(_In_ ABI::Windows::UI::Notifications::IToastNotification*, _In_ ABI::Windows::UI::Notifications::IToastFailedEventArgs*); + + + inline void* GetPluginData() + { + return _pvPopupData; + } + inline MCONTACT GetContact() + { + return _hContact; + } + + inline LRESULT ToastNotification::CallPopupProc(UINT uMsg) + { + return (_pfnPopupProc ? _pfnPopupProc((HWND)this, uMsg, 0, 0) : 0); + } + +}; -- cgit v1.2.3