binaryformat.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
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   
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   
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 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 }
00193 
00194 void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream )
00195 {
00196   stream << addressee;
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 
00213 
00214 
00215 
00216 
00217 
00218 
00219 
00220 
00221 }
 
This file is part of the documentation for kabc Library Version 3.2.0.