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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/*
ktnefwriter.cpp
Copyright (C) 2002 Bo Thorsen <bo@sonofthor.dk>
This file is part of KTNEF, the KDE TNEF support library/program.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include "ktnef/ktnefwriter.h"
#include "ktnef/ktnefproperty.h"
#include "ktnef/ktnefpropertyset.h"
#include <tqfile.h>
#include <tqdatetime.h>
#include <tqdatastream.h>
#include <kdebug.h>
#include <assert.h>
#include "ktnef/ktnefdefs.h"
class KTNEFWriter::PrivateData {
public:
PrivateData() { mFirstAttachNum = TQDateTime::currentDateTime().toTime_t(); }
KTNEFPropertySet properties;
Q_UINT16 mFirstAttachNum;
};
KTNEFWriter::KTNEFWriter() {
mData = new PrivateData;
// This is not something the user should fiddle with
// First set the TNEF version
TQVariant v(0x00010000);
addProperty( attTNEFVERSION, atpDWORD, v );
// Now set the code page to something reasonable. TODO: Use the right one
TQVariant v1( (Q_UINT32)0x4e4 );
TQVariant v2( (Q_UINT32)0x0 );
TQValueList<TQVariant> list;
list << v1;
list << v2;
v = TQVariant( list );
addProperty( attOEMCODEPAGE, atpBYTE, list );
}
KTNEFWriter::~KTNEFWriter() {
delete mData;
}
void KTNEFWriter::addProperty( int tag, int type, const TQVariant& value ) {
mData->properties.addProperty( tag, type, value );
}
void addToChecksum( Q_UINT32 i, Q_UINT16 &checksum ) {
checksum += i & 0xff;
checksum += (i >> 8) & 0xff;
checksum += (i >> 16) & 0xff;
checksum += (i >> 24) & 0xff;
}
void addToChecksum( TQCString &cs, Q_UINT16 &checksum ) {
int len = cs.length();
for (int i=0; i<len; i++)
checksum += (Q_UINT8)cs[i];
}
void writeCString( TQDataStream &stream, TQCString &str ) {
stream.writeRawBytes( str.data(), str.length() );
stream << (Q_UINT8)0;
}
Q_UINT32 mergeTagAndType( Q_UINT32 tag, Q_UINT32 type ) {
return ( ( type & 0xffff ) << 16 ) | ( tag & 0xffff );
}
/* This writes a TNEF property to the file.
*
* A TNEF property has a 1 byte type (LVL_MESSAGE or LVL_ATTACHMENT),
* a 4 byte type/tag, a 4 byte length, the data and finally the checksum.
*
* The checksum is a 16 byte int with all bytes in the data added.
*/
bool KTNEFWriter::writeProperty( TQDataStream &stream, int &bytes, int tag) {
TQMap<int,KTNEFProperty*>& properties = mData->properties.properties();
TQMap<int,KTNEFProperty*>::Iterator it = properties.find( tag );
if ( it == properties.end() )
return false;
KTNEFProperty *property = *it;
Q_UINT32 i;
Q_UINT16 checksum = 0;
TQValueList<TQVariant> list;
TQString s;
TQCString cs, cs2;
TQDateTime dt;
TQDate date;
TQTime time;
switch( tag ) {
case attMSGSTATUS:
// Q_UINT8
i = property->value().toUInt() & 0xff;
checksum = i;
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)1;
stream << (Q_UINT8)i;
bytes += 10;
break;
case attMSGPRIORITY:
case attREQUESTRES:
// Q_UINT16
i = property->value().toUInt() & 0xffff;
addToChecksum( i, checksum );
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)2;
stream << (Q_UINT16)i;
bytes += 11;
break;
case attTNEFVERSION:
// Q_UINT32
i = property->value().toUInt();
addToChecksum( i, checksum );
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)4;
stream << (Q_UINT32)i;
bytes += 13;
break;
case attOEMCODEPAGE:
// 2 Q_UINT32
list = property->value().toList();
assert( list.count() == 2 );
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)8;
i = list[0].toInt();
addToChecksum( i, checksum );
stream << (Q_UINT32)i;
i = list[1].toInt();
addToChecksum( i, checksum );
stream << (Q_UINT32)i;
bytes += 17;
break;
case attMSGCLASS:
case attSUBJECT:
case attBODY:
case attMSGID:
// QCString
cs = property->value().toString().local8Bit();
addToChecksum( cs, checksum );
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)cs.length()+1;
writeCString( stream, cs );
bytes += 9 + cs.length()+1;
break;
case attFROM:
// 2 TQString encoded to a TRP structure
list = property->value().toList();
assert( list.count() == 2 );
cs = list[0].toString().local8Bit(); // Name
cs2 = (TQString("smtp:") + list[1].toString()).local8Bit(); // Email address
i = 18 + cs.length() + cs2.length(); // 2 * sizof(TRP) + strings + 2x'\0'
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)i;
// The stream has to be aligned to 4 bytes for the strings
// TODO: Or does it? Looks like Outlook doesn't do this
// bytes += 17;
// Write the first TRP structure
stream << (Q_UINT16)4; // trpidOneOff
stream << (Q_UINT16)i; // totalsize
stream << (Q_UINT16)(cs.length()+1); // sizeof name
stream << (Q_UINT16)(cs2.length()+1); // sizeof address
// if ( bytes % 4 != 0 )
// Align the buffer
// Write the strings
writeCString( stream, cs );
writeCString( stream, cs2 );
// Write the empty padding TRP structure (just zeroes)
stream << (Q_UINT32)0 << (Q_UINT32)0;
addToChecksum( 4, checksum );
addToChecksum( i, checksum );
addToChecksum( cs.length()+1, checksum );
addToChecksum( cs2.length()+1, checksum );
addToChecksum( cs, checksum );
addToChecksum( cs2, checksum );
bytes += 10;
break;
case attDATESENT:
case attDATERECD:
case attDATEMODIFIED:
// QDateTime
dt = property->value().toDateTime();
time = dt.time();
date = dt.date();
stream << (Q_UINT8)LVL_MESSAGE;
stream << mergeTagAndType( tag, property->type() );
stream << (Q_UINT32)14;
i = (Q_UINT16)date.year();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)date.month();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)date.day();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)time.hour();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)time.minute();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)time.second();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
i = (Q_UINT16)date.dayOfWeek();
addToChecksum( i, checksum );
stream << (Q_UINT16)i;
break;
/*
case attMSGSTATUS:
{
Q_UINT8 c;
Q_UINT32 flag = 0;
if ( c & fmsRead ) flag |= MSGFLAG_READ;
if ( !( c & fmsModified ) ) flag |= MSGFLAG_UNMODIFIED;
if ( c & fmsSubmitted ) flag |= MSGFLAG_SUBMIT;
if ( c & fmsHasAttach ) flag |= MSGFLAG_HASATTACH;
if ( c & fmsLocal ) flag |= MSGFLAG_UNSENT;
d->stream_ >> c;
i = property->value().toUInt();
stream << (Q_UINT8)LVL_MESSAGE;
stream << (Q_UINT32)type;
stream << (Q_UINT32)2;
stream << (Q_UINT8)i;
addToChecksum( i, checksum );
// from reader: d->message_->addProperty( 0x0E07, MAPI_TYPE_ULONG, flag );
}
kdDebug() << "Message Status" << " (length=" << i2 << ")" << endl;
break;
*/
default:
kdDebug() << "Unknown TNEF tag: " << tag << endl;
return false;
}
stream << (Q_UINT16)checksum;
return true;
}
bool KTNEFWriter::writeFile( TQIODevice &file ) {
if ( !file.open( IO_WriteOnly ) )
return false;
TQDataStream stream( &file );
return writeFile( stream );
}
bool KTNEFWriter::writeFile( TQDataStream &stream ) {
stream.setByteOrder( TQDataStream::LittleEndian );
// Start by writing the opening TNEF stuff
stream << TNEF_SIGNATURE;
// Store the PR_ATTACH_NUM value for the first attachment
// ( must be stored even if *no* attachments are stored )
stream << mData->mFirstAttachNum;
// Now do some writing
bool ok = true;
int bytesWritten = 0;
ok &= writeProperty( stream, bytesWritten, attTNEFVERSION );
ok &= writeProperty( stream, bytesWritten, attOEMCODEPAGE );
ok &= writeProperty( stream, bytesWritten, attMSGCLASS );
ok &= writeProperty( stream, bytesWritten, attMSGPRIORITY );
ok &= writeProperty( stream, bytesWritten, attSUBJECT );
ok &= writeProperty( stream, bytesWritten, attDATESENT );
ok &= writeProperty( stream, bytesWritten, attDATESTART );
ok &= writeProperty( stream, bytesWritten, attDATEEND );
// ok &= writeProperty( stream, bytesWritten, attAIDOWNER );
ok &= writeProperty( stream, bytesWritten, attREQUESTRES );
ok &= writeProperty( stream, bytesWritten, attFROM );
ok &= writeProperty( stream, bytesWritten, attDATERECD );
ok &= writeProperty( stream, bytesWritten, attMSGSTATUS );
ok &= writeProperty( stream, bytesWritten, attBODY );
return ok;
}
void KTNEFWriter::setSender(const TQString &name, const TQString &email) {
assert( !name.isEmpty() );
assert( !email.isEmpty() );
TQVariant v1( name );
TQVariant v2( email );
TQValueList<TQVariant> list;
list << v1;
list << v2;
TQVariant v( list );
addProperty( attFROM, 0, list ); // What's up with the 0 here ??
}
void KTNEFWriter::setMessageType(MessageType m) {
// Note that the MessageType list here is probably not long enough,
// more entries are most likely needed later
TQVariant v;
switch( m ) {
case Appointment:
v = TQVariant( TQString( "IPM.Appointment" ) );
break;
case MeetingCancelled:
v = TQVariant( TQString( "IPM.Schedule.Meeting.Cancelled" ) );
break;
case MeetingRequest:
v = TQVariant( TQString( "IPM.Schedule.Meeting.Request" ) );
break;
case MeetingNo:
v = TQVariant( TQString( "IPM.Schedule.Meeting.Resp.Neg" ) );
break;
case MeetingYes:
v = TQVariant( TQString( "IPM.Schedule.Meeting.Resp.Pos" ) );
break;
case MeetingTent:
// Tent?
v = TQVariant( TQString( "IPM.Schedule.Meeting.Resp.Tent" ) );
break;
default:
return;
}
addProperty( attMSGCLASS, atpWORD, v );
}
void KTNEFWriter::setMethod( Method )
{
}
void KTNEFWriter::clearAttendees()
{
}
void KTNEFWriter::addAttendee( const TQString& /*cn*/, Role /*r*/, PartStat /*p*/,
bool /*rsvp*/, const TQString& /*mailto*/ )
{
}
// I assume this is the same as the sender?
// U also assume that this is like "Name <address>"
void KTNEFWriter::setOrganizer( const TQString& organizer ) {
int i = organizer.find( '<' );
if ( i == -1 )
return;
TQString name = organizer.left( i );
name.stripWhiteSpace();
TQString email = organizer.right( i+1 );
email = email.left( email.length()-1 );
email.stripWhiteSpace();
setSender( name, email );
}
void KTNEFWriter::setDtStart( const TQDateTime& dtStart ) {
TQVariant v( dtStart );
addProperty( attDATESTART, atpDATE, v );
}
void KTNEFWriter::setDtEnd( const TQDateTime& dtEnd ) {
TQVariant v( dtEnd );
addProperty( attDATEEND, atpDATE, v );
}
void KTNEFWriter::setLocation( const TQString& /*location*/ )
{
}
void KTNEFWriter::setUID( const TQString& uid ) {
TQVariant v( uid );
addProperty( attMSGID, atpSTRING, v );
}
// Date sent
void KTNEFWriter::setDtStamp( const TQDateTime& dtStamp ) {
TQVariant v( dtStamp );
addProperty( attDATESENT, atpDATE, v );
}
void KTNEFWriter::setCategories( const TQStringList& )
{
}
// I hope this is the body
void KTNEFWriter::setDescription( const TQString &body ) {
TQVariant v( body );
addProperty( attBODY, atpTEXT, v );
}
void KTNEFWriter::setSummary( const TQString &s ) {
TQVariant v( s );
addProperty( attSUBJECT, atpSTRING, v );
}
// TNEF encoding: Normal = 3, high = 2, low = 1
// MAPI encoding: Normal = -1, high = 0, low = 1
void KTNEFWriter::setPriority( Priority p ) {
TQVariant v( (Q_UINT32)p );
addProperty( attMSGPRIORITY, atpSHORT, v );
}
void KTNEFWriter::setAlarm( const TQString& /*description*/, AlarmAction /*action*/,
const TQDateTime& /*wakeBefore*/ )
{
}
|