<commit_before>/*
    Copyright (c) 2010 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "dbconfigmysql.h"

#include "../../libs/xdgbasedirs_p.h"
#include "akdebug.h"
#include "utils.h"

#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>

using namespace Akonadi;

DbConfigMysql::DbConfigMysql()
  : mDatabaseProcess( 0 )
{
}

QString DbConfigMysql::driverName() const
{
  return QLatin1String( "QMYSQL" );
}

QString DbConfigMysql::databaseName() const
{
  return mDatabaseName;
}

bool DbConfigMysql::init( QSettings &settings )
{
  // determine default settings depending on the driver
  QString defaultDbName;
  QString defaultHostName;
  QString defaultOptions;
  QString defaultServerPath;
  QString defaultCleanShutdownCommand;

  const QString socketDirectory = Utils::preferredSocketDirectory( XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_misc" ) ) );

  defaultDbName = QLatin1String( "akonadi" );
  const bool defaultInternalServer = true;
#ifdef MYSQLD_EXECUTABLE
   if ( QFile::exists( QLatin1String( MYSQLD_EXECUTABLE ) ) )
     defaultServerPath = QLatin1String( MYSQLD_EXECUTABLE );
#endif
  const QStringList mysqldSearchPath = QStringList()
      << QLatin1String( "/usr/sbin" )
      << QLatin1String( "/usr/local/sbin" )
      << QLatin1String( "/usr/local/libexec" )
      << QLatin1String( "/usr/libexec" )
      << QLatin1String( "/opt/mysql/libexec" )
      << QLatin1String( "/opt/local/lib/mysql5/bin" )
      << QLatin1String( "/opt/mysql/sbin" );
  if ( defaultServerPath.isEmpty() )
    defaultServerPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqld" ), mysqldSearchPath );

  const QString mysqladminPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqladmin" ), mysqldSearchPath );
  if ( !mysqladminPath.isEmpty() ) {
#ifndef Q_OS_WIN
    defaultCleanShutdownCommand = QString::fromLatin1( "%1 shutdown --socket=%2/mysql.socket" )
                                      .arg( mysqladminPath )
                                      .arg( socketDirectory );
#else
    defaultCleanShutdownCommand = QString::fromLatin1( "%1 shutdown --shared-memory" ).arg( mysqladminPath );
#endif
  }

  mMysqlInstallDbPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysql_install_db" ), mysqldSearchPath );
  akDebug() << "Found mysql_install_db: " << mMysqlInstallDbPath;

  mMysqlCheckPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqlcheck" ), mysqldSearchPath );
  akDebug() << "Found mysqlcheck: " << mMysqlCheckPath;

  mInternalServer = settings.value( QLatin1String( "QMYSQL/StartServer" ), defaultInternalServer ).toBool();
  if ( mInternalServer ) {
#if !(defined Q_WS_WIN)
    defaultOptions = QString::fromLatin1( "UNIX_SOCKET=%1/mysql.socket" ).arg( socketDirectory );
#endif
  }

  // read settings for current driver
  settings.beginGroup( driverName() );
  mDatabaseName = settings.value( QLatin1String( "Name" ), defaultDbName ).toString();
  mHostName = settings.value( QLatin1String( "Host" ), defaultHostName ).toString();
  mUserName = settings.value( QLatin1String( "User" ) ).toString();
  mPassword = settings.value( QLatin1String( "Password" ) ).toString();
  mConnectionOptions = settings.value( QLatin1String( "Options" ), defaultOptions ).toString();
  mServerPath = settings.value( QLatin1String("ServerPath"), defaultServerPath ).toString();
  mCleanServerShutdownCommand = settings.value( QLatin1String( "CleanServerShutdownCommand" ), defaultCleanShutdownCommand ).toString();
  settings.endGroup();

  // verify settings and apply permanent changes (written out below)
  if ( mInternalServer && mConnectionOptions.isEmpty() )
    mConnectionOptions = defaultOptions;
  if ( mInternalServer && (mServerPath.isEmpty() || !QFile::exists( mServerPath ) ) )
    mServerPath = defaultServerPath;

  // store back the default values
  settings.beginGroup( driverName() );
  settings.setValue( QLatin1String( "Name" ), mDatabaseName );
  settings.setValue( QLatin1String( "Host" ), mHostName );
  settings.setValue( QLatin1String( "Options" ), mConnectionOptions );
  if ( !mServerPath.isEmpty() )
    settings.setValue( QLatin1String( "ServerPath" ), mServerPath );
  settings.setValue( QLatin1String( "StartServer" ), mInternalServer );
  settings.endGroup();
  settings.sync();

  // apply temporary changes to the settings
  if ( mInternalServer ) {
    mHostName.clear();
    mUserName.clear();
    mPassword.clear();
  }

  return true;<commit_msg>Ignore connection options unconditionally when using internal MySQL.<commit_after>/*
    Copyright (c) 2010 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "dbconfigmysql.h"

#include "../../libs/xdgbasedirs_p.h"
#include "akdebug.h"
#include "utils.h"

#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>

using namespace Akonadi;

DbConfigMysql::DbConfigMysql()
  : mDatabaseProcess( 0 )
{
}

QString DbConfigMysql::driverName() const
{
  return QLatin1String( "QMYSQL" );
}

QString DbConfigMysql::databaseName() const
{
  return mDatabaseName;
}

bool DbConfigMysql::init( QSettings &settings )
{
  // determine default settings depending on the driver
  QString defaultDbName;
  QString defaultHostName;
  QString defaultOptions;
  QString defaultServerPath;
  QString defaultCleanShutdownCommand;

  const QString socketDirectory = Utils::preferredSocketDirectory( XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_misc" ) ) );

  defaultDbName = QLatin1String( "akonadi" );
  const bool defaultInternalServer = true;
#ifdef MYSQLD_EXECUTABLE
   if ( QFile::exists( QLatin1String( MYSQLD_EXECUTABLE ) ) )
     defaultServerPath = QLatin1String( MYSQLD_EXECUTABLE );
#endif
  const QStringList mysqldSearchPath = QStringList()
      << QLatin1String( "/usr/sbin" )
      << QLatin1String( "/usr/local/sbin" )
      << QLatin1String( "/usr/local/libexec" )
      << QLatin1String( "/usr/libexec" )
      << QLatin1String( "/opt/mysql/libexec" )
      << QLatin1String( "/opt/local/lib/mysql5/bin" )
      << QLatin1String( "/opt/mysql/sbin" );
  if ( defaultServerPath.isEmpty() )
    defaultServerPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqld" ), mysqldSearchPath );

  const QString mysqladminPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqladmin" ), mysqldSearchPath );
  if ( !mysqladminPath.isEmpty() ) {
#ifndef Q_OS_WIN
    defaultCleanShutdownCommand = QString::fromLatin1( "%1 shutdown --socket=%2/mysql.socket" )
                                      .arg( mysqladminPath )
                                      .arg( socketDirectory );
#else
    defaultCleanShutdownCommand = QString::fromLatin1( "%1 shutdown --shared-memory" ).arg( mysqladminPath );
#endif
  }

  mMysqlInstallDbPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysql_install_db" ), mysqldSearchPath );
  akDebug() << "Found mysql_install_db: " << mMysqlInstallDbPath;

  mMysqlCheckPath = XdgBaseDirs::findExecutableFile( QLatin1String( "mysqlcheck" ), mysqldSearchPath );
  akDebug() << "Found mysqlcheck: " << mMysqlCheckPath;

  mInternalServer = settings.value( QLatin1String( "QMYSQL/StartServer" ), defaultInternalServer ).toBool();
  if ( mInternalServer ) {
#if !(defined Q_WS_WIN)
    defaultOptions = QString::fromLatin1( "UNIX_SOCKET=%1/mysql.socket" ).arg( socketDirectory );
#endif
  }

  // read settings for current driver
  settings.beginGroup( driverName() );
  mDatabaseName = settings.value( QLatin1String( "Name" ), defaultDbName ).toString();
  mHostName = settings.value( QLatin1String( "Host" ), defaultHostName ).toString();
  mUserName = settings.value( QLatin1String( "User" ) ).toString();
  mPassword = settings.value( QLatin1String( "Password" ) ).toString();
  mConnectionOptions = settings.value( QLatin1String( "Options" ), defaultOptions ).toString();
  mServerPath = settings.value( QLatin1String("ServerPath"), defaultServerPath ).toString();
  mCleanServerShutdownCommand = settings.value( QLatin1String( "CleanServerShutdownCommand" ), defaultCleanShutdownCommand ).toString();
  settings.endGroup();

  // verify settings and apply permanent changes (written out below)
  if ( mInternalServer )
    mConnectionOptions = defaultOptions;
  if ( mInternalServer && (mServerPath.isEmpty() || !QFile::exists( mServerPath ) ) )
    mServerPath = defaultServerPath;

  // store back the default values
  settings.beginGroup( driverName() );
  settings.setValue( QLatin1String( "Name" ), mDatabaseName );
  settings.setValue( QLatin1String( "Host" ), mHostName );
  settings.setValue( QLatin1String( "Options" ), mConnectionOptions );
  if ( !mServerPath.isEmpty() )
    settings.setValue( QLatin1String( "ServerPath" ), mServerPath );
  settings.setValue( QLatin1String( "StartServer" ), mInternalServer );
  settings.endGroup();
  settings.sync();

  // apply temporary changes to the settings
  if ( mInternalServer ) {
    mHostName.clear();
    mUserName.clear();
    mPassword.clear();
  }

  return true;
{'fraction_non_alphanumeric': 0.11335272590785399, 'fraction_numerical': 0.010134184104344562, 'mean_word_length': 4.39918946301925, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '507756', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3185, 'n_words': 825}