kword

kwmailmerge_kspread.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2004 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <kdebug.h>
22#include <tdeglobal.h>
23#include <tdelocale.h>
24
25#include <kspread_map.h>
26
27#include "kwmailmerge_kspread.h"
28#include "kwmailmerge_kspread_config.h"
29
30using namespace KSpread;
31
32KWMailMergeKSpread::KWMailMergeKSpread( TDEInstance *instance, TQObject *parent )
33 : KWMailMergeDataSource( instance, parent ), _spreadSheetNumber( 1 )
34{
35}
36
37KWMailMergeKSpread::~KWMailMergeKSpread()
38{
39}
40
41int KWMailMergeKSpread::getNumRecords() const
42{
43 return rows() - 2;
44}
45
46TQString KWMailMergeKSpread::getValue( const TQString &name, int record ) const
47{
48 if ( record < 0 )
49 return name;
50
51 const Cell* cell = _sheet->cellAt( _columnMap[ name ], record + 2 );
52
53 if ( cell )
54 return cellText( cell );
55 else
56 return i18n( "Unkown mail merge variable: %1" ).arg( name );
57}
58
59void KWMailMergeKSpread::load( TQDomElement& parentElem )
60{
61 TQDomNode contentNode = parentElem.namedItem( "CONTENT" );
62 if ( contentNode.isNull() )
63 return;
64
65 TQDomElement element = contentNode.toElement();
66 if ( element.isNull() )
67 return;
68
69 _url = element.attribute( TQString::fromLatin1( "URL" ) );
70 _spreadSheetNumber = element.attribute( TQString::fromLatin1( "SpreadSheetNumber" ) ).toInt();
71
72 initDocument();
73}
74
75void KWMailMergeKSpread::save( TQDomDocument& doc, TQDomElement& parent )
76{
77 TQDomElement content = doc.createElement( TQString::fromLatin1( "CONTENT" ) );
78 parent.appendChild( content );
79
80 content.setAttribute( "URL", _url.url() );
81 content.setAttribute( "SpreadSheetNumber", _spreadSheetNumber );
82}
83
84void KWMailMergeKSpread::refresh( bool )
85{
86}
87
88bool KWMailMergeKSpread::showConfigDialog( TQWidget *parent, int )
89{
90 KWMailMergeKSpreadConfig dlg( parent, this );
91
92 int retval = dlg.exec();
93 if ( retval )
94 initDocument();
95
96 return retval;
97}
98
99void KWMailMergeKSpread::initDocument()
100{
101 _document = new Doc();
102
103 connect( _document, TQ_SIGNAL( completed() ), TQ_SLOT( initSpreadSheets() ) );
104
105 _document->openURL( _url );
106}
107
108void KWMailMergeKSpread::initSpreadSheets()
109{
110
111 _columnMap.clear();
112 sampleRecord.clear();
113
114 TQPtrListIterator<Sheet> it( _document->map()->sheetList() );
115 int counter = 0;
116 for ( it.toFirst(); it.current(), counter < _spreadSheetNumber; ++it ) {
117 _sheet = it.current();
118 counter++;
119 }
120
121 if ( !_sheet ) {
122 kdError() << "No spread sheet available" << endl;
123 return;
124 }
125
126 if ( rows() < 2 ) // empty table
127 return;
128
129 int cols = columns();
130 for ( int i = 1; i < cols; ++i ) {
131 const Cell* cell = _sheet->cellAt( i, 1 );
132
133 // init record list
134 sampleRecord[ cellText( cell ) ] = cellText( cell );
135 _columnMap.insert( cellText( cell ), i );
136 }
137}
138
139int KWMailMergeKSpread::rows() const
140{
141 if ( !_sheet )
142 return 0;
143
144 int row = 1;
145
146 for (; row < _sheet->maxRow(); ) {
147 const Cell* cell = _sheet->cellAt( 1, row );
148 if ( cellText( cell ).isEmpty() )
149 break;
150
151 row++;
152 }
153
154 return row;
155}
156
157int KWMailMergeKSpread::columns() const
158{
159 if ( !_sheet )
160 return 0;
161
162 int col = 1;
163
164 for (; col < _sheet->maxColumn(); ) {
165 const Cell* cell = _sheet->cellAt( col, 1 );
166 if ( cellText( cell ).isEmpty() )
167 break;
168
169 col++;
170 }
171
172 return col;
173}
174
175TQString KWMailMergeKSpread::cellText( const Cell *cell ) const
176{
177 TQString text = TQString();
178
179 if ( !cell->isDefault() && !cell->isEmpty() ) {
180 if ( cell->isFormula() )
181 text = cell->strOutText();
182 else if ( !cell->link().isEmpty() )
183 text = cell->link();
184 else
185 text = cell->text();
186 }
187#if 0
188 switch( cell->content() ) {
189 case Cell::Text:
190 case Cell::Formula:
191 text = cell->strOutText();
192 break;
193 case Cell::RichText:
194 case Cell::VisualFormula:
195 text = cell->text(); // untested
196 break;
197 }
198 }
199#endif
200 return text;
201}
202
203extern "C"
204{
205 KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_kspread( TDEInstance *instance, TQObject *parent )
206 {
207 return new KWMailMergeKSpread( instance, parent );
208 }
209}
210
211#include "kwmailmerge_kspread.moc"