0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/databrowser/lrconnectiondialog.cpp

176 lines
6.9 KiB
C++
Raw Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
2021-08-18 20:21:36 +03:00
* Copyright (C) 2021 by Alexander Arin *
2016-02-17 10:11:00 +03:00
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
****************************************************************************/
#include "lrconnectiondialog.h"
#include "ui_lrconnectiondialog.h"
2016-02-17 10:11:00 +03:00
#include "lrglobal.h"
2016-02-17 10:11:00 +03:00
#include <QDebug>
#include <QFileDialog>
2016-02-17 10:11:00 +03:00
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
2016-02-17 10:11:00 +03:00
#include <stdexcept>
namespace LimeReport {
ConnectionDialog::ConnectionDialog(LimeReport::IConnectionController* conControl,
LimeReport::ConnectionDesc* connectionDesc, QWidget* parent):
2016-02-17 10:11:00 +03:00
QDialog(parent),
ui(new Ui::ConnectionDialog),
m_connection(connectionDesc),
m_controller(conControl),
m_savedConnectionName("")
2016-02-17 10:11:00 +03:00
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose, true);
m_changeMode = m_connection != 0;
2016-02-17 10:11:00 +03:00
}
ConnectionDialog::~ConnectionDialog() { delete ui; }
2016-02-17 10:11:00 +03:00
void ConnectionDialog::init()
{
ui->cbbDrivers->addItems(QSqlDatabase::drivers());
ui->cbbUseDefaultConnection->setEnabled(!m_controller->containsDefaultConnection());
2016-02-17 10:11:00 +03:00
}
void ConnectionDialog::showEvent(QShowEvent*) { connectionToUI(); }
2016-02-17 10:11:00 +03:00
void ConnectionDialog::slotAccept()
{
try {
checkFieldsFill();
if (ui->cbAutoConnect->isChecked())
checkConnection();
if (!m_connection) {
2016-02-17 10:11:00 +03:00
m_controller->addConnectionDesc(uiToConnection());
} else {
m_controller->changeConnectionDesc(uiToConnection(m_connection));
2016-02-17 10:11:00 +03:00
}
close();
} catch (LimeReport::ReportError& exception) {
QMessageBox::critical(this, tr("Error"), exception.what());
2016-02-17 10:11:00 +03:00
}
}
void ConnectionDialog::slotCheckConnection()
{
try {
2016-02-17 10:11:00 +03:00
checkConnection();
QMessageBox::information(this, tr("Connection"),
tr("Connection succsesfully established!"));
} catch (LimeReport::ReportError& exception) {
QMessageBox::critical(this, tr("Error"), exception.what());
2016-02-17 10:11:00 +03:00
}
}
void ConnectionDialog::checkFieldsFill()
{
if (ui->leConnectionName->text().isEmpty()) {
throw LimeReport::ReportError(tr("Connection Name is empty"));
}
if (!m_changeMode && QSqlDatabase::connectionNames().contains(ui->leConnectionName->text())) {
throw LimeReport::ReportError(tr("Connection with name ") + ui->leConnectionName->text()
+ tr(" already exists! "));
2016-02-17 10:11:00 +03:00
}
}
bool ConnectionDialog::checkConnection()
{
QScopedPointer<LimeReport::ConnectionDesc> con(uiToConnection());
if (!m_controller->checkConnectionDesc(con.data())) {
throw LimeReport::ReportError(m_controller->lastError());
2016-02-17 10:11:00 +03:00
}
return true;
}
ConnectionDesc* ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc)
2016-02-17 10:11:00 +03:00
{
LimeReport::ConnectionDesc* result;
if (conDesc)
result = conDesc;
else
result = new LimeReport::ConnectionDesc();
result->setName(ConnectionDesc::connectionNameForReport(ui->leConnectionName->text()));
result->setHost(ui->leServerName->text());
if (!ui->lePort->text().isEmpty())
result->setPort(ui->lePort->text());
result->setDriver(ui->cbbDrivers->currentText());
result->setUserName(ui->leUserName->text());
result->setPassword(ui->lePassword->text());
result->setDatabaseName(ui->leDataBase->text());
result->setAutoconnect(ui->cbAutoConnect->isChecked());
result->setKeepDBCredentials(!ui->cbbKeepCredentials->isChecked());
return result;
2016-02-17 10:11:00 +03:00
}
void ConnectionDialog::connectionToUI()
{
init();
if (!m_connection)
return;
ui->leConnectionName->setText(ConnectionDesc::connectionNameForUser(m_connection->name()));
ui->cbbUseDefaultConnection->setChecked(
m_connection->name().compare(QSqlDatabase::defaultConnection) == 0);
2016-02-17 10:11:00 +03:00
ui->leDataBase->setText(m_connection->databaseName());
ui->leServerName->setText(m_connection->host());
ui->leUserName->setText(m_connection->userName());
ui->lePassword->setText(m_connection->password());
ui->cbbDrivers->setCurrentIndex(ui->cbbDrivers->findText(m_connection->driver()));
ui->cbAutoConnect->setChecked(m_connection->autoconnect());
ui->cbbKeepCredentials->setChecked(!m_connection->keepDBCredentials());
ui->lePort->setText(m_connection->port());
2016-02-17 10:11:00 +03:00
}
void ConnectionDialog::on_toolButton_clicked()
{
ui->leDataBase->setText(QFileDialog::getOpenFileName());
}
void ConnectionDialog::on_cbbUseDefaultConnection_toggled(bool checked)
{
if (checked) {
m_savedConnectionName = ui->leConnectionName->text();
ui->leConnectionName->setText(tr("defaultConnection"));
ui->leConnectionName->setEnabled(false);
} else {
ui->leConnectionName->setText(m_savedConnectionName);
ui->leConnectionName->setEnabled(true);
}
}
void ConnectionDialog::on_toolButton_2_toggled(bool checked)
{
ui->lePassword->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
}
} // namespace LimeReport