# kabc Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# resourceimap.cpp

    00001 /*
    00002     This file is part of libkabc and/or kaddressbook.
    00003     Copyright (c) 2002 Klarälvdalens Datakonsult AB
    00004         <info@klaralvdalens-datakonsult.se>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public License
    00017     along with this library; see the file COPYING.LIB.  If not, write to
    00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019     Boston, MA 02111-1307, USA.
    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   // Ensure that there is a kmail running
    00057   return ( kapp->startServiceByDesktopName( "kmail" ) == 0 );
    00058 }
    00059
    00060 void ResourceIMAP::close()
    00061 {
    00062   // Nothing to close
    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   // For loading, send a DCOP call off to KMail
    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   // Important; we need the synchronous call, even though we don't
    00100   // expect a return value.
    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   // Now parse the vCards in that file
    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   // FormatPlugin only supports loading from a file, not from
    00130   // memory, so we have to write to a temp file first. This is all
    00131   // very uncool, but that's the price for reusing the vCard
    00132   // parser. In the future, the FormatPlugin interface needs
    00133   // changing big time.
    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; // No need to continue in this case.
    00149
    00150   Q_ASSERT( returnType == "bool" );
    00151   QDataStream argIn( returnData, IO_ReadOnly );
    00152   bool ok;
    00153   argIn >> ok;
    00154   tempFile.unlink();
    00155
    00156   // Always try to unlock
    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   // We don't have anything to clean up here.
    00181 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:21 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001