kabc Library API Documentation

resourcenet.cpp

00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include <qfile.h> 00022 00023 #include <kdebug.h> 00024 #include <kio/netaccess.h> 00025 #include <kio/scheduler.h> 00026 #include <klocale.h> 00027 #include <ktempfile.h> 00028 #include <kurlrequester.h> 00029 00030 #include "addressbook.h" 00031 #include "formatfactory.h" 00032 #include "resourcenetconfig.h" 00033 #include "stdaddressbook.h" 00034 00035 #include "resourcenet.h" 00036 00037 using namespace KABC; 00038 00039 class ResourceNet::ResourceNetPrivate 00040 { 00041 public: 00042 KIO::Job *mLoadJob; 00043 bool mIsLoading; 00044 00045 KIO::Job *mSaveJob; 00046 bool mIsSaving; 00047 }; 00048 00049 ResourceNet::ResourceNet( const KConfig *config ) 00050 : Resource( config ), mFormat( 0 ), 00051 mLocalTempFile( 0 ), mUseLocalTempFile( false ), 00052 d( new ResourceNetPrivate ) 00053 { 00054 if ( config ) { 00055 init( KURL( config->readPathEntry( "NetUrl" ) ), config->readEntry( "NetFormat" ) ); 00056 } else { 00057 init( KURL(), "vcard" ); 00058 } 00059 } 00060 00061 ResourceNet::ResourceNet( const KURL &url, const QString &format ) 00062 : Resource( 0 ), mFormat( 0 ), 00063 mLocalTempFile( 0 ), mUseLocalTempFile( false ), 00064 d( new ResourceNetPrivate ) 00065 { 00066 init( url, format ); 00067 } 00068 00069 void ResourceNet::init( const KURL &url, const QString &format ) 00070 { 00071 d->mLoadJob = 0; 00072 d->mIsLoading = false; 00073 d->mSaveJob = 0; 00074 d->mIsSaving = false; 00075 00076 mFormatName = format; 00077 00078 FormatFactory *factory = FormatFactory::self(); 00079 mFormat = factory->format( mFormatName ); 00080 if ( !mFormat ) { 00081 mFormatName = "vcard"; 00082 mFormat = factory->format( mFormatName ); 00083 } 00084 00085 setUrl( url ); 00086 } 00087 00088 ResourceNet::~ResourceNet() 00089 { 00090 if ( d->mIsLoading ) 00091 d->mLoadJob->kill(); 00092 if ( d->mIsSaving ) 00093 d->mSaveJob->kill(); 00094 00095 delete d; 00096 d = 0; 00097 00098 delete mFormat; 00099 mFormat = 0; 00100 00101 delete mLocalTempFile; 00102 mLocalTempFile = 0; 00103 } 00104 00105 void ResourceNet::writeConfig( KConfig *config ) 00106 { 00107 Resource::writeConfig( config ); 00108 00109 config->writePathEntry( "NetUrl", mUrl.url() ); 00110 config->writeEntry( "NetFormat", mFormatName ); 00111 } 00112 00113 Ticket *ResourceNet::requestSaveTicket() 00114 { 00115 kdDebug(5700) << "ResourceNet::requestSaveTicket()" << endl; 00116 00117 if ( mTempFile.isEmpty() ) 00118 return 0; 00119 00120 return createTicket( this ); 00121 } 00122 00123 void ResourceNet::releaseSaveTicket( Ticket *ticket ) 00124 { 00125 KIO::NetAccess::removeTempFile( mTempFile ); 00126 delete ticket; 00127 } 00128 00129 bool ResourceNet::doOpen() 00130 { 00131 return true; 00132 } 00133 00134 void ResourceNet::doClose() 00135 { 00136 } 00137 00138 bool ResourceNet::load() 00139 { 00140 if ( !KIO::NetAccess::exists( mUrl, true, 0 ) ) { 00141 mLocalTempFile = new KTempFile(); 00142 mLocalTempFile->setAutoDelete( true ); 00143 mUseLocalTempFile = true; 00144 mTempFile = mLocalTempFile->name(); 00145 } 00146 00147 if ( !KIO::NetAccess::download( mUrl, mTempFile, 0 ) ) { 00148 addressBook()->error( i18n( "Unable to download file '%1'." ).arg( mUrl.url() ) ); 00149 return false; 00150 } 00151 00152 QFile file( mTempFile ); 00153 if ( !file.open( IO_ReadOnly ) ) { 00154 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mUrl.url() ) ); 00155 return false; 00156 } 00157 00158 return mFormat->loadAll( addressBook(), this, &file ); 00159 } 00160 00161 bool ResourceNet::asyncLoad() 00162 { 00163 if ( mLocalTempFile ) { 00164 kdDebug(5700) << "stale temp file detected " << mLocalTempFile->name() << endl; 00165 mLocalTempFile->setAutoDelete( true ); 00166 delete mLocalTempFile; 00167 } 00168 00169 mLocalTempFile = new KTempFile(); 00170 mUseLocalTempFile = true; 00171 mTempFile = mLocalTempFile->name(); 00172 00173 KURL dest; 00174 dest.setPath( mTempFile ); 00175 00176 KIO::Scheduler::checkSlaveOnHold( true ); 00177 d->mLoadJob = KIO::file_copy( mUrl, dest, -1, true, false, false ); 00178 d->mIsLoading = true; 00179 connect( d->mLoadJob, SIGNAL( result( KIO::Job* ) ), 00180 this, SLOT( downloadFinished( KIO::Job* ) ) ); 00181 00182 return true; 00183 } 00184 00185 bool ResourceNet::save( Ticket* ) 00186 { 00187 QFile file( mTempFile ); 00188 00189 if ( !file.open( IO_WriteOnly ) ) { 00190 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mUrl.url() ) ); 00191 return false; 00192 } 00193 00194 mFormat->saveAll( addressBook(), this, &file ); 00195 file.close(); 00196 00197 return KIO::NetAccess::upload( mTempFile, mUrl, 0 ); 00198 } 00199 00200 bool ResourceNet::asyncSave( Ticket* ) 00201 { 00202 QFile file( mTempFile ); 00203 00204 if ( !file.open( IO_WriteOnly ) ) { 00205 emit savingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) ); 00206 return false; 00207 } 00208 00209 mFormat->saveAll( addressBook(), this, &file ); 00210 file.close(); 00211 00212 KURL src; 00213 src.setPath( mTempFile ); 00214 00215 KIO::Scheduler::checkSlaveOnHold( true ); 00216 d->mSaveJob = KIO::file_copy( src, mUrl, -1, true, false, false ); 00217 d->mIsSaving = true; 00218 connect( d->mSaveJob, SIGNAL( result( KIO::Job* ) ), 00219 this, SLOT( uploadFinished( KIO::Job* ) ) ); 00220 00221 return true; 00222 } 00223 00224 void ResourceNet::setUrl( const KURL &url ) 00225 { 00226 mUrl = url; 00227 } 00228 00229 KURL ResourceNet::url() const 00230 { 00231 return mUrl; 00232 } 00233 00234 void ResourceNet::setFormat( const QString &name ) 00235 { 00236 mFormatName = name; 00237 if ( mFormat ) 00238 delete mFormat; 00239 00240 FormatFactory *factory = FormatFactory::self(); 00241 mFormat = factory->format( mFormatName ); 00242 } 00243 00244 QString ResourceNet::format() const 00245 { 00246 return mFormatName; 00247 } 00248 00249 void ResourceNet::downloadFinished( KIO::Job* ) 00250 { 00251 d->mIsLoading = false; 00252 00253 if ( !mLocalTempFile ) 00254 emit loadingError( this, i18n( "Download failed in some way!" ) ); 00255 00256 QFile file( mTempFile ); 00257 if ( !file.open( IO_ReadOnly ) ) { 00258 emit loadingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) ); 00259 return; 00260 } 00261 00262 if ( !mFormat->loadAll( addressBook(), this, &file ) ) 00263 emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mTempFile ) ); 00264 else 00265 emit loadingFinished( this ); 00266 } 00267 00268 void ResourceNet::uploadFinished( KIO::Job *job ) 00269 { 00270 d->mIsSaving = false; 00271 00272 if ( job->error() ) 00273 emit savingError( this, job->errorString() ); 00274 else 00275 emit savingFinished( this ); 00276 } 00277 00278 #include "resourcenet.moc"
KDE Logo
This file is part of the documentation for kabc Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 14 00:35:18 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003