Skip to content

Commit

Permalink
Merge branch 'main-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
mohabouje committed May 11, 2017
2 parents 21a8b9f + 7f81d42 commit 092b4cc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 104 deletions.
31 changes: 30 additions & 1 deletion example/qt-gui-example/WinToastExample/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ void MainWindow::on_imagePathSelector_clicked()

}

class CustomHandler : public IWinToastHandler {
public:
void toastActivated() const {
std::wcout << L"The user clicked in this toast" << std::endl;
}

void toastFailed() const {
std::wcout << L"Error showing current toast" << std::endl;
}
void toastDismissed(WinToastDismissalReason state) const {
switch (state) {
case UserCanceled:
std::wcout << L"The user dismissed this toast" << std::endl;
break;
case ApplicationHidden:
std::wcout << L"The application hid the toast using ToastNotifier.hide()" << std::endl;
break;
case TimedOut:
std::wcout << L"The toast has timed out" << std::endl;
break;
default:
std::wcout << L"Toast not activated" << std::endl;
break;
}
}
};

void MainWindow::on_showToast_clicked()
{
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText04);
Expand All @@ -41,7 +68,9 @@ void MainWindow::on_showToast_clicked()
templ.setTextField(ui->secondLine->text().toStdWString(), WinToastTemplate::SecondLine);
templ.setTextField(ui->secondLine->text().toStdWString(), WinToastTemplate::ThirdLine);

if (!WinToast::instance()->showToast(templ, new WinToastHandler)) {


if (!WinToast::instance()->showToast(templ, new CustomHandler())) {
QMessageBox::warning(this, "Error", "Could not launch your toast notification!");
}
}
91 changes: 33 additions & 58 deletions src/wintoastlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#else
#define DEBUG_MSG(str) do { std::wcout << str << std::endl; } while( false )
#endif

using namespace WinToastLib;
namespace DllImporter {

Expand Down Expand Up @@ -64,6 +63,30 @@ namespace DllImporter {
}
}

class WinToastStringWrapper {
public:
WinToastStringWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw() {
HRESULT hr = DllImporter::WindowsCreateStringReference(stringRef, length, &_header, &_hstring);
if (!SUCCEEDED(hr)) {
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
}
}
WinToastStringWrapper(_In_ const std::wstring &stringRef) throw() {
HRESULT hr = DllImporter::WindowsCreateStringReference(stringRef.c_str(), static_cast<UINT32>(stringRef.length()), &_header, &_hstring);
if (FAILED(hr)) {
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
}
}
~WinToastStringWrapper() {
DllImporter::WindowsDeleteString(_hstring);
}
inline HSTRING Get() const throw() { return _hstring; }
private:
HSTRING _hstring;
HSTRING_HEADER _header;

};

