From 811ea5bd5dae079cf114e5f9f4a4788ade55d5f7 Mon Sep 17 00:00:00 2001 From: Stacy Harper Date: Thu, 25 Mar 2021 00:28:40 +0100 Subject: [PATCH] Add a contact menu to manage them Signed-off-by: Stacy Harper Signed-off-by: Maarten van Gompel --- scripts/core/sxmo_appmenu.sh | 1 + scripts/core/sxmo_common.sh | 2 + scripts/core/sxmo_contactmenu.sh | 98 ++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100755 scripts/core/sxmo_contactmenu.sh diff --git a/scripts/core/sxmo_appmenu.sh b/scripts/core/sxmo_appmenu.sh index 1042ae4..79e4feb 100755 --- a/scripts/core/sxmo_appmenu.sh +++ b/scripts/core/sxmo_appmenu.sh @@ -391,6 +391,7 @@ programchoicesinit() { $(command -v foxtrotgps >/dev/null && echo "$icon_gps Maps ^ 0 ^ foxtrotgps") $icon_phn Dialer ^ 0 ^ sxmo_modemdial.sh $icon_msg Texts ^ 0 ^ sxmo_modemtext.sh + $icon_usr Contacts ^ 0 ^ sxmo_contactmenu.sh $(command -v megapixels >/dev/null && echo "$icon_cam Camera ^ 0 ^ GDK_SCALE=2 megapixels") $icon_net Networks ^ 0 ^ sxmo_networks.sh $icon_mus Audio ^ 0 ^ sxmo_appmenu.sh audioout diff --git a/scripts/core/sxmo_common.sh b/scripts/core/sxmo_common.sh index 4f99f83..1b2e06f 100644 --- a/scripts/core/sxmo_common.sh +++ b/scripts/core/sxmo_common.sh @@ -15,6 +15,8 @@ export NOTIFDIR="$XDG_DATA_HOME"/sxmo/notifications export CACHEDIR="$XDG_CACHE_HOME"/sxmo # shellcheck disable=SC2034 export LOGDIR="$XDG_DATA_HOME"/sxmo/modem +# shellcheck disable=SC2034 +export CONTACTFILE="$XDG_CONFIG_HOME/sxmo/contacts.tsv" command -v "$KEYBOARD" > /dev/null || export KEYBOARD=svkbd-mobile-intl diff --git a/scripts/core/sxmo_contactmenu.sh b/scripts/core/sxmo_contactmenu.sh new file mode 100755 index 0000000..f1761d0 --- /dev/null +++ b/scripts/core/sxmo_contactmenu.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env sh + +# shellcheck source=scripts/core/sxmo_common.sh +. "$(dirname "$0")/sxmo_common.sh" + +newcontact() { + name="$(echo | sxmo_dmenu_with_kb.sh -c -l 2 -p "Name")" + number="$(echo | sxmo_dmenu_with_kb.sh -c -l 2 -p "Number")" + + PICKED="$number $name" # now act like if we picked this new contact + echo "$PICKED" >> "$CONTACTFILE" +} + +editcontactname() { + oldnumber="$(echo "$1" | cut -d" " -f1)" + oldname="$(echo "$1" | cut -d" " -f2)" + + ENTRIES="$(printf %b "Old name: $oldname")" + PICKED="$( + echo "$ENTRIES" | + sxmo_dmenu_with_kb.sh -c -l 3 -p "Edit Contact" + )" + + if echo "$PICKED" | grep -q "^Old name: "; then + editcontact "$1" + else + newcontact="$oldnumber $PICKED" + sed -i "s/^$1$/$newcontact/" "$CONTACTFILE" && editcontact "$newcontact" + fi +} + +editcontactnumber() { + oldnumber="$(echo "$1" | cut -d" " -f1)" + oldname="$(echo "$1" | cut -d" " -f2)" + + ENTRIES="$(printf %b "Old number: $oldnumber")" + PICKED="$( + echo "$ENTRIES" | + sxmo_dmenu_with_kb.sh -c -l 3 -p "Edit Contact" + )" + + if echo "$PICKED" | grep -q "^Old number: "; then + editcontact "$1" + else + newcontact="$PICKED $oldname" + sed -i "s/^$1$/$newcontact/" "$CONTACTFILE" && editcontact "$newcontact" + fi +} + +deletecontact() { + name="$(echo "$1" | cut -d" " -f2)" + + ENTRIES="$(printf "Yes\nNo")" + PICKED="$( + echo "$ENTRIES" | + dmenu -c -l 3 -p "Delete $nameĀ ?" + )" + + echo "$PICKED" | grep -q "^Yes" && sed -i "/^$1$/d" "$CONTACTFILE" +} + +editcontact() { + number="$(echo "$1" | cut -d" " -f1)" + name="$(echo "$1" | cut -d" " -f2)" + ENTRIES="$(printf %b "Cancel\nDelete\nName: $name\nNumber: $number")" + + PICKED="$( + echo "$ENTRIES" | + dmenu -c -l 4 -p "Edit Contact" + )" + + if echo "$PICKED" | grep -q "^Delete"; then + deletecontact "$1" + elif echo "$PICKED" | grep -q "^Name: "; then + editcontactname "$1" + elif echo "$PICKED" | grep -q "^Number: "; then + editcontactnumber "$1" + fi +} + +main() { + while true; do + CONTACTS="$(sed 's/\t/: /g' "$CONTACTFILE")" + ENTRIES="$(echo "$CONTACTS" | xargs -0 printf "Close Menu\nNew Contact\n%s")" + + PICKED="$( + echo "$ENTRIES" | + sxmo_dmenu_with_kb.sh -i -c -l 10 -p "Contacts" + )" + + echo "$PICKED" | grep -q "Close Menu" && exit + echo "$PICKED" | grep -q "New Contact" && newcontact + + editcontact "$(echo "$PICKED" | sed 's/: /\t/g')" + done +} + +main