/* This file is part of the KDE project Copyright (C) 2001 Joseph Wenninger 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 "KWTQtSqlPowerSerialDataSource.h" #include "KWTQtSqlPowerSerialDataSource.moc" #include "KWTQtSqlMailMergeOpen.h" #include #include #include #include #include #include #include #include #include #include #include #include #define KWTQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() ) /****************************************************************** * * Class: KWTQtSqlSerialDataSource * ******************************************************************/ KWTQtSqlPowerSerialDataSource::KWTQtSqlPowerSerialDataSource(KInstance *inst,TQObject *tqparent) : KWTQtSqlSerialDataSourceBase(inst,tqparent),myquery(0) { port=i18n("default"); } KWTQtSqlPowerSerialDataSource::~KWTQtSqlPowerSerialDataSource() { if (myquery) delete myquery; TQSqlDatabase::removeDatabase("KWTQTSTQLPOWER"); } void KWTQtSqlPowerSerialDataSource::refresh(bool force) { if ((force) || (myquery==0)) { if (myquery) { delete myquery; myquery=0; } TQString tmp=query.upper(); if (!tmp.startsWith("SELECT")) return; if ((!database) || (!database->isOpen()))openDatabase(); myquery=new KWMySqlCursor(query,true,database); myquery->setMode(TQSqlCursor::ReadOnly); } kdDebug()<size())< (int)myquery->size() ) return name; if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<"); if (!myquery->tqcontains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").tqarg(name); return (myquery->value(name)).toString(); } void KWTQtSqlPowerSerialDataSource::save( TQDomDocument &doc, TQDomElement &tqparent) { TQDomElement def=doc.createElement(TQString::tqfromLatin1("DEFINITION")); tqparent.appendChild(def); { TQDomElement defEnt=doc.createElement(TQString::tqfromLatin1("DATABASE")); defEnt.setAttribute(TQString::tqfromLatin1("hostname"),hostname); defEnt.setAttribute(TQString::tqfromLatin1("port"),port); defEnt.setAttribute(TQString::tqfromLatin1("driver"),driver); defEnt.setAttribute(TQString::tqfromLatin1("databasename"),databasename); defEnt.setAttribute(TQString::tqfromLatin1("username"),username); def.appendChild(defEnt); defEnt=doc.createElement(TQString::tqfromLatin1("TQUERY")); defEnt.setAttribute(TQString::tqfromLatin1("value"),query); def.appendChild(defEnt); TQDomElement sampleEnt=doc.createElement(TQString::tqfromLatin1("SAMPLERECORD")); tqparent.appendChild(sampleEnt); for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it) { TQDomElement fieldEnt=doc.createElement(TQString::tqfromLatin1("FIELD")); fieldEnt.setAttribute(TQString::tqfromLatin1("name"),it.key()); sampleEnt.appendChild(fieldEnt); } } } void KWTQtSqlPowerSerialDataSource::load( TQDomElement& tqparentElem ) { clearSampleRecord(); TQDomNode defNd=tqparentElem.namedItem("DEFINITION"); if (!defNd.isNull()) { TQDomElement def=defNd.toElement(); TQDomNode dbNd=def.namedItem("DATABASE"); if (!dbNd.isNull()) { TQDomElement dbEnt=dbNd.toElement(); if (dbEnt.tagName()==TQString::tqfromLatin1("DATABASE")) { hostname=dbEnt.attribute(TQString::tqfromLatin1("hostname")); port=dbEnt.attribute(TQString::tqfromLatin1("port")); driver=dbEnt.attribute(TQString::tqfromLatin1("driver")); databasename=dbEnt.attribute(TQString::tqfromLatin1("databasename")); username=dbEnt.attribute(TQString::tqfromLatin1("username")); } } TQDomNode queryNd=def.namedItem("TQUERY"); if (!queryNd.isNull()) { query=queryNd.toElement().attribute(TQString::tqfromLatin1("value")); } } defNd=tqparentElem.namedItem("SAMPLERECORD"); if (!defNd.isNull()) { TQDomElement def1=defNd.toElement(); for (TQDomElement defEnt=defNd.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement()) { addSampleRecordEntry(defEnt.attribute(TQString::tqfromLatin1("name"))); } } } bool KWTQtSqlPowerSerialDataSource::showConfigDialog(TQWidget *par,int action) { bool ret=false; if (action==KWSLEdit) { if ((!database) || (!database->isOpen()))openDatabase(); KWTQtSqlPowerMailMergeEditor *dia=new KWTQtSqlPowerMailMergeEditor(par,this); ret=dia->exec(); delete dia; } else ret=KWTQtSqlSerialDataSourceBase::showConfigDialog(par,action); return ret; } void KWTQtSqlPowerSerialDataSource::clearSampleRecord() {sampleRecord.clear();} void KWTQtSqlPowerSerialDataSource::addSampleRecordEntry(TQString name) {sampleRecord[name]=name; }//i18n("No Value");} /****************************************************************** * * Class: KWTQtSqlMailMergeEditor * ******************************************************************/ KWTQtSqlPowerMailMergeEditor::KWTQtSqlPowerMailMergeEditor( TQWidget *tqparent, KWTQtSqlPowerSerialDataSource *db_ ) :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, tqparent, "", true ), db( db_ ) { (new TQVBoxLayout(plainPage()))->setAutoAdd(true); setMainWidget(widget=new KWTQtSqlPowerWidget(plainPage())); connect(widget->setup,TQT_SIGNAL(clicked()),this,TQT_SLOT(openSetup())); connect(widget->tables,TQT_SIGNAL(currentChanged(TQListBoxItem*)),this,TQT_SLOT(slotTableChanged(TQListBoxItem*))); connect(widget->execute,TQT_SIGNAL(clicked()),this,TQT_SLOT(slotExecute())); connect(this,TQT_SIGNAL(okClicked()),this,TQT_SLOT(slotSetQuery())); widget->query->setText(db->query); updateDBViews(); } void KWTQtSqlPowerMailMergeEditor::slotSetQuery() { db->query=widget->query->text(); db->refresh(true); } void KWTQtSqlPowerMailMergeEditor::slotExecute() { if (!db->database) if (!db->openDatabase()) return; TQString tmp=widget->query->text().upper(); if (!tmp.startsWith("SELECT")) return; KWMySqlCursor *cur=new KWMySqlCursor(widget->query->text(),true,db->database); cur->setMode(TQSqlCursor::ReadOnly); db->clearSampleRecord(); kdDebug()<count())<count();i++) db->addSampleRecordEntry(cur->fieldName(i)); widget->queryresult->setSqlCursor(cur,true,true); widget->queryresult->refresh(TQDataTable::RefreshAll); } void KWTQtSqlPowerMailMergeEditor::slotTableChanged ( TQListBoxItem * item ) { widget->fields->clear(); if (item) { if (!db->database) return; TQSqlRecord rec=db->database->record(item->text()); for (uint i=0;ifields->insertItem(rec.fieldName(i)); } } } void KWTQtSqlPowerMailMergeEditor::openSetup() { KWTQtSqlMailMergeOpen *dia=new KWTQtSqlMailMergeOpen(this,db); if (dia->exec()) { db->openDatabase(); updateDBViews(); } delete dia; } void KWTQtSqlPowerMailMergeEditor::updateDBViews() { widget->fields->clear(); widget->tables->clear(); if (!db->database) return; widget->tables->insertStringList(db->database->tables()); } KWTQtSqlPowerMailMergeEditor::~KWTQtSqlPowerMailMergeEditor(){;} extern "C" { KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb_power(KInstance *inst,TQObject *tqparent) { return new KWTQtSqlPowerSerialDataSource(inst,tqparent); } }