summaryrefslogtreecommitdiff
path: root/src/backends/sqlite/SQLiteContactSource.cpp
blob: 58f5745d76963132bdcccb0385cbba405caae178 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (C) 2007-2008 Patrick Ohly
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "config.h"
#include "EvolutionSyncSource.h"

#ifdef ENABLE_SQLITE

#include "SQLiteContactSource.h"

#include <common/base/Log.h>
#include "vocl/VConverter.h"

#include <algorithm>
#include <cctype>
#include <sstream>

using namespace vocl;

#include <boost/algorithm/string/case_conv.hpp>

enum {
    PERSON_LAST,
    PERSON_MIDDLE,
    PERSON_FIRST,
    PERSON_PREFIX,
    PERSON_SUFFIX,
    PERSON_LASTSORT,
    PERSON_FIRSTSORT,
    PERSON_ORGANIZATION,
    PERSON_DEPARTMENT,
    PERSON_UNIT,
    PERSON_NOTE,
    PERSON_BIRTHDAY,
    PERSON_JOBTITLE,
    PERSON_TITLE,
    PERSON_NICKNAME,
    PERSON_FULLNAME,
    PERSON_CATEGORIES,

    PERSON_AIM,
    PERSON_GROUPWISE,
    PERSON_ICQ,
    PERSON_YAHOO,

    PERSON_FILEAS,
    PERSON_ANNIVERSARY,

    PERSON_ASSISTANT,
    PERSON_MANAGER,
    PERSON_SPOUSE,

    PERSON_URL,
    PERSON_BLOG_URL,
    PERSON_VIDEO_URL,

    LAST_COL
};

void SQLiteContactSource::open()
{
    static const SQLiteUtil::Mapping mapping[LAST_COL + 1] = {
        { "Last", "ABPerson" },
        { "Middle", "ABPerson" },
        { "First", "ABPerson" },
        { "Prefix", "ABPerson" },
        { "Suffix", "ABPerson" },
        { "FirstSort", "ABPerson" },
        { "LastSort", "ABPerson" },
        { "Organization", "ABPerson" },
        { "Department", "ABPerson" },
        { "Unit", "ABPerson" },
        { "Note", "ABPerson", "NOTE" },
        { "Birthday", "ABPerson", "BDAY" },
        { "JobTitle", "ABPerson", "ROLE" },
        { "Title", "ABPerson", "TITLE" },
        { "Nickname", "ABPerson", "NICKNAME" },
        { "CompositeNameFallback", "ABPerson", "FN" },
        { "Categories", "ABPerson", "CATEGORIES" },

        { "AIM", "ABPerson", "X-AIM" },
        { "Groupwise", "ABPerson", "X-GROUPWISE" },
        { "ICQ", "ABPerson", "X-ICQ" },
        { "Yahoo", "ABPerson", "X-YAHOO" },

        { "FileAs", "ABPerson", "X-EVOLUTION-FILE-AS" },
        { "Anniversary", "ABPerson", "X-EVOLUTION-ANNIVERSARY" },

        { "Assistant", "ABPerson", "X-EVOLUTION-ASSISTANT" },
        { "Manager", "ABPerson", "X-EVOLUTION-MANAGER" },
        { "Spouse", "ABPerson", "X-EVOLUTION-SPOUSE" },

    
        { "URL", "ABPerson", "URL" },
        { "BlogURL", "ABPerson", "X-EVOLUTION-BLOG-URL" },
        { "VideoURL", "ABPerson", "X-EVOLUTION-VIDEO-URL" },

        { NULL }
    };
    static const char *schema = 
        "BEGIN TRANSACTION;"
        "CREATE TABLE ABPerson (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, "
        "First TEXT, "
        "Last TEXT, "
        "Middle TEXT, "
        "FirstPhonetic TEXT, "
        "MiddlePhonetic TEXT, "
        "LastPhonetic TEXT, "
        "Organization TEXT, "
        "Department TEXT, "
        "Unit TEXT, "
        "Note TEXT, "
        "Kind INTEGER, "
        "Birthday TEXT, "
        "JobTitle TEXT, "
        "Title TEXT, "
        "Nickname TEXT, "
        "Prefix TEXT, "
        "Suffix TEXT, "
        "FirstSort TEXT, "
        "LastSort TEXT, "
        "CreationDate INTEGER, "
        "ModificationDate INTEGER, "
        "CompositeNameFallback TEXT, "
        "Categories TEXT, "
        "AIM TEXT, "
        "Groupwise TEXT, "
        "ICQ Text, "
        "Yahoo TEXT, "
        "Anniversary TEXT, "
        "Assistant TEXT, "
        "Manager TEXT, "
        "Spouse TEXT, "
        "URL TEXT, "
        "BlogURL TEXT, "
        "VideoURL TEXT, "
        "FileAs TEXT);"
        "COMMIT;";

    string id = getDatabaseID();
    m_sqlite.open(getName(),
                  id.c_str(),
                  mapping,
                  schema);
}

