summaryrefslogtreecommitdiff
path: root/kmime/kmime_headers.h
blob: 85e19320582e84270faff5c0612423ef2e926a61 (plain)
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
/*  -*- c++ -*-
    kmime_headers.h

    KMime, the KDE Internet mail/usenet news message library.
    Copyright (c) 2001-2002 the KMime authors.
    See file AUTHORS for details
    Copyright (c) 2006 Volker Krause <vkrause@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.
*/
/**
  @file
  This file is part of the API for handling @ref MIME data and
  defines the various header classes:
   - header's base class defining the common interface
   - generic base classes for different types of fields
   - incompatible, Structured-based field classes
   - compatible, Unstructured-based field classes

  @brief
  Defines the various headers classes.

  @authors the KMime authors (see AUTHORS file),
  Volker Krause \<vkrause@kde.org\>
*/

#ifndef __KMIME_HEADERS_H__
#define __KMIME_HEADERS_H__

#include "kmime_export.h"
#include "kmime_header_parsing.h"

#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QRegExp>
#include <QtCore/QDateTime>
#include <QtCore/QMap>
#include <QtCore/QList>
#include <QtCore/QByteArray>

#include <kdatetime.h>

namespace KMime {

class Content;

namespace Headers {

class BasePrivate;

enum contentCategory {
  CCsingle,
  CCcontainer,
  CCmixedPart,
  CCalternativePart
};

/**
  Various possible values for the "Content-Transfer-Encoding" header.
*/
enum contentEncoding {
  CE7Bit,              ///< 7bit
  CE8Bit,              ///< 8bit
  CEquPr,              ///< quoted-printable
  CEbase64,            ///< base64
  CEuuenc,             ///< uuencode
  CEbinary             ///< binary
};

/**
  Various possible values for the "Content-Disposition" header.
*/
enum contentDisposition {
  CDInvalid,           ///< Default, invalid value
  CDinline,            ///< inline
  CDattachment,        ///< attachment
  CDparallel           ///< parallel (invalid, do not use)
};

//often used charset
// TODO: get rid of this!
static const QByteArray Latin1( "ISO-8859-1" );

//@cond PRIVATE
// internal macro to generate default constructors
#define kmime_mk_trivial_ctor( subclass )                               \
  public:                                                               \
    explicit subclass( Content *parent = 0 );                           \
    subclass( Content *parent, const QByteArray &s );                   \
    subclass( Content *parent, const QString &s, const QByteArray &charset ); \
    ~subclass();

#define kmime_mk_dptr_ctor( subclass ) \
  protected: \
    explicit subclass( subclass##Private *d, KMime::Content *parent = 0 );

#define kmime_mk_trivial_ctor_with_name( subclass )     \
  kmime_mk_trivial_ctor( subclass )                     \
    const char *type() const;                           \
    static const char *staticType();
//@endcond

//
//
// HEADER'S BASE CLASS. DEFINES THE COMMON INTERFACE
//
//

/** Baseclass of all header-classes. It represents a
    header-field as described in RFC-822.  */
class KMIME_EXPORT Base
{
  public:
    /**
      A list of headers.
    */
    typedef QList<KMime::Headers::Base*> List;

    /**
      Creates an empty header with a parent-content.
    */
    explicit Base( KMime::Content *parent = 0 );

    /**
      Destructor.
    */
    virtual ~Base();

    /**
      Returns the parent of this header.
    */
    KMime::Content *parent() const;

    /**
      Sets the parent for this header to @p parent.
    */
    void setParent( KMime::Content *parent );

    /**
      Parses the given string. Take care of RFC2047-encoded strings.
      @param s The encoded header data.
    */
    virtual void from7BitString( const QByteArray &s ) = 0;

    /**
      Returns the encoded header.
      @param withHeaderType Specifies whether the header-type should be included.
    */
    virtual QByteArray as7BitString( bool withHeaderType = true ) const = 0;

    /**
      Returns the charset that is used for RFC2047-encoding.
    */
    QByteArray rfc2047Charset() const;

    /**
      Sets the charset for RFC2047-encoding.
      @param cs The new charset used for RFC2047 encoding.
    */
    void setRFC2047Charset( const QByteArray &cs );

    /**
      Returns the default charset.
    */
    QByteArray defaultCharset() const;

    /**
      Returns if the default charset is mandatory.
    */
    bool forceDefaultCharset() const;

    /**
      Parses the given string and set the charset.
      @param s The header data as unicode string.
      @param b The charset preferred for encoding.
    */
    virtual void fromUnicodeString( const QString &s, const QByteArray &b ) = 0;

    /**
      Returns the decoded content of the header without the header-type.

      @note The return value of this method should only be used when showing an address
            to the user. It is not guaranteed that fromUnicodeString( asUnicodeString(), ... )
            will return the original string.
    */
    virtual QString asUnicodeString() const = 0;

    /**
      Deletes.
    */
    virtual void clear() = 0;

    /**
      Checks if this header contains any data.
    */
    virtual bool isEmpty() const = 0;

    /**
      Returns the type of this header (e.g. "From").
    */
    virtual const char *type() const;

    /**
      Checks if this header is of type @p t.
    */
    bool is( const char *t ) const;

    /**
      Checks if this header is a MIME header.
    */
    bool isMimeHeader() const;

    /**
      Checks if this header is a X-Header.
    */
    bool isXHeader() const;

  protected:
    /**
      Helper method, returns the header prefix including ":".
    */
    QByteArray typeIntro() const;

    //@cond PRIVATE
    BasePrivate *d_ptr;
    kmime_mk_dptr_ctor( Base )
    //@endcond

  private:
    Q_DECLARE_PRIVATE(Base)
    Q_DISABLE_COPY(Base)
};

//
//
// GENERIC BASE CLASSES FOR DIFFERENT TYPES OF FIELDS
//
//

namespace Generics {

class UnstructuredPrivate;

/**
  Abstract base class for unstructured header fields
  (e.g. "Subject", "Comment", "Content-description").

  Features: Decodes the header according to RFC2047, incl. RFC2231
  extensions to encoded-words.

  Subclasses need only re-implement @p const @p char* @p type().
*/

// known issues:
// - uses old decodeRFC2047String function, instead of our own...

class KMIME_EXPORT Unstructured : public Base
{
  //@cond PRIVATE
  kmime_mk_dptr_ctor( Unstructured )
  //@endcond
  public:
    explicit Unstructured( Content *p = 0 );
    Unstructured( Content *p, const QByteArray &s );
    Unstructured( Content *p, const QString &s, const QByteArray &cs );
    ~Unstructured();

    virtual void from7BitString( const QByteArray &s );
    virtual QByteArray as7BitString( bool withHeaderType=true ) const;

    virtual void fromUnicodeString( const QString &s,
                                  const QByteArray &b );
    virtual QString asUnicodeString() const;

    virtual void clear();

    virtual bool isEmpty() const;

  private:
    Q_DECLARE_PRIVATE(Unstructured)
};


class StructuredPrivate;

/**
  @brief
  Base class for structured header fields.

  This is the base class for all structured header fields.
  It contains parsing methods for all basic token types found in rfc2822.

  @section Parsing

  At the basic level, there are tokens & tspecials (rfc2045),
  atoms & specials, quoted-strings, domain-literals (all rfc822) and
  encoded-words (rfc2047).

  As a special token, we have the comment. It is one of the basic
  tokens defined in rfc822, but it's parsing relies in part on the
  basic token parsers (e.g. comments may contain encoded-words).
  Also, most upper-level parsers (notably those for phrase and
  dot-atom) choose to ignore any comment when parsing.

  Then there are the real composite tokens, which are made up of one
  or more of the basic tokens (and semantically invisible comments):
  phrases (rfc822 with rfc2047) and dot-atoms (rfc2822).

  This finishes the list of supported token types. Subclasses will
  provide support for more higher-level tokens, where necessary,
  using these parsers.

  @author Marc Mutz <mutz@kde.org>
*/

class KMIME_EXPORT Structured : public Base
{
  public:
    explicit Structured( Content *p = 0 );
    Structured( Content *p, const QByteArray &s );
    Structured( Content *p, const QString &s, const QByteArray &cs );
    ~Structured();

    virtual void from7BitString( const QByteArray &s );
    virtual QString asUnicodeString() const;
    virtual void fromUnicodeString( const QString &s, const QByteArray &b );

  protected:
    /**
      This method parses the raw header and needs to be implemented in
      every sub-class.

      @param scursor Pointer to the start of the data still to parse.
      @param send Pointer to the end of the data.
      @param isCRLF true if input string is terminated with a CRLF.
    */
    virtual bool parse( const char* &scursor, const char *const send,
                        bool isCRLF = false ) = 0;

    //@cond PRIVATE
    kmime_mk_dptr_ctor( Structured )
    //@endcond

  private:
    Q_DECLARE_PRIVATE(Structured)
};


class AddressPrivate;

/**
  Base class for all address related headers.
*/
class KMIME_EXPORT Address : public Structured
{
  public:
    explicit Address( Content *p = 0 );
    Address( Content *p, const QByteArray &s );
    Address( Content *p, const QString &s, const QByteArray &cs );
    ~Address();
  protected:
    //@cond PRIVATE
    kmime_mk_dptr_ctor( Address )
    //@endcond
  private:
    Q_DECLARE_PRIVATE(Address)
};


class MailboxListPrivate;

/**
  Base class for headers that deal with (possibly multiple)
  addresses, but don't allow groups.

  @see RFC 2822, section 3.4
*/
class KMIME_EXPORT MailboxList : public Address
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( MailboxList )
  kmime_mk_dptr_ctor( MailboxList )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void fromUnicodeString( const QString &s, const QByteArray &b );
    virtual QString asUnicodeString() const;

    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Adds an address to this header.

      @param mbox A Mailbox object specifying the address.
    */
    void addAddress( const Types::Mailbox &mbox );

    /**
      Adds an address to this header.
      @param address The actual email address, with or without angle brackets.
      @param displayName An optional name associated with the address.
    */
    void addAddress( const QByteArray &address,
                     const QString &displayName = QString() );

    /**
      Returns a list of all addresses in this header, regardless of groups.
    */
    QList<QByteArray> addresses() const;

    /**
      Returns a list of all display names associated with the addresses in
      this header. An empty entry is added for addresses that do not have
      a display name.
    */
    QStringList displayNames() const;

    /**
      Returns a list of assembled display name / address strings of the
      following form: "Display Name &lt;address&gt;". These are unicode
      strings without any transport encoding, ie. they are only suitable
      for displaying.
    */
    QStringList prettyAddresses() const;

    /**
      Returns a list of mailboxes listed in this header.
    */
    Types::Mailbox::List mailboxes() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(MailboxList)
};


class SingleMailboxPrivate;

/**
   Base class for headers that deal with exactly one mailbox
   (e.g. Sender).
*/
class KMIME_EXPORT SingleMailbox : public MailboxList
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( SingleMailbox )
  //@endcond
  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );
  private:
    Q_DECLARE_PRIVATE(SingleMailbox)
};


class AddressListPrivate;

/**
  Base class for headers that deal with (possibly multiple)
  addresses, allowing groups.

  Note: Groups are parsed but not represented in the API yet. All addresses in
  groups are listed as if they would not be part of a group.

  @todo Add API for groups?

  @see RFC 2822, section 3.4
*/
class KMIME_EXPORT AddressList : public Address
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( AddressList )
  kmime_mk_dptr_ctor( AddressList )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void fromUnicodeString( const QString &s, const QByteArray &b );
    virtual QString asUnicodeString() const;

    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Adds an address to this header.

