<s> import os <EOL> from PySide import QtGui , QtCore <EOL> from functools import partial <EOL> from neoman import messages as m <EOL> from neoman . storage import settings <EOL> from neoman . exc import ModeSwitchError <EOL> from neoman . model . neo import YubiKeyNeo <EOL> from neoman . model . applet import Applet <EOL> from neoman . model . modes import MODE <EOL> from neoman . view . tabs import TabWidgetWithAbout <EOL> U2F_URL = "<STR_LIT>" + "<STR_LIT>" <EOL> def get_text ( * args , ** kwargs ) : <EOL> flags = ( <EOL> QtCore . Qt . WindowTitleHint | <EOL> QtCore . Qt . WindowSystemMenuHint <EOL> ) <EOL> kwargs [ '<STR_LIT>' ] = flags <EOL> return QtGui . QInputDialog . getText ( * args , ** kwargs ) <EOL> class NeoPage ( TabWidgetWithAbout ) : <EOL> _neo = QtCore . Signal ( YubiKeyNeo ) <EOL> applet = QtCore . Signal ( Applet ) <EOL> def __init__ ( self ) : <EOL> super ( NeoPage , self ) . __init__ ( ) <EOL> self . _tabs = [ ] <EOL> self . _supported = True <EOL> self . _unsupported_tab = UnsupportedTab ( ) <EOL> settings_tab = SettingsTab ( ) <EOL> self . _neo . connect ( settings_tab . set_neo ) <EOL> self . addTab ( settings_tab , m . settings ) <EOL> if QtCore . QCoreApplication . instance ( ) . devmode : <EOL> apps = AppsTab ( self , <NUM_LIT:1> ) <EOL> self . _neo . connect ( apps . set_neo ) <EOL> apps . applet . connect ( self . _set_applet ) <EOL> self . addTab ( apps , m . installed_apps ) <EOL> def addTab ( self , tab , title ) : <EOL> self . _tabs . append ( ( tab , title ) ) <EOL> if self . _supported : <EOL> super ( NeoPage , self ) . addTab ( tab , title ) <EOL> @ QtCore . Slot ( YubiKeyNeo ) <EOL> def setNeo ( self , neo ) : <EOL> self . _supported = neo and neo . supported <EOL> self . clear ( ) <EOL> if self . _supported : <EOL> for ( tab , title ) in self . _tabs : <EOL> super ( NeoPage , self ) . addTab ( tab , title ) <EOL> else : <EOL> super ( NeoPage , self ) . addTab ( self . _unsupported_tab , m . settings ) <EOL> self . _neo . emit ( neo ) <EOL> @ QtCore . Slot ( Applet ) <EOL> def _set_applet ( self , applet ) : <EOL> self . applet . emit ( applet ) <EOL> class UnsupportedTab ( QtGui . QWidget ) : <EOL> def __init__ ( self ) : <EOL> super ( UnsupportedTab , self ) . __init__ ( ) <EOL> layout = QtGui . QVBoxLayout ( ) <EOL> layout . addWidget ( QtGui . QLabel ( m . unsupported_device ) ) <EOL> self . setLayout ( layout ) <EOL> class SettingsTab ( QtGui . QWidget ) : <EOL> def __init__ ( self ) : <EOL> super ( SettingsTab , self ) . __init__ ( ) <EOL> self . _neo = None <EOL> self . _name = QtGui . QLabel ( ) <EOL> self . _serial = QtGui . QLabel ( ) <EOL> self . _firmware = QtGui . QLabel ( ) <EOL> self . _u2f = QtGui . QLabel ( ) <EOL> self . _u2f . setOpenExternalLinks ( True ) <EOL> layout = QtGui . QVBoxLayout ( ) <EOL> name_row = QtGui . QHBoxLayout ( ) <EOL> name_row . addWidget ( self . _name ) <EOL> self . _name_btn = QtGui . QPushButton ( m . change_name ) <EOL> self . _name_btn . clicked . connect ( self . change_name ) <EOL> name_row . addWidget ( self . _name_btn ) <EOL> details_row = QtGui . QHBoxLayout ( ) <EOL> details_row . addWidget ( self . _serial ) <EOL> details_row . addWidget ( self . _firmware ) <EOL> self . _u2f_row = QtGui . QHBoxLayout ( ) <EOL> self . _u2f_row . addWidget ( QtGui . QLabel ( ) ) <EOL> self . _u2f_row . addWidget ( self . _u2f ) <EOL> layout . addLayout ( name_row ) <EOL> layout . addLayout ( details_row ) <EOL> layout . addLayout ( self . _u2f_row ) <EOL> button = QtGui . QPushButton ( m . manage_keys ) <EOL> button . clicked . connect ( self . manage_keys ) <EOL> self . _mode_btn = QtGui . QPushButton ( m . change_mode ) <EOL> self . _mode_btn . clicked . connect ( self . change_mode ) <EOL> layout . addWidget ( self . _mode_btn ) <EOL> self . _mode_note = QtGui . QLabel ( m . note_1 % m . mode_note ) <EOL> self . _mode_note . setWordWrap ( True ) <EOL> layout . addWidget ( self . _mode_note ) <EOL> layout . addStretch ( ) <EOL> self . setLayout ( layout ) <EOL> @ QtCore . Slot ( YubiKeyNeo ) <EOL> def set_neo ( self , neo ) : <EOL> self . _neo = neo <EOL> if not neo : <EOL> return <EOL> self . _name_btn . setDisabled ( neo . serial is None ) <EOL> self . _name . setText ( m . name_1 % neo . name ) <EOL> self . _serial . setText ( m . serial_1 % neo . serial ) <EOL> show_firmware = neo . version != ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _u2f_row . setDirection ( <EOL> QtGui . QBoxLayout . LeftToRight if show_firmware else <EOL> QtGui . QBoxLayout . RightToLeft ) <EOL> self . _firmware . setVisible ( show_firmware ) <EOL> self . _firmware . setText ( m . firmware_1 % '<STR_LIT:.>' . join ( map ( str , neo . version ) ) ) <EOL> if neo . allowed_modes [ <NUM_LIT:2> ] : <EOL> self . _u2f . setText ( m . u2f_1 % m . u2f_supported ) <EOL> else : <EOL> self . _u2f . setText ( m . u2f_1 % m . u2f_not_supported_1 % U2F_URL ) <EOL> self . _mode_btn . setText ( m . change_mode_1 % MODE . name_for_mode ( neo . mode ) ) <EOL> self . _mode_note . setVisible ( neo . version < ( <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def change_name ( self ) : <EOL> name , ok = get_text ( self , m . name , m . change_name_desc , <EOL> text = self . _neo . name ) <EOL> if ok : <EOL> self . _neo . name = name <EOL> self . _name . setText ( m . name_1 % name ) <EOL> def manage_keys ( self ) : <EOL> print m . manage_keys <EOL> def change_mode ( self ) : <EOL> mode = ModeDialog . change_mode ( self . _neo , self ) <EOL> if mode is not None : <EOL> try : <EOL> self . _neo . set_mode ( mode ) <EOL> except ModeSwitchError : <EOL> QtGui . QMessageBox . critical ( self , m . mode_error , <EOL> m . mode_error_desc ) <EOL> return <EOL> self . _mode_btn . setText ( m . change_mode_1 % MODE . name_for_mode ( mode ) ) <EOL> remove_dialog = QtGui . QMessageBox ( self ) <EOL> remove_dialog . setWindowTitle ( m . change_mode ) <EOL> remove_dialog . setIcon ( QtGui . QMessageBox . Information ) <EOL> remove_dialog . setText ( m . remove_device ) <EOL> remove_dialog . setStandardButtons ( QtGui . QMessageBox . NoButton ) <EOL> self . _neo . removed . connect ( remove_dialog . accept ) <EOL> remove_dialog . exec_ ( ) <EOL> class ModeDialog ( QtGui . QDialog ) : <EOL> def __init__ ( self , neo , parent = None ) : <EOL> super ( ModeDialog , self ) . __init__ ( parent ) <EOL> self . setWindowFlags ( self . windowFlags ( ) <EOL> ^ QtCore . Qt . WindowContextHelpButtonHint ) <EOL> layout = QtGui . QVBoxLayout ( ) <EOL> layout . addWidget ( QtGui . QLabel ( m . change_mode_desc ) ) <EOL> boxes = QtGui . QHBoxLayout ( ) <EOL> self . _otp = QtGui . QCheckBox ( m . otp ) <EOL> self . _otp . clicked . connect ( self . _state_changed ) <EOL> boxes . addWidget ( self . _otp ) <EOL> self . _ccid = QtGui . QCheckBox ( m . ccid ) <EOL> self . _ccid . clicked . connect ( self . _state_changed ) <EOL> boxes . addWidget ( self . _ccid ) <EOL> self . _u2f = QtGui . QCheckBox ( m . u2f ) <EOL> self . _u2f . clicked . connect ( self . _state_changed ) <EOL> boxes . addWidget ( self . _u2f ) <EOL> layout . addLayout ( boxes ) <EOL> buttons = QtGui . QDialogButtonBox ( QtGui . QDialogButtonBox . Ok | <EOL> QtGui . QDialogButtonBox . Cancel ) <EOL> buttons . accepted . connect ( self . accept ) <EOL> buttons . rejected . connect ( self . reject ) <EOL> self . _ok = buttons . button ( QtGui . QDialogButtonBox . Ok ) <EOL> layout . addWidget ( buttons ) <EOL> self . setWindowTitle ( m . change_mode ) <EOL> self . setLayout ( layout ) <EOL> allowed = neo . allowed_modes <EOL> self . _otp . setEnabled ( allowed [ <NUM_LIT:0> ] ) <EOL> self . _otp . setVisible ( allowed [ <NUM_LIT:0> ] ) <EOL> self . _ccid . setEnabled ( allowed [ <NUM_LIT:1> ] ) <EOL> self . _ccid . setVisible ( allowed [ <NUM_LIT:1> ] ) <EOL> self . _u2f . setEnabled ( allowed [ <NUM_LIT:2> ] ) <EOL> self . _u2f . setVisible ( allowed [ <NUM_LIT:2> ] ) <EOL> self . mode = neo . mode <EOL> def _state_changed ( self ) : <EOL> self . _ok . setDisabled ( not any ( self . flags ) ) <EOL> @ property <EOL> def flags ( self ) : <EOL> return self . _otp . isChecked ( ) , self . _ccid . isChecked ( ) , self . _u2f . isChecked ( ) <EOL> @ property <EOL> def mode ( self ) : <EOL> return MODE . mode_for_flags ( * self . flags ) <EOL> @ mode . setter <EOL> def mode ( self , value ) : <EOL> otp , ccid , u2f , touch_eject = MODE . flags_for_mode ( value ) <EOL> self . _otp . setChecked ( otp and self . _otp . isEnabled ( ) ) <EOL> self . _ccid . setChecked ( ccid and self . _ccid . isEnabled ( ) ) <EOL> self . _u2f . setChecked ( u2f and self . _u2f . isEnabled ( ) ) <EOL> @ classmethod <EOL> def change_mode ( cls , neo , parent = None ) : <EOL> dialog = cls ( neo , parent ) <EOL> if dialog . exec_ ( ) : <EOL> mode = dialog . mode <EOL> legacy = neo . version < ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> if legacy and mode == <NUM_LIT:2> : <EOL> mode = <NUM_LIT> <EOL> return mode <EOL> else : <EOL> return None <EOL> class AppsTab ( QtGui . QWidget ) : <EOL> applet = QtCore . Signal ( Applet ) <EOL> def __init__ ( self , parent , index ) : <EOL> super ( AppsTab , self ) . __init__ ( ) <EOL> self . parent = parent <EOL> self . index = index <EOL> layout = QtGui . QVBoxLayout ( ) <EOL> self . _apps = [ ] <EOL> self . _apps_list = QtGui . QListView ( ) <EOL> self . _apps_list . setModel ( QtGui . QStringListModel ( [ ] ) ) <EOL> self . _apps_list . setEditTriggers ( QtGui . QListView . NoEditTriggers ) <EOL> self . _apps_list . doubleClicked . connect ( self . open_app ) <EOL> layout . addWidget ( self . _apps_list ) <EOL> self . _install_cap_btn = QtGui . QPushButton ( m . install_cap ) <EOL> self . _install_cap_btn . clicked . connect ( self . install_cap ) <EOL> layout . addWidget ( self . _install_cap_btn ) <EOL> layout . addStretch ( ) <EOL> self . setLayout ( layout ) <EOL> parent . currentChanged . connect ( self . tab_changed ) <EOL> def install_cap ( self ) : <EOL> path = settings . value ( '<STR_LIT>' , None ) <EOL> ( cap , _ ) = QtGui . QFileDialog . getOpenFileName ( self , m . select_cap , <EOL> path , "<STR_LIT>" ) <EOL> if not cap : <EOL> return <EOL> settings . setValue ( '<STR_LIT>' , os . path . dirname ( cap ) ) <EOL> worker = QtCore . QCoreApplication . instance ( ) . worker <EOL> self . _cap = os . path . basename ( cap ) <EOL> worker . post ( m . installing , partial ( self . _neo . install_app , cap ) , <EOL> self . install_done ) <EOL> @ QtCore . Slot ( object ) <EOL> def install_done ( self , status ) : <EOL> if status : <EOL> print status <EOL> QtGui . QMessageBox . warning ( self , m . error_installing , <EOL> m . error_installing_1 % self . _cap ) <EOL> self . set_neo ( self . _neo ) <EOL> def open_app ( self , index ) : <EOL> readable = index . data ( ) <EOL> aid = readable [ readable . rindex ( '<STR_LIT:(>' ) + <NUM_LIT:1> : readable . rindex ( '<STR_LIT:)>' ) ] <EOL> appletmanager = QtCore . QCoreApplication . instance ( ) . appletmanager <EOL> self . applet . emit ( appletmanager . get_applet ( aid ) ) <EOL> def tab_changed ( self , index ) : <EOL> if index != self . index : <EOL> return <EOL> try : <EOL> while self . _neo . locked : <EOL> try : <EOL> self . _neo . unlock ( ) <EOL> except Exception as e : <EOL> del self . _neo . key <EOL> print e <EOL> pw , ok = get_text ( self , m . key_required , m . key_required_desc ) <EOL> if not ok : <EOL> self . parent . setCurrentIndex ( <NUM_LIT:0> ) <EOL> return <EOL> self . _neo . key = pw <EOL> appletmanager = QtCore . QCoreApplication . instance ( ) . appletmanager <EOL> self . _apps = filter ( None , map ( appletmanager . get_applet , <EOL> self . _neo . list_apps ( ) ) ) <EOL> self . _apps_list . model ( ) . setStringList ( <EOL> map ( lambda app : "<STR_LIT>" % ( app . name , app . aid ) , self . _apps ) ) <EOL> except AttributeError : <EOL> pass <EOL> @ QtCore . Slot ( YubiKeyNeo ) <EOL> def set_neo ( self , neo ) : <EOL> self . _neo = neo <EOL> if not neo or not neo . has_ccid : <EOL> self . parent . setTabEnabled ( self . index , False ) <EOL> self . parent . setTabToolTip ( self . index , m . requires_ccid ) <EOL> return <EOL> self . parent . setTabEnabled ( self . index , True ) <EOL> self . parent . setTabToolTip ( self . index , None ) <EOL> if neo . locked : <EOL> try : <EOL> neo . unlock ( ) <EOL> except : <EOL> return <EOL> appletmanager = QtCore . QCoreApplication . instance ( ) . appletmanager <EOL> self . _apps = filter ( None , map ( appletmanager . get_applet , <EOL> neo . list_apps ( ) ) ) <EOL> self . _apps_list . model ( ) . setStringList ( <EOL> map ( lambda app : "<STR_LIT>" % ( app . name , app . aid ) , self . _apps ) ) </s>
