kword

KWQtSqlSerialDataSourceBase.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KWQtSqlSerialDataSourceBase.h"
21#include "KWQtSqlSerialDataSourceBase.moc"
22#include "KWQtSqlMailMergeOpen.h"
23#include <tqlayout.h>
24#include <tqdom.h>
25#include <kcombobox.h>
26#include <klineedit.h>
27#include <kpushbutton.h>
28#include <tqsqldatabase.h>
29#include <tqmessagebox.h>
30#include <kpassdlg.h>
31#include <tqsqlrecord.h>
32#include <tqsqlcursor.h>
33#include <tqdatatable.h>
34#include <kdebug.h>
35#include <tdelocale.h>
36#include <kiconloader.h>
37
38
39
40
41/******************************************************************
42 *
43 * Class: KWQtSqlSerialDataSourceBase
44 *
45 ******************************************************************/
46
47int KWQtSqlSerialDataSourceBase::connectionId=0;
48
49KWQtSqlSerialDataSourceBase::KWQtSqlSerialDataSourceBase(TDEInstance *inst,TQObject *parent)
50 : KWMailMergeDataSource(inst,parent)
51{
52 DataBaseConnection=TQString("KWTQTSQLPOWER")+parent->name()+TQString("--%1").arg(connectionId++);
53 port=i18n("default");
54}
55
56KWQtSqlSerialDataSourceBase::~KWQtSqlSerialDataSourceBase()
57{
58 TQSqlDatabase::removeDatabase(DataBaseConnection);
59}
60
61
62bool KWQtSqlSerialDataSourceBase::showConfigDialog(TQWidget *par,int action)
63{
64 bool ret=false;
65 if (action==KWSLOpen)
66 {
67 KWQtSqlMailMergeOpen *dia=new KWQtSqlMailMergeOpen(par,this);
68
69 ret=dia->exec();
70 if (ret) openDatabase();
71 delete dia;
72 }
73 return ret;
74}
75
76bool KWQtSqlSerialDataSourceBase::openDatabase()
77{
78 TQString pwd;
79 TQSqlDatabase::removeDatabase(DataBaseConnection);
80 database=TQSqlDatabase::addDatabase(driver,DataBaseConnection);
81 if (database)
82 {
83 if (database->lastError().type()!=TQSqlError::None)
84 {
85 TQMessageBox::critical(0,i18n("Error"),database->lastError().databaseText(),TQMessageBox::Abort,TQMessageBox::NoButton,TQMessageBox::NoButton);
86 return false;
87 }
88 database->setDatabaseName(databasename);
89 database->setUserName(username);
90 database->setHostName(hostname);
91 if ((port!=i18n("default"))&& (!port.isEmpty()))
92 database->setPort(port.toInt());
93
94 if (KPasswordDialog::getPassword(pwd, i18n("Please enter the password for the database connection"))
95 == KPasswordDialog::Accepted) database->setPassword(pwd);
96 if (database->open())
97 {
98 return true;
99 }
100 TQMessageBox::critical(0,i18n("Error"),database->lastError().databaseText(),TQMessageBox::Abort,TQMessageBox::NoButton,TQMessageBox::NoButton);
101 return false;
102 }
103 TQMessageBox::critical(0,i18n("Error"),i18n("Unable to create database object"),TQMessageBox::Abort,TQMessageBox::NoButton,TQMessageBox::NoButton);
104 return false;
105}