resourceimap.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resourceimap.h"
00023 #include <kglobal.h>
00024 #include <klocale.h>
00025 #include <kapp.h>
00026 #include <dcopclient.h>
00027 #include <kabc/formatfactory.h>
00028 #include <ktempfile.h>
00029 #include <kdebug.h>
00030
00031 using namespace KABC;
00032
00033 extern "C"
00034 {
00035 Resource *resource( AddressBook *ab, const KConfig* )
00036 {
00037 KGlobal::locale()->insertCatalogue( "kabc_imap" );
00038 return new ResourceIMAP( ab );
00039 }
00040 }
00041
00042 ResourceIMAP::ResourceIMAP( AddressBook *ab )
00043 : Resource( ab )
00044 {
00045 FormatFactory *factory = FormatFactory::self();
00046 mFormat = factory->format( "vcard" );
00047 }
00048
00049 ResourceIMAP::~ResourceIMAP()
00050 {
00051 delete mFormat;
00052 }
00053
00054 bool ResourceIMAP::open()
00055 {
00056
00057 return ( kapp->startServiceByDesktopName( "kmail" ) == 0 );
00058 }
00059
00060 void ResourceIMAP::close()
00061 {
00062
00063 }
00064
00065 Ticket * ResourceIMAP::requestSaveTicket()
00066 {
00067 DCOPClient* dcopClient = kapp->dcopClient();
00068 QByteArray returnData;
00069 QCString returnType;
00070 if ( !dcopClient->call( "kmail", "KMailIface", "lockContactsFolder()",
00071 QByteArray(), returnType, returnData, true ) ) {
00072 return false;
00073 }
00074
00075 Q_ASSERT( returnType == "bool" );
00076 QDataStream argIn( returnData, IO_ReadOnly );
00077 bool ok;
00078 argIn >> ok;
00079
00080 if ( !ok )
00081 return 0;
00082 else
00083 return createTicket( this );
00084 }
00085
00086 bool ResourceIMAP::load()
00087 {
00088 kdDebug(5700) << "ResourceIMAP::load()" << endl;
00089
00090 KTempFile tempFile( QString::null, ".vcf" );
00091
00092 DCOPClient* dcopClient = kapp->dcopClient();
00093 QByteArray outgoingData;
00094 QDataStream outgoingStream( outgoingData, IO_WriteOnly );
00095 outgoingStream << tempFile.name();
00096 QByteArray returnData;
00097 QCString returnType;
00098
00099
00100
00101 if ( !dcopClient->call( "kmail", "KMailIface",
00102 "requestAddresses(QString)", outgoingData,
00103 returnType, returnData, true ) ) {
00104 kdDebug(5700) << "DCOP call failed" << endl;
00105 return false;
00106 }
00107
00108
00109 QFile file( tempFile.name() );
00110 if ( !file.open( IO_ReadOnly ) ) {
00111 kdDebug(5700) << "Could not open temp file " << tempFile.name() << endl;
00112 return false;
00113 }
00114
00115 kdDebug(5700) << "Opened temp file " << tempFile.name() << endl;
00116
00117 mFormat->loadAll( addressBook(), this, &file );
00118
00119 tempFile.unlink();
00120
00121 Resource::load();
00122
00123 return true;
00124 }
00125
00126
00127 bool ResourceIMAP::save( Ticket *ticket )
00128 {
00129
00130
00131
00132
00133
00134 KTempFile tempFile( QString::null, ".vcf" );
00135 mFormat->saveAll( addressBook(), this, tempFile.file() );
00136 tempFile.close();
00137
00138 DCOPClient* dcopClient = kapp->dcopClient();
00139 QCString returnType;
00140 QByteArray returnData;
00141 QByteArray paramData;
00142 QDataStream paramStream( paramData, IO_WriteOnly );
00143 paramStream << tempFile.name();
00144 paramStream << removedUIDs();
00145 if ( !dcopClient->call( "kmail", "KMailIface",
00146 "storeAddresses(QString,QStringList)", paramData,
00147 returnType, returnData, true ) )
00148 return false;
00149
00150 Q_ASSERT( returnType == "bool" );
00151 QDataStream argIn( returnData, IO_ReadOnly );
00152 bool ok;
00153 argIn >> ok;
00154 tempFile.unlink();
00155
00156
00157 if ( !dcopClient->call( "kmail", "KMailIface",
00158 "unlockContactsFolder()", QByteArray(),
00159 returnType, returnData, true ) ) {
00160 return false;
00161 }
00162
00163 Q_ASSERT( returnType == "bool" );
00164 QDataStream argIn2( returnData, IO_ReadOnly );
00165 bool ok2;
00166 argIn2 >> ok2;
00167
00168 Resource::save( ticket );
00169
00170 return ( ok2 && ok );
00171 }
00172
00173 QString ResourceIMAP::identifier() const
00174 {
00175 return "KMAIL-IMAP";
00176 }
00177
00178 void ResourceIMAP::cleanUp()
00179 {
00180
00181 }
This file is part of the documentation for kdelibs Version 3.1.5.