1 # low_device_manager module
2 # Authors: Nicholas Alexander && Otacilio Lacerda
3 # Module responsible for management of devices informations.
8 from PyQt4.QtCore import *
10 import pcsutils as utils
11 from pcsdeviceinfo import PcsDeviceInfo
12 from pcsdeviceutils import *
13 from ui.tsuigeneralmethods import showMessageBox
16 HOME = os.path.expanduser("~")
17 DEVICES_FILE = os.path.join(HOME, ".pcsuite/devices/.ip_list")
20 class PcsDeviceManager(QObject):
21 """Class responsible for devices management such as adding and removing
22 devices, get batery, memory and name informations and saving Device objects.
24 The DeviceManager holds a list of Devices objects and can save and load this
25 list on a file and retrieve information about each Device.
30 QObject.__init__(self)
33 # FIXME: initialize this in another place
37 self._currentIp = None
39 def _batteryException(self):
40 errorMessage = "Could not get device battery status, check if " +\
41 "python is installed on your device. To get information about " + \
42 "python installation visit: " +\
43 "http://pymaemo.garage.maemo.org/installation.html"
44 showMessageBox(errorMessage,
45 "Error while collecting device information")
47 def _addDevice(self, deviceIp):
48 """Add a new device to list connecting to it in the process.
51 host_ip -- The IP of the device to connect.
56 deviceInfo = PcsDeviceInfo()
57 deviceInfo.ip = deviceIp
58 (deviceInfo.name, deviceInfo.system,
59 deviceInfo.ossoBackup) = queryProductInformation(deviceIp)
60 if deviceInfo.name == "NO INFORMATION":
61 return "connectException"
63 deviceInfo.battery = float(queryDeviceBattery(deviceIp))
65 return "batteryException"
67 if deviceInfo.battery < 0:
68 deviceInfo.charging = True
70 deviceInfo.storage = queryDeviceStorage(deviceIp)
72 if self.getDevice(deviceIp) != None:
75 self._deviceList.append(deviceInfo)
79 def removeDevice(self, deviceIp):
80 """Remove a Device from list.
83 device_ip -- The IP of the device to remove
86 deviceInfo = self.getDevice(deviceIp)
88 self._deviceList.remove(deviceInfo)
92 raise Exception("No device with that ip was found")
95 """Returns a list with the IP address of all devices in the object's
100 for deviceInfo in self._deviceList:
101 ips.append(deviceInfo.ip)
104 def saveDevices(self):
105 """Save the list of Device objects in DEVICES_FILE file."""
106 obj = self._deviceList
107 file = open(DEVICES_FILE, "w")
108 pickle.dump(obj, file)
111 def loadDevices(self):
112 """Loads the list of Device objects from DEVICES_FILE path if possible."""
114 if os.path.exists(DEVICES_FILE):
115 file = open(DEVICES_FILE)
116 self._deviceList = pickle.load(file)
119 def getDevice(self, ip):
120 # Returns the Device object with the provided ip
121 for deviceInfo in self._deviceList:
122 if deviceInfo.ip == ip:
126 def setCurrentDevice (self, ip):
129 def getCurrentDevice(self):
130 return self.getDevice(self._currentIp)