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
00028 class DeviceListener:
00029
00030
00031
00032
00033
00034 def __init__(self, widget=None):
00035 self.bus = dbus.SystemBus()
00036 self.manager = self.bus.get_object(
00037 'org.freedesktop.UDisks',
00038 '/org/freedesktop/UDisks')
00039 self.interface = dbus.Interface(
00040 self.manager,
00041 'org.freedesktop.UDisks')
00042 self.interface.connect_to_signal('DeviceAdded', self.cbAdd)
00043 self.interface.connect_to_signal('DeviceChanged', self.cbChange)
00044 self.interface.connect_to_signal("DeviceRemoved",self.cbDel)
00045 self.connectedVolumes={}
00046 self.widget=widget
00047 self.pollDevices()
00048
00049
00050
00051
00052
00053
00054 def pollDevices(self):
00055 self.connectedVolumes={}
00056 for d in self.interface.EnumerateDevices():
00057 pathUDisks=self.vfatUsbPath(d)
00058 if pathUDisks:
00059 self.connectedVolumes[pathUDisks]=d
00060
00061 return
00062
00063
00064
00065
00066
00067
00068 def cbAdd(self, path):
00069 key=self.vfatUsbPath(path)
00070 if key:
00071 self.connectedVolumes[key]=path
00072
00073 if self.widget:
00074 self.widget.emit(SIGNAL("deviceAdded(QString)"), key)
00075 return
00076
00077
00078
00079
00080
00081
00082 def cbChange(self, path):
00083 key=self.vfatUsbPath(path)
00084 if key and not self.connectedVolumes.has_key(key):
00085 self.connectedVolumes[key]=path
00086
00087 if self.widget:
00088 self.widget.emit(SIGNAL("deviceAdded(QString)"), key)
00089 return
00090
00091
00092
00093
00094
00095
00096
00097 def cbDel(self, path):
00098 key=str(path)
00099 if self.connectedVolumes.has_key(key):
00100 if self.widget:
00101 self.widget.emit(SIGNAL("deviceRemoved(QString)"), key)
00102 self.connectedVolumes.pop(key)
00103
00104 return
00105
00106
00107
00108
00109
00110
00111
00112
00113 def vfatUsbPath(self, dev):
00114 if type(dev)==type(""):
00115 dev=self.connectedVolumes[dev]
00116 o=self.bus.get_object("org.freedesktop.UDisks", dev)
00117 if self.isVfatUsb(o):
00118 return str(dev)
00119 return ""
00120
00121
00122
00123
00124
00125
00126 def isVfatUsb(self, o):
00127 i=dbus.Interface(o, "org.freedesktop.DBus.Properties")
00128 try:
00129 result=str(i.Get('','DriveConnectionInterface'))=="usb" and \
00130 str(i.Get('','IdType'))=="vfat"
00131 except:
00132 result=False
00133 return result
00134
00135