void SQLiteContactSource::close()
{
    // Our change tracking is time based.
    // Don't let caller proceed without waiting for
    // one second to prevent being called again before
    // the modification time stamp is larger than it
    // is now.
    sleepSinceModification(1);

    m_sqlite.close();
}

SQLiteContactSource::Databases SQLiteContactSource::getDatabases()
{
    Databases result;

    result.push_back(Database("select database via file path",
                              "file:///<absolute path>"));
    return result;
}

void SQLiteContactSource::listAllItems(RevisionMap_t &revisions)
{
    sqliteptr all(m_sqlite.prepareSQL("SELECT ROWID, CreationDate, ModificationDate FROM ABPerson;"));
    while (m_sqlite.checkSQL(sqlite3_step(all)) == SQLITE_ROW) {
        string uid = m_sqlite.toString(SQLITE3_COLUMN_KEY(all, 0));
        string modTime = m_sqlite.time2str(m_sqlite.getTimeColumn(all, 2));
        revisions.insert(RevisionMap_t::value_type(uid, modTime));
    }
}

SyncItem *SQLiteContactSource::createItem(const string &uid)
{
    logItem(uid, "extracting from database", true);

    sqliteptr contact(m_sqlite.prepareSQL("SELECT * FROM ABPerson WHERE ROWID = '%s';", uid.c_str()));
    if (m_sqlite.checkSQL(sqlite3_step(contact)) != SQLITE_ROW) {
        throw runtime_error(string(getName()) + ": contact not found: " + uid);
    }

    VObject vobj;
    string tmp;

    vobj.addProperty("BEGIN", "VCARD");
    vobj.addProperty("VERSION", "2.1");
    vobj.setVersion("2.1");

    tmp = m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_LAST).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_MIDDLE).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_FIRST).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_PREFIX).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_SUFFIX).colindex);
    if (tmp.size() > 4) {
        vobj.addProperty("N", tmp.c_str());
    }

    tmp = m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_ORGANIZATION).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_DEPARTMENT).colindex);
    tmp += VObject::SEMICOLON_REPLACEMENT;
    tmp += m_sqlite.getTextColumn(contact, m_sqlite.getMapping(PERSON_UNIT).colindex);
    if (tmp.size() > 2) {
        vobj.addProperty("ORG", tmp.c_str());
    }
 
    m_sqlite.rowToVObject(contact, vobj);
    vobj.addProperty("END", "VCARD");
    vobj.fromNativeEncoding();

    arrayptr<char> finalstr(vobj.toString(), "VOCL string");
    LOG.debug("%s", (char *)finalstr);

    cxxptr<SyncItem> item( new SyncItem( uid.c_str() ) );
    item->setData( (char *)finalstr, strlen(finalstr) );
    item->setDataType( getMimeType() );
    item->setModificationTime( 0 );

    return item.release();
}

