Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Notification.cpp
Go to the documentation of this file.
1 
6 #include <windows.h>
7 #include <iostream>
8 #include <string>
9 #include <cstdlib>
10 
11 #include "../include/Notification.hpp"
12 
13 using namespace std;
14 
26 Notification::Notification(string title, string body) {
27  showNotification(title, body);
28 }
29 
40 void Notification::showNotification(const string& title, const string& body) {
41  // Get the path to the executable's directory
42  char executablePath[MAX_PATH];
43  GetModuleFileName(NULL, executablePath, MAX_PATH);
44 
45  // Extract the directory from the executable's path
46  string path(executablePath);
47  size_t lastSlash = path.find_last_of("\\/");
48  string directory = path.substr(0, lastSlash);
49 
50  // Construct the relative path to the PowerShell script and resolve it to an absolute path
51  char fullScriptPath[MAX_PATH];
52  if (_fullpath(fullScriptPath, (directory + "\\..\\script\\notification.ps1").c_str(), MAX_PATH)) {
53  string scriptPath(fullScriptPath);
54 
55  // Similarly, resolve the image path to an absolute path
56  char fullImagePath[MAX_PATH];
57  if (_fullpath(fullImagePath, (directory + "\\..\\logo.png").c_str(), MAX_PATH)) {
58  string imagePath(fullImagePath);
59 
60  // Prepare the PowerShell command with parameters
61  string powershellCommand = "powershell.exe -ExecutionPolicy Bypass -File \"" + scriptPath +
62  "\" -title \"" + title +
63  "\" -message \"" + body +
64  "\" -imagePath \"" + imagePath + "\"";
65 
66  // Set up the structures for creating the hidden process
67  STARTUPINFO si;
68  PROCESS_INFORMATION pi;
69 
70  ZeroMemory(&si, sizeof(si));
71  si.cb = sizeof(si);
72  ZeroMemory(&pi, sizeof(pi));
73 
74  // This hides the PowerShell window
75  si.dwFlags = STARTF_USESHOWWINDOW;
76  si.wShowWindow = SW_HIDE;
77 
78  // Create the process to run the PowerShell command
79  if (!CreateProcess(NULL,
80  const_cast<char*>(powershellCommand.c_str()), // Command line
81  NULL, // Process handle not inheritable
82  NULL, // Thread handle not inheritable
83  FALSE, // No handle inheritance
84  0, // No creation flags
85  NULL, // Use parent's environment block
86  NULL, // Use parent's starting directory
87  &si, // Pointer to STARTUPINFO (to hide window)
88  &pi) // Pointer to PROCESS_INFORMATION
89  ) {
90  std::cerr << "CreateProcess failed: " << GetLastError() << std::endl;
91  return;
92  }
93 
94  // Close process and thread handles immediately (since the process runs in background)
95  CloseHandle(pi.hProcess);
96  CloseHandle(pi.hThread);
97  } else {
98  cerr << "Failed to resolve absolute path for logo.png" << endl;
99  }
100  } else {
101  cerr << "Failed to resolve absolute path for notification.ps1" << endl;
102  }
103 
104  return;
105 }
STL namespace.
void showNotification(const string &title, const string &body)
Displays a notification with the given title and body.
Notification(string title, string body)
Constructs a Notification object and displays the notification.