kword

KWQtSqlPowerSerialDataSource.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KWQtSqlPowerSerialDataSource.h"
00021 #include "KWQtSqlPowerSerialDataSource.moc"
00022 #include "KWQtSqlMailMergeOpen.h"
00023 #include <qlayout.h>
00024 #include <qdom.h>
00025 #include <kcombobox.h>
00026 #include <klineedit.h>
00027 #include <kpushbutton.h>
00028 #include <qsqldatabase.h>
00029 #include <qmessagebox.h>
00030 #include <kpassdlg.h>
00031 #include <qsqlrecord.h>
00032 #include <qsqlcursor.h>
00033 #include <qdatatable.h>
00034 #include <kdebug.h>
00035 
00036 #define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
00037 
00038 /******************************************************************
00039  *
00040  * Class: KWQtSqlSerialDataSource
00041  *
00042  ******************************************************************/
00043 
00044 KWQtSqlPowerSerialDataSource::KWQtSqlPowerSerialDataSource(KInstance *inst,QObject *parent)
00045     : KWQtSqlSerialDataSourceBase(inst,parent),myquery(0)
00046 {
00047     port=i18n("default");
00048 }
00049 
00050 KWQtSqlPowerSerialDataSource::~KWQtSqlPowerSerialDataSource()
00051 {
00052     if (myquery) delete myquery;
00053     QSqlDatabase::removeDatabase("KWQTSQLPOWER");
00054 }
00055 
00056 void KWQtSqlPowerSerialDataSource::refresh(bool force)
00057 {
00058     if ((force) || (myquery==0))
00059     {
00060         if (myquery)
00061         {
00062             delete myquery;
00063             myquery=0;
00064         }
00065         QString tmp=query.upper();
00066         if (!tmp.startsWith("SELECT")) return;
00067         if ((!database) || (!database->isOpen()))openDatabase();
00068         myquery=new KWMySqlCursor(query,true,database);
00069         myquery->setMode(QSqlCursor::ReadOnly);
00070     }
00071     kdDebug()<<QString("There were %1 rows in the query").arg(myquery->size())<<endl;
00072 }
00073 
00074 QString KWQtSqlPowerSerialDataSource::getValue( const QString &name, int record ) const
00075 {
00076     int num=record;
00077 
00078     if (!myquery) return name;
00079     if ( num < 0 || num > (int)myquery->size() )
00080         return name;
00081     if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
00082     if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
00083     return (myquery->value(name)).toString();
00084 }
00085 
00086 void KWQtSqlPowerSerialDataSource::save( QDomDocument &doc, QDomElement &parent)
00087 {
00088         QDomElement def=doc.createElement(QString::fromLatin1("DEFINITION"));
00089         parent.appendChild(def);
00090         {
00091                 QDomElement defEnt=doc.createElement(QString::fromLatin1("DATABASE"));
00092                 defEnt.setAttribute(QString::fromLatin1("hostname"),hostname);
00093                 defEnt.setAttribute(QString::fromLatin1("port"),port);
00094                 defEnt.setAttribute(QString::fromLatin1("driver"),driver);
00095                 defEnt.setAttribute(QString::fromLatin1("databasename"),databasename);
00096                 defEnt.setAttribute(QString::fromLatin1("username"),username);
00097                 def.appendChild(defEnt);
00098 
00099         defEnt=doc.createElement(QString::fromLatin1("QUERY"));
00100         defEnt.setAttribute(QString::fromLatin1("value"),query);
00101         def.appendChild(defEnt);
00102 
00103         QDomElement sampleEnt=doc.createElement(QString::fromLatin1("SAMPLERECORD"));
00104         parent.appendChild(sampleEnt);
00105             for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
00106             {
00107                     QDomElement fieldEnt=doc.createElement(QString::fromLatin1("FIELD"));
00108                     fieldEnt.setAttribute(QString::fromLatin1("name"),it.key());
00109                     sampleEnt.appendChild(fieldEnt);
00110             }
00111         }
00112 }
00113 
00114 void KWQtSqlPowerSerialDataSource::load( QDomElement& parentElem )
00115 {
00116         clearSampleRecord();
00117         QDomNode defNd=parentElem.namedItem("DEFINITION");
00118         if (!defNd.isNull())
00119     {
00120             QDomElement def=defNd.toElement();
00121         QDomNode dbNd=def.namedItem("DATABASE");
00122         if (!dbNd.isNull())
00123         {
00124             QDomElement dbEnt=dbNd.toElement();
00125             if (dbEnt.tagName()==QString::fromLatin1("DATABASE"))
00126             {
00127                 hostname=dbEnt.attribute(QString::fromLatin1("hostname"));
00128                 port=dbEnt.attribute(QString::fromLatin1("port"));
00129                 driver=dbEnt.attribute(QString::fromLatin1("driver"));
00130                 databasename=dbEnt.attribute(QString::fromLatin1("databasename"));
00131                 username=dbEnt.attribute(QString::fromLatin1("username"));
00132             }
00133         }
00134         QDomNode queryNd=def.namedItem("QUERY");
00135         if (!queryNd.isNull())
00136         {
00137             query=queryNd.toElement().attribute(QString::fromLatin1("value"));
00138         }
00139         }
00140 
00141         defNd=parentElem.namedItem("SAMPLERECORD");
00142         if (!defNd.isNull())
00143     {
00144         QDomElement def1=defNd.toElement();
00145             for (QDomElement defEnt=defNd.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
00146             {
00147                    addSampleRecordEntry(defEnt.attribute(QString::fromLatin1("name")));
00148             }
00149     }
00150 }
00151 
00152 bool KWQtSqlPowerSerialDataSource::showConfigDialog(QWidget *par,int action)
00153 {
00154    bool ret=false;
00155    if (action==KWSLEdit)
00156    {
00157         if ((!database) || (!database->isOpen()))openDatabase();
00158     KWQtSqlPowerMailMergeEditor *dia=new KWQtSqlPowerMailMergeEditor(par,this);
00159     ret=dia->exec();
00160     delete dia;
00161    }
00162     else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);
00163    return ret;
00164 }
00165 
00166 void KWQtSqlPowerSerialDataSource::clearSampleRecord() {sampleRecord.clear();}
00167 
00168 void KWQtSqlPowerSerialDataSource::addSampleRecordEntry(QString name)
00169 {sampleRecord[name]=name; }//i18n("No Value");}
00170 
00171 
00172 /******************************************************************
00173  *
00174  * Class: KWQtSqlMailMergeEditor
00175  *
00176  ******************************************************************/
00177 
00178 KWQtSqlPowerMailMergeEditor::KWQtSqlPowerMailMergeEditor( QWidget *parent, KWQtSqlPowerSerialDataSource *db_ )
00179     :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
00180 {
00181         (new QVBoxLayout(plainPage()))->setAutoAdd(true);
00182         setMainWidget(widget=new KWQtSqlPowerWidget(plainPage()));
00183     connect(widget->setup,SIGNAL(clicked()),this,SLOT(openSetup()));
00184     connect(widget->tables,SIGNAL(currentChanged(QListBoxItem*)),this,SLOT(slotTableChanged(QListBoxItem*)));
00185     connect(widget->execute,SIGNAL(clicked()),this,SLOT(slotExecute()));
00186     connect(this,SIGNAL(okClicked()),this,SLOT(slotSetQuery()));
00187     widget->query->setText(db->query);
00188     updateDBViews();
00189 }
00190 
00191 void KWQtSqlPowerMailMergeEditor::slotSetQuery()
00192 {
00193     db->query=widget->query->text();
00194     db->refresh(true);
00195 }
00196 
00197 void KWQtSqlPowerMailMergeEditor::slotExecute()
00198 {
00199     if (!db->database) if (!db->openDatabase()) return;
00200     QString tmp=widget->query->text().upper();
00201     if (!tmp.startsWith("SELECT")) return;
00202     KWMySqlCursor *cur=new KWMySqlCursor(widget->query->text(),true,db->database);
00203     cur->setMode(QSqlCursor::ReadOnly);
00204 
00205     db->clearSampleRecord();
00206     kdDebug()<<QString("Fieldname count %1").arg(cur->count())<<endl;
00207     for (uint i=0;i<cur->count();i++)
00208         db->addSampleRecordEntry(cur->fieldName(i));
00209 
00210     widget->queryresult->setSqlCursor(cur,true,true);
00211     widget->queryresult->refresh(QDataTable::RefreshAll);
00212 }
00213 
00214 void KWQtSqlPowerMailMergeEditor::slotTableChanged ( QListBoxItem * item )
00215 {
00216     widget->fields->clear();
00217     if (item)
00218     {
00219         if (!db->database) return;
00220         QSqlRecord rec=db->database->record(item->text());
00221         for (uint i=0;i<rec.count();i++)
00222         {
00223             widget->fields->insertItem(rec.fieldName(i));
00224         }
00225     }
00226 }
00227 
00228 void KWQtSqlPowerMailMergeEditor::openSetup()
00229 {
00230         KWQtSqlMailMergeOpen *dia=new KWQtSqlMailMergeOpen(this,db);
00231         if (dia->exec())
00232     {
00233          db->openDatabase();
00234          updateDBViews();
00235     }
00236         delete dia;
00237 }
00238 
00239 
00240 void KWQtSqlPowerMailMergeEditor::updateDBViews()
00241 {
00242     widget->fields->clear();
00243     widget->tables->clear();
00244     if (!db->database) return;
00245     widget->tables->insertStringList(db->database->tables());
00246 }
00247 
00248 KWQtSqlPowerMailMergeEditor::~KWQtSqlPowerMailMergeEditor(){;}
00249 
00250 
00251 extern "C" {
00252         KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb_power(KInstance *inst,QObject *parent)
00253         {
00254                 return new KWQtSqlPowerSerialDataSource(inst,parent);
00255         }
00256 }
KDE Home | KDE Accessibility Home | Description of Access Keys