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

tdeabc

  • tdeabc
  • plugins
  • file
resourcefile.cpp
1 /*
2  This file is part of libtdeabc.
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 <tdeapplication.h>
33 #include <tdeconfig.h>
34 #include <kdebug.h>
35 #include <tdeio/scheduler.h>
36 #include <tdelocale.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 TDEABC;
48 
49 ResourceFile::ResourceFile( const TDEConfig *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( TDEConfig *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(5700) << "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(5700) << "Evaluating" << backup.name() << " size: " << backup.size() << endl;
172  if ( backup.size() != 0 )
173  {
174  kdDebug(5700) << "Restoring backup " << i << endl;
175  const TQString src = mFileName + "__" + TQString::number(i);
176  const TQString dest = mFileName;
177 
178  // copy src to dest
179  if ( ! backup.open( IO_ReadOnly ) ) {
180 // const TQByteArray data = backup.readAll();
181  kdDebug(5700) << "can not open source for reading " << src << endl;
182  continue;
183  }
184 
185  // remove dest
186  TQFile::remove( dest );
187 
188  TQString text;
189  TQTextStream instream( &backup );
190  instream.setEncoding( TQTextStream::UnicodeUTF8 );
191  text = instream.read();
192  backup.close();
193 
194  TQFile out( dest );
195  if ( ! out.open( IO_WriteOnly ) ) {
196 // out.writeBlock( data );
197  kdDebug(5700) << "can not open target for writing " << dest << endl;
198  continue;
199  }
200  TQTextStream outstream( &out );
201  outstream.setEncoding( TQTextStream::UnicodeUTF8 );
202  outstream << text;
203  out.close();
204 
205  return true;
206  }
207  }
208  return true;
209  }
210 
211  bool ok = mFormat->checkFormat( &file );
212  file.close();
213 
214  return ok;
215  }
216 }
217 
218 void ResourceFile::doClose()
219 {
220 }
221 
222 bool ResourceFile::load()
223 {
224  kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl;
225 
226  mAsynchronous = false;
227 
228  TQFile file( mFileName );
229  if ( !file.open( IO_ReadOnly ) ) {
230  addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
231  return false;
232  }
233 
234  clear();
235 
236  return mFormat->loadAll( addressBook(), this, &file );
237 }
238 
239 bool ResourceFile::asyncLoad()
240 {
241  kdDebug(5700) << "ResourceFile::asyncLoad()" << endl;
242 
243  mAsynchronous = true;
244 
245  bool ok = load();
246 
247  if ( !ok )
248  emitLoadingError();
249  else
250  emitLoadingFinished();
251 
252  return true;
253 }
254 
255 bool ResourceFile::save( Ticket * )
256 {
257  kdDebug(5700) << "ResourceFile::save()" << endl;
258 
259  // Only do the logrotate dance when the __0 file is not 0 bytes.
260  TQFile file( mFileName + "__0" );
261  if ( file.size() != 0 ) {
262  const TQString last = mFileName + "__20";
263  kdDebug(5700) << "deleting " << last << endl;
264 
265  TQFile::remove( last );
266 
267  for (int i=19; i>=0; i--)
268  {
269  const TQString src = mFileName + "__" + TQString::number(i);
270  const TQString dest = mFileName + "__" + TQString::number(i+1);
271  kdDebug() << "moving " << src << " -> " << dest << endl;
272 
273  // copy src to dest
274  TQFile in( src );
275  if ( ! in.exists() )
276  continue;
277  if ( ! in.open( IO_ReadOnly ) ) {
278 // const TQByteArray data = in.readAll();
279  kdDebug(5700) << "can not open source for reading " << src << endl;
280  return false;
281  }
282  TQString text;
283  TQTextStream instream( &in );
284 
285  instream.setEncoding( TQTextStream::UnicodeUTF8 );
286  text = instream.read();
287  in.close();
288 
289  TQFile out( dest );
290  if ( ! out.open( IO_WriteOnly ) ) {
291 // out.writeBlock( data );
292  kdDebug(5700) << "can not open target for writing " << dest << endl;
293  return false;
294  }
295  TQTextStream outstream( &out );
296  outstream.setEncoding( TQTextStream::UnicodeUTF8 );
297  outstream << text;
298  out.close();
299 
300  // remove src
301  TQFile::remove( src );
302  }
303  } else
304  kdDebug(5700) << "Not starting logrotate __0 is 0 bytes." << endl;
305 
306  TQString extension = "__0";
307  (void) KSaveFile::backupFile( mFileName, TQString::null /*directory*/,
308  extension );
309  mDirWatch.stopScan();
310 
311  KSaveFile saveFile( mFileName );
312  bool ok = false;
313 
314  if ( saveFile.status() == 0 && saveFile.file() ) {
315  mFormat->saveAll( addressBook(), this, saveFile.file() );
316  ok = saveFile.close();
317  }
318 
319  if ( !ok ) {
320  saveFile.abort();
321  addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
322  }
323 
324  mDirWatch.startScan();
325 
326  return ok;
327 }
328 
329 bool ResourceFile::asyncSave( Ticket *ticket )
330 {
331  kdDebug(5700) << "ResourceFile::asyncSave()" << endl;
332 
333  bool ok = save( ticket );
334 
335  if ( !ok )
336  TQTimer::singleShot( 0, this, TQT_SLOT( emitSavingError() ) );
337  else
338  TQTimer::singleShot( 0, this, TQT_SLOT( emitSavingFinished() ) );
339 
340  return ok;
341 }
342 
343 void ResourceFile::setFileName( const TQString &fileName )
344 {
345  mDirWatch.stopScan();
346  if ( mDirWatch.contains( mFileName ) )
347  mDirWatch.removeFile( mFileName );
348 
349  mFileName = fileName;
350 
351  mDirWatch.addFile( mFileName );
352  mDirWatch.startScan();
353 }
354 
355 TQString ResourceFile::fileName() const
356 {
357  return mFileName;
358 }
359 
360 void ResourceFile::setFormat( const TQString &format )
361 {
362  mFormatName = format;
363  delete mFormat;
364 
365  FormatFactory *factory = FormatFactory::self();
366  mFormat = factory->format( mFormatName );
367 }
368 
369 TQString ResourceFile::format() const
370 {
371  return mFormatName;
372 }
373 
374 void ResourceFile::fileChanged()
375 {
376  kdDebug(5700) << "ResourceFile::fileChanged(): " << mFileName << endl;
377 
378  if ( !addressBook() )
379  return;
380 
381  if ( mAsynchronous )
382  asyncLoad();
383  else {
384  load();
385  kdDebug() << "addressBookChanged() " << endl;
386  addressBook()->emitAddressBookChanged();
387  }
388 }
389 
390 void ResourceFile::removeAddressee( const Addressee &addr )
391 {
392  TQFile::remove( TQFile::encodeName( locateLocal( "data", "tdeabc/photos/" ) + addr.uid() ) );
393  TQFile::remove( TQFile::encodeName( locateLocal( "data", "tdeabc/logos/" ) + addr.uid() ) );
394  TQFile::remove( TQFile::encodeName( locateLocal( "data", "tdeabc/sounds/" ) + addr.uid() ) );
395 
396  mAddrMap.erase( addr.uid() );
397 }
398 
399 void ResourceFile::emitSavingFinished()
400 {
401  emit savingFinished( this );
402 }
403 
404 void ResourceFile::emitSavingError()
405 {
406  emit savingError( this, i18n( "Unable to save file '%1'." ).arg( mFileName ) );
407 }
408 
409 void ResourceFile::emitLoadingFinished()
410 {
411  emit loadingFinished( this );
412 }
413 
414 void ResourceFile::emitLoadingError()
415 {
416  emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mFileName ) );
417 }
418 
419 #include "resourcefile.moc"
TDEConfig
TDEABC::FormatFactory
Class for loading format plugins.
Definition: formatfactory.h:57
KSaveFile::status
int status() const
TDEABC::ResourceFile::setFormat
void setFormat(const TQString &name)
Sets a new format by name.
Definition: resourcefile.cpp:360
locateLocal
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
TDEABC::ResourceFile::writeConfig
virtual void writeConfig(TDEConfig *cfg)
Writes the config back.
Definition: resourcefile.cpp:101
TDEABC::ResourceFile::~ResourceFile
~ResourceFile()
Destructor.
Definition: resourcefile.cpp:95
TDEABC::StdAddressBook::fileName
static TQString fileName()
Returns the default file name for vcard-based addressbook.
Definition: stdaddressbook.cpp:43
KSaveFile
TDEABC::Lock
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
Definition: lock.h:35
kdDebug
kdbgstream kdDebug(int area=0)
TDEABC::FormatFactory::self
static FormatFactory * self()
Returns the global format factory.
Definition: formatfactory.cpp:38
KSaveFile::abort
void abort()
TDEABC::ResourceFile::requestSaveTicket
virtual Ticket * requestSaveTicket()
Requests a save ticket, that is used by save()
Definition: resourcefile.cpp:113
TDEABC::ResourceFile::removeAddressee
virtual void removeAddressee(const Addressee &addr)
Remove a addressee from its source.
Definition: resourcefile.cpp:390
KSaveFile::close
bool close()
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEABC::FormatPlugin::checkFormat
virtual bool checkFormat(TQFile *file) const =0
Checks if given file contains the right format.
tdelocale.h
TDEABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:37
TDEABC::Addressee::uid
TQString uid() const
Return unique identifier.
Definition: addressee.src.cpp:174
TDEABC::ResourceFile::load
virtual bool load()
Loads all addressees from file to the address book.
Definition: resourcefile.cpp:222
TDEABC::ResourceFile::format
TQString format() const
Returns the format name.
Definition: resourcefile.cpp:369
TDEABC::Lock::lock
virtual bool lock()
Lock resource.
Definition: lock.cpp:81
TDEABC::ResourceFile::doOpen
virtual bool doOpen()
Tries to open the file and checks for the proper format.
Definition: resourcefile.cpp:144
TDEABC::Addressee
address book entry
Definition: addressee.src.h:74
TDEABC::ResourceFile::doClose
virtual void doClose()
Closes the file again.
Definition: resourcefile.cpp:218
TDEABC::FormatPlugin::loadAll
virtual bool loadAll(AddressBook *, Resource *, TQFile *file)=0
Load whole addressbook from file.
TDEABC::ResourceFile::ResourceFile
ResourceFile(const TDEConfig *cfg)
Constructor.
Definition: resourcefile.cpp:49
TDEABC::ResourceFile::fileName
TQString fileName() const
Return name of file used for loading and saving the address book.
Definition: resourcefile.cpp:355
KSaveFile::file
TQFile * file()
KSaveFile::backupFile
static bool backupFile(const TQString &filename, const TQString &backupDir=TQString::null, const TQString &backupExtension=TQString::fromLatin1("~"))
endl
kndbgstream & endl(kndbgstream &s)
TDEABC::FormatPlugin::saveAll
virtual void saveAll(AddressBook *, Resource *, TQFile *file)=0
Save whole addressbook to file.
TDEABC::ResourceFile::save
virtual bool save(Ticket *ticket)
Saves all addresses from address book to file.
Definition: resourcefile.cpp:255
TDEABC::ResourceFile::setFileName
void setFileName(const TQString &)
Set name of file to be used for saving.
Definition: resourcefile.cpp:343
TDEABC::FormatFactory::format
FormatPlugin * format(const TQString &type)
Returns a pointer to a format object or a null pointer if format type doesn't exist.
Definition: formatfactory.cpp:109

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.8.8
This website is maintained by Timothy Pearson.