From adf6fe5be8aecac851d24f2b0c1e848f15bfd0b4 Mon Sep 17 00:00:00 2001 From: waltergallegog Date: Wed, 31 Jan 2018 20:56:05 +0100 Subject: [PATCH] code commenting and cleanup --- SEcubeWallet.pro.user | 2 +- scr/addentry.cpp | 59 ++++--- scr/addentry.h | 8 +- scr/dbmanager.cpp | 54 ------ scr/dbmanager.h | 48 ----- scr/deleteconfirmation.cpp | 4 +- scr/mainwindow.cpp | 352 +++++++++++++++++++------------------ scr/mainwindow.h | 36 ++-- scr/scr.pro | 2 - scr/securefiledialog.cpp | 8 +- 10 files changed, 244 insertions(+), 329 deletions(-) delete mode 100644 scr/dbmanager.cpp delete mode 100644 scr/dbmanager.h diff --git a/SEcubeWallet.pro.user b/SEcubeWallet.pro.user index 9ae0545..417406d 100644 --- a/SEcubeWallet.pro.user +++ b/SEcubeWallet.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/scr/addentry.cpp b/scr/addentry.cpp index 58ec423..1d4c1a2 100644 --- a/scr/addentry.cpp +++ b/scr/addentry.cpp @@ -1,32 +1,37 @@ #include "addentry.h" #include "ui_addentry.h" +#define OK_BUTTON 0 +#define UNUSED(expr) (void)(expr) + +//Default constructor, called from add entry AddEntry::AddEntry(QWidget *parent) : QDialog(parent), ui(new Ui::AddEntry){ ui->setupUi(this); ui->PassDoNot->setVisible(false); - ui->buttonBox->buttons()[0]->setEnabled(false); + ui->buttonBox->buttons()[0]->setEnabled(false); //Ok button initially disabled, as user has not entered any data setWindowTitle( tr("Fillout the new entry") ); - setModal( true ); + setModal(true); //Modal, so user cannot access main window without first closing this one. } +// Second constructor, called from eddit entry AddEntry::AddEntry(QWidget *parent, QString EditUserIn, QString EditPassIn, QString EditDomIn ) : QDialog(parent), ui(new Ui::AddEntry) { - ui->setupUi(this); + //Initialize fields with the data to edit ui->InUser->setText(EditUserIn); ui->InDomain->setText(EditDomIn); ui->InPass->setText(EditPassIn); ui->InPass2->setText(EditPassIn); ui->PassDoNot->setVisible(false); - ui->buttonBox->buttons()[0]->setEnabled(false); - setWindowTitle( tr("Fillout the new entry") ); - setModal( true ); + ui->buttonBox->buttons()[OK_BUTTON]->setEnabled(false); + setWindowTitle( tr("Edit the entry") ); + setModal(true); } AddEntry::~AddEntry(){ @@ -45,56 +50,62 @@ QString AddEntry::getPassword(){ return ui->InPass->text(); } - +// Each time the user modifies one of the text fields, we check if all of them have data, so we can enable Ok button void AddEntry::EnableOkButton(){ bool ok = !ui->InDomain->text().isEmpty() && !ui->InUser->text().isEmpty() && !ui->InPass->text().isEmpty() && !ui->InPass2->text().isEmpty(); - ui->buttonBox->buttons()[0]->setEnabled(ok); + ui->buttonBox->buttons()[OK_BUTTON]->setEnabled(ok); } +// If passwords are not equal, warning message void AddEntry::PasswordWarning(){ if (ui->InPass->text() == ui->InPass2->text()) - EqPass=true; + EqPass=true; // Flag, to allow ok button else EqPass=false; ui->PassDoNot->setVisible(!EqPass); } +// If the user wants to show the passwords, change EchoMode +void AddEntry::on_ShowPass_toggled(bool checked) +{ + if (checked){ + ui->InPass->setEchoMode(QLineEdit::Normal); + ui->InPass2->setEchoMode(QLineEdit::Normal); + } + else{ + ui->InPass->setEchoMode(QLineEdit::Password); + ui->InPass2->setEchoMode(QLineEdit::Password); + } +} + void AddEntry::on_InUser_textChanged(const QString &arg1){ EnableOkButton(); + UNUSED(arg1); } void AddEntry::on_InDomain_textChanged(const QString &arg1){ EnableOkButton(); + UNUSED(arg1); } void AddEntry::on_InPass_textChanged(const QString &arg1){ EnableOkButton(); PasswordWarning(); + UNUSED(arg1); } void AddEntry::on_InPass2_textChanged(const QString &arg1){ EnableOkButton(); PasswordWarning(); + UNUSED(arg1); } - -void AddEntry::on_buttonBox_clicked(QAbstractButton *button){ +void AddEntry::on_buttonBox_clicked(QAbstractButton* button){ if (EqPass) - this->accept(); -} - -void AddEntry::on_ShowPass_toggled(bool checked) -{ - if (checked){ - ui->InPass->setEchoMode(QLineEdit::Normal); - ui->InPass2->setEchoMode(QLineEdit::Normal); - } - else{ - ui->InPass->setEchoMode(QLineEdit::Password); - ui->InPass2->setEchoMode(QLineEdit::Password); - } + this->accept(); //Only emith accept() when passwords are equal + UNUSED(button); } diff --git a/scr/addentry.h b/scr/addentry.h index 46caa26..db86f6a 100644 --- a/scr/addentry.h +++ b/scr/addentry.h @@ -15,10 +15,15 @@ class AddEntry : public QDialog Q_OBJECT public: + // Constructor when calling from add explicit AddEntry(QWidget *parent = 0); + + //Constructor when calling from edit. explicit AddEntry(QWidget *parent, QString EditUserIn, QString EditPassIn, QString EditDomIn); - ~AddEntry(); + ~AddEntry();//Destructor + + //Methods to access data from main. QString getUser(); QString getDomain(); QString getPassword(); @@ -30,7 +35,6 @@ private slots: void on_InPass_textChanged(const QString &arg1); void on_InPass2_textChanged(const QString &arg1); void on_buttonBox_clicked(QAbstractButton *button); - void on_ShowPass_toggled(bool checked); private: diff --git a/scr/dbmanager.cpp b/scr/dbmanager.cpp deleted file mode 100644 index 8ab88e9..0000000 --- a/scr/dbmanager.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include -#include - -DbManager::DbManager(const QString &fileName){ - const QString DRIVER("QSQLITE"); - - if(!(QSqlDatabase::isDriverAvailable(DRIVER))) { - qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available"; - exit (1); - } - - m_db = QSqlDatabase::addDatabase(DRIVER); - - m_db.setDatabaseName(fileName); - if(!m_db.open()){ - qWarning() << "ERROR: " << m_db.lastError(); - exit (1); - } -} - -DbManager::~DbManager(){ - if (m_db.isOpen()) - m_db.close(); -} - -bool DbManager::isOpen() const{ - return m_db.isOpen(); -} - -bool DbManager::createTable(){ - bool success = false; - QSqlQuery query; - - query.prepare("create table Wallet " - "(id integer primary key, " - "Username TEXT, " - "Password TEXT, " - "Domain TEXT )"); - - if (!query.exec()){ - qDebug() << "Couldn't create the table 'wallet': one might already exist."; - qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text(); - success = false; - } - return success; -} - - - - - diff --git a/scr/dbmanager.h b/scr/dbmanager.h deleted file mode 100644 index 0084765..0000000 --- a/scr/dbmanager.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef DBMANAGER_H -#define DBMANAGER_H - -#include - -/** - * \class DbManager - * - * \brief SQL Database Manager class - * - * DbManager sets up the connection with SQL database - * and performs some basics queries. The class requires - * existing SQL database. You can create it with sqlite: - * 1. $ sqlite3 people.db - * 2. sqilte> CREATE TABLE people(ids integer primary key, name text); - * 3. sqlite> .quit - */ -class DbManager -{ -public: - /** - * @brief Constructor - * - * Constructor sets up connection with db and opens it - * @param path - absolute path to db file - */ - DbManager(const QString& path); - - /** - * @brief Destructor - * - * Close the db connection - */ - ~DbManager(); - - bool isOpen() const; - - /** - * @brief Creates a new 'people' table if it doesn't already exist - * @return true - 'people' table created successfully, false - table not created - */ - bool createTable(); - -private: - QSqlDatabase m_db; -}; - -#endif // DBMANAGER_H diff --git a/scr/deleteconfirmation.cpp b/scr/deleteconfirmation.cpp index 41f271d..961e66d 100644 --- a/scr/deleteconfirmation.cpp +++ b/scr/deleteconfirmation.cpp @@ -1,6 +1,7 @@ #include "deleteconfirmation.h" #include "ui_deleteconfirmation.h" +//Nothing to see here, just a simple dialog, configuration is done in the UI editor. DeleteConfirmation::DeleteConfirmation(QWidget *parent) : QDialog(parent), ui(new Ui::DeleteConfirmation) @@ -11,7 +12,6 @@ DeleteConfirmation::DeleteConfirmation(QWidget *parent) : setModal( true ); } -DeleteConfirmation::~DeleteConfirmation() -{ +DeleteConfirmation::~DeleteConfirmation(){ delete ui; } diff --git a/scr/mainwindow.cpp b/scr/mainwindow.cpp index f04f1ff..2068ffe 100644 --- a/scr/mainwindow.cpp +++ b/scr/mainwindow.cpp @@ -1,6 +1,10 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#define IDENT_COL 0 +#define PASS_COL 3 + +#define DRIVER "QSQLITE" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -8,22 +12,25 @@ MainWindow::MainWindow(QWidget *parent) : init(); } -MainWindow::~MainWindow() +MainWindow::~MainWindow()//Destructor { secure_finit(); /*Once the user has finished all the operations it is strictly required to call the secure_finit() to avoid memory leakege*/ - if(db.open()) // close anye existent database + if(db.open()){ // close anye existent connections and database + model->clear(); db.close(); + QSqlDatabase::removeDatabase(DRIVER); + db = QSqlDatabase(); + } if(s.logged_in) L1_logout(&s); - delete ui; } void MainWindow::init() { - + // Configure UI and initial state of buttons as not clickable because there is no wallet to edit yet ui->setupUi(this); ui->AddEntry->setEnabled(false); ui->EditEntry->setEnabled(false); @@ -32,9 +39,9 @@ void MainWindow::init() ui->TableTitle->setVisible(false); ui->WalletView->hide(); ui->WalletView->horizontalHeader()->setStretchLastSection(true); + setWindowTitle(tr("SEcube Wallet")); - - // Password login dialog + // SEcube Password login dialog LoginDialog* loginDialog = new LoginDialog( this ); loginDialog->exec(); @@ -43,6 +50,7 @@ void MainWindow::init() exit(1); } + // Get opened session in LoginDialog se3_session* tmp=loginDialog->getSession(); memcpy(&s, tmp, sizeof(se3_session)); if(L1_crypto_set_time(&s, (uint32_t)time(0))){ @@ -55,13 +63,9 @@ void MainWindow::init() exit(1); /*After the board is connected and the user is correctly logged in, the secure_init() should be issued. * The parameter se3_session *s contains all the information that let the system acknowledge which board - * is connected and if the user has successfully logged in. This function may set a default configuration - * thanks to the L1 provided services: it will be used thefirst available key for encryption, and the - * first available algorithm that can manage to encrypt and authenticate data at the same time. Since - * keys must not be shared outside the device, from the host side, the user may just request to use a - * key represented by a unique ID(uint32_t keyID).*/ + * is connected and if the user has successfully logged in.*/ } - + return; } @@ -71,38 +75,40 @@ void MainWindow::on_NewWallet_clicked() SecureFileDialog *fileDialog = new SecureFileDialog( this, 1 ); //option 1 means we are creating new file fileDialog->exec(); if(fileDialog->result()==QDialog::Rejected){ - return; + return; // Error in Dialog or Cancel button, do nothing } fileName = fileDialog->getChosenFile(); if(!fileName.isEmpty()){ - ui->TableTitle->setText(fileName); + ui->TableTitle->setText(fileName); // To display name of wallet in UI. ui->TableTitle->setVisible(true); - OpenDataBase(); // + if (!OpenDataBase()) + return; //error opening database CreateViewTable(); } + + return; } -void MainWindow::OpenDataBase (){ - qDebug() << "entered open data base"; +// Create/Open sqlite dataBase and create/open wallet table +bool MainWindow::OpenDataBase (){ - const QString DRIVER("QSQLITE"); if(!(QSqlDatabase::isDriverAvailable(DRIVER))) { //Check if sqlite is installed on OS qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available"; - exit (1); + exit (1); // as the application does not work without Sqlite, exit. } - if (db.open()){ //close any prev. opened database + if (db.open()){ //close any prev. opened connections and database model->clear(); db.close(); QSqlDatabase::removeDatabase(DRIVER); db = QSqlDatabase(); } - db = QSqlDatabase::addDatabase(DRIVER); + db = QSqlDatabase::addDatabase(DRIVER); db.setDatabaseName(fileName); if(!db.open()){ //Check if it was possible to open the database qWarning() << "ERROR: " << db.lastError(); - exit (1); + return false; } QSqlQuery query; @@ -110,70 +116,137 @@ void MainWindow::OpenDataBase (){ "(id integer primary key, " "Username TEXT, " "Domain TEXT, " - "Password TEXT )"); + "Password TEXT )"); //The table if (!query.exec()) - qDebug() << "Couldn't create the table 'wallet': one might already exist."; + qWarning() << "Couldn't create the table 'wallet': one might already exist"; - return; + return true; } +//After table is created in database, display it in a QTableView, and manage it using a Table Model void MainWindow::CreateViewTable(){ - qDebug() << "entered CreateViewTable"; - model = new QSqlTableModel; - + // Create and configure model to access table easily + model = new QSqlTableModel; model->setTable("Wallet"); model->select(); + model->setEditStrategy(QSqlTableModel::OnManualSubmit);// as the table will be not edditable, the changes are updated by calling submitAll() - ui->WalletView->setModel(model); - //ui->WalletView->setColumnHidden(0, true); //Hide Identification - ui->WalletView->setColumnHidden(3, true); - ui->WalletView->setEditTriggers(QAbstractItemView::NoEditTriggers); - ui->WalletView->setSelectionBehavior( QAbstractItemView::SelectItems ); - ui->WalletView->setSelectionMode( QAbstractItemView::SingleSelection ); - - - - model->setEditStrategy(QSqlTableModel::OnManualSubmit); - ui->WalletView->show(); - ui->AddEntry->setEnabled(true); - ui->CipherClose->setEnabled(true); + //Connect the model to the view table: Sqltable -> Model -> TableView + ui->WalletView->setModel(model); + //Configure the table + ui->WalletView->setEditTriggers(QAbstractItemView::NoEditTriggers);// To make the table view not edditable + //ui->WalletView->setColumnHidden(IDENT_COL, true);//Hide Identification column, not important for user, only for sqlite. + ui->WalletView->setColumnHidden(PASS_COL, true);//Initially hide passwords column + ui->WalletView->setSelectionBehavior( QAbstractItemView::SelectItems ); //To allow only one row selection... + ui->WalletView->setSelectionMode( QAbstractItemView::SingleSelection ); //So we can edit one entry per time + ui->WalletView->show();// show table, initially hidden + + //As we now have a wallet, we can enable the buttons ui->AddEntry->setEnabled(true); ui->EditEntry->setEnabled(true); ui->DeleteEntry->setEnabled(true); - ui->CipherClose->setEnabled(true); ui->Showpass->setChecked(false); - + ui->CipherClose->setEnabled(true); return; } - +//Call dialog and populate Table with the aqcuired data void MainWindow::on_AddEntry_clicked(){ AddEntry *add = new AddEntry(this); add->exec(); if(add->result()==QDialog::Rejected){ - return; + return; // Error or cancel, do nothing } - QSqlRecord rec = model->record(); - rec.setGenerated("id", false); + QSqlRecord rec = model->record(); // Temp entry + rec.setGenerated("id", false); // The ID column is managed automatically by the model, not by the user rec.setValue("Username", add->getUser()); rec.setValue("Password",add->getPassword()); rec.setValue("Domain",add->getDomain()); - int newRecNo = model->rowCount(); + int newRecNo = model->rowCount(); //Where to insert the new record?: at the end of the table if (model->insertRecord(newRecNo, rec)) - model->submitAll(); + model->submitAll();// if insert ok, submit changes. + + return; +} + +//Call dialog to allow user edit the data +void MainWindow::on_EditEntry_clicked() +{ + QModelIndexList selection = ui->WalletView->selectionModel()->selectedIndexes();//Get Items selected in table + model->submitAll();//Submit any pending changes before continue. (cannot be done before select bcs it deletes it) + + // If Multiple rows can be selected (Disabled for edit. Could be good for delete) + for(int i=0; i< selection.count(); i++){ + QModelIndex index = selection.at(i); + QSqlRecord rec = model->record(index.row());//Get the entry in the row selected by user + + //Call add entry with second constructor, so we can pass it the values to edit + AddEntry *add = new AddEntry(this, rec.value("Username").toString(),rec.value("Password").toString(), rec.value("Domain").toString()); + add->exec(); + + if(add->result()==QDialog::Rejected) + return; // If error or cancel, do nothing + //Change the values in record + rec.setGenerated("id", false); + rec.setValue("Username", add->getUser()); + rec.setValue("Password",add->getPassword()); + rec.setValue("Domain",add->getDomain()); + + //Write back record in original position + if (model->setRecord(index.row(), rec)) + if(!model->submitAll()) //Submit changes if no errors + qDebug() << "Error: " << model->lastError(); + } + return; +} + +void MainWindow::on_DeleteEntry_clicked() +{ + QModelIndexList selection = ui->WalletView->selectionModel()->selectedIndexes();//Get Items selected in table + model->submitAll();//Submit any pending changes before continue. (cannot be done before select bcs it deletes it) + + // If Multiple rows can be selected (disabled to allow edit) + for(int i=0; i< selection.count(); i++){ + //Before continuing, promt confirmation dialog + DeleteConfirmation * conf = new DeleteConfirmation; + conf->exec(); + if(conf->result()==QDialog::Rejected) + return;//if error or cancel, do nothing + + QModelIndex index = selection.at(i); + model->removeRow(index.row()); + if(!model->submitAll()) //Submit changes if no errors + qDebug() << "Error: " << model->lastError(); + } + return; +} + + +//When user wants, make passwords visible +void MainWindow::on_Showpass_toggled(bool checked){ + ui->WalletView->setColumnHidden(PASS_COL, !checked); + return; } +//To change envirolment, key ID and encryption +void MainWindow::on_EnvironmentBut_clicked(){ + EnvironmentDialog *envDialog = new EnvironmentDialog(this, &s); + envDialog->exec(); + return; +} + +//Cipher database file and delete the plain file. (From SeFile_IMG example) void MainWindow::on_CipherClose_clicked(){ uint16_t ret = SE3_OK; char enc_filename[65]; @@ -182,40 +255,41 @@ void MainWindow::on_CipherClose_clicked(){ memset(enc_filename, 0, 65); crypto_filename(fileName.toUtf8().data(), enc_filename, &enc_len ); - // Begin encryption of file QString *path_plainfile = new QString(fileName); if((ret = secure_open(path_plainfile->toUtf8().data(), &sefile_file, SEFILE_WRITE, SEFILE_NEWFILE))){ - if ( ret == SEFILE_SIGNATURE_MISMATCH ){ // Check if signature on header failed - // ui->plainName_lbl->setText("Signature Mismatch"); - // ui->encName_lbl->setText("Signature Mismatch"); - }else{ - // ui->plainName_lbl->setText("Error code: " + ret); - // ui->encName_lbl->setText("Error code: " + ret); - } - return; // todo error + if ( ret == SEFILE_SIGNATURE_MISMATCH ) // Check if signature on header failed + qDebug() << "Signature Mismatch"; + else + qDebug() << "Error in open sec file: " << ret; + + return; //Something went wrong with open file, do nothing } - QFile plain_file(fileName); - plain_file.open(QIODevice::ReadOnly); + //fileName holds the current database file + QFile plain_file(fileName); + plain_file.open(QIODevice::ReadOnly); //only need to read it qint64 size_len = plain_file.size(); + // Begin encryption of file uint8_t *buffer = (uint8_t*) malloc((size_t)size_len*sizeof(uint8_t)); memcpy(buffer, plain_file.readAll().data(), (size_t)size_len); if((ret = secure_write(&sefile_file, buffer, size_len))) { - if ( ret == SEFILE_SIGNATURE_MISMATCH ){ - // ui->plainName_lbl->setText("Signature Mismatch"); - // ui->encName_lbl->setText("Signature Mismatch"); - }else{ - // ui->plainName_lbl->setText("Error code: " + ret); - // ui->encName_lbl->setText("Error code: " + ret); - } + if ( ret == SEFILE_SIGNATURE_MISMATCH ) + qDebug() << "Signature Mismatch"; + else + qDebug() << "Error in open sec file: " << ret; + + free(buffer); + secure_close(&sefile_file); + plain_file.close(); + return; //Something went wrong with open file, do nothing } free(buffer); secure_close(&sefile_file); plain_file.close(); - // clean + // If encryption ok, delete plain file and set UI to "not wallet" state QFile::remove(fileName); ui->WalletView->hide(); ui->AddEntry->setEnabled(false); @@ -223,24 +297,24 @@ void MainWindow::on_CipherClose_clicked(){ ui->DeleteEntry->setEnabled(false); ui->CipherClose->setEnabled(false); ui->TableTitle->setVisible(false); - return; // todo error + return; } -void MainWindow::on_OpenCyphered_clicked() -{ +//Open Cyphered database and display it +void MainWindow::on_OpenCyphered_clicked(){ QByteArray plain_buffer; uint8_t *buffer; - char enc_filename[65]; uint16_t ret = SE3_OK; - uint16_t enc_len = 0; uint32_t nBytesRead = 0, file_len = 0; + //call securefileDialog. Option 0 displays the cyphered files in the current directory SecureFileDialog *fileDialog = new SecureFileDialog( this, 0 ); fileDialog->exec(); if(fileDialog->result()==QDialog::Rejected){ return ; } + path = fileDialog->getChosenFile(); fileName = QFileInfo(QFile(path)).fileName(); @@ -249,114 +323,46 @@ void MainWindow::on_OpenCyphered_clicked() QString *path_plainfile = new QString(path + fileName); + //Open file if((ret = secure_open(path_plainfile->toUtf8().data(), &sefile_file, SEFILE_WRITE, SEFILE_OPEN))){ - if ( ret == SEFILE_SIGNATURE_MISMATCH ){ // Check if signature on header failed - // ui->plainName_lbl->setText("Signature Mismatch"); - // ui->encName_lbl->setText("Signature Mismatch"); - }else{ - // ui->plainName_lbl->setText("Error code: " + ret); - // ui->encName_lbl->setText("Error code: " + ret); - } - return; // todo error + if ( ret == SEFILE_SIGNATURE_MISMATCH ) // Check if signature on header failed + qDebug() << "Signature Mismatch"; + else + qDebug() << "Error in open sec file: " << ret; + + return; //Something went wrong with open file, do nothing } + //Get size if((ret = secure_getfilesize(path_plainfile->toUtf8().data(), &file_len))){ - if ( ret == SEFILE_SIGNATURE_MISMATCH ){ - // ui->plainName_lbl->setText("Signature Mismatch"); - // ui->encName_lbl->setText("Signature Mismatch"); - secure_close(&sefile_file); - return; - }else if (ret > 0){ - // ui->plainName_lbl->setText("Error code: " + ret); - // ui->encName_lbl->setText("Error code: " + ret); - secure_close(&sefile_file); - return; - } + if ( ret == SEFILE_SIGNATURE_MISMATCH ) + qDebug() << "Signature Mismatch"; + else if (ret > 0) + qDebug() << "Error in open sec file: " << ret; + secure_close(&sefile_file); + return; } + + //Start decrypthion buffer = (uint8_t *)calloc(file_len, sizeof(uint8_t)); if((ret = secure_read(&sefile_file, buffer, file_len, &nBytesRead))){ - if ( ret == SEFILE_SIGNATURE_MISMATCH ){ - // ui->plainName_lbl->setText("Signature Mismatch"); - // ui->encName_lbl->setText("Signature Mismatch"); - secure_close(&sefile_file); - return; - }else if (ret > 0){ - // ui->plainName_lbl->setText("Error code: " + ret); - // ui->encName_lbl->setText("Error code: " + ret); - secure_close(&sefile_file); - return; - } + if ( ret == SEFILE_SIGNATURE_MISMATCH ) + qDebug() << "Signature Mismatch"; + else if (ret > 0) + qDebug() << "Error in open sec file: " << ret; + secure_close(&sefile_file); + return; } - plain_buffer = QByteArray((char *)buffer, (int)nBytesRead); - QFile plain_file(fileName); - plain_file.open(QIODevice::WriteOnly); - plain_file.write(plain_buffer); - plain_file.close(); - OpenDataBase(); + plain_buffer = QByteArray((char *)buffer, (int)nBytesRead); + QFile plain_file(fileName); //Create plain fail + plain_file.open(QIODevice::WriteOnly); //We need to write it + plain_file.write(plain_buffer);//Write + plain_file.close();//Close + + //Re-Open data base and create table. + if (!OpenDataBase()) + return; //error opening database CreateViewTable(); -} - -void MainWindow::on_DeleteEntry_clicked() -{ - QModelIndexList selection = ui->WalletView->selectionModel()->selectedIndexes(); - model->submitAll(); - - // Multiple rows can be selected (not anymore, disabled to allow edit) - for(int i=0; i< selection.count(); i++){ - - DeleteConfirmation * conf = new DeleteConfirmation; - conf->exec(); - if(conf->result()==QDialog::Rejected) - return; - - QModelIndex index = selection.at(i); - qDebug() << index.row(); - model->removeRows(index.row(),1); - } - if(!model->submitAll()) - qDebug() << "Error: " << model->lastError(); -} - -void MainWindow::on_EnvironmentBut_clicked() -{ - EnvironmentDialog *envDialog = new EnvironmentDialog(this, &s); - envDialog->exec(); - -} - - -void MainWindow::on_EditEntry_clicked() -{ - QModelIndexList selection = ui->WalletView->selectionModel()->selectedIndexes(); - model->submitAll(); - - // Multiple rows can be selected - for(int i=0; i< selection.count(); i++){ - QModelIndex index = selection.at(i); - qDebug() << index.row(); - - QSqlRecord rec = model->record(index.row()); - - AddEntry *add = new AddEntry(this, rec.value("Username").toString(),rec.value("Password").toString(), rec.value("Domain").toString()); - add->exec(); - - if(add->result()==QDialog::Rejected) - return; - - rec.setGenerated("id", false); - rec.setValue("Username", add->getUser()); - rec.setValue("Password",add->getPassword()); - rec.setValue("Domain",add->getDomain()); - - if (model->setRecord(index.row(), rec)); - - } - if(!model->submitAll()) - qDebug() << "Error: " << model->lastError(); - -} - -void MainWindow::on_Showpass_toggled(bool checked){ - ui->WalletView->setColumnHidden(3, !checked); + return; } diff --git a/scr/mainwindow.h b/scr/mainwindow.h index e797c7b..9323db7 100644 --- a/scr/mainwindow.h +++ b/scr/mainwindow.h @@ -1,8 +1,8 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H + #include -#include //other windows, dialogs #include "logindialog.h" @@ -16,19 +16,17 @@ #include "se3/L1.h" //Sqlite related -//#include "dbmanager.h" - -#include - -#include #include #include #include #include -#include #include - +//Miscellaneous +#include +#include +#include +#include namespace Ui { @@ -57,22 +55,20 @@ private slots: private: Ui::MainWindow *ui; - QString path, fileName; - se3_session s; // session variable - QSqlTableModel *model; - - SEFILE_FHANDLE sefile_file; - se3_key *keyTable; - se3_algo *algTable; + // SeCube related + se3_session s; // session variable + SEFILE_FHANDLE sefile_file; // Encrypted file - QSqlDatabase db; + // Database related + QSqlDatabase db; // The database + QSqlTableModel *model; // Model to handle tables in the database easily + QString path, fileName; // To store database filename //Methods - void init(); //initialization. Call LoginDialog - void CreateViewTable(); - void OpenDataBase(); - + void init(); //initialization. Call LoginDialog and configure UI + bool OpenDataBase(); //Create/Open Data base and create table, connections + void CreateViewTable();//Create the table model and display the data in the UI. }; #endif // MAINWINDOW_H diff --git a/scr/scr.pro b/scr/scr.pro index 46275a8..5bca5b2 100644 --- a/scr/scr.pro +++ b/scr/scr.pro @@ -29,7 +29,6 @@ SOURCES += main.cpp\ environmentdialog.cpp \ logindialog.cpp \ securefiledialog.cpp \ - dbmanager.cpp \ addentry.cpp \ deleteconfirmation.cpp @@ -37,7 +36,6 @@ HEADERS += mainwindow.h \ environmentdialog.h \ logindialog.h \ securefiledialog.h \ - dbmanager.h \ addentry.h \ deleteconfirmation.h diff --git a/scr/securefiledialog.cpp b/scr/securefiledialog.cpp index e679482..9d51765 100644 --- a/scr/securefiledialog.cpp +++ b/scr/securefiledialog.cpp @@ -6,7 +6,11 @@ SecureFileDialog::SecureFileDialog(QWidget *parent, int newFile) : QDialog(paren currentPath = QDir::currentPath(); this->newFile=newFile; setUpGUI(); - setWindowTitle( tr("Choose Wallet to open") ); + if (!newFile) + setWindowTitle( tr("Choose Wallet to open") ); + else + setWindowTitle( tr("Create New Wallet") ); + setModal( true ); } SecureFileDialog::~SecureFileDialog() @@ -168,5 +172,3 @@ void SecureFileDialog::refreshFileView(){ void SecureFileDialog::slotAbortFile(){ reject(); } - -