      @param mbox A Mailbox object specifying the address.
    */
    void addAddress( const Types::Mailbox &mbox );

    /**
      Adds an address to this header.
      @param address The actual email address, with or without angle brackets.
      @param displayName An optional name associated with the address.
    */
    void addAddress( const QByteArray &address, const QString &displayName = QString() );

    /**
      Returns a list of all addresses in this header, regardless of groups.
    */
    QList<QByteArray> addresses() const;

    /**
      Returns a list of all display names associated with the addresses in this header.
      An empty entry is added for addresses that don't have a display name.
    */
    QStringList displayNames() const;

    /**
      Returns a list of assembled display name / address strings of the following form:
      "Display Name &lt;address&gt;". These are unicode strings without any transport
      encoding, ie. they are only suitable for displaying.
    */
    QStringList prettyAddresses() const;

    /**
      Returns a list of mailboxes listed in this header.
    */
    Types::Mailbox::List mailboxes() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(AddressList)
};


class IdentPrivate;

/**
  Base class for headers which deal with a list of msg-id's.

  @see RFC 2822, section 3.6.4
*/
class KMIME_EXPORT Ident : public Address
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( Ident )
  kmime_mk_dptr_ctor( Ident )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the list of identifiers contained in this header.
      Note:
      - Identifiers are not enclosed in angle-brackets.
      - Identifiers are listed in the same order as in the header.
    */
    QList<QByteArray> identifiers() const;

    /**
      Appends a new identifier to this header.
      @param id The identifier to append, with or without angle-brackets.
    */
    void appendIdentifier( const QByteArray &id );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(Ident)
};


