<s> import os <EOL> import rfc822 <EOL> from twisted . python . filepath import FilePath <EOL> import gtk . glade <EOL> from vertex . q2qclient import ClientQ2QService <EOL> from vertex . q2q import Q2QAddress <EOL> class _NullCb : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __call__ ( self , * a , ** kw ) : <EOL> print '<STR_LIT>' , self . name , a , kw <EOL> class _SignalAttacher : <EOL> def __init__ ( self , original ) : <EOL> self . original = original <EOL> def __getitem__ ( self , callbackName ) : <EOL> return getattr ( self . original , callbackName , None ) or _NullCb ( callbackName ) <EOL> GLADE_FILE = os . path . splitext ( __file__ ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> class IdentificationDialog : <EOL> def __init__ ( self , clientService , plug ) : <EOL> self . xml = gtk . glade . XML ( GLADE_FILE , "<STR_LIT>" ) <EOL> self . clientService = clientService <EOL> self . xml . signal_autoconnect ( _SignalAttacher ( self ) ) <EOL> self . addressEntry = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . passwordEntry = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . progressBar = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . progressLabel = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . identifyWindow = self . xml . get_widget ( "<STR_LIT>" ) <EOL> self . cancelButton = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . okButton = self . xml . get_widget ( '<STR_LIT>' ) <EOL> self . plug = plug <EOL> def identifyCancel ( self , event ) : <EOL> self . identifyWindow . destroy ( ) <EOL> def identifyOK ( self , event ) : <EOL> idstr = self . addressEntry . get_text ( ) <EOL> D = self . clientService . authorize ( <EOL> Q2QAddress . fromString ( idstr ) , <EOL> self . passwordEntry . get_text ( ) ) <EOL> sensitiveWidgets = [ self . addressEntry , <EOL> self . passwordEntry , <EOL> self . okButton , <EOL> self . cancelButton ] <EOL> for widget in sensitiveWidgets : <EOL> widget . set_sensitive ( False ) <EOL> self . progressLabel . set_text ( "<STR_LIT>" ) <EOL> def itWorked ( workedNone ) : <EOL> self . identifyWindow . destroy ( ) <EOL> self . plug . setCurrentID ( idstr ) <EOL> def itDidntWork ( error ) : <EOL> self . progressLabel . set_text ( error . getErrorMessage ( ) ) <EOL> for widget in sensitiveWidgets : <EOL> widget . set_sensitive ( True ) <EOL> D . addCallbacks ( itWorked , itDidntWork ) <EOL> class AddContactDialog : <EOL> def __init__ ( self , plug ) : <EOL> self . xml = gtk . glade . XML ( GLADE_FILE , "<STR_LIT>" ) <EOL> self . xml . signal_autoconnect ( _SignalAttacher ( self ) ) <EOL> self . window = self . xml . get_widget ( "<STR_LIT>" ) <EOL> self . window . show_all ( ) <EOL> self . plug = plug <EOL> def doAddContact ( self , evt ) : <EOL> name = self . xml . get_widget ( "<STR_LIT>" ) . get_text ( ) <EOL> addr = self . xml . get_widget ( "<STR_LIT>" ) . get_text ( ) <EOL> self . plug . addBuddy ( name , addr ) <EOL> self . popdownDialog ( ) <EOL> def popdownDialog ( self , evt = None ) : <EOL> self . window . destroy ( ) <EOL> class AcceptConnectionDialog : <EOL> def __init__ ( self , d , From , to , protocol ) : <EOL> self . d = d <EOL> self . xml = gtk . glade . XML ( GLADE_FILE , "<STR_LIT>" ) <EOL> self . xml . signal_autoconnect ( _SignalAttacher ( self ) ) <EOL> self . label = self . xml . get_widget ( "<STR_LIT>" ) <EOL> self . label . set_text ( <EOL> "<STR_LIT>" % ( From , protocol ) ) <EOL> self . window = self . xml . get_widget ( "<STR_LIT>" ) <EOL> self . window . show_all ( ) <EOL> done = False <EOL> def destroyit ( self , evt ) : <EOL> self . window . destroy ( ) <EOL> def acceptConnectionEvt ( self , evt ) : <EOL> self . done = True <EOL> print "<STR_LIT>" <EOL> self . d . callback ( <NUM_LIT:1> ) <EOL> print "<STR_LIT>" <EOL> self . window . destroy ( ) <EOL> def rejectConnectionEvt ( self , evt ) : <EOL> print "<STR_LIT>" <EOL> if not self . done : <EOL> print "<STR_LIT>" <EOL> from twisted . python import failure <EOL> self . d . errback ( failure . Failure ( KeyError ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> print "<STR_LIT:OK>" <EOL> from twisted . internet . protocol import ServerFactory <EOL> from twisted . internet . protocol import Protocol <EOL> class VertexDemoProtocol ( Protocol ) : <EOL> def connectionMade ( self ) : <EOL> print '<STR_LIT>' <EOL> def dataReceived ( self , data ) : <EOL> print '<STR_LIT>' , data <EOL> class VertexFactory ( ServerFactory ) : <EOL> protocol = VertexDemoProtocol <EOL> def __init__ ( self , plug ) : <EOL> self . plug = plug <EOL> def startFactory ( self ) : <EOL> pass <EOL> def stopFactory ( self ) : <EOL> pass <EOL> class BuddyItem : <EOL> def __init__ ( self , plug , alias , q2qaddress ) : <EOL> mi = self . menuItem = gtk . MenuItem ( alias + "<STR_LIT>" + q2qaddress + "<STR_LIT:>>" ) <EOL> mi . connect ( "<STR_LIT>" , self . initiateFileTransfer ) <EOL> mi . show_all ( ) <EOL> self . plug = plug <EOL> self . alias = alias <EOL> self . q2qaddress = q2qaddress <EOL> self . plug . loadedBuddies [ q2qaddress ] = self <EOL> def initiateFileTransfer ( self , evt ) : <EOL> print '<STR_LIT>' + self . alias + self . q2qaddress <EOL> def addToMenu ( self ) : <EOL> self . plug . section . append ( self . menuItem ) <EOL> def removeFromMenu ( self ) : <EOL> self . plug . section . remove ( self . menuItem ) <EOL> from twisted . plugin import IPlugin <EOL> from prime . iprime import IMenuApplication <EOL> from zope . interface import implements <EOL> class PlugEntry : <EOL> implements ( IMenuApplication , IPlugin ) <EOL> def __init__ ( self ) : <EOL> self . xml = gtk . glade . XML ( GLADE_FILE , "<STR_LIT>" ) <EOL> def register ( self , section ) : <EOL> print '<STR_LIT>' <EOL> self . section = section <EOL> workingdir = FilePath ( os . path . expanduser ( "<STR_LIT>" ) ) <EOL> self . clientService = ClientQ2QService ( <EOL> workingdir . child ( "<STR_LIT>" ) . path , <EOL> verifyHook = self . displayVerifyDialog , <EOL> inboundTCPPortnum = <NUM_LIT> , <EOL> udpEnabled = False ) <EOL> self . setCurrentID ( self . clientService . getDefaultFrom ( ) ) <EOL> self . buddiesfile = workingdir . child ( "<STR_LIT>" ) <EOL> self . loadedBuddies = { } <EOL> self . parseBuddies ( ) <EOL> def parseBuddies ( self ) : <EOL> try : <EOL> self . buddyList = rfc822 . AddressList ( self . buddiesfile . open ( ) . read ( ) ) <EOL> except IOError : <EOL> return <EOL> self . clearContactMenu ( ) <EOL> for dispn , addr in self . buddyList : <EOL> if addr not in self . loadedBuddies : <EOL> BuddyItem ( self , dispn , addr ) <EOL> self . buildContactMenu ( ) <EOL> def clearContactMenu ( self ) : <EOL> for bud in self . loadedBuddies . values ( ) : <EOL> bud . removeFromMenu ( ) <EOL> def buildContactMenu ( self ) : <EOL> l = self . loadedBuddies . values ( ) <EOL> l . sort ( key = lambda x : x . alias ) <EOL> l . reverse ( ) <EOL> for bud in l : <EOL> bud . addToMenu ( ) <EOL> def addBuddy ( self , alias , q2qaddr ) : <EOL> temp = self . buddiesfile . temporarySibling ( ) <EOL> try : <EOL> origdata = self . buddiesfile . open ( ) . read ( ) <EOL> except IOError : <EOL> origdata = '<STR_LIT>' <EOL> moredata = '<STR_LIT>' % ( alias , q2qaddr ) <EOL> ftemp = temp . open ( '<STR_LIT:w>' ) <EOL> ftemp . write ( origdata ) <EOL> ftemp . write ( moredata ) <EOL> ftemp . close ( ) <EOL> temp . moveTo ( self . buddiesfile ) <EOL> self . parseBuddies ( ) <EOL> def displayVerifyDialog ( self , From , to , protocol ) : <EOL> from twisted . internet import defer <EOL> d = defer . Deferred ( ) <EOL> AcceptConnectionDialog ( d , From , to , protocol ) <EOL> return d <EOL> def setCurrentID ( self , idName ) : <EOL> if idName is not None : <EOL> currentID = Q2QAddress . fromString ( idName ) <EOL> SL = self . xml . get_widget ( "<STR_LIT>" ) . get_children ( ) [ <NUM_LIT:0> ] . set_label <EOL> def loggedIn ( result ) : <EOL> SL ( str ( currentID ) ) <EOL> self . currentID = currentID <EOL> def notLoggedIn ( error ) : <EOL> SL ( "<STR_LIT>" ) <EOL> self . clientService . listenQ2Q ( currentID , <EOL> { '<STR_LIT>' : VertexFactory ( self ) } , <EOL> "<STR_LIT>" ) . addCallbacks ( <EOL> loggedIn , notLoggedIn ) . addCallback ( <EOL> lambda ign : self . clientService . startService ( ) ) <EOL> def toggleAnimate ( self , event ) : <EOL> if self . animator . animating : <EOL> self . animator . stop ( ) <EOL> else : <EOL> self . animator . start ( ) <EOL> def identifyDialog ( self , event ) : <EOL> IdentificationDialog ( self . clientService , self ) <EOL> def addContact ( self , event ) : <EOL> AddContactDialog ( self ) </s>
