kword

KWQtSqlSerialDataSourceBase.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KWQtSqlSerialDataSourceBase.h"
00021 #include "KWQtSqlSerialDataSourceBase.moc"
00022 #include "KWQtSqlMailMergeOpen.h"
00023 #include <qlayout.h>
00024 #include <qdom.h>
00025 #include <kcombobox.h>
00026 #include <klineedit.h>
00027 #include <kpushbutton.h>
00028 #include <qsqldatabase.h>
00029 #include <qmessagebox.h>
00030 #include <kpassdlg.h>
00031 #include <qsqlrecord.h>
00032 #include <qsqlcursor.h>
00033 #include <qdatatable.h>
00034 #include <kdebug.h>
00035 #include <klocale.h>
00036 #include <kiconloader.h>
00037 
00038 
00039 
00040 
00041 /******************************************************************
00042  *
00043  * Class: KWQtSqlSerialDataSourceBase
00044  *
00045  ******************************************************************/
00046 
00047 int KWQtSqlSerialDataSourceBase::connectionId=0;
00048 
00049 KWQtSqlSerialDataSourceBase::KWQtSqlSerialDataSourceBase(KInstance *inst,QObject *parent)
00050     : KWMailMergeDataSource(inst,parent)
00051 {
00052     DataBaseConnection=QString("KWQTSQLPOWER")+parent->name()+QString("--%1").arg(connectionId++);
00053     port=i18n("default");
00054 }
00055 
00056 KWQtSqlSerialDataSourceBase::~KWQtSqlSerialDataSourceBase()
00057 {
00058     QSqlDatabase::removeDatabase(DataBaseConnection);
00059 }
00060 
00061 
00062 bool KWQtSqlSerialDataSourceBase::showConfigDialog(QWidget *par,int action)
00063 {
00064    bool ret=false;
00065    if (action==KWSLOpen)
00066    {
00067     KWQtSqlMailMergeOpen *dia=new KWQtSqlMailMergeOpen(par,this);
00068 
00069     ret=dia->exec();
00070     if (ret) openDatabase();
00071     delete dia;
00072    }
00073    return ret;
00074 }
00075 
00076 bool  KWQtSqlSerialDataSourceBase::openDatabase()
00077 {
00078     QCString pwd;
00079     QSqlDatabase::removeDatabase(DataBaseConnection);
00080         database=QSqlDatabase::addDatabase(driver,DataBaseConnection);
00081         if (database)
00082         {
00083                 if (database->lastError().type()!=QSqlError::None)
00084                 {
00085             QMessageBox::critical(0,i18n("Error"),database->lastError().databaseText(),QMessageBox::Abort,QMessageBox::NoButton,QMessageBox::NoButton);
00086             return false;
00087                 }
00088                 database->setDatabaseName(databasename);
00089                 database->setUserName(username);
00090                 database->setHostName(hostname);
00091         if ((port!=i18n("default"))&& (!port.isEmpty()))
00092             database->setPort(port.toInt());
00093 
00094         if (KPasswordDialog::getPassword(pwd, i18n("Please enter the password for the database connection"))
00095             == KPasswordDialog::Accepted) database->setPassword(pwd);
00096                 if (database->open())
00097                 {
00098                         return true;
00099                 }
00100         QMessageBox::critical(0,i18n("Error"),database->lastError().databaseText(),QMessageBox::Abort,QMessageBox::NoButton,QMessageBox::NoButton);
00101         return false;
00102         }
00103     QMessageBox::critical(0,i18n("Error"),i18n("Unable to create database object"),QMessageBox::Abort,QMessageBox::NoButton,QMessageBox::NoButton);
00104         return false;
00105 }
KDE Home | KDE Accessibility Home | Description of Access Keys