"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/plugins/mainwindow.cpp" (30 Jan 2020, 7800 Bytes) of package /linux/misc/zuluCrypt-5.7.1.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 "mainwindow.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.7.0_vs_5.7.1.

    1 /*
    2  *
    3  *  Copyright (c) 2012
    4  *  name : Francis Banyikwa
    5  *  email: mhogomchungu@gmail.com
    6  *  This program is free software: you can redistribute it and/or modify
    7  *  it under the terms of the GNU General Public License as published by
    8  *  the Free Software Foundation, either version 2 of the License, or
    9  *  (at your option) any later version.
   10  *
   11  *  This program is distributed in the hope that it will be useful,
   12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *  GNU General Public License for more details.
   15  *
   16  *  You should have received a copy of the GNU General Public License
   17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include "mainwindow.h"
   21 #include "ui_mainwindow.h"
   22 #include <QDebug>
   23 
   24 #include <sys/types.h>
   25 #include <unistd.h>
   26 
   27 #include "../zuluCrypt-gui/utility.h"
   28 
   29 #include "../zuluCrypt-cli/pluginManager/libzuluCryptPluginManager.h"
   30 
   31 #include "task.hpp"
   32 
   33 #include <iostream>
   34 
   35 MainWindow::MainWindow( MainWindow::function_t f,QWidget * parent ) :
   36     QWidget( parent ),
   37     m_ui( new Ui::MainWindow ),
   38     m_printToStdOut( !qgetenv( "zuluCryptPrintToStdOut" ).isEmpty() ),
   39     m_handle( nullptr ),
   40     m_function( std::move( f ) )
   41 {
   42     m_ui->setupUi( this ) ;
   43     this->setFixedSize( this->size() ) ;
   44 
   45     m_ui->lineEditKey->setEchoMode( QLineEdit::Password ) ;
   46 
   47     this->setWindowIcon( QIcon( ":/default.png" ) ) ;
   48     m_ui->pbKeyFile->setIcon( QIcon( ":/file.png" ) ) ;
   49 
   50     connect( m_ui->pbCancel,SIGNAL( clicked() ),this,SLOT( pbCancel() ) ) ;
   51     connect( m_ui->pbOpen,SIGNAL( clicked() ),this,SLOT( pbOpen() ) ) ;
   52     connect( m_ui->pbKeyFile,SIGNAL( clicked() ),this,SLOT( pbKeyFile() ) ) ;
   53 
   54     m_ui->lineEditKey->setFocus() ;
   55 
   56     m_working = false ;
   57 
   58     m_requireKey = false ;
   59     m_requireKeyFile = true ;
   60 
   61     auto ac = new QAction( this ) ;
   62 
   63     QList<QKeySequence> keys ;
   64 
   65     keys.append( Qt::Key_Enter ) ;
   66     keys.append( Qt::Key_Return ) ;
   67 
   68     ac->setShortcuts( keys ) ;
   69 
   70     connect( ac,SIGNAL( triggered() ),this,SLOT( defaultButton() ) ) ;
   71 
   72     this->addAction( ac ) ;
   73 
   74     m_findExecutable = []( QVector<QString>& updated,const QVector<QString>& original ){
   75 
   76         if( original.isEmpty() ){
   77 
   78             return QString() ;
   79         }
   80 
   81         QString e ;
   82 
   83         for( const auto& it : original ){
   84 
   85             auto _not_found = [&]( const char * path ){
   86 
   87                 e = path + it ;
   88 
   89                 bool r = QFile::exists( e ) ;
   90 
   91                 if( r ){
   92 
   93                     updated.append( e ) ;
   94                 }else{
   95                     updated.append( it ) ;
   96                 }
   97 
   98                 return r == false ;
   99             } ;
  100 
  101             if( _not_found( "/usr/local/bin/" ) ){
  102 
  103                 if( _not_found( "/usr/bin/" ) ){
  104 
  105                     if( _not_found( "/usr/sbin/" ) ){
  106 
  107                         return it ;
  108                     }
  109                 }
  110             }
  111         }
  112 
  113         return QString() ;
  114     } ;
  115 }
  116 
  117 void MainWindow::Show()
  118 {
  119     if( m_appName.endsWith( " Key" ) ){
  120 
  121         this->setWindowTitle( tr( "%1 Module" ).arg( m_appName ) ) ;
  122     }else{
  123         this->setWindowTitle( tr( "%1 Key Module" ).arg( m_appName ) ) ;
  124     }
  125 
  126     this->show() ;
  127 }
  128 
  129 void MainWindow::setButtonIcon( const QString& icon )
  130 {
  131     QString x( ":/" + icon ) ;
  132     this->setWindowIcon( QIcon( x ) ) ;
  133     m_ui->pbKeyFile->setIcon( QIcon( x ) ) ;
  134 }
  135 
  136 void MainWindow::setRequireKey( bool k )
  137 {
  138     m_requireKey = k ;
  139 }
  140 
  141 void MainWindow::setRequireKeyFile( bool k )
  142 {
  143     m_requireKeyFile = k ;
  144 }
  145 
  146 void MainWindow::defaultButton()
  147 {
  148     if( m_ui->pbCancel->hasFocus() ){
  149 
  150         this->pbCancel() ;
  151     }else{
  152         this->pbOpen() ;
  153     }
  154 }
  155 
  156 void MainWindow::setToken( char * const * e )
  157 {
  158     if( !m_printToStdOut ){
  159 
  160         m_handle = zuluCryptPluginManagerOpenConnection( *( e + 3 ) ) ;
  161     }
  162 }
  163 
  164 void MainWindow::setApplicationName( const QString& appName )
  165 {
  166     m_appName = appName ;
  167 }
  168 
  169 void MainWindow::setkeyLabel( const QString& keyLabel )
  170 {
  171     m_ui->label_2->setText( keyLabel ) ;
  172 }
  173 
  174 void MainWindow::setheaderPath(const QString& s )
  175 {
  176     m_ui->lineEditKeyFile->setText( s ) ;
  177 }
  178 
  179 void MainWindow::setkeyFileLabel( const QString& keyFileLabel )
  180 {
  181     m_ui->label->setText( keyFileLabel ) ;
  182 }
  183 
  184 void MainWindow::SetFocus()
  185 {
  186     if( m_ui->lineEditKey->text().isEmpty() ){
  187 
  188         m_ui->lineEditKey->setFocus() ;
  189 
  190     }else if( m_ui->lineEditKeyFile->text().isEmpty() ){
  191 
  192         m_ui->lineEditKeyFile->setFocus() ;
  193     }else{
  194         m_ui->pbOpen->setFocus() ;
  195     }
  196 }
  197 
  198 void MainWindow::pbCancel()
  199 {
  200     if( m_working ){
  201 
  202         DialogMsg msg( this ) ;
  203 
  204         auto st = msg.ShowUIYesNoDefaultNo( tr( "WARNING"),
  205                            tr( "Are you sure you want to terminate this operation prematurely?" ) ) ;
  206 
  207         if( st == QMessageBox::Yes ){
  208 
  209             this->enableAlll() ;
  210 
  211             m_working = false ;
  212 
  213             this->cancelled() ;
  214         }
  215     }else{
  216         this->cancelled() ;
  217     }
  218 }
  219 
  220 void MainWindow::cancelled()
  221 {
  222     this->Exit( 1 ) ;
  223 }
  224 
  225 void MainWindow::Exit( int st )
  226 {
  227     QCoreApplication::exit( st ) ;
  228 }
  229 
  230 void MainWindow::setfindExeFunction( std::function<QString( QVector<QString>&,const QVector<QString>& )> f )
  231 {
  232     m_findExecutable = f ;
  233 }
  234 
  235 void MainWindow::setExe( const QVector<QString>& exe )
  236 {
  237     m_exe = exe ;
  238 }
  239 
  240 void MainWindow::setKeyFileAsKey( void )
  241 {
  242     m_keyfileAsKey = true ;
  243     m_ui->lineEditKeyFile->setEchoMode( QLineEdit::Password ) ;
  244     m_ui->pbKeyFile->setVisible( false ) ;
  245 }
  246 
  247 void MainWindow::pbOpen()
  248 {
  249     DialogMsg msg( this ) ;
  250 
  251     QString key = m_ui->lineEditKey->text().toLatin1() ;
  252 
  253     if( m_requireKey ){
  254 
  255         if( key.isEmpty() ){
  256 
  257             return msg.ShowUIOK( tr( "ERROR" ),tr( "Key field is empty" ) ) ;
  258         }
  259     }
  260 
  261     QString keyFile = m_ui->lineEditKeyFile->text() ;
  262 
  263     keyFile.replace( "file://","" ) ;
  264 
  265     if( m_requireKeyFile ){
  266 
  267         if( !m_keyfileAsKey ){
  268 
  269             if( keyFile.isEmpty() ){
  270                 return msg.ShowUIOK( tr( "ERROR" ),tr( "Path to %1 keyfile is empty" ).arg( m_appName ) ) ;
  271             }
  272             if( !QFile::exists( keyFile ) ){
  273                 return msg.ShowUIOK( tr( "ERROR" ),tr( "Invalid path to %1 keyfile" ).arg( m_appName ) ) ;
  274             }
  275         }
  276     }
  277 
  278     QVector<QString> exe ;
  279 
  280     auto e = m_findExecutable( exe,m_exe ) ;
  281 
  282     if( !e.isEmpty() ){
  283 
  284         return msg.ShowUIOK( tr( "ERROR" ),
  285                      tr( "Could not find \"%1\" executable in \"/usr/local/bin\",\"/usr/bin\" and \"/usr/sbin\"" ).arg( e ) ) ;
  286     }
  287 
  288     this->disableAll() ;
  289 
  290     m_working = true ;
  291 
  292     Task::run( [ &,exe,keyFile,key ](){
  293 
  294         auto s = m_function( exe,keyFile,key ) ;
  295 
  296         if( s.isEmpty() ){
  297 
  298             return false ;
  299         }else{
  300             if( m_printToStdOut ){
  301 
  302                 std::cout << s.constData() << std::flush ;
  303                 return true ;
  304             }else{
  305                 if( m_handle ){
  306 
  307                     zuluCryptPluginManagerSendKey( m_handle,s.constData(),size_t( s.size() ) ) ;
  308 
  309                     return true ;
  310                 }else{
  311                     return false ;
  312                 }
  313             }
  314         }
  315 
  316     } ).then( [ this ]( bool passed ){
  317 
  318         if( passed ){
  319 
  320             this->Exit( 0 ) ;
  321         }else{
  322             DialogMsg msg( this ) ;
  323             m_working = false ;
  324 
  325             if( m_appName.endsWith( " key" ) ){
  326 
  327                 msg.ShowUIOK( tr( "ERROR" ),tr("Could not decrypt the %1,wrong key?" ).arg( m_appName ) ) ;
  328             }else{
  329                 msg.ShowUIOK( tr( "ERROR" ),tr("Could not decrypt the %1 key,wrong key?" ).arg( m_appName ) ) ;
  330             }
  331 
  332             this->enableAlll() ;
  333             m_ui->lineEditKey->setFocus() ;
  334         }
  335     } ) ;
  336 }
  337 
  338 void MainWindow::pbKeyFile()
  339 {
  340     auto Z = QFileDialog::getOpenFileName( this,tr( "Select A Keyfile" ),utility::homePath() ) ;
  341 
  342     if( !Z.isEmpty() ){
  343 
  344         m_ui->lineEditKeyFile->setText( Z ) ;
  345     }
  346 
  347     this->SetFocus() ;
  348 }
  349 
  350 void MainWindow::closeEvent( QCloseEvent * e )
  351 {
  352     e->ignore() ;
  353     this->pbCancel() ;
  354 }
  355 
  356 void MainWindow::disableAll()
  357 {
  358     m_ui->label->setEnabled( false ) ;
  359     m_ui->label_2->setEnabled( false ) ;
  360     m_ui->lineEditKey->setEnabled( false ) ;
  361     m_ui->lineEditKeyFile->setEnabled( false ) ;
  362     m_ui->pbKeyFile->setEnabled( false ) ;
  363     m_ui->pbOpen->setEnabled( false ) ;
  364     m_ui->pbCancel->setEnabled( false ) ;
  365 }
  366 
  367 void MainWindow::enableAlll()
  368 {
  369     m_ui->label->setEnabled( true ) ;
  370     m_ui->label_2->setEnabled( true ) ;
  371     m_ui->lineEditKey->setEnabled( true ) ;
  372     m_ui->lineEditKeyFile->setEnabled( true ) ;
  373     m_ui->pbKeyFile->setEnabled( true ) ;
  374     m_ui->pbOpen->setEnabled( true ) ;
  375     m_ui->pbCancel->setEnabled( true ) ;
  376 }
  377 
  378 MainWindow::~MainWindow()
  379 {
  380     zuluCryptPluginManagerCloseConnection( m_handle ) ;
  381     delete m_ui ;
  382 }