#!/bin/sh

# SPDX-FileCopyrightText: 2018 Oded Arbel <https://geek.co.li>
# SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
#
# SPDX-License-Identifier: MIT

## Credits: https://geek.co.il/2018/07/30/script-day-different-default-browser-per-kde-activity

set -eu

if ! command -v notify-send >/dev/null &&
  ! command -v kdialog >/dev/null; then
  exit 1
fi
command -v qdbus >/dev/null || exit 1

case "${XDG_SESSION_DESKTOP:-}" in
  KDE|plasma) ;;
  *) exit 1;;
esac

service="org.kde.ActivityManager"
interface="$service.Activities"
path="/ActivityManager/Activities"
signal="CurrentActivityChanged"

dbus-monitor --profile \
  "type=signal,path=$path,interface=$interface,member=$signal" | \
while read -r _ _ _ _ _ path interface member; do
  test "$member" = "$signal" || continue
  id="$(qdbus "$service" "$path" "$interface.CurrentActivity")"
  name="$(qdbus "$service" "$path" "$interface.ActivityName" "$id")"
  if command -v kdialog >/dev/null; then
    kdialog --title "Activity: $name" --passivepopup "Switched Activities" 3
  elif command -v notify-send >/dev/null; then
    notify-send -u normal -t 3000 "Activity: $name" "Switched activities"
  fi
done
