summaryrefslogtreecommitdiffstats
path: root/kabc/plugins/evolution/resourceevo.cpp
blob: a1858bf833ef08d081761be11c54cb4c67b26fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <qdir.h>

#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>

#include <stdio.h>

#include <kabc/vcardparser/vcardtool.h>

#include "dbwrapper.h"
#include "resourceevo.h"

using namespace Evolution;
using namespace KABC;

class EvolutionFactory : public KRES::PluginFactoryBase
{
  public:
    KRES::Resource *resource( const KConfig *config )
    {
      return new ResourceEvolution( config );
    }

    KRES::ConfigWidget *configWidget( QWidget * )
    {
      return 0;
    }
};

extern "C"
{
  KDE_EXPORT void *init_kabc_evo()
  {
    return ( new EvolutionFactory() );
  }
}

ResourceEvolution::ResourceEvolution( const KConfig* conf )
    : Resource( conf ), mWrap(0l)
{
    m_isOpen = false;
}
ResourceEvolution::~ResourceEvolution() {
    delete mWrap;
}
bool ResourceEvolution::doOpen() {
    mWrap = new DBWrapper;
    if (!mWrap->open( QDir::homeDirPath() + "/evolution/local/Contacts/addressbook.db" ) ) {
        return false;
    }

    QString val;
    if (!mWrap->find( "PAS-DB-VERSION", val ) )
        return false;

    if (!val.startsWith("0.2") )
        return false;

    m_isOpen = true;

    return true;
}
void ResourceEvolution::doClose() {
    delete mWrap;
    mWrap = 0l;
    m_isOpen = false;
}
Ticket* ResourceEvolution::requestSaveTicket() {
    if ( !addressBook() ) return 0;
    return createTicket( this );
}
/*
 * skip the first key
 */

bool ResourceEvolution::load() {
    /* doOpen never get's called :( */
    if (!doOpen()) return false;
    if (!mWrap ) return false; // open first!

    DBIterator it = mWrap->begin();
    // skip the "PAS-DB-VERSION"

    for ( ; it != mWrap->end(); ++it ) {
        if ( it.key().startsWith("PAS-DB-VERSION") )
            continue;

        qWarning( "val:%s", it.value().latin1() );
        VCardTool tool;
        QString str = it.value().stripWhiteSpace();
        Addressee::List list = tool.parseVCards( str );
        if (!list.first().isEmpty() ) {
            Addressee adr = list.first();
            adr.setResource(this);
            addressBook()->insertAddressee( adr );
        }
    }
    return true;
}
bool ResourceEvolution::save( Ticket* ticket ) {
    delete ticket;
    if (!m_isOpen ) return false;

    // just delete the summary so evolution will regenerate it 
    // on next start up
    (void)QFile::remove( QDir::homeDirPath() + "/evolution/local/Contacts/addressbook.db.summary" );


    AddressBook::Iterator it;
    Addressee::List list;
    for ( it = addressBook()->begin(); it !=addressBook()->end(); ++it ) {
        if ( (*it).resource() != this || !(*it).changed() )
            continue;

	// remove, convert add set unchanged false
        list.clear();
        mWrap->remove( (*it).uid() );
        VCardTool tool;
        list.append( (*it) );
        mWrap->add( (*it).uid(), tool.createVCards( list,  VCard::v2_1) );

	(*it).setChanged( false );
    }

    return true;
}
void ResourceEvolution::removeAddressee( const Addressee& rem) {
    if (!m_isOpen) return;

    mWrap->remove( rem.uid() );
}