summaryrefslogtreecommitdiffstats
path: root/kresources/carddav/writer.cpp
blob: 68bf126d53a69e0e65cb11b8277cfa1234677815 (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
/*=========================================================================
| KABCDAV
|--------------------------------------------------------------------------
| (c) 2010  Timothy Pearson
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Remote address book writing class.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#include "writer.h"
#include <kdebug.h>
#include <string>

/*=========================================================================
| DEFINES
 ========================================================================*/

// Use carddav_modify_object() function.
// If it's not set, a pair of carddav_delete_object/carddav_add_object
// is used for modifying objects.
// It's done, because, for some reason, Zimbra returns an error
// on carddav_modify_object.
//#define USE_CARDDAV_MODIFY

/*=========================================================================
| NAMESPACE
 ========================================================================*/

using namespace KABC;

/*=========================================================================
| METHODS
 ========================================================================*/

void CardDavWriter::cleanJob() {
    CardDavJob::cleanJob();
}

int CardDavWriter::runJob(runtime_info* RT) {
    kdDebug() << "writer::run, url: " << url() << "\n";

    int res = OK;

    kdDebug() << "pushing added objects" << '\n';
    res = pushObjects(mAdded, carddav_add_object, OK, RT);
    if (OK == res) {
#ifdef USE_CARDDAV_MODIFY
        kdDebug() << "pushing changed objects" << '\n';
        if (getUseURI() == false)
            res = pushObjects(mChanged, carddav_modify_object, OK, RT);
        else
            res = pushObjects(mChanged, carddav_modify_object_by_uri, OK, RT);
        if (OK == res) {
            kdDebug() << "pushing deleted objects" << '\n';
            while (mDeleted.contains("BEGIN:VCARD", TRUE) > 0) {
                int endLocation = mDeleted.find("END:VCARD", 0, TRUE);
                TQString deletedCurSeq = mDeleted.mid(0, endLocation+9);
                mDeleted = mDeleted.remove(0, endLocation+9);
                if (getUseURI() == false)
                    res = pushObjects(deletedCurSeq, carddav_delete_object, OK, RT);
                else
                    res = pushObjects(deletedCurSeq, carddav_delete_object_by_uri, OK, RT);
            }
        }
#else // if USE_CARDDAV_MODIFY
        kdDebug() << "pushing changed objects (delete)" << '\n';
        if (getUseURI() == false)
            res = pushObjects(mChanged, carddav_delete_object, OK, RT);
        else
            res = pushObjects(mChanged, carddav_delete_object_by_uri, OK, RT);
        if (OK == res) {
            kdDebug() << "pushing changed objects (add)" << '\n';
            if (getUseURI() == false)
                res = pushObjects(mChanged, carddav_add_object, OK, RT);
            else
                res = pushObjects(mChanged, carddav_add_object, OK, RT);
            if (OK == res) {
                kdDebug() << "pushing deleted objects" << '\n';
                while (mDeleted.contains("BEGIN:VCARD", TRUE) > 0) {
                    int endLocation = mDeleted.find("END:VCARD", 0, TRUE);
                    TQString deletedCurSeq = mDeleted.mid(0, endLocation+9);
                    mDeleted = mDeleted.remove(0, endLocation+9);
                    if (getUseURI() == false)
                        res = pushObjects(deletedCurSeq, carddav_delete_object, OK, RT);
                    else
                        res = pushObjects(deletedCurSeq, carddav_delete_object_by_uri, OK, RT);
                }
            }
        }
#endif // if USE_CARDDAV_MODIFY
    }

    if (OK != res) {
        clearObjects();
    }

    return res;
}

// EOF ========================================================================