• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
  • formats
binaryformat.cpp
1/*
2 This file is part of libtdeabc.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <tqdatastream.h>
22#include <tqimage.h>
23
24#include <kdebug.h>
25#include <tdelocale.h>
26#include <tdestandarddirs.h>
27
28#include "addressbook.h"
29#include "addressee.h"
30#include "picture.h"
31#include "sound.h"
32
33#include "binaryformat.h"
34
35#define BINARY_FORMAT_VERSION 1
36
37using namespace TDEABC;
38
39extern "C"
40{
41 TDE_EXPORT FormatPlugin *format()
42 {
43 return new BinaryFormat;
44 }
45}
46
47bool BinaryFormat::load( Addressee &addressee, TQFile *file )
48{
49 kdDebug(5700) << "BinaryFormat::load()" << endl;
50 TQDataStream stream( file );
51
52 if ( !checkHeader( stream ) )
53 return false;
54
55 loadAddressee( addressee, stream );
56
57 return true;
58}
59
60bool BinaryFormat::loadAll( AddressBook*, Resource *resource, TQFile *file )
61{
62 kdDebug(5700) << "BinaryFormat::loadAll()" << endl;
63
64 TQDataStream stream( file );
65
66 if ( !checkHeader( stream ) )
67 return false;
68
69 TQ_UINT32 entries;
70
71 stream >> entries;
72
73 for ( uint i = 0; i < entries; ++i ) {
74 Addressee addressee;
75 loadAddressee( addressee, stream );
76 addressee.setResource( resource );
77 addressee.setChanged( false );
78 resource->insertAddressee( addressee );
79 }
80
81 return true;
82}
83
84void BinaryFormat::save( const Addressee &addressee, TQFile *file )
85{
86 kdDebug(5700) << "BinaryFormat::save()" << endl;
87
88 TQDataStream stream( file );
89
90 writeHeader( stream );
91
92 TQ_UINT32 entries = 1;
93 stream << entries;
94 saveAddressee( addressee, stream );
95}
96
97void BinaryFormat::saveAll( AddressBook*, Resource *resource, TQFile *file )
98{
99 kdDebug(5700) << "BinaryFormat::saveAll()" << endl;
100
101 TQ_UINT32 counter = 0;
102 TQDataStream stream( file );
103
104 writeHeader( stream );
105 // set dummy number of entries
106 stream << counter;
107
108 Resource::Iterator it;
109 for ( it = resource->begin(); it != resource->end(); ++it ) {
110 saveAddressee( (*it), stream );
111 counter++;
112 (*it).setChanged( false );
113 }
114
115 // set real number of entries
116 stream.device()->at( 2 * sizeof( TQ_UINT32 ) );
117 stream << counter;
118}
119
120bool BinaryFormat::checkFormat( TQFile *file ) const
121{
122 kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
123
124 TQDataStream stream( file );
125
126 return checkHeader( stream );
127}
128
129bool BinaryFormat::checkHeader( TQDataStream &stream ) const
130{
131 TQ_UINT32 magic, version;
132
133 stream >> magic >> version;
134
135 TQFile *file = dynamic_cast<TQFile*>( stream.device() );
136
137 if ( !file ) {
138 kdError() << i18n("Not a file?") << endl;
139 return false;
140 }
141
142 if ( magic != 0x2e93e ) {
143 kdError() << TQString(i18n("File '%1' is not binary format.").arg( file->name() )) << endl;
144 return false;
145 }
146
147 if ( version != BINARY_FORMAT_VERSION ) {
148 kdError() << TQString(i18n("File '%1' is the wrong version.").arg( file->name() )) << endl;
149 return false;
150 }
151
152 return true;
153}
154
155void BinaryFormat::writeHeader( TQDataStream &stream )
156{
157 TQ_UINT32 magic, version;
158
159 magic = 0x2e93e;
160 version = BINARY_FORMAT_VERSION;
161
162 stream << magic << version;
163}
164
165void BinaryFormat::loadAddressee( Addressee &addressee, TQDataStream &stream )
166{
167 stream >> addressee;
168/*
169 // load pictures
170 Picture photo = addressee.photo();
171 Picture logo = addressee.logo();
172
173 if ( photo.isIntern() ) {
174 TQImage img;
175 if ( !img.load( locateLocal( "data", "tdeabc/photos/" ) + addressee.uid() ) )
176 kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl;
177
178 addressee.setPhoto( img );
179 }
180
181 if ( logo.isIntern() ) {
182 TQImage img;
183 if ( !img.load( locateLocal( "data", "tdeabc/logos/" ) + addressee.uid() ) )
184 kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl;
185
186 addressee.setLogo( img );
187 }
188
189 // load sound
190 // TODO: load sound data from file
191*/
192}
193
194void BinaryFormat::saveAddressee( const Addressee &addressee, TQDataStream &stream )
195{
196 stream << addressee;
197/*
198 // load pictures
199 Picture photo = addressee.photo();
200 Picture logo = addressee.logo();
201
202 if ( photo.isIntern() ) {
203 TQImage img = photo.data();
204 TQString fileName = locateLocal( "data", "tdeabc/photos/" ) + addressee.uid();
205
206 if ( !img.save( fileName, "PNG" ) )
207 kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl;
208 }
209
210 if ( logo.isIntern() ) {
211 TQImage img = logo.data();
212 TQString fileName = locateLocal( "data", "tdeabc/logos/" ) + addressee.uid();
213
214 if ( !img.save( fileName, "PNG" ) )
215 kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl;
216 }
217
218 // save sound
219 // TODO: save the sound data to file
220*/
221}
TDEABC::AddressBook
Address Book.
Definition: addressbook.h:44
TDEABC::Addressee
address book entry
Definition: addressee.src.h:75
TDEABC::Addressee::setChanged
void setChanged(bool value)
Mark addressee as changed.
Definition: addressee.src.cpp:1024
TDEABC::Addressee::setResource
void setResource(Resource *resource)
Set resource where the addressee is from.
Definition: addressee.src.cpp:1013
TDEABC::BinaryFormat
binary file format for addressbook entries.
Definition: binaryformat.h:34
TDEABC::BinaryFormat::checkFormat
bool checkFormat(TQFile *file) const
Check for valid format of a file.
Definition: binaryformat.cpp:120
TDEABC::BinaryFormat::save
void save(const Addressee &, TQFile *file)
Save single addressee to file.
Definition: binaryformat.cpp:84
TDEABC::BinaryFormat::saveAll
void saveAll(AddressBook *, Resource *, TQFile *file)
Save all addressees to file.
Definition: binaryformat.cpp:97
TDEABC::BinaryFormat::loadAll
bool loadAll(AddressBook *, Resource *, TQFile *file)
Load whole addressee from file.
Definition: binaryformat.cpp:60
TDEABC::FormatPlugin
Base class for address book formats.
Definition: formatplugin.h:43
TDEABC::Resource::Iterator
Resource Iterator.
Definition: resource.h:69
kdError
kdbgstream kdError(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
KDE::version
unsigned int version()
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
tdelocale.h

tdeabc

Skip menu "tdeabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.