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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. PIDOF=`which pidof`
  13. if [ "${PIDOF}" = "" ]; then
  14. # For some reason some distros hide pidof...
  15. if [ -e /sbin/pidof ]; then
  16. PIDOF=/sbin/pidof
  17. elif [ -e /usr/sbin/pidof ]; then
  18. PIDOF=/usr/sbin/pidof
  19. fi;
  20. fi;
  21. ## Helper Functions
  22. if [ "${PIDOF}" = "" ]; then
  23. ISKDE=`${PIDOF} -x -s kdeinit`
  24. ISGNOME=`${PIDOF} -x -s gnome-panel`
  25. else
  26. ISKDE=`ps ux | grep kdeinit | grep -v grep`
  27. ISGNOME=`ps ux | grep gnome-panel | grep -v grep`
  28. fi;
  29. KDIALOG=`which kdialog`
  30. ZENITY=`which zenity`
  31. CAPTION=${1}
  32. FILESIZE=${2}
  33. progresswindow=""
  34. TYPE=""
  35. PIPE="progresspipe_${$}"
  36. retval="1"
  37. readprogress() {
  38. data=""
  39. input=""
  40. while [ -e ${PIPE} ]; do
  41. if [ "${TYPE}" = "KDE" ]; then
  42. if [ `dcop ${progresswindow} wasCancelled` = "true" ]; then
  43. break;
  44. fi
  45. fi;
  46. input=`cat "${PIPE}"`
  47. if [ "${input}" == "quit" ]; then
  48. break;
  49. elif [ "${input}" != "" ]; then
  50. data=${input}
  51. input=""
  52. res=`echo "scale=4 ; (${data}/${FILESIZE})*100" | bc`
  53. val=${res%%.*}
  54. if [ "${val}" = "" ]; then
  55. val=0
  56. fi;
  57. if [ "${TYPE}" = "KDE" ]; then
  58. dcop ${progresswindow} setProgress ${val}
  59. if [ ${?} -ne 0 ]; then
  60. break;
  61. fi;
  62. elif [ "${TYPE}" = "GNOME" ]; then
  63. echo ${val}
  64. if [ $? -ne 0 ] ; then
  65. break;
  66. fi
  67. else
  68. if [ $((${val} % 2)) -eq 0 ]; then
  69. echo "-> "${val}"%"
  70. fi;
  71. fi;
  72. if [ "${val}" = "100" ]; then
  73. retval="0"
  74. break;
  75. fi;
  76. fi;
  77. done;
  78. }
  79. if [ "" = "${CAPTION}" -o "" = ${FILESIZE} ]; then
  80. echo "Insufficient Parameters."
  81. echo "Usage: ${0} <caption> <totalvalue> [pipename]"
  82. exit;
  83. fi;
  84. if [ "" != "${3}" ]; then
  85. # Assume pipe name is what we want, delete existing file.
  86. PIPE=${3}
  87. rm -Rf ${PIPE}
  88. else
  89. # Make sure we get a file that doesn't already exist, keep appending our
  90. # pid untill we get a file that hasn't been taken.
  91. while [ -e "${PIPE}" ]; do
  92. PIPE="${PIPE}_${$}"
  93. done;
  94. fi;
  95. echo "Using pipe: "${PIPE}
  96. mkfifo "${PIPE}"
  97. closeProgress() {
  98. if [ "${TYPE}" = "KDE" -a ${retval} != "0" ]; then
  99. dcop ${progresswindow} close
  100. fi;
  101. echo "Deleting ${PIPE}"
  102. rm -Rf "${PIPE}"
  103. exit $retval;
  104. }
  105. trap 'closeProgress' INT TERM EXIT
  106. if [ "" != "${ISKDE}" -a "" != "${KDIALOG}" -a "" != "${DISPLAY}" ]; then
  107. echo "Progress dialog on Display: ${DISPLAY}"
  108. progresswindow=`${KDIALOG} --title "DMDirc: ${CAPTION}" --progressbar "${CAPTION}" 100`
  109. dcop ${progresswindow} setAutoClose true
  110. dcop ${progresswindow} showCancelButton true
  111. TYPE="KDE"
  112. readprogress
  113. elif [ "" != "${ISGNOME}" -a "" != "${ZENITY}" -a "" != "${DISPLAY}" ]; then
  114. echo "Progress dialog on Display: ${DISPLAY}"
  115. TYPE="GNOME"
  116. readprogress | ${ZENITY} --progress --auto-close --auto-kill --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  117. else
  118. echo "Progress For: ${CAPTION}"
  119. echo "-> 0%"
  120. readprogress
  121. echo ""
  122. echo "Finished!"
  123. fi