1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
This file is part of libtdeabc.
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqdatastream.h>
#include <tqimage.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include "addressbook.h"
#include "addressee.h"
#include "picture.h"
#include "sound.h"
#include "binaryformat.h"
#define BINARY_FORMAT_VERSION 1
using namespace TDEABC;
extern "C"
{
TDE_EXPORT FormatPlugin *format()
{
return new BinaryFormat;
}
}
bool BinaryFormat::load( Addressee &addressee, TQFile *file )
{
kdDebug(5700) << "BinaryFormat::load()" << endl;
TQDataStream stream( file );
if ( !checkHeader( stream ) )
return false;
loadAddressee( addressee, stream );
return true;
}
bool BinaryFormat::loadAll( AddressBook*, Resource *resource, TQFile *file )
{
kdDebug(5700) << "BinaryFormat::loadAll()" << endl;
TQDataStream stream( file );
if ( !checkHeader( stream ) )
return false;
TQ_UINT32 entries;
stream >> entries;
for ( uint i = 0; i < entries; ++i ) {
Addressee addressee;
loadAddressee( addressee, stream );
addressee.setResource( resource );
addressee.setChanged( false );
resource->insertAddressee( addressee );
}
return true;
}
void BinaryFormat::save( const Addressee &addressee, TQFile *file )
{
kdDebug(5700) << "BinaryFormat::save()" << endl;
TQDataStream stream( file );
writeHeader( stream );
TQ_UINT32 entries = 1;
stream << entries;
saveAddressee( addressee, stream );
}
void BinaryFormat::saveAll( AddressBook*, Resource *resource, TQFile *file )
{
kdDebug(5700) << "BinaryFormat::saveAll()" << endl;
TQ_UINT32 counter = 0;
TQDataStream stream( file );
writeHeader( stream );
// set dummy number of entries
stream << counter;
Resource::Iterator it;
for ( it = resource->begin(); it != resource->end(); ++it ) {
saveAddressee( (*it), stream );
counter++;
(*it).setChanged( false );
}
// set real number of entries
stream.device()->at( 2 * sizeof( TQ_UINT32 ) );
stream << counter;
}
bool BinaryFormat::checkFormat( TQFile *file ) const
{
kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
TQDataStream stream( file );
return checkHeader( stream );
}
bool BinaryFormat::checkHeader( TQDataStream &stream ) const
{
TQ_UINT32 magic, version;
stream >> magic >> version;
TQFile *file = dynamic_cast<TQFile*>( stream.device() );
if ( !file ) {
kdError() << i18n("Not a file?") << endl;
return false;
}
if ( magic != 0x2e93e ) {
kdError() << TQString(i18n("File '%1' is not binary format.").arg( file->name() )) << endl;
return false;
}
if ( version != BINARY_FORMAT_VERSION ) {
kdError() << TQString(i18n("File '%1' is the wrong version.").arg( file->name() )) << endl;
return false;
}
return true;
}
void BinaryFormat::writeHeader( TQDataStream &stream )
{
TQ_UINT32 magic, version;
magic = 0x2e93e;
version = BINARY_FORMAT_VERSION;
stream << magic << version;
}
void BinaryFormat::loadAddressee( Addressee &addressee, TQDataStream &stream )
{
stream >> addressee;
/*
// load pictures
Picture photo = addressee.photo();
Picture logo = addressee.logo();
if ( photo.isIntern() ) {
TQImage img;
if ( !img.load( locateLocal( "data", "tdeabc/photos/" ) + addressee.uid() ) )
kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl;
addressee.setPhoto( img );
}
if ( logo.isIntern() ) {
TQImage img;
if ( !img.load( locateLocal( "data", "tdeabc/logos/" ) + addressee.uid() ) )
kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl;
addressee.setLogo( img );
}
// load sound
// TODO: load sound data from file
*/
}
void BinaryFormat::saveAddressee( const Addressee &addressee, TQDataStream &stream )
{
stream << addressee;
/*
// load pictures
Picture photo = addressee.photo();
Picture logo = addressee.logo();
if ( photo.isIntern() ) {
TQImage img = photo.data();
TQString fileName = locateLocal( "data", "tdeabc/photos/" ) + addressee.uid();
if ( !img.save( fileName, "PNG" ) )
kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl;
}
if ( logo.isIntern() ) {
TQImage img = logo.data();
TQString fileName = locateLocal( "data", "tdeabc/logos/" ) + addressee.uid();
if ( !img.save( fileName, "PNG" ) )
kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl;
}
// save sound
// TODO: save the sound data to file
*/
}
|