kword

kwmailmerge_kspread.cpp

00001 /*
00002    This file is part of the KDE project
00003    Copyright (C) 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <kdebug.h>
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 
00025 #include <kspread_map.h>
00026 
00027 #include "kwmailmerge_kspread.h"
00028 #include "kwmailmerge_kspread_config.h"
00029 
00030 using namespace KSpread;
00031 
00032 KWMailMergeKSpread::KWMailMergeKSpread( KInstance *instance, QObject *parent )
00033   : KWMailMergeDataSource( instance, parent ), _spreadSheetNumber( 1 )
00034 {
00035 }
00036 
00037 KWMailMergeKSpread::~KWMailMergeKSpread()
00038 {
00039 }
00040 
00041 int KWMailMergeKSpread::getNumRecords() const
00042 {
00043   return rows() - 2;
00044 }
00045 
00046 QString KWMailMergeKSpread::getValue( const QString &name, int record ) const
00047 {
00048   if ( record < 0 )
00049     return name;
00050 
00051   const Cell* cell = _sheet->cellAt( _columnMap[ name ], record + 2 );
00052 
00053   if ( cell )
00054     return cellText( cell );
00055   else
00056     return i18n( "Unkown mail merge variable: %1" ).arg( name );
00057 }
00058 
00059 void KWMailMergeKSpread::load( QDomElement& parentElem )
00060 {
00061   QDomNode contentNode = parentElem.namedItem( "CONTENT" );
00062   if ( contentNode.isNull() )
00063     return;
00064 
00065   QDomElement element = contentNode.toElement();
00066   if ( element.isNull() )
00067     return;
00068 
00069   _url = element.attribute( QString::fromLatin1( "URL" ) );
00070   _spreadSheetNumber = element.attribute( QString::fromLatin1( "SpreadSheetNumber" ) ).toInt();
00071 
00072   initDocument();
00073 }
00074 
00075 void KWMailMergeKSpread::save( QDomDocument& doc, QDomElement& parent )
00076 {
00077   QDomElement content = doc.createElement( QString::fromLatin1( "CONTENT" ) );
00078   parent.appendChild( content );
00079 
00080   content.setAttribute( "URL", _url.url() );
00081   content.setAttribute( "SpreadSheetNumber", _spreadSheetNumber );
00082 }
00083 
00084 void KWMailMergeKSpread::refresh( bool )
00085 {
00086 }
00087 
00088 bool KWMailMergeKSpread::showConfigDialog( QWidget *parent, int )
00089 {
00090   KWMailMergeKSpreadConfig dlg( parent, this );
00091 
00092   int retval = dlg.exec();
00093   if ( retval )
00094     initDocument();
00095 
00096   return retval;
00097 }
00098 
00099 void KWMailMergeKSpread::initDocument()
00100 {
00101   _document = new Doc();
00102 
00103   connect( _document, SIGNAL( completed() ), SLOT( initSpreadSheets() ) );
00104 
00105   _document->openURL( _url );
00106 }
00107 
00108 void KWMailMergeKSpread::initSpreadSheets()
00109 {
00110 
00111   _columnMap.clear();
00112   sampleRecord.clear();
00113 
00114   QPtrListIterator<Sheet> it( _document->map()->sheetList() );
00115   int counter = 0;
00116   for ( it.toFirst(); it.current(), counter < _spreadSheetNumber; ++it ) {
00117     _sheet = it.current();
00118     counter++;
00119   }
00120 
00121   if ( !_sheet ) {
00122     kdError() << "No spread sheet available" << endl;
00123     return;
00124   }
00125 
00126   if ( rows() < 2 ) // empty table
00127     return;
00128 
00129   int cols = columns();
00130   for ( int i = 1; i < cols; ++i ) {
00131     const Cell* cell = _sheet->cellAt( i, 1 );
00132 
00133     // init record list
00134     sampleRecord[ cellText( cell ) ] = cellText( cell );
00135     _columnMap.insert( cellText( cell ), i );
00136   }
00137 }
00138 
00139 int KWMailMergeKSpread::rows() const
00140 {
00141   if ( !_sheet )
00142     return 0;
00143 
00144   int row = 1;
00145 
00146   for (; row < _sheet->maxRow(); ) {
00147     const Cell* cell = _sheet->cellAt( 1, row );
00148     if ( cellText( cell ).isEmpty() )
00149       break;
00150 
00151     row++;
00152   }
00153 
00154   return row;
00155 }
00156 
00157 int KWMailMergeKSpread::columns() const
00158 {
00159   if ( !_sheet )
00160     return 0;
00161 
00162   int col = 1;
00163 
00164   for (; col < _sheet->maxColumn(); ) {
00165     const Cell* cell = _sheet->cellAt( col, 1 );
00166     if ( cellText( cell ).isEmpty() )
00167       break;
00168 
00169     col++;
00170   }
00171 
00172   return col;
00173 }
00174 
00175 QString KWMailMergeKSpread::cellText( const Cell *cell ) const
00176 {
00177   QString text = QString::null;
00178 
00179   if ( !cell->isDefault() && !cell->isEmpty() ) {
00180       if ( cell->isFormula() )
00181           text = cell->strOutText();
00182       else if ( !cell->link().isEmpty() )
00183           text = cell->link();
00184       else
00185           text = cell->text();
00186   }
00187 #if 0
00188       switch( cell->content() ) {
00189      case Cell::Text:
00190      case Cell::Formula:
00191       text = cell->strOutText();
00192       break;
00193      case Cell::RichText:
00194      case Cell::VisualFormula:
00195       text = cell->text(); // untested
00196       break;
00197     }
00198   }
00199 #endif
00200   return text;
00201 }
00202 
00203 extern "C"
00204 {
00205   KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_kspread( KInstance *instance, QObject *parent )
00206   {
00207     return new KWMailMergeKSpread( instance, parent );
00208   }
00209 }
00210 
00211 #include "kwmailmerge_kspread.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys