summaryrefslogtreecommitdiffstats
path: root/blueman_open
blob: f46d97a16521f8e5695f3f2b3533c921a0d6b202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
# Blueman Mount Script for Slackware
# by Zarren Spry & Robby Workman


DEBUG=${DEBUG:-"1"}
if [ ! -z $DEBUG ];then
  dbg_msg="Check /var/log/messages for errors."
else
  dbg_msg="Enable *DEBUG* for error log."
fi

device_addr=$1
device_name=$(hcitool name $device_addr)
own_name=$(basename $0)
basedir=${basedir:-$HOME/.gvfs}
dir=${basedir}
browser=${browser:-"xdg-open"}
max_tries=5

# Check for kdialog
if which kdialog 1>/dev/null 2>/dev/null; then
  messagetype="kdialog"
else
  messagetype="libnotify"
fi

# Mount function
mount_device ()
{
  loop=0
  while [ $loop -lt $max_tries ]
  do
    [ ! -z $DEBUG ] && logger -i -t $own_name "Perform $loop try to mount device $device_name to $dir using gvfs."
    [ ! -z $DEBUG ] && logger -i -t $own_name "Execute: gvfs-mount obex://["$device_addr"]"
    out=$(gvfs-mount obex://["$device_addr"] 2>&1)
    [ ! -z $DEBUG ] && logger -i -t $own_name "$out"
    mounts=$(ls $dir | grep $device_name)
    [ ! -z $DEBUG ] && logger -i -t $own_name "Found $mounts mounted inside $dir"
    
    if [ "$mounts" = "$device_name" ] ;then
      MSG_TXT="Successfully mounted $device_name to $dir"
      if [ $messagetype == "kdialog" ]; then
	kdialog --passivepopup "$MSG_TXT" 2
      elif [ $messagetype == "libnotify" ]; then
	notify-send --expire-time=20000 --icon=blueman "$MSG_TXT"
      fi
      [ ! -z $DEBUG ] && logger -i -t $own_name "Open $dir/$device_name with $browser"
      out=$($browser $dir/$device_name 2>&1)
      [ ! -z $DEBUG ] && logger -i -t $own_name "$out"
      exit 0
    else
      [ ! -z $DEBUG ] && logger -i -t $own_name "Failed to mount $device_name to $dir/$device_name from try $loop "
      if [ $loop -ne 4 ]; then
	MSG_TXT="Attempt $loop to mount $device_name to $dir failed. Retrying, please wait ..."
      else
	MSG_TXT="Failed to mount $device_name to $dir. $dbg_msg"
      fi
      if [ $messagetype == "kdialog" ]; then
        kdialog --passivepopup "$MSG_TXT" 3
      elif [ $messagetype == "libnotify" ]; then
        notify-send --expire-time=20000 --icon=blueman "$MSG_TXT"
      fi
    fi
    sleep 2
    ((loop += 1))
  done
}

# Unmount function
umount_device ()
{
  mounts=$(ls $dir | grep $device_name)
  if [ "$mounts" = "$device_name" ] ;then
    [ ! -z $DEBUG ] && logger -i -t $own_name "Performing umount of $dir/$device_name."
    out=$(gvfs-mount -u obex://[$device_addr] 2>&1)
    [ ! -z $DEBUG ] && logger -i -t $own_name "$out"
    if [ -d $dir/$device_name ] ;then
      [ ! -z $DEBUG ] && logger -i -t $own_name "$device_name failed to unmount. Please perform its umount manually."
      MSG_TXT="Failed to umount $dir - $dbg_msg"
      if [ $messagetype == "kdialog" ]; then
	kdialog --passivepopup "$MSG_TXT" 3
      elif [ $messagetype == "libnotify" ]; then
	notify-send --expire-time=20000 --icon=blueman "$MSG_TXT"
      fi
    else
      [ ! -z $DEBUG ] && logger -i -t $own_name "Successfully unmounted $device_name."
    fi
  else
    [ ! -z $DEBUG ] && logger -i -t $own_name "$dir is not mounted - no need to perform umount..."
  fi
}

# Main
MSG_TXT="Mount device is in progress... Please wait..."
if [ $messagetype == "kdialog" ]; then
  kdialog --passivepopup "$MSG_TXT" 4
elif [ $messagetype == "libnotify" ]; then
  notify-send --expire-time=20000 --icon=blueman "$MSG_TXT"
fi

umount_device
mount_device