namespace Util {
inline HRESULT defaultExecutablePath(_In_ WCHAR* path, _In_ DWORD nSize = MAX_PATH) {
DWORD written = GetModuleFileNameEx(GetCurrentProcess(), nullptr, path, nSize);
Expand Down Expand Up @@ -109,7 +132,7 @@ namespace Util {
return hr;
}

inline HRESULT setEventHandlers(_In_ IToastNotification* notification, _In_ std::shared_ptr<WinToastHandler> eventHandler) {
inline HRESULT setEventHandlers(_In_ IToastNotification* notification, _In_ std::shared_ptr<IWinToastHandler> eventHandler) {
EventRegistrationToken activatedToken, dismissedToken, failedToken;
HRESULT hr = notification->add_Activated(
Callback < Implements < RuntimeClassFlags<ClassicCom>,
Expand All @@ -128,7 +151,7 @@ namespace Util {
ToastDismissalReason reason;
if (SUCCEEDED(e->get_Reason(&reason)))
{
eventHandler->toastDismissed(static_cast<WinToastHandler::WinToastDismissalReason>(reason));
eventHandler->toastDismissed(static_cast<IWinToastHandler::WinToastDismissalReason>(reason));
}
return S_OK;
}).Get(), &dismissedToken);
Expand All @@ -146,6 +169,8 @@ namespace Util {
}
}



WinToast* WinToast::_instance = nullptr;
WinToast* WinToast::instance() {
if (_instance == nullptr) {
Expand All @@ -159,6 +184,9 @@ WinToast::WinToast() : _isInitialized(false)
DllImporter::initialize();
}

WinToast::~WinToast() {
}

void WinToast::setAppName(_In_ const std::wstring& appName) {
_appName = appName;
}
Expand Down Expand Up @@ -338,7 +366,7 @@ HRESULT WinToast::createShellLink() {



bool WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ WinToastHandler* handler) {
bool WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ IWinToastHandler* handler) {
if (!isInitialized()) {
DEBUG_MSG("Error when launching the toast. WinToast is not initialized =(");
return _isInitialized;
Expand All @@ -355,7 +383,7 @@ bool WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ WinToastHandl
if (SUCCEEDED(hr)) {
hr = _notificationFactory->CreateToastNotification(xmlDocument(), &_notification);
if (SUCCEEDED(hr)) {
hr = Util::setEventHandlers(notification(), std::shared_ptr<WinToastHandler>(handler));
hr = Util::setEventHandlers(notification(), std::shared_ptr<IWinToastHandler>(handler));
if (SUCCEEDED(hr)) {
hr = _notifier->Show(notification());
}
Expand Down Expand Up @@ -413,7 +441,6 @@ WinToastTemplate::WinToastTemplate(const WinToastTemplateType& type) :
initComponentsFromType();
}


WinToastTemplate::~WinToastTemplate()
{
_textFields.clear();
Expand All @@ -433,55 +460,3 @@ void WinToastTemplate::initComponentsFromType() {
_hasImage = _type < Text01;
_textFields = std::vector<std::wstring>(TextFieldsCount[_type], L"");
}


void WinToastHandler::toastActivated() const {
std::wcout << L"The user clicked in this toast" << std::endl;

}

void WinToastHandler::toastFailed() const {
std::wcout << L"Error showing current toast" << std::endl;
}

void WinToastHandler::toastDismissed(WinToastHandler::WinToastDismissalReason state) const {
switch (state) {
case UserCanceled:
std::wcout << L"The user dismissed this toast" << std::endl;
break;
case ApplicationHidden:
std::wcout << L"The application hid the toast using ToastNotifier.hide()" << std::endl;
break;
case TimedOut:
std::wcout << L"The toast has timed out" << std::endl;
break;
default:
std::wcout << L"Toast not activated" << std::endl;
break;
}
}


WinToastStringWrapper::WinToastStringWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw() {
HRESULT hr = DllImporter::WindowsCreateStringReference(stringRef, length, &_header, &_hstring);
if (!SUCCEEDED(hr)) {
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
}
}


WinToastStringWrapper::WinToastStringWrapper(const std::wstring &stringRef)
{
HRESULT hr = DllImporter::WindowsCreateStringReference(stringRef.c_str(), static_cast<UINT32>(stringRef.length()), &_header, &_hstring);
if (FAILED(hr)) {
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
}
}

WinToastStringWrapper::~WinToastStringWrapper() {
DllImporter::WindowsDeleteString(_hstring);
}

HSTRING WinToastStringWrapper::Get() const throw() {
return _hstring;
}
63 changes: 18 additions & 45 deletions src/wintoastlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,22 @@ using namespace Windows::Foundation;

#define DEFAULT_SHELL_LINKS_PATH L"\\Microsoft\\Windows\\Start Menu\\Programs\\"
#define DEFAULT_LINK_FORMAT L".lnk"

namespace WinToastLib {
class WinToastStringWrapper {
public:
WinToastStringWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw();
WinToastStringWrapper(_In_ const std::wstring &stringRef) throw();
~WinToastStringWrapper();
HSTRING Get() const throw();
private:
HSTRING _hstring;
HSTRING_HEADER _header;

};


class WinToastHandler {
class IWinToastHandler {
public:
enum WinToastDismissalReason {
UserCanceled = ToastDismissalReason::ToastDismissalReason_UserCanceled,
ApplicationHidden = ToastDismissalReason::ToastDismissalReason_ApplicationHidden,
TimedOut = ToastDismissalReason::ToastDismissalReason_TimedOut
};

virtual ~WinToastHandler() {}

virtual void toastActivated() const;
virtual void toastDismissed(WinToastDismissalReason state) const;
virtual void toastFailed() const;
virtual void toastActivated() const = 0;
virtual void toastDismissed(WinToastDismissalReason state) const = 0;
virtual void toastFailed() const = 0;
};

class WinToastTemplate
{
class WinToastTemplate {
public:
enum TextField {
FirstLine = 0,
SecondLine,
ThirdLine,
LineCount
};

enum TextField { FirstLine = 0, SecondLine, ThirdLine };
enum WinToastTemplateType {
ImageAndText01 = ToastTemplateType::ToastTemplateType_ToastImageAndText01,
ImageAndText02 = ToastTemplateType::ToastTemplateType_ToastImageAndText02,
Expand All @@ -79,7 +55,6 @@ namespace WinToastLib {

WinToastTemplate(_In_ const WinToastTemplateType& type = ImageAndText02);
~WinToastTemplate();

int textFieldsCount() const { return _textFields.size(); }
bool hasImage() const { return _hasImage; }
std::vector<std::wstring> textFields() const { return _textFields; }
Expand All @@ -99,20 +74,29 @@ namespace WinToastLib {

class WinToast {
public:
WinToast(void);
virtual ~WinToast();
static WinToast* instance();
static bool isCompatible();
static std::wstring configureAUMI(_In_ const std::wstring& company,
_In_ const std::wstring& name,
_In_ const std::wstring& surname,
_In_ const std::wstring& versionInfo
);
bool initialize();
bool isInitialized() const { return _isInitialized; }
bool showToast(_In_ const WinToastTemplate& toast, _In_ WinToastHandler* handler);
virtual bool initialize();
virtual bool isInitialized() const { return _isInitialized; }
virtual bool showToast(_In_ const WinToastTemplate& toast, _In_ IWinToastHandler* handler);
std::wstring appName() const;
std::wstring appUserModelId() const;
void setAppUserModelId(_In_ const std::wstring& appName);
void setAppName(_In_ const std::wstring& appName);

protected:
inline IXmlDocument* xmlDocument() const { return _xmlDocument.Get(); }
inline IToastNotifier* notifier() const { return _notifier.Get(); }
inline IToastNotificationFactory* notificationFactory() const { return _notificationFactory.Get(); }
inline IToastNotificationManagerStatics* notificationManager() const { return _notificationManager.Get(); }
inline IToastNotification* notification() const { return _notification.Get(); }
private:
bool _isInitialized;
std::wstring _appName;
Expand All @@ -124,21 +108,10 @@ namespace WinToastLib {
ComPtr<IToastNotification> _notification;
static WinToast* _instance;

WinToast(void);
IXmlDocument* xmlDocument() const { return _xmlDocument.Get(); }
IToastNotifier* notifier() const { return _notifier.Get(); }
IToastNotificationFactory* notificationFactory() const { return _notificationFactory.Get(); }
IToastNotificationManagerStatics* notificationManager() const { return _notificationManager.Get(); }
IToastNotification* notification() const { return _notification.Get(); }

HRESULT validateShellLink();
HRESULT createShellLink();
HRESULT setImageField(_In_ const std::wstring& path);
HRESULT setTextField(_In_ const std::wstring& text, _In_ int pos);
};



}

#endif // WINTOASTLIB_H

0 comments on commit 092b4cc

Please sign in to comment.