summaryrefslogtreecommitdiffabout
path: root/kabc/formats/binaryformat.cpp
Unidiff
Diffstat (limited to 'kabc/formats/binaryformat.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/formats/binaryformat.cpp236
1 files changed, 236 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/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (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
45using namespace KABC;
46
47/*US
48extern "C"
49{
50 FormatPlugin *format()
51 {
52 qDebug(" BinaryFormat::format = new BinaryFormat");
53 return new BinaryFormat;
54 }
55}
56*/
57bool 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
70bool 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
93void 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
106void 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
131bool BinaryFormat::checkFormat( QFile *file ) const
132{
133 kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
134
135 QDataStream stream( file );
136
137 return checkHeader( stream );
138}
139
140bool 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
170void 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
180void 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
209void 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}