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

kabc

  • kabc
  • plugins
  • file
resourcefile.cpp
1 /*
2  This file is part of libkabc.
3 
4  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (c) 2006 Tom Abers <tomalbers@kde.nl>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include <tqfile.h>
29 #include <tqfileinfo.h>
30 #include <tqtimer.h>
31 
32 #include <kapplication.h>
33 #include <kconfig.h>
34 #include <kdebug.h>
35 #include <kio/scheduler.h>
36 #include <klocale.h>
37 #include <ksavefile.h>
38 #include <kstandarddirs.h>
39 
40 #include "formatfactory.h"
41 #include "resourcefileconfig.h"
42 #include "stdaddressbook.h"
43 #include "lock.h"
44 
45 #include "resourcefile.h"
46 
47 using namespace KABC;
48 
49 ResourceFile::ResourceFile( const KConfig *config )
50  : Resource( config ), mFormat( 0 ),
51  mAsynchronous( false )
52 {
53  TQString fileName, formatName;
54 
55  if ( config ) {
56  fileName = config->readPathEntry( "FileName", StdAddressBook::fileName() );
57  formatName = config->readEntry( "FileFormat", "vcard" );
58  } else {
59  fileName = StdAddressBook::fileName();
60  formatName = "vcard";
61  }
62 
63  init( fileName, formatName );
64 }
65 
66 ResourceFile::ResourceFile( const TQString &fileName,
67  const TQString &formatName )
68  : Resource( 0 ), mFormat( 0 ),
69  mAsynchronous( false )
70 {
71  init( fileName, formatName );
72 }
73 
74 void ResourceFile::init( const TQString &fileName, const TQString &formatName )
75 {
76  mFormatName = formatName;
77 
78  FormatFactory *factory = FormatFactory::self();
79  mFormat = factory->format( mFormatName );
80 
81  if ( !mFormat ) {
82  mFormatName = "vcard";
83  mFormat = factory->format( mFormatName );
84  }
85 
86  connect( &mDirWatch, TQT_SIGNAL( dirty(const TQString&) ), TQT_SLOT( fileChanged() ) );
87  connect( &mDirWatch, TQT_SIGNAL( created(const TQString&) ), TQT_SLOT( fileChanged() ) );
88  connect( &mDirWatch, TQT_SIGNAL( deleted(const TQString&) ), TQT_SLOT( fileChanged() ) );
89 
90  setFileName( fileName );
91 
92  mLock = 0;
93 }
94 
95 ResourceFile::~ResourceFile()
96 {
97  delete mFormat;
98  mFormat = 0;
99 }
100 
101 void ResourceFile::writeConfig( KConfig *config )
102 {
103  Resource::writeConfig( config );
104 
105  if ( mFileName == StdAddressBook::fileName() )
106  config->deleteEntry( "FileName" );
107  else
108  config->writePathEntry( "FileName", mFileName );
109 
110  config->writeEntry( "FileFormat", mFormatName );
111 }
112 
113 Ticket *ResourceFile::requestSaveTicket()
114 {
115  kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
116 
117  if ( !addressBook() ) return 0;
118 
119  delete mLock;
120  mLock = new Lock( mFileName );
121 
122  if ( mLock->lock() ) {
123  addressBook()->emitAddressBookLocked();
124  } else {
125  addressBook()->error( mLock->error() );
126  kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
127  << mFileName << "': " << mLock->error() << endl;
128  return 0;
129  }
130 
131  return createTicket( this );
132 }
133 
134 void ResourceFile::releaseSaveTicket( Ticket *ticket )
135 {
136  delete ticket;
137 
138  delete mLock;
139  mLock = 0;
140 
141  addressBook()->emitAddressBookUnlocked();
142 }
143 
144 bool ResourceFile::doOpen()
145 {
146  TQFile file( mFileName );
147 
148  if ( !file.exists() ) {
149  // try to create the file
150  bool ok = file.open( IO_WriteOnly );
151  if ( ok )
152  file.close();
153 
154  return ok;
155  } else {
156  TQFileInfo fileInfo( mFileName );
157  if ( readOnly() || !fileInfo.isWritable() ) {
158  if ( !file.open( IO_ReadOnly ) )
159  return false;
160  } else {
161  if ( !file.open( IO_ReadWrite ) )
162  return false;
163  }
164 
165  if ( file.size() == 0 ) {
166  file.close();
167  kdDebug() << "File size is zero. Evaluating backups" << endl;
168  for (int i=0; i!=20; i++)
169  {
170  TQFile backup( mFileName + "__" + TQString::number(i) );
171  kdDebug() << "Evaluating" << backup.name() << " size: " << backup.size() << endl;
172  if ( backup.size() != 0 )
173  {
174  kdDebug() << "Restoring backup " << i << endl;
175  const TQString src = mFileName + "__" + TQString::number(i);
176  const TQString dest = mFileName;
177 
178  // remove dest
179  TQFile::remove( dest );
180 
181  // copy src to dest
182  if ( backup.open( IO_ReadOnly ) ) {
183  const TQByteArray data = backup.readAll();
184 
185  TQFile out( dest );
186  if ( out.open( IO_WriteOnly ) ) {
187  out.writeBlock( data );
188  out.close();
189  }
190 
191  backup.close();
192  }
193  return true;
194  }
195  }
196  return true;
197  }
198 
199  bool ok = mFormat->checkFormat( &file );
200  file.close();
201 
202  return ok;
203  }
204 }
205 
206 void ResourceFile::doClose()
207 {
208 }
209 
210 bool ResourceFile::load()
211 {
212  kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl;
213 
214  mAsynchronous = false;
215 
216  TQFile file( mFileName );
217  if ( !file.open( IO_ReadOnly ) ) {
218  addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
219  return false;
220  }
221 
222  clear();
223 
224  return mFormat->loadAll( addressBook(), this, &file );
225 }
226 
227 bool ResourceFile::asyncLoad()
228 {
229  kdDebug(5700) << "ResourceFile::asyncLoad()" << endl;
230 
231  mAsynchronous = true;
232 
233  bool ok = load();
234 
235  if ( !ok )
236  emitLoadingError();
237  else
238  emitLoadingFinished();
239 
240  return true;
241 }
242 
243 bool ResourceFile::save( Ticket * )
244 {
245  kdDebug(5700) << "ResourceFile::save()" << endl;
246 
247  // Only do the logrotate dance when the __0 file is not 0 bytes.
248  TQFile file( mFileName + "__0" );
249  if ( file.size() != 0 ) {
250  const TQString last = mFileName + "__20";
251  kdDebug() << "deleting " << last << endl;
252 
253  TQFile::remove( last );
254 
255  for (int i=19; i>=0; i--)
256  {
257  const TQString src = mFileName + "__" + TQString::number(i);
258  const TQString dest = mFileName + "__" + TQString::number(i+1);
259  kdDebug() << "moving " << src << " -> " << dest << endl;
260 
261  // copy src to dest
262  TQFile in( src );
263  if ( in.open( IO_ReadOnly ) ) {
264  const TQByteArray data = in.readAll();
265 
266  TQFile out( dest );
267  if ( out.open( IO_WriteOnly ) ) {
268  out.writeBlock( data );
269  out.close();
270  }
271 
272  in.close();
273  }
274 
275  // remove src
276  TQFile::remove( src );
277  }
278  } else
279  kdDebug() << "Not starting logrotate __0 is 0 bytes." << endl;
280 
281  TQString extension = "__0";
282  (void) KSaveFile::backupFile( mFileName, TQString::null /*directory*/,
283  extension );
284 
285  mDirWatch.stopScan();
286 
287  KSaveFile saveFile( mFileName );
288  bool ok = false;
289 
290  if ( saveFile.status() == 0 && saveFile.file() ) {
291  mFormat->saveAll( addressBook(), this, saveFile.file() );
292  ok = saveFile.close();
293  }
294 
295  if ( !ok ) {
296  saveFile.abort();
297  addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
298  }
299 
300  mDirWatch.startScan();
301 
302  return ok;
303 }
304 
305 bool ResourceFile::asyncSave( Ticket *ticket )
306 {
307  kdDebug(5700) << "ResourceFile::asyncSave()" << endl;
308 
309  bool ok = save( ticket );
310 
311  if ( !ok )
312  TQTimer::singleShot( 0, this, TQT_SLOT( emitSavingError() ) );
313  else
314  TQTimer::singleShot( 0, this, TQT_SLOT( emitSavingFinished() ) );
315 
316  return ok;
317 }
318 
319 void ResourceFile::setFileName( const TQString &fileName )
320 {
321  mDirWatch.stopScan();
322  if ( mDirWatch.contains( mFileName ) )
323  mDirWatch.removeFile( mFileName );
324 
325  mFileName = fileName;
326 
327  mDirWatch.addFile( mFileName );
328  mDirWatch.startScan();
329 }
330 
331 TQString ResourceFile::fileName() const
332 {
333  return mFileName;
334 }
335 
336 void ResourceFile::setFormat( const TQString &format )
337 {
338  mFormatName = format;
339  delete mFormat;
340 
341  FormatFactory *factory = FormatFactory::self();
342  mFormat = factory->format( mFormatName );
343 }
344 
345 TQString ResourceFile::format() const
346 {
347  return mFormatName;
348 }
349 
350 void ResourceFile::fileChanged()
351 {
352  kdDebug(5700) << "ResourceFile::fileChanged(): " << mFileName << endl;
353 
354  if ( !addressBook() )
355  return;
356 
357  if ( mAsynchronous )
358  asyncLoad();
359  else {
360  load();
361  kdDebug() << "addressBookChanged() " << endl;
362  addressBook()->emitAddressBookChanged();
363  }
364 }
365 
366 void ResourceFile::removeAddressee( const Addressee &addr )
367 {
368  TQFile::remove( TQFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
369  TQFile::remove( TQFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
370  TQFile::remove( TQFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
371 
372  mAddrMap.erase( addr.uid() );
373 }
374 
375 void ResourceFile::emitSavingFinished()
376 {
377  emit savingFinished( this );
378 }
379 
380 void ResourceFile::emitSavingError()
381 {
382  emit savingError( this, i18n( "Unable to save file '%1'." ).arg( mFileName ) );
383 }
384 
385 void ResourceFile::emitLoadingFinished()
386 {
387  emit loadingFinished( this );
388 }
389 
390 void ResourceFile::emitLoadingError()
391 {
392  emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mFileName ) );
393 }
394 
395 #include "resourcefile.moc"

kabc

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

kabc

Skip menu "kabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kabc by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |