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
|
/*
This file is part of the KDE project
Copyright (C) 2004 Tobias Koenig <tokoe@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 <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kspread_map.h>
#include "kwmailmerge_kspread.h"
#include "kwmailmerge_kspread_config.h"
using namespace KSpread;
KWMailMergeKSpread::KWMailMergeKSpread( KInstance *instance, QObject *parent )
: KWMailMergeDataSource( instance, parent ), _spreadSheetNumber( 1 )
{
}
KWMailMergeKSpread::~KWMailMergeKSpread()
{
}
int KWMailMergeKSpread::getNumRecords() const
{
return rows() - 2;
}
QString KWMailMergeKSpread::getValue( const QString &name, int record ) const
{
if ( record < 0 )
return name;
const Cell* cell = _sheet->cellAt( _columnMap[ name ], record + 2 );
if ( cell )
return cellText( cell );
else
return i18n( "Unkown mail merge variable: %1" ).arg( name );
}
void KWMailMergeKSpread::load( QDomElement& parentElem )
{
QDomNode contentNode = parentElem.namedItem( "CONTENT" );
if ( contentNode.isNull() )
return;
QDomElement element = contentNode.toElement();
if ( element.isNull() )
return;
_url = element.attribute( QString::fromLatin1( "URL" ) );
_spreadSheetNumber = element.attribute( QString::fromLatin1( "SpreadSheetNumber" ) ).toInt();
initDocument();
}
void KWMailMergeKSpread::save( QDomDocument& doc, QDomElement& parent )
{
QDomElement content = doc.createElement( QString::fromLatin1( "CONTENT" ) );
parent.appendChild( content );
content.setAttribute( "URL", _url.url() );
content.setAttribute( "SpreadSheetNumber", _spreadSheetNumber );
}
void KWMailMergeKSpread::refresh( bool )
{
}
bool KWMailMergeKSpread::showConfigDialog( QWidget *parent, int )
{
KWMailMergeKSpreadConfig dlg( parent, this );
int retval = dlg.exec();
if ( retval )
initDocument();
return retval;
}
void KWMailMergeKSpread::initDocument()
{
_document = new Doc();
connect( _document, SIGNAL( completed() ), SLOT( initSpreadSheets() ) );
_document->openURL( _url );
}
void KWMailMergeKSpread::initSpreadSheets()
{
_columnMap.clear();
sampleRecord.clear();
QPtrListIterator<Sheet> it( _document->map()->sheetList() );
int counter = 0;
for ( it.toFirst(); it.current(), counter < _spreadSheetNumber; ++it ) {
_sheet = it.current();
counter++;
}
if ( !_sheet ) {
kdError() << "No spread sheet available" << endl;
return;
}
if ( rows() < 2 ) // empty table
return;
int cols = columns();
for ( int i = 1; i < cols; ++i ) {
const Cell* cell = _sheet->cellAt( i, 1 );
// init record list
sampleRecord[ cellText( cell ) ] = cellText( cell );
_columnMap.insert( cellText( cell ), i );
}
}
int KWMailMergeKSpread::rows() const
{
if ( !_sheet )
return 0;
int row = 1;
for (; row < _sheet->maxRow(); ) {
const Cell* cell = _sheet->cellAt( 1, row );
if ( cellText( cell ).isEmpty() )
break;
row++;
}
return row;
}
int KWMailMergeKSpread::columns() const
{
if ( !_sheet )
return 0;
int col = 1;
for (; col < _sheet->maxColumn(); ) {
const Cell* cell = _sheet->cellAt( col, 1 );
if ( cellText( cell ).isEmpty() )
break;
col++;
}
return col;
}
QString KWMailMergeKSpread::cellText( const Cell *cell ) const
{
QString text = QString::null;
if ( !cell->isDefault() && !cell->isEmpty() ) {
if ( cell->isFormula() )
text = cell->strOutText();
else if ( !cell->link().isEmpty() )
text = cell->link();
else
text = cell->text();
}
#if 0
switch( cell->content() ) {
case Cell::Text:
case Cell::Formula:
text = cell->strOutText();
break;
case Cell::RichText:
case Cell::VisualFormula:
text = cell->text(); // untested
break;
}
}
#endif
return text;
}
extern "C"
{
KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_kspread( KInstance *instance, QObject *parent )
{
return new KWMailMergeKSpread( instance, parent );
}
}
#include "kwmailmerge_kspread.moc"
|