TrackingSyncSource::InsertItemResult SQLiteContactSource::insertItem(const string &uid, const SyncItem &item)
{
    string newuid = uid;
    string creationTime;
    std::auto_ptr<VObject> vobj(VConverter::parse((char *)((SyncItem &)item).getData()));
    if (vobj.get() == 0) {
        throwError(string("parsing contact ") + ((SyncItem &)item).getKey());
    }
    vobj->toNativeEncoding();

    int numparams = 0;
    stringstream cols;
    stringstream values;
    VProperty *prop;

    // parse up to three fields of ORG
    prop = vobj->getProperty("ORG");
    string organization, department, unit;
    if (prop && prop->getValue()) {
        string fn = prop->getValue();
        size_t sep1 = fn.find(VObject::SEMICOLON_REPLACEMENT);
        size_t sep2 = sep1 == fn.npos ? fn.npos : fn.find(VObject::SEMICOLON_REPLACEMENT, sep1 + 1);

        organization = fn.substr(0, sep1);
        if (sep1 != fn.npos) {
            department = fn.substr(sep1 + 1, (sep2 == fn.npos) ? fn.npos : sep2 - sep1 - 1);
        }
        if (sep2 != fn.npos) {
            unit = fn.substr(sep2 + 1);
        }
    }
    cols << m_sqlite.getMapping(PERSON_ORGANIZATION).colname << ", " <<
        m_sqlite.getMapping(PERSON_DEPARTMENT).colname << ", " <<
        m_sqlite.getMapping(PERSON_UNIT).colname << ", ";
    values << "?, ?, ?, ";
    numparams += 3;

    // parse the name, insert empty fields if not found
    prop = vobj->getProperty("N");
    string first, middle, last, prefix, suffix, firstsort, lastsort;
    if (prop && prop->getValue()) {
        string fn = prop->getValue();
        size_t sep1 = fn.find(VObject::SEMICOLON_REPLACEMENT);
        size_t sep2 = sep1 == fn.npos ? fn.npos : fn.find(VObject::SEMICOLON_REPLACEMENT, sep1 + 1);
        size_t sep3 = sep2 == fn.npos ? fn.npos : fn.find(VObject::SEMICOLON_REPLACEMENT, sep2 + 1);
        size_t sep4 = sep3 == fn.npos ? fn.npos : fn.find(VObject::SEMICOLON_REPLACEMENT, sep3 + 1);

        last = fn.substr(0, sep1);
        if (sep1 != fn.npos) {
            middle = fn.substr(sep1 + 1, (sep2 == fn.npos) ? fn.npos : sep2 - sep1 - 1);
        }
        if (sep2 != fn.npos) {
            first = fn.substr(sep2 + 1, (sep3 == fn.npos) ? fn.npos : sep3 - sep2 - 1);
        }
        if (sep3 != fn.npos) {
            prefix = fn.substr(sep3 + 1, (sep4 == fn.npos) ? fn.npos : sep4 - sep3 - 1);
        }
        if (sep4 != fn.npos) {
            suffix = fn.substr(sep4 + 1);
        }
    }
    cols << m_sqlite.getMapping(PERSON_FIRST).colname << ", " <<
        m_sqlite.getMapping(PERSON_MIDDLE).colname << ", " <<
        m_sqlite.getMapping(PERSON_LAST).colname << ", " <<
        m_sqlite.getMapping(PERSON_PREFIX).colname << ", " <<
        m_sqlite.getMapping(PERSON_SUFFIX).colname << ", " <<
        m_sqlite.getMapping(PERSON_LASTSORT).colname << ", " <<
        m_sqlite.getMapping(PERSON_FIRSTSORT).colname;
    values << "?, ?, ?, ?, ?, ?, ?";
    numparams += 7;


    // synthesize sort keys: upper case with specific order of first/last name
    firstsort = first + " " + last;
    boost::to_upper(firstsort);
    lastsort = last + " " + first;
    boost::to_upper(lastsort);

    // optional fixed UID, potentially fixed creation time
    if (uid.size()) {
        creationTime = m_sqlite.findColumn("ABPerson", "ROWID", uid.c_str(), "CreationDate", "");
        cols << ", ROWID";
        values << ", ?";
        numparams++;
    }
    cols << ", CreationDate, ModificationDate";
    values << ", ?, ?";
    numparams += 2;

    // delete complete row so that we can recreate it
    if (uid.size()) {
        sqliteptr remove(m_sqlite.prepareSQL("DELETE FROM ABPerson WHERE ROWID == ?;"));
        m_sqlite.checkSQL(sqlite3_bind_text(remove, 1, uid.c_str(), -1, SQLITE_TRANSIENT));
        m_sqlite.checkSQL(sqlite3_step(remove));
    }

    string cols_str = cols.str();
    string values_str = values.str();
    sqliteptr insert(m_sqlite.vObjectToRow(*vobj,
                                                    "ABPerson",
                                                    numparams,
                                                    cols.str(),
                                                    values.str()));

    // now bind parameter values in the same order as the columns specification above
    int param = 1;
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, organization.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, department.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, unit.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, first.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, middle.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, last.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, prefix.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, suffix.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, lastsort.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, firstsort.c_str(), -1, SQLITE_TRANSIENT));
    if (uid.size()) {
        m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, uid.c_str(), -1, SQLITE_TRANSIENT));
        m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, creationTime.c_str(), -1, SQLITE_TRANSIENT));
    } else {
        m_sqlite.checkSQL(sqlite3_bind_int64(insert, param++, (long long)time(NULL)));
    }
    SQLiteUtil::syncml_time_t modificationTime = time(NULL);
    m_sqlite.checkSQL(sqlite3_bind_int64(insert, param++, modificationTime));
                      
    m_sqlite.checkSQL(sqlite3_step(insert));
                      
    if (!uid.size()) {
        // figure out which UID was assigned to the new contact
        newuid = m_sqlite.findColumn("SQLITE_SEQUENCE", "NAME", "ABPerson", "SEQ", "");
    }
    return InsertItemResult(newuid,
                            m_sqlite.time2str(modificationTime),
                            false);
}


