| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# binaryformat.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qdatastream.h>
    00022 #include <qimage.h>
    00023
    00024 #include <kdebug.h>
    00025 #include <klocale.h>
    00026 #include <kstandarddirs.h>
    00027
    00028 #include "addressbook.h"
    00029 #include "addressee.h"
    00030 #include "picture.h"
    00031 #include "sound.h"
    00032
    00033 #include "binaryformat.h"
    00034
    00035 #define BINARY_FORMAT_VERSION 1
    00036
    00037 using namespace KABC;
    00038
    00039 extern "C"
    00040 {
    00041   FormatPlugin *format()
    00042   {
    00043     return new BinaryFormat;
    00044   }
    00045 }
    00046
    00047 bool BinaryFormat::load( Addressee &addressee, QFile *file )
    00048 {
    00049   kdDebug(5700) << "BinaryFormat::load()" << endl;
    00050   QDataStream stream( file );
    00051
    00052   if ( !checkHeader( stream ) )
    00053     return false;
    00054
    00055   loadAddressee( addressee, stream );
    00056
    00057   return true;
    00058 }
    00059
    00060 bool BinaryFormat::loadAll( AddressBook *addressBook, Resource *resource, QFile *file )
    00061 {
    00062   kdDebug(5700) << "BinaryFormat::loadAll()" << endl;
    00063
    00064   QDataStream stream( file );
    00065
    00066   if ( !checkHeader( stream ) )
    00067     return false;
    00068
    00069   Q_UINT32 entries;
    00070
    00071   stream >> entries;
    00072
    00073   for ( uint i = 0; i < entries; ++i ) {
    00074     Addressee addressee;
    00075     loadAddressee( addressee, stream );
    00076     addressee.setResource( resource );
    00077     addressBook->insertAddressee( addressee );
    00078   }
    00079
    00080   return true;
    00081 }
    00082
    00083 void BinaryFormat::save( const Addressee &addressee, QFile *file )
    00084 {
    00085   kdDebug(5700) << "BinaryFormat::save()" << endl;
    00086
    00087   QDataStream stream( file );
    00088
    00089   writeHeader( stream );
    00090
    00091   Q_UINT32 entries = 1;
    00092   stream << entries;
    00093   saveAddressee( addressee, stream );
    00094 }
    00095
    00096 void BinaryFormat::saveAll( AddressBook *ab, Resource *resource, QFile *file )
    00097 {
    00098   kdDebug(5700) << "BinaryFormat::saveAll()" << endl;
    00099
    00100   Q_UINT32 counter = 0;
    00101   QDataStream stream( file );
    00102
    00103   writeHeader( stream );
    00104   // set dummy number of entries
    00105   stream << counter;
    00106
    00107   AddressBook::Iterator it;
    00108   for ( it = ab->begin(); it != ab->end(); ++it ) {
    00109     if ( (*it).resource() == resource ) {
    00110       saveAddressee( (*it), stream );
    00111       counter++;
    00112       (*it).setChanged( false );
    00113     }
    00114   }
    00115
    00116   // set real number of entries
    00117   stream.device()->at( 2 * sizeof( Q_UINT32 ) );
    00118   stream << counter;
    00119 }
    00120
    00121 bool BinaryFormat::checkFormat( QFile *file ) const
    00122 {
    00123   kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
    00124
    00125   QDataStream stream( file );
    00126
    00127   return checkHeader( stream );
    00128 }
    00129
    00130 bool BinaryFormat::checkHeader( QDataStream &stream ) const
    00131 {
    00132   Q_UINT32 magic, version;
    00133
    00134   stream >> magic >> version;
    00135
    00136   QFile *file = dynamic_cast<QFile*>( stream.device() );
    00137
    00138   if ( magic != 0x2e93e ) {
    00139     kdError() << i18n("File '%1' is not binary format.").arg( file->name() ) << endl;
    00140     return false;
    00141   }
    00142
    00143   if ( version != BINARY_FORMAT_VERSION ) {
    00144     kdError() << i18n("File '%1' is the wrong version.").arg( file->name() ) << endl;
    00145     return false;
    00146   }
    00147
    00148   return true;
    00149 }
    00150
    00151 void BinaryFormat::writeHeader( QDataStream &stream )
    00152 {
    00153   Q_UINT32 magic, version;
    00154
    00155   magic = 0x2e93e;
    00156   version = BINARY_FORMAT_VERSION;
    00157
    00158   stream << magic << version;
    00159 }
    00160
    00161 void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream )
    00162 {
    00163   stream >> addressee;
    00164
    00165   // load pictures
    00166   Picture photo = addressee.photo();
    00167   Picture logo = addressee.logo();
    00168
    00169   if ( photo.isIntern() ) {
    00170     QImage img;
    00171     if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) )
    00172       kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl;
    00173
    00174     addressee.setPhoto( img );
    00175   }
    00176
    00177   if ( logo.isIntern() ) {
    00178     QImage img;
    00179     if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) )
    00180       kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl;
    00181
    00182     addressee.setLogo( img );
    00183   }
    00184
    00185   // load sound
    00186   // TODO: load sound data from file
    00187 }
    00188
    00189 void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream )
    00190 {
    00191   stream << addressee;
    00192
    00193   // load pictures
    00194   Picture photo = addressee.photo();
    00195   Picture logo = addressee.logo();
    00196
    00197   if ( photo.isIntern() ) {
    00198     QImage img = photo.data();
    00199     QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid();
    00200
    00201     if ( !img.save( fileName, "PNG" ) )
    00202       kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl;
    00203   }
    00204
    00205   if ( logo.isIntern() ) {
    00206     QImage img = logo.data();
    00207     QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid();
    00208
    00209     if ( !img.save( fileName, "PNG" ) )
    00210       kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl;
    00211   }
    00212
    00213   // save sound
    00214   // TODO: save the sound data to file
    00215 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:19 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001