class SingleIdentPrivate;

/**
  Base class for headers which deal with a single msg-id.

  @see RFC 2822, section 3.6.4
*/
class KMIME_EXPORT SingleIdent : public Ident
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( SingleIdent )
  kmime_mk_dptr_ctor( SingleIdent )
  //@endcond
  public:
    /**
      Returns the identifier contained in this header.
      Note: The identifiers is not enclosed in angle-brackets.
    */
    QByteArray identifier() const;

    /**
      Sets the identifier.
      @param id The new identifier with or without angle-brackets.
    */
    void setIdentifier( const QByteArray &id );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(SingleIdent)
};


class TokenPrivate;

/**
  Base class for headers which deal with a single atom.
*/
class KMIME_EXPORT Token : public Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( Token )
  kmime_mk_dptr_ctor( Token )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the token.
    */
    QByteArray token() const;

    /**
      Sets the token to @p t,
    */
    void setToken( const QByteArray &t );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(Token)
};


class PhraseListPrivate;

/**
  Base class for headers containing a list of phrases.
*/
class KMIME_EXPORT PhraseList : public Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( PhraseList )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual QString asUnicodeString() const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the list of phrases contained in this header.
    */
    QStringList phrases() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(PhraseList)
};


class DotAtomPrivate;

/**
  Base class for headers containing a dot atom.
*/
class KMIME_EXPORT DotAtom : public Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( DotAtom )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual QString asUnicodeString() const;
    virtual void clear();
    virtual bool isEmpty() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(DotAtom)
};


