libmilisdialog 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!/bin/bash
  2. # xdialog kullanımını kolaylaştıran yordamlar.
  3. ## milisarge <milisarge@gmail.com>
  4. ## brokenman <brokenman@porteus.org> librokenman betiğinden yararlanılmıştır.
  5. ###################################
  6. ## READ THE COMMENTS CAREFULLY TO
  7. ## KNOW THE ARGUMENTS REQUIRED !!
  8. ###################################
  9. ## In most cases script returns variable $answ
  10. ## In this case 0=yes 1=no
  11. ## E.g is_64bit ($answ returns 0 then yes it is a 64bit machine)
  12. script=${0##*/} ### This is the users script name, not this one
  13. scriptpath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" ### Full script path
  14. answ=/tmp/.answ
  15. log=/var/log/libmilisdialog.log
  16. # Generic output for errors
  17. error(){
  18. echo $1 >> $log
  19. }
  20. # Find which window desktop session we are in. It returns wm=
  21. get_desktop_session(){
  22. # get desktop info
  23. startx=`ps x | grep -c /usr/bin/startx`
  24. if [ $startx -eq 2 ]; then
  25. lxde=`ps x | grep -v grep | grep -c /usr/bin/lxsession`
  26. trinity=`ps x | grep -v grep | grep tdeinit`
  27. kde4=`ps x | grep -v grep | grep -c /usr/bin/starttde`
  28. xfce=`ps x | grep -v grep | grep -c xfdesktop`
  29. else
  30. lxde=`ps aux | sed '/X '$(echo $DISPLAY|cut -d. -f1)'/,$ !d' | sed '1,/X / !d' | grep -c /usr/bin/lxsession`
  31. trinity=`ps aux | sed '/ '$DISPLAY' /,$ !d' | sed '1,/X / !d' | grep tdeinit`
  32. kde4=`ps aux | sed '/ '$DISPLAY' /,$ !d' | sed '1,/X / !d' | grep -c /usr/bin/startkde`
  33. xfce=`ps aux | sed '/ '$(echo $DISPLAY|cut -d. -f1)' /,$ !d' | sed '1,/X / !d' | grep -c xfdesktop`
  34. fi
  35. if [ "$lxde" -eq 1 ]; then
  36. wm=lxde
  37. elif [ "$trinity" ]; then
  38. wm=tde
  39. elif [ "$kde4" -eq 1 -a -z "$trinity" ]; then
  40. wm=kde4
  41. elif [ "$xfce" -eq 1 ]; then
  42. wm=xfce
  43. fi
  44. [ -z $wm ] && error "There was a problem finding the desktop session"
  45. }
  46. # milis mimari tipi
  47. is_64bit(){
  48. [ `uname -m|egrep "64"` ] && answ=0 || answ=1
  49. }
  50. # Silently kill a named process (number)
  51. kills(){
  52. # Check that $1 was given
  53. if [ $1 ]; then
  54. kill $1 2>/dev/null &
  55. wait $1 2>/dev/null
  56. else
  57. answ=3
  58. error "You must supply a process ID to kill!"
  59. return
  60. fi
  61. }
  62. # Unpack a compressed file to current dir
  63. # $1=compressed file
  64. unpack_file(){
  65. if [ ! $1 ]; then
  66. answ=3
  67. error "You must supply a compressed file"
  68. cyan "Supported files: .zip .tar.gz .tar.bz2 .xzm"
  69. return
  70. fi
  71. # Make sure file has valid extension
  72. local chkf
  73. local ext
  74. local nam
  75. local nam2
  76. chkf=`echo $1|egrep "*.tar.gz|.tar.bz2|.zip|.xzm|.tar.xz|.txz|.tgz"`
  77. if [ -z $chkf ]; then
  78. answ=3
  79. error "ERROR:" "This type of file is not supported!"
  80. return
  81. fi
  82. # Get extension
  83. ext=${chkf##*.}
  84. nam=${1##*/}
  85. case $ext in
  86. gz )
  87. tar zxvf $1 >/dev/null|| answ=3 && error "ERROR unpacking $1"
  88. ;;
  89. bz2 )
  90. tar xvjf $1 >/dev/null|| bzip2 -d $1 || answ=3 && error "ERROR unpacking $1"
  91. ;;
  92. zip )
  93. unzip $1 >/dev/null|| answ=3 && error "ERROR unpacking $1"
  94. ;;
  95. xzm )
  96. unsquashfs $1 >/dev/null 2>&1 answ=3 && error "ERROR unpacking $1"
  97. ;;
  98. xz )
  99. tar -Jxf $1 >/dev/null || answ=3 && error "ERROR unpacking $1"
  100. ;;
  101. txz )
  102. mktemp -d ./unpacked.XXXXXX
  103. local targ
  104. targ=`ls|grep unpacked`
  105. installpkg -root $targ $1 >/dev/null 2>&1 || answ=3 && error "ERROR unpacking $1"
  106. ;;
  107. tgz )
  108. mktemp -d ./unpacked.XXXXXX || answ=3 && error "ERROR unpacking $1"
  109. local targ
  110. targ=`ls|grep unpacked`
  111. installpkg -root $targ $1 >/dev/null 2>&1 || answ=3 && error "ERROR unpacking $1"
  112. ;;
  113. esac
  114. }
  115. # Make a posix test
  116. # $1=dir
  117. make_posix_test(){
  118. f1=$1/._test1
  119. f2=$1/._test2
  120. touch $f1
  121. ln -s $f1 $f2 2>/dev/null && chmod +x $f1 2>/dev/null && [ -x $f1 ] && chmod -x $f1 2>/dev/null && [ ! -x $f1 ] && rm -f $f1 $f2
  122. if [ $? -ne 0 ]; then
  123. answ=1
  124. rm -f $f1 $f2 >/dev/null 2>&1
  125. else
  126. answ=0
  127. fi
  128. }
  129. # Remove duplicates
  130. # $1=file
  131. remove_duplicates(){
  132. if [ ! $1 ]; then
  133. answ=3 && error "You must supply a valid list"
  134. return
  135. elif [ ! -f $1 ]; then
  136. error "Could not find $1" $FUNCNAME $LINENO
  137. cyan "Make sure the file exists and is a list"
  138. exit
  139. else
  140. sort -u $1 > /tmp/.hold
  141. rm $1
  142. mv /tmp/.hold $1
  143. fi
  144. }
  145. # A simple Xdialog download gauge
  146. # $1=title $2=text $3=link $4=rc-file
  147. download() {
  148. if [ $# -lt 4 ]; then
  149. error "You must supply 4 args to: $FUNCNAME" $FUNCNAME $LINENO
  150. cyan "\$1=title, \$2=text, \$3=link \$4=rc-file"
  151. cyan "If you don't have an rc-file just put anything"
  152. exit
  153. else
  154. touch /tmp/.dload
  155. dl=/tmp/.dload
  156. wget $3 -o $dl &
  157. (
  158. while [ "$i" != "98" -a "$f" == "" ]; do
  159. echo $i
  160. i=$(cat $dl 2>/dev/null| grep "%" |awk '{print $(NF-2)}'|sed 's/\(.*\)./\1/'|tail -n1)
  161. f=$(grep "100%" $dl)
  162. sleep 0.1
  163. done
  164. ) | Xdialog --title "$1" --rc-file "$4" --icon "$5" --gauge "Downloading $2...." 400x150
  165. [ -f $dl ] && rm -f $dl
  166. fi
  167. [ -f $dl ] && rm $dl
  168. }
  169. # Check that a filename is slackware compatible
  170. # If name is bad will return badname=1
  171. # If field has -lt 4 will return $badflds
  172. # If $PKG_ARCH is -ne i486,x86_64,noarch,fw will return $badarch
  173. check_slackware_name(){
  174. if [ $# -eq 0 ]; then
  175. error "You must supply a slackware file name!" $FUNCNAME $LINENO
  176. else
  177. local FILE=${1##*/}
  178. # Make sure all is in lower case
  179. FILE=`echo $FILE|tr "[:upper:]" "[:lower:]"`
  180. # Make sure there are no less than 4 fields
  181. local flds=`awk -F- '{print NF}' $FILE`
  182. [ $flds -lt 4 ] && { answ=1; return; }
  183. # Split package name to individual elements
  184. eval $(echo $FILE | sed 's/\(.*\)-\([^-]*\)-\([^-]*\)-\([0-9]*\)\(.*\)*/\
  185. PKG_NAME=\1 PKG_VER=\2 PKG_ARCH=\3 PKG_BUILD=\4 PKG_TAG=\5/')
  186. # Check that package arch is legit
  187. [ ! `echo $PKG_ARCH|egrep "i*86|x86_64|noarch|fw"` ] && { answ=1; return; }
  188. answ=0
  189. fi
  190. }
  191. # makes sure that is a directory is chosen, it is not on aufs
  192. # unless in tmp or home folder (never in root)
  193. # This function returns nopermit=1 if dir is bad.
  194. is_on_aufs(){
  195. if [ $# -ne 1 ]; then
  196. answ=3
  197. return
  198. else
  199. [ ! -d $1 ] && { answ=3; return; }
  200. [ "`df -T $1|tail -n1|awk '{print$1}'`" == "aufs" ] && answ=0 || answ=1
  201. fi
  202. }
  203. # Verify that this is a milis module & identify squashfs version
  204. # $1=module path
  205. # Returns: badext=1 bad extension | sq=4 squashfs4 | sq=3 squashfs3 | sq=0 not valid
  206. is_squash4(){
  207. if [ ! $1 ]; then
  208. error "You must supply the path to a valid Milis module" $FUNCNAME $LINENO
  209. return
  210. else
  211. mname=$1
  212. ftype=`file $mname|egrep -o "endian|data"`
  213. [ -z $ftype ] && answ=0 || answ=1
  214. fi
  215. }
  216. # Silently mount a module on loopback and check is module
  217. # Mount module to /mnt/loop
  218. mount_module(){
  219. if [ ! $1 ]; then
  220. answ=3
  221. return
  222. else
  223. is_squash4
  224. if [ $answ -eq 1 ]; then
  225. answ=3
  226. else
  227. mloop "$1" >/dev/null 2>&1 && answ=0 || answ=3
  228. fi
  229. fi
  230. }
  231. # Check if a website is online and working (silently)
  232. # Returns url=0 if it is avaiable
  233. silent_check_url(){
  234. if [ $# -lt 1 ]; then
  235. answ=3
  236. error "You must supply a URL to:" $FUNCNAME $LINENO
  237. exit
  238. else
  239. if (wget -q --spider --force-html --inet4-only "$1" >/dev/null 2>&1); then
  240. answ=0
  241. else
  242. answ=1
  243. fi
  244. fi
  245. }
  246. # Check if a website is online and working (silently)
  247. # Returns url=0 if it is avaiable
  248. has_internet(){
  249. if (wget -q --spider --force-html --inet4-only http://www.google.com >/dev/null 2>&1); then
  250. answ=0
  251. else
  252. answ=1
  253. fi
  254. }
  255. # Check that writable flag is set
  256. is_writable(){
  257. [ -w $1 ] && answ=0 || answ=1
  258. }
  259. # Get the last field in a given string with given delimiter
  260. # $1=delimiter
  261. get_last_field(){
  262. awk -F"$1" '{print$NF}'
  263. }
  264. # Get the field count for given string with given delimiter
  265. get_field_count(){
  266. awk -F"$1" '{print NF}'
  267. }
  268. # Make a free loop if one is not present
  269. check_free_loop_device(){
  270. x=`grep /dev/loop /proc/mounts | tail -n1 | cut -d " " -f1 | sed 's@/dev/loop@@'`; let y=x+1
  271. loop=/dev/loop$y
  272. if [ ! -e $loop ]; then mknod $loop b 7 $y; fi
  273. unset x loop y
  274. }
  275. # Green colored output
  276. green() {
  277. echo -e "\033[01;32m$@\033[00m"
  278. }
  279. # Yellow colored output
  280. yellow() {
  281. echo -e "\033[01;33m$@\033[00m"
  282. }
  283. # Red colored output
  284. function redd() {
  285. echo -e "\033[01;31m$@\033[00m"
  286. }
  287. # Cyan colored output
  288. cyan() {
  289. echo -e "\033[01;36m$@\033[00m"
  290. }
  291. # Blue colored output
  292. blue() {
  293. echo -e "\033[01;34m$@\033[00m"
  294. }
  295. # Magenta colored output
  296. magenta() {
  297. echo -e "\033[01;35m$@\033[00m"
  298. }
  299. ### GTKDIALOG LIBRARIES
  300. ## Use manual progress update like so:
  301. ## echo "My message goes here" > /tmp/.message
  302. ## manual_progress "My title goes here"
  303. ## sleep 5 (or other code)
  304. ## echo "Second update goes here" > /tmp/.message
  305. ## sleep 3 (or other code)
  306. ## kill_manual_progress
  307. manual_progress(){
  308. # Open a window for progress
  309. export MANUAL_PROGRESS='
  310. <window title="Milis message" icon-name="cdr" resizable="false" width-request="400" window_position="1">
  311. <vbox>
  312. <frame>
  313. <text use-markup="true" width-request="310">
  314. <label>"'$1'"</label>
  315. <variable>PROGRESS_TXT</variable>
  316. </text>
  317. <progressbar visible="true">
  318. <variable>PROGRESS_BAR</variable>
  319. <label>...</label>
  320. <input>prog_counter</input>
  321. </progressbar>
  322. </frame>
  323. </vbox>
  324. </window>
  325. '
  326. gtkdialog -c -p MANUAL_PROGRESS 2>/tmp/.progress_trap &
  327. }; export -f manual_progress
  328. ## This is called by the manual_progress function.
  329. prog_counter(){
  330. case $STATE in
  331. MANUAL )
  332. while [ A != B ]; do
  333. MSG=`cat /tmp/.message`
  334. echo $MSG
  335. sleep 0.3
  336. done
  337. echo "ALL DONE"
  338. unset STATE
  339. ;;
  340. DOWNLOAD )
  341. while [ A != B ]; do
  342. if [ -f /tmp/.cdload ]; then M=`cat /tmp/.cdload`; echo "$M"; fi
  343. sleep 0.3
  344. [ `grep "100%" /tmp/.message` ] && echo 1
  345. i=$(grep -o "[^ ]*%[^ ]*" /tmp/.message|sed "s/%//g"|tail -n1)
  346. [ "$i" == "100" ] && i=1
  347. [ "$i" != "" ] && echo "$i"
  348. sleep 0.3
  349. done
  350. echo "ALL DONE"
  351. unset STATE
  352. ;;
  353. BUILDMODULE )
  354. while [ A != B ]; do
  355. if [ ! -f /tmp/.message ]; then echo "Waiting ..."; fi
  356. [ `grep "100%" /tmp/.message` ] && echo Finished
  357. i=$(grep -o ...% /tmp/.message|sed -e "s/%//g" -e 's/ //g'|tail -n1)
  358. [ "$i" != "" ] && echo "$i"
  359. sleep 0.3
  360. done
  361. echo "ALL DONE"
  362. unset STATE
  363. ;;
  364. * )
  365. while [ A != B ]; do
  366. MSG=`cat /tmp/.message`
  367. echo $MSG
  368. sleep 0.3
  369. done
  370. echo "ALL DONE"
  371. unset STATE
  372. ;;
  373. esac
  374. [ -f /tmp/.cdload ] && rm /tmp/.cdload 2>&-
  375. [ -f /tmp/.message ] && rm /tmp/.message 2>&-
  376. }; export -f prog_counter
  377. kill_manual_progress() {
  378. myself=`ps ax|grep MANUAL_PROGRESS|awk '{print$1}'|head -n1`
  379. for children in `ps -o pid --ppid $myself|sed '/PID/d'`; do
  380. kill $children >/dev/null 2>&-
  381. done
  382. kill $myself >/dev/null 2>&-
  383. [ -f /tmp/.message ] && rm /tmp/.message
  384. [ -f /tmp/.progress_trap ] && rm /tmp/.progress_trap
  385. }; export -f kill_manual_progress
  386. gtk_message(){
  387. echo '
  388. <window window_position="1" title="Milis mesaj" icon-name="cdr" allow-shrink="false" width-request="'$2'">
  389. <vbox>
  390. <hbox>
  391. <frame>
  392. <pixmap icon_size="6">
  393. <input file stock="'$3'"></input>
  394. </pixmap>
  395. </frame>
  396. <frame>
  397. <text wrap="true"><label>"'$1'"</label></text>
  398. </frame>
  399. </hbox>
  400. <hbox>
  401. <button ok></button>
  402. </hbox>
  403. </vbox>
  404. <action signal="hide">exit:abort main window by X</action>
  405. </window>
  406. ' | gtkdialog -s
  407. }
  408. ## $1=message $2=width $3=icon(file)
  409. gtk_message_custom(){
  410. echo '
  411. <window window_position="1" title="Milis mesaj" icon-name="cdr" allow-shrink="false" width-request="'$2'">
  412. <vbox>
  413. <hbox>
  414. <frame>
  415. <pixmap icon_size="6">
  416. <input file>'$3'</input>
  417. </pixmap>
  418. </frame>
  419. <frame>
  420. <text use-markup="true" wrap="true"><label>"'$1'"</label></text>
  421. </frame>
  422. </hbox>
  423. <hbox>
  424. <button ok></button>
  425. </hbox>
  426. </vbox>
  427. <action signal="hide">exit:abort main window by X</action>
  428. </window>
  429. ' | gtkdialog -s
  430. }
  431. gtk_choose_file(){
  432. echo '
  433. <text label="'$1'"></text>
  434. <entry editable="false" fs-title="'$2'" fs-action="'$3'" fs-folder="'$4'" height-request="32">
  435. <variable>'$6'</variable>
  436. </entry>
  437. <button>
  438. <variable>'$5'</variable>
  439. <input file stock="gtk-find"></input>
  440. <action>fileselect:'$6'</action>
  441. </button>
  442. '
  443. }
  444. start_window(){
  445. echo '<window window_position="1" title="'$1'" icon-name="'$2'" allow-shrink="false" width-request="'$3'" height-request="'$4'">
  446. <vbox margin="10">'
  447. }
  448. end_window(){
  449. echo '</vbox><action signal="hide">exit:Cancel</action></window>'
  450. }
  451. txtmarkup(){
  452. echo '<text use-markup="true" width-request="'$1'" selectable="true" can-focus="no"><label>"'$2'"</label></text>'
  453. }
  454. txtcolor(){
  455. echo '<text use-markup="true" width-request="'$1'"><label>"<span fgcolor='"'$2'"' size='"'$3'"' weight='"'$4'"'>'$5'</span>"</label></text>'
  456. }
  457. txtfile(){
  458. echo '<text width-request="'$1'" selectable="true" can-focus="no"><label>NoTextFound</label><input file>'$2'</input></text>'
  459. }
  460. pixmapstock(){
  461. echo '<pixmap><input file stock="'$1'"></input></pixmap>'
  462. }
  463. pixmapicon(){
  464. echo '<pixmap><height>'$1'</height><width>'$2'</width><input file icon="'$3'"></input></pixmap>'
  465. }
  466. pixmapfile(){
  467. echo '<pixmap><height>'$1'</height><width>'$2'</width><input file>'$3'</input></pixmap>'
  468. }
  469. vsep(){
  470. echo '<vseparator></vseparator>'
  471. }
  472. hsep(){
  473. echo '<hseparator></hseparator>'
  474. }
  475. butstock(){
  476. echo '<button use-underline="true"><variable>'$1'</variable><label>'$2'</label><input file stock="'$3'"></input>'
  477. }
  478. buticon(){
  479. echo '<button><variable>'$1'</variable><label>'$2'</label><input file icon="'$3'"></input>'
  480. }
  481. butok(){
  482. echo '<button ok></button>'
  483. }
  484. butcancel(){
  485. echo '<button cancel></button>'
  486. }
  487. butyes(){
  488. echo '<button yes></button>'
  489. }
  490. butno(){
  491. echo '<button no></button>'
  492. }
  493. butreturn(){
  494. echo '<button tooltip-text="'$1'" use-underline="true"><label>_Return</label><input file stock="gtk-go-back"></input><sensitive>'$2'</sensitive>'
  495. }
  496. butcustom(){
  497. echo '<button tooltip-text="'$1'" sensitive="'$6'"><label>'$2'</label><variable>'$3'</variable><input file '$4'="'$5'"></input>'
  498. }
  499. rbut(){
  500. echo '<radiobutton active="'$1'"><variable>'$2'</variable><label>'$3'</label>'
  501. }
  502. chkbox(){
  503. echo '<checkbox active="'$1'"><label>"'$3'"</label><variable>'$2'</variable>'
  504. }
  505. blankline(){
  506. echo '<text><label>""</label></text>'
  507. }
  508. entry(){
  509. echo '<entry sensitive="'$1'" tooltip-text="'$2'"><default>"'$3'"</default><variable>'$4'</variable>'
  510. }
  511. table(){
  512. echo '<table><variable>'$1'</variable><label>"'$4'"</label><height>'$2'</height><width>'$3'</width>'
  513. }
  514. if [ "$1" == "-h" ]; then
  515. yellow start_window
  516. blue "WindowTitle WindowIcon width height"
  517. yellow "txtmarkup"
  518. blue "width text"
  519. yellow "txtcolor"
  520. blue "width color size weight text"
  521. yellow txtfile
  522. blue "width file"
  523. yellow pixmapstock
  524. blue "gtk-name"
  525. yellow pixmapicon
  526. blue "height width icon"
  527. yellow pixmapfile
  528. blue "height width file"
  529. yellow vsep
  530. blue "vertical seperator (nothing required)"
  531. yellow hsep
  532. blue "horizontal seperator (nothing required)"
  533. yellow butok
  534. blue "nothing required"
  535. yellow butcancel
  536. blue "nothing required"
  537. yellow butyes
  538. blue "nothing required"
  539. yellow butno
  540. blue "nothing required"
  541. yellow butstock
  542. blue "variable label gtk-icon"
  543. cyan "must be closed with </button>"
  544. yellow buticon
  545. blue "variable label icon"
  546. cyan "must be closed with </button>"
  547. yellow butreturn
  548. blue "tooltip-text sensitive (true/false)"
  549. cyan "must be closed with </button>"
  550. yellow butcustom
  551. blue "tooltip label variable stock/icon gtk-icon/iconname sensitive (true/false)"
  552. cyan "must be closed with </button>"
  553. yellow rbut
  554. blue "active (true/false) variable label"
  555. cyan "must be closed with </radiobutton>"
  556. yellow chbox
  557. blue "active (true/false) variable label"
  558. cyan "must be closed with </checkbox>"
  559. yellow blankline
  560. blue "makes a blank line"
  561. yellow entry
  562. blue "sensitive tooltip-text defaulttext variable"
  563. cyan "must be closed with </entry>"
  564. yellow table
  565. blue "variable height width label"
  566. cyan "enter <item>something</item>"
  567. cyan "Must be closed with </table>"
  568. fi