Exemple #1
0
 def __init__(self, parent, reactor, profile, addContactCallback,
              closeCallback):
     QDialog.__init__(self, parent)
     self.ui = Ui_AddContactDialog()
     self.ui.setupUi(self)
     self.reactor = reactor
     self.profile = profile
     self.addContactCallback = addContactCallback
     self.closeCallback = closeCallback
     self.fetchOp = None
     self.fetchError = False
     self.origStatus = self.ui.keyIdStatus.text()
     self.connect(self.ui.keyId, SIGNAL('textChanged(const QString&)'),
                  self._onKeyIdChanged)
     self.connect(self.ui.keyId, SIGNAL('returnPressed()'),
                  self._onKeyIdReturn)
     self.connect(self.ui.keyId, SIGNAL('clicked()'),
                  self._onFetchKeyClicked)
     self.connect(self.ui.fetchKeyButton, SIGNAL('clicked()'),
                  self._onFetchKeyClicked)
     self.connect(self.ui.publicKey, SIGNAL('textChanged()'),
                  self._onPublicKeyChanged)
     self.connect(self.ui.contactName,
                  SIGNAL('textChanged(const QString&)'),
                  self._onContactNameChanged)
     self.connect(self.ui.contactName, SIGNAL('returnPressed()'),
                  self._onContactNameReturn)
     self.connect(self.ui.addContactButton, SIGNAL('clicked()'),
                  self._onAddContactClicked)
     self.connect(self.ui.cancelButton, SIGNAL('clicked()'), self.close)
     self._updateUI()
Exemple #2
0
 def __init__( self, parent, reactor, profile, addContactCallback,
         closeCallback ) :
     QDialog.__init__( self, parent )
     self.ui = Ui_AddContactDialog()
     self.ui.setupUi( self )
     self.reactor = reactor
     self.profile = profile
     self.addContactCallback = addContactCallback
     self.closeCallback = closeCallback
     self.fetchOp = None
     self.fetchError = False
     self.origStatus = self.ui.keyIdStatus.text()
     self.connect( self.ui.keyId,
             SIGNAL('textChanged(const QString&)'),
             self._onKeyIdChanged )
     self.connect( self.ui.keyId, SIGNAL('returnPressed()'),
             self._onKeyIdReturn )
     self.connect( self.ui.keyId, SIGNAL('clicked()'),
             self._onFetchKeyClicked )
     self.connect( self.ui.fetchKeyButton, SIGNAL('clicked()'),
             self._onFetchKeyClicked )
     self.connect( self.ui.publicKey, SIGNAL('textChanged()'),
             self._onPublicKeyChanged )
     self.connect( self.ui.contactName,
             SIGNAL('textChanged(const QString&)'),
             self._onContactNameChanged )
     self.connect( self.ui.contactName, SIGNAL('returnPressed()'),
             self._onContactNameReturn )
     self.connect( self.ui.addContactButton, SIGNAL('clicked()'),
             self._onAddContactClicked )
     self.connect( self.ui.cancelButton, SIGNAL('clicked()'),
             self.close )
     self._updateUI()
