dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | #--------------------------------------------- |
| 3 | # xdg-copy |
| 4 | # |
| 5 | # Utility script to copy files specified by URLs, including |
| 6 | # downloading and uploading from/to remote sites. |
| 7 | # |
| 8 | # Refer to the usage() function below for usage. |
| 9 | # |
| 10 | # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 11 | # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
| 12 | # |
| 13 | # LICENSE: |
| 14 | # |
| 15 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 16 | # copy of this software and associated documentation files (the "Software"), |
| 17 | # to deal in the Software without restriction, including without limitation |
| 18 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 19 | # and/or sell copies of the Software, and to permit persons to whom the |
| 20 | # Software is furnished to do so, subject to the following conditions: |
| 21 | # |
| 22 | # The above copyright notice and this permission notice shall be included |
| 23 | # in all copies or substantial portions of the Software. |
| 24 | # |
| 25 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 26 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 28 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 | # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 | # OTHER DEALINGS IN THE SOFTWARE. |
| 32 | # |
| 33 | #--------------------------------------------- |
| 34 | |
| 35 | manualpage() |
| 36 | { |
| 37 | cat << _MANUALPAGE |
| 38 | Name |
| 39 | |
| 40 | xdg-copy -- command line tool for copying files between desktop URIs |
| 41 | |
| 42 | Synopsis |
| 43 | |
| 44 | xdg-copy source destination |
| 45 | |
| 46 | xdg-copy { --help | --manual | --version } |
| 47 | |
| 48 | Description |
| 49 | |
| 50 | xdg-copy copies source to destination and provides visual feedback to the |
| 51 | user during the operation. Both source and destination can either be a |
| 52 | file or URL. Supported URL types are file, ftp, http and https. Additional |
| 53 | URL types may be supported depending on the desktop environment. |
| 54 | |
| 55 | xdg-copy is for use inside a desktop session only. It is not recommended |
| 56 | to use xdg-copy as root. |
| 57 | |
| 58 | Options |
| 59 | |
| 60 | --help |
| 61 | Show command synopsis. |
| 62 | |
| 63 | --manual |
| 64 | Show this manualpage. |
| 65 | |
| 66 | --version |
| 67 | Show the xdg-utils version information. |
| 68 | |
| 69 | Exit Codes |
| 70 | |
| 71 | An exit code of 0 indicates success while a non-zero exit code indicates |
| 72 | failure. The following failure codes can be returned: |
| 73 | |
| 74 | 1 |
| 75 | Error in command line syntax. |
| 76 | |
| 77 | 2 |
| 78 | One of the files passed on the command line did not exist. |
| 79 | |
| 80 | 3 |
| 81 | A required tool could not be found. |
| 82 | |
| 83 | 4 |
| 84 | The action failed. |
| 85 | |
| 86 | Examples |
| 87 | |
| 88 | xdg-copy "http://portland.freedesktop.org/png/freedesktop-logo.png" . |
| 89 | |
| 90 | xdg-copy "/tmp/foobar.png" "/home/user/foobar-copy.png" |
| 91 | _MANUALPAGE |
| 92 | } |
| 93 | |
| 94 | usage() |
| 95 | { |
| 96 | cat << _USAGE |
| 97 | xdg-copy -- command line tool for copying files between desktop URIs |
| 98 | |
| 99 | Synopsis |
| 100 | |
| 101 | xdg-copy source destination |
| 102 | |
| 103 | xdg-copy { --help | --manual | --version } |
| 104 | |
| 105 | _USAGE |
| 106 | } |
| 107 | |
| 108 | #@xdg-utils-common@ |
| 109 | |
| 110 | #---------------------------------------------------------------------------- |
| 111 | # Common utility functions included in all XDG wrapper scripts |
| 112 | #---------------------------------------------------------------------------- |
| 113 | |
| 114 | #------------------------------------------------------------- |
| 115 | # Exit script on successfully completing the desired operation |
| 116 | |
| 117 | exit_success() |
| 118 | { |
| 119 | if [ $# -gt 0 ]; then |
| 120 | echo "$@" |
| 121 | echo |
| 122 | fi |
| 123 | |
| 124 | exit 0 |
| 125 | } |
| 126 | |
| 127 | |
| 128 | #----------------------------------------- |
| 129 | # Exit script on malformed arguments, not enough arguments |
| 130 | # or missing required option. |
| 131 | # prints usage information |
| 132 | |
| 133 | exit_failure_syntax() |
| 134 | { |
| 135 | if [ $# -gt 0 ]; then |
| 136 | echo "xdg-copy: $@" >&2 |
| 137 | echo "Try 'xdg-copy --help' for more information." >&2 |
| 138 | else |
| 139 | usage |
| 140 | echo "Use 'man xdg-copy' or 'xdg-copy --manual' for additional info." |
| 141 | fi |
| 142 | |
| 143 | exit 1 |
| 144 | } |
| 145 | |
| 146 | #------------------------------------------------------------- |
| 147 | # Exit script on missing file specified on command line |
| 148 | |
| 149 | exit_failure_file_missing() |
| 150 | { |
| 151 | if [ $# -gt 0 ]; then |
| 152 | echo "xdg-copy: $@" >&2 |
| 153 | fi |
| 154 | |
| 155 | exit 2 |
| 156 | } |
| 157 | |
| 158 | #------------------------------------------------------------- |
| 159 | # Exit script on failure to locate necessary tool applications |
| 160 | |
| 161 | exit_failure_operation_impossible() |
| 162 | { |
| 163 | if [ $# -gt 0 ]; then |
| 164 | echo "xdg-copy: $@" >&2 |
| 165 | fi |
| 166 | |
| 167 | exit 3 |
| 168 | } |
| 169 | |
| 170 | #------------------------------------------------------------- |
| 171 | # Exit script on failure returned by a tool application |
| 172 | |
| 173 | exit_failure_operation_failed() |
| 174 | { |
| 175 | if [ $# -gt 0 ]; then |
| 176 | echo "xdg-copy: $@" >&2 |
| 177 | fi |
| 178 | |
| 179 | exit 4 |
| 180 | } |
| 181 | |
| 182 | |
| 183 | #---------------------------------------- |
| 184 | # Checks for shared commands, e.g. --help |
| 185 | |
| 186 | check_common_commands() |
| 187 | { |
| 188 | while [ $# -gt 0 ] ; do |
| 189 | parm=$1 |
| 190 | shift |
| 191 | |
| 192 | case $parm in |
| 193 | --help) |
| 194 | usage |
| 195 | echo "Use 'man xdg-copy' or 'xdg-copy --manual' for additional info." |
| 196 | exit_success |
| 197 | ;; |
| 198 | |
| 199 | --manual) |
| 200 | manualpage |
| 201 | exit_success |
| 202 | ;; |
| 203 | |
| 204 | --version) |
| 205 | echo "xdg-copy 1.0beta1" |
| 206 | exit_success |
| 207 | ;; |
| 208 | esac |
| 209 | done |
| 210 | } |
| 211 | |
| 212 | check_common_commands "$@" |
| 213 | |
| 214 | #-------------------------------------- |
| 215 | # Checks for known desktop environments |
| 216 | # set variable DE to the desktop environments name, lowercase |
| 217 | |
| 218 | detectDE() |
| 219 | { |
| 220 | if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 221 | elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
| 222 | elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce; |
| 223 | fi |
| 224 | } |
| 225 | |
| 226 | #---------------------------------------------------------------------------- |
| 227 | |
| 228 | |
| 229 | |
| 230 | copy_kde() |
| 231 | { |
| 232 | kfmclient copy "$1" "$2" |
| 233 | |
| 234 | if [ $? -eq 0 ]; then |
| 235 | exit_success |
| 236 | else |
| 237 | exit_failure_operation_failed |
| 238 | fi |
| 239 | } |
| 240 | |
| 241 | copy_gnome() |
| 242 | { |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 243 | if gvfs-copy --help 2>/dev/null 1>&2; then |
| 244 | gvfs-copy "$1" "$2" |
| 245 | else |
| 246 | gnomevfs-copy "$1" "$2" |
| 247 | fi |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 248 | |
| 249 | if [ $? -eq 0 ]; then |
| 250 | exit_success |
| 251 | else |
| 252 | exit_failure_operation_failed |
| 253 | fi |
| 254 | } |
| 255 | |
| 256 | [ x"$1" != x"" ] || exit_failure_syntax |
| 257 | |
| 258 | source= |
| 259 | dest= |
| 260 | while [ $# -gt 0 ] ; do |
| 261 | parm=$1 |
| 262 | shift |
| 263 | |
| 264 | case $parm in |
| 265 | -*) |
| 266 | exit_failure_syntax "unexpected option '$parm'" |
| 267 | ;; |
| 268 | |
| 269 | *) |
| 270 | if [ -n "$dest" ] ; then |
| 271 | exit_failure_syntax "unexpected argument '$parm'" |
| 272 | fi |
| 273 | if [ -n "$source" ] ; then |
| 274 | dest=$parm |
| 275 | else |
| 276 | source=$parm |
| 277 | fi |
| 278 | ;; |
| 279 | esac |
| 280 | done |
| 281 | |
| 282 | if [ -z "${source}" ] ; then |
| 283 | exit_failure_syntax "source argument missing" |
| 284 | fi |
| 285 | if [ -z "${dest}" ] ; then |
| 286 | exit_failure_syntax "destination argument missing" |
| 287 | fi |
| 288 | |
| 289 | detectDE |
| 290 | |
| 291 | case "$DE" in |
| 292 | kde) |
| 293 | copy_kde "$source" "$dest" |
| 294 | ;; |
| 295 | |
| 296 | gnome) |
| 297 | copy_gnome "$source" "$dest" |
| 298 | ;; |
| 299 | |
| 300 | *) |
| 301 | exit_failure_operation_impossible "no method available for copying '$source' to '$dest'" |
| 302 | ;; |
| 303 | esac |