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

kabc

  • sources
  • kde-4.12
  • kdepimlibs
  • kabc
  • formats
binaryformat.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "binaryformat.h"
22 #include "kabc/addressbook.h"
23 #include "kabc/addressee.h"
24 #include "kabc/picture.h"
25 #include "kabc/sound.h"
26 
27 #include <kdebug.h>
28 #include <klocalizedstring.h>
29 #include <kstandarddirs.h>
30 
31 #include <QtCore/QDataStream>
32 #include <QImage>
33 
34 #define BINARY_FORMAT_VERSION 1
35 
36 using namespace KABC;
37 
38 extern "C"
39 {
40  KDE_EXPORT Format *format()
41  {
42  return new BinaryFormat;
43  }
44 }
45 
46 bool BinaryFormat::load( Addressee &addressee, QFile *file )
47 {
48  kDebug();
49  QDataStream stream( file );
50 
51  if ( !checkHeader( stream ) ) {
52  return false;
53  }
54 
55  loadAddressee( addressee, stream );
56 
57  return true;
58 }
59 
60 bool BinaryFormat::loadAll( AddressBook *, Resource *resource, QFile *file )
61 {
62  kDebug();
63 
64  QDataStream stream( file );
65 
66  if ( !checkHeader( stream ) ) {
67  return false;
68  }
69 
70  quint32 entries;
71 
72  stream >> entries;
73 
74  for ( uint i = 0; i < entries; ++i ) {
75  Addressee addressee;
76  loadAddressee( addressee, stream );
77  addressee.setResource( resource );
78  addressee.setChanged( false );
79  resource->insertAddressee( addressee );
80  }
81 
82  return true;
83 }
84 
85 void BinaryFormat::save( const Addressee &addressee, QFile *file )
86 {
87  kDebug();
88 
89  QDataStream stream( file );
90 
91  writeHeader( stream );
92 
93  quint32 entries = 1;
94  stream << entries;
95  saveAddressee( addressee, stream );
96 }
97 
98 void BinaryFormat::saveAll( AddressBook *, Resource *resource, QFile *file )
99 {
100  kDebug();
101 
102  quint32 counter = 0;
103  QDataStream stream( file );
104 
105  writeHeader( stream );
106  // set dummy number of entries
107  stream << counter;
108 
109  Resource::Iterator it;
110  for ( it = resource->begin(); it != resource->end(); ++it ) {
111  saveAddressee( ( *it ), stream );
112  counter++;
113  ( *it ).setChanged( false );
114  }
115 
116  // set real number of entries
117  stream.device()->seek( 2 * sizeof( quint32 ) );
118  stream << counter;
119 }
120 
121 bool BinaryFormat::checkFormat( QFile *file ) const
122 {
123  kDebug();
124 
125  QDataStream stream( file );
126 
127  return checkHeader( stream );
128 }
129 
130 bool BinaryFormat::checkHeader( QDataStream &stream ) const
131 {
132  quint32 magic, version;
133 
134  stream >> magic >> version;
135 
136  QFile *file = dynamic_cast<QFile*>( stream.device() );
137 
138  if ( !file ) {
139  kError() << i18n( "Not a file?" );
140  return false;
141  }
142 
143  if ( magic != 0x2e93e ) {
144  kError() << i18n( "File '%1' is not binary format.", file->fileName() );
145  return false;
146  }
147 
148  if ( version != BINARY_FORMAT_VERSION ) {
149  kError() << i18n( "File '%1' is the wrong version.", file->fileName() );
150  return false;
151  }
152 
153  return true;
154 }
155 
156 void BinaryFormat::writeHeader( QDataStream &stream )
157 {
158  quint32 magic, version;
159 
160  magic = 0x2e93e;
161  version = BINARY_FORMAT_VERSION;
162 
163  stream << magic << version;
164 }
165 
166 void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream )
167 {
168  stream >> addressee;
169 /*
170  // load pictures
171  Picture photo = addressee.photo();
172  Picture logo = addressee.logo();
173 
174  if ( photo.isIntern() ) {
175  QImage img;
176  if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) )
177  kDebug() << "No photo available for '" << addressee.uid() << "'.";
178 
179  addressee.setPhoto( img );
180  }
181 
182  if ( logo.isIntern() ) {
183  QImage img;
184  if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) )
185  kDebug() << "No logo available for '" << addressee.uid() << "'.";
186 
187  addressee.setLogo( img );
188  }
189 
190  // load sound
191  // TODO: load sound data from file
192 */
193 }
194 
195 void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream )
196 {
197  stream << addressee;
198 /*
199  // load pictures
200  Picture photo = addressee.photo();
201  Picture logo = addressee.logo();
202 
203  if ( photo.isIntern() ) {
204  QImage img = photo.data();
205  QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid();
206 
207  if ( !img.save( fileName, "PNG" ) )
208  kDebug() << "Unable to save photo for '" << addressee.uid() << "'.";
209  }
210 
211  if ( logo.isIntern() ) {
212  QImage img = logo.data();
213  QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid();
214 
215  if ( !img.save( fileName, "PNG" ) )
216  kDebug() << "Unable to save logo for '" << addressee.uid() << "'.";
217  }
218 
219  // save sound
220  // TODO: save the sound data to file
221 */
222 }
KABC::Addressee::setResource
void setResource(Resource *resource)
Set resource where the addressee is from.
Definition: addressee.cpp:1844
KABC::Resource::insertAddressee
virtual void insertAddressee(const Addressee &addr)
Insert an addressee into the resource.
Definition: resource.cpp:284
KABC::Addressee::setChanged
void setChanged(bool value)
Mark addressee as changed.
Definition: addressee.cpp:1855
KABC::Resource::end
virtual ConstIterator end() const
Returns an iterator pointing to the last addressee in the resource.
Definition: resource.cpp:257
KABC::BinaryFormat
binary file format for addressbook entries.
Definition: binaryformat.h:33
KABC::BinaryFormat::save
void save(const Addressee &, QFile *file)
Save single addressee to file.
Definition: binaryformat.cpp:85
KABC::BinaryFormat::load
bool load(Addressee &, QFile *file)
Load single addressee from file.
Definition: binaryformat.cpp:46
KABC::BinaryFormat::checkFormat
bool checkFormat(QFile *file) const
Check for valid format of a file.
Definition: binaryformat.cpp:121
KABC::BinaryFormat::loadAll
bool loadAll(AddressBook *, Resource *, QFile *file)
Load whole addressee from file.
Definition: binaryformat.cpp:60
KABC::Resource::begin
virtual ConstIterator begin() const
Returns an iterator pointing to the first addressee in the resource.
Definition: resource.cpp:242
KABC::Addressee
address book entry
Definition: addressee.h:74
KABC::Resource
Definition: resource.h:64
KABC::AddressBook
Address Book.
Definition: addressbook.h:46
KABC::Format
Base class for address book formats.
Definition: format.h:42
KABC::BinaryFormat::saveAll
void saveAll(AddressBook *, Resource *, QFile *file)
Save all addressees to file.
Definition: binaryformat.cpp:98
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:01:05 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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