Exemple #3
0
class AddContactDialog( QDialog ) :
    def __init__( self, parent, reactor, profile, addContactCallback,
            closeCallback ) :
        QDialog.__init__( self, parent )
        self.ui = Ui_AddContactDialog()
        self.ui.setupUi( self )
        self.reactor = reactor
        self.profile = profile
        self.addContactCallback = addContactCallback
        self.closeCallback = closeCallback
        self.fetchOp = None
        self.fetchError = False
        self.origStatus = self.ui.keyIdStatus.text()
        self.connect( self.ui.keyId,
                SIGNAL('textChanged(const QString&)'),
                self._onKeyIdChanged )
        self.connect( self.ui.keyId, SIGNAL('returnPressed()'),
                self._onKeyIdReturn )
        self.connect( self.ui.keyId, SIGNAL('clicked()'),
                self._onFetchKeyClicked )
        self.connect( self.ui.fetchKeyButton, SIGNAL('clicked()'),
                self._onFetchKeyClicked )
        self.connect( self.ui.publicKey, SIGNAL('textChanged()'),
                self._onPublicKeyChanged )
        self.connect( self.ui.contactName,
                SIGNAL('textChanged(const QString&)'),
                self._onContactNameChanged )
        self.connect( self.ui.contactName, SIGNAL('returnPressed()'),
                self._onContactNameReturn )
        self.connect( self.ui.addContactButton, SIGNAL('clicked()'),
                self._onAddContactClicked )
        self.connect( self.ui.cancelButton, SIGNAL('clicked()'),
                self.close )
        self._updateUI()

    def _showError( self, msg ) :
        QMessageBox.critical( self, 'Error', msg )

    def _readData( self ) :
        self.keyId = unicode(self.ui.keyId.text()).encode('utf8')
        self.pemPublicKey = unicode(self.ui.publicKey.toPlainText()).encode('utf8')
        self.contactName = unicode(self.ui.contactName.text()).encode('utf8')

    def _updateUI( self ) :
        self._readData()
        if self.fetchOp :
            self.ui.keyIdStatus.setText( 'Fetching public key...' )
            self.ui.keyId.setEnabled( False )
            self.ui.fetchKeyButton.setEnabled( False )
            self.ui.publicKey.setEnabled( False )
            self.ui.contactName.setEnabled( False )
            self.ui.addContactButton.setEnabled( False )
            return
        if self.fetchError :
            self.ui.keyIdStatus.setText( 'Error fetching key. Check KeyID.' )
        else :
            self.ui.keyIdStatus.setText( self.origStatus )
        self.ui.keyId.setEnabled( True )
        self.ui.publicKey.setEnabled( True )
        self.ui.contactName.setEnabled( True )
        self.ui.fetchKeyButton.setEnabled( bool(self.keyId) )
        self.ui.addContactButton.setEnabled( bool(self.pemPublicKey)
                and bool(self.contactName) )

    def _onKeyIdChanged( self, text ) :
        self.fetchError = False
        self._updateUI()

    def _onKeyIdReturn( self ) :
        self.ui.fetchKeyButton.click()

    def _fetchKey( self, keyId, callback=None ) :
        def onFetchKey( responseCode, data ) :
            if responseCode != 200 :
                op.notify( None )
                return
            inp = StringIO.StringIO( data )
            name = inp.readline().strip()
            pemPublicKey = inp.read()
            if name and not isValidUserName(name) :
                op.notify( None )
                return
            op.notify( (name,pemPublicKey) )
        httpOp = HttpRequest( self.reactor ).get(
                'http://cspace.in/pubkey/%s' % keyId,
                onFetchKey )
        op = AsyncOp( callback, httpOp.cancel )
        return op

    def _onFetchKeyClicked( self ) :
        try :
            int(self.keyId)
        except ValueError :
            self._showError( 'Invalid KeyID' )
            self.ui.keyId.setFocus()
            self.ui.keyId.selectAll()
            return
        self.ui.publicKey.clear()
        self.ui.contactName.clear()
        self.fetchError = False
        self.fetchOp = self._fetchKey( self.keyId, self._onKeyFetched )
        self._updateUI()

    def _onKeyFetched( self, result ) :
        self.fetchOp = None
        if result is None :
            self.fetchError = True
            self._updateUI()
            self.ui.keyId.setFocus()
            self.ui.keyId.selectAll()
            return
        self.fetchError = False
        name,pemPublicKey = result
        self.ui.contactName.setText( name )
        self.ui.publicKey.setPlainText( pemPublicKey )
        self._updateUI()
        self.ui.contactName.setFocus()
        self.ui.contactName.selectAll()

    def _onPublicKeyChanged( self ) :
        self.fetchError = False
        self._updateUI()

    def _onContactNameChanged( self, text ) :
        self.fetchError = False
        self._updateUI()

    def _onContactNameReturn( self ) :
        self.ui.addContactButton.click()

    def _onAddContactClicked( self ) :
        k = RSAKey()
        try :
            k.fromPEM_PublicKey( self.pemPublicKey )
        except RSAError :
            self._showError( 'Invalid public key.' )
            self.ui.publicKey.setFocus()
            self.ui.publicKey.selectAll()
            return
        if not isValidUserName(self.contactName) :
            self._showError( 'Only lowercase alphabets(a-z), ' +
                    'digits(0-9), and underscore(\'_\') are allowed ' +
                    'in the contact name.' )
            self.ui.contactName.setFocus()
            self.ui.contactName.selectAll()
            return
        contact = self.profile.getContactByPublicKey( k )
        if contact :
            self._showError( 'This public key is already present in ' +
                    'your contact list as \'%s\'.' % contact.name )
            self.ui.publicKey.setFocus()
            self.ui.publicKey.selectAll()
            return
        contact = self.profile.getContactByName( self.contactName )
        if contact :
            self._showError( 'This name is already present in your ' +
                    'contact list.\nPlease choose a different name.' )
            self.ui.contactName.setFocus()
            self.ui.contactName.selectAll()
            return
        contact = Contact( k, self.contactName )
        self.addContactCallback( contact )
        self.accept()

    def done( self, r ) :
        QDialog.done( self, r )
        self.close()

    def closeEvent( self, ev ) :
        if self.fetchOp :
            self.fetchOp.cancel()
            self.fetchOp = None
        if self.closeCallback :
            self.closeCallback()
            self.closeCallback = None
        delaygc( self )
        QDialog.closeEvent( self, ev )
