Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
A checkable tray menu item and a Diagnostics-tab checkbox write or remove ~/.config/autostart/strawberry-tray.desktop (XDG autostart, atomic write), pointing at the installed strawberry-tray when there is one. Sessions that do not run autostart entries on their own (sway, i3, Hyprland, niri without uwsm/dex) get a note with the exact line to add to the compositor config instead of a silent no-op; GNOME, KDE, XFCE and the like, or an active xdg-desktop-autostart.target, need nothing more. The logic is Qt-free (app/autostart.cpp) with a unit test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6 KiB
C++
174 lines
6 KiB
C++
#include "autostart.hpp"
|
|
|
|
#include "strawberry/proc_read.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <sys/wait.h>
|
|
#include <vector>
|
|
|
|
namespace strawberry::app {
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace {
|
|
|
|
std::string env_or_empty(const char* name) {
|
|
const char* v = std::getenv(name);
|
|
return v != nullptr ? std::string(v) : std::string();
|
|
}
|
|
|
|
std::string lower(std::string s) {
|
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
|
return s;
|
|
}
|
|
|
|
std::vector<std::string> desktops(const std::string& current) {
|
|
std::vector<std::string> out;
|
|
std::stringstream in(lower(current));
|
|
std::string item;
|
|
while (std::getline(in, item, ':')) {
|
|
if (!item.empty()) out.push_back(item);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool has_desktop(const AutostartEnv& env, std::initializer_list<const char*> names) {
|
|
const auto list = desktops(env.current_desktop);
|
|
for (const char* n : names) {
|
|
if (std::find(list.begin(), list.end(), n) != list.end()) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Desktop-entry values are not shell-quoted; an exec path with spaces or
|
|
/// specials is quoted per the spec.
|
|
std::string quote_exec(const std::string& path) {
|
|
if (path.find_first_of(" \t\"'\\$`") == std::string::npos) return path;
|
|
std::string out = "\"";
|
|
for (char c : path) {
|
|
if (c == '"' || c == '`' || c == '$' || c == '\\') out += '\\';
|
|
out += c;
|
|
}
|
|
return out + "\"";
|
|
}
|
|
|
|
} // namespace
|
|
|
|
AutostartEnv autostart_env_from_process() {
|
|
AutostartEnv env;
|
|
env.xdg_config_home = env_or_empty("XDG_CONFIG_HOME");
|
|
env.home = env_or_empty("HOME");
|
|
env.current_desktop = env_or_empty("XDG_CURRENT_DESKTOP");
|
|
// Run the check without a shell; output is ignored, the status matters.
|
|
if (FILE* p = ::popen("systemctl --user -q is-active xdg-desktop-autostart.target 2>/dev/null", "r")) {
|
|
const int status = ::pclose(p);
|
|
env.autostart_target_active = status != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
|
}
|
|
return env;
|
|
}
|
|
|
|
std::string autostart_file(const AutostartEnv& env) {
|
|
const std::string base = !env.xdg_config_home.empty() && env.xdg_config_home.front() == '/'
|
|
? env.xdg_config_home
|
|
: env.home + "/.config";
|
|
return base + "/autostart/strawberry-tray.desktop";
|
|
}
|
|
|
|
std::string autostart_entry(const std::string& exec_path) {
|
|
return "[Desktop Entry]\n"
|
|
"Type=Application\n"
|
|
"Name=Strawberry\n"
|
|
"Comment=Strawberry security scanner (tray)\n"
|
|
"Exec=" + quote_exec(exec_path) + "\n"
|
|
"Icon=security-high\n"
|
|
"Terminal=false\n"
|
|
"X-GNOME-Autostart-enabled=true\n"
|
|
"X-Strawberry-Autostart=true\n";
|
|
}
|
|
|
|
bool autostart_enabled(const AutostartEnv& env) {
|
|
const auto text = read_small_file(autostart_file(env), 64 * 1024);
|
|
if (!text) return false;
|
|
std::stringstream in(*text);
|
|
std::string line;
|
|
while (std::getline(in, line)) {
|
|
if (!line.empty() && line.back() == '\r') line.pop_back();
|
|
if (lower(line) == "hidden=true" || lower(line) == "x-gnome-autostart-enabled=false") return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool set_autostart(const AutostartEnv& env, bool enable, const std::string& exec_path, std::string& error) {
|
|
try {
|
|
const fs::path file = autostart_file(env);
|
|
std::error_code ec;
|
|
if (!enable) {
|
|
fs::remove(file, ec);
|
|
if (ec) {
|
|
error = "could not remove " + file.string() + ": " + ec.message();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
if (exec_path.empty() || exec_path.front() != '/') {
|
|
error = "need an absolute path to strawberry-tray";
|
|
return false;
|
|
}
|
|
fs::create_directories(file.parent_path(), ec);
|
|
if (ec) {
|
|
error = "could not create " + file.parent_path().string() + ": " + ec.message();
|
|
return false;
|
|
}
|
|
const fs::path tmp = file.string() + ".tmp";
|
|
{
|
|
std::ofstream out(tmp, std::ios::trunc);
|
|
out << autostart_entry(exec_path);
|
|
out.flush();
|
|
if (!out) {
|
|
error = "could not write " + tmp.string();
|
|
return false;
|
|
}
|
|
}
|
|
fs::rename(tmp, file, ec);
|
|
if (ec) {
|
|
error = "could not install " + file.string() + ": " + ec.message();
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
error = e.what();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool session_runs_xdg_autostart(const AutostartEnv& env) {
|
|
if (env.autostart_target_active) return true;
|
|
return has_desktop(env, {"gnome", "kde", "xfce", "lxqt", "lxde", "cinnamon", "x-cinnamon", "mate", "budgie",
|
|
"budgie-desktop", "pantheon", "unity", "deepin", "enlightenment"});
|
|
}
|
|
|
|
std::string session_hint(const AutostartEnv& env, const std::string& exec_path) {
|
|
if (session_runs_xdg_autostart(env)) return {};
|
|
const std::string exec = quote_exec(exec_path.empty() ? std::string("strawberry-tray") : exec_path);
|
|
if (has_desktop(env, {"sway", "i3"})) {
|
|
return "This session (sway/i3) does not run autostart entries on its own. Add this line to your "
|
|
"sway config: exec " + exec;
|
|
}
|
|
if (has_desktop(env, {"hyprland"})) {
|
|
return "Hyprland does not run autostart entries on its own. Add to hyprland.conf: exec-once = " + exec;
|
|
}
|
|
if (has_desktop(env, {"niri"})) {
|
|
return "niri does not run autostart entries on its own. Add to its config: spawn-at-startup \"" +
|
|
(exec_path.empty() ? std::string("strawberry-tray") : exec_path) + "\"";
|
|
}
|
|
return "This session may not run autostart entries. If Strawberry does not start at login, launch " + exec +
|
|
" from your session or window manager startup.";
|
|
}
|
|
|
|
} // namespace strawberry::app
|