dynamically load available sql plugins

Developers
Spockmeat
2006-11-07
2013-04-26
  • Spockmeat
    Spockmeat
    2006-11-07

    I wasn't sure where to post this, but I made a few modifications to the loginOptions.cpp file under the common directory that allows the login window to automatically get the available sql plugins and allow the user to select the one they prefer. I needed to do this to suppose the mysql database I'm using, and I find it much better to dynamically generate the plug in list rather than hard code it for a particular database. I don't see anywhere to post the file, so I'll just paste my modified version here (based on what was current in CVS as of this post date). I hope this is useful to someone, I tested it under linux and windows with the QMYSQL driver and it works for me.

    /*
    * Copyright (c) 2002-2006 by OpenMFG, LLC
    *
    * This program 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 2 of the License, or
    * (at your option) any later version.
    *
    * This program 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.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    *
    * If you do not wish to be bound by the terms of the GNU General Public
    * License, DO NOT USE THIS SOFTWARE.  Please contact OpenMFG, LLC at
    * info@openmfg.com for details on how to purchase a commercial license.
    */

    #include "loginOptions.h"

    #include <qvariant.h>
    #include <qsettings.h>
    #include "dbtools.h"

    /*
    *  Constructs a loginOptions as a child of 'parent', with the
    *  name 'name' and widget flags set to 'f'.
    *
    *  The dialog will by default be modeless, unless you set 'modal' to
    *  true to construct a modal dialog.
    */
    loginOptions::loginOptions(QWidget* parent, const char* name, bool modal, Qt::WFlags fl)
        : QDialog(parent, name, modal, fl)
    {
        setupUi(this);

        // fill in the sql driver list
        _driver->clear();
        _driver->addItems(QSqlDatabase::drivers());
        // signals and slots connections
        connect(_save, SIGNAL(clicked()), this, SLOT(sSave()));
        init();
    }

    /*
    *  Destroys the object and frees any allocated resources
    */
    loginOptions::~loginOptions()
    {
        // no need to delete child widgets, Qt does it all for us
    }

    /*
    *  Sets the strings of the subwidgets using the current
    *  language.
    */
    void loginOptions::languageChange()
    {
        retranslateUi(this);
    }

    void loginOptions::init()
    {
      _database->setFocus();
    }

    void loginOptions::set(ParameterList &pParams)
    {
      QVariant param;
      bool     valid;

      param = pParams.value("databaseURL", &valid);
      if (valid)
      {
        _databaseURL = param.toString();

        QString protocol;
        QString server;
        QString database;
        QString port;
        parseDatabaseURL(_databaseURL, protocol, server, database, port);
        int index = _driver->findText(protocol,Qt::MatchExactly);
        if(index == -1)
        {
            qDebug((QString("Error, the protocol %1 was not found!").arg(protocol)));
        // just set something
        _driver->setCurrentIndex(0);
        } else {
        _driver->setCurrentIndex(index);
        }
        _server->setText(server);
        _database->setText(database);
        _port->setText(port);
      }
    }

    void loginOptions::sSave()
    {
      buildDatabaseURL(_databaseURL, _driver->currentText(), _server->text(), _database->text(), _port->text());
      QSettings setting;
      setting.setPath("OpenMFG.com", "OpenRPT", QSettings::UserScope);
      setting.writeEntry("/OpenRPT/_databaseURL", _databaseURL);

      accept();
    }

     
    • Chris Ryan
      Chris Ryan
      2006-11-27

      I have committed these changes along with several modifications to have the code support aliases and user readable driver names.