-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoTempCleanUp.cpp
More file actions
293 lines (239 loc) · 9.25 KB
/
Copy pathAutoTempCleanUp.cpp
File metadata and controls
293 lines (239 loc) · 9.25 KB
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
#include <Windows.h>
#include <shellapi.h>
#include <Shellapi.h>
#include <filesystem>
#include <string>
#include <lmcons.h>
#include <iostream>
#include <fstream>
#include <tchar.h>
using namespace std;
#define ID_TRAYICON 1
#define ID_CLEANUP 3
#define ID_INFO 4
#define ID_EXIT 5
#define IDI_ICON 101
/**
* @brief Opens a browser window which opens the link to the GitHub repo
*/
void ShowInfo() {
LPCWSTR url = TEXT("https://github.com/tr3xxx/AutoTempCleanUp");
ShellExecute(NULL, L"open", url, NULL, NULL, SW_SHOWNORMAL);
}
/**
* @brief Cleans up the users temp and %temp% folder by using windows clean up tool
**/
void CleanUp() {
string temp_path = "C:\\Windows\\Temp\\*";
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
string user_temp_path = "C:\\Users\\<username>\\AppData\\Local\\Temp\\*";
// TODO: Change the loop the a replace function
if (GetUserNameA(username, &username_len))
{
for (int i = 0; i < user_temp_path.length(); i++)
{
if (user_temp_path[i] == '<')
{
user_temp_path.replace(i, 9, username);
}
}
}
// Run the cmd command
system(("cmd /c \"cleanmgr /sagerun:1 & del " + temp_path + " /q & del " + user_temp_path + " /q\"").c_str());
}
/**
* @brief Displays a context menu when the user left-clicks or right-clicks on the trayicon.
* @param hwnd the handle to the window that will be associated with the context menu
**/
void openPopUp(HWND hwnd) {
// Create a new popup menu
HMENU hMenu = CreatePopupMenu();
// Add the "CleanUp Now" menu item
wstring title = L"AutoTempCleanUp";
AppendMenu(hMenu, MF_GRAYED, ID_CLEANUP, title.c_str());
// Add a separator between the "Title" and "CleanUp" menu items
wstring separator1 = L"-------------";
AppendMenu(hMenu, MF_SEPARATOR, 0, separator1.c_str());
// Add the "CleanUp Now" menu item
wstring cleanup = L"CleanUp Now";
AppendMenu(hMenu, MF_STRING, ID_CLEANUP, cleanup.c_str());
// Add the "Info" menu item
wstring info = L"Info";
AppendMenu(hMenu, MF_STRING, ID_INFO, info.c_str());
// Add a separator between the "Info" and "Exit" menu items
wstring separator2 = L"-------------";
AppendMenu(hMenu, MF_SEPARATOR, 0, separator2.c_str());
// Add the "Exit" menu item
wstring exit = L"Exit";
AppendMenu(hMenu, MF_STRING, ID_EXIT, exit.c_str());
// Bring the window to the foreground to ensure the menu is displayed in front of other windows
SetForegroundWindow(hwnd);
// Get the current cursor position
POINT pt;
GetCursorPos(&pt);
// Create a NOTIFYICONIDENTIFIER structure for the tray icon
NOTIFYICONIDENTIFIER nid;
nid.cbSize = sizeof(NOTIFYICONIDENTIFIER);
nid.hWnd = hwnd;
nid.uID = ID_TRAYICON;
// Get the bounding rectangle of the taskbar button associated with the tray icon
RECT taskbarRect;
Shell_NotifyIconGetRect(&nid, &taskbarRect);
// Calculate the position of the menu above the tray icon
POINT menuPos = pt;
// Show the popup menu at the calculated position
TrackPopupMenu(hMenu, TPM_RIGHTALIGN | TPM_BOTTOMALIGN, menuPos.x, menuPos.y, 0, hwnd, NULL);
// Destroy the menu to free up resources
DestroyMenu(hMenu);
}
/**
*
* @brief Window procedure for the main window. Handles creation and destruction of the system tray icon, and events related to the icon.
* @param hwnd The handle to the window.
* @param uMsg The message identifier.
* @param wParam Additional message information.
* @param lParam Additional message information.
* @return The result of the message processing and depends on the message sent.
*/
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// Define a static NOTIFYICONDATA variable to hold information about the tray icon
static NOTIFYICONDATA nid;
switch (uMsg) {
case WM_CREATE: {
// Initialize the system tray icon
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = ID_TRAYICON;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_USER + 1;
// Load the icon from the resources and set it as the tray icon
HICON hIcon = static_cast<HICON>(LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE));
if (hIcon != NULL) {
nid.hIcon = hIcon;
}
else {
nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION));
}
// Set the tooltip text for the tray icon and add it to the system tray
wstring tip = L"AutoTempCleanUp";
lstrcpy(nid.szTip, tip.c_str());
Shell_NotifyIcon(NIM_ADD, &nid);
break;
}
case WM_USER + 1: {
// Handle system tray icon events
switch (LOWORD(lParam)) {
// Open the popup menu when the user left - clicks or right - clicks on the tray icon
case WM_LBUTTONDOWN: {
openPopUp(hwnd);
break;
}
case WM_RBUTTONDOWN: {
openPopUp(hwnd);
break;
}
}
}
case WM_COMMAND: {
// Handle menu commands
switch (LOWORD(wParam)) {
case ID_CLEANUP: {
// Perform the cleanup operation and show a notification
CleanUp();
NOTIFYICONDATA nid = {};
HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON));
if (hIcon != NULL) {
nid.hIcon = hIcon;
}
else {
nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION));
}
wstring msg = L"AutoTempCleaner cleaned up your temporary files successfully";
wstring title = L"AutoTempCleanUp";
lstrcpy(nid.szInfo, msg.c_str());
lstrcpy(nid.szInfoTitle, title.c_str());
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uFlags = NIF_INFO;
nid.dwInfoFlags = NIIF_INFO;
Shell_NotifyIcon(NIM_ADD, &nid);
break;
}
case ID_INFO: {
// Opens the GitHub repo
ShowInfo();
break;
}
case ID_EXIT: {
// Exit the program
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
}
}
break;
}
case WM_DESTROY: {
// Remove the system tray icon and exit the message loop
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
/**
* @brief puts the executable in the autostart after checking if its alreay in
**/
void Autostart() {
// Get the path to the executable
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
// Open the Run key
HKEY hKey;
RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);
// Check if the executable path is already in the Run key
TCHAR szValue[MAX_PATH];
DWORD dwSize = sizeof(szValue);
BOOL bExists = (RegQueryValueEx(hKey, TEXT("AutoTempCleanUp"), NULL, NULL, (LPBYTE)szValue, &dwSize) == ERROR_SUCCESS);
if (!bExists)
{
// Add the executable path to the Run key
RegSetValueEx(hKey, TEXT("AutoTempCleanUp"), 0, REG_SZ, (LPBYTE)szPath, (_tcslen(szPath) + 1) * sizeof(TCHAR));
}
// Close the key
RegCloseKey(hKey);
}
/**
*@brief Entry point of the program. This function initializes the program by calling the Autostart and
CleanUp functions, creates the main window, and enters the message loop to handle window messages.
*@param hInstance The handle to the current instance of the program.
*@param hPrevInstance The handle to the previous instance of the program.
*@param lpCmdLine The command-line arguments passed to the program.
*@param nCmdShow Controls how the window is to be shown.
*@return The exit code of the program.
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Create the main window
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc; // Assign the function that will handle the window messages
wc.hInstance = hInstance;
wc.lpszClassName = L"TempCleaner"; // Set the name of the window class
RegisterClass(&wc); // Register the window class with the system
// Create the main window with default settings
HWND hwnd = CreateWindowW(L"TempCleaner", L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
// Show the main window and enter the message loop
ShowWindow(hwnd, SW_HIDE); // Hide the window
// Call the CleanUp function to delete any temporary files
CleanUp();
// Call the Autostart function to check and set up the program's autostart feature
Autostart();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return the exit code of the program
return (int)msg.wParam;
}