|
#!/bin/sh |
|
# Manage file emblems |
|
# Tool for use with PCManFM context menu in SliTaz Linux |
|
# Aleksey Bobylev, 24 July 2019; 31 January 2020 |
|
|
|
|
|
# Input: |
|
# $1 - mode: 'list' (default) or 'add' - used internally |
|
# $2 - URI of the requested file (folder); it is may be prefixed |
|
# by 'file://' protocol like 'file:///usr/bin' or with any other supported one |
|
# (network file stores like smb://, etc.) |
|
# Note, user can attach an emblems to any file or folder, for example to /bin/. |
|
|
|
mode="$1" |
|
URI="$2" |
|
simpleURI="$(echo -n "$URI" | sed 's|file://||')" |
|
|
|
# Semi-random list of available emblems since it's not standardized anywhere |
|
|
|
AVAILABLE="emblem-default emblem-favorite emblem-important emblem-shared \ |
|
emblem-documents emblem-downloads emblem-music emblem-photos \ |
|
emblem-videos emblem-mail emblem-ok emblem-link emblem-symbolic-link \ |
|
emblem-generic emblem-new emblem-urgent emblem-web \ |
|
emblem-synchronizing emblem-system emblem-readonly emblem-unreadable" |
|
|
|
|
|
# Get emblems from the requested file (folder). |
|
# Responce of `gvfs-info -a 'metadata::emblems' /bin` looks like: |
|
# uri: file:///bin <- can't switch off this line using gvfs-info attributes |
|
# attributes: <- this too, and it's translatable |
|
# metadata::emblems: [emblem-important, emblem-system] <- what we need |
|
|
|
EMBLEMS=" $(gvfs-info -a 'metadata::emblems' $URI \ |
|
| sed -n '/metadata::emblems:/s|.*\[||;s|,||g;s|\]||p') " |
|
# Result is " emblem-important emblem-system " (note all the spaces!) |
|
#notify-send "mode=«$mode» URI=«$URI» simpleURI=«$simpleURI» EMBLEMS=«$EMBLEMS»" |
|
|
|
# Auxiliary function for Yad: make the checkboxed list |
|
# Find the rest of emblems to support any (already set) emblems not listed above |
|
|
|
list_emblems() { |
|
REST="$EMBLEMS" |
|
|
|
for i in $AVAILABLE; do |
|
if [ "${EMBLEMS/$i /}" != "$EMBLEMS" ]; then |
|
echo -n "true $i $i " |
|
else |
|
echo -n "false $i $i " |
|
fi |
|
|
|
REST="${REST/$i /}" |
|
done |
|
|
|
for i in $REST; do |
|
echo -n "true $i $i " |
|
done |
|
} |
|
|
|
|
|
# Display Yad dialog |
|
# Unfortunately, PCManFM don't redraw its window automatically, so to see |
|
# updated emblems user need to refresh it manually (Ctrl+R or F5) |
|
|
|
use_list() { |
|
new_emblems=$(yad \ |
|
--mouse --width=300 --height=400 \ |
|
--window-icon=emblem-favorite \ |
|
--title="$simpleURI emblems" \ |
|
--image=emblem-favorite --image-on-top \ |
|
--text="Select the required emblems. |
|
<i>Refresh the folder (Ctrl+R or F5) to update the view when done.</i>" \ |
|
--button='gtk-add:2' \ |
|
--button='gtk-cancel:1' \ |
|
--button='gtk-apply:0' \ |
|
--list \ |
|
--column='Select:CHK' \ |
|
--column='Icon:IMG' \ |
|
--column='Name:TEXT' \ |
|
--always-print-result --print-column='3' --separator='' \ |
|
--checklist \ |
|
$(list_emblems) \ |
|
) |
|
|
|
code="$?" |
|
case $code in |
|
0) |
|
# Set the emblems |
|
# New emblems should come as separate arguments, so $new_emblems isn't quoted here |
|
# notify-send "gvfs-set-attribute \"$URI\" -t stringv metadata::emblems $new_emblems" |
|
# URIsafe="$(echo "$URI" | sed 's|(|\\(|g;s|)|\\)|g')" |
|
if [ -z $new_emblems ]; then |
|
# remove all the emblems |
|
gvfs-set-attribute $URI -t stringv metadata::emblems '' |
|
else |
|
gvfs-set-attribute $URI -t stringv metadata::emblems $new_emblems |
|
fi |
|
;; |
|
2) |
|
$0 add "$URI" |
|
;; |
|
# 1 = [Cancel], 252 = [Esc] |
|
esac |
|
} |
|
|
|
|
|
# Add arbitrary icon to the emblems |
|
|
|
use_add() { |
|
add_emblem=$(yad \ |
|
--mouse --width=300 --height=100 \ |
|
--window-icon=emblem-favorite \ |
|
--title="Add emblem" \ |
|
--image=emblem-favorite --image-on-top \ |
|
--text="Enter icon name you want to add to emblems. |
|
Press the find icon to browse icons installed on your system, then close the icon browser and enter icon name manually." \ |
|
--button='gtk-cancel:1' \ |
|
--button='gtk-add:0' \ |
|
--entry \ |
|
--ricon='gtk-find' \ |
|
--ricon-action='yad-icon-browser &' \ |
|
) |
|
|
|
code="$?" |
|
case $code in |
|
0) |
|
# Set the emblems |
|
if [ -z $EMBLEMS -a -z $add_emblem ]; then |
|
# remove all the emblems |
|
gvfs-set-attribute $URI -t stringv metadata::emblems '' |
|
else |
|
gvfs-set-attribute $URI -t stringv metadata::emblems $EMBLEMS $add_emblem |
|
fi |
|
;; |
|
# 1 = [Cancel], 252 = [Esc] |
|
esac |
|
} |
|
|
|
|
|
case $mode in |
|
list) use_list;; |
|
add) use_add;; |
|
esac |
|
|
|
|
|
exit |