Exemple #4
0
class AddContactDialog(QDialog):
    def __init__(self, parent, reactor, profile, addContactCallback,
                 closeCallback):
        QDialog.__init__(self, parent)
        self.ui = Ui_AddContactDialog()
        self.ui.setupUi(self)
        self.reactor = reactor
        self.profile = profile
        self.addContactCallback = addContactCallback
        self.closeCallback = closeCallback
        self.fetchOp = None
        self.fetchError = False
        self.origStatus = self.ui.keyIdStatus.text()
        self.connect(self.ui.keyId, SIGNAL('textChanged(const QString&)'),
                     self._onKeyIdChanged)
        self.connect(self.ui.keyId, SIGNAL('returnPressed()'),
                     self._onKeyIdReturn)
        self.connect(self.ui.keyId, SIGNAL('clicked()'),
                     self._onFetchKeyClicked)
        self.connect(self.ui.fetchKeyButton, SIGNAL('clicked()'),
                     self._onFetchKeyClicked)
        self.connect(self.ui.publicKey, SIGNAL('textChanged()'),
                     self._onPublicKeyChanged)
        self.connect(self.ui.contactName,
                     SIGNAL('textChanged(const QString&)'),
                     self._onContactNameChanged)
        self.connect(self.ui.contactName, SIGNAL('returnPressed()'),
                     self._onContactNameReturn)
        self.connect(self.ui.addContactButton, SIGNAL('clicked()'),
                     self._onAddContactClicked)
        self.connect(self.ui.cancelButton, SIGNAL('clicked()'), self.close)
        self._updateUI()

    def _showError(self, msg):
        QMessageBox.critical(self, 'Error', msg)

    def _readData(self):
        self.keyId = unicode(self.ui.keyId.text()).encode('utf8')
        self.pemPublicKey = unicode(
            self.ui.publicKey.toPlainText()).encode('utf8')
        self.contactName = unicode(self.ui.contactName.text()).encode('utf8')

    def _updateUI(self):
        self._readData()
        if self.fetchOp:
            self.ui.keyIdStatus.setText('Fetching public key...')
            self.ui.keyId.setEnabled(False)
            self.ui.fetchKeyButton.setEnabled(False)
            self.ui.publicKey.setEnabled(False)
            self.ui.contactName.setEnabled(False)
            self.ui.addContactButton.setEnabled(False)
            return
        if self.fetchError:
            self.ui.keyIdStatus.setText('Error fetching key. Check KeyID.')
        else:
            self.ui.keyIdStatus.setText(self.origStatus)
        self.ui.keyId.setEnabled(True)
        self.ui.publicKey.setEnabled(True)
        self.ui.contactName.setEnabled(True)
        self.ui.fetchKeyButton.setEnabled(bool(self.keyId))
        self.ui.addContactButton.setEnabled(
            bool(self.pemPublicKey) and bool(self.contactName))

    def _onKeyIdChanged(self, text):
        self.fetchError = False
        self._updateUI()

    def _onKeyIdReturn(self):
        self.ui.fetchKeyButton.click()

    def _fetchKey(self, keyId, callback=None):
        def onFetchKey(responseCode, data):
            if responseCode != 200:
                op.notify(None)
                return
            inp = StringIO.StringIO(data)
            name = inp.readline().strip()
            pemPublicKey = inp.read()
            if name and not isValidUserName(name):
                op.notify(None)
                return
            op.notify((name, pemPublicKey))

        httpOp = HttpRequest(self.reactor).get(
            'http://cspace.in/pubkey/%s' % keyId, onFetchKey)
        op = AsyncOp(callback, httpOp.cancel)
        return op

    def _onFetchKeyClicked(self):
        try:
            int(self.keyId)
        except ValueError:
            self._showError('Invalid KeyID')
            self.ui.keyId.setFocus()
            self.ui.keyId.selectAll()
            return
        self.ui.publicKey.clear()
        self.ui.contactName.clear()
        self.fetchError = False
        self.fetchOp = self._fetchKey(self.keyId, self._onKeyFetched)
        self._updateUI()

    def _onKeyFetched(self, result):
        self.fetchOp = None
        if result is None:
            self.fetchError = True
            self._updateUI()
            self.ui.keyId.setFocus()
            self.ui.keyId.selectAll()
            return
        self.fetchError = False
        name, pemPublicKey = result
        self.ui.contactName.setText(name)
        self.ui.publicKey.setPlainText(pemPublicKey)
        self._updateUI()
        self.ui.contactName.setFocus()
        self.ui.contactName.selectAll()

    def _onPublicKeyChanged(self):
        self.fetchError = False
        self._updateUI()

    def _onContactNameChanged(self, text):
        self.fetchError = False
        self._updateUI()

    def _onContactNameReturn(self):
        self.ui.addContactButton.click()

    def _onAddContactClicked(self):
        k = RSAKey()
        try:
            k.fromPEM_PublicKey(self.pemPublicKey)
        except RSAError:
            self._showError('Invalid public key.')
            self.ui.publicKey.setFocus()
            self.ui.publicKey.selectAll()
            return
        if not isValidUserName(self.contactName):
            self._showError('Only lowercase alphabets(a-z), ' +
                            'digits(0-9), and underscore(\'_\') are allowed ' +
                            'in the contact name.')
            self.ui.contactName.setFocus()
            self.ui.contactName.selectAll()
            return
        contact = self.profile.getContactByPublicKey(k)
        if contact:
            self._showError('This public key is already present in ' +
                            'your contact list as \'%s\'.' % contact.name)
            self.ui.publicKey.setFocus()
            self.ui.publicKey.selectAll()
            return
        contact = self.profile.getContactByName(self.contactName)
        if contact:
            self._showError('This name is already present in your ' +
                            'contact list.\nPlease choose a different name.')
            self.ui.contactName.setFocus()
            self.ui.contactName.selectAll()
            return
        contact = Contact(k, self.contactName)
        self.addContactCallback(contact)
        self.accept()

    def done(self, r):
        QDialog.done(self, r)
        self.close()

    def closeEvent(self, ev):
        if self.fetchOp:
            self.fetchOp.cancel()
            self.fetchOp = None
        if self.closeCallback:
            self.closeCallback()
            self.closeCallback = None
        delaygc(self)
        QDialog.closeEvent(self, ev)