"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/external_libraries/lxqt_wallet/frontend/lxqt_internal_wallet.cpp" (12 Sep 2019, 8841 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 "lxqt_internal_wallet.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 "lxqt_internal_wallet.h"
   32 
   33 namespace Task = LXQt::Wallet::Task ;
   34 
   35 LXQt::Wallet::internalWallet::internalWallet() : m_wallet( nullptr )
   36 {
   37 }
   38 
   39 LXQt::Wallet::internalWallet::~internalWallet()
   40 {
   41     lxqt_wallet_close( &m_wallet ) ;
   42 }
   43 
   44 void LXQt::Wallet::internalWallet::setImage( const QIcon& image )
   45 {
   46     this->setWindowIcon( image ) ;
   47 }
   48 
   49 void LXQt::Wallet::internalWallet::openWallet( QString password )
   50 {
   51     m_password = password ;
   52 
   53     Task::run< lxqt_wallet_error >( [ this ](){
   54 
   55         return lxqt_wallet_open( &m_wallet,
   56                      m_password.toLatin1().constData(),
   57                      m_password.size(),m_walletName.toLatin1().constData(),
   58                      m_applicationName.toLatin1().constData() ) ;
   59 
   60     } ).then( [ this ]( lxqt_wallet_error r ){
   61 
   62         this->opened( r == lxqt_wallet_no_error ) ;
   63     } ) ;
   64 }
   65 
   66 void LXQt::Wallet::internalWallet::opened( bool opened )
   67 {
   68     m_correctPassword( opened ) ;
   69 
   70     m_opened = opened ;
   71 
   72     if( m_opened ){
   73 
   74         m_loop.exit() ;
   75 
   76         this->walletIsOpen( m_opened ) ;
   77     }
   78 }
   79 
   80 bool LXQt::Wallet::internalWallet::open( const QString& walletName,
   81                      const QString& applicationName,
   82                      QWidget * parent,
   83                      const QString& password,
   84                      const QString& displayApplicationName )
   85 {
   86     this->open( walletName,
   87             applicationName,
   88             []( bool e ){ Q_UNUSED( e ) ; },
   89             parent,
   90             password,
   91             displayApplicationName ) ;
   92 
   93     m_loop.exec() ;
   94 
   95     return m_opened ;
   96 }
   97 
   98 void LXQt::Wallet::internalWallet::open( const QString& walletName,
   99                      const QString& applicationName,
  100                      std::function< void( bool ) > function,
  101                      QWidget * parent,
  102                      const QString& password,
  103                      const QString& displayApplicationName )
  104 {
  105     if( parent ){
  106 
  107         this->setParent( parent ) ;
  108     }
  109 
  110     m_walletName      = walletName ;
  111     m_applicationName = applicationName ;
  112     m_password        = password ;
  113 
  114     m_walletOpened = std::move( function ) ;
  115 
  116     if( m_applicationName.isEmpty() ){
  117 
  118         m_applicationName = m_walletName ;
  119     }
  120 
  121     if( displayApplicationName.isEmpty() ){
  122 
  123         m_displayApplicationName = m_applicationName ;
  124     }else{
  125         m_displayApplicationName = displayApplicationName ;
  126     }
  127 
  128     if( LXQt::Wallet::walletExists( LXQt::Wallet::BackEnd::internal,m_walletName,m_applicationName ) ){
  129 
  130         this->openWallet() ;
  131     }else{
  132         this->createWallet() ;
  133     }
  134 }
  135 
  136 void LXQt::Wallet::internalWallet::openWallet()
  137 {
  138     if( m_password.isEmpty() ){
  139 
  140         /*
  141          * to prevent an unnecessary prompt,try to open a wallet without a password and then
  142          * prompt on failure,this will allow a silent opening of the wallet set without a password.
  143          */
  144 
  145         Task::run< lxqt_wallet_error >( [ this ](){
  146 
  147             return lxqt_wallet_open( &m_wallet,
  148                          m_password.toLatin1().constData(),
  149                          m_password.size(),
  150                          m_walletName.toLatin1().constData(),
  151                          m_applicationName.toLatin1().constData() ) ;
  152 
  153         } ).then( [ this ]( lxqt_wallet_error r ){
  154 
  155             if( r == lxqt_wallet_no_error ){
  156 
  157                 this->opened( true ) ;
  158             }else{
  159                 /*
  160                  * passwordless opening failed,prompt a user for a password
  161                  */
  162 
  163                 using pwd = LXQt::Wallet::password_dialog ;
  164 
  165                 auto _cancelled = [ this ](){
  166 
  167                     m_opened = false ;
  168 
  169                     m_loop.exit() ;
  170 
  171                     this->walletIsOpen( false ) ;
  172                 } ;
  173 
  174                 pwd::instance( this,
  175                            m_walletName,
  176                            m_displayApplicationName,
  177                            [ this ]( const QString& p ){ this->openWallet( p ) ; },
  178                            std::move( _cancelled ),
  179                            &m_correctPassword ) ;
  180             }
  181         } ) ;
  182     }else{
  183         this->openWallet( m_password ) ;
  184     }
  185 }
  186 
  187 void LXQt::Wallet::internalWallet::createWallet()
  188 {
  189     using cbd = LXQt::Wallet::changePassWordDialog ;
  190 
  191     const auto& w = m_walletName ;
  192     const auto& d = m_displayApplicationName ;
  193 
  194     cbd::instance( this,w,d,[ this ]( const QString& password,bool create ){
  195 
  196         if( create ){
  197 
  198             m_password = password ;
  199 
  200             Task::run< lxqt_wallet_error >( [ this ](){
  201 
  202                 return lxqt_wallet_create( m_password.toLatin1().constData(),
  203                                m_password.size(),
  204                                m_walletName.toLatin1().constData(),
  205                                m_applicationName.toLatin1().constData() ) ;
  206 
  207             } ).then( [ this ]( lxqt_wallet_error r ){
  208 
  209                 if( r == lxqt_wallet_no_error ){
  210 
  211                     this->openWallet( m_password ) ;
  212                 }else{
  213                     this->walletIsOpen( false ) ;
  214                 }
  215             } ) ;
  216         }else{
  217             this->walletIsOpen( false ) ;
  218         }
  219     } ) ;
  220 }
  221 
  222 void LXQt::Wallet::internalWallet::changeWalletPassWord( const QString& walletName,
  223                              const QString& applicationName,
  224                              std::function< void( bool ) > function )
  225 {
  226     LXQt::Wallet::changePassWordDialog::instance_1( this,walletName,applicationName,
  227                             [ this,function ]( bool e ){ function( e ) ;} ) ;
  228 }
  229 
  230 QByteArray LXQt::Wallet::internalWallet::readValue( const QString& key )
  231 {
  232     lxqt_wallet_key_values_t key_value ;
  233 
  234     if( lxqt_wallet_read_key_value( m_wallet,key.toLatin1().constData(),key.size() + 1,&key_value ) ){
  235 
  236         return QByteArray( key_value.key_value,key_value.key_value_size ) ;
  237     }else{
  238         return QByteArray() ;
  239     }
  240 }
  241 
  242 QVector< std::pair< QString,QByteArray > > LXQt::Wallet::internalWallet::readAllKeyValues( void )
  243 {
  244     QVector< std::pair < QString,QByteArray > > w ;
  245 
  246     lxqt_wallet_iterator_t iter ;
  247 
  248     iter.iter_pos = 0 ;
  249 
  250     while( lxqt_wallet_iter_read_value( m_wallet,&iter ) ){
  251 
  252         w.append( { QByteArray( iter.entry.key,iter.entry.key_size - 1 ),
  253                 QByteArray( iter.entry.key_value,iter.entry.key_value_size ) } ) ;
  254     }
  255 
  256     return w ;
  257 }
  258 
  259 QStringList LXQt::Wallet::internalWallet::readAllKeys()
  260 {
  261     QStringList l ;
  262     lxqt_wallet_iterator_t iter ;
  263 
  264     iter.iter_pos = 0 ;
  265 
  266     while( lxqt_wallet_iter_read_value( m_wallet,&iter ) ){
  267 
  268         l.append( QByteArray( iter.entry.key,iter.entry.key_size - 1 ) ) ;
  269     }
  270 
  271     return l ;
  272 }
  273 
  274 bool LXQt::Wallet::internalWallet::addKey( const QString& key,const QByteArray& value )
  275 {
  276     /*
  277      * For the key,we add +1 to the key size to include the '\0' character in the key to
  278      * avoid possible collisions if our keys prefix match
  279      */
  280     auto r = lxqt_wallet_add_key( m_wallet,
  281                       key.toLatin1().constData(),
  282                       key.size() + 1,
  283                       value.constData(),
  284                       value.size() ) ;
  285 
  286     return r == lxqt_wallet_no_error ;
  287 }
  288 
  289 void LXQt::Wallet::internalWallet::deleteKey( const QString& key )
  290 {
  291     lxqt_wallet_delete_key( m_wallet,key.toLatin1().constData(),key.size() + 1 ) ;
  292 }
  293 
  294 int LXQt::Wallet::internalWallet::walletSize( void )
  295 {
  296     return lxqt_wallet_wallet_size( m_wallet ) ;
  297 }
  298 
  299 void LXQt::Wallet::internalWallet::closeWallet( bool b )
  300 {
  301     Q_UNUSED( b ) ;
  302     lxqt_wallet_close( &m_wallet ) ;
  303 }
  304 
  305 LXQt::Wallet::BackEnd LXQt::Wallet::internalWallet::backEnd()
  306 {
  307     return LXQt::Wallet::BackEnd::internal ;
  308 }
  309 
  310 bool LXQt::Wallet::internalWallet::opened()
  311 {
  312     return m_wallet != 0 ;
  313 }
  314 
  315 QObject * LXQt::Wallet::internalWallet::qObject()
  316 {
  317     this->setObjectName( m_password ) ;
  318     return this ;
  319 }
  320 
  321 void LXQt::Wallet::internalWallet::walletIsOpen( bool e )
  322 {
  323     m_walletOpened( e ) ;
  324 }
  325 
  326 QString LXQt::Wallet::internalWallet::storagePath()
  327 {
  328     return QString() ;
  329 }
  330 
  331 QStringList LXQt::Wallet::internalWallet::managedWalletList()
  332 {
  333     char path[ 4096 ] ;
  334     lxqt_wallet_application_wallet_path( path,4096,m_applicationName.toLatin1().constData() ) ;
  335     QDir d( path ) ;
  336     QStringList l = d.entryList() ;
  337     l.removeOne( "." ) ;
  338     l.removeOne( ".." ) ;
  339 
  340     if( l.size() > 0 ){
  341         /*
  342          * remove the extension part of a file name
  343          */
  344         const QString& q = l.at( 0 ) ;
  345         l.replaceInStrings( q.mid( q.indexOf( "." ) ),"" ) ;
  346     }
  347 
  348     return l ;
  349 }
  350 
  351 QString LXQt::Wallet::internalWallet::localDefaultWalletName()
  352 {
  353     return QString() ;
  354 }
  355 
  356 QString LXQt::Wallet::internalWallet::networkDefaultWalletName()
  357 {
  358     return QString() ;
  359 }