diff --git a/src/utils.cpp b/src/utils.cpp index bf9bbb9..edf3294 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,107 +1,125 @@ +/* + SnoreToast is capable to invoke Windows 8 toast notifications. + Copyright (C) 2019 Hannah von Reth + + SnoreToast is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreToast is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreToast. If not, see . +*/ + #include "utils.h" #include "snoretoasts.h" #include #include #include using namespace Microsoft::WRL; namespace { bool s_registered = false; } namespace Utils { HRESULT registerActivator() { if (!s_registered) { s_registered = true; Microsoft::WRL::Module::Create([] {}); Microsoft::WRL::Module::GetModule().IncrementObjectCount(); return Microsoft::WRL::Module::GetModule().RegisterObjects(); } return S_OK; } void unregisterActivator() { if (s_registered) { s_registered = false; Microsoft::WRL::Module::GetModule().UnregisterObjects(); Microsoft::WRL::Module::GetModule().DecrementObjectCount(); } } std::unordered_map splitData(const std::wstring &data) { std::unordered_map out; std::wstring tmp; std::wstringstream wss(data); while(std::getline(wss, tmp, L';')) { const auto pos = tmp.find(L"="); out[tmp.substr(0, pos)] = tmp.substr(pos + 1); } return out; } std::wstring selfLocate() { static std::wstring path; if (path.empty()) { size_t size; do { path.resize(path.size() + 1024); size = GetModuleFileNameW(nullptr, const_cast(path.data()), static_cast(path.size())); } while (GetLastError() == ERROR_INSUFFICIENT_BUFFER); path.resize(size); } return path; } bool writePipe(const std::wstring &pipe, const std::wstring &data) { HANDLE hPipe = CreateFile(pipe.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (hPipe != INVALID_HANDLE_VALUE) { DWORD written; const DWORD toWrite = static_cast(data.size() * sizeof(wchar_t)); WriteFile(hPipe, data.c_str(), toWrite, &written, nullptr); const bool success = written == toWrite; tLog << (success ? L"Wrote: " : L"Failed to write: ") << data << " to " << pipe; WriteFile(hPipe, nullptr, sizeof(wchar_t), &written, nullptr); CloseHandle(hPipe); return success; } return false; } std::wstring formatData(const std::vector > &data) { std::wstringstream out; for (const auto &p : data) { if (!p.second.empty()) { out << p.first << L"=" << p.second << L";"; } } return out.str(); } } ToastLog::ToastLog() { *this << Utils::selfLocate() << L" v" << SnoreToasts::version() << L": "; } ToastLog::~ToastLog() { OutputDebugStringW(m_log.str().c_str()); } diff --git a/src/utils.h b/src/utils.h index d0c4425..67c359d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,43 +1,61 @@ +/* + SnoreToast is capable to invoke Windows 8 toast notifications. + Copyright (C) 2019 Hannah von Reth + + SnoreToast is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreToast is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreToast. If not, see . +*/ + #pragma once #include #include #include namespace Utils { HRESULT registerActivator(); void unregisterActivator(); std::unordered_map splitData(const std::wstring &data); std::wstring selfLocate(); std::wstring formatData(const std::vector> &data); bool writePipe(const std::wstring &pipe, const std::wstring &data); }; class ToastLog { public: ToastLog(); ~ToastLog(); inline ToastLog &log() { return *this;} private: std::wstringstream m_log; template friend ToastLog & operator<<(ToastLog &, const T&); }; #define tLog ToastLog().log() << __FUNCSIG__ << ": " template ToastLog &operator<< (ToastLog &log, const T &t) { log.m_log << t; return log; }