wok-6.x view yandex-disk/stuff/usr/bin/yandex-disk-helper @ rev 19523
Add yandex-disk (with custom desktop integration)
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Sun Nov 27 18:29:04 2016 +0200 (2016-11-27) |
parents | |
children |
line source
1 #!/bin/sh
2 # yandex-disk-helper - simple Yandex.Disk maintenance app
3 # Aleksei Bobylev <al.bobylev@gmail.com>, 2016
6 # Settings for usage statistics bar:
8 barw='48' # bar width in symbols
9 bars=' ' # bar symbol
10 barf='10' # bar font (Pango markup)
11 baru='background="#4277CA"' # style for 'used' part (Pango markup)
12 bart='background="#CA9342"' # style for 'trash' part (Pango markup)
13 bara='background="#CACACA"' # style for 'available' part (Pango markup)
16 # i18n
17 _() { gettext 'yandex-disk-helper' "$1"; }
19 tmpid='/tmp/yandex-disk-notify.id'
21 title="$(_ 'Yandex.Disk')"
23 # Get untranslated status
24 statusC=$(LC_ALL=C yandex-disk status)
27 # Draw colored bar: used, trash, available, and print used %
29 bar() {
30 echo "$statusC" | awk -vwidth="$barw" -vsym="$bars" -vfont="$barf" \
31 -vusedstyle="$baru" -vtrashstyle="$bart" -vavailstyle="$bara" '
32 # Convert argument in human-readable form like "23.7 MB" into bytes
33 function bytes() {
34 split($0, a, ": ");
35 split(a[2], s, " ");
36 if (s[2] == "KB") { return (sprintf("%.0f", s[1] * 1024)); }
37 if (s[2] == "MB") { return (sprintf("%.0f", s[1] * 1024 * 1024)); }
38 if (s[2] == "GB") { return (sprintf("%.0f", s[1] * 1024 * 1024 * 1024)); }
39 }
41 # Draw bar using specified symbol
42 function bar(n) {
43 num = 1 * sprintf("%.0f", n);
44 out = "";
45 if (num > 0)
46 for (i = 1; i <= num; i++) out = out sym;
47 return out;
48 }
50 # Get sizes
51 /Total:/ { total = bytes(); }
52 /Used:/ { used = bytes(); }
53 /Trash size:/ { trash = bytes(); }
54 /Available:/ { avail = bytes(); }
56 END {
57 # If daemon stopped, we got no values, then skip
58 if (total) {
59 # Calculate bar parts width in symbols
60 usedbar = width * (used - trash) / total;
61 trashbar = width * trash / total;
62 availbar = width * avail / total;
64 usedpct = 100 * used / total;
66 # Print complete bar
67 printf("<span font=\"%s\"><tt>", font);
68 printf("<span %s>%s</span>", usedstyle, bar(usedbar));
69 printf("<span %s>%s</span>", trashstyle, bar(trashbar));
70 printf("<span %s>%s</span>", availstyle, bar(availbar));
71 printf("</tt></span>");
72 printf(" %.1f %%", usedpct);
73 }
74 }
75 '
76 }
79 # Localize 'GB' and 'MB' at the end of line
81 gbmb() {
82 local GB=$(_ 'GB') MB=$(_ 'MB')
83 sed "s|GB$|$GB|;s|MB$|$MB|"
84 }
87 # Display Yad window with Yandex.Disk status
89 status() {
90 version=$(yandex-disk -v | head -n1)
91 copyright=$(yandex-disk -v | tail -n1)
93 yandex-disk status | gbmb | \
94 yad --window-icon='yandex-disk' --center \
95 --title="$title" --width='500' --height='300' \
96 --image='yandex-disk' \
97 --text="\
98 <span size='4000'> </span>
99 <span size='x-large'>$version</span>
100 <small>$copyright</small>
102 $(bar)" \
103 --text-align='center' \
104 --text-info \
105 --button='gtk-preferences:2' \
106 --button='gtk-close'
107 }
110 # Display Yad window with Yandex.Disk preferences
112 preferences() {
113 # Get the value of variable $dir
114 . "$HOME/.config/yandex-disk/config.cfg"
116 yad --window-icon='yandex-disk' --center \
117 --title="$title" --image='yandex-disk' --fixed \
118 --form --columns="2" \
119 --field="$(_ 'Reconfigure')!system-run:FBTN" "$0 configure" \
120 --field="$(_ 'Start')!media-playback-start:FBTN" "$0 start" \
121 --field="$(_ 'Stop')!media-playback-stop:FBTN" "$0 stop" \
122 --field=" :LBL" "" \
123 --field="$(_ 'Open local folder')!folder:FBTN" "pcmanfm $dir" \
124 --field="$(_ 'Open online folder')!folder-remote:FBTN" "$BROWSER https://disk.yandex.ru/" \
125 --button='gtk-close' \
126 >/dev/null
127 }
130 # Display Yad window when Yandex.Disk not configured yet
132 firstrun() {
133 yad --window-icon='yandex-disk' --center \
134 --title="$title" --width='300' --image='yandex-disk' \
135 --text="<b>$(_ 'Welcome to Yandex.Disk!')</b>
137 $(_ 'To use Yandex.Disk on this computer, you must first configure it.')" || exit 0
139 $0 configure
140 exit 0
141 }
144 # Check commands: start, stop, and configure
146 case $1 in
147 start|stop)
148 answer=$(yandex-disk $1)
149 if [ -e "$tmpid" ]; then
150 # Replace existing notification
151 notify_id=$(head -n1 $tmpid)
152 notify-send -i 'yandex-disk' -r "${notify_id:-1}" -a tazweb "$title" "$answer"
153 else
154 # Make new notification
155 notify-send -i 'yandex-disk' -p -a tazweb "$title" "$answer" > "$tmpid"
156 fi
158 # Remove tmpid if it expired 5 sec (notification closed by timeout);
159 # leave it if it updated (new notification was replaced the existing)
160 # For example: press Stop, wait a second, press Start: there should be the single notification
161 touch "$tmpid"; sleep 1; tmp_anchor=$(mktemp); sleep 4
162 [ "$tmpid" -ot "$tmp_anchor" ] && rm "$tmpid"
163 rm "$tmp_anchor"
165 exit 0
166 ;;
168 configure)
169 # Stop daemon before, because it will not get the same path instead
170 yandex-disk stop
172 # Run configuration in the terminal window
173 answer=$(terminal -t "$title" -e yandex-disk setup)
175 # Additional configuration in the YAD window
176 # Add bookmark if: checkbox checked and Finish pressed and bookmark absent yet
177 answer=$(yad --window-icon='yandex-disk' --center \
178 --title="$title" --image='yandex-disk' --fixed \
179 --form --always-print-result \
180 --field="$(_ 'Add folder to bookmarks'):CHK" "true" \
181 --button="$(_ 'Finish')")
182 [ $? -eq 0 ] || exit 0
184 case "$answer" in
185 *TRUE*)
186 . "$HOME/.config/yandex-disk/config.cfg"
187 bookmarks="$HOME/.config/gtk-3.0/bookmarks"
188 fgrep -q "$dir" "$bookmarks" && exit 0
189 echo "file://$dir $title" >> "$bookmarks"
190 ;;
191 esac
193 exit 0
194 ;;
195 esac
198 # Run without commands
200 # Is Yandex.Disk already configured?
201 [ ! -e "$HOME/.config/yandex-disk/iid" ] && firstrun
203 # Display status
204 status
205 [ $? -ne 2 ] && exit 0
207 # Button Preferences pressed
208 preferences
210 exit 0