vcardconverter.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "vcard21parser.h"
00022 #include "vcardformatimpl.h"
00023 #include "vcardtool.h"
00024 
00025 #include "vcardconverter.h"
00026 
00027 using namespace KABC;
00028 
00029 struct VCardConverter::VCardConverterData
00030 {
00031   VCard21Parser vcard21parser;
00032   VCardFormatImpl vcard30parser;
00033 };
00034 
00035 VCardConverter::VCardConverter()
00036   : d( new VCardConverterData )
00037 {
00038 }
00039 
00040 VCardConverter::~VCardConverter()
00041 {
00042   delete d;
00043   d = 0;
00044 }
00045 
00046 QString VCardConverter::createVCard( const Addressee &addr, Version version )
00047 {
00048   Addressee::List list;
00049   list.append( addr );
00050 
00051   return createVCards( list, version );
00052 }
00053 
00054 QCString VCardConverter::createVCardRaw( const Addressee &addr, Version version )
00055 {
00056   Addressee::List list;
00057   list.append( addr );
00058 
00059   return createVCardsRaw( list, version );
00060 }
00061 
00062 QString VCardConverter::createVCards( Addressee::List list, Version version )
00063 {
00064   VCardTool tool;
00065 
00066   return tool.createVCards( list, ( version == v3_0 ? VCard::v3_0 : VCard::v2_1 ) );
00067 }
00068 
00069 QCString VCardConverter::createVCardsRaw( Addressee::List list, Version version )
00070 {
00071   VCardTool tool;
00072 
00073   return tool.createVCardsRaw( list, ( version == v3_0 ? VCard::v3_0 : VCard::v2_1 ) );
00074 }
00075 
00076 Addressee VCardConverter::parseVCard( const QString& vcard )
00077 {
00078   Addressee::List list = parseVCards( vcard );
00079 
00080   return list[ 0 ];
00081 }
00082 
00083 Addressee VCardConverter::parseVCardRaw( const QCString& vcard )
00084 {
00085   Addressee::List list = parseVCardsRaw( vcard );
00086 
00087   return list[ 0 ];
00088 }
00089 
00090 Addressee::List VCardConverter::parseVCards( const QString& vcard )
00091 {
00092   VCardTool tool;
00093 
00094   return tool.parseVCards( vcard );
00095 }
00096 
00097 Addressee::List VCardConverter::parseVCardsRaw( const QCString& vcard )
00098 {
00099   VCardTool tool;
00100 
00101   return tool.parseVCardsRaw( vcard );
00102 }
00103 
00104 // ---------------------------- deprecated stuff ---------------------------- //
00105 
00106 bool VCardConverter::vCardToAddressee( const QString &str, Addressee &addr, Version version )
00107 {
00108   if ( version == v2_1 ) {
00109     addr = d->vcard21parser.readFromString( str );
00110     return true;
00111   }
00112 
00113   if ( version == v3_0 )
00114     return d->vcard30parser.readFromString( str, addr );
00115 
00116   return false;
00117 }
00118 
00119 bool VCardConverter::addresseeToVCard( const Addressee &addr, QString &str, Version version )
00120 {
00121   if ( version == v2_1 )
00122     return false;
00123 
00124   if ( version == v3_0 )
00125     return d->vcard30parser.writeToString( addr, str );
00126 
00127   return false;
00128 }
00129 
00130 
00131 /* Helper functions */
00132 
00133 QString KABC::dateToVCardString( const QDateTime &dateTime )
00134 {
00135   return dateTime.toString("yyyyMMddThhmmssZ");
00136 }
00137 
00138 QString KABC::dateToVCardString( const QDate &date )
00139 {
00140   return date.toString("yyyyMMdd");
00141 }
00142 
00143 QDateTime KABC::VCardStringToDate( const QString &dateString )
00144 {
00145   QDate date;
00146   QTime time;
00147   QString d( dateString );
00148 
00149   d = d.remove('-').remove(':');
00150 
00151   if (d.length()>=8)
00152     date = QDate( d.mid(0,4).toUInt(), d.mid(4,2).toUInt(), d.mid(6,2).toUInt() );
00153   if (d.length()>9 && d[8].upper()=='T')
00154     time = QTime( d.mid(9,2).toUInt(), d.mid(11,2).toUInt(), d.mid(13,2).toUInt() );
00155 
00156   return QDateTime( date, time );
00157 }
00158 
KDE Home | KDE Accessibility Home | Description of Access Keys