# Desktop icons for Fvwm-Crystal. Support any file manager.
#
# Version:	2.1.0
# Licence:	GPL 2
#
# Written by Dominique Michel <dominique_libre@users.sourceforge.net>, 2010-2013
# for Fvwm-Crystal.
#
# From Fvwm FAQ, 7.13 with huge modifications and added functions.
# This file will add nice 2 pictures icons (a standard feature on the Amiga OS) on the desktop:
# one mandatory for $HOME, the other are optional and are divided into 2 ranges:
# the XDG user directories and the mounted partitions.
# A left click will launch Thunar into the corresponding mount point.
# A middle click will do the same with mc (Thunar doesn't heve a built-in shell).
# A right click on the $HOME icon open the preferences.
#
# Requirement: gawk, sort, Thunar, mc, mount, and /proc/mounts.
# Recommanded: pmount and pmount-gui

# Preferences {{{1
LoadPreferences ShowXDG
LoadPreferences ShowPartitions
LoadPreferences FileBrowser1
LoadPreferences FileBrowser2

# For the recipes
SetEnv DefaultDesktopManager Crystal
AddToFunc ExitFunction I UnsetEnv DefaultDesktopManager

# We must calculate the size of the button box {{{1
# Home: 1 icon, XDG user dirs: 8 icons, partitions: variable
# The icons number {{{2
DestroyFunc GetPartitions
AddToFunc GetPartitions
+ I PipeRead "gawk \'/^\\/.*\\// { print $$$2; }\' </proc/mounts |sort > /tmp/Icon-Drives; \
	echo SetEnv IconNumber $(($[IconNumber]+`wc -l < /tmp/Icon-Drives`))"

DestroyFunc GetIconsNumber
AddToFunc GetIconsNumber
+ I SetEnv IconNumber 1
+ I Test (EnvMatch ShowXDG yes) SetEnv IconNumber 9
+ I Test (EnvMatch ShowPartitions yes) GetPartitions
# The number of row and columns {{{2
+ I PipeRead 'echo "SetEnv IconRowsNumber $(((($[vp.height]-92))/60))"'
+ I PipeRead 'tmpvar=$(($[IconNumber]/$[IconRowsNumber])); if [ $(($[IconRowsNumber]*${tmpvar})) = $[IconNumber] ]; then echo "SetEnv IconColumnsNumber ${tmpvar}"; else echo "SetEnv IconColumnsNumber $((${tmpvar}+1))"; fi'
# IconBox {{{1
#+ I PipeRead 'echo "Style * IconBox $(($[vp.width]-((((50*$[IconColumnsNumber]))+$[infostore.DeskIconsXSup))))x$(($[vp.height]-92))+$((36+((50*$[IconColumnsNumber]))))+46"'

# umount/pumount wrapper
DestroyFunc DesktopUmount
AddToFunc DesktopUmount
+ I PipeRead 'if [ "`echo $* | grep -e media`" = "" ]; \
      then exem="umount"; \
      else exem="pumount"; \
      fi ; \
      echo "+ \\\"Unmount $*\\\" Exec exec ${exem} $*"'

# The button {{{1
DestroyFunc ShowDesktopIcons
AddToFunc ShowDesktopIcons
+ I Exec exec killall -9 DesktopCheckMounts $[infostore.SILENT]
+ I GetIconsNumber
+ I KillModule FvwmButtons DesktopIcons
+ I DestroyModuleConfig DesktopIcons: *
+ I *DesktopIcons: Geometry $[infostore.DeskIconsSign]+18+46
+ I *DesktopIcons: Padding 1 1
+ I *DesktopIcons: BoxSize Fixed
+ I *DesktopIcons: Rows $[IconRowsNumber]
+ I *DesktopIcons: Columns $[IconColumnsNumber]
+ I *DesktopIcons: Font "xft:$[panel_font]:pixelsize=$[panel_font_size]:$[panel_font_style]" 
+ I *DesktopIcons: Colorset $[infostore.cs_transparent]
# Home
+ I *DesktopIcons: (1x1+0+0, Padding 1 6, Frame 0, Icon 48x48/categories/Home.png, ActiveIcon 48x48/categories/Home.png.active.png, Action (Mouse 1) "$[infostore.FileBrowser1] ${HOME} $[infostore.SILENT]", Action (Mouse 2) "$[infostore.FileBrowser2] /home/`id -un` $[infostore.SILENT]", Action (Mouse 3) "Menu /Preferences/IconLauncher")
# XDG user dirs
+ I Test (EnvMatch ShowXDG yes) PipeRead 'xdgicons=""; for name in DESKTOP DOWNLOAD TEMPLATES PUBLICSHARE DOCUMENTS MUSIC PICTURES VIDEOS; \
	do eval dir=\\\$\$(echo XDG_"${name}"_DIR); \
           echo "DestroyMenu /XDG${name}"; \
	   echo "AddToMenu /XDG${name}"; \
	   echo "+ ${dir} Nop"; \
	   echo "*DesktopIcons: (1x1, Padding 1 1, Frame 0, Icon 48x48/categories/${name}.png, ActiveIcon 48x48/categories/${name}.png.active.png, PressIcon 48x48/categories/${name}.png.active.png, Action (Mouse 1) $[infostore.FileBrowser1] ${dir} $[infostore.SILENT], Action(Mouse 2) $[infostore.FileBrowser2] ${dir} $[infostore.SILENT], Action (Mouse 3) Menu /XDG${name})"; \
	   xdgicons="${xdgicons} ${dir}"; \
	   y=++y; \
	done ; \
	echo "InfoStoreAdd XDGIcons \\\"${xdgicons}\\\""'
