-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwelcome-dnd
executable file
·174 lines (143 loc) · 4.85 KB
/
welcome-dnd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# A config file helper for Welcome's Personal Commands.
# User can drag&drop e.g. .desktop or document files onto this window.
#
# Assumes config file's welcome_own_commands variable has
# the ending parenthesis ')' as the FIRST and ONLY character on a line.
DndDIE() {
echo "Error: $1" >&2
exit 1
}
source /usr/share/endeavouros/scripts/eos-script-lib-yad || DndDIE "source failed."
export -f eos_yad
DndCreateConfigFile() {
if [ ! -r "$configfile" ] ; then
cat <<EOF > $configfile
#!/bin/bash
local welcome_own_commands=(
# --field=" Pamac!system-software-install!GUI package manager":fbtn 'pamac-manager'
)
# Note: leave the ')' character above as the first and only character on that line!
EOF
fi
}
DndAddField() {
local configfile="$HOME/.config/welcome-own-cmds.conf"
local mode=""
# Check definitions (old or new). If you have both old and new definitions, then assume new.
if [ -r "$configfile" ] ; then
/usr/bin/grep "^[ \t]*local[ \t][ \t]*welcome_own_commands" "$configfile" && mode=old
/usr/bin/grep "^[ \t]*personal_commands_init" "$configfile" && mode=new
/usr/bin/grep "^[ \t]*personal_commands_add" "$configfile" && mode=new
fi
case "$mode" in
old)
local field=" --field=' $name!$icon!$description':fbtn '$command'"
# make sure the config file exists in a proper format
DndCreateConfigFile
# add a field line in the end of variable welcome_own_commands
sed -i "$configfile" -e "/^)$/i\ $field"
;;
*)
# new or empty
cat <<EOF >> "$configfile"
personal_commands_add "$command" \\
"$name" \\
"$icon" \\
"$description"
EOF
;;
esac
}
DndDesktopFile() {
local file="${arg#file://}"
local icon="$(grep "^Icon=" "$file" | head -n 1 | cut -d '=' -f 2)"
local name="$(grep "^Name=" "$file" | head -n 1 | cut -d '=' -f 2)"
local generic_name="$(grep "^GenericName=" "$file" | head -n 1 | cut -d '=' -f 2)"
local comment="$(grep "^Comment=" "$file" | head -n 1 | cut -d '=' -f 2)"
local description="Description not available."
local command="xdg-open '$file'"
if [ -n "$generic_name" ] ; then
description="$generic_name"
elif [ -n "$comment" ] ; then
description="$comment"
fi
DndAddField
}
DndDocumentFile() {
local file="${arg#file://}"
local desktopfile="$(xdg-mime query default $(xdg-mime query filetype "$file"))"
local dt2="$(find /usr/share/applications -name "$desktopfile")"
test -n "$dt2" || dt2="$(find "$HOME/.local/share/applications" -name "$desktopfile")"
case "$file" in
/*) ;;
*) file="$PWD/$file" ;;
esac
local ending="${file##*.}"
local name="$(basename "$file")"
local description="A $ending file"
local command="xdg-open '$file'"
local icon="$(grep "^Icon=" "$dt2" | head -n 1 | cut -d '=' -f 2)"
DndAddField
}
DndUrl() {
local file="${arg}"
local name="$file"
local description="A URL"
local command="xdg-open '$file'"
local icon="web-browser"
#local desktopfile="$(xdg-mime query default $(xdg-mime query filetype $file))"
DndAddField
}
DndServerStart() {
local handler="command=/usr/bin/$progname"
if [ -n "$(ps -ef | grep "$handler" | grep -v "grep $handler")" ] ; then
echo "$progname server is already running!" >&2
return
fi
local text="Drop item(s) into this window:\n"
text+="- URL shortcut from the address line of a web browser\n"
text+="- document and file shortcuts from a file manager\n"
text+="- .desktop file (=launcher) shortcut from the start menu"
local yadcmd=(eos_yad --dnd
--$handler
--title="Welcome: Personal Commands drag & drop support"
--text="$text"
--width=500 --height=200
# --posx=5 --posy=5
)
"${yadcmd[@]}" &
}
RestartWelcomeAndDndServer() {
local progname2=eos-welcome
local restart_welcome=no
pkill -f "$progname2" && restart_welcome=yes
pkill -f "/usr/bin/yad --window-icon=$EOS_WICON"
sleep 0.5
if [ "$restart_welcome" = "yes" ] ; then
$progname2 &
sleep 1
fi
DndServerStart
}
DndMain()
{
local progname=welcome-dnd
if [ "$1" = "" ] ; then
DndServerStart
return
fi
local arg
for arg in "$@" ; do
echo "Dropping '$arg'." >&2
case "$arg" in
https://* | http://*) DndUrl ;;
*.desktop) DndDesktopFile ;;
/* | file://*) DndDocumentFile ;;
*) DndDIE "'$arg' not supported." ;;
esac
sleep 0.5
done
RestartWelcomeAndDndServer
}
DndMain "$@"