"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/zuluCrypt-gui/erasedevice.cpp" (30 Sep 2019, 7845 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 "erasedevice.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.6.0_vs_5.7.0.

    1 /*
    2  *  Copyright ( c ) 2012-2015
    3  *  name : Francis Banyikwa
    4  *  email: mhogomchungu@gmail.com
    5  *  This program is free software: you can redistribute it and/or modify
    6  *  it under the terms of the GNU General Public License as published by
    7  *  the Free Software Foundation, either version 2 of the License, or
    8  *  ( at your option ) any later version.
    9  *
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *  GNU General Public License for more details.
   14  *
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 
   19 #include "erasedevice.h"
   20 #include "ui_erasedevice.h"
   21 
   22 #include <QCloseEvent>
   23 #include <QFileDialog>
   24 #include <QMessageBox>
   25 #include <QDateTime>
   26 
   27 #include "utility.h"
   28 #include "openvolume.h"
   29 #include "dialogmsg.h"
   30 #include "../zuluCrypt-cli/constants.h"
   31 
   32 erasedevice::erasedevice( QWidget * parent ) : QDialog( parent ),m_ui( new Ui::erasedevice )
   33 {
   34     m_ui->setupUi( this ) ;
   35 
   36     m_label.setOptions( m_ui->label_2,m_ui->pushButton ) ;
   37 
   38     this->setFixedSize( this->size() ) ;
   39     this->setFont( parent->font() ) ;
   40 
   41     m_ui->progressBar->setValue( 0 ) ;
   42     m_ui->progressBar->setMaximum( 100 ) ;
   43     m_ui->progressBar->setMinimum( 0 ) ;
   44 
   45     this->setWindowTitle( tr( "Write Random Data Over Existing Data" ) ) ;
   46 
   47     connect( m_ui->pushButtonFile,SIGNAL( clicked() ),this,SLOT( pbFile() ) ) ;
   48     connect( m_ui->pushButtonPartition,SIGNAL( clicked() ),this,SLOT( pbPartition() ) ) ;
   49     connect( m_ui->pushButtonStart,SIGNAL( clicked() ),this,SLOT( pbStart() ) ) ;
   50     connect( m_ui->pushButtonCancel,SIGNAL( clicked() ),this,SLOT( pbCancel() ) ) ;
   51 
   52     connect( this,SIGNAL( sendProgress( QString,QString,QString,QString,int ) ),
   53          this,SLOT( setProgress( QString,QString,QString,QString,int ) ) ) ;
   54 
   55     m_ui->pushButtonFile->setIcon( QIcon( ":/file.png" ) ) ;
   56     m_ui->pushButtonPartition->setIcon( QIcon( ":/partition.png" ) ) ;
   57 
   58     m_ui->lineEdit->setFocus() ;
   59 
   60     this->installEventFilter( this ) ;
   61 }
   62 
   63 bool erasedevice::eventFilter( QObject * watched,QEvent * event )
   64 {
   65     return utility::eventFilter( this,watched,event,[ this ](){ this->HideUI() ; } ) ;
   66 }
   67 
   68 void erasedevice::ShowUI()
   69 {
   70     m_option = 0 ;
   71 
   72     DialogMsg msg( this ) ;
   73 
   74     auto x = tr ( "\
   75 The next dialog will write random data to a device leading to permanent loss of all contents on the device.\n\n\
   76 Are you sure you want to continue? " ) ;
   77 
   78     if( msg.ShowUIYesNoDefaultNo( tr( "WARNING" ),x ) == QMessageBox::Yes ){
   79 
   80         this->show() ;
   81     }else{
   82         this->HideUI() ;
   83     }
   84 }
   85 
   86 void erasedevice::ShowUI( const QString& path )
   87 {
   88     m_option = 1 ;
   89     m_ui->lineEdit->setText( path ) ;
   90     this->show() ;
   91     this->pbStart() ;
   92 }
   93 
   94 void erasedevice::taskResult( int st )
   95 {
   96     this->setWindowTitle( tr( "Write Random Data Over Existing Data" ) ) ;
   97 
   98     DialogMsg msg( this ) ;
   99 
  100     switch( st ){
  101 
  102         case 0: m_ui->progressBar->setValue( 100 ) ;
  103         {
  104             QString a = tr( "Average Speed:" ) + " " + m_average_speed + "\n" ;
  105             QString b = tr( "Total Time:" ) + " " + m_total_time + "\n\n" ;
  106             m_label.show( a + b + tr( "Data on the device successfully erased" ) ) ;
  107         }
  108         break ;
  109         case 1: m_label.show( tr( "Could not create mapper" ) )                     ;break ;
  110         case 2: m_label.show( tr( "Could not resolve device path" ) )                   ;break ;
  111         case 3: m_label.show( tr( "Random data successfully written" ) )                ;break ;
  112         //case 4: msg.ShowUIOK( tr( "ERROR!" ),tr( "User chose not to proceed" ) )          ;break ;
  113         case 5: m_label.show( tr( "Operation terminated per user choice" ) )                ;break ;
  114         case 6: m_label.show( tr( "Can not write on a device with opened mapper" ) )            ;break ;
  115         case 7: m_label.show( tr( "Policy prevents non root user opening mapper on system partition" ) );break;
  116         case 8: msg.ShowPermissionProblem( "Writing",m_ui->lineEdit->text() )               ;break ;
  117         case 9: m_label.show( tr( "Device path is invalid" ) )                      ;break ;
  118         case 10:m_label.show( tr( "Passphrase file does not exist" ) )                  ;break ;
  119         case 11:m_label.show( tr( "Could not get enought memory to hold the key file" ) )       ;break ;
  120         case 12:m_label.show( tr( "Insufficient privilege to open key file for reading" ) )     ;break ;
  121         case 13:m_label.show( tr( "This device appear to already be in use" ) )             ;break ;
  122         case 14:m_label.show( tr( "Can not open a mapper on a mounted device" ) )           ;break ;
  123         default:m_label.show( tr( "Could not write to the device" ) ) ;
  124     }
  125 
  126     this->HideUI() ;
  127 }
  128 
  129 void erasedevice::HideUI()
  130 {   
  131     this->hide() ;
  132     this->deleteLater() ;
  133 }
  134 
  135 void erasedevice::pbStart()
  136 {
  137     auto path = m_ui->lineEdit->text() ;
  138 
  139     DialogMsg msg( this ) ;
  140 
  141     if( path.isEmpty() ){
  142 
  143         return m_label.show( tr( "Device path field is empty" ) ) ;
  144     }
  145 
  146     path = utility::resolvePath( path ) ;
  147 
  148     if( !utility::pathExists( path ) ){
  149 
  150         return m_label.show( tr( "Invalid path to device" ) ) ;
  151     }
  152 
  153     if( m_option == 0 ){
  154 
  155         QString x = tr( "\
  156 Are you really sure you want to write random data to \"%1\" effectively destroying all contents in it?" ).arg( path ) ;
  157 
  158         if( msg.ShowUIYesNoDefaultNo( tr( "WARNING!" ),x ) == QMessageBox::No ){
  159 
  160             return ;
  161         }
  162     }
  163 
  164     if( utility::requireSystemPermissions( path ) ){
  165 
  166         if( !utility::enablePolkit( utility::background_thread::False ) ){
  167 
  168             return m_label.show( tr( "Failed to enable polkit support" ) ) ;
  169         }
  170     }
  171 
  172     this->setWindowTitle( tr( "Writing Random Data Over Existing Data" ) ) ;
  173 
  174     this->disableAll() ;
  175 
  176     m_exit = false ;
  177     m_running = true ;
  178 
  179     utility::progress update( 1500,[ this ]( const utility::progress::result& m ){
  180 
  181         emit sendProgress( m.current_speed,
  182                    m.average_speed,
  183                    m.eta,
  184                    m.total_time,
  185                    m.percentage_done ) ;
  186     } ) ;
  187 
  188     auto r = utility::clearVolume( path,&m_exit,0,update.updater_quint() ).await() ;
  189 
  190     m_running = false ;
  191 
  192     this->taskResult( r ) ;
  193 }
  194 
  195 void erasedevice::enableAll()
  196 {
  197     m_ui->label->setEnabled( true ) ;
  198     m_ui->labelPath->setEnabled( true ) ;
  199     m_ui->lineEdit->setEnabled( true ) ;
  200     m_ui->pushButtonCancel->setEnabled( true ) ;
  201     m_ui->pushButtonFile->setEnabled( true ) ;
  202     m_ui->pushButtonPartition->setEnabled( true ) ;
  203     m_ui->pushButtonStart->setEnabled( true ) ;
  204     m_ui->label_3->setEnabled( true ) ;
  205     m_ui->label_4->setEnabled( true ) ;
  206     m_ui->labelAverageSpeed->setEnabled( true ) ;
  207     m_ui->labelETA->setEnabled( true ) ;
  208 }
  209 
  210 void erasedevice::disableAll()
  211 {
  212     m_ui->label->setEnabled( false ) ;
  213     m_ui->labelPath->setEnabled( false ) ;
  214     m_ui->lineEdit->setEnabled( false ) ;
  215     //m_ui->pushButtonCancel->setEnabled( false ) ; //need this one enabled
  216     m_ui->pushButtonFile->setEnabled( false ) ;
  217     m_ui->pushButtonPartition->setEnabled( false ) ;
  218     m_ui->pushButtonStart->setEnabled( false ) ;
  219     m_ui->label_3->setEnabled( false ) ;
  220     m_ui->label_4->setEnabled( false ) ;
  221     m_ui->labelAverageSpeed->setEnabled( false ) ;
  222     m_ui->labelETA->setEnabled( false ) ;
  223 }
  224 
  225 void erasedevice::setProgress( QString cs,QString av,QString eta,QString tt,int st )
  226 {
  227     Q_UNUSED( cs )
  228     m_ui->labelAverageSpeed->setText( av ) ;
  229     m_ui->labelETA->setText( eta ) ;
  230     m_ui->progressBar->setValue( st ) ;
  231 
  232     m_average_speed = av ;
  233     m_total_time = tt ;
  234 }
  235 
  236 void erasedevice::pbCancel()
  237 {
  238     if( m_running ){
  239 
  240         m_exit = true ;
  241     }else{
  242         this->HideUI() ;
  243     }
  244 }
  245 
  246 void erasedevice::pbFile()
  247 {
  248     auto Z = QFileDialog::getOpenFileName( this,tr( "Enter Path To Volume To Be Erased" ),utility::homePath() ) ;
  249     m_ui->lineEdit->setText( Z ) ;
  250 }
  251 
  252 void erasedevice::pbPartition()
  253 {
  254     auto e = tr( "Select A Non System Partition To Erase Its Contents" ) ;
  255 
  256     openvolume::instance( this,false ).partitionList( e," -N",[ this ]( const QString& e ){
  257 
  258         this->setPath( e ) ;
  259     } ) ;
  260 }
  261 
  262 void erasedevice::setPath( QString p )
  263 {
  264     m_ui->lineEdit->setText( p ) ;
  265 }
  266 
  267 void erasedevice::closeEvent( QCloseEvent * e )
  268 {
  269     e->ignore() ;
  270     this->pbCancel() ;
  271 }
  272 
  273 erasedevice::~erasedevice()
  274 {
  275     delete m_ui ;
  276 }