-rw-r--r-- | kabc/formats/binaryformat.cpp | 236 | ||||
-rw-r--r-- | kabc/formats/binaryformat.h | 76 | ||||
-rw-r--r-- | kabc/formats/vcardformatplugin2.cpp | 123 | ||||
-rw-r--r-- | kabc/formats/vcardformatplugin2.h | 68 |
4 files changed, 503 insertions, 0 deletions
diff --git a/kabc/formats/binaryformat.cpp b/kabc/formats/binaryformat.cpp new file mode 100644 index 0000000..e2f28b8 --- a/dev/null +++ b/kabc/formats/binaryformat.cpp | |||
@@ -0,0 +1,236 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | |||
28 | |||
29 | #include <qdatastream.h> | ||
30 | #include <qimage.h> | ||
31 | |||
32 | #include <kdebug.h> | ||
33 | #include <klocale.h> | ||
34 | #include <kstandarddirs.h> | ||
35 | |||
36 | #include "addressbook.h" | ||
37 | #include "addressee.h" | ||
38 | #include "picture.h" | ||
39 | #include "sound.h" | ||
40 | |||
41 | #include "binaryformat.h" | ||
42 | |||
43 | #define BINARY_FORMAT_VERSION 2 | ||
44 | |||
45 | using namespace KABC; | ||
46 | |||
47 | /*US | ||
48 | extern "C" | ||
49 | { | ||
50 | FormatPlugin *format() | ||
51 | { | ||
52 | qDebug(" BinaryFormat::format = new BinaryFormat"); | ||
53 | return new BinaryFormat; | ||
54 | } | ||
55 | } | ||
56 | */ | ||
57 | bool BinaryFormat::load( Addressee &addressee, QFile *file ) | ||
58 | { | ||
59 | kdDebug(5700) << "BinaryFormat::load()" << endl; | ||
60 | QDataStream stream( file ); | ||
61 | |||
62 | if ( !checkHeader( stream ) ) | ||
63 | return false; | ||
64 | |||
65 | loadAddressee( addressee, stream ); | ||
66 | |||
67 | return true; | ||
68 | } | ||
69 | |||
70 | bool BinaryFormat::loadAll( AddressBook *addressBook, Resource *resource, QFile *file ) | ||
71 | { | ||
72 | kdDebug(5700) << "BinaryFormat::loadAll()" << endl; | ||
73 | |||
74 | QDataStream stream( file ); | ||
75 | |||
76 | if ( !checkHeader( stream ) ) | ||
77 | return false; | ||
78 | |||
79 | Q_UINT32 entries; | ||
80 | |||
81 | stream >> entries; | ||
82 | |||
83 | for ( uint i = 0; i < entries; ++i ) { | ||
84 | Addressee addressee; | ||
85 | loadAddressee( addressee, stream ); | ||
86 | addressee.setResource( resource ); | ||
87 | addressBook->insertAddressee( addressee ); | ||
88 | } | ||
89 | |||
90 | return true; | ||
91 | } | ||
92 | |||
93 | void BinaryFormat::save( const Addressee &addressee, QFile *file ) | ||
94 | { | ||
95 | kdDebug(5700) << "BinaryFormat::save()" << endl; | ||
96 | |||
97 | QDataStream stream( file ); | ||
98 | |||
99 | writeHeader( stream ); | ||
100 | |||
101 | Q_UINT32 entries = 1; | ||
102 | stream << entries; | ||
103 | saveAddressee( addressee, stream ); | ||
104 | } | ||
105 | |||
106 | void BinaryFormat::saveAll( AddressBook *ab, Resource *resource, QFile *file ) | ||
107 | { | ||
108 | kdDebug(5700) << "BinaryFormat::saveAll()" << endl; | ||
109 | |||
110 | Q_UINT32 counter = 0; | ||
111 | QDataStream stream( file ); | ||
112 | |||
113 | writeHeader( stream ); | ||
114 | // set dummy number of entries | ||
115 | stream << counter; | ||
116 | |||
117 | AddressBook::Iterator it; | ||
118 | for ( it = ab->begin(); it != ab->end(); ++it ) { | ||
119 | if ( (*it).resource() == resource ) { | ||
120 | saveAddressee( (*it), stream ); | ||
121 | counter++; | ||
122 | (*it).setChanged( false ); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | // set real number of entries | ||
127 | stream.device()->at( 2 * sizeof( Q_UINT32 ) ); | ||
128 | stream << counter; | ||
129 | } | ||
130 | |||
131 | bool BinaryFormat::checkFormat( QFile *file ) const | ||
132 | { | ||
133 | kdDebug(5700) << "BinaryFormat::checkFormat()" << endl; | ||
134 | |||
135 | QDataStream stream( file ); | ||
136 | |||
137 | return checkHeader( stream ); | ||
138 | } | ||
139 | |||
140 | bool BinaryFormat::checkHeader( QDataStream &stream ) const | ||
141 | { | ||
142 | Q_UINT32 magic, version; | ||
143 | |||
144 | stream >> magic >> version; | ||
145 | |||
146 | //US QFile *file = dynamic_cast<QFile*>( stream.device() ); | ||
147 | QFile *file = (QFile*)( stream.device() ); | ||
148 | |||
149 | if ( !file ) { | ||
150 | qDebug("BinaryFormat::checkHeader : Not a file?"); | ||
151 | kdError() << i18n("Not a file?") << endl; | ||
152 | return false; | ||
153 | } | ||
154 | |||
155 | if ( magic != 0x2e93e ) { | ||
156 | qDebug("BinaryFormat::checkHeader : File '%s' is not binary format.", file->name().latin1()); | ||
157 | kdError() << i18n("File '%1' is not binary format.").arg( file->name() ) << endl; | ||
158 | return false; | ||
159 | } | ||
160 | |||
161 | if ( version != BINARY_FORMAT_VERSION ) { | ||
162 | qDebug("BinaryFormat::checkHeader : File '%s' is the wrong version.", file->name().latin1()); | ||
163 | kdError() << i18n("File '%1' is the wrong version.").arg( file->name() ) << endl; | ||
164 | return false; | ||
165 | } | ||
166 | |||
167 | return true; | ||
168 | } | ||
169 | |||
170 | void BinaryFormat::writeHeader( QDataStream &stream ) | ||
171 | { | ||
172 | Q_UINT32 magic, version; | ||
173 | |||
174 | magic = 0x2e93e; | ||
175 | version = BINARY_FORMAT_VERSION; | ||
176 | |||
177 | stream << magic << version; | ||
178 | } | ||
179 | |||
180 | void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream ) | ||
181 | { | ||
182 | stream >> addressee; | ||
183 | /* | ||
184 | // load pictures | ||
185 | Picture photo = addressee.photo(); | ||
186 | Picture logo = addressee.logo(); | ||
187 | |||
188 | if ( photo.isIntern() ) { | ||
189 | QImage img; | ||
190 | if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) ) | ||
191 | kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl; | ||
192 | |||
193 | addressee.setPhoto( img ); | ||
194 | } | ||
195 | |||
196 | if ( logo.isIntern() ) { | ||
197 | QImage img; | ||
198 | if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) ) | ||
199 | kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl; | ||
200 | |||
201 | addressee.setLogo( img ); | ||
202 | } | ||
203 | |||
204 | // load sound | ||
205 | // TODO: load sound data from file | ||
206 | */ | ||
207 | } | ||
208 | |||
209 | void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream ) | ||
210 | { | ||
211 | stream << addressee; | ||
212 | /* | ||
213 | // load pictures | ||
214 | Picture photo = addressee.photo(); | ||
215 | Picture logo = addressee.logo(); | ||
216 | |||
217 | if ( photo.isIntern() ) { | ||
218 | QImage img = photo.data(); | ||
219 | QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid(); | ||
220 | |||
221 | if ( !img.save( fileName, "PNG" ) ) | ||
222 | kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl; | ||
223 | } | ||
224 | |||
225 | if ( logo.isIntern() ) { | ||
226 | QImage img = logo.data(); | ||
227 | QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid(); | ||
228 | |||
229 | if ( !img.save( fileName, "PNG" ) ) | ||
230 | kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl; | ||
231 | } | ||
232 | |||
233 | // save sound | ||
234 | // TODO: save the sound data to file | ||
235 | */ | ||
236 | } | ||
diff --git a/kabc/formats/binaryformat.h b/kabc/formats/binaryformat.h new file mode 100644 index 0000000..415fd3b --- a/dev/null +++ b/kabc/formats/binaryformat.h | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /* | ||
21 | Enhanced Version of the file for platform independent KDE tools. | ||
22 | Copyright (c) 2004 Ulf Schenk | ||
23 | |||
24 | $Id$ | ||
25 | */ | ||
26 | |||
27 | #ifndef KABC_BINARYFORMAT_H | ||
28 | #define KABC_BINARYFORMAT_H | ||
29 | |||
30 | #include "formatplugin.h" | ||
31 | |||
32 | namespace KABC { | ||
33 | |||
34 | class AddressBook; | ||
35 | class Addressee; | ||
36 | |||
37 | /** | ||
38 | @short binary file format for addressbook entries. | ||
39 | */ | ||
40 | class BinaryFormat : public FormatPlugin | ||
41 | { | ||
42 | public: | ||
43 | /** | ||
44 | * Load single addressee from file. | ||
45 | */ | ||
46 | bool load( Addressee &, QFile *file ); | ||
47 | |||
48 | /** | ||
49 | * Load whole addressee from file. | ||
50 | */ | ||
51 | bool loadAll( AddressBook *, Resource *, QFile *file ); | ||
52 | |||
53 | /** | ||
54 | * Save single addressee to file. | ||
55 | */ | ||
56 | void save( const Addressee &, QFile *file ); | ||
57 | |||
58 | /** | ||
59 | * Save all addressees to file. | ||
60 | */ | ||
61 | void saveAll( AddressBook *, Resource *, QFile *file ); | ||
62 | |||
63 | /** | ||
64 | * Check for valid format of a file. | ||
65 | */ | ||
66 | bool checkFormat( QFile *file ) const; | ||
67 | |||
68 | private: | ||
69 | void loadAddressee( Addressee &, QDataStream & ); | ||
70 | void saveAddressee( const Addressee &, QDataStream & ); | ||
71 | bool checkHeader( QDataStream & ) const; | ||
72 | void writeHeader( QDataStream & ); | ||
73 | }; | ||
74 | |||
75 | } | ||
76 | #endif | ||
diff --git a/kabc/formats/vcardformatplugin2.cpp b/kabc/formats/vcardformatplugin2.cpp new file mode 100644 index 0000000..2a772b4 --- a/dev/null +++ b/kabc/formats/vcardformatplugin2.cpp | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | Enhanced Version of the file for platform independent KDE tools. | ||
3 | Copyright (c) 2004 Ulf Schenk | ||
4 | |||
5 | $Id$ | ||
6 | */ | ||
7 | |||
8 | #include "vcardformatplugin2.h" | ||
9 | |||
10 | #include "address.h" | ||
11 | #include "addressee.h" | ||
12 | #include "vcardparser/vcardtool.h" | ||
13 | |||
14 | #include <qtextstream.h> | ||
15 | #include <qfile.h> | ||
16 | |||
17 | using namespace KABC; | ||
18 | |||
19 | /*US | ||
20 | extern "C" | ||
21 | { | ||
22 | FormatPlugin *format() | ||
23 | { | ||
24 | qDebug(" VCardFormatPlugin2::format = new VCardFormatPlugin2"); | ||
25 | return new VCardFormatPlugin2(); | ||
26 | } | ||
27 | } | ||
28 | */ | ||
29 | |||
30 | VCardFormatPlugin2::VCardFormatPlugin2() | ||
31 | { | ||
32 | } | ||
33 | |||
34 | VCardFormatPlugin2::~VCardFormatPlugin2() | ||
35 | { | ||
36 | } | ||
37 | |||
38 | bool VCardFormatPlugin2::load( Addressee &addressee, QFile *file ) | ||
39 | { | ||
40 | QString data; | ||
41 | |||
42 | QTextStream t( file ); | ||
43 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
44 | data = t.read(); | ||
45 | |||
46 | VCardTool tool; | ||
47 | |||
48 | Addressee::List l = tool.parseVCards( data ); | ||
49 | |||
50 | if ( ! l.first().isEmpty() ) { | ||
51 | addressee = l.first(); | ||
52 | return true; | ||
53 | } | ||
54 | |||
55 | return false; | ||
56 | } | ||
57 | |||
58 | bool VCardFormatPlugin2::loadAll( AddressBook *addressBook, Resource *resource, QFile *file ) | ||
59 | { | ||
60 | QString data; | ||
61 | |||
62 | QTextStream t( file ); | ||
63 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
64 | data = t.read(); | ||
65 | |||
66 | VCardTool tool; | ||
67 | |||
68 | Addressee::List l = tool.parseVCards( data ); | ||
69 | |||
70 | Addressee::List::Iterator itr; | ||
71 | |||
72 | for ( itr = l.begin(); itr != l.end(); ++itr) { | ||
73 | Addressee addressee = *itr; | ||
74 | addressee.setResource( resource ); | ||
75 | addressBook->insertAddressee( addressee ); | ||
76 | } | ||
77 | |||
78 | return true; | ||
79 | } | ||
80 | |||
81 | void VCardFormatPlugin2::save( const Addressee &addressee, QFile *file ) | ||
82 | { | ||
83 | VCardTool tool; | ||
84 | Addressee::List vcardlist; | ||
85 | |||
86 | |||
87 | vcardlist.append( addressee ); | ||
88 | |||
89 | QTextStream t( file ); | ||
90 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
91 | t << tool.createVCards( vcardlist ); | ||
92 | } | ||
93 | |||
94 | void VCardFormatPlugin2::saveAll( AddressBook *ab, Resource *resource, QFile *file ) | ||
95 | { | ||
96 | VCardTool tool; | ||
97 | Addressee::List vcardlist; | ||
98 | |||
99 | AddressBook::Iterator it; | ||
100 | for ( it = ab->begin(); it != ab->end(); ++it ) { | ||
101 | if ( (*it).resource() == resource ) { | ||
102 | (*it).setChanged( false ); | ||
103 | vcardlist.append( *it ); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | QTextStream t( file ); | ||
108 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
109 | t << tool.createVCards( vcardlist ); | ||
110 | } | ||
111 | |||
112 | bool VCardFormatPlugin2::checkFormat( QFile *file ) const | ||
113 | { | ||
114 | QString line; | ||
115 | |||
116 | file->readLine( line, 1024 ); | ||
117 | line = line.stripWhiteSpace(); | ||
118 | if ( line == "BEGIN:VCARD" ) | ||
119 | return true; | ||
120 | else | ||
121 | return false; | ||
122 | } | ||
123 | |||
diff --git a/kabc/formats/vcardformatplugin2.h b/kabc/formats/vcardformatplugin2.h new file mode 100644 index 0000000..585ab6b --- a/dev/null +++ b/kabc/formats/vcardformatplugin2.h | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2003 Zack Rusin <zack@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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | |||
28 | #ifndef VCARDFORMATPLUGIN2_H | ||
29 | #define VCARDFORMATPLUGIN2_H | ||
30 | |||
31 | #include "formatplugin.h" | ||
32 | #include "addressee.h" | ||
33 | |||
34 | class QFile; | ||
35 | |||
36 | namespace KABC { | ||
37 | |||
38 | class Resource; | ||
39 | class AddressBook; | ||
40 | /** | ||
41 | @short Interface of vCard backend for address book. | ||
42 | |||
43 | This class implements the file format interface of address book entries for | ||
44 | the vCard format. | ||
45 | */ | ||
46 | class VCardFormatPlugin2 : public FormatPlugin | ||
47 | { | ||
48 | public: | ||
49 | VCardFormatPlugin2(); | ||
50 | virtual ~VCardFormatPlugin2(); | ||
51 | |||
52 | bool load( Addressee &, QFile *file ); | ||
53 | bool loadAll( AddressBook *, Resource *, QFile *file ); | ||
54 | void save( const Addressee &, QFile *file ); | ||
55 | void saveAll( AddressBook *, Resource *, QFile *file ); | ||
56 | |||
57 | bool checkFormat( QFile *file ) const; | ||
58 | |||
59 | private: | ||
60 | struct VCardFormatPrivate; | ||
61 | VCardFormatPrivate *d; | ||
62 | }; | ||
63 | |||
64 | |||
65 | } | ||
66 | |||
67 | |||
68 | #endif | ||