You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

progressbar.sh 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. KDETYPE=""
  65. if [ "${ISKDE}" != "" -o "${ZENITY}" = "" ]; then
  66. # Check to see if we have the dcop or dbus binaries needed..
  67. USEDCOP=`kdialog --help | grep -i dcop`
  68. if [ "${USEDCOP}" != "" -a "${DCOP}" != "" ]; then
  69. KDETYPE="dcop"
  70. USEKDIALOG="1";
  71. else if [ "${USEDCOP}" = "" -a "${QDBUS}" != "" ]; then
  72. KDETYPE="qdbus"
  73. USEKDIALOG="1";
  74. else if [ "${USEDCOP}" = "" -a "${DBUSSEND}" != "" ]; then
  75. KDETYPE="dbussend"
  76. USEKDIALOG="1";
  77. fi;
  78. fi;
  79. CAPTION=${1}
  80. FILESIZE=${2}
  81. WGETPID=${3}
  82. progresswindow=""
  83. TYPE=""
  84. PIPE="progresspipe_${$}"
  85. retval="1"
  86. CONTINUE="1"
  87. readprogress() {
  88. data=""
  89. input=""
  90. while [ ${CONTINUE} -eq "1" -a -e ${PIPE} ]; do
  91. if [ "${TYPE}" = "KDE" ]; then
  92. wasCancelled="false"
  93. if [ "${KDETYPE}" = "dcop" ]; then
  94. wasCancelled=`${DCOP} ${progresswindow} wasCancelled`;
  95. elif [ "${KDETYPE}" = "qdbus" ]; then
  96. wasCancelled=` ${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.wasCancelled`;
  97. elif [ "${KDETYPE}" = "dbussend" ]; then
  98. wasCancelled=` ${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.wasCancelled | grep boolean | awk '{print $2}'`;
  99. fi
  100. if [ "${wasCancelled}" = "true" ]; then
  101. break;
  102. fi;
  103. fi;
  104. input=`cat "${PIPE}" | tail ${TAILOPTS}1`
  105. if [ "${input}" = "quit" ]; then
  106. break;
  107. elif [ "${input}" != "" ]; then
  108. data=${input}
  109. input=""
  110. res=`echo "scale=4 ; (${data}/${FILESIZE})*100" | bc`
  111. val=${res%%.*}
  112. if [ "${val}" = "" ]; then
  113. val=0
  114. fi;
  115. if [ "${TYPE}" = "KDE" ]; then
  116. if [ "${KDETYPE}" = "dcop" ]; then
  117. ${DCOP} ${progresswindow} setProgress ${val}
  118. returnVal=${?}
  119. elif [ "${KDETYPE}" = "qdbus" ]; then
  120. ${QDBUS} ${progresswindow} org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog value 20
  121. returnVal=${?}
  122. elif [ "${KDETYPE}" = "dbussend" ]; then
  123. ${DBUSSEND} --print-reply --dest=${progresswindow} org.freedesktop.DBus.Properties.Set string:'org.kde.kdialog.ProgressDialog' string:'value' variant:int32:${val} > /dev/null
  124. returnVal=${?}
  125. fi;
  126. if [ ${returnVal} -ne 0 ]; then
  127. break;
  128. fi;
  129. elif [ "${TYPE}" = "GNOME" ]; then
  130. echo ${val}
  131. if [ $? -ne 0 ] ; then
  132. break;
  133. fi
  134. else
  135. if [ $((${val} % 2)) -eq 0 ]; then
  136. echo "-> "${val}"%"
  137. fi;
  138. fi;
  139. if [ "${val}" = "100" ]; then
  140. retval="0"
  141. CONTINUE="0"
  142. break;
  143. fi;
  144. fi;
  145. done;
  146. }
  147. if [ "" = "${CAPTION}" -o "" = ${FILESIZE} ]; then
  148. echo "Insufficient Parameters."
  149. echo "Usage: ${0} <caption> <totalvalue> [pipename]"
  150. exit;
  151. fi;
  152. if [ "" != "${3}" ]; then
  153. # Assume pipe name is what we want, delete existing file.
  154. PIPE=${3}
  155. rm -Rf ${PIPE}
  156. else
  157. # Make sure we get a file that doesn't already exist, keep appending our
  158. # pid untill we get a file that hasn't been taken.
  159. while [ -e "${PIPE}" ]; do
  160. PIPE="${PIPE}_${$}"
  161. done;
  162. fi;
  163. echo "Using pipe: "${PIPE}
  164. mkfifo "${PIPE}"
  165. closeProgress() {
  166. CONTINUE="0"
  167. if [ "${TYPE}" = "KDE" -a ${retval} != "0" ]; then
  168. dcop ${progresswindow} close
  169. fi;
  170. echo "Exiting with value: $retval"
  171. if [ -e ${PIPE} ]; then
  172. echo "Attempting to kill wget"
  173. kill -9 ${WGETPID}
  174. echo "Emptying pipe"
  175. cat ${PIPE};
  176. echo "Deleting Pipe ${PIPE}"
  177. rm -Rf "${PIPE}"
  178. fi;
  179. exit $retval;
  180. }
  181. trap 'closeProgress' INT TERM EXIT
  182. # if kdialog exists, and we have a display, and we are not running gnome,
  183. # and either we are running kde or zenity doesn't exist..
  184. if [ "" != "${KDIALOG}" -a "" != "${DISPLAY}" -a "" = "${ISGNOME}" -a "${USEKDIALOG}" = "1" ]; then
  185. echo "Progress dialog on Display: ${DISPLAY}"
  186. progresswindow=`${KDIALOG} --title "DMDirc: ${CAPTION}" --progressbar "${CAPTION}" 100`
  187. if [ "${KDETYPE}" = "dcop" ]; then
  188. ${DCOP} ${progresswindow} setAutoClose true
  189. ${DCOP} ${progresswindow} showCancelButton true
  190. elif [ "${KDETYPE}" = "qdbus" ]; then
  191. ${QDBUS} ${progresswindow} org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog autoClose true
  192. ${QDBUS} ${progresswindow} org.kde.kdialog.ProgressDialog.showCancelButton true
  193. elif [ "${KDETYPE}" = "dbussend" ]; then
  194. ${DBUSSEND} --print-reply --dest=${progresswindow} org.kde.kdialog.ProgressDialog.showCancelButton boolean:true >/dev/null
  195. ${DBUSSEND} --print-reply --dest=${progresswindow} org.freedesktop.DBus.Properties.Set string:'org.kde.kdialog.ProgressDialog' string:'autoClose' variant:boolean:true > /dev/null
  196. fi;
  197. TYPE="KDE"
  198. readprogress
  199. CONTINUE="0"
  200. echo "Progress Bar Complete"
  201. elif [ "" != "${ZENITY}" -a "" != "${DISPLAY}" ]; then
  202. # Else, if zenity exists and we have a display
  203. echo "Progress dialog on Display: ${DISPLAY}"
  204. TYPE="GNOME"
  205. readprogress | ${ZENITY} --progress --auto-close --auto-kill --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  206. CONTINUE="0"
  207. echo "Progress Bar Complete"
  208. else
  209. # Else, basic command-line progress
  210. echo "Progress For: ${CAPTION}"
  211. echo "-> 0%"
  212. readprogress
  213. CONTINUE="0"
  214. echo ""
  215. echo "Finished!"
  216. fi
  217. echo "Exiting progressbar"
  218. exit 0;