KWQtSqlPowerSerialDataSource.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 "KWQtSqlPowerSerialDataSource.h"
21#include "KWQtSqlPowerSerialDataSource.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
36#define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
37
38/******************************************************************
39 *
40 * Class: KWQtSqlSerialDataSource
41 *
42 ******************************************************************/
43
44KWQtSqlPowerSerialDataSource::KWQtSqlPowerSerialDataSource(TDEInstance *inst,TQObject *parent)
45 : KWQtSqlSerialDataSourceBase(inst,parent),myquery(0)
46{
47 port=i18n("default");
48}
49
50KWQtSqlPowerSerialDataSource::~KWQtSqlPowerSerialDataSource()
51{
52 if (myquery) delete myquery;
53 TQSqlDatabase::removeDatabase("KWTQTSQLPOWER");
54}
55
56void KWQtSqlPowerSerialDataSource::refresh(bool force)
57{
58 if ((force) || (myquery==0))
59 {
60 if (myquery)
61 {
62 delete myquery;
63 myquery=0;
64 }
65 TQString tmp=query.upper();
66 if (!tmp.startsWith("SELECT")) return;
67 if ((!database) || (!database->isOpen()))openDatabase();
68 myquery=new KWMySqlCursor(query,true,database);
69 myquery->setMode(TQSqlCursor::ReadOnly);
70 }
71 kdDebug()<<TQString("There were %1 rows in the query").arg(myquery->size())<<endl;
72}
73
74TQString KWQtSqlPowerSerialDataSource::getValue( const TQString &name, int record ) const
75{
76 int num=record;
77
78 if (!myquery) return name;
79 if ( num < 0 || num > (int)myquery->size() )
80 return name;
81 if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
82 if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
83 return (myquery->value(name)).toString();
84}
85
86void KWQtSqlPowerSerialDataSource::save( TQDomDocument &doc, TQDomElement &parent)
87{
88 TQDomElement def=doc.createElement(TQString::fromLatin1("DEFINITION"));
89 parent.appendChild(def);
90 {
91 TQDomElement defEnt=doc.createElement(TQString::fromLatin1("DATABASE"));
92 defEnt.setAttribute(TQString::fromLatin1("hostname"),hostname);
93 defEnt.setAttribute(TQString::fromLatin1("port"),port);
94 defEnt.setAttribute(TQString::fromLatin1("driver"),driver);
95 defEnt.setAttribute(TQString::fromLatin1("databasename"),databasename);
96 defEnt.setAttribute(TQString::fromLatin1("username"),username);
97 def.appendChild(defEnt);
98
99 defEnt=doc.createElement(TQString::fromLatin1("QUERY"));
100 defEnt.setAttribute(TQString::fromLatin1("value"),query);
101 def.appendChild(defEnt);
102
103 TQDomElement sampleEnt=doc.createElement(TQString::fromLatin1("SAMPLERECORD"));
104 parent.appendChild(sampleEnt);
105 for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
106 {
107 TQDomElement fieldEnt=doc.createElement(TQString::fromLatin1("FIELD"));
108 fieldEnt.setAttribute(TQString::fromLatin1("name"),it.key());
109 sampleEnt.appendChild(fieldEnt);
110 }
111 }
112}
113
114void KWQtSqlPowerSerialDataSource::load( TQDomElement& parentElem )
115{
116 clearSampleRecord();
117 TQDomNode defNd=parentElem.namedItem("DEFINITION");
118 if (!defNd.isNull())
119 {
120 TQDomElement def=defNd.toElement();
121 TQDomNode dbNd=def.namedItem("DATABASE");
122 if (!dbNd.isNull())
123 {
124 TQDomElement dbEnt=dbNd.toElement();
125 if (dbEnt.tagName()==TQString::fromLatin1("DATABASE"))
126 {
127 hostname=dbEnt.attribute(TQString::fromLatin1("hostname"));
128 port=dbEnt.attribute(TQString::fromLatin1("port"));
129 driver=dbEnt.attribute(TQString::fromLatin1("driver"));
130 databasename=dbEnt.attribute(TQString::fromLatin1("databasename"));
131 username=dbEnt.attribute(TQString::fromLatin1("username"));
132 }
133 }
134 TQDomNode queryNd=def.namedItem("QUERY");
135 if (!queryNd.isNull())
136 {
137 query=queryNd.toElement().attribute(TQString::fromLatin1("value"));
138 }
139 }
140
141 defNd=parentElem.namedItem("SAMPLERECORD");
142 if (!defNd.isNull())
143 {
144 TQDomElement def1=defNd.toElement();
145 for (TQDomElement defEnt=defNd.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
146 {
147 addSampleRecordEntry(defEnt.attribute(TQString::fromLatin1("name")));
148 }
149 }
150}
151
152bool KWQtSqlPowerSerialDataSource::showConfigDialog(TQWidget *par,int action)
153{
154 bool ret=false;
155 if (action==KWSLEdit)
156 {
157 if ((!database) || (!database->isOpen()))openDatabase();
158 KWQtSqlPowerMailMergeEditor *dia=new KWQtSqlPowerMailMergeEditor(par,this);
159 ret=dia->exec();
160 delete dia;
161 }
162 else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);
163 return ret;
164}
165
166void KWQtSqlPowerSerialDataSource::clearSampleRecord() {sampleRecord.clear();}
167
168void KWQtSqlPowerSerialDataSource::addSampleRecordEntry(TQString name)
169{sampleRecord[name]=name; }//i18n("No Value");}
170
171
172/******************************************************************
173 *
174 * Class: KWQtSqlMailMergeEditor
175 *
176 ******************************************************************/
177
178KWQtSqlPowerMailMergeEditor::KWQtSqlPowerMailMergeEditor( TQWidget *parent, KWQtSqlPowerSerialDataSource *db_ )
179 :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
180{
181 (new TQVBoxLayout(plainPage()))->setAutoAdd(true);
182 setMainWidget(widget=new KWQtSqlPowerWidget(plainPage()));
183 connect(widget->setup,TQ_SIGNAL(clicked()),this,TQ_SLOT(openSetup()));
184 connect(widget->tables,TQ_SIGNAL(currentChanged(TQListBoxItem*)),this,TQ_SLOT(slotTableChanged(TQListBoxItem*)));
185 connect(widget->execute,TQ_SIGNAL(clicked()),this,TQ_SLOT(slotExecute()));
186 connect(this,TQ_SIGNAL(okClicked()),this,TQ_SLOT(slotSetQuery()));
187 widget->query->setText(db->query);
188 updateDBViews();
189}
190
191void KWQtSqlPowerMailMergeEditor::slotSetQuery()
192{
193 db->query=widget->query->text();
194 db->refresh(true);
195}
196
197void KWQtSqlPowerMailMergeEditor::slotExecute()
198{
199 if (!db->database) if (!db->openDatabase()) return;
200 TQString tmp=widget->query->text().upper();
201 if (!tmp.startsWith("SELECT")) return;
202 KWMySqlCursor *cur=new KWMySqlCursor(widget->query->text(),true,db->database);
203 cur->setMode(TQSqlCursor::ReadOnly);
204
205 db->clearSampleRecord();
206 kdDebug()<<TQString("Fieldname count %1").arg(cur->count())<<endl;
207 for (uint i=0;i<cur->count();i++)
208 db->addSampleRecordEntry(cur->fieldName(i));
209
210 widget->queryresult->setSqlCursor(cur,true,true);
211 widget->queryresult->refresh(TQDataTable::RefreshAll);
212}
213
214void KWQtSqlPowerMailMergeEditor::slotTableChanged ( TQListBoxItem * item )
215{
216 widget->fields->clear();
217 if (item)
218 {
219 if (!db->database) return;
220 TQSqlRecord rec=db->database->record(item->text());
221 for (uint i=0;i<rec.count();i++)
222 {
223 widget->fields->insertItem(rec.fieldName(i));
224 }
225 }
226}
227
228void KWQtSqlPowerMailMergeEditor::openSetup()
229{
230 KWQtSqlMailMergeOpen *dia=new KWQtSqlMailMergeOpen(this,db);
231 if (dia->exec())
232 {
233 db->openDatabase();
234 updateDBViews();
235 }
236 delete dia;
237}
238
239
240void KWQtSqlPowerMailMergeEditor::updateDBViews()
241{
242 widget->fields->clear();
243 widget->tables->clear();
244 if (!db->database) return;
245 widget->tables->insertStringList(db->database->tables());
246}
247
248KWQtSqlPowerMailMergeEditor::~KWQtSqlPowerMailMergeEditor(){;}
249
250
251extern "C" {
252 KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb_power(TDEInstance *inst,TQObject *parent)
253 {
254 return new KWQtSqlPowerSerialDataSource(inst,parent);
255 }
256}