Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

progressbar.sh 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #!/bin/sh
  2. #
  3. # Show a progress bar.
  4. #
  5. # If kdialog/zenity are available they are used.
  6. # Progress data is read from the created pipe, echoing "quit" to the pipe will
  7. # cause the script to terminate, as will the progress reaching 100%
  8. #
  9. # Pressing cancel on a zenity dialog will terminate right away, kdialog will
  10. # only terminate when the next line of data is read from the pipe.
  11. #
  12. # Check the which command exists, and if so make sure it behaves how we want
  13. # it to...
  14. WHICH=`which which 2>/dev/null`
  15. if [ "" = "${WHICH}" ]; then
  16. echo "which command not found. Aborting.";
  17. exit 0;
  18. else
  19. # Solaris sucks
  20. BADWHICH=`which /`
  21. if [ "${BADWHICH}" != "" ]; then
  22. echo "Replacing bad which command.";
  23. # "Which" on solaris gives non-empty results for commands that don't exist
  24. which() {
  25. OUT=`${WHICH} ${1}`
  26. if [ $? -eq 0 ]; then
  27. echo ${OUT}
  28. else
  29. echo ""
  30. fi;
  31. }
  32. fi;
  33. fi
  34. # Solaris also has a crappy version of tail!
  35. tail -n +1 /dev/null >/dev/null 2>&1
  36. if [ $? -eq 2 ]; then
  37. TAILOPTS="-"
  38. else
  39. TAILOPTS="-n"
  40. fi;
  41. PIDOF=`which pidof`
  42. if [ "${PIDOF}" = "" ]; then
  43. # For some reason some distros hide pidof...
  44. if [ -e /sbin/pidof ]; then
  45. PIDOF=/sbin/pidof
  46. elif [ -e /usr/sbin/pidof ]; then
  47. PIDOF=/usr/sbin/pidof
  48. fi;
  49. fi;
  50. ## Helper Functions
  51. if [ -n "${PIDOF}" ]; then
  52. ISKDE=`${PIDOF} -x -s kdeinit kdeinit4`
  53. ISGNOME=`${PIDOF} -x -s gnome-panel`
  54. else
  55. ISKDE=`pgrep kdeinit`
  56. ISGNOME=`pgrep gnome-panel`
  57. fi;
  58. KDIALOG=`which kdialog`
  59. ZENITY=`which zenity`
  60. USEKDIALOG="0";
  61. QDBUS=`which qdbus`
  62. DBUSSEND=`which dbus-send`
  63. DCOP=`which dcop`
  64. BC=`which bc`
  65. KDETYPE=""
  66. if [ "${ISKDE}" != "" -o "${ZENITY}" = "" ]; then
  67. # Check to see if we have the dcop or dbus binaries needed..
  68. USEDCOP=`kdialog --help | grep -i dcop`
  69. if [ "${USEDCOP}" != "" -a "${DCOP}" != "" ]; then
  70. KDETYPE="dcop"
  71. USEKDIALOG="1";
  72. elif [ "${USEDCOP}" = "" -a "${QDBUS}" != "" ]; then
  73. KDETYPE="qdbus"
  74. USEKDIALOG="1";
  75. elif [ "${USEDCOP}" = "" -a "${DBUSSEND}" != "" ]; then
  76. KDETYPE="dbussend"
  77. USEKDIALOG="1";
  78. fi;
  79. fi;
  80. # Check for special watchdog mode.
  81. # In this mode we are used to kill ourself after a certain time limit to make sure we don't hang.
  82. if [ "${1}" = "--watchdog" ]; then
  83. if [ "" != "${2}" ]; then
  84. echo "Watchdog for: ${2}"
  85. sleep 5;
  86. kill -1 ${2} >/dev/null 2>&1 &
  87. sleep 5;
  88. kill -9 ${2} >/dev/null 2>&1 &
  89. exit 0;
  90. else
  91. echo "No PID specified to watch."
  92. exit 1;
  93. fi;
  94. exit 1;
  95. fi;
  96. if [ "${1}" = "--pulsate" ]; then
  97. shift;
  98. PULSATE="1"
  99. else
  100. PULSATE="0"
  101. fi;
  102. CAPTION=${1}
  103. FILESIZE=${2}
  104. WGETPID=${4}
  105. progresswindow=""
  106. TYPE=""
  107. PIPE="progresspipe_${$}"
  108. retval="1"
  109. CONTINUE="1"
  110. readprogress() {
  111. data=""
  112. input=""
  113. while [ ${CONTINUE} -eq "1" -a -e ${PIPE} ]; do
  114. if [ "${TYPE}" = "KDE" ]; then
  115. wasCancelled="false"
  116. if [ "${KDETYPE}" = "dcop" ]; then
  117. wasCancelled=`${DCOP} ${progresswindow} wasCancelled`;
  118. elif [ "${KDETYPE}" = "qdbus" ]; then
  119. wasCancelled=`${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.wasCancelled`;
  120. elif [ "${KDETYPE}" = "dbussend" ]; then
  121. wasCancelled=`${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.wasCancelled | grep boolean | awk '{print $2}'`;
  122. fi
  123. if [ "${wasCancelled}" = "true" ]; then
  124. touch .downloadWasCancelled
  125. break;
  126. fi;
  127. fi;
  128. input=`cat "${PIPE}" | tail ${TAILOPTS}1`
  129. if [ "${input}" = "quit" ]; then
  130. break;
  131. elif [ "${input}" != "" ]; then
  132. data=${input}
  133. input=""
  134. if [ "${PULSATE}" = "1" ]; then
  135. val=-1
  136. if [ "${data}" != "-1" ]; then
  137. if [ "${TYPE}" = "KDE" ]; then
  138. if [ "${KDETYPE}" = "dcop" ]; then
  139. ${DCOP} ${progresswindow} setLabelText "${data}"
  140. elif [ "${KDETYPE}" = "qdbus" ]; then
  141. ${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.setLabelText "${data}"
  142. elif [ "${KDETYPE}" = "dbussend" ]; then
  143. ${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.setLabelText "string:\'${data}\'" > /dev/null
  144. fi;
  145. elif [ "${TYPE}" = "GNOME" ]; then
  146. echo "${data}" | sed 's/^/# /g'
  147. else
  148. echo "${data}"
  149. fi;
  150. fi;
  151. else
  152. if [ "${BC}" != "" ]; then
  153. res=`echo "scale=4 ; (${data}/${FILESIZE})*100" | bc`;
  154. else
  155. # Note, this will return 0 or 100, nothing else, better than spamming
  156. # errors and never closing tho.
  157. res=`echo $(( (${data}/${FILESIZE})*100 ))`;
  158. fi;
  159. val=${res%%.*}
  160. if [ "${val}" = "" ]; then
  161. val=0
  162. fi;
  163. fi;
  164. if [ "${TYPE}" = "KDE" ]; then
  165. if [ "${KDETYPE}" = "dcop" ]; then
  166. ${DCOP} ${progresswindow} setProgress ${val}
  167. returnVal=${?}
  168. elif [ "${KDETYPE}" = "qdbus" ]; then
  169. ${QDBUS} ${progresswindow} org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog value ${val}
  170. returnVal=${?}
  171. elif [ "${KDETYPE}" = "dbussend" ]; then
  172. ${DBUSSEND} --print-reply --dest=${progresswindow} org.freedesktop.DBus.Properties.Set string:'org.kde.kdialog.ProgressDialog' string:'value' variant:int32:${val} > /dev/null
  173. returnVal=${?}
  174. fi;
  175. if [ ${returnVal} -ne 0 ]; then
  176. break;
  177. fi;
  178. elif [ "${TYPE}" = "GNOME" ]; then
  179. echo ${val}
  180. if [ $? -ne 0 ] ; then
  181. break;
  182. fi
  183. else
  184. if [ $((${val} % 2)) -eq 0 ]; then
  185. echo "-> "${val}"%"
  186. fi;
  187. fi;
  188. if [ "${val}" = "100" ]; then
  189. retval="0"
  190. CONTINUE="0"
  191. break;
  192. fi;
  193. fi;
  194. done;
  195. }
  196. if [ "" = "${CAPTION}" -o "" = "${FILESIZE}" ]; then
  197. echo "Insufficient Parameters."
  198. echo "Usage: ${0} <caption> <totalvalue> [pipename]"
  199. exit;
  200. fi;
  201. if [ "" != "${3}" ]; then
  202. # Assume pipe name is what we want, delete existing file.
  203. PIPE=${3}
  204. rm -Rf ${PIPE}
  205. else
  206. # Make sure we get a file that doesn't already exist, keep appending our
  207. # pid untill we get a file that hasn't been taken.
  208. while [ -e "${PIPE}" ]; do
  209. PIPE="${PIPE}_${$}"
  210. done;
  211. fi;
  212. echo "Using pipe: "${PIPE}
  213. mkfifo "${PIPE}"
  214. EMPTYPIPE="1"
  215. WDPID=""
  216. WD2PID=""
  217. closeProgress() {
  218. CONTINUE="0"
  219. if [ "${TYPE}" = "KDE" -a ${retval} != "0" ]; then
  220. if [ "${KDETYPE}" = "dcop" ]; then
  221. ${DCOP} ${progresswindow} close
  222. elif [ "${KDETYPE}" = "qdbus" ]; then
  223. ${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.close
  224. elif [ "${KDETYPE}" = "dbussend" ]; then
  225. ${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.close > /dev/null
  226. fi;
  227. fi;
  228. echo "Exiting with value: $retval"
  229. if [ "${WGETPID}" != "" ]; then
  230. echo "Attempting to kill wget (${WGETPID})"
  231. kill -9 ${WGETPID}
  232. fi;
  233. if [ -e ${PIPE} ]; then
  234. if [ "${EMPTYPIPE}" = "1" ]; then
  235. sh ${0} --watchdog ${$} &
  236. WDPID=${!}
  237. EMPTYPIPE="2"
  238. echo "Emptying pipe (${PIPE}) this may hang if no data has been written to the pipe."
  239. cat ${PIPE};
  240. elif [ "${EMPTYPIPE}" = "2" ]; then
  241. echo "Helping pipe (${PIPE}) this may hang if nothing is reading from the pipe."
  242. sh ${0} --watchdog ${$} &
  243. WD2PID=${!}
  244. EMPTYPIPE="3"
  245. echo "." > ${PIPE};
  246. else
  247. echo "Ignoring Pipe..."
  248. fi;
  249. echo "Killing watchdog 1."
  250. kill -9 ${WDPID}
  251. echo "Killing watchdog 2."
  252. kill -9 ${WD2PID}
  253. echo "Deleting Pipe ${PIPE}"
  254. rm -Rf "${PIPE}"
  255. fi;
  256. exit $retval;
  257. }
  258. trap 'closeProgress' INT TERM EXIT
  259. rm .downloadWasCancelled
  260. # if kdialog exists, and we have a display, and we are not running gnome,
  261. # and either we are running kde or zenity doesn't exist..
  262. if [ "" != "${KDIALOG}" -a "" != "${DISPLAY}" -a "" = "${ISGNOME}" -a "${USEKDIALOG}" = "1" ]; then
  263. echo "Progress dialog on Display: ${DISPLAY}"
  264. progresswindow=`${KDIALOG} --title "DMDirc: ${CAPTION}" --progressbar "${CAPTION}" 100`
  265. if [ "${PULSATE}" = "1" ]; then
  266. SHOWCANCEL="false"
  267. else
  268. SHOWCANCEL="true"
  269. fi;
  270. if [ "${KDETYPE}" = "dcop" ]; then
  271. ${DCOP} ${progresswindow} setAutoClose true
  272. ${DCOP} ${progresswindow} showCancelButton ${SHOWCANCEL}
  273. elif [ "${KDETYPE}" = "qdbus" ]; then
  274. ${QDBUS} ${progresswindow} org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog autoClose true
  275. ${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.showCancelButton ${SHOWCANCEL}
  276. elif [ "${KDETYPE}" = "dbussend" ]; then
  277. ${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.showCancelButton boolean:${SHOWCANCEL} >/dev/null
  278. ${DBUSSEND} --print-reply --dest=${progresswindow} org.freedesktop.DBus.Properties.Set string:'org.kde.kdialog.ProgressDialog' string:'autoClose' variant:boolean:true > /dev/null
  279. fi;
  280. TYPE="KDE"
  281. readprogress
  282. CONTINUE="0"
  283. echo "Progress Bar Complete"
  284. elif [ "" != "${ZENITY}" -a "" != "${DISPLAY}" ]; then
  285. # Else, if zenity exists and we have a display
  286. echo "Progress dialog on Display: ${DISPLAY}"
  287. TYPE="GNOME"
  288. if [ "${PULSATE}" = "1" ]; then
  289. readprogress | ${ZENITY} --progress --pulsate --auto-close --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  290. else
  291. readprogress | ${ZENITY} --progress --auto-close --auto-kill --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  292. fi;
  293. CONTINUE="0"
  294. echo "Progress Bar Complete"
  295. else
  296. # Else, basic command-line progress
  297. echo "Progress For: ${CAPTION}"
  298. echo "-> 0%"
  299. readprogress
  300. CONTINUE="0"
  301. echo ""
  302. echo "Finished!"
  303. fi
  304. echo "Exiting progressbar"
  305. exit 0;