| 474 | } |
| 475 | |
| 476 | void DatabaseManagerWidget::testConnection() { |
| 477 | if (!m_current_connection) |
| 478 | return; |
| 479 | |
| 480 | // don't allow to test the connection for file DBs if the file doesn't exist |
| 481 | if (isFileDB(ui.cbDriver->currentText())) { |
| 482 | QString fileName{ui.leDatabase->text()}; |
| 483 | #ifdef HAVE_WINDOWS |
| 484 | if (!fileName.isEmpty() && fileName.at(1) != QLatin1Char(':')) |
| 485 | #else |
| 486 | if (!fileName.isEmpty() && fileName.at(0) != QLatin1Char('/')) |
| 487 | #endif |
| 488 | fileName = QDir::homePath() + QStringLiteral("/") + fileName; |
| 489 | |
| 490 | if (!QFile::exists(fileName)) { |
| 491 | KMessageBox::error(this, i18n("Failed to connect to the database '%1'.", m_current_connection->dbName), i18n("Connection Failed")); |
| 492 | return; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | WAIT_CURSOR; |
| 497 | const QString& driver = m_current_connection->driver; |
| 498 | QSqlDatabase db = QSqlDatabase::addDatabase(driver); |
| 499 | db.close(); |
| 500 | |
| 501 | // db name or custom connection string for ODBC, if available |
| 502 | if (isODBC(driver) && m_current_connection->customConnectionEnabled) |
| 503 | db.setDatabaseName(m_current_connection->customConnectionString); |
| 504 | else |
| 505 | db.setDatabaseName(m_current_connection->dbName); |
| 506 | |
| 507 | // host and port number, if required |
| 508 | if (!isFileDB(driver) && !isODBC(driver)) { |
| 509 | db.setHostName(m_current_connection->hostName); |
| 510 | db.setPort(m_current_connection->port); |
| 511 | } |
| 512 | |
| 513 | // authentication, if required |
| 514 | if (!isFileDB(driver)) { |
| 515 | db.setUserName(m_current_connection->userName); |
| 516 | db.setPassword(m_current_connection->password); |
| 517 | } |
| 518 | |
| 519 | if (db.isValid() && db.open() && db.isOpen()) { |
| 520 | db.close(); |
| 521 | RESET_CURSOR; |
| 522 | KMessageBox::information(this, i18n("Connection to the database '%1' was successful.", m_current_connection->dbName), i18n("Connection Successful")); |
| 523 | } else { |
| 524 | RESET_CURSOR; |
| 525 | KMessageBox::error(this, |
| 526 | i18n("Failed to connect to the database '%1'.", m_current_connection->dbName) + QStringLiteral("\n\n") |
| 527 | + db.lastError().databaseText(), |
| 528 | i18n("Connection Failed")); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /*! |
| 533 | * returns \c true if \c driver is for file databases like Sqlite or for ODBC datasources. |