Skip to content

Commit

Permalink
Only send desktop notification if the list of available updates diffe…
Browse files Browse the repository at this point in the history
…rs from the last check (#61)

* Only send desktop notification if the list of available updates differs from the last check

Currently, the desktop notification included in the `check()` function is sent whenever there are update available without any sort of filter.
That means that, with the systemd timer triggering the `check()` function each hour,  you will get a notification for the same exact available updates each hour over and over until you actually apply them (in the eventuality that there's no other available updates in the mean time).

This commit makes the `check()` function verify that the current list of available updates differs from the one obtained by the last check before re-sending a notification.
That way, a new notification is sent only if there are new available updates compare to the last check so you don't get multiple notifications for the exact same update over time.

* Better order for the condition

* Only create the state directory and files if notify-send is installed
This directory and the files contained in it are only relevant for the desktop notifications

* Check/create the state dir/files for the notif before checking if there are pending updates
That makes the current_check and last_check files reflectst the reality by being emptied if there are no update available.
The diff is only ran if the variable  isn't empty so there won't be any false positive.

* Remove empty lines from the current_state file after redirecting the update_available var value to it
When there are no update available, redirecting the value of the update_available var (which is then empty) to the file produces an empty (but existing) line.
This commit aims to remove it so the file is actually empty (instead of containing an empty/blank line).
  • Loading branch information
Antiz96 authored Nov 24, 2023
1 parent 401b265 commit 150fb11
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/script/arch-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# https://github.com/Antiz96/arch-update
# Licensed under the GPL-3.0 license

# Variables definition
# General variables
name="arch-update"
version="1.6.2"
option="${1}"
Expand Down Expand Up @@ -298,22 +298,39 @@ check() {
icon_checking

if [ -n "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_number=$( (checkupdates ; "${aur_helper}" -Qua ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d') | wc -l )
update_available=$(checkupdates ; "${aur_helper}" -Qua ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
elif [ -n "${aur_helper}" ] && [ -z "${flatpak}" ]; then
update_number=$( (checkupdates ; "${aur_helper}" -Qua) | wc -l )
update_available=$(checkupdates ; "${aur_helper}" -Qua)
elif [ -z "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_number=$( (checkupdates ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d') | wc -l )
update_available=$(checkupdates ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
else
update_number=$(checkupdates | wc -l)
update_available=$(checkupdates)
fi

if [ -n "${notif}" ]; then
statedir="${XDG_STATE_HOME:-${HOME}/.local/state}/${name}"
mkdir -p "${statedir}"

if [ -f "${statedir}/current_check" ]; then
mv -f "${statedir}/current_check" "${statedir}/last_check"
fi

if [ "${update_number}" -ne 0 ]; then
echo "${update_available}" > "${statedir}/current_check"
sed -i '/^\s*$/d' "${statedir}/current_check"
fi

if [ -n "${update_available}" ]; then
icon_updates_available

if [ -n "${notif}" ]; then
if [ "${update_number}" -eq 1 ]; then
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} update available"
else
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} updates available"
if ! diff "${statedir}/current_check" "${statedir}/last_check" &>/dev/null; then
update_number=$(wc -l "${statedir}/current_check" | awk '{print $1}')

if [ "${update_number}" -eq 1 ]; then
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} update available"
else
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} updates available"
fi
fi
fi
else
Expand Down

0 comments on commit 150fb11

Please sign in to comment.