-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
287 lines (242 loc) · 10.3 KB
/
main.cpp
File metadata and controls
287 lines (242 loc) · 10.3 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
#include <gtk/gtk.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <unordered_map>
#include <functional>
#include <openssl/evp.h> // Für die neue API
#include <iomanip>
namespace fs = std::filesystem;
std::vector<std::string> menu_items = {
"System bereinigen (APT + Logs)",
"Temporäre Dateien löschen",
"Papierkorb leeren",
"Browser-Cache löschen",
"Nicht verwendete Flatpak/Snap entfernen",
"Doppelte Dateien finden & löschen"
};
// Log-Datei für die Konsolenausgabe
std::ofstream log_file("cleanup_log.txt", std::ios::out | std::ios::app);
// Funktion zum Schreiben in die Log-Datei
void write_log(const std::string& message, GtkTextBuffer* text_buffer) {
if (log_file.is_open()) {
log_file << message << std::endl;
}
// TextBuffer im GTK-TextView aktualisieren
GtkTextIter end_iter;
gtk_text_buffer_get_end_iter(text_buffer, &end_iter);
gtk_text_buffer_insert(text_buffer, &end_iter, message.c_str(), -1);
gtk_text_buffer_insert(text_buffer, &end_iter, "\n", -1);
}
void run_command(const std::string& command, GtkTextBuffer* text_buffer) {
write_log("Running command: " + command, text_buffer);
int ret = system(command.c_str());
if (ret != 0) {
write_log("Command failed: " + command, text_buffer);
} else {
write_log("Command succeeded: " + command, text_buffer);
}
}
void clean_apt(GtkTextBuffer* text_buffer) {
write_log("Starting APT cleaning...", text_buffer);
run_command("pkexec sudo apt update", text_buffer);
run_command("pkexec apt autoremove -y", text_buffer);
run_command("pkexec apt autoclean", text_buffer);
run_command("pkexec apt clean", text_buffer);
write_log("APT cleaning completed.", text_buffer);
}
void clean_journal(GtkTextBuffer* text_buffer) {
write_log("Starting journal cleaning...", text_buffer);
run_command("pkexec journalctl --vacuum-time=7d", text_buffer);
write_log("Journal cleaning completed.", text_buffer);
}
void empty_trash(GtkTextBuffer* text_buffer) {
write_log("Starting trash cleaning...", text_buffer);
std::vector<std::string> trash_dirs = {
std::string(getenv("HOME")) + "/.local/share/Trash/files",
std::string(getenv("HOME")) + "/.Trash"
};
for (const auto& dir : trash_dirs) {
std::string command = "rm -rf " + dir;
run_command(command, text_buffer);
}
write_log("Trash cleaning completed.", text_buffer);
}
void clean_tmp(GtkTextBuffer* text_buffer) {
write_log("Starting temporary files cleaning...", text_buffer);
std::vector<std::string> tmp_dirs = { "/tmp", std::string(getenv("HOME")) + "/.cache" };
for (const auto& dir : tmp_dirs) {
run_command("rm -rf " + dir + "/*", text_buffer);
}
write_log("Temporary files cleaning completed.", text_buffer);
}
void clean_flatpak(GtkTextBuffer* text_buffer) {
write_log("Starting Flatpak cleaning...", text_buffer);
run_command("flatpak uninstall --unused -y", text_buffer);
write_log("Flatpak cleaning completed.", text_buffer);
}
void clean_snap(GtkTextBuffer* text_buffer) {
write_log("Starting Snap cleaning...", text_buffer);
run_command("snap list --all | awk '/disabled/{print $1, $2}' | "
"while read snapname revision; do sudo snap remove \"$snapname\" --revision=\"$revision\"; done", text_buffer);
write_log("Snap cleaning completed.", text_buffer);
}
void clean_browser_cache(GtkTextBuffer* text_buffer) {
write_log("Starting browser cache cleaning...", text_buffer);
std::vector<std::string> browsers = {
std::string(getenv("HOME")) + "/.mozilla/firefox",
std::string(getenv("HOME")) + "/.cache/chromium",
std::string(getenv("HOME")) + "/.cache/google-chrome",
std::string(getenv("HOME")) + "/.cache/BraveSoftware"
};
for (const auto& path : browsers) {
if (fs::exists(path)) {
std::string command = "rm -rf " + path;
run_command(command, text_buffer);
}
}
write_log("Browser cache cleaning completed.", text_buffer);
}
std::string file_hash(const std::string& path, const std::string& algo = "sha256") {
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file");
}
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
const EVP_MD* md = EVP_get_digestbyname(algo.c_str());
if (!md) {
EVP_MD_CTX_free(ctx);
throw std::invalid_argument("Unknown hash algorithm");
}
EVP_DigestInit_ex(ctx, md, nullptr);
char buffer[8192];
while (file.read(buffer, sizeof(buffer))) {
EVP_DigestUpdate(ctx, buffer, file.gcount());
}
EVP_DigestUpdate(ctx, buffer, file.gcount());
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int length;
EVP_DigestFinal_ex(ctx, hash, &length);
EVP_MD_CTX_free(ctx);
// Hex-String zurückgeben
std::stringstream hex_stream;
for (unsigned int i = 0; i < length; ++i) {
hex_stream << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
return hex_stream.str();
}
void find_and_remove_duplicates(const std::string& search_path = std::string(getenv("HOME")), GtkTextBuffer* text_buffer = nullptr) {
write_log("Starting duplicate file removal...", text_buffer);
std::unordered_map<size_t, std::vector<std::string>> size_map;
for (const auto& entry : fs::recursive_directory_iterator(search_path)) {
if (fs::is_regular_file(entry.path())) {
try {
size_map[fs::file_size(entry.path())].push_back(entry.path().string());
} catch (...) {}
}
}
std::vector<std::string> duplicates;
for (const auto& [size, files] : size_map) {
if (files.size() < 2) continue;
std::unordered_map<std::string, std::string> hash_dict;
for (const auto& file : files) {
try {
std::string hash = file_hash(file);
if (hash_dict.find(hash) != hash_dict.end()) {
duplicates.push_back(file);
} else {
hash_dict[hash] = file;
}
} catch (...) {}
}
}
for (const auto& dup : duplicates) {
try {
fs::remove(dup);
} catch (...) {}
}
write_log("Duplicate file removal completed.", text_buffer);
}
// GTK GUI
void start_cleanup(GtkWidget* widget, gpointer data) {
std::vector<std::function<void(GtkTextBuffer*)>> selected_functions = *static_cast<std::vector<std::function<void(GtkTextBuffer*)>>*>(data);
// Funktionen im Hauptthread ausführen
for (const auto& func : selected_functions) {
func(nullptr); // Ersetzen Sie nullptr mit dem TextBuffer für Logausgabe
}
GtkWidget* message = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Bereinigung abgeschlossen!");
gtk_dialog_run(GTK_DIALOG(message));
gtk_widget_destroy(message);
}
void on_menu_item_toggled(GtkToggleButton* button, gpointer data) {
int idx = GPOINTER_TO_INT(data);
std::vector<std::function<void(GtkTextBuffer*)>>* selected_functions = static_cast<std::vector<std::function<void(GtkTextBuffer*)>>*>(g_object_get_data(G_OBJECT(button), "selected_functions"));
if (gtk_toggle_button_get_active(button)) {
switch (idx) {
case 0:
selected_functions->push_back(clean_apt);
selected_functions->push_back(clean_journal);
break;
case 1:
selected_functions->push_back(clean_tmp);
break;
case 2:
selected_functions->push_back(empty_trash);
break;
case 3:
selected_functions->push_back(clean_browser_cache);
break;
case 4:
selected_functions->push_back(clean_flatpak);
selected_functions->push_back(clean_snap);
break;
case 5:
selected_functions->push_back([&](GtkTextBuffer* text_buffer) {
find_and_remove_duplicates("/home", text_buffer);
});
break;
}
}
}
// Neues Fenster zum Anzeigen des Logs
GtkWidget* create_log_window(GtkTextBuffer* text_buffer) {
GtkWidget* log_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(log_window), "Konsolen Log");
gtk_window_set_default_size(GTK_WINDOW(log_window), 500, 300);
GtkWidget* scroll_window = gtk_scrolled_window_new(nullptr, nullptr);
gtk_container_add(GTK_CONTAINER(log_window), scroll_window);
GtkTextView* text_view = GTK_TEXT_VIEW(gtk_text_view_new());
gtk_container_add(GTK_CONTAINER(scroll_window), GTK_WIDGET(text_view));
// Setzen des Buffers
gtk_text_view_set_buffer(text_view, text_buffer);
gtk_widget_show_all(log_window);
return log_window;
}
int main(int argc, char* argv[]) {
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Systembereinigung");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 400);
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER(window), box);
GtkTextBuffer* text_buffer = gtk_text_buffer_new(nullptr);
GtkWidget* log_button = gtk_button_new_with_label("Log anzeigen");
gtk_box_pack_start(GTK_BOX(box), log_button, FALSE, FALSE, 0);
g_signal_connect(log_button, "clicked", G_CALLBACK(create_log_window), text_buffer);
std::vector<std::function<void(GtkTextBuffer*)>> selected_functions;
for (size_t i = 0; i < menu_items.size(); ++i) {
GtkWidget* toggle_button = gtk_check_button_new_with_label(menu_items[i].c_str());
g_signal_connect(toggle_button, "toggled", G_CALLBACK(on_menu_item_toggled), GINT_TO_POINTER(i));
g_object_set_data(G_OBJECT(toggle_button), "selected_functions", &selected_functions);
gtk_box_pack_start(GTK_BOX(box), toggle_button, FALSE, FALSE, 0);
}
GtkWidget* start_button = gtk_button_new_with_label("Starten");
g_signal_connect(start_button, "clicked", G_CALLBACK(start_cleanup), &selected_functions);
gtk_box_pack_start(GTK_BOX(box), start_button, FALSE, FALSE, 0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}