Skip to content

Commit

Permalink
Add notification support for Windows version
Browse files Browse the repository at this point in the history
  • Loading branch information
shalithasuranga committed Nov 24, 2020
1 parent ca0d74d commit 960bf28
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core-windows/bin/app/assets/neutralino.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions core-windows/src/core/os/os.cpp
Expand Up @@ -32,7 +32,6 @@
#include <string>
#include <array>

#include "../../settings.h"
#include "../../functions.h"

using namespace std;
Expand All @@ -52,21 +51,8 @@ namespace os {
}

string command = "cmd /c " + input["command"].get<std::string>();

PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
char temp[256];
GetTempPathA(256, temp);
string tmpFile = string(temp) + "nl_o" + functions::generateToken(4) + ".tmp";
CreateProcessA(NULL,(LPSTR)(command + " > " + tmpFile).c_str(),NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,NULL,&si,&pi);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

output["stdout"] = settings::getFileContent(tmpFile);
DeleteFile(tmpFile.c_str());
output["stdout"] = functions::execCommand(command);
return output.dump();

}

string getEnvar(string jso) {
Expand Down Expand Up @@ -169,5 +155,32 @@ namespace os {

}

string showNotification(string jso) {
json input;
json output;
try {
input = json::parse(jso);
}
catch(exception e){
output["error"] = "JSON parse error is occurred!";
return output.dump();
}
string command = "powershell -Command \"& {[reflection.assembly]::loadwithpartialname('System.Windows.Forms');"
"[reflection.assembly]::loadwithpartialname('System.Drawing');"
"$notify = new-object system.windows.forms.notifyicon;"
"$notify.icon = [System.Drawing.SystemIcons]::Information;"
"$notify.visible = $true;"
"$notify.showballoontip(0 ,'"+ input["summary"].get<string>() + "','" + input["body"].get<string>() + "',[system.windows.forms.tooltipicon]::None)}\"";

string commandOutput = functions::execCommand(command);

if(commandOutput.find("'powershell'") != string::npos)
output["message"] = "Notification is pushed to the system";
else
output["error"] = "An error thrown while sending the notification";
return output.dump();

}


}
4 changes: 3 additions & 1 deletion core-windows/src/core/os/os.h
Expand Up @@ -31,14 +31,16 @@ namespace os {
string getEnvar(string jso);
string dialogOpen(string jso);
string dialogSave(string jso);
string showNotification(string jso);

typedef string (*pfunc)(string);

map <string, pfunc> funcmap = {
{"os.runCommand", os::runCommand },
{"os.getEnvar", os::getEnvar},
{"os.dialogOpen", os::dialogOpen},
{"os.dialogSave", os::dialogSave}
{"os.dialogSave", os::dialogSave},
{"os.showNotification", os::showNotification}
};
}

Expand Down
20 changes: 20 additions & 0 deletions core-windows/src/functions.cpp
Expand Up @@ -25,6 +25,9 @@
#include <vector>
#include <time.h>
#include <random>
#include <windows.h>

#include "settings.h"

using namespace std;
namespace functions {
Expand Down Expand Up @@ -55,4 +58,21 @@ namespace functions {
return s;
}

string execCommand(string command) {
string output = "";
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
char temp[256];
GetTempPathA(256, temp);
string tmpFile = string(temp) + "nl_o" + functions::generateToken(4) + ".tmp";
CreateProcessA(NULL,(LPSTR)(command + " > " + tmpFile).c_str(),NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,NULL,&si,&pi);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

output = settings::getFileContent(tmpFile);
DeleteFile(tmpFile.c_str());
return output;
}

}
1 change: 1 addition & 0 deletions core-windows/src/functions.h
Expand Up @@ -31,6 +31,7 @@ using namespace std;
namespace functions {
vector<string> split(const string &s, char delim);
string generateToken(int len = 32);
string execCommand(string command);
}

#endif // end of FUNCTIONS_H

0 comments on commit 960bf28

Please sign in to comment.