class ParametrizedPrivate;

/**
  Base class for headers containing a parameter list such as "Content-Type".
*/
class KMIME_EXPORT Parametrized : public Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor( Parametrized )
  kmime_mk_dptr_ctor( Parametrized )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;

    virtual bool isEmpty() const;
    virtual void clear();

    //FIXME: Shouldn't the parameter keys be QByteArray and not QStrings? Only the values can be
    //       non-ascii!

    /**
      Returns the value of the specified parameter.
      @param key The parameter name.
    */
    QString parameter( const QString &key ) const;

    /**
      @return true if a parameter with the given @p key exists.
      @since 4.5
    */
    bool hasParameter( const QString &key ) const;

    /**
      Sets the parameter @p key to @p value.
      @param key The parameter name.
      @param value The new value for @p key.
    */
    void setParameter( const QString &key, const QString &value );

  protected:
    virtual bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(Parametrized)
};

} // namespace Generics

//
//
// INCOMPATIBLE, GSTRUCTURED-BASED FIELDS:
//
//

class ReturnPathPrivate;

/**
  Represents the Return-Path header field.

  @see RFC 2822, section 3.6.7
*/
class KMIME_EXPORT ReturnPath : public Generics::Address
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( ReturnPath )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(ReturnPath)
};

// Address et al.:

// rfc(2)822 headers:
/**
   Represent a "From" header.

   @see RFC 2822, section 3.6.2.
*/
class KMIME_EXPORT From : public Generics::MailboxList
{
  kmime_mk_trivial_ctor_with_name( From )
};

/**
  Represents a "Sender" header.

  @see RFC 2822, section 3.6.2.
*/
class KMIME_EXPORT Sender : public Generics::SingleMailbox
{
  kmime_mk_trivial_ctor_with_name( Sender )
};

/**
  Represents a "To" header.

  @see RFC 2822, section 3.6.3.
*/
class KMIME_EXPORT To : public Generics::AddressList
{
  kmime_mk_trivial_ctor_with_name( To )
};

/**
  Represents a "Cc" header.

  @see RFC 2822, section 3.6.3.
*/
class KMIME_EXPORT Cc : public Generics::AddressList
{
  kmime_mk_trivial_ctor_with_name( Cc )
};

/**
  Represents a "Bcc" header.

  @see RFC 2822, section 3.6.3.
*/
class KMIME_EXPORT Bcc : public Generics::AddressList
{
  kmime_mk_trivial_ctor_with_name( Bcc )
};

