File indexing completed on 2024-04-21 03:53:29

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KCONTACTS_VCARDCONVERTER_H
0009 #define KCONTACTS_VCARDCONVERTER_H
0010 
0011 #include "kcontacts/addressee.h"
0012 #include "kcontacts_export.h"
0013 #include <QString>
0014 
0015 namespace KContacts
0016 {
0017 /**
0018   @short Class to converting contact objects into vCard format and vice versa.
0019 
0020   This class implements reading and writing of contact using from/to the
0021   vCard format. Currently vCard version 2.1 and 3.0 is supported.
0022 
0023   Example:
0024 
0025   \code
0026 
0027   QFile file( "myfile.vcf" );
0028   file.open( QIODevice::ReadOnly );
0029 
0030   QByteArray data = file.readAll();
0031 
0032   VCardConverter converter;
0033   Addressee::List list = converter.parseVCards( data );
0034 
0035   // print formatted name of first contact
0036   qDebug( "name=%s", list[ 0 ].formattedName().toLatin1() );
0037 
0038   \endcode
0039 */
0040 class KCONTACTS_EXPORT VCardConverter
0041 {
0042 public:
0043     /**
0044       @li v2_1 - VCard format version 2.1
0045       @li v3_0 - VCard format version 3.0
0046       @li v4_0 - VCard format version 4.0
0047      */
0048     enum Version {
0049         v2_1,
0050         v3_0,
0051         v4_0,
0052     };
0053 
0054     /**
0055       Constructor.
0056      */
0057     VCardConverter();
0058 
0059     /**
0060       Destructor.
0061      */
0062     ~VCardConverter();
0063 
0064     /**
0065       Creates a string in vCard format which contains the given
0066       contact.
0067 
0068       @param addr The contact object
0069       @param version The version of the generated vCard format
0070      */
0071     Q_REQUIRED_RESULT QByteArray createVCard(const Addressee &addr, Version version = v3_0) const;
0072 
0073     /**
0074       Creates a string in vCard format which contains the given
0075       list of contact.
0076 
0077       @param list The list of contact objects
0078       @param version The version of the generated vCard format
0079      */
0080     // FIXME: Add error handling
0081     Q_REQUIRED_RESULT QByteArray createVCards(const Addressee::List &list, Version version = v3_0) const;
0082 
0083     /**
0084      * @since 4.9.1
0085      */
0086     Q_REQUIRED_RESULT QByteArray exportVCard(const Addressee &addr, Version version) const;
0087 
0088     /**
0089      * @since 4.9.1
0090      */
0091     Q_REQUIRED_RESULT QByteArray exportVCards(const Addressee::List &list, Version version) const;
0092 
0093     /**
0094       Parses a string in vCard format and returns the first contact.
0095      */
0096     Q_REQUIRED_RESULT Addressee parseVCard(const QByteArray &vcard) const;
0097 
0098     /**
0099       Parses a string in vCard format and returns a list of contact objects.
0100      */
0101     // FIXME: Add error handling
0102     Q_REQUIRED_RESULT Addressee::List parseVCards(const QByteArray &vcard) const;
0103 
0104 private:
0105     Q_DISABLE_COPY(VCardConverter)
0106     class VCardConverterPrivate;
0107     VCardConverterPrivate *const d;
0108 };
0109 
0110 /**
0111     Helper functions
0112   */
0113 
0114 /**
0115  * Converts a QDateTime to a date string as it is used in VCard and LDIF files.
0116  * The return value is in the form "yyyyMMddThhmmssZ" (e.g. "20031201T120000Z")
0117  * @param dateTime date and time to be converted
0118  */
0119 Q_REQUIRED_RESULT KCONTACTS_EXPORT QString dateToVCardString(const QDateTime &dateTime);
0120 
0121 /**
0122  * Converts a QDate to a short date string as it is used in VCard and LDIF files.
0123  * The return value is in the form "yyyyMMdd" (e.g. "20031201")
0124  * @param date date to be converted
0125  */
0126 Q_REQUIRED_RESULT KCONTACTS_EXPORT QString dateToVCardString(QDate date);
0127 
0128 /**
0129  * Converts a date string as it is used in VCard and LDIF files to a QDateTime value.
0130  * If the date string does not contain a time value, it will be returned as 00:00:00.
0131  * (e.g. "20031201T120000" will return a QDateTime for 2003-12-01 at 12:00)
0132  * @param dateString string representing the date and time.
0133  */
0134 Q_REQUIRED_RESULT KCONTACTS_EXPORT QDateTime VCardStringToDate(const QString &dateString);
0135 
0136 /**
0137  * @brief adaptIMAttributes. Convert KAddressBook attribute to VCard IM Attribute
0138  * @param data
0139  */
0140 KCONTACTS_EXPORT void adaptIMAttributes(QByteArray &data);
0141 }
0142 #endif