Initial commit (Vesion 0.1)
[tablet-suite] / src / pcsdevicemanager.py
1 # low_device_manager module 
2 # Authors: Nicholas Alexander && Otacilio Lacerda
3 # Module responsible for management of devices informations. 
4
5 import pickle
6 import os
7
8 from PyQt4.QtCore import *
9
10 import pcsutils as utils
11 from pcsdeviceinfo import PcsDeviceInfo
12 from pcsdeviceutils import *
13 from ui.tsuigeneralmethods import showMessageBox
14
15 USER_HOST = 'root'
16 HOME = os.path.expanduser("~")
17 DEVICES_FILE = os.path.join(HOME, ".pcsuite/devices/.ip_list")
18
19
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.
23
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.
26
27     """
28     _currentIp = None
29     def __init__(self):
30         QObject.__init__(self)
31         self._deviceList = []
32         
33         # FIXME: initialize this in another place
34         utils.initDirs()
35         self.loadDevices()
36
37         self._currentIp = None
38
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")
46
47     def _addDevice(self, deviceIp):
48         """Add a new device to list connecting to it in the process.
49     
50         Arguments:
51         host_ip -- The IP of the device to connect.
52         
53         """
54         self.loadDevices()
55                     
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"
62         try:
63             deviceInfo.battery = float(queryDeviceBattery(deviceIp))
64         except:
65             return "batteryException"
66     
67         if deviceInfo.battery < 0:
68             deviceInfo.charging = True
69             
70         deviceInfo.storage = queryDeviceStorage(deviceIp)
71         
72         if self.getDevice(deviceIp) != None:
73             return deviceInfo
74         
75         self._deviceList.append(deviceInfo)
76         self.saveDevices()
77         return deviceInfo
78
79     def removeDevice(self, deviceIp):
80         """Remove a Device from list.
81
82         Arguments:
83         device_ip -- The IP  of the device to remove
84
85         """
86         deviceInfo = self.getDevice(deviceIp)
87         if deviceInfo != -1:
88             self._deviceList.remove(deviceInfo)
89             self.saveDevices()
90             return 1
91         else:
92             raise Exception("No device with that ip was found")
93
94     def getDevices(self):
95         """Returns a list with the IP address of all devices in the object's 
96         devices list.
97         
98         """
99         ips = []
100         for deviceInfo in self._deviceList:
101             ips.append(deviceInfo.ip)
102         return ips
103
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)
109         file.close()
110
111     def loadDevices(self):
112         """Loads the list of Device objects from DEVICES_FILE path if possible."""
113
114         if os.path.exists(DEVICES_FILE):
115             file = open(DEVICES_FILE)
116             self._deviceList = pickle.load(file)
117             file.close()
118
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:
123                 return deviceInfo
124         return None
125
126     def setCurrentDevice (self, ip):
127         self._currentIp = ip
128         
129     def getCurrentDevice(self):
130         return self.getDevice(self._currentIp)
131