/**
  Represents a "ReplyTo" header.

  @see RFC 2822, section 3.6.2.
*/
class KMIME_EXPORT ReplyTo : public Generics::AddressList
{
  kmime_mk_trivial_ctor_with_name( ReplyTo )
};


class MailCopiesToPrivate;

/**
  Represents a "Mail-Copies-To" header.

  @see http://www.newsreaders.com/misc/mail-copies-to.html
*/
class KMIME_EXPORT MailCopiesTo : public Generics::AddressList
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( MailCopiesTo )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual QString asUnicodeString() const;

    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns true if a mail copy was explicitly requested.
    */
    bool alwaysCopy() const;

    /**
      Sets the header to "poster".
    */
    void setAlwaysCopy();

    /**
      Returns true if a mail copy was explicitly denied.
    */
    bool neverCopy() const;

    /**
      Sets the header to "never".
    */
    void setNeverCopy();

  protected:
    virtual bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(MailCopiesTo)
};


class ContentTransferEncodingPrivate;

/**
  Represents a "Content-Transfer-Encoding" header.

  @see RFC 2045, section 6.
*/
class KMIME_EXPORT ContentTransferEncoding : public Generics::Token
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( ContentTransferEncoding )
  //@endcond
  public:
    virtual void clear();

    /**
      Returns the encoding specified in this header.
    */
    contentEncoding encoding() const;

    /**
      Sets the encoding to @p e.
    */
    void setEncoding( contentEncoding e );

    /**
      Returns whether the Content containing this header is already decoded.
    */
    // KDE5: rename to isDecoded().
    bool decoded() const;

    /**
      Set whether the Content containing this header is already decoded.
      For instance, if you fill your Content with already-encoded base64 data,
      you will want to setDecoded( false ).
    */
    void setDecoded( bool decoded = true );

    /**
      Returns whether the Content containing this header needs to be encoded
      (i.e., if decoded() is true and encoding() is base64 or quoted-printable).
    */
    bool needToEncode() const;

  protected:
    virtual bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(ContentTransferEncoding)
};

/**
  Represents a "Keywords" header.

  @see RFC 2822, section 3.6.5.
*/
class KMIME_EXPORT Keywords : public Generics::PhraseList
{
  kmime_mk_trivial_ctor_with_name( Keywords )
};

// DotAtom:

/**
  Represents a "MIME-Version" header.

  @see RFC 2045, section 4.
*/
class KMIME_EXPORT MIMEVersion : public Generics::DotAtom
{
  kmime_mk_trivial_ctor_with_name( MIMEVersion )
};

// Ident:

/**
  Represents a "Message-ID" header.

  @see RFC 2822, section 3.6.4.
*/
class KMIME_EXPORT MessageID : public Generics::SingleIdent
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( MessageID )
  //@endcond
  public:
    /**
      Generate a message identifer.
      @param fqdn A fully qualified domain name.
    */
    void generate( const QByteArray &fqdn );
};

class ContentIDPrivate;

/**
  Represents a "Content-ID" header.
*/
class KMIME_EXPORT ContentID : public Generics::SingleIdent
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( ContentID )
  kmime_mk_dptr_ctor( ContentID )
  //@endcond

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );
  private:
    Q_DECLARE_PRIVATE(ContentID)
};

/**
  Represents a "Supersedes" header.
*/
class KMIME_EXPORT Supersedes : public Generics::SingleIdent
{
  kmime_mk_trivial_ctor_with_name( Supersedes )
};

/**
  Represents a "In-Reply-To" header.

  @see RFC 2822, section 3.6.4.
*/
class KMIME_EXPORT InReplyTo : public Generics::Ident
{
  kmime_mk_trivial_ctor_with_name( InReplyTo )
};

/**
  Represents a "References" header.

  @see RFC 2822, section 3.6.4.
*/
class KMIME_EXPORT References : public Generics::Ident
{
  kmime_mk_trivial_ctor_with_name( References )
};


class ContentTypePrivate;

