-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.cpp
347 lines (317 loc) · 13.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
SnoreToast is capable to invoke Windows 8 toast notifications.
Copyright (C) 2013-2019 Hannah von Reth <vonreth@kde.org>
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 <http://www.gnu.org/licenses/>.
*/
#include "snoretoasts.h"
#include "config.h"
#include "toasteventhandler.h"
#include "snoretoastactioncenterintegration.h"
#include "linkhelper.h"
#include "utils.h"
#include <cmrc/cmrc.hpp>
#include <appmodel.h>
#include <shellapi.h>
#include <roapi.h>
#include <algorithm>
#include <functional>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
CMRC_DECLARE(SnoreToastResource);
std::wstring getAppId(const std::wstring &pid, const std::wstring &fallbackAppID)
{
if (pid.empty()) {
return fallbackAppID;
}
const int _pid = std::stoi(pid);
const HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, _pid);
if (!process) {
tLog << "Failed to retreive appid for " << _pid
<< " Failed to retrive process hanlde: " << Utils::formatWinError(GetLastError());
return fallbackAppID;
}
uint32_t size = 0;
long rc = GetApplicationUserModelId(process, &size, nullptr);
if (rc != ERROR_INSUFFICIENT_BUFFER) {
if (rc == APPMODEL_ERROR_NO_APPLICATION) {
tLog << "Failed to retreive appid for " << _pid << " Process is a desktop application";
} else {
tLog << "Failed to retreive appid for " << _pid
<< " Error: " << Utils::formatWinError(rc);
}
CloseHandle(process);
return fallbackAppID;
}
std::wstring out(size, 0);
rc = GetApplicationUserModelId(process, &size, out.data());
CloseHandle(process);
if (rc != ERROR_SUCCESS) {
tLog << "Failed to retreive appid for " << _pid << " Error: " << Utils::formatWinError(rc);
return fallbackAppID;
}
// strip 0
out.resize(out.size() - 1);
tLog << "AppId from pid" << out;
return out;
}
void help(const std::wstring &error)
{
if (!error.empty()) {
std::wcerr << error << std::endl;
} else {
std::wcerr << L"Welcome to SnoreToast " << SnoreToasts::version() << "." << std::endl
<< L"A command line application capable of creating Windows Toast notifications."
<< std::endl;
}
const auto filesystem = cmrc::SnoreToastResource::get_filesystem();
const auto help = filesystem.open("help.txt");
std::wcerr << help.begin() << std::endl;
}
void version()
{
std::wcerr << L"SnoreToast version " << SnoreToasts::version() << std::endl
<< L"ToastActivatorCLSID: " << SNORETOAST_CALLBACK_GUID << std::endl
<< L"Copyright (C) 2022 Hannah von Reth <vonreth@kde.org>" << std::endl
<< L"SnoreToast is free software: you can redistribute it and/or modify" << std::endl
<< L"it under the terms of the GNU Lesser General Public License as published by"
<< std::endl
<< L"the Free Software Foundation, either version 3 of the License, or" << std::endl
<< L"(at your option) any later version." << std::endl;
}
std::filesystem::path getIcon()
{
auto image = std::filesystem::temp_directory_path() / "snoretoast" / SnoreToasts::version()
/ "logo.png";
if (!std::filesystem::exists(image)) {
std::filesystem::create_directories(image.parent_path());
const auto filesystem = cmrc::SnoreToastResource::get_filesystem();
const auto img = filesystem.open("256-256-snoretoast.png");
std::ofstream out(image, std::ios::binary);
out.write(const_cast<char *>(img.begin()), img.size());
out.close();
}
return image;
}
SnoreToastActions::Actions parse(std::vector<wchar_t *> args)
{
HRESULT hr = S_OK;
std::wstring appID;
std::wstring pid;
std::filesystem::path pipe;
std::filesystem::path application;
std::wstring title;
std::wstring body;
std::filesystem::path image;
std::wstring id;
std::wstring sound(L"Notification.Default");
std::wstring buttons;
Duration duration = Duration::Short;
bool silent = false;
bool closeNotify = false;
bool isTextBoxEnabled = false;
auto nextArg = [&](std::vector<wchar_t *>::const_iterator &it,
const std::wstring &helpText) -> std::wstring {
if (it != args.cend()) {
return *it++;
} else {
help(helpText);
exit(static_cast<int>(SnoreToastActions::Actions::Error));
}
};
auto it = args.begin() + 1;
while (it != args.end()) {
std::wstring arg(nextArg(it, L""));
std::transform(arg.begin(), arg.end(), arg.begin(),
[](int i) -> int { return ::tolower(i); });
if (arg == L"-m") {
body = nextArg(it,
L"Missing argument to -m.\n"
L"Supply argument as -m \"message string\"");
} else if (arg == L"-t") {
title = nextArg(it,
L"Missing argument to -t.\n"
L"Supply argument as -t \"bold title string\"");
} else if (arg == L"-p") {
image = nextArg(it, L"Missing argument to -p. Supply argument as -p \"image path\"");
} else if (arg == L"-s") {
sound = nextArg(it,
L"Missing argument to -s.\n"
L"Supply argument as -s \"sound name\"");
} else if (arg == L"-d") {
std::wstring _d = nextArg(it,
L"Missing argument to -d.\n"
L"Supply argument as -d (short |long)");
if (_d == L"short") {
duration = Duration::Short;
} else if (_d == L"long") {
duration = Duration::Long;
} else {
help(_d + L" is not a valid");
return SnoreToastActions::Actions::Error;
}
} else if (arg == L"-id") {
id = nextArg(it,
L"Missing argument to -id.\n"
L"Supply argument as -id \"id\"");
} else if (arg == L"-silent") {
silent = true;
} else if (arg == L"-appid") {
appID = nextArg(it,
L"Missing argument to -appID.\n"
L"Supply argument as -appID \"Your.APP.ID\"");
} else if (arg == L"-pid") {
pid = nextArg(it,
L"Missing argument to -pid.\n"
L"Supply argument as -pid \"pid\"");
} else if (arg == L"-pipename") {
pipe = nextArg(it,
L"Missing argument to -pipeName.\n"
L"Supply argument as -pipeName \"\\.\\pipe\\foo\\\"");
} else if (arg == L"-application") {
application = nextArg(it,
L"Missing argument to -application.\n"
L"Supply argument as -application \"C:\\foo.exe\"");
} else if (arg == L"-b") {
buttons = nextArg(it,
L"Missing argument to -b.\n"
L"Supply argument for buttons as -b \"button1;button2\"");
} else if (arg == L"-tb") {
isTextBoxEnabled = true;
} else if (arg == L"-install") {
const std::wstring shortcut(
nextArg(it,
L"Missing argument to -install.\n"
L"Supply argument as -install \"path to your shortcut\" \"path to the "
L"application the shortcut should point to\" \"App.ID\""));
const std::wstring exe(
nextArg(it,
L"Missing argument to -install.\n"
L"Supply argument as -install \"path to your shortcut\" \"path to the "
L"application the shortcut should point to\" \"App.ID\""));
appID = nextArg(it,
L"Missing argument to -install.\n"
L"Supply argument as -install \"path to your shortcut\" \"path to the "
L"application the shortcut should point to\" \"App.ID\"");
return SUCCEEDED(LinkHelper::tryCreateShortcut(
shortcut, exe, appID, SnoreToastActionCenterIntegration::uuid()))
? SnoreToastActions::Actions::Clicked
: SnoreToastActions::Actions::Error;
} else if (arg == L"-close") {
id = nextArg(it,
L"Missing agument to -close"
L"Supply argument as -close \"id\"");
closeNotify = true;
} else if (arg == L"-v") {
version();
return SnoreToastActions::Actions::Clicked;
} else if (arg == L"-h") {
help(L"");
return SnoreToastActions::Actions::Clicked;
} else {
std::wstringstream ws;
ws << L"Unknown argument: " << arg << std::endl;
help(ws.str());
return SnoreToastActions::Actions::Error;
}
}
appID = getAppId(pid, appID);
if (appID.empty()) {
std::wstringstream _appID;
_appID << L"Snore.DesktopToasts." << SnoreToasts::version();
appID = _appID.str();
hr = LinkHelper::tryCreateShortcut(std::filesystem::path(L"SnoreToast")
/ SnoreToasts::version() / L"SnoreToast",
appID, SnoreToastActionCenterIntegration::uuid());
if (!SUCCEEDED(hr)) {
return SnoreToastActions::Actions::Error;
}
}
if (closeNotify) {
if (!id.empty()) {
SnoreToasts app(appID);
app.setId(id);
if (app.closeNotification()) {
return SnoreToastActions::Actions::Clicked;
}
} else {
help(L"Close only works if an -id id was provided.");
}
} else {
hr = (title.length() > 0 && body.length() > 0) ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) {
if (isTextBoxEnabled) {
if (pipe.empty()) {
std::wcerr << L"TextBox notifications only work if a pipe for the result "
L"was provided"
<< std::endl;
return SnoreToastActions::Actions::Error;
}
}
if (image.empty()) {
image = getIcon();
}
SnoreToasts app(appID);
app.setPipeName(pipe);
app.setApplication(application);
app.setSilent(silent);
app.setSound(sound);
app.setId(id);
app.setButtons(buttons);
app.setTextBoxEnabled(isTextBoxEnabled);
app.setDuration(duration);
app.displayToast(title, body, image);
return app.userAction();
} else {
help(L"");
return SnoreToastActions::Actions::Clicked;
}
}
return SnoreToastActions::Actions::Error;
}
SnoreToastActions::Actions handleEmbedded()
{
SnoreToasts::waitForCallbackActivation();
return SnoreToastActions::Actions::Clicked;
}
int WINAPI wWinMain(HINSTANCE, HINSTANCE, wchar_t *, int)
{
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// atache to the parent console output, if its an interactive terminal
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
FILE *dummy;
_wfreopen_s(&dummy, L"CONOUT$", L"w", stdout);
setvbuf(stdout, nullptr, _IONBF, 0);
_wfreopen_s(&dummy, L"CONOUT$", L"w", stderr);
setvbuf(stderr, nullptr, _IONBF, 0);
std::ios::sync_with_stdio();
}
}
const auto commandLine = GetCommandLineW();
int argc;
wchar_t **argv = CommandLineToArgvW(commandLine, &argc);
tLog << commandLine;
SnoreToastActions::Actions action = SnoreToastActions::Actions::Clicked;
HRESULT hr = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
if (SUCCEEDED(hr)) {
if (std::wstring(commandLine).find(L"-Embedding") != std::wstring::npos) {
action = handleEmbedded();
} else {
action = parse(std::vector<wchar_t *>(argv, argv + argc));
}
Windows::Foundation::Uninitialize();
}
return static_cast<int>(action);
}