Aller à la documentation de ce fichier.00001
00002
00003
00004 licence={}
00005 licence['en']="""
00006 file deviceListener.py
00007 this file is part of the project scolasync
00008
00009 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
00010
00011 This program is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version3 of the License, or
00014 (at your option) any later version.
00015
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details.
00020
00021 You should have received a copy of the GNU General Public License
00022 along with this program. If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025 import dbus
00026 from PyQt4.QtCore import *
00027 import ownedUsbDisk
00028
00029 class DeviceListener:
00030
00031
00032
00033
00034
00035 def __init__(self, widget=None):
00036 self.bus = dbus.SystemBus()
00037 self.manager = self.bus.get_object(
00038 'org.freedesktop.UDisks',
00039 '/org/freedesktop/UDisks')
00040 self.interface = dbus.Interface(
00041 self.manager,
00042 'org.freedesktop.UDisks')
00043 self.interface.connect_to_signal('DeviceAdded', self.cbAdd)
00044 self.interface.connect_to_signal('DeviceChanged', self.cbChange)
00045 self.interface.connect_to_signal("DeviceRemoved",self.cbDel)
00046 self.connectedVolumes={}
00047 self.widget=widget
00048 self.pollDevices()
00049
00050
00051
00052
00053
00054
00055 def pollDevices(self):
00056 self.connectedVolumes={}
00057 for d in self.interface.EnumerateDevices():
00058 pathUDisks=self.vfatUsbPath(d)
00059 if pathUDisks:
00060 self.connectedVolumes[pathUDisks]=d
00061
00062 return
00063
00064
00065
00066
00067
00068
00069 def cbAdd(self, path):
00070 key=self.vfatUsbPath(path)
00071 if key:
00072 self.connectedVolumes[key]=path
00073
00074 if self.widget:
00075 self.widget.emit(SIGNAL("deviceAdded(QString)"), key)
00076 return
00077
00078
00079
00080
00081
00082
00083 def cbChange(self, path):
00084 key=self.vfatUsbPath(path)
00085 if key and not self.connectedVolumes.has_key(key):
00086 self.connectedVolumes[key]=path
00087
00088 if self.widget:
00089 self.widget.emit(SIGNAL("deviceAdded(QString)"), key)
00090 return
00091
00092
00093
00094
00095
00096
00097
00098 def cbDel(self, path):
00099 key=str(path)
00100 if self.connectedVolumes.has_key(key):
00101 if self.widget:
00102 self.widget.emit(SIGNAL("deviceRemoved(QString)"), key)
00103 self.connectedVolumes.pop(key)
00104
00105 return
00106
00107
00108
00109
00110
00111
00112
00113
00114 def vfatUsbPath(self, dev):
00115 if type(dev)==type(""):
00116 dev=self.connectedVolumes[dev]
00117 o=self.bus.get_object("org.freedesktop.UDisks", dev)
00118 if self.isVfatUsb(o):
00119 return str(dev)
00120 return ""
00121
00122
00123
00124
00125
00126
00127
00128
00129 def identify(self,dev):
00130 if type(dev)==type(""):
00131 dev=self.connectedVolumes[dev]
00132 o=self.bus.get_object("org.freedesktop.UDisks", dev)
00133 i=dbus.Interface(o, "org.freedesktop.DBus.Properties")
00134 stickId, tattoo, uuid = "", "", ""
00135 try:
00136 p=i.Get('','DeviceMountPaths')
00137 if len(p) > 0:
00138 mountPoint=str(p[0])
00139 tattoo=ownedUsbDisk.tattooInDir(mountPoint)
00140 except:
00141 pass
00142 try:
00143 uuid=str(i.Get('','IdUuid'))
00144 except:
00145 pass
00146 try:
00147 stickId=str(i.Get("", "DriveSerial"))
00148 except:
00149 pass
00150 return (stickId, tattoo, uuid)
00151
00152
00153
00154
00155
00156
00157
00158 def isVfatUsb(self, o):
00159 i=dbus.Interface(o, "org.freedesktop.DBus.Properties")
00160 try:
00161 result=str(i.Get('','DriveConnectionInterface'))=="usb" and \
00162 str(i.Get('','IdType'))=="vfat"
00163 except:
00164 result=False
00165 return result
00166
00167