void SQLiteContactSource::deleteItem(const string &uid)
{
    sqliteptr del;

    del.set(m_sqlite.prepareSQL("DELETE FROM ABPerson WHERE "
                                "ABPerson.ROWID = ?;"));
    m_sqlite.checkSQL(sqlite3_bind_text(del, 1, uid.c_str(), -1, SQLITE_TRANSIENT));
    m_sqlite.checkSQL(sqlite3_step(del));
}

void SQLiteContactSource::logItem(const string &uid, const string &info, bool debug)
{
    if (LOG.getLevel() >= (debug ? LOG_LEVEL_DEBUG : LOG_LEVEL_INFO)) {
        (LOG.*(debug ? &Log::debug : &Log::info))("%s: %s %s",
                                                  getName(),
                                                  m_sqlite.findColumn("ABPerson",
                                                                      "ROWID",
                                                                      uid.c_str(),
                                                                      "FirstSort",
                                                                      uid.c_str()).c_str(),
                                                  info.c_str());
    }
}

void SQLiteContactSource::logItem(const SyncItem &item, const string &info, bool debug)
{
    if (!item.getData()) {
        logItem(string(item.getKey()), info, debug);
        return;
    }

    if (LOG.getLevel() >= (debug ? LOG_LEVEL_DEBUG : LOG_LEVEL_INFO)) {
        string data = (const char *)item.getData();

        // avoid pulling in a full vcard parser by just searching for a specific property,
        // FN in this case
        string name = "???";
        string prop = "\nFN:";
        size_t start = data.find(prop);
        if (start != data.npos) {
            start += prop.size();
            size_t end = data.find("\n", start);
            if (end != data.npos) {
                name = data.substr(start, end - start);
            }
        }

        (LOG.*(debug ? &Log::debug : &Log::info))("%s: %s %s",
                                                  getName(),
                                                  name.c_str(),
                                                  info.c_str());
    }
}

#endif /* ENABLE_SQLITE */

#ifdef ENABLE_MODULES
# include "SQLiteContactSourceRegister.cpp"
#endif