bool QgsNewSpatialiteLayerDialog::createDb()
{
    QString dbPath = mDatabaseComboBox->currentText();
    if ( dbPath.isEmpty() )
        return false;

    QFile newDb( dbPath );
    if ( !newDb.exists() )
    {
        QString errCause;
        bool res = false;

        QString spatialite_lib = QgsProviderRegistry::instance()->library( "spatialite" );
        QLibrary* myLib = new QLibrary( spatialite_lib );
        bool loaded = myLib->load();
        if ( loaded )
        {
            QgsDebugMsg( "spatialite provider loaded" );

            typedef bool ( *createDbProc )( const QString&, QString& );
            createDbProc createDbPtr = ( createDbProc ) cast_to_fptr( myLib->resolve( "createDb" ) );
            if ( createDbPtr )
            {
                res = createDbPtr( dbPath, errCause );
            }
            else
            {
                errCause = "Resolving createDb(...) failed";
            }
        }
        delete myLib;

        if ( !res )
        {
            QMessageBox::warning( 0, tr( "SpatiaLite Database" ), errCause );
            pbnFindSRID->setEnabled( false );
        }
    }

    QFileInfo fi( newDb );
    if ( !fi.exists() )
    {
        pbnFindSRID->setEnabled( false );
        return false;
    }

    QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

    QSettings settings;
    if ( !settings.contains( key ) )
    {
        settings.setValue( "/SpatiaLite/connections/selected", fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
        settings.setValue( key, fi.canonicalFilePath() );

        QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
    }

    pbnFindSRID->setEnabled( true );

    return true;
}
bool QgsNewSpatialiteLayerDialog::createDb()
{
  QString dbPath = mDatabaseComboBox->currentText();
  if ( dbPath.isEmpty() )
    return false;

  QFile newDb( dbPath );
  if ( newDb.exists() )
  {
    QMessageBox msgBox;
    msgBox.setIcon( QMessageBox::Question );
    msgBox.setWindowTitle( tr( "The File Already Exists." ) );
    msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
    QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
    QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
    msgBox.setStandardButtons( QMessageBox::Cancel );
    msgBox.setDefaultButton( addNewLayerButton );
    int ret = msgBox.exec();
    if ( ret == QMessageBox::Cancel )
    {
      return false;
    }

    if ( msgBox.clickedButton() == overwriteButton )
    {
      newDb.remove();
    }
  }

  if ( !newDb.exists() )
  {
    QString errCause;
    bool res = false;

    QString spatialite_lib = QgsProviderRegistry::instance()->library( QStringLiteral( "spatialite" ) );
    QLibrary *myLib = new QLibrary( spatialite_lib );
    bool loaded = myLib->load();
    if ( loaded )
    {
      QgsDebugMsg( "SpatiaLite provider loaded" );

      typedef bool ( *createDbProc )( const QString &, QString & );
      createDbProc createDbPtr = ( createDbProc ) cast_to_fptr( myLib->resolve( "createDb" ) );
      if ( createDbPtr )
      {
        res = createDbPtr( dbPath, errCause );
      }
      else
      {
        errCause = QStringLiteral( "Resolving createDb(...) failed" );
      }
    }
    delete myLib;

    if ( !res )
    {
      QMessageBox::warning( nullptr, tr( "SpatiaLite Database" ), errCause );
      pbnFindSRID->setEnabled( false );
    }
  }

  QFileInfo fi( newDb );
  if ( !fi.exists() )
  {
    pbnFindSRID->setEnabled( false );
    return false;
  }

  QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

  QgsSettings settings;
  if ( !settings.contains( key ) )
  {
    settings.setValue( QStringLiteral( "SpatiaLite/connections/selected" ), fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
    settings.setValue( key, fi.canonicalFilePath() );

    QMessageBox::information( nullptr, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
  }

  pbnFindSRID->setEnabled( true );

  return true;
}