KWQtSqlSerialDataSource.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 "KWQtSqlSerialDataSource.h"
21#include "KWQtSqlSerialDataSource.moc"
22#include "KWQtSqlEasyFilter.h"
23
24#include <tdelocale.h>
25#include <tqlayout.h>
26#include <tqsqlcursor.h>
27#include <tqdatatable.h>
28#include <tqcheckbox.h>
29#include <tqsqlrecord.h>
30#include <tqsqlquery.h>
31#include <kdebug.h>
32
33#define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
34
35/******************************************************************
36 *
37 * Class: KWQtSqlSerialDataSource
38 *
39 ******************************************************************/
40
41KWQtSqlSerialDataSource::KWQtSqlSerialDataSource(TDEInstance *inst,TQObject *parent)
42 : KWQtSqlSerialDataSourceBase(inst,parent)
43{
44 myquery=0;
45}
46
47KWQtSqlSerialDataSource::~KWQtSqlSerialDataSource()
48{
49 delete myquery;
50 TQSqlDatabase::removeDatabase("KWTQTSQLPOWER");
51}
52
53
54TQString KWQtSqlSerialDataSource::getValue( const TQString &name, int record ) const
55{
56 int num=record;
57
58 if (!myquery) return name;
59 if ( num < 0 || num > (int)myquery->size() )
60 return name;
61 if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
62 if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
63 return (myquery->value(name)).toString();
64}
65
66void KWQtSqlSerialDataSource::save( TQDomDocument & /*doc*/, TQDomElement & /*parent*/)
67{
68/*
69 TQDomElement def=doc.createElement(TQString::fromLatin1("DEFINITION"));
70 parent.appendChild(def);
71 for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
72 {
73 TQDomElement defEnt=doc.createElement(TQString::fromLatin1("FIELD"));
74 defEnt.setAttribute(TQString::fromLatin1("name"),it.key());
75 def.appendChild(defEnt);
76 }
77 TQDomElement cont=doc.createElement(TQString::fromLatin1("CONTENT"));
78 parent.appendChild(cont);
79 for (Db::Iterator dbI=db.begin();dbI!=db.end();++dbI)
80 {
81 TQDomElement rec=doc.createElement(TQString::fromLatin1("RECORD"));
82 cont.appendChild(rec);
83 for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
84 {
85 TQDomElement recEnt=doc.createElement(TQString::fromLatin1("ITEM"));
86 recEnt.setAttribute(TQString::fromLatin1("name"),it.key());
87 recEnt.setAttribute(TQString::fromLatin1("data"),(*dbI)[it.key()]);
88 rec.appendChild(recEnt);
89 }
90 }
91*/
92}
93
94void KWQtSqlSerialDataSource::load( TQDomElement& /*parentElem*/ )
95{
96/*
97 db.clear();
98 sampleRecord.clear();
99 TQDomNode defNd=parentElem.namedItem("DEFINITION");
100 if (defNd.isNull()) return;
101 TQDomElement def=defNd.toElement();
102 for (TQDomElement defEnt=def.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
103 {
104 sampleRecord[defEnt.attribute(TQString::fromLatin1("name"))]=defEnt.attribute(TQString::fromLatin1("name"));//i18n( "No Value" );
105 }
106 TQDomNode contNd=parentElem.namedItem("CONTENT");
107 if (contNd.isNull()) return;
108 for (TQDomNode rec=contNd.firstChild();!rec.isNull();rec=rec.nextSibling())
109 {
110 appendRecord();
111 for (TQDomElement recEnt=rec.firstChild().toElement();!recEnt.isNull();recEnt=recEnt.nextSibling().toElement())
112 {
113 setValue(recEnt.attribute(TQString::fromLatin1("name")),
114 recEnt.attribute(TQString::fromLatin1("data")),db.count()-1);
115 }
116 }
117*/
118}
119
120bool KWQtSqlSerialDataSource::showConfigDialog(TQWidget *par,int action)
121{
122 bool ret=false;
123 if (action==KWSLEdit)
124 {
125 KWQtSqlDataSourceEditor *dia=new KWQtSqlDataSourceEditor(par,this);
126 ret=dia->exec();
127 delete dia;
128 }
129 else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);
130
131 return ret;
132}
133
134void KWQtSqlSerialDataSource::refresh(bool force)
135{
136 if ((force) || (myquery==0))
137 {
138 if (myquery)
139 {
140 delete myquery;
141 myquery=0;
142 }
143 if ((!database) || (!database->isOpen())) openDatabase();
144 if ((!database) || (!database->isOpen())) return;
145 myquery=new TQSqlCursor(tableName,true,database);
146 myquery->setMode(TQSqlCursor::ReadOnly);
147 myquery->select(filter);
148 }
149 kdDebug()<<TQString("There were %1 rows in the query").arg(myquery->size())<<endl;
150
151}
152
153
154
155
156
157/******************************************************************
158 *
159 * Class: KWQtSqlDataSourceEditor
160 *
161 ******************************************************************/
162
163
164
165KWQtSqlDataSourceEditor::KWQtSqlDataSourceEditor( TQWidget *parent, KWQtSqlSerialDataSource *db_ )
166 :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
167{
168 tableName=db->tableName;
169 filter=db->filter;
170 (new TQVBoxLayout(plainPage()))->setAutoAdd(true);
171 setMainWidget(widget=new QtSqlDataSourceEditor(plainPage()));
172 connect(widget->tableCombo,TQ_SIGNAL(activated(int)),this,TQ_SLOT(tableChanged(int)));
173 connect(widget->editFilter,TQ_SIGNAL(clicked()),this,TQ_SLOT(editFilter()));
174 updateTableCombo();
175
176// connect(this,TQ_SIGNAL(okClicked()),this,TQ_SLOT(slotSetQuery()));
177}
178
179void KWQtSqlDataSourceEditor::tableChanged(int item)
180{
181 tableName=widget->tableCombo->text(item);
182 TQSqlCursor *tmpCursor=new TQSqlCursor(tableName,true,db->database);
183 tmpCursor->setMode(TQSqlCursor::ReadOnly);
184
185 if (widget->filterCheckBox->isChecked()) tmpCursor->select(filter);
186
187 widget->DataTable->setSqlCursor(tmpCursor,true,true);
188 widget->DataTable->refresh(TQDataTable::RefreshAll);
189}
190
191void KWQtSqlDataSourceEditor::updateTableCombo()
192{
193 widget->tableCombo->clear();
194 if (!db->database) return;
195 widget->tableCombo->insertItem("");
196 widget->tableCombo->insertStringList(db->database->tables());
197}
198
199void KWQtSqlDataSourceEditor::slotSetQuery()
200{
201 db->tableName=tableName;
202 db->filter=filter;
203 db->refresh(true);
204}
205
206
207void KWQtSqlDataSourceEditor::editFilter()
208{
209 KWQtSqlEasyFilter *f=new KWQtSqlEasyFilter(static_cast<TQWidget*>(parent()));
210 f->exec();
211}
212
213extern "C" {
214 KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb(TDEInstance *inst,TQObject *parent)
215 {
216 return new KWQtSqlSerialDataSource(inst,parent);
217 }
218}