/**
  Represents a "Content-Type" header.

  @see RFC 2045, section 5.
*/
class KMIME_EXPORT ContentType : public Generics::Parametrized
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( ContentType )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the mimetype.
    */
    QByteArray mimeType() const;

    /**
      Returns the media type (first part of the mimetype).
    */

    QByteArray mediaType() const;

    /**
      Returns the mime sub-type (second part of the mimetype).
    */
    QByteArray subType() const;

    /**
      Sets the mimetype and clears already existing parameters.
      @param mimeType The new mimetype.
    */
    void setMimeType( const QByteArray &mimeType );

    /**
      Tests if the media type equals @p mediatype.
    */
    bool isMediatype( const char *mediatype ) const;

    /**
      Tests if the mime sub-type equals @p subtype.
    */
    bool isSubtype( const char *subtype ) const;

    /**
      Returns true if the associated MIME entity is a text.
    */
    bool isText() const;

    /**
      Returns true if the associated MIME entity is a plain text.
    */
    bool isPlainText() const;

    /**
      Returns true if the associated MIME entity is a HTML file.
    */
    bool isHTMLText() const;

    /**
      Returns true if the associated MIME entity is an image.
    */
    bool isImage() const;

    /**
      Returns true if the associated MIME entity is a mulitpart container.
    */
    bool isMultipart() const;

    /**
      Returns true if the associated MIME entity contains partial data.
      @see partialNumber(), partialCount()
    */
    bool isPartial() const;

    /**
      Returns the charset for the associated MIME entity.
    */
    QByteArray charset() const;

    /**
      Sets the charset.
    */
    void setCharset( const QByteArray &s );

    /**
      Returns the boundary (for mulitpart containers).
    */
    QByteArray boundary() const;

    /**
      Sets the mulitpart container boundary.
    */
    void setBoundary( const QByteArray &s );

    /**
      Returns the name of the associated MIME entity.
    */
    QString name() const;

    /**
      Sets the name to @p s using charset @p cs.
    */
    void setName( const QString &s, const QByteArray &cs );

    /**
      Returns the identifier of the associated MIME entity.
    */
    QByteArray id() const;

    /**
      Sets the identifier.
    */
    void setId( const QByteArray &s );

    /**
      Returns the position of this part in a multi-part set.
      @see isPartial(), partialCount()
    */
    int partialNumber() const;

    /**
      Returns the total number of parts in a multi-part set.
      @see isPartial(), partialNumber()
    */
    int partialCount() const;

    /**
      Sets parameters of a partial MIME entity.
      @param total The total number of entities in the multi-part set.
      @param number The number of this entity in a multi-part set.
    */
    void setPartialParams( int total, int number );

    // TODO: document
    contentCategory category() const;

    void setCategory( contentCategory c );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE(ContentType)
};


class ContentDispositionPrivate;

/**
  Represents a "Content-Disposition" header.

  @see RFC 2183
*/
class KMIME_EXPORT ContentDisposition : public Generics::Parametrized
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( ContentDisposition )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual bool isEmpty() const;
    virtual void clear();

    /**
      Returns the content disposition.
    */
    contentDisposition disposition() const;

    /**
      Sets the content disposition.
      @param disp The new content disposition.
    */
    void setDisposition( contentDisposition disp );

    /**
      Returns the suggested filename for the associated MIME part.
      This is just a convenience function, it is equivalent to calling
      parameter( "filename" );
    */
    QString filename() const;

    /**
      Sets the suggested filename for the associated MIME part.
      This is just a convenience function, it is equivalent to calling
      setParameter( "filename", filename );
      @param filename The filename.
    */
    void setFilename( const QString &filename );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF=false );

  private:
    Q_DECLARE_PRIVATE( ContentDisposition )
};

//
//
// COMPATIBLE GUNSTRUCTURED-BASED FIELDS:
//
//


class GenericPrivate;

/**
  Represents an arbitrary header, that can contain any header-field.
  Adds a type over Unstructured.
  @see Unstructured
*/
class KMIME_EXPORT Generic : public Generics::Unstructured
{
  public:
    Generic();
    Generic( const char *t );
    Generic( const char *t, Content *p );
    Generic( const char *t, Content *p, const QByteArray &s );
    Generic( const char *t, Content *p, const QString &s, const QByteArray &cs );
    ~Generic();

    virtual void clear();

    virtual bool isEmpty() const;

    virtual const char *type() const;

    void setType( const char *type );

  private:
    Q_DECLARE_PRIVATE( Generic )
};

/**
  Represents a "Subject" header.

  @see RFC 2822, section 3.6.5.
*/
class KMIME_EXPORT Subject : public Generics::Unstructured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( Subject )
  //@endcond
  public:
    bool isReply() const;
};

