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 ResourceNet::ResourceNet( const KConfig *config )
00040   : Resource( config ), mFormat( 0 ),
00041     mLocalTempFile( 0 ), mUseLocalTempFile( false )
00042 {
00043   if ( config ) {
00044     init( config->readPathEntry( "NetUrl" ), config->readEntry( "NetFormat" ) );
00045   } else {
00046     init( KURL(), "vcard" );
00047   }
00048 }
00049 
00050 ResourceNet::ResourceNet( const KURL &url, const QString &format )
00051   : Resource( 0 ), mFormat( 0 ),
00052     mLocalTempFile( 0 ), mUseLocalTempFile( false )
00053 {
00054   init( url, format );
00055 }
00056 
00057 void ResourceNet::init( const KURL &url, const QString &format )
00058 {
00059   mFormatName = format;
00060 
00061   FormatFactory *factory = FormatFactory::self();
00062   mFormat = factory->format( mFormatName );
00063   if ( !mFormat ) {
00064     mFormatName = "vcard";
00065     mFormat = factory->format( mFormatName );
00066   }
00067 
00068   setUrl( url );
00069 }
00070 
00071 ResourceNet::~ResourceNet()
00072 {
00073   delete mFormat;
00074   mFormat = 0;
00075 
00076   delete mLocalTempFile;
00077   mLocalTempFile = 0;
00078 }
00079 
00080 void ResourceNet::writeConfig( KConfig *config )
00081 {
00082   Resource::writeConfig( config );
00083 
00084   config->writePathEntry( "NetUrl", mUrl.url() );
00085   config->writeEntry( "NetFormat", mFormatName );
00086 }
00087 
00088 Ticket *ResourceNet::requestSaveTicket()
00089 {
00090   kdDebug(5700) << "ResourceNet::requestSaveTicket()" << endl;
00091 
00092   if ( mTempFile.isEmpty() )
00093     return 0;
00094 
00095   return createTicket( this );
00096 }
00097 
00098 void ResourceNet::releaseSaveTicket( Ticket *ticket )
00099 {
00100   KIO::NetAccess::removeTempFile( mTempFile );
00101   delete ticket;
00102 }
00103 
00104 bool ResourceNet::doOpen()
00105 {
00106   return true;
00107 }
00108 
00109 void ResourceNet::doClose()
00110 {
00111 }
00112 
00113 bool ResourceNet::load()
00114 {
00115   if ( !KIO::NetAccess::exists( mUrl, true, 0 ) ) {
00116     mLocalTempFile = new KTempFile();
00117     mLocalTempFile->setAutoDelete( true );
00118     mUseLocalTempFile = true;
00119     mTempFile = mLocalTempFile->name();
00120   }
00121 
00122   if ( !KIO::NetAccess::download( mUrl, mTempFile, 0 ) ) {
00123     addressBook()->error( i18n( "Unable to download file '%1'." ).arg( mUrl.url() ) );
00124     return false;
00125   }
00126 
00127   QFile file( mTempFile );
00128   if ( !file.open( IO_ReadOnly ) ) {
00129     addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mUrl.url() ) );
00130     return false;
00131   }
00132 
00133   return mFormat->loadAll( addressBook(), this, &file );
00134 }
00135 
00136 bool ResourceNet::asyncLoad()
00137 {
00138   if ( mLocalTempFile ) {
00139     kdDebug(5700) << "stale temp file dedected " << mLocalTempFile->name() << endl;
00140     mLocalTempFile->setAutoDelete( true );
00141     delete mLocalTempFile;
00142   }
00143 
00144   mLocalTempFile = new KTempFile();
00145   mUseLocalTempFile = true;
00146   mTempFile = mLocalTempFile->name();
00147 
00148   KURL dest;
00149   dest.setPath( mTempFile );
00150 
00151   KIO::Scheduler::checkSlaveOnHold( true );
00152   KIO::Job * job = KIO::file_copy( mUrl, dest, -1, true, false, false );
00153   connect( job, SIGNAL( result( KIO::Job* ) ),
00154            this, SLOT( downloadFinished( KIO::Job* ) ) );
00155 
00156   return true;
00157 }
00158 
00159 bool ResourceNet::save( Ticket* )
00160 {
00161   QFile file( mTempFile );
00162 
00163   if ( !file.open( IO_WriteOnly ) ) {
00164     addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mUrl.url() ) );
00165     return false;
00166   }
00167   
00168   mFormat->saveAll( addressBook(), this, &file );
00169   file.close();
00170 
00171   return KIO::NetAccess::upload( mTempFile, mUrl, 0 );
00172 }
00173 
00174 bool ResourceNet::asyncSave( Ticket* )
00175 {
00176   QFile file( mTempFile );
00177 
00178   if ( !file.open( IO_WriteOnly ) ) {
00179     emit savingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) );
00180     return false;
00181   }
00182   
00183   mFormat->saveAll( addressBook(), this, &file );
00184   file.close();
00185 
00186   KURL src;
00187   src.setPath( mTempFile );
00188 
00189   KIO::Scheduler::checkSlaveOnHold( true );
00190   KIO::Job * job = KIO::file_copy( src, mUrl, -1, true, false, false );
00191   connect( job, SIGNAL( result( KIO::Job* ) ),
00192            this, SLOT( uploadFinished( KIO::Job* ) ) );
00193 
00194   return true;
00195 }
00196 
00197 void ResourceNet::setUrl( const KURL &url )
00198 {
00199   mUrl = url;
00200 }
00201 
00202 KURL ResourceNet::url() const
00203 {
00204   return mUrl;
00205 }
00206 
00207 void ResourceNet::setFormat( const QString &name )
00208 {
00209   mFormatName = name;
00210   if ( mFormat )
00211     delete mFormat;
00212   
00213   FormatFactory *factory = FormatFactory::self();
00214   mFormat = factory->format( mFormatName );
00215 }
00216 
00217 QString ResourceNet::format() const
00218 {
00219   return mFormatName;
00220 }
00221 
00222 void ResourceNet::downloadFinished( KIO::Job* )
00223 {
00224   if ( !mLocalTempFile )
00225     emit loadingError( this, i18n( "Download failed in some way!" ) );
00226 
00227   QFile file( mTempFile );
00228   if ( !file.open( IO_ReadOnly ) ) {
00229     emit loadingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) );
00230     return;
00231   }
00232 
00233   if ( !mFormat->loadAll( addressBook(), this, &file ) )
00234     emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mTempFile ) );
00235   else
00236     emit loadingFinished( this );
00237 }
00238 
00239 void ResourceNet::uploadFinished( KIO::Job *job )
00240 {
00241   if ( job->error() )
00242     emit savingError( this, job->errorString() );
00243   else
00244     emit savingFinished( this );
00245 }
00246 
00247 #include "resourcenet.moc"
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:05:37 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003