-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchyprlandwelcome.cpp
277 lines (236 loc) · 11.8 KB
/
chyprlandwelcome.cpp
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
#include "chyprlandwelcome.h"
#include "./ui_chyprlandwelcome.h"
#include <QDesktopServices>
#include <QUrl>
#include <QApplication>
#include <QTabBar>
#include <QTimer>
#include <QProcess>
#include <filesystem>
#include <thread>
QWidget* findByName(QString name) {
QList<QWidget*> widgets = QApplication::allWidgets();
QList<QWidget*>::iterator it = std::find_if(widgets.begin(), widgets.end(), [name](QWidget* widget) -> bool { return widget->objectName() == name; });
return it == widgets.end() ? nullptr : *it;
}
std::string execAndGet(std::string cmd) {
cmd += " 2>&1";
std::array<char, 128> buffer;
std::string result;
const std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe)
return "";
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
bool appExists(std::string binName) {
std::string ret = execAndGet("which " + binName);
return ret.find("not found") == std::string::npos && ret.find("no " + binName + " in") == std::string::npos && ret.find("/") != std::string::npos;
}
bool binExists(std::string path) {
std::string ret = execAndGet("[ -f " + path + " ] || echo \"no\"");
return ret.find("no") == std::string::npos;
}
bool appIsRunning(std::string binName) {
std::string ret = execAndGet("pidof " + binName);
return !ret.empty();
}
struct SAppCheck {
std::vector<std::string> binaryNames;
std::vector<std::string> binaryPaths;
std::string name;
QLabel* label = nullptr;
bool needsAllInstalled = false;
bool needsAllRunning = false;
std::vector<std::string> runningNames;
std::string currentText = "";
};
std::vector<SAppCheck> appChecks;
//
void CHyprlandWelcome::startAppTimer() {
appChecks.push_back(SAppCheck{{"waybar", "ags", "eww"}, {}, "Status Bar", (QLabel*)findByName("INSTALL_BAR"), false, false, {"waybar", "gjs", "eww-daemon", "eww"}});
appChecks.push_back(SAppCheck{{"dolphin", "thunar", "ranger"}, {}, "File Manager", (QLabel*)findByName("INSTALL_FM")});
appChecks.push_back(SAppCheck{{"mako", "dunst"}, {}, "Notification Daemon", (QLabel*)findByName("INSTALL_NOTIF")});
appChecks.push_back(SAppCheck{{"anyrun", "fuzzel", "wofi", "tofi"}, {}, "App Launcher", (QLabel*)findByName("INSTALL_LAUNCHER")});
appChecks.push_back(SAppCheck{{"hyprpaper", "swaybg", "swww"}, {}, "Wallpaper Utility", (QLabel*)findByName("INSTALL_WALLPAPER")});
appChecks.push_back(SAppCheck{{"pipewire", "wireplumber"}, {}, "Pipewire*", (QLabel*)findByName("INSTALL_PW"), true, true});
appChecks.push_back(SAppCheck{{"xdg-desktop-portal", "xdg-desktop-portal-hyprland", "xdg-desktop-portal-gtk"},
{"/usr/lib/xdg-desktop-portal", "/usr/lib/xdg-desktop-portal-hyprland", "/usr/lib/xdg-desktop-portal-gtk", "/usr/libexec/xdg-desktop-portal",
"/usr/libexec/xdg-desktop-portal-hyprland", "/usr/libexec/xdg-desktop-portal-gtk"},
"XDG Desktop Portal*",
(QLabel*)findByName("INSTALL_XDP"),
false,
true,
{"xdg-desktop-portal", "xdg-desktop-portal-hyprland"}});
appChecks.push_back(SAppCheck{
{}, {"/usr/lib/polkit-kde-authentication-agent-1"}, "Authentication Agent", (QLabel*)findByName("INSTALL_AUTH"), false, false, {"polkit-kde-authentication-agent-1"}});
appChecks.push_back(SAppCheck{{"qtwaylandscanner"}, {}, "QT Wayland Support", (QLabel*)findByName("INSTALL_QTW")});
appChecks.push_back(SAppCheck{{"kitty", "wezterm", "alacritty", "foot", "konsole", "gnome-terminal"}, {}, "Terminal", (QLabel*)findByName("INSTALL_TERM")});
appChecks.push_back(SAppCheck{{"qt5ct", "qt6ct"}, {}, "QT Theming", (QLabel*)findByName("INSTALL_QTTHEME")});
QTimer* timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, [this]() {
if (this->currentTab != 1)
return;
appScanMutex.lock();
for (const auto& app : appChecks) {
if (app.currentText.empty())
continue;
app.label->setText(QString::fromStdString(app.currentText));
}
appScanMutex.unlock();
});
timer->start(1000);
std::thread t([this]() {
while (true) {
if (exit)
return;
if (currentTab != 1) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
continue;
}
for (auto& app : appChecks) {
std::vector<std::string> found;
std::vector<std::string> runningApps;
bool running = false;
for (const auto& bin : app.binaryNames) {
if (appExists(bin))
found.push_back(bin);
if (app.runningNames.empty() && appIsRunning(bin)) {
runningApps.push_back(bin);
running = true;
}
}
if (!app.runningNames.empty())
for (const auto& bin : app.runningNames) {
if (appIsRunning(bin)) {
runningApps.push_back(bin);
running = true;
}
}
for (const auto& bin : app.binaryPaths) {
if (binExists(bin))
found.push_back(bin);
}
if ((found.empty() && !running) || (app.needsAllInstalled && found.size() != app.binaryNames.size() + app.binaryPaths.size())) {
appScanMutex.lock();
app.currentText = "<html><head/><body><p><span style=\" color:#ff0000;\">❌</span> " + app.name + "</p></body></html>";
appScanMutex.unlock();
} else if (app.needsAllRunning && runningApps.size() != (app.runningNames.empty() ? app.binaryNames : app.runningNames).size()) {
appScanMutex.lock();
app.currentText = "<html><head/><body><p><span style=\" color:#ff0000;\">❌</span> " + app.name + ": Found all, but not running.</p></body></html>";
appScanMutex.unlock();
} else if (!running) {
std::string text = "<html><head/><body><p><span style=\" color:#00ff00;\">✔️</span> " + app.name + ": <span style=\" color:#00ff00;\">found</span> ";
for (auto& f : found)
text += f + ", ";
text.pop_back();
text.pop_back();
text += "</p></body></html>";
appScanMutex.lock();
app.currentText = text;
appScanMutex.unlock();
} else {
std::string text = "<html><head/><body><p><span style=\" color:#00ffff;\">✔️</span> " + app.name + ": <span style=\" color:#00ffff;\">running</span> ";
for (auto& f : runningApps)
text += f + ", ";
text.pop_back();
text.pop_back();
text += "</p></body></html>";
appScanMutex.lock();
app.currentText = text;
appScanMutex.unlock();
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
t.detach();
}
std::string getDataStatePath() {
const auto HOME = getenv("HOME");
if (!HOME)
return "";
const auto XDG_DATA_HOME = getenv("XDG_DATA_HOME");
if (XDG_DATA_HOME)
return std::string{XDG_DATA_HOME} + "/hyprland-welcome";
return std::string{HOME} + "/.local/share/hyprland-welcome";
}
void CHyprlandWelcome::exitDontShowAgain() {
const auto STATEPATH = getDataStatePath();
if (STATEPATH.empty()) {
exit = true;
QApplication::exit(0);
return;
}
const auto CONFPATH = STATEPATH + "/state";
if (!std::filesystem::exists(STATEPATH))
std::filesystem::create_directories(STATEPATH);
execAndGet("echo \"noshow\" > " + CONFPATH);
exit = true;
QApplication::exit(0);
}
CHyprlandWelcome::CHyprlandWelcome(QWidget* parent) : QMainWindow(parent), ui(new Ui::CHyprlandWelcome) {
ui->setupUi(this);
const auto GHBUTTON = (QPushButton*)findByName("githubButton");
QObject::connect(GHBUTTON, &QPushButton::clicked, [] { QDesktopServices::openUrl(QString{"https://github.com/hyprwm/hyprland"}); });
const auto WIKIBUTTON = (QPushButton*)findByName("openWikiButton");
QObject::connect(WIKIBUTTON, &QPushButton::clicked, [] { QDesktopServices::openUrl(QString{"https://wiki.hyprland.org/Configuring/Configuring-Hyprland/"}); });
const auto EXITBUTTON = (QPushButton*)findByName("exitButton");
QObject::connect(EXITBUTTON, &QPushButton::clicked, [this] {
exit = true;
QApplication::exit(0);
});
const auto TABS = (QTabWidget*)findByName("tabs");
QObject::connect(TABS->tabBar(), &QTabBar::currentChanged, [TABS, this] { TABS->tabBar()->setCurrentIndex(this->currentTab); });
const auto TAB1NEXT = (QPushButton*)findByName("tab1Next");
QObject::connect(TAB1NEXT, &QPushButton::clicked, [TABS, this] {
this->currentTab = 1;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto TAB2NEXT = (QPushButton*)findByName("tab2Next");
QObject::connect(TAB2NEXT, &QPushButton::clicked, [TABS, this] {
this->currentTab = 2;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto TAB3NEXT = (QPushButton*)findByName("tab3Next");
QObject::connect(TAB3NEXT, &QPushButton::clicked, [TABS, this] {
this->currentTab = 3;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto TAB2BACK = (QPushButton*)findByName("tab2Back");
QObject::connect(TAB2BACK, &QPushButton::clicked, [TABS, this] {
this->currentTab = 0;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto TAB3BACK = (QPushButton*)findByName("tab3Back");
QObject::connect(TAB3BACK, &QPushButton::clicked, [TABS, this] {
this->currentTab = 1;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto TAB4BACK = (QPushButton*)findByName("tab4Back");
QObject::connect(TAB4BACK, &QPushButton::clicked, [TABS, this] {
this->currentTab = 2;
TABS->tabBar()->setCurrentIndex(this->currentTab);
});
const auto OPENTERMINAL = (QPushButton*)findByName("openTerminalButton");
QObject::connect(OPENTERMINAL, &QPushButton::clicked, [TABS, this] {
QProcess* term = new QProcess(this);
term->start("/bin/sh", QStringList{"-c", "kitty || alacritty || wezterm || foot || konsole || gnome-terminal || xterm"});
});
const auto OPENTERMINAL2 = (QPushButton*)findByName("openTerminalButton2");
QObject::connect(OPENTERMINAL2, &QPushButton::clicked, [TABS, this] {
QProcess* term = new QProcess(this);
term->start("/bin/sh", QStringList{"-c", "kitty || alacritty || wezterm || foot || konsole || gnome-terminal || xterm"});
});
const auto EXITDONTSHOW = (QPushButton*)findByName("dontShowAgain");
const auto FINISH = (QPushButton*)findByName("finishButton");
QObject::connect(EXITDONTSHOW, &QPushButton::clicked, [this] { this->exitDontShowAgain(); });
QObject::connect(FINISH, &QPushButton::clicked, [this] { this->exitDontShowAgain(); });
TABS->tabBar()->setCurrentIndex(this->currentTab);
startAppTimer();
}
CHyprlandWelcome::~CHyprlandWelcome() {
delete ui;
}