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

tdeabc

binaryformat.cpp

00001 /*
00002     This file is part of libtdeabc.
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 <tqdatastream.h>
00022 #include <tqimage.h>
00023 
00024 #include <kdebug.h>
00025 #include <tdelocale.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 TDEABC;
00038 
00039 extern "C"
00040 {
00041   KDE_EXPORT FormatPlugin *format()
00042   {
00043     return new BinaryFormat;
00044   }
00045 }
00046 
00047 bool BinaryFormat::load( Addressee &addressee, TQFile *file )
00048 {
00049   kdDebug(5700) << "BinaryFormat::load()" << endl;
00050   TQDataStream 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, TQFile *file )
00061 {
00062   kdDebug(5700) << "BinaryFormat::loadAll()" << endl;
00063 
00064   TQDataStream stream( file );
00065 
00066   if ( !checkHeader( stream ) )
00067     return false;
00068 
00069   TQ_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, TQFile *file )
00085 {
00086   kdDebug(5700) << "BinaryFormat::save()" << endl;
00087 
00088   TQDataStream stream( file );
00089 
00090   writeHeader( stream );
00091 
00092   TQ_UINT32 entries = 1;
00093   stream << entries;
00094   saveAddressee( addressee, stream );
00095 }
00096 
00097 void BinaryFormat::saveAll( AddressBook*, Resource *resource, TQFile *file )
00098 {
00099   kdDebug(5700) << "BinaryFormat::saveAll()" << endl;
00100 
00101   TQ_UINT32 counter = 0;
00102   TQDataStream 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( TQ_UINT32 ) );
00117   stream << counter;
00118 }
00119 
00120 bool BinaryFormat::checkFormat( TQFile *file ) const
00121 {
00122   kdDebug(5700) << "BinaryFormat::checkFormat()" << endl;
00123 
00124   TQDataStream stream( file );
00125 
00126   return checkHeader( stream );
00127 }
00128 
00129 bool BinaryFormat::checkHeader( TQDataStream &stream ) const
00130 {
00131   TQ_UINT32 magic, version;
00132     
00133   stream >> magic >> version;
00134 
00135   TQFile *file = dynamic_cast<TQFile*>( stream.device() );
00136 
00137   if ( !file ) {
00138     kdError() << i18n("Not a file?") << endl;
00139     return false;
00140   }
00141 
00142   if ( magic != 0x2e93e ) {
00143     kdError() << TQString(i18n("File '%1' is not binary format.").arg( file->name() )) << endl;
00144     return false;
00145   }
00146 
00147   if ( version != BINARY_FORMAT_VERSION ) {
00148     kdError() << TQString(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( TQDataStream &stream )
00156 {
00157   TQ_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, TQDataStream &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     TQImage img;
00175     if ( !img.load( locateLocal( "data", "tdeabc/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     TQImage img;
00183     if ( !img.load( locateLocal( "data", "tdeabc/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, TQDataStream &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     TQImage img = photo.data();
00204     TQString fileName = locateLocal( "data", "tdeabc/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     TQImage img = logo.data();
00212     TQString fileName = locateLocal( "data", "tdeabc/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 }

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.6.3
This website is maintained by Timothy Pearson.