Initial commit (Vesion 0.1)
[tablet-suite] / src / .svn / text-base / pcsdevicemanager.py.svn-base
diff --git a/src/.svn/text-base/pcsdevicemanager.py.svn-base b/src/.svn/text-base/pcsdevicemanager.py.svn-base
new file mode 100644 (file)
index 0000000..a63160f
--- /dev/null
@@ -0,0 +1,131 @@
+# low_device_manager module 
+# Authors: Nicholas Alexander && Otacilio Lacerda
+# Module responsible for management of devices informations. 
+
+import pickle
+import os
+
+from PyQt4.QtCore import *
+
+import pcsutils as utils
+from pcsdeviceinfo import PcsDeviceInfo
+from pcsdeviceutils import *
+from ui.tsuigeneralmethods import showMessageBox
+
+USER_HOST = 'root'
+HOME = os.path.expanduser("~")
+DEVICES_FILE = os.path.join(HOME, ".pcsuite/devices/.ip_list")
+
+
+class PcsDeviceManager(QObject):
+    """Class responsible for devices management such as adding and removing
+    devices, get batery, memory and name informations and saving Device objects.
+
+    The DeviceManager holds a list of Devices objects and can save and load this
+    list on a file and retrieve information about each Device.
+
+    """
+    _currentIp = None
+    def __init__(self):
+        QObject.__init__(self)
+        self._deviceList = []
+        
+        # FIXME: initialize this in another place
+        utils.initDirs()
+        self.loadDevices()
+
+        self._currentIp = None
+
+    def _batteryException(self):
+        errorMessage = "Could not get device battery status, check if " +\
+            "python is installed on your device. To get information about " + \
+            "python installation visit: " +\
+            "http://pymaemo.garage.maemo.org/installation.html"
+        showMessageBox(errorMessage,
+                              "Error while collecting device information")
+
+    def _addDevice(self, deviceIp):
+        """Add a new device to list connecting to it in the process.
+    
+        Arguments:
+        host_ip -- The IP of the device to connect.
+        
+        """
+        self.loadDevices()
+                    
+        deviceInfo = PcsDeviceInfo()
+        deviceInfo.ip = deviceIp
+        (deviceInfo.name, deviceInfo.system,
+         deviceInfo.ossoBackup) = queryProductInformation(deviceIp) 
+        if deviceInfo.name == "NO INFORMATION":
+            return "connectException"
+        try:
+            deviceInfo.battery = float(queryDeviceBattery(deviceIp))
+        except:
+            return "batteryException"
+    
+        if deviceInfo.battery < 0:
+            deviceInfo.charging = True
+            
+        deviceInfo.storage = queryDeviceStorage(deviceIp)
+        
+        if self.getDevice(deviceIp) != None:
+            return deviceInfo
+        
+        self._deviceList.append(deviceInfo)
+        self.saveDevices()
+        return deviceInfo
+
+    def removeDevice(self, deviceIp):
+        """Remove a Device from list.
+
+        Arguments:
+        device_ip -- The IP  of the device to remove
+
+        """
+        deviceInfo = self.getDevice(deviceIp)
+        if deviceInfo != -1:
+            self._deviceList.remove(deviceInfo)
+            self.saveDevices()
+            return 1
+        else:
+            raise Exception("No device with that ip was found")
+
+    def getDevices(self):
+        """Returns a list with the IP address of all devices in the object's 
+        devices list.
+        
+        """
+        ips = []
+        for deviceInfo in self._deviceList:
+            ips.append(deviceInfo.ip)
+        return ips
+
+    def saveDevices(self):
+        """Save the list of Device objects in DEVICES_FILE file."""
+        obj = self._deviceList
+        file = open(DEVICES_FILE, "w")
+        pickle.dump(obj, file)
+        file.close()
+
+    def loadDevices(self):
+        """Loads the list of Device objects from DEVICES_FILE path if possible."""
+
+        if os.path.exists(DEVICES_FILE):
+            file = open(DEVICES_FILE)
+            self._deviceList = pickle.load(file)
+            file.close()
+
+    def getDevice(self, ip):
+        # Returns the Device object with the provided ip
+        for deviceInfo in self._deviceList:
+            if deviceInfo.ip == ip:
+                return deviceInfo
+        return None
+
+    def setCurrentDevice (self, ip):
+        self._currentIp = ip
+        
+    def getCurrentDevice(self):
+        return self.getDevice(self._currentIp)
+