kabc Library: binaryformat.cpp Source File (kabc Library)

| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | 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*, 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     addressee.setChanged( false );
    00078     resource->insertAddressee( addressee );
    00079   }
    00080
    00081   return true;
    00082 }
    00083
    00084 void BinaryFormat::save( const Addressee &addressee, QFile *file )
    00085 {
    00086   kdDebug(5700) << "BinaryFormat::save()" << endl;
    00087
    00088   QDataStream stream( file );
    00089
    00090   writeHeader( stream );
    00091
    00092   Q_UINT32 entries = 1;
    00093   stream << entries;
    00094   saveAddressee( addressee, stream );
    00095 }
    00096
    00097 void BinaryFormat::saveAll( AddressBook*, Resource *resource, QFile *file )
    00098 {
    00099   kdDebug(5700) << "BinaryFormat::saveAll()" << endl;
    00100
    00101   Q_UINT32 counter = 0;
    00102   QDataStream stream( file );
    00103
    00104   writeHeader( stream );
    00105   // set dummy number of entries
    00106   stream << counter;
    00107
    00108   Resource::Iterator it;
    00109   for ( it = resource->begin(); it != resource->end(); ++it ) {
    00110     saveAddressee( (*it), stream );
    00111     counter++;
    00112     (*it).setChanged( false );
    00113   }
    00114
    00115   // set real number of entries
    00116   stream.device()->at( 2 * sizeof( Q_UINT32 ) );
    00117   stream << counter;
    00118 }
    00119
    00120 bool BinaryFormat::checkFormat( QFile *file ) const
    00121 {
    00122   kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
    00123
    00124   QDataStream stream( file );
    00125
    00126   return checkHeader( stream );
    00127 }
    00128
    00129 bool BinaryFormat::checkHeader( QDataStream &stream ) const
    00130 {
    00131   Q_UINT32 magic, version;
    00132
    00133   stream >> magic >> version;
    00134
    00135   QFile *file = dynamic_cast<QFile*>( stream.device() );
    00136
    00137   if ( !file ) {
    00138     kdError() << i18n("Not a file?") << endl;
    00139     return false;
    00140   }
    00141
    00142   if ( magic != 0x2e93e ) {
    00143     kdError() << i18n("File '%1' is not binary format.").arg( file->name() ) << endl;
    00144     return false;
    00145   }
    00146
    00147   if ( version != BINARY_FORMAT_VERSION ) {
    00148     kdError() << i18n("File '%1' is the wrong version.").arg( file->name() ) << endl;
    00149     return false;
    00150   }
    00151
    00152   return true;
    00153 }
    00154
    00155 void BinaryFormat::writeHeader( QDataStream &stream )
    00156 {
    00157   Q_UINT32 magic, version;
    00158
    00159   magic = 0x2e93e;
    00160   version = BINARY_FORMAT_VERSION;
    00161
    00162   stream << magic << version;
    00163 }
    00164
    00165 void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream )
    00166 {
    00167   stream >> addressee;
    00168 /*
    00169   // load pictures
    00170   Picture photo = addressee.photo();
    00171   Picture logo = addressee.logo();
    00172
    00173   if ( photo.isIntern() ) {
    00174     QImage img;
    00175     if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) )
    00176       kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl;
    00177
    00178     addressee.setPhoto( img );
    00179   }
    00180
    00181   if ( logo.isIntern() ) {
    00182     QImage img;
    00183     if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) )
    00184       kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl;
    00185
    00186     addressee.setLogo( img );
    00187   }
    00188
    00189   // load sound
    00190   // TODO: load sound data from file
    00191 */
    00192 }
    00193
    00194 void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream )
    00195 {
    00196   stream << addressee;
    00197 /*
    00198   // load pictures
    00199   Picture photo = addressee.photo();
    00200   Picture logo = addressee.logo();
    00201
    00202   if ( photo.isIntern() ) {
    00203     QImage img = photo.data();
    00204     QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid();
    00205
    00206     if ( !img.save( fileName, "PNG" ) )
    00207       kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl;
    00208   }
    00209
    00210   if ( logo.isIntern() ) {
    00211     QImage img = logo.data();
    00212     QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid();
    00213
    00214     if ( !img.save( fileName, "PNG" ) )
    00215       kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl;
    00216   }
    00217
    00218   // save sound
    00219   // TODO: save the sound data to file
    00220 */
    00221 }

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:29 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003