/**
  Represents a "Organization" header.
*/
class KMIME_EXPORT Organization : public Generics::Unstructured
{
  kmime_mk_trivial_ctor_with_name( Organization )
};

/**
  Represents a "Content-Description" header.
*/
class KMIME_EXPORT ContentDescription : public Generics::Unstructured
{
  kmime_mk_trivial_ctor_with_name( ContentDescription )
};

/**
  Represents a "Content-Location" header.
  @since 4.2
*/
class KMIME_EXPORT ContentLocation : public Generics::Unstructured
{
  kmime_mk_trivial_ctor_with_name( ContentLocation )
};

class ControlPrivate;

/**
  Represents a "Control" header.

  @see RFC 1036, section 3.
*/
class KMIME_EXPORT Control : public Generics::Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( Control )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the control message type.
    */
    QByteArray controlType() const;

    /**
      Returns the control message parameter.
    */
    QByteArray parameter() const;

    /**
      Returns true if this is a cancel control message.
      @see RFC 1036, section 3.1.
    */
    bool isCancel() const;

    /**
      Changes this header into a cancel control message for the given message-id.
      @param msgid The message-id of the article that should be canceled.
    */
    void setCancel( const QByteArray &msgid );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF = false );

  private:
    Q_DECLARE_PRIVATE(Control)
};


class DatePrivate;

/**
  Represents a "Date" header.

  @see RFC 2822, section 3.3.
*/
class KMIME_EXPORT Date : public Generics::Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( Date )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the date contained in this header.
    */
    KDateTime dateTime() const;

    /**
      Sets the date.
    */
    void setDateTime( const KDateTime &dt );

    /**
      Returns the age of the message.
    */
    int ageInDays() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF = false );

  private:
    Q_DECLARE_PRIVATE( Date )
};


class NewsgroupsPrivate;

/**
  Represents a "Newsgroups" header.

  @see RFC 1036, section 2.1.3.
*/
class KMIME_EXPORT Newsgroups : public Generics::Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( Newsgroups )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual void fromUnicodeString( const QString &s, const QByteArray &b );
    virtual QString asUnicodeString() const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the list of newsgroups.
    */
    QList<QByteArray> groups() const;

    /**
      Sets the newsgroup list.
    */
    void setGroups( const QList<QByteArray> &groups );

    /**
      Returns true if this message has been cross-posted, i.e. if it has been
      posted to multiple groups.
    */
    bool isCrossposted() const;

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF = false );

  private:
    Q_DECLARE_PRIVATE( Newsgroups )
};

/**
  Represents a "Followup-To" header.

  @see RFC 1036, section 2.2.3.
*/
class KMIME_EXPORT FollowUpTo : public Newsgroups
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( FollowUpTo )
  //@endcond
};


class LinesPrivate;

/**
  Represents a "Lines" header.

  @see RFC 1036, section 2.2.12.
*/
class KMIME_EXPORT Lines : public Generics::Structured
{
  //@cond PRIVATE
  kmime_mk_trivial_ctor_with_name( Lines )
  //@endcond
  public:
    virtual QByteArray as7BitString( bool withHeaderType = true ) const;
    virtual QString asUnicodeString() const;
    virtual void clear();
    virtual bool isEmpty() const;

    /**
      Returns the number of lines, undefined if isEmpty() returns true.
    */
    int numberOfLines() const;

    /**
      Sets the number of lines to @p lines.
    */
    void setNumberOfLines( int lines );

  protected:
    bool parse( const char* &scursor, const char *const send, bool isCRLF = false );

  private:
    Q_DECLARE_PRIVATE( Lines )
};

/**
  Represents a "User-Agent" header.
*/
class KMIME_EXPORT UserAgent : public Generics::Unstructured
{
  kmime_mk_trivial_ctor_with_name( UserAgent )
};

/** Creates a header based on @param type. If @param type is a known header type,
 * the right object type will be created, otherwise a null pointer is returned. */
KMIME_EXPORT Base *createHeader( const QByteArray& type );

}  //namespace Headers

}  //namespace KMime

// undefine code generation macros again
#undef kmime_mk_trivial_ctor
#undef kmime_mk_dptr_ctor
#undef kmime_mk_trivial_ctor_with_name

#endif // __KMIME_HEADERS_H__