Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Public Member Functions | Static Public Member Functions
Notification Class Reference

A class to handle system notifications using PowerShell scripts. More...

#include <Notification.hpp>

Public Member Functions

 Notification (string title, string body)
 Constructs a Notification object and displays the notification. More...
 
void showNotification (const string &title, const string &body)
 Displays a notification with the given title and body. More...
 

Static Public Member Functions

static void setupNotifications ()
 Setups up the environment or configurations needed for notifications. More...
 

Detailed Description

A class to handle system notifications using PowerShell scripts.

A class to manage system notifications using PowerShell scripts.

Definition at line 12 of file Notification.hpp.

Constructor & Destructor Documentation

◆ Notification()

Notification::Notification ( string  title,
string  body 
)

Constructs a Notification object and displays the notification.

Constructor that creates a Notification object and shows the notification.

Parameters
titleThe title of the notification.
bodyThe body message of the notification.

Definition at line 26 of file Notification.cpp.

26  {
27  showNotification(title, body);
28 }
void showNotification(const string &title, const string &body)
Displays a notification with the given title and body.

Member Function Documentation

◆ setupNotifications()

static void Notification::setupNotifications ( )
static

Setups up the environment or configurations needed for notifications.

This static method can be used to initialize any resources required for notifications before creating individual notification instances.

◆ showNotification()

void Notification::showNotification ( const string &  title,
const string &  body 
)

Displays a notification with the given title and body.

Displays a notification using PowerShell with the given title and body.

Parameters
titleThe title of the notification.
bodyThe body message of the notification.

This method constructs the path to the PowerShell script and an image file, then creates a hidden process to execute the PowerShell command that shows the notification.

Parameters
titleThe title of the notification.
bodyThe body message of the notification.

Definition at line 40 of file Notification.cpp.

40  {
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 }

The documentation for this class was generated from the following files: