1/*
2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "vcardformat.h"
22#include "vcardconverter.h"
23#include "address.h"
24#include "addressee.h"
25
26#include <QtCore/QFile>
27
28using namespace KABC;
29
30VCardFormat::VCardFormat()
31 : d( 0 )
32{
33}
34
35VCardFormat::~VCardFormat()
36{
37 //delete d;
38}
39
40bool VCardFormat::load( Addressee &addressee, QFile *file )
41{
42 QByteArray data;
43
44 data = file->readAll();
45
46 VCardConverter converter;
47 Addressee::List l = converter.parseVCards( data );
48
49 if ( ! l.first().isEmpty() ) {
50 addressee = l.first();
51 return true;
52 }
53
54 return false;
55}
56
57bool VCardFormat::loadAll( AddressBook *, Resource *resource, QFile *file )
58{
59 QByteArray data;
60
61 data = file->readAll();
62
63 VCardConverter converter;
64
65 Addressee::List l = converter.parseVCards( data );
66
67 Addressee::List::iterator itr;
68 for ( itr = l.begin(); itr != l.end(); ++itr ) {
69 Addressee addressee = *itr;
70 addressee.setResource( resource );
71 addressee.setChanged( false );
72 resource->insertAddressee( addressee );
73 }
74
75 return true;
76}
77
78void VCardFormat::save( const Addressee &addressee, QFile *file )
79{
80 VCardConverter converter;
81 Addressee::List vcardlist;
82
83 vcardlist.append( addressee );
84
85 QByteArray data = converter.createVCards( vcardlist );
86
87 file->write( data );
88}
89
90void VCardFormat::saveAll( AddressBook *, Resource *resource, QFile *file )
91{
92 VCardConverter converter;
93 Addressee::List vcardlist;
94
95 Resource::Iterator it;
96 Resource::Iterator end( resource->end() );
97 for ( it = resource->begin(); it != end; ++it ) {
98 ( *it ).setChanged( false );
99 vcardlist.append( *it );
100 }
101
102 QByteArray data = converter.createVCards( vcardlist );
103
104 file->write( data );
105}
106
107bool VCardFormat::checkFormat( QFile *file ) const
108{
109 QByteArray line = file->readLine();
110 line = line.trimmed();
111 if ( line == "BEGIN:VCARD" ) {
112 return true;
113 } else {
114 return false;
115 }
116}
117