resourcenet.cpp
00001 #include <qfile.h>
00002
00003 #include <kdebug.h>
00004 #include <kio/netaccess.h>
00005 #include <klocale.h>
00006 #include <ktempfile.h>
00007 #include <kurlrequester.h>
00008
00009 #include "addressbook.h"
00010 #include "formatfactory.h"
00011 #include "resourcenetconfig.h"
00012 #include "stdaddressbook.h"
00013
00014 #include "resourcenet.h"
00015
00016 using namespace KABC;
00017
00018 extern "C"
00019 {
00020 ResourceConfigWidget *config_widget( QWidget *parent ) {
00021 KGlobal::locale()->insertCatalogue("kabc_net");
00022 return new ResourceNetConfig( parent, "ResourceDirConfig" );
00023 }
00024
00025 Resource *resource( AddressBook *ab, const KConfig *config ) {
00026 KGlobal::locale()->insertCatalogue("kabc_net");
00027 return new ResourceNet( ab, config );
00028 }
00029 }
00030
00031
00032 ResourceNet::ResourceNet( AddressBook *addressBook, const KConfig *config )
00033 : Resource( addressBook )
00034 {
00035 KURL url = config->readPathEntry( "NetUrl" );
00036 QString type = config->readEntry( "NetFormat" );
00037
00038 FormatFactory *factory = FormatFactory::self();
00039 FormatPlugin *format = factory->format( type );
00040
00041 init( url, format );
00042 }
00043
00044 ResourceNet::ResourceNet( AddressBook *addressBook, const KURL &url,
00045 FormatPlugin *format ) :
00046 Resource( addressBook )
00047 {
00048 init( url, format );
00049 }
00050
00051 ResourceNet::~ResourceNet()
00052 {
00053 delete mFormat;
00054 }
00055
00056 void ResourceNet::init( const KURL &url, FormatPlugin *format )
00057 {
00058 if ( !format ) {
00059 FormatFactory *factory = FormatFactory::self();
00060 mFormat = factory->format( "vcard" );
00061 } else {
00062 mFormat = format;
00063 }
00064
00065 setUrl( url );
00066 }
00067
00068 Ticket *ResourceNet::requestSaveTicket()
00069 {
00070 kdDebug(5700) << "ResourceNet::requestSaveTicket()" << endl;
00071
00072 if ( !addressBook() )
00073 return 0;
00074
00075 return createTicket( this );
00076 }
00077
00078
00079 bool ResourceNet::open()
00080 {
00081 KIO::UDSEntryList entries;
00082 if ( !KIO::NetAccess::listDir( mUrl, entries ) )
00083 return false;
00084
00085 return true;
00086 }
00087
00088 void ResourceNet::close()
00089 {
00090 }
00091
00092 bool ResourceNet::load()
00093 {
00094 kdDebug(5700) << "ResourceNet::load(): '" << mUrl.url() << "'" << endl;
00095
00096 KIO::UDSEntryList entries;
00097 if ( !KIO::NetAccess::listDir( mUrl, entries, false, false ) )
00098 return false;
00099
00100 QStringList files = KIO::convertUDSEntryListToFileNames( entries );
00101
00102 QStringList::Iterator it;
00103 bool ok = true;
00104 for ( it = files.begin(); it != files.end(); ++it ) {
00105 if ( (*it).endsWith( "/" ) )
00106 continue;
00107
00108 QString tmpFile;
00109 if ( KIO::NetAccess::download( mUrl.url() + "/" + (*it), tmpFile ) ) {
00110 QFile file( tmpFile );
00111 if ( !file.open( IO_ReadOnly ) ) {
00112 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
00113 ok = false;
00114 } else {
00115 if ( !mFormat->loadAll( addressBook(), this, &file ) )
00116 ok = false;
00117 }
00118
00119 KIO::NetAccess::removeTempFile( tmpFile );
00120 } else {
00121 addressBook()->error( i18n( "Unable to open URL '%1' for reading" ).arg( mUrl.url() + "/" + (*it) ) );
00122 ok = false;
00123 }
00124 }
00125
00126 return ok;
00127 }
00128
00129 bool ResourceNet::save( Ticket *ticket )
00130 {
00131 kdDebug(5700) << "ResourceNet::save(): '" << mUrl.url() << "'" << endl;
00132
00133 AddressBook::Iterator it;
00134 bool ok = true;
00135
00136 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
00137 if ( (*it).resource() != this || !(*it).changed() )
00138 continue;
00139
00140 KTempFile tmpFile;
00141 QFile *file = tmpFile.file();
00142
00143 mFormat->save( *it, file );
00144
00145
00146 (*it).setChanged( false );
00147
00148 tmpFile.close();
00149
00150 if ( !KIO::NetAccess::upload( tmpFile.name(), mUrl.url() + "/" + (*it).uid() ) ) {
00151 addressBook()->error( i18n( "Unable to save URL '%1'" ).arg( mUrl.url() + "/" + (*it).uid() ) );
00152 ok = false;
00153 }
00154
00155 tmpFile.unlink();
00156 }
00157
00158 delete ticket;
00159
00160 return ok;
00161 }
00162
00163 void ResourceNet::setUrl( const KURL &url )
00164 {
00165 mUrl = url;
00166 }
00167
00168 KURL ResourceNet::url() const
00169 {
00170 return mUrl;
00171 }
00172
00173 QString ResourceNet::identifier() const
00174 {
00175 return url().url();
00176 }
00177
00178 void ResourceNet::removeAddressee( const Addressee& addr )
00179 {
00180 KIO::NetAccess::del( mUrl.url() + "/" + addr.uid() );
00181 }
00182
00183 #include "resourcenet.moc"
This file is part of the documentation for kdelibs Version 3.1.5.