-
Notifications
You must be signed in to change notification settings - Fork 10
/
yadFunctions.sh
308 lines (283 loc) · 13 KB
/
yadFunctions.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
##########################################################################
# This script contains menu functions used only by main script on
# Desktop Mode. The application to manage windows is Yad.
# @author César Rodríguez González
# @since 1.3, 2016-11-14
# @version 1.3.3, 2017-03-23
# @license MIT
##########################################################################
###
# This function get the height of a Yad window
# @since v1.3.2
# @param int rowsNumber Number of rows (categories or applications)
# @return int Height for current Yad window
##
function getHeight
{
local rowsNumber=$1
local baseHeight=352
height=$(($baseHeight+$(($(($rowsNumber))*$rowHeight))))
if [ $height -gt $maxHeight ]; then
height=$maxHeight
fi
echo $height
}
##
# This function returns the selected application list of a specific category
# @param String categoryName Name of the categoryName
# @since 1.3.2
# @return String List of selected applications of the category
##
function getSelectedAppsFromCategory
{
categoryName="$1"
if [ -f "$tempFolder/yad$categoryName" ]; then
local selection=`cat "$tempFolder/yad$categoryName" | awk -F '|' '{print $2}' | tr '\n' '|'`
if [ -n "$selection" ]; then
IFS='|' read -ra selectionArray <<< "$selection"
local selectedApps
if [ "${selectionArray[0]}" == "[$all]" ]; then
echo "$( getApplicationList "$categoryName" )"
else
echo "`echo "${selection// /_}" | tr '|' ' '`"
fi
else
echo ""
fi
else
echo ""
fi
}
##
# This function gets option parameters of each row of Yad window.
# One row per application of a specified category
# @since v1.3.2
# @param String[] applicationArray List of category applications
# @param String categoryName Name of the actual category
# @return String Parameters of application selectable in box
##
function getApplicationOptions
{
local applicationArray=(${!1}) categoryName="$2"
local selectedApps=$( getSelectedAppsFromCategory $categoryName )
local appName appDescription appObservation options="" appNameForMenu enabled
for appName in "${applicationArray[@]}"; do
# application name without '_' character as showed in window
appNameForMenu="`echo $appName | tr '_' ' '`"
eval appDescription=\$$appName"Description"
eval appObservation=\$$appName"Observation"
local isSelected="`echo $selectedApps | grep -w "$appName"`"
if [ -z "$isSelected" ]; then enabled="FALSE"; else enabled="TRUE"; fi
options+="$enabled \"$appNameForMenu\" \"<i>$appDescription</i>\" \"<i>$appObservation</i>\" "
done
echo "$options"
}
##
# This function gets a application list window of a specified category
# First row: all applications. Rest of rows: one per application.
# @since v1.3.2
# @param String[] applicationArray list of applications
# @param String categoryName name of category
# @param int key random number used to match tabs in window
# @param int categoryNumber index of category order
# @return String commands to create app. window
##
function getApplicationsWindow
{
local applicationArray=(${!1}) categoryName="$2" key=$3 categoryNumber=$4
local formattedText="<span font='$fontFamilyText $fontSmallSize'>$selectAppsToInstallCurrentCategory</span>"
# Set first row: ALL applications
local appRows="false \"[$all]\" \"\" \"\" "
# Set rest of rows. One per aplication
appRows+=$( getApplicationOptions applicationArray[@] "$categoryName" )
# Create yad window (desktop mode)
window="yad --plug=$key --tabnum=$categoryNumber --text \"$formattedText\" --list --checklist --column \"\" --column \"$nameLabel\" --column \"$descriptionLabel\" --column \"$observationLabel\" $appRows"
echo "$window"
}
##
# This function filters applicaction list of a specific category.
# If uninstall process, then, filter thouse apps of a category that
# have been installed.
# @since v1.3.2
# @param String categoryName Name of the category
# @param String uninstalled If uninstall proccess
# @return String List of apps
##
function getApplicationList
{
local categoryName="$1"
# Delete blank and comment lines,then filter by category name and take application list (second column)
local applicationList=(`cat "$appListFile" | awk -F ',' -v category=$categoryName '!/^($|#|,)/{ if ($1 == category) print $2; }'`)
if [ "$uninstaller" == "true" ]; then
local installedAppList=""
for application in "${applicationList[@]}"; do
# Delete blank and comment lines,then filter by application name and take package list (third column)
local packageList="`cat "$appListFile" | awk -F ',' -v app=$application '!/^($|#|,)/{ if ($2 == app) print $3; }' | tr '|' ' '`"
if [ -n "`dpkg -s $packageList 2>&1 | grep "Status: install ok installed"`" ]; then
# The application is installed
installedAppList+="$application "
fi
done
echo "$installedAppList"
else
echo "${applicationList[@]}"
fi
}
##
# This functions creates a summary window of selected applications to be installed
# @param String[] categoryArray List of categories
# @since 1.3.2
# @return String Summary window
##
function getSummaryWindow
{
local categoryArray=(${!1})
local totalCategoriesNumber=$((${#categoryArray[@]}+1))
local rows height=$( getHeight $totalCategoriesNumber)
local formattedText+="<span font='$fontFamilyText $fontBigSize'>$appsToBeInstalled</span>"
local categoryName selectionArray selectedApplications
for categoryName in "${categoryArray[@]}"; do
if [ -f "$tempFolder/yad$categoryName" ]; then
local selection=`cat "$tempFolder/yad$categoryName" | awk -F '|' '{print $2}' | tr '\n' '|'`
if [ -n "$selection" ]; then
IFS='|' read -ra selectionArray <<< "$selection"
local selectedAppsFromCategory=$( getSelectedAppsFromCategory $categoryName )
eval categoryDescription=\$$categoryName"Description"
local appNameForMenu appDescription
for appName in $selectedAppsFromCategory; do
appNameForMenu="`echo $appName | tr '_' ' '`"
eval appDescription=\$$appName"Description"
local credentials="" appUsername="" appPassword=""
if [ -f "$scriptRootFolder/etc/credentials/$appName.properties" ]; then
. $scriptRootFolder/etc/credentials/$appName.properties
if [ -n "$appUsername" ] || [ -n "$appPassword" ]; then
credentials="$appUsername / $appPassword"
fi
fi
rows+="\"$categoryDescription\" \"$appNameForMenu\" \"$appDescription\" \"$credentials\" "
done
selectedApplications+="$selectedAppsFromCategory "
fi
fi
done
echo "$selectedApplications" > $tempFolder/selectedAppsFile
local customIcon
if [ "$uninstaller" == "true" ]; then customIcon="trash32.png"; else customIcon="installing32.png"; fi
echo "yad --title=\"$installerTitle\" --text \"$formattedText\" --list --width=$width --height=500 --column \"$categoryLabel\" --column \"$nameLabel\" --column \"$descriptionLabel\" --column \"$credentialsLabel\" $rows --window-icon=\"$installerIconFolder/tux-shell-console32.png\" --button=\"!/$installerIconFolder/back32.png:3\" --button=\"!/$installerIconFolder/credentials32.png:2\" --button=\"!/$installerIconFolder/door32.png:1\" --button=\"!/$installerIconFolder/$customIcon:0\" --image=\"$installerIconFolder/summary96.png\" --image-on-top --center"
}
##
# This function creates a credential window of thouse applications which
# require authentication to allow the modification of username / password
# @since 1.3.3
##
function getCredentialsWindow
{
local appNumber=1 key=$RANDOM
local formattedText="<span font='$fontFamilyText $fontSmallSize'>$credentialsTitle</span>"
local window="yad --notebook --key=$key --title=\"$installerTitle\" --text=\"$formattedText\""
window+=" --image=\"$installerIconFolder/login-credentials-yad.png\" --image-on-top"
window+=" --button=\"!/$installerIconFolder/cancel.png:1\" --button=\"!/$installerIconFolder/save32.png:0\""
window+=" --window-icon=\"$installerIconFolder/tux-shell-console32.png\""
# Array of applications which requires authentication
local credentialArray=(`ls $credentialFolder/`)
for credentialFile in "${credentialArray[@]}"; do
if [[ $credentialFile != template* ]] && [ -f $credentialFolder/$credentialFile ]; then
local appName="${credentialFile%.*}"
local appNameForMenu="`echo $appName | tr '_' ' '`"
. $credentialFolder/$appName.properties
local usernameStr="" passwordStr=""
if [ "$usernameCanBeEdited" == "false" ]; then usernameStr=":RO"; fi
if [ "$passwordCanBeEdited" == "false" ]; then passwordStr=":RO"; fi
yad --plug=$key --tabnum=$appNumber --form --field "$usernameLabel$usernameStr" "$appUsername" --field "$passwordLabel$passwordStr" "$appPassword" > "$tempFolder/yad$appName" &
window+=" --tab=\"$appNameForMenu\" --tab-pos=left"
appNumber=$(($appNumber+1))
fi
done
local height=$( getHeight ${#credentialArray[@]} )
window+=" --width=300 --heigh=$height --center"
echo "$window"
}
##
# This function save credentials of thouse applications which
# require authentication
# @since 1.3.3
##
function saveCredentials
{
# Array of applications which requires authentication
local credentialArray=(`ls $credentialFolder/`)
for credentialFile in "${credentialArray[@]}"; do
if [[ $credentialFile != template* ]] && [ -f $credentialFolder/$credentialFile ]; then
local appName="${credentialFile%.*}"
local selection=`cat "$tempFolder/yad$appName"`
IFS='|' read -ra authentication <<< "$selection"
local username="${authentication[0]}" password="${authentication[1]}"
# Save credentials to file
sed -i "s/^appUsername=.*/appUsername=$username/g" $credentialFolder/$credentialFile
sed -i "s/^appPassword=.*/appPassword=$password/g" $credentialFolder/$credentialFile
fi
done
}
##
# This function calls other functions to show category box and all others
# application boxes to let the user selects applications to install.
# @since v1.3.2
# @return String Selected app.list with '.' separator
##
function menu
{
# Array of categories from appListFile of your distro. Delete blank and comment lines. Take category list (first column) and remove duplicated rows in appListFile content.
local categoryArray=(`cat "$appListFile" | awk -F ',' '!/^($|#|,)/{ print $1; }' | uniq | sort`)
local formattedText="<span font='$fontFamilyText $fontBigSize'>$installerTitle</span>"
formattedText+="\n\n<span font='$fontFamilyText $fontSmallSize'>$scriptDescription</span>"
local exitMainWindow="false"
while [ "$exitMainWindow" == "false" ]; do
pkill yad*
local categoryNumber=1 key=$RANDOM maxApplicationNumber=0 categoryDescription applicationArray totalApplicationNumber
local window="yad --notebook --key=$key --title=\"$installerTitle\" --text=\"$formattedText\""
window+=" --image=\"$installerIconFolder/yad-tux-shell-console96.png\" --image-on-top"
window+=" --button=\"!/$installerIconFolder/facebook32.png:4\" --button=\"!/$installerIconFolder/www32.png:3\" --button=\"!/$installerIconFolder/octocat32.png:2\" --button=\"!/$installerIconFolder/door32.png:1\" --button=\"!/$installerIconFolder/next32.png:0\""
window+=" --window-icon=\"$installerIconFolder/tux-shell-console32.png\""
for categoryName in "${categoryArray[@]}"; do
eval categoryDescription=\$$categoryName"Description"
# Applications for the category
applicationArray=( $( getApplicationList "$categoryName" ) )
totalApplicationNumber=$((${#applicationArray[@]}+1))
if [ $totalApplicationNumber -gt $maxApplicationNumber ]; then
maxApplicationNumber=$totalApplicationNumber
fi
eval $( getApplicationsWindow applicationArray[@] "$categoryName" $key $categoryNumber ) > "$tempFolder/yad$categoryName" &
window+=" --tab=\"<span font='$fontFamilyText $fontSmallSize'><b>$categoryDescription</b></span>\" --tab-pos=left"
categoryNumber=$(($categoryNumber+1))
done
local height=$( getHeight $maxApplicationNumber )
window+=" --width=$width --height=$height --center"
eval "$window"
local resultCode=$?
pkill yad*
case $resultCode in
0) local exitSummaryWindow="false"
while [ "$exitSummaryWindow" == "false" ]; do
local summaryWindow=$( getSummaryWindow categoryArray[@] )
eval "$summaryWindow"
case $? in
0) exitSummaryWindow="true"; exitMainWindow="true" ;;
1) exitSummaryWindow="true"; exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
2) eval $( getCredentialsWindow )
if [ $? -eq 0 ]; then
saveCredentials
fi
exitSummaryWindow="false" ;;
3) exitSummaryWindow="true"; exitMainWindow="false" ;;
*) exitSummaryWindow="true"; exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
esac
done ;;
1) exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
2) xdg-open 'https://github.com/cesar-rgon/desktop-app-installer'; exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
3) xdg-open 'https://cesar-rgon.github.io/desktop-app-installer-website'; exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
4) xdg-open 'https://www.facebook.com/desktopAppInstaller'; exitMainWindow="true"; rm -f "$tempFolder/selectedAppsFile" ;;
*) exitMainWindow="true"; echo "" ;;
esac
done
}