Skip to content

Commit

Permalink
Merge pull request #76 from mapster/feature/apply-autostart-condition
Browse files Browse the repository at this point in the history
Check autostart condition before launching
  • Loading branch information
Biont committed Jun 6, 2023
2 parents 4c104e4 + e37172c commit 7a9a71b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sway-launcher-desktop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,27 @@ function generate-command() {
}' "$1"
}

function shouldAutostart() {
local condition="$(cat $1 | grep "AutostartCondition" | cut -d'=' -f2)"
local filename="${XDG_CONFIG_HOME-${HOME}/.config}/${condition#* }"
case $condition in
if-exists*)
[[ -e $filename ]]
;;
unless-exists*)
[[ ! -e $filename ]]
;;
*)
return 0
;;
esac
}

function autostart() {
for application in $(list-autostart); do
(exec setsid /bin/sh -c "$(run-desktop "${application}")" &>/dev/null &)
if shouldAutostart "$application" ; then
(exec setsid /bin/sh -c "$(run-desktop "${application}")" &>/dev/null &)
fi
done
}

Expand Down
20 changes: 20 additions & 0 deletions tests/autostart-condition.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bats

setup() {
export DID_RUN="$(mktemp -d)"
export XDG_CONFIG_HOME=./data/autostart-folders/condition-home
export XDG_CONFIG_DIRS=
}

teardown() {
rm -r $DID_RUN
}

@test "Only starts applications with passing AutostartCondition" {
run ../sway-launcher-desktop.sh autostart 3>&2
echo -e "DID_RUN=$DID_RUN"
[[ ! -e $DID_RUN/unless-exists.file-exists ]]
[[ -e $DID_RUN/unless-exists.file-not-exists ]]
[[ ! -e $DID_RUN/if-exists.file-not-exists ]]
[[ -e $DID_RUN/if-exists.file-exists ]]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Desktop Entry]
Name=Initial Setup
Exec=touch $DID_RUN/if-exists.file-exists
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;System;
OnlyShowIn=GNOME;Unity;
NoDisplay=true
AutostartCondition=if-exists marker-file.txt
X-GNOME-HiddenUnderSystemd=true
X-Ubuntu-Gettext-Domain=gnome-initial-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Desktop Entry]
Name=Initial Setup
Exec=touch $DID_RUN/if-exists.file-not-exists
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;System;
OnlyShowIn=GNOME;Unity;
NoDisplay=true
AutostartCondition=if-exists non-existing-file.txt
X-GNOME-HiddenUnderSystemd=true
X-Ubuntu-Gettext-Domain=gnome-initial-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Desktop Entry]
Name=Initial Setup
Exec=touch $DID_RUN/unless-exists.file-exists
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;System;
OnlyShowIn=GNOME;Unity;
NoDisplay=true
AutostartCondition=unless-exists marker-file.txt
X-GNOME-HiddenUnderSystemd=true
X-Ubuntu-Gettext-Domain=gnome-initial-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Desktop Entry]
Name=Initial Setup
Exec=touch $DID_RUN/unless-exists.file-not-exists
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;System;
OnlyShowIn=GNOME;Unity;
NoDisplay=true
AutostartCondition=unless-exists non-existing-file.txt
X-GNOME-HiddenUnderSystemd=true
X-Ubuntu-Gettext-Domain=gnome-initial-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yes

0 comments on commit 7a9a71b

Please sign in to comment.