summaryrefslogtreecommitdiffstats
path: root/blueman_open
blob: 50d6b4322ca77ef90499018f88d5daeeb873ad76 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash

phone_addr=$1
own_name=$(basename $0)
dir=$(mktemp -d)/$phone_addr
browser=${browser:-"xdg-open"}

# Check for kdialog

if which kdialog &>/dev/null; then
    messagetype="kdialog2"
else
    messagetype="gtkdialog"
fi

# gtkdialog stuff

function gtkbox1() {
  export messagebox='
    <vbox>
    <frame Blueman Open Device.>
      <text>
	<label>"'`echo $1`'"</label>
      </text>
    </frame>
    </vbox>
    '
}

function gtkbox2() {
  export messagebox='
    <vbox>
    <frame Blueman Open Device.>
      <text>
	<label>"'`echo $1`'"</label>
      </text>
      <hbox>
	<button ok></button>
      </hbox>
    </frame>
    </vbox>
    '
}
function gtkmessage() {
	I=$IFS; IFS=""
	for STATEMENT in  $(gtkdialog --center --program=messagebox); do
		eval $STATEMENT
	done
	IFS=$I
}

#mount function

mount_phone ()
{
  i=0
  LIMIT=5
  while [ $i -lt $LIMIT ]
  do

    printf "$i "
    logger -i -t $own_name "Perform $i try to mount phone $phone_addr to $dir using obexfs"
    logger -i -t $own_name "Execute: obexfs -b $phone_addr -B 10 $dir"
    out=$(obexfs -b $phone_addr $dir 2>&1)
    logger -i -t $own_name "$out"
    mounts=$(grep $dir /proc/mounts | wc -l)
    logger -i -t $own_name "Found $mounts mounted $dir inside /proc/mounts"
    
    if [ $mounts -ne 0 ] ;then
      progress=99
      if [ $messagetype == "kdialog" ]; then
	kdialog --passivepopup "Successfully mounted $phone_addr to $dir" 2
      else
	gtkbox2 $"Successfully mounted $phone_addr to $dir"
	gtkmessage
      fi
      logger -i -t $own_name "Open $dir with $browser"
      out=$($browser $dir 2>&1)
      logger -i -t $own_name "$out"
      exit 0
    else
      logger -i -t $own_name "Failed to mount $phone_addr to $dir from the $i try"
    fi
    sleep 1
    ((i += 1))
  done
  if [ $mounts -eq 0 ] ;then
    if [ $messagetype == "kdialog" ]; then
      kdialog --passivepopup "Failed to mount $phone_addr to $dir.\nCheck /var/log/messages for errors." 4
    else
      gtkbox2 $"Failed to mount $phone_addr to $dir. Check /var/log/messages for errors."
      gtkmessage
    fi
    killall gtkdialog
  fi
}

#unmount function

umount_phone ()
{
  mounts=$(grep $dir /proc/mounts | wc -l)
  if [ $mounts -ne 0 ] ;then
    logger -i -t $own_name "Perform umount of $dir if it's already mounted."
    out=$(fusermount -uz $dir 2>&1)
    logger -i -t $own_name "$out"
    mounts=$(cat /proc/mounts |grep $dir|wc -l)
      if [ $mounts -eq 0 ] ;then
        logger -i -t $own_name "Successfully unmounted $dir."
      else
        logger -i -t $own_name "$dir is already mounted.  Failed to umount $dir.$out.  Please perform its umount manually."
	if [ $messagetype == "kdialog" ]; then
	  kdialog --passivepopup "Failed to umount $dir - check /var/log/messages for errors" 4
	else
	  gtkbox2 $"Failed to umount $dir - check /var/log/messages for errors"
	  gtkmessage
	fi
        exit
      fi
  else
    logger -i -t $own_name "$dir is not mounted - no need to perform umount..."
  fi
}

#mkdir function

create_dir ()
{
  progress=1
  logger -i -t $own_name "Create directory $dir to mount the phone by bluetooth..."
  out=$(mkdir -p $dir 2>&1)
  logger -i -t $own_name "$out"
}

if [ $messagetype == "kdialog" ]; then
  kdialog --passivepopup "Mount phone is in progress... Please wait..." 4	
else
  gtkbox1 $"Mount phone is in progress... Please wait..."
  gtkmessage&
fi

create_dir
umount_phone
mount_phone