# partitions
+ I Test (EnvMatch ShowPartitions yes)  PipeRead 'partitions=""; while read line; do \
      echo "DestroyMenu /Partitions${line}"; \
      echo "AddToMenu /Partitions${line}"; \
      echo "+ ${line} Nop"; \
      echo "DesktopUmount ${line}"; \
      if [ "${line}" = "/" ]; \
      then icon="48x48/categories/Root.png"; \
      else if [ "$(echo ${line} | grep -e cdrom -e dvd)" = "" ]; \
      	   then icon="48x48/categories/Work_4.png"; \
	   else icon="48x48/categories/CD.png"; \
	   fi ; \
      fi ; \
      echo "*DesktopIcons: (1x1, Padding 1 1, Frame 0, Icon ${icon}, ActiveIcon ${icon}.active.png, PressIcon ${icon}.active.png, Action (Mouse 1) $[infostore.FileBrowser1] ${line} $[infostore.SILENT], Action (Mouse 2) $[infostore.FileBrowser2] ${line} $[infostore.SILENT], Action (Mouse 3) Menu /Partitions${line})"; \
      partitions="${partitions} ${line}"; \
      done </tmp/Icon-Drives; \
      echo "InfoStoreAdd PartitionModules \\\"${partitions}\\\""'
# We don't want a PipeRead that will block FVWM every 5 seconds
+ I Test (EnvMatch ShowPartitions yes) Exec exec $[FVWM_SYSTEMDIR]/scripts/DesktopCheckMounts 5
+ I Style DesktopIcons !Borders
+ I Module FvwmButtons DesktopIcons

ShowDesktopIcons

# Functions {{{1
# Preferences menu {{{2
DestroyMenu /Preferences/IconLauncher
AddToMenu /Preferences/IconLauncher
+ DynamicPopupAction Function IconLauncher-generator /Preferences/IconLauncher

DestroyFunc FindUMPartitions
AddToFunc FindUMPartitions
+ I PipeRead "cat /etc/fstab|grep user|grep -v \\\#| awk \'{print $$2}\'|while read line; do  partition=${line}; exist=0; while read lines; do if [ ${partition} = ${lines} ]; then exist=1; fi; done </tmp/Icon-Drives; if [ ${exist} = 0 ]; then echo AddToMenu $0 \\\"Mount ${line}\\\" Exec exec mount ${line}; fi; done"

DestroyFunc IconLauncher-generator
AddToFunc IconLauncher-generator
+ I DestroyMenu recreate $0
+ I AddToMenu $0
+ I Test (x pmount-gui) AddToMenu $0 'PMount' Exec exec pmount-gui
+ I Test (x pmount-gui) AddToMenu $0 'PUmount' Exec exec pmount-gui -u
+ I PipeRead "${FVWM_SYSTEMDIR}/scripts/findumpartitions $0"
+ I AddToMenu $0 '$[gt.XDG user directories]' PopUp /Preferences/XDG
+ I AddToMenu $0 '$[gt.Mounted partitions]' PopUp /Preferences/Partitions
+ I AddToMenu $0 '$[gt.Actions]' PopUp /Preferences/FileBrowser

DestroyMenu /Preferences/XDG
AddToMenu /Preferences/XDG
+ '%22x22/fvwm-crystal/On.png%$[gt.On]' ShowXDG
+ '%22x22/fvwm-crystal/Off.png%$[gt.Off]' DontShowXDG

