2017-01-14 20:07:12 +01:00
# include " connectionmanagerwindow.h "
# include "ui_connectionmanagerwindow.h"
2017-02-01 19:59:07 +01:00
//#include "mainwindow.h"
2017-02-01 18:01:02 +01:00
# include "MasterController.h"
2017-01-14 22:03:58 +01:00
# include <QDataWidgetMapper>
2017-01-15 21:01:40 +01:00
# include <QMessageBox>
2017-01-14 22:03:58 +01:00
# include <QStandardItemModel>
2017-01-14 20:07:12 +01:00
# include "connectionlistmodel.h"
2017-02-01 18:01:02 +01:00
ConnectionManagerWindow : : ConnectionManagerWindow ( MasterController * master , QWidget * parent )
2017-01-14 20:07:12 +01:00
: QMainWindow ( parent )
, ui ( new Ui : : ConnectionManagerWindow )
2017-02-01 18:01:02 +01:00
// , m_listModel(new ConnectionListModel(this))
, m_masterController ( master )
2017-01-14 20:07:12 +01:00
{
ui - > setupUi ( this ) ;
2017-02-01 18:01:02 +01:00
ui - > listView - > setModel ( m_masterController - > getConnectionListModel ( ) ) ;
2017-01-14 20:07:12 +01:00
2017-01-15 12:27:36 +01:00
setupWidgetMappings ( ) ;
2017-01-14 22:03:58 +01:00
2017-01-14 20:07:12 +01:00
connect ( ui - > listView - > selectionModel ( ) ,
SIGNAL ( currentChanged ( QModelIndex , QModelIndex ) ) ,
this , SLOT ( on_currentChanged ( QModelIndex , QModelIndex ) ) ) ;
}
2017-01-14 22:03:58 +01:00
2017-01-14 20:07:12 +01:00
ConnectionManagerWindow : : ~ ConnectionManagerWindow ( )
{
2017-02-01 18:01:02 +01:00
// m_listModel->save();
2017-01-15 12:27:36 +01:00
2017-01-14 20:07:12 +01:00
delete ui ;
2017-02-01 18:01:02 +01:00
// delete m_listModel;
2017-01-14 22:03:58 +01:00
delete m_mapper ;
2017-01-14 20:07:12 +01:00
}
void ConnectionManagerWindow : : on_actionAdd_Connection_triggered ( )
{
2017-02-26 19:29:50 +01:00
// ConnectionConfig c;
// c.setName("new");
2017-02-01 18:01:02 +01:00
//m_listModel->add(c);
auto clm = m_masterController - > getConnectionListModel ( ) ;
2017-02-26 19:29:50 +01:00
clm - > newItem ( ) ;
2017-01-18 20:48:31 +01:00
// Select the new row
2017-02-01 18:01:02 +01:00
auto idx = clm - > index ( clm - > rowCount ( ) - 1 , 0 ) ;
2017-01-18 20:48:31 +01:00
ui - > listView - > selectionModel ( ) - > setCurrentIndex ( idx , QItemSelectionModel : : Select ) ;
2017-01-14 20:07:12 +01:00
}
void ConnectionManagerWindow : : on_currentChanged ( const QModelIndex & current ,
2017-02-02 07:22:54 +01:00
const QModelIndex & )
2017-01-14 20:07:12 +01:00
{
int currow = current . row ( ) ;
2017-02-01 18:01:02 +01:00
auto clm = m_masterController - > getConnectionListModel ( ) ;
clm - > save ( prevSelection ) ;
2017-01-14 22:03:58 +01:00
m_mapper - > setCurrentIndex ( currow ) ;
2017-01-18 20:48:31 +01:00
prevSelection = currow ;
2017-01-14 20:07:12 +01:00
}
void ConnectionManagerWindow : : on_actionDelete_connection_triggered ( )
{
auto ci = ui - > listView - > selectionModel ( ) - > currentIndex ( ) ;
if ( ci . isValid ( ) ) {
2017-02-01 18:01:02 +01:00
auto clm = m_masterController - > getConnectionListModel ( ) ;
clm - > removeRow ( ci . row ( ) ) ;
2017-01-14 20:07:12 +01:00
}
}
2017-01-14 22:03:58 +01:00
2017-01-15 12:27:36 +01:00
void ConnectionManagerWindow : : setupWidgetMappings ( )
2017-01-14 22:03:58 +01:00
{
2017-02-01 18:01:02 +01:00
auto clm = m_masterController - > getConnectionListModel ( ) ;
2017-01-15 12:27:36 +01:00
m_mapper = new QDataWidgetMapper ( this ) ;
2017-02-01 18:01:02 +01:00
m_mapper - > setModel ( clm ) ;
2017-01-15 12:27:36 +01:00
m_mapper - > addMapping ( ui - > edtName , 1 ) ;
m_mapper - > addMapping ( ui - > edtHost , 2 ) ;
m_mapper - > addMapping ( ui - > spinPort , 3 ) ;
m_mapper - > addMapping ( ui - > edtUser , 4 ) ;
m_mapper - > addMapping ( ui - > edtPassword , 5 ) ;
m_mapper - > addMapping ( ui - > edtDbname , 6 ) ;
m_mapper - > toFirst ( ) ;
2017-01-14 22:03:58 +01:00
}
2017-01-15 21:01:40 +01:00
void ConnectionManagerWindow : : on_actionConnect_triggered ( )
{
auto ci = ui - > listView - > selectionModel ( ) - > currentIndex ( ) ;
2017-02-12 08:13:38 +01:00
m_masterController - > openSqlWindowForConnection ( ci . row ( ) ) ;
2017-01-15 21:01:40 +01:00
}
void ConnectionManagerWindow : : on_actionQuit_application_triggered ( )
{
auto res = QMessageBox : : question ( this , " pglab " ,
tr ( " Close ALL windows? " ) , QMessageBox : : Yes , QMessageBox : : No ) ;
if ( res = = QMessageBox : : Yes ) {
QApplication : : quit ( ) ;
}
//closeAllWindows();
}
2017-02-01 18:01:02 +01:00
void ConnectionManagerWindow : : on_actionBackup_database_triggered ( )
{
2017-03-05 21:23:36 +01:00
auto ci = ui - > listView - > selectionModel ( ) - > currentIndex ( ) ;
m_masterController - > openBackupDlgForConnection ( ci . row ( ) ) ;
2017-02-01 18:01:02 +01:00
}
2017-02-12 08:13:38 +01:00
void ConnectionManagerWindow : : on_actionManage_server_triggered ( )
{
auto ci = ui - > listView - > selectionModel ( ) - > currentIndex ( ) ;
m_masterController - > openServerWindowForConnection ( ci . row ( ) ) ;
}
2017-02-26 19:29:50 +01:00
2017-03-05 21:23:36 +01:00
2017-02-26 19:29:50 +01:00
# include <botan/botan.h>
//#include <botan/base64.h>
//#include <botan/pbkdf.h>
//#include <botan/block_cipher.h>
//#include <botan/hex.h>
# include <botan/cryptobox.h>
2017-03-05 21:23:36 +01:00
2017-02-26 19:29:50 +01:00
void ConnectionManagerWindow : : on_testButton_clicked ( )
{
std : : string error = Botan : : runtime_version_check ( BOTAN_VERSION_MAJOR ,
BOTAN_VERSION_MINOR ,
BOTAN_VERSION_PATCH ) ;
if ( error . empty ( ) ) {
// Botan::AutoSeeded_RNG rng;
// Botan::secure_vector<Botan::byte> salt =
// //{ 0x3f, 0x0a, 0xb0, 0x11, 0x44, 0xfe, 0x9d, 0xf7, 0x85, 0xd3, 0x11, 0x38, 0xe2, 0xdf, 0x31, 0x42 };
// rng.random_vec(16);
// // salt should be random and saved with encrypted data so it can be used when we decrypt
// std::string password = "Hello kitty";
// std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf("PBKDF2(SHA-256)"));
// Botan::OctetString aes256_key = pbkdf->derive_key(32, password, salt.data(), salt.size(), 10000);
// std::string plaintext("Your great-grandfather gave this watch to your granddad for good luck. Unfortunately, Dane's luck wasn't as good as his old man's.");
// Botan::secure_vector<uint8_t> pt(plaintext.data(),plaintext.data()+plaintext.length());
// std::unique_ptr<Botan::Cipher_Mode> enc(Botan::get_cipher_mode("AES-256/CBC/PKCS7", Botan::ENCRYPTION));
// enc->set_key(aes256_key);
// //generate fresh nonce (IV)
// //std::unique_ptr<Botan::RandomNumberGenerator> rng(new Botan::AutoSeeded_RNG);
// std::vector<uint8_t> iv(enc->default_nonce_length());
// rng.randomize(iv.data(), iv.size());
// enc->start(iv);
// enc->finish(pt);
// //std::cout << std::endl << enc->name() << " with iv " << Botan::hex_encode(iv) << std::endl << Botan::hex_encode(pt);
//std::string s = aes256_key.as_string();// + "\n" + t.format_string();
std : : string passphrase = " my passphrase " ;
std : : string plaintext ( " password1234 " ) ;
try {
Botan : : AutoSeeded_RNG rng ;
std : : string encrypted = Botan : : CryptoBox : : encrypt ( ( const uint8_t * ) plaintext . data ( ) , plaintext . length ( ) , passphrase , rng ) ;
std : : string decrypted = Botan : : CryptoBox : : decrypt ( encrypted , passphrase ) ;
std : : string s = encrypted + " \n " + decrypted ;
QMessageBox : : information ( this , " pglab " ,
QString : : fromUtf8 ( s . c_str ( ) ) , QMessageBox : : Yes ) ;
}
catch ( Botan : : Decoding_Error & e ) {
QMessageBox : : information ( this , " pglab " ,
tr ( " Failure to decrypt " ) , QMessageBox : : Yes ) ;
}
}
else {
QMessageBox : : information ( this , " pglab " ,
QString : : fromUtf8 ( error . c_str ( ) ) , QMessageBox : : Yes ) ;
}
}