Skip to content
API Documentation
BastionGuard™
ScanPage.hpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
21#pragma once
22
23#include <gtkmm.h>
24#include <giomm.h>
25#include <glibmm.h>
26#include <glib/gi18n.h>
27
28#include <string>
29#include <vector>
30#include <unordered_map>
31#include <unordered_set>
32#include <filesystem>
33#include <mutex>
34
35#include "LifetimeGuard.hpp"
36
37class ScanPage : public Gtk::Box {
38public:
39 explicit ScanPage(Gtk::Window& parent);
40 ~ScanPage() override;
41
42private:
43 Gtk::Window& parentWindow;
44
45
46 Gtk::Box buttonBox{Gtk::Orientation::HORIZONTAL, 5};
47 Gtk::Button btnChooseFolder{_("Seleziona cartella")};
48 Gtk::Button btnChooseFile{_("Seleziona file")};
49 Gtk::Button btnScan{_("Scansiona")};
50
51 Gtk::Box autoScanBox{Gtk::Orientation::HORIZONTAL, 6};
52 Gtk::Label autoScanLabel;
53 Gtk::Switch autoScanSwitch;
54 bool autoScanEnabled = true;
55
56 Gtk::TextView textView;
57 Glib::RefPtr<Gtk::TextBuffer> textBuffer;
58
59 Gtk::ListBox fileListBox;
60 Gtk::Box apiKeyActionsBox{Gtk::Orientation::HORIZONTAL, 6};
61 Glib::RefPtr<Gtk::SizeGroup> apiKeySizeGroup;
62
63 Glib::RefPtr<Gio::Subprocess> subprocess;
64 Glib::RefPtr<Gio::InputStream> stdoutStream;
65 Glib::RefPtr<Gio::DataInputStream> dataStream;
66
67
68 Gtk::Box apiKeyBox{Gtk::Orientation::HORIZONTAL, 6};
69 Gtk::Entry apiKeyEntry;
70 Gtk::Button btnShowApiKey;
71 Gtk::Button btnApiKeySave{"Salva"};
72 Gtk::Button btnPrivacy;
73
74 bool apiKeyVisible = false;
76
77
78
79 std::mutex logMutex;
80 std::vector<std::string> pendingLog;
81 bool logFlushScheduled = false;
82
83
84 void enqueueLog(const std::string& msg);
85 void flushLog();
86
87 void saveCloudConfig();
88 void loadCloudConfig();
91
93
94
95 std::string selectedPath;
96 std::vector<std::string> infectedFilesBuffer;
97 bool scanInProgress = false;
98
99 void onChooseFolder();
100 void onChooseFile();
101 void onStartScan();
102
103 void readOutput(const Glib::RefPtr<Gio::AsyncResult>& result);
104 void addFileToList(const std::string& filepath);
105
106 void saveConfig();
107 void loadConfig();
108
109
110 std::vector<std::string> knownDownloadDirs;
111 std::vector<std::string> detectBrowserDownloadDirs();
112 std::vector<std::string> getXdgUserDirs();
113 bool isOutsideKnownDownloadDirs(const std::string& path);
114
115
116 int inotifyFd = -1;
117 Glib::RefPtr<Glib::IOSource> inotifySource;
118
119
120 std::unordered_map<int, std::string> watchMap;
121
122
123 std::unordered_set<std::string> alreadyWatched;
124
125
126 void addInotifyRecursive(const std::string& path);
127
128
129 bool onInotifyEvent(Glib::IOCondition cond);
130
131 void handleInotifyFileEvent(const std::string& path, uint32_t mask);
132
133
136 void disableAllMonitors();
137 void onChooseFolderFinish(const Glib::RefPtr<Gio::AsyncResult>& result,
138 Glib::RefPtr<Gtk::FileDialog> dialog);
139
140 void onChooseFileFinish(const Glib::RefPtr<Gio::AsyncResult>& result,
141 Glib::RefPtr<Gtk::FileDialog> dialog);
142
143
144 void scanFileAutomatically(const std::string& filepath);
145 bool isInterestingDownload(const std::string& path);
146
147 std::string resolveFinalDownloadedFile(const std::string& path);
148
149
150 void onAurPkgbuildDetected(const std::string& path);
151 void scanAurAuto(const std::string& path);
152
153
154 void cloudCheckMalwareBazaar(const std::string& filepath);
155 void cloudCheckMalwareBazaarWorker(const std::string& filepath);
156
157 void handleInfectedFile(const std::string& filepath);
158 void handleInfectedFiles(const std::vector<std::string>& files);
159 void handleInfectedFileWithName(const std::string& filepath, const std::string& virusName);
160
161
162 void showAlertWindow(const std::string& file, const std::string& family);
163 void logMessage(const std::string& msg);
164 bool safeExists(const std::string& p) noexcept;
165
167};
#define _(s)
Glib::RefPtr< Gio::InputStream > stdoutStream
Definition ScanPage.hpp:64
BastionGuard::AliveFlag alive_
Definition ScanPage.hpp:166
bool onInotifyEvent(Glib::IOCondition cond)
Definition ScanPage.cpp:399
Glib::RefPtr< Glib::IOSource > inotifySource
Definition ScanPage.hpp:117
void scanAurAuto(const std::string &path)
void enableAutoScanDownloads()
Definition ScanPage.cpp:979
Gtk::Entry apiKeyEntry
Definition ScanPage.hpp:69
void logMessage(const std::string &msg)
std::vector< std::string > infectedFilesBuffer
Definition ScanPage.hpp:96
Gtk::TextView textView
Definition ScanPage.hpp:56
std::string selectedPath
Definition ScanPage.hpp:95
std::vector< std::string > detectBrowserDownloadDirs()
Definition ScanPage.cpp:895
void showAlertWindow(const std::string &file, const std::string &family)
Gtk::ListBox fileListBox
Definition ScanPage.hpp:59
bool autoScanEnabled
Definition ScanPage.hpp:54
std::vector< std::string > pendingLog
Definition ScanPage.hpp:80
void onStartScan()
Definition ScanPage.cpp:651
std::mutex logMutex
Definition ScanPage.hpp:79
Gtk::Button btnScan
Definition ScanPage.hpp:49
void handleInfectedFile(const std::string &filepath)
Glib::RefPtr< Gtk::SizeGroup > apiKeySizeGroup
Definition ScanPage.hpp:61
bool safeExists(const std::string &p) noexcept
bool isInterestingDownload(const std::string &path)
void showCloudPrivacyDisclaimer()
Gtk::Switch autoScanSwitch
Definition ScanPage.hpp:53
void onChooseFolderFinish(const Glib::RefPtr< Gio::AsyncResult > &result, Glib::RefPtr< Gtk::FileDialog > dialog)
Definition ScanPage.cpp:446
void cloudCheckMalwareBazaarWorker(const std::string &filepath)
void onToggleApiKeyVisibility()
void addInotifyRecursive(const std::string &path)
Definition ScanPage.cpp:335
std::unordered_map< int, std::string > watchMap
Definition ScanPage.hpp:120
void saveCloudConfig()
Definition ScanPage.cpp:532
std::vector< std::string > getXdgUserDirs()
Definition ScanPage.cpp:992
void handleInfectedFileWithName(const std::string &filepath, const std::string &virusName)
bool logFlushScheduled
Definition ScanPage.hpp:81
void readOutput(const Glib::RefPtr< Gio::AsyncResult > &result)
Definition ScanPage.cpp:704
Gtk::Box buttonBox
Definition ScanPage.hpp:46
Gtk::Label autoScanLabel
Definition ScanPage.hpp:52
Glib::RefPtr< Gtk::TextBuffer > textBuffer
Definition ScanPage.hpp:57
void handleInfectedFiles(const std::vector< std::string > &files)
Gtk::Button btnChooseFolder
Definition ScanPage.hpp:47
bool scanInProgress
Definition ScanPage.hpp:97
void enqueueLog(const std::string &msg)
Definition ScanPage.cpp:286
void flushLog()
Definition ScanPage.cpp:297
Gtk::Button btnShowApiKey
Definition ScanPage.hpp:70
bool apiKeyVisible
Definition ScanPage.hpp:74
void saveConfig()
Definition ScanPage.cpp:314
std::string resolveFinalDownloadedFile(const std::string &path)
void enableGlobalDownloadMonitor()
std::unordered_set< std::string > alreadyWatched
Definition ScanPage.hpp:123
std::string malwareBazaarApiKey
Definition ScanPage.hpp:75
void onAurPkgbuildDetected(const std::string &path)
void onChooseFolder()
Definition ScanPage.cpp:431
bool isOutsideKnownDownloadDirs(const std::string &path)
Definition ScanPage.cpp:970
void onApiKeySaveClicked()
Definition ScanPage.cpp:551
void cloudCheckMalwareBazaar(const std::string &filepath)
Gtk::Box autoScanBox
Definition ScanPage.hpp:51
void scanFileAutomatically(const std::string &filepath)
Definition ScanPage.cpp:760
void addFileToList(const std::string &filepath)
Definition ScanPage.cpp:878
Gtk::Button btnApiKeySave
Definition ScanPage.hpp:71
Gtk::Box apiKeyBox
Definition ScanPage.hpp:68
Glib::RefPtr< Gio::Subprocess > subprocess
Definition ScanPage.hpp:63
Gtk::Window & parentWindow
Definition ScanPage.hpp:43
int inotifyFd
Definition ScanPage.hpp:116
Gtk::Button btnChooseFile
Definition ScanPage.hpp:48
void disableAllMonitors()
Definition ScanPage.cpp:638
Gtk::Button btnPrivacy
Definition ScanPage.hpp:72
void onChooseFile()
Definition ScanPage.cpp:463
std::vector< std::string > knownDownloadDirs
Definition ScanPage.hpp:110
void loadConfig()
Definition ScanPage.cpp:324
Gtk::Box apiKeyActionsBox
Definition ScanPage.hpp:60
void handleInotifyFileEvent(const std::string &path, uint32_t mask)
Definition ScanPage.cpp:558
void onChooseFileFinish(const Glib::RefPtr< Gio::AsyncResult > &result, Glib::RefPtr< Gtk::FileDialog > dialog)
Definition ScanPage.cpp:478
Glib::RefPtr< Gio::DataInputStream > dataStream
Definition ScanPage.hpp:65
void loadCloudConfig()
Definition ScanPage.cpp:494
ScanPage(Gtk::Window &parent)
Definition ScanPage.cpp:131
std::shared_ptr< std::atomic< bool > > AliveFlag
AliveFlag make_alive_flag()