DestroyFunc ShowXDG
AddToFunc ShowXDG
+ I SavePreferences ShowXDG "SetEnv ShowXDG yes"
+ I SetEnv ShowXDG yes
+ I ShowDesktopIcons
+ I RedrawIconBox

DestroyFunc DontShowXDG
AddToFunc DontShowXDG
+ I SavePreferences ShowXDG "SetEnv ShowXDG no"
+ I SetEnv ShowXDG no
+ I ShowDesktopIcons
+ I RedrawIconBox

DestroyMenu /Preferences/Partitions
AddToMenu /Preferences/Partitions
+ '%22x22/fvwm-crystal/On.png%$[gt.On]' ShowPartitions
+ '%22x22/fvwm-crystal/Off.png%$[gt.Off]' DontShowPartitions

DestroyFunc ShowPartitions
AddToFunc ShowPartitions
+ I SavePreferences ShowPartitions "SetEnv ShowPartitions yes"
+ I SetEnv ShowPartitions yes
+ I ShowDesktopIcons
+ I RedrawIconBox

DestroyFunc DontShowPartitions
AddToFunc DontShowPartitions
+ I SavePreferences ShowPartitions "SetEnv ShowPartitions no"
+ I SetEnv ShowPartitions no
+ I ShowDesktopIcons
+ I RedrawIconBox

DestroyMenu /Preferences/FileBrowser
AddToMenu /Preferences/FileBrowser
+ MissingSubmenuFunction FileBrowser-generator
+ '%22x22/categories/directory.png%$[gt.Action ]1' Popup BrowserAction1
+ '%22x22/categories/directory.png%$[gt.Action ]2' Popup BrowserAction2
+ '%22x22/categories/directory.png%$[gt.Custom action ]1' Module FvwmForm FileBrowser-Action1-Dialog
+ '%22x22/categories/directory.png%$[gt.Custom action ]2' Module FvwmForm FileBrowser-Action2-Dialog

DestroyFunc FileBrowser-generator
AddToFunc FileBrowser-generator
+ I DestroyMenu recreate $0
+ I AddToMenu $0
+ I PipeRead 'index=`echo "$0" | sed -e "s:BrowserAction::"`; $[FVWM_SYSTEMDIR]/scripts/DesktopActions "$0" ${index}'

All (FileBrowser-Action1-Dialog) Close
DestroyModuleConfig FileBrowser-Action1-Dialog: *
*FileBrowser-Action1-Dialog: Title "$[gt.File browser action]"
*FileBrowser-Action1-Dialog: Text "$[gt.Type a command to bind to the first mouse button.]"
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Text "$[gt.A console command must be prefixed by A and space,]"
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Text "$[gt.use X and space otherwise.]"
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Input ActionName 40 "A mc"
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Line center
*FileBrowser-Action1-Dialog: Button quit " $[gt.Save] "
*FileBrowser-Action1-Dialog: Command SetFileBrowser 1 "$(ActionName)"
*FileBrowser-Action1-Dialog: Button quit " $[gt.Not now] " ^[
*FileBrowser-Action1-Dialog: Command Nop
*FileBrowser-Action1-Dialog: Line center

All (FileBrowser-Action1-Dialog) Close
DestroyModuleConfig FileBrowser-Action2-Dialog: *
*FileBrowser-Action2-Dialog: Title "$[gt.File browser action]"
*FileBrowser-Action2-Dialog: Text "$[gt.Type a command to bind to the second mouse button.]"
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Text "$[gt.A console command must be prefixed by A and space,]"
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Text "$[gt.use X and space otherwise.]"
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Input ActionName 40 "A mc"
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Line center
*FileBrowser-Action2-Dialog: Button quit " $[gt.Save] "
*FileBrowser-Action2-Dialog: Command SetFileBrowser 2 "$(ActionName)"
*FileBrowser-Action2-Dialog: Button quit " $[gt.Not now] " ^[
*FileBrowser-Action2-Dialog: Command Nop
*FileBrowser-Action2-Dialog: Line center
DestroyFunc SetFileBrowser
AddToFunc SetFileBrowser
+ I SavePreferences FileBrowser$[0] "InfoStoreAdd FileBrowser$[0] \'$[1]\'"
+ I InfoStoreAdd FileBrowser$[0] '$[1]'
+ I ShowDesktopIcons
+ I RedrawIconBox

AddToFunc ExitFunction I Exec exec killall -9 DesktopCheckMounts $[infostore.SILENT]

# vim:ft=fvwm
