/usr/share/pyshared/smartcard/wx/CardAndReaderTreePanel.py is in python-pyscard 1.6.12.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | """wxPython panel display cards/readers as a TreeCtrl.
__author__ = "gemalto http://www.gemalto.com"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
# smartcard imports
from threading import RLock
from smartcard.Exceptions import CardConnectionException, NoCardException
from smartcard.ReaderMonitoring import ReaderMonitor, ReaderObserver
from smartcard.CardMonitoring import CardMonitor, CardObserver
from smartcard.util import toHexString
import smartcard.wx.SimpleSCardApp
from smartcard.wx import ICO_SMARTCARD, ICO_READER
# wxPython GUI modules (http://www.wxpython.org)
import wx
class BaseCardTreeCtrl(wx.TreeCtrl):
"""Base class for the smart card and reader tree controls."""
def __init__(self,
parent,
ID=wx.NewId(),
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=0,
clientpanel=None):
"""Constructor. Initializes a smartcard or reader tree control."""
wx.TreeCtrl.__init__(
self, parent, ID, pos, size, wx.TR_SINGLE | wx.TR_NO_BUTTONS)
self.clientpanel = clientpanel
self.parent = parent
isz = (16, 16)
il = wx.ImageList(isz[0], isz[1])
self.capindex = il.Add(
wx.ArtProvider_GetBitmap(wx.ART_HELP_BOOK, wx.ART_OTHER, isz))
self.fldrindex = il.Add(
wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz))
self.fldropenindex = il.Add(
wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz))
if None != ICO_SMARTCARD:
self.cardimageindex = il.Add(
wx.Bitmap(ICO_SMARTCARD, wx.BITMAP_TYPE_ICO))
if None != ICO_READER:
self.readerimageindex = il.Add(
wx.Bitmap(ICO_READER, wx.BITMAP_TYPE_ICO))
self.il = il
self.SetImageList(self.il)
def Repaint(self):
self.Refresh()
wx.PostEvent(self, wx.PaintEvent())
class CardTreeCtrl(BaseCardTreeCtrl):
"""The CardTreeCtrl monitors inserted cards and notifies the
application client dialog of any card activation."""
def __init__(self,
parent,
ID=wx.NewId(),
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=0,
clientpanel=None):
"""Constructor. Create a smartcard tree control."""
BaseCardTreeCtrl.__init__(self, parent, ID, pos, size,
wx.TR_SINGLE | wx.TR_NO_BUTTONS,
clientpanel)
self.root = self.AddRoot("Smartcards")
self.SetPyData(self.root, None)
self.SetItemImage(self.root, self.fldrindex, wx.TreeItemIcon_Normal)
self.SetItemImage(
self.root, self.fldropenindex, wx.TreeItemIcon_Expanded)
self.Expand(self.root)
def OnAddCards(self, addedcards):
"""Called when a card is inserted.
Adds a smart card to the smartcards tree."""
parentnode = self.root
for cardtoadd in addedcards:
childCard = self.AppendItem(parentnode, toHexString(cardtoadd.atr))
self.SetItemText(childCard, toHexString(cardtoadd.atr))
self.SetPyData(childCard, cardtoadd)
self.SetItemImage(
childCard, self.cardimageindex, wx.TreeItemIcon_Normal)
self.SetItemImage(
childCard, self.cardimageindex, wx.TreeItemIcon_Expanded)
self.Expand(childCard)
self.Expand(self.root)
self.EnsureVisible(self.root)
self.Repaint()
def OnRemoveCards(self, removedcards):
"""Called when a card is removed.
Removes a card from the tree."""
parentnode = self.root
for cardtoremove in removedcards:
(childCard, cookie) = self.GetFirstChild(parentnode)
while childCard.IsOk():
if self.GetItemText(childCard) == \
toHexString(cardtoremove.atr):
self.Delete(childCard)
(childCard, cookie) = self.GetNextChild(parentnode, cookie)
self.Expand(self.root)
self.EnsureVisible(self.root)
self.Repaint()
class ReaderTreeCtrl(BaseCardTreeCtrl):
"""The ReaderTreeCtrl monitors inserted cards and readers and notifies the
application client dialog of any card activation."""
def __init__(self,
parent,
ID=wx.NewId(),
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=0,
clientpanel=None):
"""Constructor. Create a reader tree control."""
BaseCardTreeCtrl.__init__(
self,
parent,
ID,
pos,
size,
wx.TR_SINGLE | wx.TR_NO_BUTTONS,
clientpanel)
self.mutex = RLock()
self.root = self.AddRoot("Smartcard Readers")
self.SetPyData(self.root, None)
self.SetItemImage(
self.root, self.fldrindex, wx.TreeItemIcon_Normal)
self.SetItemImage(
self.root, self.fldropenindex, wx.TreeItemIcon_Expanded)
self.Expand(self.root)
def AddATR(self, readernode, atr):
"""Add an ATR to a reader node."""
capchild = self.AppendItem(readernode, atr)
self.SetPyData(capchild, None)
self.SetItemImage(
capchild, self.cardimageindex, wx.TreeItemIcon_Normal)
self.SetItemImage(
capchild, self.cardimageindex, wx.TreeItemIcon_Expanded)
self.Expand(capchild)
return capchild
def GetATR(self, reader):
"""Return the ATR of the card inserted into the reader."""
atr = "no card inserted"
try:
if not type(reader) is str:
connection = reader.createConnection()
connection.connect()
atr = toHexString(connection.getATR())
connection.disconnect()
except NoCardException:
pass
except CardConnectionException:
pass
return atr
def OnAddCards(self, addedcards):
"""Called when a card is inserted.
Adds the smart card child to the reader node."""
self.mutex.acquire()
try:
parentnode = self.root
for cardtoadd in addedcards:
(childReader, cookie) = self.GetFirstChild(parentnode)
found = False
while childReader.IsOk() and not found:
if self.GetItemText(childReader) == str(cardtoadd.reader):
(childCard, cookie2) = self.GetFirstChild(childReader)
self.SetItemText(childCard, toHexString(cardtoadd.atr))
self.SetPyData(childCard, cardtoadd)
found = True
else:
(childReader, cookie) = self.GetNextChild(
parentnode, cookie)
# reader was not found, add reader node
# this happens when card monitoring thread signals
# added cards before reader monitoring thread signals
# added readers
if not found:
childReader = self.AppendItem(
parentnode, str(cardtoadd.reader))
self.SetPyData(childReader, cardtoadd.reader)
self.SetItemImage(
childReader,
self.readerimageindex,
wx.TreeItemIcon_Normal)
self.SetItemImage(
childReader,
self.readerimageindex,
wx.TreeItemIcon_Expanded)
childCard = self.AddATR(
childReader, toHexString(cardtoadd.atr))
self.SetPyData(childCard, cardtoadd)
self.Expand(childReader)
self.Expand(self.root)
finally:
self.mutex.release()
self.EnsureVisible(self.root)
self.Repaint()
def OnAddReaders(self, addedreaders):
"""Called when a reader is inserted.
Adds the smart card reader to the smartcard readers tree."""
self.mutex.acquire()
try:
parentnode = self.root
for readertoadd in addedreaders:
# is the reader already here?
found = False
(childReader, cookie) = self.GetFirstChild(parentnode)
while childReader.IsOk() and not found:
if self.GetItemText(childReader) == str(readertoadd):
found = True
else:
(childReader, cookie) = self.GetNextChild(
parentnode, cookie)
if not found:
childReader = self.AppendItem(parentnode, str(readertoadd))
self.SetPyData(childReader, readertoadd)
self.SetItemImage(
childReader,
self.readerimageindex,
wx.TreeItemIcon_Normal)
self.SetItemImage(
childReader,
self.readerimageindex,
wx.TreeItemIcon_Expanded)
self.AddATR(
childReader,
self.GetATR(readertoadd))
self.Expand(childReader)
self.Expand(self.root)
finally:
self.mutex.release()
self.EnsureVisible(self.root)
self.Repaint()
def OnRemoveCards(self, removedcards):
"""Called when a card is removed.
Removes the card from the tree."""
self.mutex.acquire()
try:
parentnode = self.root
for cardtoremove in removedcards:
(childReader, cookie) = self.GetFirstChild(parentnode)
found = False
while childReader.IsOk() and not found:
if self.GetItemText(childReader) == \
str(cardtoremove.reader):
(childCard, cookie2) = self.GetFirstChild(childReader)
self.SetItemText(childCard, 'no card inserted')
found = True
else:
(childReader, cookie) = \
self.GetNextChild(parentnode, cookie)
self.Expand(self.root)
finally:
self.mutex.release()
self.EnsureVisible(self.root)
self.Repaint()
def OnRemoveReaders(self, removedreaders):
"""Called when a reader is removed.
Removes the reader from the smartcard readers tree."""
self.mutex.acquire()
try:
parentnode = self.root
for readertoremove in removedreaders:
(childReader, cookie) = self.GetFirstChild(parentnode)
while childReader.IsOk():
if self.GetItemText(childReader) == str(readertoremove):
self.Delete(childReader)
else:
(childReader, cookie) = \
self.GetNextChild(parentnode, cookie)
self.Expand(self.root)
finally:
self.mutex.release()
self.EnsureVisible(self.root)
self.Repaint()
class CardAndReaderTreePanel(wx.Panel):
"""Panel containing the smart card and reader tree controls."""
class _CardObserver(CardObserver):
"""Inner CardObserver. Gets notified of card insertion
removal by the CardMonitor."""
def __init__(self, cardtreectrl):
self.cardtreectrl = cardtreectrl
def update(self, observable, (addedcards, removedcards)):
"""CardObserver callback that is notified
when cards are added or removed."""
self.cardtreectrl.OnRemoveCards(removedcards)
self.cardtreectrl.OnAddCards(addedcards)
class _ReaderObserver(ReaderObserver):
"""Inner ReaderObserver. Gets notified of reader insertion/removal
by the ReaderMonitor."""
def __init__(self, readertreectrl):
self.readertreectrl = readertreectrl
def update(self, observable, (addedreaders, removedreaders)):
"""ReaderObserver callback that is notified when
readers are added or removed."""
self.readertreectrl.OnRemoveReaders(removedreaders)
self.readertreectrl.OnAddReaders(addedreaders)
def __init__(self, parent, appstyle, clientpanel):
"""Constructor. Create a smartcard and reader tree control on the
left-hand side of the application main frame.
parent: the tree panel parent
appstyle: a combination of the following styles (bitwise or |)
TR_SMARTCARD: display a smartcard tree panel
TR_READER: display a reader tree panel
default is TR_DEFAULT = TR_SMARTCARD
clientpanel: the client panel to notify of smartcard and reader events
"""
wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
sizer = wx.BoxSizer(wx.VERTICAL)
# create the smartcard tree
if appstyle & smartcard.wx.SimpleSCardApp.TR_SMARTCARD:
self.cardtreectrl = CardTreeCtrl(self, clientpanel=clientpanel)
# create the smartcard insertion observer
self.cardtreecardobserver = self._CardObserver(self.cardtreectrl)
# register as a CardObserver; we will ge
# notified of added/removed cards
self.cardmonitor = CardMonitor()
self.cardmonitor.addObserver(self.cardtreecardobserver)
sizer.Add(
self.cardtreectrl, flag=wx.EXPAND | wx.ALL, proportion=1)
# create the reader tree
if appstyle & smartcard.wx.SimpleSCardApp.TR_READER:
self.readertreectrl = ReaderTreeCtrl(
self, clientpanel=clientpanel)
# create the reader insertion observer
self.readertreereaderobserver = self._ReaderObserver(
self.readertreectrl)
# register as a ReaderObserver; we will ge
# notified of added/removed readers
self.readermonitor = ReaderMonitor()
self.readermonitor.addObserver(self.readertreereaderobserver)
# create the smartcard insertion observer
self.readertreecardobserver = self._CardObserver(
self.readertreectrl)
# register as a CardObserver; we will get
# notified of added/removed cards
self.cardmonitor = CardMonitor()
self.cardmonitor.addObserver(self.readertreecardobserver)
sizer.Add(
self.readertreectrl, flag=wx.EXPAND | wx.ALL, proportion=1)
self.SetSizer(sizer)
self.SetAutoLayout(True)
def OnDestroy(self, event):
"""Called on panel destruction."""
# deregister observers
if hasattr(self, cardmonitor):
self.cardmonitor.deleteObserver(self.cardtreecardobserver)
if hasattr(self, readermonitor):
self.readermonitor.deleteObserver(self.readertreereaderobserver)
self.cardmonitor.deleteObserver(self.readertreecardobserver)
event.Skip()
|