summaryrefslogtreecommitdiff
path: root/src/backends/sqlite/SQLiteContactSource.cpp
blob: c20f02b2e10fd82288f843b82374624d83e29f74 (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
/*
 * Copyright (C) 2007-2009 Patrick Ohly <patrick.ohly@gmx.de>
 * Copyright (C) 2009 Intel Corporation
 *
 * 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
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <synthesis/SDK_util.h>
#ifdef ENABLE_SQLITE

#include "SQLiteContactSource.h"

#include <syncevo/SynthesisEngine.h>
#include <synthesis/sync_dbapi.h>

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


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

#include <syncevo/declarations.h>
SE_BEGIN_CXX

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", "N_LAST"},
        { "Middle", "ABPerson", "N_MIDDLE"},
        { "First", "ABPerson", "N_FIRST" },
        { "Prefix", "ABPerson", "N_PREFIX" },
        { "Suffix", "ABPerson", "N_SUFFIX" },
        { "FirstSort", "ABPerson", "" },
        { "LastSort", "ABPerson", "" },
        { "Organization", "ABPerson", "ORG_NAME" },
        { "Department", "ABPerson", "ORG_DIVISION" },
        { "Unit", "ABPerson", "ORG_OFFICE" },
        //{"Team", "ABPerson", "ORG_TEAM" }
        { "Note", "ABPerson", "NOTE" },
        { "Birthday", "ABPerson", "BDAY" },
        { "JobTitle", "ABPerson", "ROLE" },
        { "Title", "ABPerson", "TITLE" },
        { "Nickname", "ABPerson", "NICKNAME" },
        { "CompositeNameFallback", "ABPerson", "FN" },
        { "Categories", "ABPerson", "CATEGORIES" },

        { "AIM", "ABPerson", "AIM_HANDLE" },
        { "Groupwise", "ABPerson", "GROUPWISE_HANDLE" },
        { "ICQ", "ABPerson", "ICQ_HANDLE" },
        { "Yahoo", "ABPerson", "YAHOO_HANDLE" },

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

        { "Assistant", "ABPerson", "ASSISTANT" },
        { "Manager", "ABPerson", "MANAGER" },
        { "Spouse", "ABPerson", "SPOUSE" },

    
        { "URL", "ABPerson", "WEB" },
        { "BlogURL", "ABPerson", "BLOGURL" },
        { "VideoURL", "ABPerson", "VIDEOURL" },

        { 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()
{
    m_sqlite.close();
}

void SQLiteContactSource::getSynthesisInfo(SynthesisInfo &info, XMLConfigFragments &fragment)
{
    SourceType sourceType = getSourceType();
    string type;
    if (!sourceType.m_format.empty()) {
        type = sourceType.m_format;
    }
    info.m_native = "vCard21";
    info.m_fieldlist = "contacts";
    info.m_datatypes =
        "        <use datatype='vCard21' mode='rw' preferred='yes'/>\n"
        "        <use datatype='vCard30' mode='rw'/>\n";

    if (type == "text/x-vcard:2.1" || type == "text/x-vcard") {
        info.m_datatypes =
            "        <use datatype='vCard21' mode='rw' preferred='yes'/>\n";
        if (!sourceType.m_forceFormat) {
            info.m_datatypes +=
                "        <use datatype='vCard30' mode='rw'/>\n";
        }
    } else if (type == "text/vcard:3.0" || type == "text/vcard") {
        info.m_datatypes =
            "        <use datatype='vCard30' mode='rw' preferred='yes'/>\n";
        if (!sourceType.m_forceFormat) {
            info.m_datatypes +=
                "        <use datatype='vCard21' mode='rw'/>\n";
        }
    } else {
        throwError(string("configured MIME type not supported: ") + type);
    }
 
}


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

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

bool SQLiteContactSource::isEmpty()
{
    // there are probably more efficient ways to do this, but this is just
    // a proof-of-concept anyway
    sqliteptr all(m_sqlite.prepareSQL("SELECT ROWID FROM ABPerson;"));
    while (m_sqlite.checkSQL(sqlite3_step(all)) == SQLITE_ROW) {
        return false;
    }
    return true;
}

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));
    }
}

sysync::TSyError SQLiteContactSource::readItemAsKey(sysync::cItemID aID, sysync::KeyH aItemKey)
{
    string uid = aID->item;

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

    for (int i = 0; i<LAST_COL; i++) {
        SQLiteUtil::Mapping map = m_sqlite.getMapping(i);
        string field = map.fieldname;
        if(!field.empty()) {
            string value = m_sqlite.getTextColumn(contact, map.colindex);
            sysync::TSyError res = getSynthesisAPI()->setValue(aItemKey, field, value.c_str(), value.size());
            if (res != sysync::LOCERR_OK) {
                SE_LOG_WARNING(getDisplayName(), "SQLite backend: set field %s value %s failed", field.c_str(), value.c_str());
            }
        }
    }
    return sysync::LOCERR_OK;
}

sysync::TSyError SQLiteContactSource::insertItemAsKey(sysync::KeyH aItemKey, sysync::cItemID aID, sysync::ItemID newID)
{
    string uid = aID ? aID->item :"";
    string newuid = uid;
    string creationTime;
    string first, last;

    int numparams = 0;
    stringstream cols;
    stringstream values;

    std::list<string> insValues;
    for (int i = 0; i<LAST_COL; i++) {
        SQLiteUtil::Mapping map = m_sqlite.getMapping(i);
        string field = map.fieldname;
        SharedBuffer data;
        if (!field.empty() && !getSynthesisAPI()->getValue (aItemKey, field, data)) {
            insValues.push_back (string (data.get()));
            cols << m_sqlite.getMapping(i).colname << ", ";
            values <<"?, ";
            numparams ++;
            if (field == "N_FIRST") {
                first = insValues.back();
            } else if (field == "N_LAST") {
                last = insValues.back();
            }
        }
    }

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

    cols << "FirstSort, LastSort";
    values << "?, ?";
    numparams += 2;
    insValues.push_back(firstsort);
    insValues.push_back(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.prepareSQL("INSERT INTO ABPerson( %s ) VALUES( %s );", cols.str().c_str(), values.str().c_str()));

    // now bind parameter values in the same order as the columns specification above
    int param = 1;
    BOOST_FOREACH (string &value, insValues) {
        m_sqlite.checkSQL(sqlite3_bind_text(insert, param++, value.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", "");
    }
    newID->item = StrAlloc(newuid.c_str());

    updateRevision(*m_trackingNode, uid, newuid, m_sqlite.time2str(modificationTime));
    return sysync::LOCERR_OK;
}


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));
    // TODO: throw STATUS_NOT_FOUND exception when nothing was deleted
    deleteRevision(*m_trackingNode, uid);
}

void SQLiteContactSource::enableServerMode()
{
    SyncSourceAdmin::init(m_operations, this);
    SyncSourceBlob::init(m_operations, getCacheDir());
}

bool SQLiteContactSource::serverModeEnabled() const
{
    return m_operations.m_loadAdminData;
}


void SQLiteContactSource::beginSync(const std::string &lastToken, const std::string &resumeToken)
{
    detectChanges(*m_trackingNode, CHANGES_FULL);
}


std::string SQLiteContactSource::endSync(bool success)
{
    if (success) {
        m_trackingNode->flush();
    } else {
        // The Synthesis docs say that we should rollback in case of
        // failure. Cannot do that for data, so lets at least keep
        // the revision map unchanged.
    }

    // no token handling at the moment (not needed for clients)
    return "";
}

SE_END_CXX

#endif /* ENABLE_SQLITE */

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