#!/bin/bash # SPDX-FileCopyrightText: Christian Schendel # SPDX-License-Identifier: 0BSD set -eo pipefail ## NOTE A patched version of qt6ct is required. ## NOTE The benefits are: ## NOTE KDE color schemes support,KDE QML applications theming support and ## NOTE KDE's icon engine support ## NOTE See https://aur.archlinux.org/pkgbase/qt6ct-kde/ version='@VERSION@' pkgname='night-theme-switcher' default_config_file="/usr/share/$pkgname/nts.skel" user_name=$(getent passwd "$USER") homedir=$(echo "$user_name" | cut -d: -f6) # allow user to override from cli thus using multiple files as needed XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$homedir/.config}" user_config_file=${user_config_file:-$XDG_CONFIG_HOME/$pkgname/$pkgname.conf} ### Begin insert of Arch script # Avoid any encoding problems export LANG=C # check if messages are to be printed using color unset all_off bold blue green red yellow if [[ -t 2 ]]; then # prefer terminal safe colored and bold text when tput is supported if tput setaf 0 &>/dev/null; then all_off="$(tput sgr0)" bold="$(tput bold)" blue="${bold}$(tput setaf 4)" green="${bold}$(tput setaf 2)" red="${bold}$(tput setaf 1)" yellow="${bold}$(tput setaf 3)" else all_off="\e[1;0m" bold="\e[1;1m" blue="${bold}\e[1;34m" green="${bold}\e[1;32m" red="${bold}\e[1;31m" yellow="${bold}\e[1;33m" fi fi readonly all_off bold blue green red yellow ### End insert of Arch script # dependency checks probably not needed but they do not hurt if [ ! "$QT_QPA_PLATFORMTHEME" == "qt6ct" ]; then notify-send \ 'Wrong environment variable' \ 'Please set QT_QPA_PLATFORMTHEME=qt6ct.' \ -h string:x-canonical-private-synchronous:sys-notify \ -u critical \ -i dialog-error mesg="environment variable QT_QPA_PLATFORMTHEME=qt6ct is not set." echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if ! command -v breeze-settings6 >/dev/null 2>&1; then notify-send \ 'Missing dependency' \ 'Please install breeze.' \ -h string:x-canonical-private-synchronous:sys-notify \ -u critical \ -i dialog-error mesg="breeze is required to use this script." echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if ! command -v qt6ct >/dev/null 2>&1; then notify-send \ 'Missing dependency' \ 'Please install qt6ct.' \ -h string:x-canonical-private-synchronous:sys-notify \ -u critical \ -i dialog-error mesg="qt6ct-kde is required to use this script." echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if ! command -v gsettings >/dev/null 2>&1; then notify-send \ 'Missing dependency' \ 'gsettings not found.\nPlease install glib2.' \ -h string:x-canonical-private-synchronous:sys-notify \ -u critical \ -i dialog-error mesg="glib2 is required to use this script." echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi check_config() { if [[ ! -f $default_config_file ]]; then local mesg="$default_config_file is missing. Reinstall this package to continue." echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -f "$user_config_file" ]]; then echo -e "${bold}------------------------------------------------------------${all_off}" echo -e "${bold} No config file found so creating a fresh one in:${all_off}" echo -e "${bold}${blue} $XDG_CONFIG_HOME/$pkgname/$pkgname.conf${all_off}" echo echo -e "${bold} Edit this file before invoking $pkgname again.${all_off}" echo -e "${bold}------------------------------------------------------------${all_off}" install -Dm644 $default_config_file "$user_config_file" exit 0 else # shellcheck source=nts.skel . "$user_config_file" # parse config file for correctness if [[ ! -f "$kcolorscheme_light" ]]; then local mesg="Invalid kcolorscheme_light defined in\n ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -f "$kcolorscheme_dark" ]]; then local mesg="Invalid kcolorscheme_dark defined in ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -d "/usr/share/icons/$icon_theme_light" ]]; then local mesg="Invalid icon_theme_light defined in\n ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -d "/usr/share/icons/$icon_theme_dark" ]]; then local mesg="Invalid icon_theme_dark defined in ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -d "/usr/share/themes/$gtk_theme_light" ]]; then local mesg="Invalid gtk_theme_light defined in\n ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi if [[ ! -d "/usr/share/themes/$gtk_theme_dark" ]]; then local mesg="Invalid gtk_theme_dark defined in ${blue}$user_config_file" echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}" exit 1 fi fi } _reset_settings() { ## GTK3 gtk3_settings_file="$XDG_CONFIG_HOME/gtk-3.0/settings.ini" gtk4_settings_file="$XDG_CONFIG_HOME/gtk-4.0/settings.ini" qt6ct_settings_file="$XDG_CONFIG_HOME/qt6ct/qt6ct.conf" gsettings reset org.gnome.desktop.interface color-scheme gsettings reset org.gnome.desktop.interface gtk-theme gsettings reset org.gnome.desktop.interface icon-theme if [[ ! -w "$gtk3_settings_file" ]]; then touch "$gtk3_settings_file" fi if [[ ! -w "$gtk4_settings_file" ]]; then touch "$gtk4_settings_file" fi if [[ ! -w "$qt6ct_settings_file" ]]; then touch "$qt6ct_settings_file" fi } _set_theme() { ## GTK gsettings set org.gnome.desktop.interface color-scheme "$gtk_color_scheme" gsettings set org.gnome.desktop.interface gtk-theme "$gtk_theme" gsettings set org.gnome.desktop.interface icon-theme "$icon_theme" ## QT if [[ -x /usr/lib/plasma-changeicons ]]; then /usr/lib/plasma-changeicons "$icon_theme" >/dev/null 2>&1 fi if [[ -w "$HOME/.config/qt5ct/qt5ct.conf" ]]; then sed -i "s/icon_theme=.*/icon_theme=$icon_theme/g" \ "$HOME/.config/qt5ct/qt5ct.conf" sed -i "s,custom_palette=.*,custom_palette=$qt_color_scheme_path,g" \ "$HOME/.config/qt5ct/qt5ct.conf" fi if [[ -w "$HOME/.config/qt6ct/qt6ct.conf" ]]; then sed -i "s/icon_theme=.*/icon_theme=$icon_theme/g" \ "$HOME/.config/qt6ct/qt6ct.conf" sed -i "s/custom_palette=.*/custom_palette=$qt_custom_palette/g" \ "$HOME/.config/qt6ct/qt6ct.conf" sed -i "s,color_scheme_path=.*,color_scheme_path=$qt_color_scheme_path,g" \ "$HOME/.config/qt6ct/qt6ct.conf" fi } switch-dark-light() { gtk_color_scheme=$(gsettings get org.gnome.desktop.interface color-scheme) if [[ ! $gtk_color_scheme == ''\''prefer-dark'\''' ]]; then gtk_color_scheme=prefer-dark gtk_prefer_dark_theme=1 gtk_theme=$gtk_theme_dark icon_theme=$icon_theme_dark qt_custom_palette=true qt_color_scheme_path=$kcolorscheme_dark _set_theme else gtk_color_scheme=prefer-light gtk_prefer_dark_theme=0 gtk_theme=$gtk_theme_light icon_theme=$icon_theme_light qt_custom_palette=true qt_color_scheme_path=$kcolorscheme_light _set_theme fi } restore-theme-on-login() { find "$HOME/.config//gtk-3.0/" ! -name 'bookmarks' -type f -exec rm -f {} + find "$HOME/.config//gtk-4.0/" ! -name 'bookmarks' -type f -exec rm -f {} + gtk_color_scheme=$(gsettings get org.gnome.desktop.interface color-scheme) if [[ ! $gtk_color_scheme == ''\''prefer-dark'\''' ]]; then gtk_color_scheme=prefer-light gtk_prefer_dark_theme=0 gtk_theme=$gtk_theme_light icon_theme=$icon_theme_light qt_custom_palette=true qt_color_scheme_path=$kcolorscheme_light _set_theme else gtk_color_scheme=prefer-dark gtk_prefer_dark_theme=1 gtk_theme=$gtk_theme_dark icon_theme=$icon_theme_dark qt_custom_palette=true qt_color_scheme_path=$kcolorscheme_dark _set_theme fi } preview() { echo -e "${bold}$pkgname v$version${all_off}" echo echo -e "${bold} KColorScheme" if [[ -f "$kcolorscheme_light" ]]; then echo -en "${bold} Light:" echo -e "$(tput cr)$(tput cuf 10)${blue}$kcolorscheme_light${all_off}${bold}${all_off}" fi if [[ -f "$kcolorscheme_dark" ]]; then echo -en "${bold} Dark:" echo -e "$(tput cr)$(tput cuf 10)${blue}$kcolorscheme_dark${all_off}${bold}${all_off}" fi echo echo -e "${bold} Icon Theme" if [[ -d "/usr/share/icons/$icon_theme_light" ]]; then echo -en "${bold} Light:" echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/icons/$icon_theme_light${all_off}${bold}${all_off}" fi if [[ -d "/usr/share/icons/$icon_theme_dark" ]]; then echo -en "${bold} Dark:" echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/icons/$icon_theme_dark${all_off}${bold}${all_off}" fi echo echo -e "${bold} GTK Theme" if [[ -d "/usr/share/themes/$gtk_theme_light" ]]; then echo -en "${bold} Light:" echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/themes/$gtk_theme_light${all_off}${bold}${all_off}" fi if [[ -d "/usr/share/themes/$gtk_theme_dark" ]]; then echo -en "${bold} Dark:" echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/themes/$gtk_theme_dark${all_off}${bold}${all_off}" fi } case "$1" in s) check_config && switch-dark-light ;; r) check_config && restore-theme-on-login ;; c) check_config && _reset_settings && preview ;; p) check_config && preview ;; *) echo -e "${bold}$pkgname v$version${all_off}" echo echo -e "${bold} Usage: ${red}$0${all_off}${bold} ${all_off}${blue}[option]${all_off}" echo echo -e "${bold} s) swith between dark and light theme${all_off}" echo -e "${bold} r) restore theme (usefull only once after login)${all_off}" echo -e "${bold} c) create sane default GTK/qt6ct settings files${all_off}" echo -e "${bold} p) Preview settings${all_off}" ;; esac # vim: set ts=4 sw=4 et: