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

tdeabc

  • tdeabc
  • plugins
  • dir
resourcedir.cpp
1 /*
2  This file is part of libtdeabc.
3  Copyright (c) 2002 - 2003 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 <errno.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 #include <tqregexp.h>
28 #include <tqtimer.h>
29 #include <tqwidget.h>
30 
31 #include <tdeapplication.h>
32 #include <tdeconfig.h>
33 #include <kdebug.h>
34 #include <kgenericfactory.h>
35 #include <tdeglobal.h>
36 #include <tdelocale.h>
37 #include <kstandarddirs.h>
38 #include <kurlrequester.h>
39 
40 #include "addressbook.h"
41 #include "formatfactory.h"
42 #include "resourcedirconfig.h"
43 #include "stdaddressbook.h"
44 #include "lock.h"
45 
46 #include "resourcedir.h"
47 
48 using namespace TDEABC;
49 
50 extern "C"
51 {
52  void *init_tdeabc_dir()
53  {
54  return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>();
55  }
56 }
57 
58 
59 ResourceDir::ResourceDir( const TDEConfig *config )
60  : Resource( config ), mAsynchronous( false )
61 {
62  if ( config ) {
63  init( config->readPathEntry( "FilePath", StdAddressBook::directoryName() ),
64  config->readEntry( "FileFormat", "vcard" ) );
65  } else {
66  init( StdAddressBook::directoryName(), "vcard" );
67  }
68 }
69 
70 ResourceDir::ResourceDir( const TQString &path, const TQString &format )
71  : Resource( 0 ), mAsynchronous( false )
72 {
73  init( path, format );
74 }
75 
76 void ResourceDir::init( const TQString &path, const TQString &format )
77 {
78  mFormatName = format;
79 
80  FormatFactory *factory = FormatFactory::self();
81  mFormat = factory->format( mFormatName );
82 
83  if ( !mFormat ) {
84  mFormatName = "vcard";
85  mFormat = factory->format( mFormatName );
86  }
87 
88  mLock = 0;
89 
90  connect( &mDirWatch, TQ_SIGNAL( dirty(const TQString&) ), TQ_SLOT( pathChanged() ) );
91  connect( &mDirWatch, TQ_SIGNAL( created(const TQString&) ), TQ_SLOT( pathChanged() ) );
92  connect( &mDirWatch, TQ_SIGNAL( deleted(const TQString&) ), TQ_SLOT( pathChanged() ) );
93 
94  setPath( path );
95 }
96 
97 ResourceDir::~ResourceDir()
98 {
99  delete mFormat;
100  mFormat = 0;
101 }
102 
103 void ResourceDir::writeConfig( TDEConfig *config )
104 {
105  Resource::writeConfig( config );
106 
107  if ( mPath == StdAddressBook::directoryName() )
108  config->deleteEntry( "FilePath" );
109  else
110  config->writePathEntry( "FilePath", mPath );
111 
112  config->writeEntry( "FileFormat", mFormatName );
113 }
114 
115 Ticket *ResourceDir::requestSaveTicket()
116 {
117  kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl;
118 
119  if ( !addressBook() ) return 0;
120 
121  delete mLock;
122  mLock = new Lock( mPath );
123 
124  if ( mLock->lock() ) {
125  addressBook()->emitAddressBookLocked();
126  } else {
127  addressBook()->error( mLock->error() );
128  kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock path '"
129  << mPath << "': " << mLock->error() << endl;
130  return 0;
131  }
132 
133  return createTicket( this );
134 }
135 
136 void ResourceDir::releaseSaveTicket( Ticket *ticket )
137 {
138  delete ticket;
139 
140  delete mLock;
141  mLock = 0;
142 }
143 
144 bool ResourceDir::doOpen()
145 {
146  TQDir dir( mPath );
147  if ( !dir.exists() ) { // no directory available
148  return dir.mkdir( dir.path() );
149  } else {
150  TQString testName = dir.entryList( TQDir::Files )[0];
151  if ( testName.isNull() || testName.isEmpty() ) // no file in directory
152  return true;
153 
154  TQFile file( mPath + "/" + testName );
155  if ( file.open( IO_ReadOnly ) )
156  return true;
157 
158  if ( file.size() == 0 )
159  return true;
160 
161  bool ok = mFormat->checkFormat( &file );
162  file.close();
163  return ok;
164  }
165 }
166 
167 void ResourceDir::doClose()
168 {
169 }
170 
171 bool ResourceDir::load()
172 {
173  kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl;
174 
175  mAsynchronous = false;
176 
177  TQDir dir( mPath );
178  TQStringList files = dir.entryList( TQDir::Files );
179 
180  TQStringList::Iterator it;
181  bool ok = true;
182  for ( it = files.begin(); it != files.end(); ++it ) {
183  TQFile file( mPath + "/" + (*it) );
184 
185  if ( !file.open( IO_ReadOnly ) ) {
186  addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
187  ok = false;
188  continue;
189  }
190 
191  if ( !mFormat->loadAll( addressBook(), this, &file ) )
192  ok = false;
193 
194  file.close();
195  }
196 
197  return ok;
198 }
199 
200 bool ResourceDir::asyncLoad()
201 {
202  mAsynchronous = true;
203 
204  bool ok = load();
205  if ( !ok )
206  emit loadingError( this, i18n( "Loading resource '%1' failed!" )
207  .arg( resourceName() ) );
208  else
209  emit loadingFinished( this );
210 
211  return ok;
212 }
213 
214 bool ResourceDir::save( Ticket * )
215 {
216  kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
217 
218  Addressee::Map::Iterator it;
219  bool ok = true;
220 
221  mDirWatch.stopScan();
222 
223  for ( it = mAddrMap.begin(); it != mAddrMap.end(); ++it ) {
224  if ( !it.data().changed() )
225  continue;
226 
227  TQFile file( mPath + "/" + (*it).uid() );
228  if ( !file.open( IO_WriteOnly ) ) {
229  addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
230  continue;
231  }
232 
233  mFormat->save( *it, &file );
234 
235  // mark as unchanged
236  (*it).setChanged( false );
237 
238  file.close();
239  }
240 
241  mDirWatch.startScan();
242 
243  return ok;
244 }
245 
246 bool ResourceDir::asyncSave( Ticket *ticket )
247 {
248  bool ok = save( ticket );
249  if ( !ok )
250  emit savingError( this, i18n( "Saving resource '%1' failed!" )
251  .arg( resourceName() ) );
252  else
253  emit savingFinished( this );
254 
255  return ok;
256 }
257 
258 void ResourceDir::setPath( const TQString &path )
259 {
260  mDirWatch.stopScan();
261  if ( mDirWatch.contains( mPath ) )
262  mDirWatch.removeDir( mPath );
263 
264  mPath = path;
265  mDirWatch.addDir( mPath, true );
266  mDirWatch.startScan();
267 }
268 
269 TQString ResourceDir::path() const
270 {
271  return mPath;
272 }
273 
274 void ResourceDir::setFormat( const TQString &format )
275 {
276  mFormatName = format;
277 
278  if ( mFormat )
279  delete mFormat;
280 
281  FormatFactory *factory = FormatFactory::self();
282  mFormat = factory->format( mFormatName );
283 }
284 
285 TQString ResourceDir::format() const
286 {
287  return mFormatName;
288 }
289 
290 void ResourceDir::pathChanged()
291 {
292  if ( !addressBook() )
293  return;
294 
295  clear();
296  if ( mAsynchronous )
297  asyncLoad();
298  else {
299  load();
300  addressBook()->emitAddressBookChanged();
301  }
302 }
303 
304 void ResourceDir::removeAddressee( const Addressee& addr )
305 {
306  TQFile::remove( mPath + "/" + addr.uid() );
307  mAddrMap.erase( addr.uid() );
308 }
309 
310 #include "resourcedir.moc"
TDEABC::Addressee
address book entry
Definition: addressee.src.h:75
TDEABC::Addressee::uid
TQString uid() const
Return unique identifier.
Definition: addressee.src.cpp:174
TDEABC::FormatFactory
Class for loading format plugins.
Definition: formatfactory.h:58
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::Lock
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
Definition: lock.h:36
TDEABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:38
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfigBase::writePathEntry
void writePathEntry(const TQString &pKey, const TQString &path, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::deleteEntry
void deleteEntry(const TQString &pKey, bool bNLS=false, bool bGlobal=false)
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::readPathEntry
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfig
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
KStdAction::clear
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEStdAccel::save
const TDEShortcut & save()
tdelocale.h

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