vcardformatplugin.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@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 <qfile.h>
00022 
00023 #include "address.h"
00024 #include "addressee.h"
00025 #include "vcardconverter.h"
00026 
00027 #include "vcardformatplugin.h"
00028 
00029 using namespace KABC;
00030 
00031 VCardFormatPlugin::VCardFormatPlugin()
00032 {
00033 }
00034 
00035 VCardFormatPlugin::~VCardFormatPlugin()
00036 {
00037 }
00038 
00039 bool VCardFormatPlugin::load( Addressee &addressee, QFile *file )
00040 {
00041   const QByteArray rawData = file->readAll();
00042   const QCString data( rawData.data(), rawData.size() );
00043 
00044   VCardConverter converter;
00045   Addressee::List l = converter.parseVCardsRaw( data );
00046 
00047   if ( ! l.first().isEmpty() ) {
00048     addressee = l.first();
00049     return true;
00050   }
00051 
00052   return false;
00053 }
00054 
00055 bool VCardFormatPlugin::loadAll( AddressBook*, Resource *resource, QFile *file )
00056 {
00057   const QByteArray rawData = file->readAll();
00058   const QCString data( rawData.data(), rawData.size() );
00059 
00060   VCardConverter converter;
00061   Addressee::List l = converter.parseVCardsRaw( data );
00062 
00063   Addressee::List::iterator itr;
00064   for ( itr = l.begin(); itr != l.end(); ++itr) {
00065     Addressee addressee = *itr;
00066     addressee.setResource( resource );
00067     addressee.setChanged( false );
00068     resource->insertAddressee( addressee );
00069   }
00070 
00071   return true;
00072 }
00073 
00074 void VCardFormatPlugin::save( const Addressee &addressee, QFile *file )
00075 {
00076   VCardConverter converter ;
00077   Addressee::List vcardlist;
00078 
00079 
00080   vcardlist.append( addressee );
00081 
00082   const QCString data = converter.createVCardsRaw( vcardlist );
00083   file->writeBlock( data, data.length() );
00084 }
00085 
00086 void VCardFormatPlugin::saveAll( AddressBook*, Resource *resource, QFile *file )
00087 {
00088   VCardConverter converter;
00089   Addressee::List vcardlist;
00090 
00091   Resource::Iterator it;
00092   for ( it = resource->begin(); it != resource->end(); ++it ) {
00093     (*it).setChanged( false );
00094     vcardlist.append( *it );
00095   }
00096 
00097   const QCString data = converter.createVCardsRaw( vcardlist );
00098   file->writeBlock( data, data.length() );
00099 }
00100 
00101 bool VCardFormatPlugin::checkFormat( QFile *file ) const
00102 {
00103   QString line;
00104 
00105   file->readLine( line, 1024 );
00106   line = line.stripWhiteSpace();
00107   if ( line == "BEGIN:VCARD" )
00108     return true;
00109   else
00110     return false;
00111 }
KDE Home | KDE Accessibility Home | Description of Access Keys