157 lines
6.5 KiB
Bash
Executable File
157 lines
6.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# args_json: {"type":"object","required":["action"],"properties":{"action":{"type":"string","description":"list|current|switch|create|remove"},"name":{"type":"string","description":"Activity name or ID"}}}
|
|
# ollie:prompt
|
|
# ## gui_activities
|
|
#
|
|
# Manage KDE Plasma Activities.
|
|
#
|
|
# **Args**: `[action]` or `[action, name_or_id]`
|
|
# - Actions: `list`, `current`, `switch`, `create`, `remove`
|
|
#
|
|
# ```
|
|
# gui_activities(action="list")
|
|
# gui_activities(action="switch", name="Development")
|
|
# ```
|
|
# ollie:end
|
|
set -euo pipefail
|
|
|
|
SERVICE="org.kde.ActivityManager"
|
|
OBJ_PATH="/ActivityManager/Activities"
|
|
IFACE="org.kde.ActivityManager.Activities"
|
|
|
|
source "$(dirname "$0")/_lib/args.sh"
|
|
|
|
cmd=$(arg_get "action" "list")
|
|
name=$(arg_get "name" "")
|
|
|
|
case "$cmd" in
|
|
list)
|
|
# List all activities with their names and states
|
|
ids=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ListActivities" 2>/dev/null | \
|
|
sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g")
|
|
|
|
if [ -z "$ids" ]; then
|
|
echo "ERROR: cannot list activities (ActivityManager not running?)" >&2
|
|
exit 1
|
|
fi
|
|
|
|
current=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.CurrentActivity" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
|
|
echo "ID STATE NAME"
|
|
while IFS= read -r id; do
|
|
[ -z "$id" ] && continue
|
|
name=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ActivityName" "$id" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
if [ "$id" = "$current" ]; then
|
|
state="active"
|
|
else
|
|
state="stopped"
|
|
fi
|
|
echo "$id $state $name"
|
|
done <<< "$ids"
|
|
;;
|
|
current)
|
|
id=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.CurrentActivity" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
name=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ActivityName" "$id" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
echo "$id $name"
|
|
;;
|
|
switch)
|
|
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|current|switch|create|remove", "required": false}, {"name": "name_or_id", "type": "string", "description": "Activity name or ID (for switch/create/remove)", "required": false}], "examples": [["list"], ["switch", "Development"]]}}'; exit 1; fi
|
|
target="$name"
|
|
|
|
# Try as ID first
|
|
result=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.SetCurrentActivity" "$target" 2>/dev/null) && {
|
|
echo "switched to: $target"
|
|
exit 0
|
|
}
|
|
|
|
# Search by name
|
|
ids=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ListActivities" 2>/dev/null | \
|
|
sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g")
|
|
|
|
while IFS= read -r id; do
|
|
[ -z "$id" ] && continue
|
|
name=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ActivityName" "$id" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
if echo "$name" | grep -qi "$target"; then
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.SetCurrentActivity" "$id" >/dev/null
|
|
echo "switched to: $name ($id)"
|
|
exit 0
|
|
fi
|
|
done <<< "$ids"
|
|
|
|
echo "ERROR: no activity matching '$target'" >&2
|
|
exit 1
|
|
;;
|
|
create)
|
|
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|current|switch|create|remove", "required": false}, {"name": "name_or_id", "type": "string", "description": "Activity name or ID (for switch/create/remove)", "required": false}], "examples": [["list"], ["switch", "Development"]]}}'; exit 1; fi
|
|
name="$name"
|
|
id=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.AddActivity" "$name" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
echo "created: $id ($name)"
|
|
;;
|
|
remove)
|
|
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|current|switch|create|remove", "required": false}, {"name": "name_or_id", "type": "string", "description": "Activity name or ID (for switch/create/remove)", "required": false}], "examples": [["list"], ["switch", "Development"]]}}'; exit 1; fi
|
|
target="$name"
|
|
|
|
# Try as ID directly
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.RemoveActivity" "$target" 2>/dev/null && {
|
|
echo "removed: $target"
|
|
exit 0
|
|
}
|
|
|
|
# Search by name
|
|
ids=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ListActivities" 2>/dev/null | \
|
|
sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g")
|
|
|
|
while IFS= read -r id; do
|
|
[ -z "$id" ] && continue
|
|
name=$(gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.ActivityName" "$id" 2>/dev/null | \
|
|
sed "s/^('//;s/',)$//")
|
|
if echo "$name" | grep -qi "$target"; then
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.RemoveActivity" "$id" >/dev/null
|
|
echo "removed: $name ($id)"
|
|
exit 0
|
|
fi
|
|
done <<< "$ids"
|
|
|
|
echo "ERROR: no activity matching '$target'" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|current|switch|create|remove", "required": false}, {"name": "name_or_id", "type": "string", "description": "Activity name or ID (for switch/create/remove)", "required": false}], "examples": [["list"], ["switch", "Development"]]}}' >&2
|
|
exit 1
|
|
;;
|
|
esac
|