resourceevo.cpp

00001 #include <qdir.h>
00002 
00003 #include <kglobal.h>
00004 #include <klocale.h>
00005 #include <kdebug.h>
00006 
00007 #include <stdio.h>
00008 
00009 #include <kabc/vcardparser/vcardtool.h>
00010 
00011 #include "dbwrapper.h"
00012 #include "resourceevo.h"
00013 
00014 using namespace Evolution;
00015 using namespace KABC;
00016 
00017 class EvolutionFactory : public KRES::PluginFactoryBase
00018 {
00019   public:
00020     KRES::Resource *resource( const KConfig *config )
00021     {
00022       return new ResourceEvolution( config );
00023     }
00024 
00025     KRES::ConfigWidget *configWidget( QWidget * )
00026     {
00027       return 0;
00028     }
00029 };
00030 
00031 extern "C"
00032 {
00033   KDE_EXPORT void *init_kabc_evo()
00034   {
00035     return ( new EvolutionFactory() );
00036   }
00037 }
00038 
00039 ResourceEvolution::ResourceEvolution( const KConfig* conf )
00040     : Resource( conf ), mWrap(0l)
00041 {
00042     m_isOpen = false;
00043 }
00044 ResourceEvolution::~ResourceEvolution() {
00045     delete mWrap;
00046 }
00047 bool ResourceEvolution::doOpen() {
00048     mWrap = new DBWrapper;
00049     if (!mWrap->open( QDir::homeDirPath() + "/evolution/local/Contacts/addressbook.db" ) ) {
00050         return false;
00051     }
00052 
00053     QString val;
00054     if (!mWrap->find( "PAS-DB-VERSION", val ) )
00055         return false;
00056 
00057     if (!val.startsWith("0.2") )
00058         return false;
00059 
00060     m_isOpen = true;
00061 
00062     return true;
00063 }
00064 void ResourceEvolution::doClose() {
00065     delete mWrap;
00066     mWrap = 0l;
00067     m_isOpen = false;
00068 }
00069 Ticket* ResourceEvolution::requestSaveTicket() {
00070     if ( !addressBook() ) return 0;
00071     return createTicket( this );
00072 }
00073 /*
00074  * skip the first key
00075  */
00076 
00077 bool ResourceEvolution::load() {
00078     /* doOpen never get's called :( */
00079     if (!doOpen()) return false;
00080     if (!mWrap ) return false; // open first!
00081 
00082     DBIterator it = mWrap->begin();
00083     // skip the "PAS-DB-VERSION"
00084 
00085     for ( ; it != mWrap->end(); ++it ) {
00086         if ( it.key().startsWith("PAS-DB-VERSION") )
00087             continue;
00088 
00089         qWarning( "val:%s", it.value().latin1() );
00090         VCardTool tool;
00091         QString str = it.value().stripWhiteSpace();
00092         Addressee::List list = tool.parseVCards( str );
00093         if (!list.first().isEmpty() ) {
00094             Addressee adr = list.first();
00095             adr.setResource(this);
00096             addressBook()->insertAddressee( adr );
00097         }
00098     }
00099     return true;
00100 }
00101 bool ResourceEvolution::save( Ticket* ticket ) {
00102     delete ticket;
00103     if (!m_isOpen ) return false;
00104 
00105     // just delete the summary so evolution will regenerate it 
00106     // on next start up
00107     (void)QFile::remove( QDir::homeDirPath() + "/evolution/local/Contacts/addressbook.db.summary" );
00108 
00109 
00110     AddressBook::Iterator it;
00111     Addressee::List list;
00112     for ( it = addressBook()->begin(); it !=addressBook()->end(); ++it ) {
00113         if ( (*it).resource() != this || !(*it).changed() )
00114             continue;
00115 
00116     // remove, convert add set unchanged false
00117         list.clear();
00118         mWrap->remove( (*it).uid() );
00119         VCardTool tool;
00120         list.append( (*it) );
00121         mWrap->add( (*it).uid(), tool.createVCards( list,  VCard::v2_1) );
00122 
00123     (*it).setChanged( false );
00124     }
00125 
00126     return true;
00127 }
00128 void ResourceEvolution::removeAddressee( const Addressee& rem) {
00129     if (!m_isOpen) return;
00130 
00131     mWrap->remove( rem.uid() );
00132 }
KDE Home | KDE Accessibility Home | Description of Access Keys