"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/zuluCrypt-gui/openvolume.cpp" (12 Sep 2019, 8552 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 "openvolume.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.4.0_vs_5.5.0.

    1 /*
    2  *
    3  *  Copyright ( c ) 2011-2015
    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 "openvolume.h"
   21 
   22 #include <QCloseEvent>
   23 #include <QDialog>
   24 #include "ui_openvolume.h"
   25 #include "task.hpp"
   26 #include <QProcess>
   27 #include <iostream>
   28 #include <QTableWidget>
   29 #include <QTableWidgetItem>
   30 #include <QObject>
   31 #include <QHeaderView>
   32 #include <QFile>
   33 #include <QThreadPool>
   34 #include <QKeySequence>
   35 #include "../zuluCrypt-cli/constants.h"
   36 #include "dialogmsg.h"
   37 #include "tablewidget.h"
   38 
   39 #include <unistd.h>
   40 #include <QDebug>
   41 
   42 #define ALL_PARTITIONS 1
   43 #define NON_SYSTEM_PARTITIONS 3
   44 
   45 openvolume::openvolume( QWidget * parent,bool e ) :
   46     QDialog( parent ),m_ui( new Ui::openvolume() ),m_enableUUID( e )
   47 {
   48     m_ui->setupUi( this ) ;
   49 
   50     m_ui->pbUUID->setEnabled( m_enableUUID ) ;
   51 
   52     this->setFixedSize( this->size() ) ;
   53     this->setFont( parent->font() ) ;
   54 
   55     connect( m_ui->tableWidget,SIGNAL( itemDoubleClicked( QTableWidgetItem * ) ),
   56          this,SLOT( tableEntryDoubleClicked( QTableWidgetItem * ) ) ) ;
   57     connect( m_ui->tableWidget,SIGNAL( currentItemChanged( QTableWidgetItem *,QTableWidgetItem * ) ),
   58          this,SLOT( currentItemChanged( QTableWidgetItem *,QTableWidgetItem * ) ) ) ;
   59     connect( m_ui->pbHelp,SIGNAL( clicked() ),this,SLOT( pbHelp() ) ) ;
   60     connect( m_ui->pbUUID,SIGNAL( clicked() ),this,SLOT( pbUUID() ) ) ;
   61     connect( m_ui->pbCancel,SIGNAL( clicked() ),this,SLOT( pbCancel() ) ) ;
   62     connect( m_ui->pbOpen,SIGNAL( clicked() ),this,SLOT( pbOpen() ) ) ;
   63 
   64     this->addAction( [ this ](){
   65 
   66         auto ac = new QAction( this ) ;
   67 
   68         ac->setShortcuts( { Qt::Key_Enter,Qt::Key_Return } ) ;
   69 
   70         connect( ac,SIGNAL( triggered() ),this,SLOT( EnterKeyPressed() ) ) ;
   71 
   72         return ac ;
   73     }() ) ;
   74 
   75     auto tw = m_ui->tableWidget ;
   76 
   77     tw->setColumnWidth( 0,183 ) ;
   78     tw->setColumnWidth( 1,90 ) ;
   79     //tw->setColumnWidth( 2,110 ) ;
   80     tw->hideColumn( 2 ) ;
   81     tw->setColumnWidth( 3,90 ) ;
   82     tw->setColumnWidth( 4,309 ) ;
   83 
   84     for( int i = 0 ; i < 5 ; i++ ){
   85 
   86         tw->horizontalHeaderItem( i )->setFont( this->font() ) ;
   87     }
   88 
   89     tw->horizontalHeader()->setVisible( true ) ;
   90 
   91     m_ui->checkBoxUUID->setVisible( false ) ;
   92 
   93     m_ui->pbHelp->setVisible( false ) ;
   94 
   95     this->installEventFilter( this ) ;
   96 
   97     this->disableAll() ;
   98 }
   99 
  100 bool openvolume::eventFilter( QObject * watched,QEvent * event )
  101 {
  102     return utility::eventFilter( this,watched,event,[ this ](){ this->HideUI() ; } ) ;
  103 }
  104 
  105 openvolume& openvolume::showEncryptedOnly()
  106 {
  107     m_showEncryptedOnly = true ;
  108     return *this ;
  109 }
  110 
  111 openvolume& openvolume::showLuksOnly()
  112 {
  113     m_showLuksOnly = true ;
  114     return *this ;
  115 }
  116 
  117 void openvolume::pbHelp()
  118 {
  119     DialogMsg msg( this ) ;
  120 
  121     QString m ;
  122 
  123     if( m_option == openvolume::allVolumes ){
  124 
  125         m = tr( "A list of all partitions on this system are displayed here.\nDouble click an entry to use it" ) ;
  126     }else{
  127         if( !utility::userIsRoot() ){
  128 
  129             m = tr( "Restart the tool from root's account or after you have created and added yourself to group \"zulucrypt\" if the volume you want to use is not on the list." ) ;
  130         }else{
  131             m = tr( "You are a root user and all partitions are displayed.\nDouble click an entry to use it" )  ;
  132         }
  133     }
  134 
  135     msg.ShowUIOK( tr( "INFO" ),m ) ;
  136 }
  137 
  138 void openvolume::pbUUID()
  139 {
  140     if( m_ui->pbUUID->isFlat() ){
  141 
  142         m_ui->pbUUID->setFlat( false ) ;
  143     }else{
  144         m_ui->pbUUID->setFlat( true ) ;
  145     }
  146 }
  147 
  148 void openvolume::pbCancel()
  149 {
  150     this->HideUI() ;
  151 }
  152 
  153 void openvolume::EnterKeyPressed()
  154 {
  155     auto tw = m_ui->tableWidget ;
  156     auto it = tw->currentItem() ;
  157 
  158     if( it ){
  159 
  160         this->tableEntryDoubleClicked( tw->item( it->row(),0 ) ) ;
  161     }
  162 }
  163 
  164 void openvolume::currentItemChanged( QTableWidgetItem * current, QTableWidgetItem * previous )
  165 {
  166     tablewidget::selectRow( current,previous ) ;
  167 }
  168 
  169 void openvolume::ShowNonSystemPartitions( std::function< void( const QString& ) > f )
  170 {
  171     m_function = std::move( f ) ;
  172 
  173     this->partitionList( tr( "Select A Partition To Create An Encrypted Volume In" )," -N" ) ;
  174 }
  175 
  176 void openvolume::partitionList( const QString& p, const QString& q,std::function< void( const QString& )> f )
  177 {
  178     m_function = std::move( f ) ;
  179 
  180     this->partitionList( p,q ) ;
  181 }
  182 
  183 void openvolume::ShowAllPartitions( std::function< void( const QString& ) > f )
  184 {
  185     m_function = std::move( f ) ;
  186 
  187     this->partitionList( tr( "Select An Encrypted Partition To Open" )," -A" ) ;
  188 }
  189 
  190 void openvolume::allowLUKSOnly()
  191 {
  192     m_disableNonLUKS = true ;
  193 }
  194 
  195 void openvolume::partitionList( const QString& title,const QString& volumeType )
  196 {
  197     this->setWindowTitle( title ) ;
  198 
  199     tablewidget::clearTable( m_ui->tableWidget ) ;
  200 
  201     m_ui->tableWidget->setEnabled( false ) ;
  202 
  203     this->show() ;
  204 
  205     if( volumeType == " -N" ){
  206 
  207         m_option = openvolume::nonSystemVolumes ;
  208 
  209     }else if( volumeType == " -A" ){
  210 
  211         m_option = openvolume::allVolumes ;
  212 
  213     }else if( volumeType == " -S" ){
  214 
  215         m_option = openvolume::systemVolumes ;
  216     }
  217 
  218     QStringList r ;
  219     QStringList l ;
  220 
  221     Task::await( [ & ](){
  222 
  223         l = utility::Task( QString( "%1 -AZ" ).arg( ZULUCRYPTzuluCrypt ) ).splitOutput( '\n' ) ;
  224 
  225         r = utility::Task( QString( "%1 -S" ).arg( ZULUCRYPTzuluCrypt ) ).splitOutput( '\n' ) ;
  226     } ) ;
  227 
  228     for( const auto& it : l ){
  229 
  230         QFont nonSystem = this->font() ;
  231 
  232         QFont system = this->font() ;
  233         system.setItalic( !system.italic() ) ;
  234         system.setBold( !system.bold() ) ;
  235 
  236         std::reference_wrapper<QFont> font = system ;
  237 
  238         auto z = utility::split( it,'\t' ) ;
  239 
  240         if( z.size() >= 4 ){
  241 
  242             const auto& fs = z.at( 3 ) ;
  243 
  244             if( !fs.contains( "member" ) ){
  245 
  246                 if( r.contains( z.first() ) ){
  247 
  248                     font = system ;
  249                 }else{
  250                     font = nonSystem ;
  251                 }
  252 
  253                 const QString& size = z.at( 1 ) ;
  254 
  255                 if( size == "1.0 KB" || size == "1,0 KB" || size == "Nil" ){
  256 
  257                     continue ;
  258                 }
  259                 if( m_showLuksOnly ){
  260 
  261                     if( fs.startsWith( "crypto_LUKS" ) ){
  262 
  263                         tablewidget::addRow( m_ui->tableWidget,z,font ) ;
  264                     }
  265                 }else if( m_showEncryptedOnly ){
  266 
  267                     //if( fs.startsWith( "crypto" ) || fs.contains( "Nil" ) ){
  268 
  269                         tablewidget::addRow( m_ui->tableWidget,z,font ) ;
  270                     //}
  271                 }else{
  272                     tablewidget::addRow( m_ui->tableWidget,z,font ) ;
  273                 }
  274             }
  275         }
  276     }
  277 
  278     m_ui->pbOpen->setEnabled( m_ui->tableWidget->rowCount() > 0 ) ;
  279 
  280     m_ui->tableWidget->setFocus() ;
  281 
  282     this->enableAll() ;
  283 }
  284 
  285 void openvolume::enableAll()
  286 {
  287     m_ui->tableWidget->setEnabled( true ) ;
  288     m_ui->pbCancel->setEnabled( true ) ;
  289     m_ui->pbOpen->setEnabled( true ) ;
  290     m_ui->pbUUID->setEnabled( m_enableUUID ) ;
  291     m_ui->pbHelp->setEnabled( true ) ;
  292 }
  293 
  294 void openvolume::disableAll()
  295 {
  296     m_ui->tableWidget->setEnabled( false ) ;
  297     m_ui->pbCancel->setEnabled( false ) ;
  298     m_ui->pbOpen->setEnabled( false ) ;
  299     m_ui->pbUUID->setEnabled( false ) ;
  300     m_ui->pbHelp->setEnabled( false ) ;
  301 }
  302 
  303 void openvolume::HideUI()
  304 {
  305     if( m_ui->pbCancel->isEnabled() ){
  306 
  307         this->hide() ;
  308         this->deleteLater() ;
  309     }
  310 }
  311 
  312 void openvolume::pbOpen()
  313 {
  314     auto table = m_ui->tableWidget ;
  315 
  316     if( table->rowCount() > 0 ){
  317 
  318         this->tableEntryDoubleClicked( table->currentItem() ) ;
  319     }
  320 }
  321 
  322 void openvolume::tableEntryDoubleClicked( QTableWidgetItem * item )
  323 {
  324     auto tw = m_ui->tableWidget ;
  325 
  326     if( m_disableNonLUKS ){
  327 
  328         if( !tw->item( item->row(),3 )->text().startsWith( "crypto_LUKS" ) ){
  329 
  330             DialogMsg m( this ) ;
  331 
  332             return m.ShowUIOK( tr( "ERROR" ),tr( "Only crypto_LUKS volumes can be selected" ) ) ;
  333         }else{
  334             m_ui->pbUUID->setFlat( true ) ;
  335         }
  336     }
  337 
  338     if( m_option == openvolume::nonSystemVolumes ) {
  339 
  340         if( !( utility::userIsRoot() || utility::userBelongsToGroup( "zulucrypt" ) ) ){
  341 
  342             if( item->font().italic() != this->font().italic() ){
  343 
  344                 //auto e = tr( "Insufficient privileges to operate on a system volume.\nRestart zuluCrypt from root's account to proceed" ) ;
  345 
  346                 //return DialogMsg( this ).ShowUIOK( tr( "ERROR" ),e ) ;
  347             }
  348         }
  349     }
  350 
  351     if( m_ui->pbUUID->isFlat() ){
  352 
  353         m_function( "UUID=\"" + tw->item( item->row(),4 )->text() + "\"" ) ;
  354     }else{
  355         m_function( tw->item( item->row(),0 )->text() ) ;
  356     }
  357 
  358     this->HideUI() ;
  359 }
  360 
  361 void openvolume::closeEvent( QCloseEvent * e )
  362 {
  363     e->ignore() ;
  364     this->HideUI() ;
  365 }
  366 
  367 openvolume::~openvolume()
  368 {
  369     delete m_ui ;
  370 }