[Syncropated-commits] r169 - trunk/src

zimmerle at garage.maemo.org zimmerle at garage.maemo.org
Tue Jan 30 02:35:23 EET 2007


Author: zimmerle
Date: 2007-01-30 02:35:22 +0200 (Tue, 30 Jan 2007)
New Revision: 169

Added:
   trunk/src/DeviceDetection.py
Log:
DeviceDetection.py first commit

Added: trunk/src/DeviceDetection.py
===================================================================
--- trunk/src/DeviceDetection.py	2007-01-30 00:28:46 UTC (rev 168)
+++ trunk/src/DeviceDetection.py	2007-01-30 00:35:22 UTC (rev 169)
@@ -0,0 +1,227 @@
+#!/usr/bin/python
+
+# DeviceDetection.py
+#
+#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
+#
+#  Author: Felipe Zimmerle <felipe at zimmerle.org>
+#          Kenneth Rohde Christiansen <kenneth.christiansen at 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 gobject
+import sys
+from Logger import Logger
+from Configuration import Configuration
+
+config = Configuration()
+
+
+log = Logger()
+
+class DeviceDetection (gobject.GObject):
+	"""
+	Device detection skel, OS independent
+
+	"""
+
+	__engine = None
+	__instance = None
+	__old_device_list = []
+	__dryrun = None
+
+	__gsignals__ = {
+		'device_detection'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+			(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING))
+	}
+
+
+	def __new__(self, dryrun='dummy'):
+
+		if self.__instance == None:
+			self.__instance = super(self, DeviceDetection).__new__(self)
+
+		return self.__instance
+
+
+	def __init__(self, dryrun=False):
+		"""
+		Constructor
+
+		Starts the correct engine, respecting the O.S.
+
+		"""
+		gobject.GObject.__init__(self)
+		self.__dryrun = dryrun
+		if sys.platform == "win32":
+			self.__engine = DeviceDetectionEngineWindows()
+		else:
+			self.__engine = DeviceDetectionEngineLinux()
+
+
+	def get_devices_list(self):
+		"""
+		Return the device list.
+
+		"""
+		devices = []
+		try:
+			devices = self.__engine.get_device_list()
+		except:
+			pass
+		if self.__dryrun:
+			devices.append(('fake', '/tmp/'))
+		return devices
+
+
+	def start(self):
+		"""
+		Starts the device watch dog.
+
+		"""
+		devices = self.get_devices_list()
+		log.debug('DeviceDetection :: Ok, device detection readed. -> ' + str(devices))
+
+
+		for i in devices:
+			log.debug('DeviceDetection :: Emiting signal.... ' + str(i[0]))
+			self.__old_device_list.append((i[0], i[1]))
+			self.emit('device_detection', 'add', i[0], i[1])
+
+		gobject.timeout_add(int(config.getValue('devices_watch_dog_time')),
+			self.devices_watch_dog)
+
+
+	def devices_watch_dog(self):
+		"""
+		Devices watch dog, if something change, emit signals telling everybory 
+		whats happened.
+
+		"""
+		devices = self.get_devices_list()
+
+		for i in devices:
+			old = False
+			log.debug('DeviceDetection :: WatchDog :: Found device: ' + str(i) )
+			for j in self.__old_device_list:
+				if j[0] == i[0] and j[1] == i[1]:
+					log.debug('DeviceDetection :: WatchDog :: ' +
+						'\'- It\'s a old device !!' + str(j[0])  + ' = ' + 
+						str(i[0]))
+					old = True
+
+			if not old:
+				log.debug('DeviceDetection :: WatchDog ::  `\'- It\'s a new device')
+				self.emit('device_detection', 'add', i[0], i[1])
+				self.__old_device_list.append(i)
+
+		for i in self.__old_device_list:
+			removed = True
+			for j in devices:
+				if i[0] == j[0]:
+					if j[0] == i[0] and j[1] == i[1]:
+						removed = False
+
+			if removed:
+				self.__old_device_list.remove(i)
+				log.debug('DeviceDetection :: WatchDog :: REMOVED device: ' + str(i))
+				self.emit('device_detection', 'del', i[0], i[1])
+
+		return str(True) == config.getValue('devices_watch_dog')
+
+
+
+class DeviceDetectionEngineWindows(gobject.GObject):
+	"""
+	Device detection MS-Windows engine.
+
+	"""
+	def get_device_list(self):
+		log.debug('DeviceDetection :: Listing know devices...')
+		devices = []
+		return devices
+
+	def get_free_space(self, drive):
+		return 0;
+
+
+class DeviceDetectionEngineLinux(gobject.GObject):
+	"""
+	Device detection Linux Engine.
+
+	"""
+
+	def get_device_list(self):
+
+		log.debug('DeviceDetection :: Listing know devices...')
+
+		import dbus
+
+		devices = []
+
+		bus = dbus.SystemBus()
+		hal_manager_obj = bus.get_object("org.freedesktop.Hal", 
+			"/org/freedesktop/Hal/Manager")
+		hal_manager = dbus.Interface(hal_manager_obj,
+			"org.freedesktop.Hal.Manager")
+
+		volume_udi_list = hal_manager.FindDeviceByCapability ('volume')
+
+		for udi in volume_udi_list:
+			volume = bus.get_object("org.freedesktop.Hal", udi)
+			fstype = volume.GetProperty ('volume.fstype')
+			mount_point = volume.GetProperty ('volume.mount_point')
+			is_mounted = volume.GetProperty ('volume.is_mounted')
+			is_mounted_read_only = volume.GetProperty ('volume.is_mounted_read_only')
+
+			device_file = volume.GetProperty ('block.device')
+			storage_udi = volume.GetProperty ('block.storage_device')
+
+			storage = bus.get_object("org.freedesktop.Hal", storage_udi)
+			drive_type = storage.GetProperty ('storage.bus')
+
+			# If it's not mounted or not usb drive, we simple dont care.
+			if drive_type != "usb" or not is_mounted:
+				continue
+
+			info_udi = volume.GetProperty ('info.parent')
+			info = bus.get_object("org.freedesktop.Hal", info_udi)
+
+			# Only accept N770 and Portable Audio Players
+			try:
+				player_type = info.GetProperty ('portable_audio_player.type')
+			except dbus.DBusException, e:
+				if info.GetProperty ('info.product') == "770":
+					player_type = "770"
+					pass
+				else:
+					continue
+
+			# Ignore iPods as they work differently than other Mass Storage Devices
+			if player_type == "ipod":
+				continue
+
+			log.debug('DeviceDetection :: Detected the follow device: ' + str(player_type) + 
+				' monted on: ' + str(mount_point) + '. ')
+			devices.append((player_type, mount_point))
+
+		return devices
+
+	def get_free_space(self, mount_point):
+		return os.statvfs(mount_point)[2]
+
+
+



More information about the Syncropated-commits mailing list