"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/external_libraries/lxqt_wallet/frontend/changepassworddialog.cpp" (12 Sep 2019, 9075 Bytes) of package /linux/misc/zuluCrypt-5.7.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "changepassworddialog.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * copyright: 2013-2015
    3  * name : Francis Banyikwa
    4  * email: mhogomchungu@gmail.com
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  *
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in
   14  *    the documentation and/or other materials provided with the
   15  *    distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   21  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
   23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  */
   30 
   31 #include "changepassworddialog.h"
   32 #include "ui_changepassworddialog.h"
   33 
   34 #include <utility>
   35 
   36 namespace Task = LXQt::Wallet::Task ;
   37 
   38 LXQt::Wallet::changePassWordDialog::changePassWordDialog( QWidget * parent,const QString& walletName,const QString& applicationName ):
   39     QDialog( parent ),m_ui( new Ui::changePassWordDialog ),m_walletName( walletName ),
   40     m_applicationName( applicationName )
   41 {
   42     m_ui->setupUi( this ) ;
   43 
   44     this->setFixedSize( this->size() ) ;
   45 
   46     if( parent ){
   47 
   48         this->setWindowIcon( parent->windowIcon() ) ;
   49     }
   50 
   51     connect( m_ui->pushButtonCancel,SIGNAL( clicked() ),this,SLOT( cancel() ) ) ;
   52 
   53     m_ui->pushButtonOK->setVisible( false ) ;
   54     m_ui->textEdit->setVisible( false ) ;
   55     m_ui->textEdit_2->setVisible( false ) ;
   56 
   57     m_walletPassWordChanged = false ;
   58 
   59     this->installEventFilter( this ) ;
   60 }
   61 
   62 bool LXQt::Wallet::changePassWordDialog::eventFilter( QObject * watched,QEvent * event )
   63 {
   64     if( watched == this ){
   65 
   66         if( event->type() == QEvent::KeyPress ){
   67 
   68             auto keyEvent = static_cast< QKeyEvent* >( event ) ;
   69 
   70             if( keyEvent->key() == Qt::Key_Escape ){
   71 
   72                 this->HideUI() ;
   73 
   74                 return true ;
   75             }
   76         }
   77     }
   78 
   79     return false ;
   80 }
   81 
   82 void LXQt::Wallet::changePassWordDialog::HideUI()
   83 {
   84     m_change( m_walletPassWordChanged ) ;
   85 
   86     this->hide() ;
   87 
   88     this->deleteLater() ;
   89 }
   90 
   91 void LXQt::Wallet::changePassWordDialog::ShowUI_1( std::function< void( bool ) >&& change )
   92 {
   93     m_change = std::move( change ) ;
   94 
   95     m_banner = m_ui->textEdit->toHtml().arg( m_applicationName,m_walletName ) ;
   96     m_ui->label->setText( m_banner ) ;
   97 
   98     connect( m_ui->pushButtonChange,SIGNAL( clicked() ),this,SLOT( change() ) ) ;
   99     connect( m_ui->pushButtonOK,SIGNAL( clicked() ),this,SLOT( ok() ) ) ;
  100 
  101     this->show() ;
  102 }
  103 
  104 void LXQt::Wallet::changePassWordDialog::ShowUI( std::function< void( const QString&,bool ) >&& create )
  105 {
  106     m_create = std::move( create ) ;
  107 
  108     this->setWindowTitle( tr( "Create a new wallet" ) ) ;
  109 
  110     m_ui->pushButtonChange->setText( tr( "Create" ) ) ;
  111 
  112     connect( m_ui->pushButtonChange,SIGNAL( clicked() ),this,SLOT( create() ) ) ;
  113     connect( m_ui->pushButtonOK,SIGNAL( clicked() ),this,SLOT( ok_1() ) ) ;
  114 
  115     m_banner = m_ui->textEdit_2->toHtml().arg( m_applicationName,m_walletName ) ;
  116     m_ui->label->setText( m_banner ) ;
  117 
  118     m_ui->label_2->setEnabled( false ) ;
  119     m_ui->lineEditCurrentPassWord->setEnabled( false ) ;
  120 
  121     this->show() ;
  122 }
  123 
  124 LXQt::Wallet::changePassWordDialog::~changePassWordDialog()
  125 {
  126     delete m_ui ;
  127 }
  128 
  129 void LXQt::Wallet::changePassWordDialog::create()
  130 {
  131     if( m_ui->lineEditNewPassWord->text() == m_ui->lineEditNewPassWord_2->text() ){
  132 
  133         m_create( m_ui->lineEditNewPassWord->text(),true ) ;
  134         this->HideUI() ;
  135     }else{
  136         m_ui->label->setText( tr( "Passwords do not match" ) ) ;
  137         m_ui->pushButtonOK->setVisible( true ) ;
  138         m_ui->pushButtonCancel->setVisible( false ) ;
  139         m_ui->pushButtonChange->setVisible( false ) ;
  140 
  141         m_ui->lineEditCurrentPassWord->setEnabled( false ) ;
  142         m_ui->lineEditNewPassWord->setEnabled( false ) ;
  143         m_ui->lineEditNewPassWord_2->setEnabled( false ) ;
  144         m_ui->label->setEnabled( true ) ;
  145         m_ui->label_2->setEnabled( false ) ;
  146         m_ui->label_2->setEnabled( false ) ;
  147         m_ui->label_3->setEnabled( false ) ;
  148         m_ui->label_4->setEnabled( false ) ;
  149     }
  150 }
  151 
  152 void LXQt::Wallet::changePassWordDialog::change()
  153 {
  154     m_ui->lineEditCurrentPassWord->setEnabled( false ) ;
  155     m_ui->lineEditNewPassWord->setEnabled( false ) ;
  156     m_ui->lineEditNewPassWord_2->setEnabled( false ) ;
  157     m_ui->pushButtonChange->setEnabled( false ) ;
  158     m_ui->pushButtonCancel->setEnabled( false ) ;
  159     m_ui->label->setEnabled( true ) ;
  160     m_ui->label_2->setEnabled( false ) ;
  161     m_ui->label_2->setEnabled( false ) ;
  162     m_ui->label_3->setEnabled( false ) ;
  163     m_ui->label_4->setEnabled( false ) ;
  164 
  165     if( m_ui->lineEditNewPassWord->text() == m_ui->lineEditNewPassWord_2->text() ){
  166 
  167         class wallet
  168         {
  169         public:
  170             wallet()
  171             {
  172             }
  173             wallet( const QString& password,const QString& walletName,const QString& applicationName )
  174             {
  175                 m_error = lxqt_wallet_open( &m_wallet,
  176                                 password.toLatin1().constData(),
  177                                 password.size(),
  178                                 walletName.toLatin1().constData(),
  179                                 applicationName.toLatin1().constData() ) ;
  180             }
  181             operator bool()
  182             {
  183                 return m_error == lxqt_wallet_no_error ;
  184             }
  185             bool changePassword( const QString& newPassword )
  186             {
  187                 auto q = newPassword.toLatin1() ;
  188                 m_error = lxqt_wallet_change_wallet_password( m_wallet,q.constData(),q.size() ) ;
  189                 return m_error == lxqt_wallet_no_error ;
  190             }
  191             void close()
  192             {
  193                 lxqt_wallet_close( &m_wallet ) ;
  194             }
  195         private:
  196             lxqt_wallet_t m_wallet = 0 ;
  197             lxqt_wallet_error m_error ;
  198         };
  199 
  200         auto password = m_ui->lineEditCurrentPassWord->text() ;
  201 
  202         Task::run< wallet >( [ this,password ](){
  203 
  204             return wallet( password,m_walletName,m_applicationName ) ;
  205 
  206         } ).then( [ this ]( wallet w ){
  207 
  208             if( w ){
  209 
  210                 if( w.changePassword( m_ui->lineEditNewPassWord->text() ) ){
  211 
  212                     m_walletPassWordChanged = true ;
  213                     this->HideUI() ;
  214                 }else{
  215                     m_ui->pushButtonChange->setEnabled( true ) ;
  216                     m_ui->pushButtonCancel->setEnabled( true ) ;
  217                     m_ui->label->setText( tr( "Wallet password could not be changed" ) ) ;
  218                     m_ui->pushButtonCancel->setVisible( false ) ;
  219                     m_ui->pushButtonChange->setVisible( false ) ;
  220                     m_ui->pushButtonOK->setVisible( true ) ;
  221                     m_ui->pushButtonOK->setFocus() ;
  222                 }
  223             }else{
  224                 m_ui->pushButtonChange->setEnabled( true ) ;
  225                 m_ui->pushButtonCancel->setEnabled( true ) ;
  226                 m_ui->label->setText( tr( "Wallet could not be opened with the presented key" ) ) ;
  227                 m_ui->pushButtonCancel->setVisible( false ) ;
  228                 m_ui->pushButtonChange->setVisible( false ) ;
  229                 m_ui->pushButtonOK->setVisible( true ) ;
  230                 m_ui->pushButtonOK->setFocus() ;
  231             }
  232 
  233             w.close() ;
  234         } ) ;
  235     }else{
  236         m_ui->label->setText( tr( "New passwords do not match" ) ) ;
  237         m_ui->pushButtonOK->setVisible( true ) ;
  238         m_ui->pushButtonCancel->setVisible( false ) ;
  239         m_ui->pushButtonChange->setVisible( false ) ;
  240         m_ui->pushButtonOK->setFocus() ;
  241     }
  242 }
  243 
  244 void LXQt::Wallet::changePassWordDialog::cancel()
  245 {
  246     m_create( QString(),false ) ;
  247     this->HideUI() ;
  248 }
  249 
  250 void LXQt::Wallet::changePassWordDialog::ok()
  251 {
  252     m_ui->lineEditCurrentPassWord->setEnabled( true ) ;
  253     m_ui->lineEditCurrentPassWord->clear() ;
  254     m_ui->lineEditCurrentPassWord->setFocus() ;
  255     m_ui->lineEditNewPassWord->setEnabled( true ) ;
  256     m_ui->lineEditNewPassWord_2->setEnabled( true ) ;
  257     m_ui->label->setEnabled( true ) ;
  258     m_ui->label_2->setEnabled( true ) ;
  259     m_ui->label_2->setEnabled( true ) ;
  260     m_ui->label_3->setEnabled( true ) ;
  261     m_ui->label_4->setEnabled( true ) ;
  262     m_ui->pushButtonOK->setVisible( false ) ;
  263     m_ui->pushButtonCancel->setVisible( true ) ;
  264     m_ui->pushButtonChange->setVisible ( true ) ;
  265     m_ui->pushButtonCancel->setEnabled( true ) ;
  266     m_ui->pushButtonChange->setEnabled( true ) ;
  267     m_ui->label->setText( m_banner ) ;
  268 }
  269 
  270 void LXQt::Wallet::changePassWordDialog::ok_1()
  271 {
  272     if( m_ui->label->text() == tr( "Passwords do not match" ) ){
  273 
  274         m_ui->lineEditCurrentPassWord->setEnabled( false ) ;
  275         m_ui->lineEditNewPassWord->setEnabled( true ) ;
  276         m_ui->lineEditNewPassWord->setFocus() ;
  277         m_ui->lineEditNewPassWord_2->setEnabled( true ) ;
  278         m_ui->label->setEnabled( true ) ;
  279         m_ui->label_2->setEnabled( true ) ;
  280         m_ui->label_2->setEnabled( true ) ;
  281         m_ui->label_3->setEnabled( true ) ;
  282         m_ui->label_4->setEnabled( true ) ;
  283         m_ui->pushButtonOK->setVisible( false ) ;
  284         m_ui->pushButtonCancel->setVisible( true ) ;
  285         m_ui->pushButtonChange->setVisible( true ) ;
  286         m_ui->pushButtonCancel->setEnabled( true ) ;
  287         m_ui->pushButtonChange->setEnabled( true ) ;
  288         m_ui->label->setText( m_banner ) ;
  289     }
  290 }
  291 
  292 void LXQt::Wallet::changePassWordDialog::closeEvent( QCloseEvent * e )
  293 {
  294     e->ignore() ;
  295     this->cancel() ;
  296 }