diff --git a/limereport/databrowser/lrconnectiondialog.cpp b/limereport/databrowser/lrconnectiondialog.cpp index 52ce1b8..60652cf 100644 --- a/limereport/databrowser/lrconnectiondialog.cpp +++ b/limereport/databrowser/lrconnectiondialog.cpp @@ -41,7 +41,7 @@ namespace LimeReport{ ConnectionDialog::ConnectionDialog(LimeReport::IConnectionController *conControl, LimeReport::ConnectionDesc* connectionDesc, QWidget *parent) : QDialog(parent), - ui(new Ui::ConnectionDialog),m_connection(connectionDesc),m_controller(conControl) + ui(new Ui::ConnectionDialog), m_connection(connectionDesc), m_controller(conControl), m_savedConnectionName("") { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose,true); @@ -56,6 +56,7 @@ ConnectionDialog::~ConnectionDialog() void ConnectionDialog::init() { ui->cbbDrivers->addItems(QSqlDatabase::drivers()); + ui->cbbUseDefaultConnection->setEnabled(!m_controller->containsDefaultConnection()); } void ConnectionDialog::showEvent(QShowEvent *) @@ -101,27 +102,20 @@ void ConnectionDialog::checkFieldsFill() bool ConnectionDialog::checkConnection() { QScopedPointer con(uiToConnection()); -// LimeReport::ConnectionDesc con; -// con.setName(ui->leConnectionName->text()+"_check"); -// con.setHost(ui->leServerName->text()); -// con.setUserName(ui->leUserName->text()); -// con.setPassword(ui->lePassword->text()); -// con.setDatabaseName(ui->leDataBase->text()); -// con.setDriver(ui->cbbDrivers->currentText()); if (!m_controller->checkConnectionDesc(con.data())){ throw LimeReport::ReportError(m_controller->lastError()); } return true; } -LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc) +ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc) { LimeReport::ConnectionDesc* result; if (conDesc) result = conDesc; else result = new LimeReport::ConnectionDesc(); - result ->setName(ui->leConnectionName->text()); + result ->setName(ConnectionDesc::connectionNameForReport(ui->leConnectionName->text())); result ->setHost(ui->leServerName->text()); result ->setDriver(ui->cbbDrivers->currentText()); result ->setUserName(ui->leUserName->text()); @@ -135,7 +129,8 @@ void ConnectionDialog::connectionToUI() { init(); if (!m_connection) return; - ui->leConnectionName->setText(m_connection->name()); + ui->leConnectionName->setText(ConnectionDesc::connectionNameForUser(m_connection->name())); + ui->cbbUseDefaultConnection->setChecked(m_connection->name().compare(QSqlDatabase::defaultConnection) == 0); ui->leDataBase->setText(m_connection->databaseName()); ui->leServerName->setText(m_connection->host()); ui->leUserName->setText(m_connection->userName()); @@ -144,10 +139,26 @@ void ConnectionDialog::connectionToUI() ui->cbAutoConnect->setChecked(m_connection->autoconnect()); } - 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); + } +} + } // namespace LimeReport + + + + + diff --git a/limereport/databrowser/lrconnectiondialog.h b/limereport/databrowser/lrconnectiondialog.h index acc86a5..531666a 100644 --- a/limereport/databrowser/lrconnectiondialog.h +++ b/limereport/databrowser/lrconnectiondialog.h @@ -50,7 +50,7 @@ protected: void init(); void checkFieldsFill(); bool checkConnection(); - LimeReport::ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0); + ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0); void connectionToUI(); signals: void conectionRegistred(LimeReport::ConnectionDesc* connectionDesc); @@ -58,11 +58,14 @@ private slots: void slotAccept(); void slotCheckConnection(); void on_toolButton_clicked(); + void on_cbbUseDefaultConnection_toggled(bool checked); + private: Ui::ConnectionDialog *ui; - LimeReport::ConnectionDesc* m_connection; + ConnectionDesc* m_connection; bool m_changeMode; - LimeReport::IConnectionController* m_controller; + IConnectionController* m_controller; + QString m_savedConnectionName; }; } // namespace LimeReport diff --git a/limereport/databrowser/lrconnectiondialog.ui b/limereport/databrowser/lrconnectiondialog.ui index 49525ec..aa06393 100644 --- a/limereport/databrowser/lrconnectiondialog.ui +++ b/limereport/databrowser/lrconnectiondialog.ui @@ -36,6 +36,13 @@ + + + + Use default application connection + + + diff --git a/limereport/databrowser/lrdatabrowser.cpp b/limereport/databrowser/lrdatabrowser.cpp index 37f6eaf..ba89566 100644 --- a/limereport/databrowser/lrdatabrowser.cpp +++ b/limereport/databrowser/lrdatabrowser.cpp @@ -126,17 +126,17 @@ void DataBrowser::slotSQLEditingFinished(SQLEditResult result) void DataBrowser::slotDeleteConnection() { - if (!getConnectionName().isEmpty()){ + if (!getConnectionName(NameForUser).isEmpty()){ if ( QMessageBox::critical( this, tr("Attention"), - tr("Do you really want to delete \"%1\" connection ?").arg(getConnectionName()), + tr("Do you really want to delete \"%1\" connection ?").arg(getConnectionName(NameForUser)), QMessageBox::Ok|QMessageBox::No, QMessageBox::No - )==QMessageBox::Ok + ) == QMessageBox::Ok ){ - m_report->dataManager()->removeConnection(getConnectionName()); + m_report->dataManager()->removeConnection(getConnectionName(NameForReport)); updateDataTree(); } } @@ -153,7 +153,7 @@ void DataBrowser::slotAddDataSource() #endif sqlEdit->setSettings(settings()); sqlEdit->setDataSources(m_report->dataManager()); - sqlEdit->setDefaultConnection(getConnectionName()); + sqlEdit->setDefaultConnection(getConnectionName(NameForReport)); connect(sqlEdit,SIGNAL(signalSqlEditingFinished(SQLEditResult)),this,SLOT(slotSQLEditingFinished(SQLEditResult))); sqlEdit->exec(); } @@ -173,7 +173,10 @@ void DataBrowser::updateDataTree() foreach(QString dataSourceName, m_report->datasourcesNames()){ QTreeWidgetItem *item=new QTreeWidgetItem(QStringList(dataSourceName),DataBrowserTree::Table); - QTreeWidgetItem *parentItem = findByNameAndType(m_report->dataManager()->connectionName(dataSourceName),DataBrowserTree::Connection); + QTreeWidgetItem *parentItem = findByNameAndType( + ConnectionDesc::connectionNameForUser(m_report->dataManager()->connectionName(dataSourceName)), + DataBrowserTree::Connection + ); if (parentItem){ parentItem->addChild(item); if (!parentItem->isExpanded()) ui->dataTree->expandItem(parentItem); @@ -336,11 +339,18 @@ QTreeWidgetItem* findConnectionItem(QTreeWidgetItem* item){ } } -QString DataBrowser::getConnectionName() +QString DataBrowser::getConnectionName(NameType nameType) { if (ui->dataTree->currentItem()){ QTreeWidgetItem * ci = findConnectionItem(ui->dataTree->currentItem()); - if (ci) return ci->text(0); + if (ci) { + switch (nameType) { + case NameForUser: + return ConnectionDesc::connectionNameForUser(ci->text(0)); + case NameForReport: + return ConnectionDesc::connectionNameForReport(ci->text(0)); + } + } else return QString(); }; return QString(); @@ -417,7 +427,11 @@ void DataBrowser::initConnections() ui->dataTree->clear(); QListitems; foreach(QString connectionName,m_report->dataManager()->connectionNames()){ - QTreeWidgetItem *item=new QTreeWidgetItem(ui->dataTree,QStringList(connectionName), DataBrowserTree::Connection); + QTreeWidgetItem *item=new QTreeWidgetItem( + ui->dataTree, + QStringList(ConnectionDesc::connectionNameForUser(connectionName)), + DataBrowserTree::Connection + ); if (m_report->dataManager()->isConnectionConnected(connectionName)) item->setIcon(0,QIcon(":/databrowser/images/database_connected")); else @@ -482,10 +496,10 @@ void DataBrowser::slotDataWindowClosed() void DataBrowser::slotChangeConnection() { - if (!getConnectionName().isEmpty()){ + if (!getConnectionName(NameForUser).isEmpty()){ ConnectionDialog *connectionEdit = new ConnectionDialog( this, - m_report->dataManager()->connectionByName(getConnectionName()), + m_report->dataManager()->connectionByName(getConnectionName(NameForReport)), this ); connectionEdit->setAttribute(Qt::WA_DeleteOnClose,true); @@ -501,7 +515,7 @@ void DataBrowser::slotChangeConnection() void DataBrowser::slotChangeConnectionState() { - QString connectionName = getConnectionName(); + QString connectionName = getConnectionName(NameForReport); if (!connectionName.isEmpty()){ if (!m_report->dataManager()->isConnectionConnected(connectionName)){ setCursor(Qt::WaitCursor); @@ -626,6 +640,12 @@ bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection) return result; } +bool DataBrowser::containsDefaultConnection() +{ + bool result = m_report->dataManager()->connectionByName(QSqlDatabase::defaultConnection); + return result; +} + QString DataBrowser::lastError() const { return m_lastError; @@ -778,4 +798,4 @@ void DataBrowser::on_variablesTree_itemDoubleClicked(QTreeWidgetItem *item, int } } -} // namespace LimeReport \ No newline at end of file +} // namespace LimeReport diff --git a/limereport/databrowser/lrdatabrowser.h b/limereport/databrowser/lrdatabrowser.h index b068742..7d0a91e 100644 --- a/limereport/databrowser/lrdatabrowser.h +++ b/limereport/databrowser/lrdatabrowser.h @@ -87,8 +87,9 @@ private slots: void on_variablesTree_itemDoubleClicked(QTreeWidgetItem *item, int); private: + enum NameType{NameForUser, NameForReport}; QString getDatasourceName(); - QString getConnectionName(); + QString getConnectionName(NameType nameType); QString getVariable(); bool isClosingWindows() const {return m_closingWindows;} QTreeWidgetItem * findByNameAndType(QString name, int itemType); @@ -108,6 +109,7 @@ private: void addConnectionDesc(ConnectionDesc *connection); void changeConnectionDesc(ConnectionDesc *connection); bool checkConnectionDesc(ConnectionDesc *connection); + bool containsDefaultConnection(); private: Ui::DataBrowser* ui; diff --git a/limereport/databrowser/lrsqleditdialog.cpp b/limereport/databrowser/lrsqleditdialog.cpp index ef292a8..31dd6f1 100644 --- a/limereport/databrowser/lrsqleditdialog.cpp +++ b/limereport/databrowser/lrsqleditdialog.cpp @@ -101,7 +101,7 @@ void SQLEditDialog::accept() else result.resultMode=SQLEditResult::SubProxy; } - result.connectionName=ui->cbbConnection->currentText(); + result.connectionName = ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()); result.datasourceName=ui->leDatasourceName->text(); result.sql=ui->textEditSQL->toPlainText(); result.dialogMode=m_dialogMode; @@ -159,15 +159,19 @@ void SQLEditDialog::check() void SQLEditDialog::initConnections() { foreach(QString connectionName, QSqlDatabase::connectionNames()){ - ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-connect.png"),connectionName); + ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-connect.png"),ConnectionDesc::connectionNameForUser(connectionName)); } + foreach(QString connectionName, m_datasources->connectionNames()){ + connectionName = (connectionName.compare(QSqlDatabase::defaultConnection)==0) ? + tr("defaultConnection") : connectionName; if (ui->cbbConnection->findText(connectionName,Qt::MatchExactly )==-1) - ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-disconnect.png"),connectionName); + ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-disconnect.png"),ConnectionDesc::connectionNameForUser(connectionName)); } + ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_defaultConnection)); if (!m_oldDatasourceName.isEmpty()){ - ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_datasources->connectionName(m_oldDatasourceName))); + ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(ConnectionDesc::connectionNameForUser(m_datasources->connectionName(m_oldDatasourceName)))); } } @@ -202,7 +206,7 @@ void SQLEditDialog::setDataSources(LimeReport::DataSourceManager *dataSources, Q void SQLEditDialog::setDefaultConnection(QString defaultConnection) { - m_defaultConnection=defaultConnection; + m_defaultConnection = ConnectionDesc::connectionNameForUser(defaultConnection); } void SQLEditDialog::slotDataSourceNameEditing() @@ -298,7 +302,11 @@ void SQLEditDialog::slotPreviewData() QMessageBox::critical(this,tr("Attention"),tr("Connection is not specified")); return; } - m_previewModel = m_datasources->previewSQL(ui->cbbConnection->currentText(),ui->textEditSQL->toPlainText(),ui->leMaster->text()); + m_previewModel = m_datasources->previewSQL( + ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()), + ui->textEditSQL->toPlainText(), + ui->leMaster->text() + ); if (m_previewModel){ ui->tvPreview->setModel(m_previewModel.data()); ui->gbDataPreview->setVisible(true); diff --git a/limereport/lrdatadesignintf.cpp b/limereport/lrdatadesignintf.cpp index daea32e..ccff471 100644 --- a/limereport/lrdatadesignintf.cpp +++ b/limereport/lrdatadesignintf.cpp @@ -378,7 +378,17 @@ bool ConnectionDesc::isEqual(const QSqlDatabase &db) (db.hostName() == m_connectionHost) && (db.connectionName() == m_connectionName) && (db.userName() == m_user) && - (db.password() == m_password); + (db.password() == m_password); +} + +QString ConnectionDesc::connectionNameForUser(const QString &connectionName) +{ + return connectionName.compare(QSqlDatabase::defaultConnection) == 0 ? tr("defaultConnection") : connectionName; +} + +QString ConnectionDesc::connectionNameForReport(const QString &connectionName) +{ + return connectionName.compare(tr("defaultConnection")) == 0 ? QSqlDatabase::defaultConnection : connectionName; } QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection) diff --git a/limereport/lrdatadesignintf.h b/limereport/lrdatadesignintf.h index 7323f80..d2c9685 100644 --- a/limereport/lrdatadesignintf.h +++ b/limereport/lrdatadesignintf.h @@ -132,6 +132,9 @@ public: bool isEqual(const QSqlDatabase& db); bool isInternal(){ return m_internal; } void setInternal(bool value) {m_internal = value;} +public: + static QString connectionNameForUser(const QString& connectionName); + static QString connectionNameForReport(const QString& connectionName); signals: void nameChanged(const QString& oldName,const QString& newName); private: @@ -150,6 +153,7 @@ public: virtual void addConnectionDesc(ConnectionDesc* connection) = 0; virtual void changeConnectionDesc(ConnectionDesc* connection) = 0; virtual bool checkConnectionDesc(ConnectionDesc* connection) = 0; + virtual bool containsDefaultConnection() = 0; virtual QString lastError() const = 0; }; diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index ea7e6e0..5607117 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -622,16 +622,17 @@ void DataSourceManager::removeDatasource(const QString &name) void DataSourceManager::removeConnection(const QString &connectionName) { - for(int i=0;iname()==connectionName){ - if (m_connections.at(i)->isInternal()){ + QList::iterator cit = m_connections.begin(); + while( cit != m_connections.end() ){ + if ( ((*cit)->name().compare(connectionName) == 0) && (*cit)->isInternal() ){ + { QSqlDatabase db = QSqlDatabase::database(connectionName); db.close(); - QSqlDatabase::removeDatabase(connectionName); } - delete m_connections.at(i); - m_connections.removeAt(i); + QSqlDatabase::removeDatabase(connectionName); } + delete (*cit); + cit = m_connections.erase(cit); } emit datasourcesChanged(); }