summaryrefslogtreecommitdiffstats
path: root/kword/mailmerge/sql/KWQtSqlSerialDataSource.cpp
blob: 04bd18308fd4d07a5f94c362487fe66dda4a72de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* This file is part of the KDE project
   Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KWQtSqlSerialDataSource.h"
#include "KWQtSqlSerialDataSource.moc"
#include "KWQtSqlEasyFilter.h"

#include <tdelocale.h>
#include <tqlayout.h>
#include <tqsqlcursor.h>
#include <tqdatatable.h>
#include <tqcheckbox.h>
#include <tqsqlrecord.h>
#include <tqsqlquery.h>
#include <kdebug.h>

#define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )

/******************************************************************
 *
 * Class: KWQtSqlSerialDataSource
 *
 ******************************************************************/

KWQtSqlSerialDataSource::KWQtSqlSerialDataSource(TDEInstance *inst,TQObject *parent)
	: KWQtSqlSerialDataSourceBase(inst,parent)
{
  myquery=0;
}

KWQtSqlSerialDataSource::~KWQtSqlSerialDataSource()
{
        delete myquery;
        TQSqlDatabase::removeDatabase("KWTQTSQLPOWER");
}


TQString KWQtSqlSerialDataSource::getValue( const TQString &name, int record ) const
{
        int num=record;

        if (!myquery) return name;
        if ( num < 0 || num > (int)myquery->size() )
                return name;
        if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
        if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
        return (myquery->value(name)).toString();
}

void KWQtSqlSerialDataSource::save( TQDomDocument & /*doc*/, TQDomElement & /*parent*/)
{
/*
	TQDomElement def=doc.createElement(TQString::fromLatin1("DEFINITION"));
	parent.appendChild(def);
	for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
	{
		TQDomElement defEnt=doc.createElement(TQString::fromLatin1("FIELD"));
		defEnt.setAttribute(TQString::fromLatin1("name"),it.key());
		def.appendChild(defEnt);
	}
	TQDomElement cont=doc.createElement(TQString::fromLatin1("CONTENT"));
	parent.appendChild(cont);
	for (Db::Iterator dbI=db.begin();dbI!=db.end();++dbI)
	{
		TQDomElement rec=doc.createElement(TQString::fromLatin1("RECORD"));
		cont.appendChild(rec);
		for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
		{
			TQDomElement recEnt=doc.createElement(TQString::fromLatin1("ITEM"));
			recEnt.setAttribute(TQString::fromLatin1("name"),it.key());
			recEnt.setAttribute(TQString::fromLatin1("data"),(*dbI)[it.key()]);
			rec.appendChild(recEnt);
		}
	}
*/
}

void KWQtSqlSerialDataSource::load( TQDomElement& /*parentElem*/ )
{
/*
	db.clear();
	sampleRecord.clear();
	TQDomNode defNd=parentElem.namedItem("DEFINITION");
	if (defNd.isNull()) return;
	TQDomElement def=defNd.toElement();
	for (TQDomElement defEnt=def.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
	{
		sampleRecord[defEnt.attribute(TQString::fromLatin1("name"))]=defEnt.attribute(TQString::fromLatin1("name"));//i18n( "No Value" );
	}
	TQDomNode contNd=parentElem.namedItem("CONTENT");
	if (contNd.isNull()) return;
	for (TQDomNode rec=contNd.firstChild();!rec.isNull();rec=rec.nextSibling())
	{
		appendRecord();
		for (TQDomElement recEnt=rec.firstChild().toElement();!recEnt.isNull();recEnt=recEnt.nextSibling().toElement())
		{
			setValue(recEnt.attribute(TQString::fromLatin1("name")),
				recEnt.attribute(TQString::fromLatin1("data")),db.count()-1);
		}
	}
*/
}

bool KWQtSqlSerialDataSource::showConfigDialog(TQWidget *par,int action)
{
	bool ret=false;
	if (action==KWSLEdit)
	{
		KWQtSqlDataSourceEditor *dia=new KWQtSqlDataSourceEditor(par,this);
		ret=dia->exec();
		delete dia;
	}
	else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);

	return ret;
}

void KWQtSqlSerialDataSource::refresh(bool force)
{
        if ((force) || (myquery==0))
        {
                if (myquery)
                {
                        delete myquery;
                        myquery=0;
                }
                if ((!database) || (!database->isOpen())) openDatabase();
		if ((!database) || (!database->isOpen())) return;
                myquery=new TQSqlCursor(tableName,true,database);
                myquery->setMode(TQSqlCursor::ReadOnly);
		myquery->select(filter);
        }
        kdDebug()<<TQString("There were %1 rows in the query").arg(myquery->size())<<endl;

}





/******************************************************************
 *
 * Class: KWQtSqlDataSourceEditor
 *
 ******************************************************************/



KWQtSqlDataSourceEditor::KWQtSqlDataSourceEditor( TQWidget *parent, KWQtSqlSerialDataSource *db_ )
        :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
{
	tableName=db->tableName;
	filter=db->filter;
        (new TQVBoxLayout(plainPage()))->setAutoAdd(true);
        setMainWidget(widget=new QtSqlDataSourceEditor(plainPage()));
	connect(widget->tableCombo,TQT_SIGNAL(activated(int)),this,TQT_SLOT(tableChanged(int)));
	connect(widget->editFilter,TQT_SIGNAL(clicked()),this,TQT_SLOT(editFilter()));
	updateTableCombo();

//        connect(this,TQT_SIGNAL(okClicked()),this,TQT_SLOT(slotSetQuery()));
}

void KWQtSqlDataSourceEditor::tableChanged(int item)
{
	tableName=widget->tableCombo->text(item);
	TQSqlCursor *tmpCursor=new TQSqlCursor(tableName,true,db->database);
	tmpCursor->setMode(TQSqlCursor::ReadOnly);

	if (widget->filterCheckBox->isChecked()) tmpCursor->select(filter);

	widget->DataTable->setSqlCursor(tmpCursor,true,true);
	widget->DataTable->refresh(TQDataTable::RefreshAll);
}

void KWQtSqlDataSourceEditor::updateTableCombo()
{
	widget->tableCombo->clear();
        if (!db->database) return;
	widget->tableCombo->insertItem("");
        widget->tableCombo->insertStringList(db->database->tables());
}

void KWQtSqlDataSourceEditor::slotSetQuery()
{
        db->tableName=tableName;
	db->filter=filter;
        db->refresh(true);
}


void KWQtSqlDataSourceEditor::editFilter()
{
	KWQtSqlEasyFilter *f=new KWQtSqlEasyFilter(static_cast<TQWidget*>(parent()));
	f->exec();
}

extern "C" {
        KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb(TDEInstance *inst,TQObject *parent)
        {
                return new KWQtSqlSerialDataSource(inst,parent);
        }
}