-rw-r--r-- | kabc/formats/binaryformat.cpp | 236 | ||||
-rw-r--r-- | kabc/formats/binaryformat.h | 76 | ||||
-rw-r--r-- | kabc/formats/vcardformatplugin2.cpp | 123 | ||||
-rw-r--r-- | kabc/formats/vcardformatplugin2.h | 68 |
4 files changed, 503 insertions, 0 deletions
diff --git a/kabc/formats/binaryformat.cpp b/kabc/formats/binaryformat.cpp new file mode 100644 index 0000000..e2f28b8 --- a/dev/null +++ b/kabc/formats/binaryformat.cpp @@ -0,0 +1,236 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + + +#include <qdatastream.h> +#include <qimage.h> + +#include <kdebug.h> +#include <klocale.h> +#include <kstandarddirs.h> + +#include "addressbook.h" +#include "addressee.h" +#include "picture.h" +#include "sound.h" + +#include "binaryformat.h" + +#define BINARY_FORMAT_VERSION 2 + +using namespace KABC; + +/*US +extern "C" +{ + FormatPlugin *format() + { + qDebug(" BinaryFormat::format = new BinaryFormat"); + return new BinaryFormat; + } +} +*/ +bool BinaryFormat::load( Addressee &addressee, QFile *file ) +{ + kdDebug(5700) << "BinaryFormat::load()" << endl; + QDataStream stream( file ); + + if ( !checkHeader( stream ) ) + return false; + + loadAddressee( addressee, stream ); + + return true; +} + +bool BinaryFormat::loadAll( AddressBook *addressBook, Resource *resource, QFile *file ) +{ + kdDebug(5700) << "BinaryFormat::loadAll()" << endl; + + QDataStream stream( file ); + + if ( !checkHeader( stream ) ) + return false; + + Q_UINT32 entries; + + stream >> entries; + + for ( uint i = 0; i < entries; ++i ) { + Addressee addressee; + loadAddressee( addressee, stream ); + addressee.setResource( resource ); + addressBook->insertAddressee( addressee ); + } + + return true; +} + +void BinaryFormat::save( const Addressee &addressee, QFile *file ) +{ + kdDebug(5700) << "BinaryFormat::save()" << endl; + + QDataStream stream( file ); + + writeHeader( stream ); + + Q_UINT32 entries = 1; + stream << entries; + saveAddressee( addressee, stream ); +} + +void BinaryFormat::saveAll( AddressBook *ab, Resource *resource, QFile *file ) +{ + kdDebug(5700) << "BinaryFormat::saveAll()" << endl; + + Q_UINT32 counter = 0; + QDataStream stream( file ); + + writeHeader( stream ); + // set dummy number of entries + stream << counter; + + AddressBook::Iterator it; + for ( it = ab->begin(); it != ab->end(); ++it ) { + if ( (*it).resource() == resource ) { + saveAddressee( (*it), stream ); + counter++; + (*it).setChanged( false ); + } + } + + // set real number of entries + stream.device()->at( 2 * sizeof( Q_UINT32 ) ); + stream << counter; +} + +bool BinaryFormat::checkFormat( QFile *file ) const +{ + kdDebug(5700) << "BinaryFormat::checkFormat()" << endl; + + QDataStream stream( file ); + + return checkHeader( stream ); +} + +bool BinaryFormat::checkHeader( QDataStream &stream ) const +{ + Q_UINT32 magic, version; + + stream >> magic >> version; + +//US QFile *file = dynamic_cast<QFile*>( stream.device() ); + QFile *file = (QFile*)( stream.device() ); + + if ( !file ) { + qDebug("BinaryFormat::checkHeader : Not a file?"); + kdError() << i18n("Not a file?") << endl; + return false; + } + + if ( magic != 0x2e93e ) { + qDebug("BinaryFormat::checkHeader : File '%s' is not binary format.", file->name().latin1()); + kdError() << i18n("File '%1' is not binary format.").arg( file->name() ) << endl; + return false; + } + + if ( version != BINARY_FORMAT_VERSION ) { + qDebug("BinaryFormat::checkHeader : File '%s' is the wrong version.", file->name().latin1()); + kdError() << i18n("File '%1' is the wrong version.").arg( file->name() ) << endl; + return false; + } + + return true; +} + +void BinaryFormat::writeHeader( QDataStream &stream ) +{ + Q_UINT32 magic, version; + + magic = 0x2e93e; + version = BINARY_FORMAT_VERSION; + + stream << magic << version; +} + +void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream ) +{ + stream >> addressee; +/* + // load pictures + Picture photo = addressee.photo(); + Picture logo = addressee.logo(); + + if ( photo.isIntern() ) { + QImage img; + if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) ) + kdDebug(5700) << "No photo available for '" << addressee.uid() << "'." << endl; + + addressee.setPhoto( img ); + } + + if ( logo.isIntern() ) { + QImage img; + if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) ) + kdDebug(5700) << "No logo available for '" << addressee.uid() << "'." << endl; + + addressee.setLogo( img ); + } + + // load sound + // TODO: load sound data from file +*/ +} + +void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream ) +{ + stream << addressee; +/* + // load pictures + Picture photo = addressee.photo(); + Picture logo = addressee.logo(); + + if ( photo.isIntern() ) { + QImage img = photo.data(); + QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid(); + + if ( !img.save( fileName, "PNG" ) ) + kdDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'." << endl; + } + + if ( logo.isIntern() ) { + QImage img = logo.data(); + QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid(); + + if ( !img.save( fileName, "PNG" ) ) + kdDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'." << endl; + } + + // save sound + // TODO: save the sound data to file +*/ +} diff --git a/kabc/formats/binaryformat.h b/kabc/formats/binaryformat.h new file mode 100644 index 0000000..415fd3b --- a/dev/null +++ b/kabc/formats/binaryformat.h @@ -0,0 +1,76 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef KABC_BINARYFORMAT_H +#define KABC_BINARYFORMAT_H + +#include "formatplugin.h" + +namespace KABC { + +class AddressBook; +class Addressee; + +/** + @short binary file format for addressbook entries. +*/ +class BinaryFormat : public FormatPlugin +{ +public: + /** + * Load single addressee from file. + */ + bool load( Addressee &, QFile *file ); + + /** + * Load whole addressee from file. + */ + bool loadAll( AddressBook *, Resource *, QFile *file ); + + /** + * Save single addressee to file. + */ + void save( const Addressee &, QFile *file ); + + /** + * Save all addressees to file. + */ + void saveAll( AddressBook *, Resource *, QFile *file ); + + /** + * Check for valid format of a file. + */ + bool checkFormat( QFile *file ) const; + +private: + void loadAddressee( Addressee &, QDataStream & ); + void saveAddressee( const Addressee &, QDataStream & ); + bool checkHeader( QDataStream & ) const; + void writeHeader( QDataStream & ); +}; + +} +#endif diff --git a/kabc/formats/vcardformatplugin2.cpp b/kabc/formats/vcardformatplugin2.cpp new file mode 100644 index 0000000..2a772b4 --- a/dev/null +++ b/kabc/formats/vcardformatplugin2.cpp @@ -0,0 +1,123 @@ +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include "vcardformatplugin2.h" + +#include "address.h" +#include "addressee.h" +#include "vcardparser/vcardtool.h" + +#include <qtextstream.h> +#include <qfile.h> + +using namespace KABC; + +/*US +extern "C" +{ + FormatPlugin *format() + { + qDebug(" VCardFormatPlugin2::format = new VCardFormatPlugin2"); + return new VCardFormatPlugin2(); + } +} +*/ + +VCardFormatPlugin2::VCardFormatPlugin2() +{ +} + +VCardFormatPlugin2::~VCardFormatPlugin2() +{ +} + +bool VCardFormatPlugin2::load( Addressee &addressee, QFile *file ) +{ + QString data; + + QTextStream t( file ); + t.setEncoding( QTextStream::UnicodeUTF8 ); + data = t.read(); + + VCardTool tool; + + Addressee::List l = tool.parseVCards( data ); + + if ( ! l.first().isEmpty() ) { + addressee = l.first(); + return true; + } + + return false; +} + +bool VCardFormatPlugin2::loadAll( AddressBook *addressBook, Resource *resource, QFile *file ) +{ + QString data; + + QTextStream t( file ); + t.setEncoding( QTextStream::UnicodeUTF8 ); + data = t.read(); + + VCardTool tool; + + Addressee::List l = tool.parseVCards( data ); + + Addressee::List::Iterator itr; + + for ( itr = l.begin(); itr != l.end(); ++itr) { + Addressee addressee = *itr; + addressee.setResource( resource ); + addressBook->insertAddressee( addressee ); + } + + return true; +} + +void VCardFormatPlugin2::save( const Addressee &addressee, QFile *file ) +{ + VCardTool tool; + Addressee::List vcardlist; + + + vcardlist.append( addressee ); + + QTextStream t( file ); + t.setEncoding( QTextStream::UnicodeUTF8 ); + t << tool.createVCards( vcardlist ); +} + +void VCardFormatPlugin2::saveAll( AddressBook *ab, Resource *resource, QFile *file ) +{ + VCardTool tool; + Addressee::List vcardlist; + + AddressBook::Iterator it; + for ( it = ab->begin(); it != ab->end(); ++it ) { + if ( (*it).resource() == resource ) { + (*it).setChanged( false ); + vcardlist.append( *it ); + } + } + + QTextStream t( file ); + t.setEncoding( QTextStream::UnicodeUTF8 ); + t << tool.createVCards( vcardlist ); +} + +bool VCardFormatPlugin2::checkFormat( QFile *file ) const +{ + QString line; + + file->readLine( line, 1024 ); + line = line.stripWhiteSpace(); + if ( line == "BEGIN:VCARD" ) + return true; + else + return false; +} + diff --git a/kabc/formats/vcardformatplugin2.h b/kabc/formats/vcardformatplugin2.h new file mode 100644 index 0000000..585ab6b --- a/dev/null +++ b/kabc/formats/vcardformatplugin2.h @@ -0,0 +1,68 @@ +/* + This file is part of libkabc. + Copyright (c) 2003 Zack Rusin <zack@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef VCARDFORMATPLUGIN2_H +#define VCARDFORMATPLUGIN2_H + +#include "formatplugin.h" +#include "addressee.h" + +class QFile; + +namespace KABC { + + class Resource; + class AddressBook; + /** + @short Interface of vCard backend for address book. + + This class implements the file format interface of address book entries for + the vCard format. + */ + class VCardFormatPlugin2 : public FormatPlugin + { + public: + VCardFormatPlugin2(); + virtual ~VCardFormatPlugin2(); + + bool load( Addressee &, QFile *file ); + bool loadAll( AddressBook *, Resource *, QFile *file ); + void save( const Addressee &, QFile *file ); + void saveAll( AddressBook *, Resource *, QFile *file ); + + bool checkFormat( QFile *file ) const; + + private: + struct VCardFormatPrivate; + VCardFormatPrivate *d; + }; + + +} + + +#endif |