Adding plugin files to version control system.
[bluehi] / plugins / canola-bluehi / bluehi / model.py
1 """
2 Project: BlueHi: A plugin for Canola
3 File name: model.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 dbus
30 import os
31
32 from terra.core.manager import Manager
33 from terra.core.task import Task
34 from terra.core.model import ModelFolder, Model
35 from terra.ui.throbber import ThrobberWidget
36 from terra.core.threaded_model import ThreadedModelFolder
37 from terra.core.threaded_func import ThreadedFunction
38 from terra.core.plugin_prefs import PluginPrefs
39 from bluetooth_manager import BluetoothManager
40
41
42 blue = BluetoothManager()
43 manager = Manager()
44
45 WaitNotify = manager.get_class("Model/WaitNotify")
46 system_properties = manager.get_class("SystemProperties")
47 PluginDefaultIcon = manager.get_class("Icon/Plugin")
48 canola_prefs = PluginPrefs("settings")
49 CanolaError = manager.get_class("Model/Notify/Error")
50
51
52 """Create plugin and icon"""
53 class Icon(PluginDefaultIcon):
54     terra_type = "Icon/Folder/Task/Apps/Bluetooth"
55     icon = "icon/main_item/bluehi"
56     plugin = "BlueHI"
57
58 class MainFolder(ModelFolder):
59     """
60     This class is loaded when the plugin starts.
61     """
62     terra_type = "Model/Folder/Task/Apps/Bluetooth"
63     terra_task_type = "Task/Folder/Task/Apps/Bluetooth"
64
65     def __init__(self, parent):
66         Task.__init__(self)
67         ModelFolder.__init__(self, "BlueHi", parent)
68         self.callback_notify = None
69
70     def do_load(self):
71         BluetoothDeviceFolder("Search devices", self)
72
73 class BluetoothDeviceFolder(ModelFolder):
74     """
75     This class search and detect the devices with bluetooth
76     enabled.
77     """
78     terra_type = "Model/Folder/Task/Apps/Bluetooth/FolderDevice"
79
80     def __init__(self, name, parent):
81         ModelFolder.__init__(self, name, parent, None)
82         self.callback_notify = None
83         self.msg = None
84
85     def do_load(self):
86         self.search()
87
88     def search(self, end_callback=None):
89         #Verifica se o bluetooth esta ativado senao estiver, ativa-o
90         #This method verify the status of device's bluetooth,if it's disable, enable it.
91         bus = dbus.SystemBus()
92         bus_adapter = dbus.Interface(bus.get_object('org.bluez',\
93                 '/org/bluez/hci0'), 'org.bluez.Adapter')
94
95         if not bus_adapter.IsConnectable():
96             bus_adapter.SetMode("connectable")
97
98         def load():
99             devices = None
100             try:
101                 devices = blue.search_devices()
102             except BluetoothError:
103                 self.msg = "Bluetooth error."
104
105             return devices
106
107         def end(exception,devices):
108             if self.msg is not None:
109                 self.callback_notify(CanolaError(self.msg))
110                 self.inform_loaded()
111                 return
112
113             if devices is None:
114                 self.msg = "Devices not found"
115                 self.callback_notify(CanolaError(self.msg))
116                 self.inform_loaded()
117                 return
118
119             if end_callback:
120                 end_callback()
121
122             for list in devices:
123                 addr = list[0]
124                 name = list[1]
125                 BluetoothListDevice([addr, name], self)
126
127             self.inform_loaded()
128
129         self.is_loading = True
130         ThreadedFunction(end,load).start()
131
132 class BluetoothListDevice(ModelFolder):
133     """
134     This class list all of devices founded, detect the device's port of
135     comunication, try to connect with other device by bluetooth and create an
136     OBEXClient to transfer the medias.
137     """
138     terra_type = "Model/Folder/Task/Apps/Bluetooth/ListDevice"
139
140     def __init__(self, list_device, parent):
141         self.list_device = list_device
142         ModelFolder.__init__(self, list_device[1], parent)
143         self.callback_notify = None
144         self.device_addr = list_device[0]
145
146     def do_load(self):
147
148         def load():
149             port = blue.connect(self.device_addr)
150             return port
151
152         def end(exception,port):
153
154             if port is None:
155                 self.callback_notify(CanolaError("Unable to connect to the\
156                         device."))
157                 self.inform_loaded()
158                 return
159
160             self.inform_loaded()
161             BluetoothAudio("Audio", self)
162             BluetoothVideo("Video", self)
163             BluetoothImage("Image", self)
164
165         self.is_loading = True
166         ThreadedFunction(end,load).start()
167
168 class BluetoothAudio(ModelFolder):
169     """
170     This class list audio medias
171     """
172     terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Audio"
173
174     def __init__(self, type, parent):
175         ModelFolder.__init__(self, type, parent)
176         self.callback_notify = None
177         self.path_audio = []
178
179     def do_load(self):
180         self.path_audio = canola_prefs["audio"]
181         albuns = os.listdir(self.path_audio[0])
182         aux = Is_Directory(' ')
183         for album in albuns:
184             if aux.search(self.path_audio[0]+'/'+album):
185                 BrowseByFolder(album, self.path_audio[0], self)
186             else:
187                 FileFolder(album,self)
188
189     def send(self, file):
190         self.path = self.path_audio[0] + "/" + file
191         if blue.send_file(self.path):
192             self.callback_notify = CanolaError("File sent successfully")
193         else:
194             self.callback_notify(CanolaError("Connection failed!\
195                     \nDetecting devices again"))
196
197 class BluetoothVideo(ModelFolder):
198     """
199     This class list video medias
200     """
201     terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Video"
202
203     def __init__(self, type, parent):
204         ModelFolder.__init__(self, type, parent)
205         self.callback_notify = None
206         self.path_video = []
207
208     def do_load(self):
209         self.path_video = canola_prefs["video"]
210         albuns = os.listdir(self.path_video[0])
211         aux = Is_Directory(' ')
212         for album in albuns:
213             if aux.search(self.path_video[0]+'/'+album):
214                 BrowseByFolder(album, self.path_video[0], self)
215             else:
216                 FileFolder(album,self)
217
218     def enviar(self,file):
219         self.path = self.path_video[0]+"/"+file
220         if blue.send_file(self.path):
221             self.callback_notify = CanolaError("File sent successfully")
222         else:
223             self.callback_notify(CanolaError("Connection failed!\
224                     \nDetecting devices again"))
225
226 class BluetoothImage(ModelFolder, Task):
227     """
228     This class list image medias
229     """
230     terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Image"
231
232     def __init__(self, type, parent):
233         ModelFolder.__init__(self, type, parent)
234         self.callback_notify = None
235         self.path_photo = []
236
237     def do_load(self):
238         self.path_photo=canola_prefs["photo"]
239         albuns=os.listdir(self.path_photo[0])
240         aux = Is_Directory(' ')
241         for album in albuns:
242             if aux.search(self.path_photo[0]+'/'+album):
243                 BrowseByFolder(album, self.path_photo[0], self)
244             else:
245                 FileFolder(album,self)
246
247     def send(self,file):
248         self.path = self.path_photo[0]+"/"+file
249         if blue.send_file(self.path):
250             self.callback_notify = CanolaError("File sent successfully")
251         else:
252             self.callback_notify(CanolaError("Connection failed!\
253                     \nDetecting devices again"))
254
255 class BrowseByFolder(ModelFolder):
256     """
257     Generic class to list folders from audio , video and photo directories
258     """
259     terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/File/BrowseByFolder"
260
261     def __init__(self, album, path, parent):
262         ModelFolder.__init__(self, album, parent)
263         self.callback_notify_conf = False
264         self.device_name = album
265         self.callback_notify = None
266         self.path = path
267         self.sub_path = None
268
269     def do_load(self):
270         self.sub_path = self.path+"/"+self.device_name
271         aux = Is_Directory(' ')
272         if aux.search(self.sub_path):
273             files = os.listdir(self.sub_path)
274             aux = Is_Directory(' ')
275             for file in files:
276                 if aux.search(self.sub_path+"/"+file):
277                     BrowseByFolder(file, self.sub_path, self)
278                 else:
279                     FileFolder(file,self)
280
281     def send(self,file):
282         self.path = self.sub_path+"/"+file
283         if blue.send_file(self.path):
284             self.callback_notify = CanolaError("File sent successfully")
285         else:
286             self.callback_notify(CanolaError("Connection failed!\
287                     \nDetecting devices again"))
288
289 class FileFolder(ModelFolder):
290     terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/File/FileFolder"
291
292     def __init__(self, name, parent):
293         ModelFolder.__init__(self, name, parent)
294         self.device_name = name
295         self.callback_notify = None
296
297     def do_load(self):
298         return 0
299
300 class Is_Directory:
301     def __init__(self, path):
302         self.path = path
303
304     def search(self, path):
305         try:
306             files = os.listdir(path)
307             return True
308         except OSError:
309             return False