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 QString data; 00042 00043 QTextStream t( file ); 00044 t.setEncoding( QTextStream::UnicodeUTF8 ); 00045 data = t.read(); 00046 00047 VCardConverter converter; 00048 Addressee::List l = converter.parseVCards( data ); 00049 00050 if ( ! l.first().isEmpty() ) { 00051 addressee = l.first(); 00052 return true; 00053 } 00054 00055 return false; 00056 } 00057 00058 bool VCardFormatPlugin::loadAll( AddressBook*, Resource *resource, QFile *file ) 00059 { 00060 QString data; 00061 00062 QTextStream t( file ); 00063 t.setEncoding( QTextStream::UnicodeUTF8 ); 00064 data = t.read(); 00065 00066 VCardConverter converter; 00067 00068 Addressee::List l = converter.parseVCards( data ); 00069 00070 Addressee::List::iterator itr; 00071 for ( itr = l.begin(); itr != l.end(); ++itr) { 00072 Addressee addressee = *itr; 00073 addressee.setResource( resource ); 00074 addressee.setChanged( false ); 00075 resource->insertAddressee( addressee ); 00076 } 00077 00078 return true; 00079 } 00080 00081 void VCardFormatPlugin::save( const Addressee &addressee, QFile *file ) 00082 { 00083 VCardConverter converter ; 00084 Addressee::List vcardlist; 00085 00086 00087 vcardlist.append( addressee ); 00088 00089 QTextStream t( file ); 00090 t.setEncoding( QTextStream::UnicodeUTF8 ); 00091 t << converter.createVCards( vcardlist ); 00092 } 00093 00094 void VCardFormatPlugin::saveAll( AddressBook*, Resource *resource, QFile *file ) 00095 { 00096 VCardConverter converter; 00097 Addressee::List vcardlist; 00098 00099 Resource::Iterator it; 00100 for ( it = resource->begin(); it != resource->end(); ++it ) { 00101 (*it).setChanged( false ); 00102 vcardlist.append( *it ); 00103 } 00104 00105 QTextStream t( file ); 00106 t.setEncoding( QTextStream::UnicodeUTF8 ); 00107 t << converter.createVCards( vcardlist ); 00108 } 00109 00110 bool VCardFormatPlugin::checkFormat( QFile *file ) const 00111 { 00112 QString line; 00113 00114 file->readLine( line, 1024 ); 00115 line = line.stripWhiteSpace(); 00116 if ( line == "BEGIN:VCARD" ) 00117 return true; 00118 else 00119 return false; 00120 }