• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

kabc

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., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "binaryformat.h"
00022 #include "kabc/addressbook.h"
00023 #include "kabc/addressee.h"
00024 #include "kabc/picture.h"
00025 #include "kabc/sound.h"
00026 
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 #include <kstandarddirs.h>
00030 
00031 #include <QtCore/QDataStream>
00032 #include <QtGui/QImage>
00033 
00034 #define BINARY_FORMAT_VERSION 1
00035 
00036 using namespace KABC;
00037 
00038 extern "C"
00039 {
00040   KDE_EXPORT Format *format()
00041   {
00042     return new BinaryFormat;
00043   }
00044 }
00045 
00046 bool BinaryFormat::load( Addressee &addressee, QFile *file )
00047 {
00048   kDebug();
00049   QDataStream stream( file );
00050 
00051   if ( !checkHeader( stream ) ) {
00052     return false;
00053   }
00054 
00055   loadAddressee( addressee, stream );
00056 
00057   return true;
00058 }
00059 
00060 bool BinaryFormat::loadAll( AddressBook *, Resource *resource, QFile *file )
00061 {
00062   kDebug();
00063 
00064   QDataStream stream( file );
00065 
00066   if ( !checkHeader( stream ) ) {
00067     return false;
00068   }
00069 
00070   quint32 entries;
00071 
00072   stream >> entries;
00073 
00074   for ( uint i = 0; i < entries; ++i ) {
00075     Addressee addressee;
00076     loadAddressee( addressee, stream );
00077     addressee.setResource( resource );
00078     addressee.setChanged( false );
00079     resource->insertAddressee( addressee );
00080   }
00081 
00082   return true;
00083 }
00084 
00085 void BinaryFormat::save( const Addressee &addressee, QFile *file )
00086 {
00087   kDebug();
00088 
00089   QDataStream stream( file );
00090 
00091   writeHeader( stream );
00092 
00093   quint32 entries = 1;
00094   stream << entries;
00095   saveAddressee( addressee, stream );
00096 }
00097 
00098 void BinaryFormat::saveAll( AddressBook *, Resource *resource, QFile *file )
00099 {
00100   kDebug();
00101 
00102   quint32 counter = 0;
00103   QDataStream stream( file );
00104 
00105   writeHeader( stream );
00106   // set dummy number of entries
00107   stream << counter;
00108 
00109   Resource::Iterator it;
00110   for ( it = resource->begin(); it != resource->end(); ++it ) {
00111     saveAddressee( (*it), stream );
00112     counter++;
00113     (*it).setChanged( false );
00114   }
00115 
00116   // set real number of entries
00117   stream.device()->seek( 2 * sizeof( quint32 ) );
00118   stream << counter;
00119 }
00120 
00121 bool BinaryFormat::checkFormat( QFile *file ) const
00122 {
00123   kDebug();
00124 
00125   QDataStream stream( file );
00126 
00127   return checkHeader( stream );
00128 }
00129 
00130 bool BinaryFormat::checkHeader( QDataStream &stream ) const
00131 {
00132   quint32 magic, version;
00133 
00134   stream >> magic >> version;
00135 
00136   QFile *file = dynamic_cast<QFile*>( stream.device() );
00137 
00138   if ( !file ) {
00139     kError() << i18n( "Not a file?" );
00140     return false;
00141   }
00142 
00143   if ( magic != 0x2e93e ) {
00144     kError() << i18n( "File '%1' is not binary format.", file->fileName() );
00145     return false;
00146   }
00147 
00148   if ( version != BINARY_FORMAT_VERSION ) {
00149     kError() << i18n( "File '%1' is the wrong version.", file->fileName() );
00150     return false;
00151   }
00152 
00153   return true;
00154 }
00155 
00156 void BinaryFormat::writeHeader( QDataStream &stream )
00157 {
00158   quint32 magic, version;
00159 
00160   magic = 0x2e93e;
00161   version = BINARY_FORMAT_VERSION;
00162 
00163   stream << magic << version;
00164 }
00165 
00166 void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream )
00167 {
00168   stream >> addressee;
00169 /*
00170   // load pictures
00171   Picture photo = addressee.photo();
00172   Picture logo = addressee.logo();
00173 
00174   if ( photo.isIntern() ) {
00175     QImage img;
00176     if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) )
00177       kDebug() << "No photo available for '" << addressee.uid() << "'.";
00178 
00179     addressee.setPhoto( img );
00180   }
00181 
00182   if ( logo.isIntern() ) {
00183     QImage img;
00184     if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) )
00185       kDebug() << "No logo available for '" << addressee.uid() << "'.";
00186 
00187     addressee.setLogo( img );
00188   }
00189 
00190   // load sound
00191   // TODO: load sound data from file
00192 */
00193 }
00194 
00195 void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream )
00196 {
00197   stream << addressee;
00198 /*
00199   // load pictures
00200   Picture photo = addressee.photo();
00201   Picture logo = addressee.logo();
00202 
00203   if ( photo.isIntern() ) {
00204     QImage img = photo.data();
00205     QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid();
00206 
00207     if ( !img.save( fileName, "PNG" ) )
00208       kDebug() << "Unable to save photo for '" << addressee.uid() << "'.";
00209   }
00210 
00211   if ( logo.isIntern() ) {
00212     QImage img = logo.data();
00213     QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid();
00214 
00215     if ( !img.save( fileName, "PNG" ) )
00216       kDebug() << "Unable to save logo for '" << addressee.uid() << "'.";
00217   }
00218 
00219   // save sound
00220   // TODO: save the sound data to file
00221 */
00222 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:05:39 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

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

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal