Adding plugin files to version control system.
[bluehi] / plugins / canola-bluehi / bluehi / model.py
diff --git a/plugins/canola-bluehi/bluehi/model.py b/plugins/canola-bluehi/bluehi/model.py
new file mode 100755 (executable)
index 0000000..e8565b5
--- /dev/null
@@ -0,0 +1,309 @@
+"""
+Project: BlueHi: A plugin for Canola
+File name: model.py
+
+Description: This software was developed in Zagaia Project
+
+Copyright (C) 2009
+    Antonio R. de C. Junior  <brankinhu@gmail.com>
+    Henry Miller M. Bilby <henrymiller.engenheiro@gmail.com>
+    Mauricio Figueiredo <mauricio.figueiredo@fucapi.br>
+    Samuel de Oliveira Fagundes <sf.oliveira@gmail.com>
+
+    This program is free software; you can redistribute it and/or modify 
+    it under the terms of the GNU General Public License as published by 
+    the Free Software Foundation; either version 2 of the License, or 
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful, but 
+    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
+    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
+    for more details.
+
+    You should have received a copy of the GNU General Public License along 
+    with this program; if not, write to the Free Software Foundation, Inc., 
+    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+
+import dbus
+import os
+
+from terra.core.manager import Manager
+from terra.core.task import Task
+from terra.core.model import ModelFolder, Model
+from terra.ui.throbber import ThrobberWidget
+from terra.core.threaded_model import ThreadedModelFolder
+from terra.core.threaded_func import ThreadedFunction
+from terra.core.plugin_prefs import PluginPrefs
+from bluetooth_manager import BluetoothManager
+
+
+blue = BluetoothManager()
+manager = Manager()
+
+WaitNotify = manager.get_class("Model/WaitNotify")
+system_properties = manager.get_class("SystemProperties")
+PluginDefaultIcon = manager.get_class("Icon/Plugin")
+canola_prefs = PluginPrefs("settings")
+CanolaError = manager.get_class("Model/Notify/Error")
+
+
+"""Create plugin and icon"""
+class Icon(PluginDefaultIcon):
+    terra_type = "Icon/Folder/Task/Apps/Bluetooth"
+    icon = "icon/main_item/bluehi"
+    plugin = "BlueHI"
+
+class MainFolder(ModelFolder):
+    """
+    This class is loaded when the plugin starts.
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth"
+    terra_task_type = "Task/Folder/Task/Apps/Bluetooth"
+
+    def __init__(self, parent):
+        Task.__init__(self)
+        ModelFolder.__init__(self, "BlueHi", parent)
+        self.callback_notify = None
+
+    def do_load(self):
+        BluetoothDeviceFolder("Search devices", self)
+
+class BluetoothDeviceFolder(ModelFolder):
+    """
+    This class search and detect the devices with bluetooth
+    enabled.
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/FolderDevice"
+
+    def __init__(self, name, parent):
+        ModelFolder.__init__(self, name, parent, None)
+        self.callback_notify = None
+        self.msg = None
+
+    def do_load(self):
+        self.search()
+
+    def search(self, end_callback=None):
+        #Verifica se o bluetooth esta ativado senao estiver, ativa-o
+        #This method verify the status of device's bluetooth,if it's disable, enable it.
+        bus = dbus.SystemBus()
+        bus_adapter = dbus.Interface(bus.get_object('org.bluez',\
+                '/org/bluez/hci0'), 'org.bluez.Adapter')
+
+        if not bus_adapter.IsConnectable():
+            bus_adapter.SetMode("connectable")
+
+        def load():
+            devices = None
+            try:
+                devices = blue.search_devices()
+            except BluetoothError:
+                self.msg = "Bluetooth error."
+
+            return devices
+
+        def end(exception,devices):
+            if self.msg is not None:
+                self.callback_notify(CanolaError(self.msg))
+                self.inform_loaded()
+                return
+
+            if devices is None:
+                self.msg = "Devices not found"
+                self.callback_notify(CanolaError(self.msg))
+                self.inform_loaded()
+                return
+
+            if end_callback:
+                end_callback()
+
+            for list in devices:
+                addr = list[0]
+                name = list[1]
+                BluetoothListDevice([addr, name], self)
+
+            self.inform_loaded()
+
+        self.is_loading = True
+        ThreadedFunction(end,load).start()
+
+class BluetoothListDevice(ModelFolder):
+    """
+    This class list all of devices founded, detect the device's port of
+    comunication, try to connect with other device by bluetooth and create an
+    OBEXClient to transfer the medias.
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/ListDevice"
+
+    def __init__(self, list_device, parent):
+        self.list_device = list_device
+        ModelFolder.__init__(self, list_device[1], parent)
+        self.callback_notify = None
+        self.device_addr = list_device[0]
+
+    def do_load(self):
+
+        def load():
+            port = blue.connect(self.device_addr)
+            return port
+
+        def end(exception,port):
+
+            if port is None:
+                self.callback_notify(CanolaError("Unable to connect to the\
+                        device."))
+                self.inform_loaded()
+                return
+
+            self.inform_loaded()
+            BluetoothAudio("Audio", self)
+            BluetoothVideo("Video", self)
+            BluetoothImage("Image", self)
+
+        self.is_loading = True
+        ThreadedFunction(end,load).start()
+
+class BluetoothAudio(ModelFolder):
+    """
+    This class list audio medias
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Audio"
+
+    def __init__(self, type, parent):
+        ModelFolder.__init__(self, type, parent)
+        self.callback_notify = None
+        self.path_audio = []
+
+    def do_load(self):
+        self.path_audio = canola_prefs["audio"]
+        albuns = os.listdir(self.path_audio[0])
+        aux = Is_Directory(' ')
+        for album in albuns:
+            if aux.search(self.path_audio[0]+'/'+album):
+                BrowseByFolder(album, self.path_audio[0], self)
+            else:
+                FileFolder(album,self)
+
+    def send(self, file):
+        self.path = self.path_audio[0] + "/" + file
+        if blue.send_file(self.path):
+            self.callback_notify = CanolaError("File sent successfully")
+        else:
+            self.callback_notify(CanolaError("Connection failed!\
+                    \nDetecting devices again"))
+
+class BluetoothVideo(ModelFolder):
+    """
+    This class list video medias
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Video"
+
+    def __init__(self, type, parent):
+        ModelFolder.__init__(self, type, parent)
+        self.callback_notify = None
+        self.path_video = []
+
+    def do_load(self):
+        self.path_video = canola_prefs["video"]
+        albuns = os.listdir(self.path_video[0])
+        aux = Is_Directory(' ')
+        for album in albuns:
+            if aux.search(self.path_video[0]+'/'+album):
+                BrowseByFolder(album, self.path_video[0], self)
+            else:
+                FileFolder(album,self)
+
+    def enviar(self,file):
+        self.path = self.path_video[0]+"/"+file
+        if blue.send_file(self.path):
+            self.callback_notify = CanolaError("File sent successfully")
+        else:
+            self.callback_notify(CanolaError("Connection failed!\
+                    \nDetecting devices again"))
+
+class BluetoothImage(ModelFolder, Task):
+    """
+    This class list image medias
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/Image"
+
+    def __init__(self, type, parent):
+        ModelFolder.__init__(self, type, parent)
+        self.callback_notify = None
+        self.path_photo = []
+
+    def do_load(self):
+        self.path_photo=canola_prefs["photo"]
+        albuns=os.listdir(self.path_photo[0])
+        aux = Is_Directory(' ')
+        for album in albuns:
+            if aux.search(self.path_photo[0]+'/'+album):
+                BrowseByFolder(album, self.path_photo[0], self)
+            else:
+                FileFolder(album,self)
+
+    def send(self,file):
+        self.path = self.path_photo[0]+"/"+file
+        if blue.send_file(self.path):
+            self.callback_notify = CanolaError("File sent successfully")
+        else:
+            self.callback_notify(CanolaError("Connection failed!\
+                    \nDetecting devices again"))
+
+class BrowseByFolder(ModelFolder):
+    """
+    Generic class to list folders from audio , video and photo directories
+    """
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/File/BrowseByFolder"
+
+    def __init__(self, album, path, parent):
+        ModelFolder.__init__(self, album, parent)
+        self.callback_notify_conf = False
+        self.device_name = album
+        self.callback_notify = None
+        self.path = path
+        self.sub_path = None
+
+    def do_load(self):
+        self.sub_path = self.path+"/"+self.device_name
+        aux = Is_Directory(' ')
+        if aux.search(self.sub_path):
+            files = os.listdir(self.sub_path)
+            aux = Is_Directory(' ')
+            for file in files:
+                if aux.search(self.sub_path+"/"+file):
+                    BrowseByFolder(file, self.sub_path, self)
+                else:
+                    FileFolder(file,self)
+
+    def send(self,file):
+        self.path = self.sub_path+"/"+file
+        if blue.send_file(self.path):
+            self.callback_notify = CanolaError("File sent successfully")
+        else:
+            self.callback_notify(CanolaError("Connection failed!\
+                    \nDetecting devices again"))
+
+class FileFolder(ModelFolder):
+    terra_type = "Model/Folder/Task/Apps/Bluetooth/Choice/File/FileFolder"
+
+    def __init__(self, name, parent):
+        ModelFolder.__init__(self, name, parent)
+        self.device_name = name
+        self.callback_notify = None
+
+    def do_load(self):
+       return 0
+
+class Is_Directory:
+    def __init__(self, path):
+        self.path = path
+
+    def search(self, path):
+        try:
+            files = os.listdir(path)
+            return True
+        except OSError:
+            return False