Adding plugin files to version control system.
[bluehi] / plugins / canola-bluehi / bluehi / bluetooth_manager.py
1 """
2 Project: BlueHi: A plugin for Canola
3 File name: bluetooth_manager.py
4
5 Description: This software was developed in Zagaia Project
6
7 Copyright (C) 2009
8     Antonio R. de C. Junior  <brankinhu@gmail.com>
9     Henry Miller M. Bilby <henrymiller.engenheiro@gmail.com>
10     Mauricio Figueiredo <mauricio.figueiredo@fucapi.br>
11     Samuel de Oliveira Fagundes <sf.oliveira@gmail.com>
12     
13     This program is free software; you can redistribute it and/or modify 
14     it under the terms of the GNU General Public License as published by 
15     the Free Software Foundation; either version 2 of the License, or 
16     (at your option) any later version.
17     
18     This program is distributed in the hope that it will be useful, but 
19     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
20     or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
21     for more details.
22     
23     You should have received a copy of the GNU General Public License along 
24     with this program; if not, write to the Free Software Foundation, Inc., 
25     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 """
27
28
29 import lightblue
30 import os
31 from lightblue import BluetoothError
32 from lightblue.obex import OBEXError
33
34 class BluetoothManager:
35
36     def search_devices(self):
37         print "def search_devices(self):"
38         self.devices = lightblue.finddevices()
39         return self.devices
40
41     def connect(self, address):
42         port = self.find_port(address)
43         if port:
44             self.list = [address, port]
45         else:
46             print "Nao foi possivel estabelecer conexao com o dispositivo"
47         return port
48
49     def send_file(self, path):
50         address = self.list[0]
51         port = self.list[1]
52         file_length = self.get_file_lenght(path)
53         file_name = self.get_file_name(path)
54
55         client = lightblue.obex.OBEXClient(address, port)
56         client.connect()
57         try:
58             put_response = client.put({"name": file_name, "length": file_length}, file(path))
59             if put_response.code != lightblue.obex.OK:
60                 print "Cancelado pelo usuario"
61                 return False
62             else:
63                 return True
64             client.disconnect()
65         except OBEXError:
66             print "Erro!"
67             client.disconnect()
68
69
70
71     def find_services(self, address):
72         return lightblue.findservices(address)
73
74     def find_port(self, address):
75         port = None
76         services = self.find_services(address)
77         for i in services:
78             print i
79             if i[2] == "OBEX Object Push":
80                 port = i[1]
81                 return port
82
83     def get_file_lenght(self, file):
84         return int(os.path.getsize(file))
85
86     def get_file_name(self, file):
87         file = file.split("/")
88         tam = len(file)
89         return file[tam - 1]