2005-11-25 21:50:21 +01:00
|
|
|
/*
|
2006-12-10 18:35:20 +01:00
|
|
|
* Copyright (C) 2005-2006 Patrick Ohly
|
|
|
|
* Copyright (C) 2007 Funambol
|
2005-11-25 21:50:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EvolutionSyncSource.h"
|
2005-11-26 22:16:03 +01:00
|
|
|
#include "EvolutionContactSource.h"
|
2006-04-09 13:48:11 +02:00
|
|
|
#include "EvolutionCalendarSource.h"
|
2005-11-25 21:50:21 +01:00
|
|
|
|
2005-11-26 22:16:03 +01:00
|
|
|
#include <common/base/Log.h>
|
|
|
|
|
|
|
|
ESource *EvolutionSyncSource::findSource( ESourceList *list, const string &id )
|
2005-11-25 21:50:21 +01:00
|
|
|
{
|
|
|
|
for (GSList *g = e_source_list_peek_groups (list); g; g = g->next) {
|
|
|
|
ESourceGroup *group = E_SOURCE_GROUP (g->data);
|
|
|
|
GSList *s;
|
|
|
|
for (s = e_source_group_peek_sources (group); s; s = s->next) {
|
|
|
|
ESource *source = E_SOURCE (s->data);
|
2006-06-10 14:26:44 +02:00
|
|
|
char *uri = e_source_get_uri(source);
|
|
|
|
bool found = !id.compare(e_source_peek_name(source)) ||
|
|
|
|
(uri && !id.compare(uri));
|
|
|
|
g_free(uri);
|
|
|
|
if (found) {
|
2005-11-25 21:50:21 +01:00
|
|
|
return source;
|
2006-06-10 14:26:44 +02:00
|
|
|
}
|
2005-11-25 21:50:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvolutionSyncSource::throwError( const string &action, GError *gerror )
|
|
|
|
{
|
|
|
|
string gerrorstr;
|
|
|
|
if (gerror) {
|
2006-05-29 21:32:58 +02:00
|
|
|
gerrorstr += ": ";
|
2005-11-25 21:50:21 +01:00
|
|
|
gerrorstr += gerror->message;
|
|
|
|
g_clear_error(&gerror);
|
|
|
|
} else {
|
|
|
|
gerrorstr = ": failed";
|
|
|
|
}
|
2005-11-26 22:16:03 +01:00
|
|
|
|
2006-05-26 14:49:19 +02:00
|
|
|
throw runtime_error(string(getName()) + ": " + action + gerrorstr);
|
2005-11-26 22:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EvolutionSyncSource::resetItems()
|
|
|
|
{
|
|
|
|
m_allItems.clear();
|
|
|
|
m_newItems.clear();
|
|
|
|
m_updatedItems.clear();
|
|
|
|
m_deletedItems.clear();
|
2005-11-25 21:50:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string EvolutionSyncSource::getData(SyncItem& item)
|
|
|
|
{
|
|
|
|
char *mem = (char *)malloc(item.getDataSize() + 1);
|
|
|
|
memcpy(mem, item.getData(), item.getDataSize());
|
|
|
|
mem[item.getDataSize()] = 0;
|
|
|
|
|
|
|
|
string res(mem);
|
|
|
|
free(mem);
|
|
|
|
return res;
|
|
|
|
}
|
2005-11-26 22:16:03 +01:00
|
|
|
|
|
|
|
string EvolutionSyncSource::getPropertyValue(ManagementNode &node, const string &property)
|
|
|
|
{
|
2006-11-27 22:08:12 +01:00
|
|
|
char *value = node.readPropertyValue(property.c_str());
|
2005-11-26 22:16:03 +01:00
|
|
|
string res;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
res = value;
|
|
|
|
delete [] value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-05-26 14:49:19 +02:00
|
|
|
void EvolutionSyncSource::handleException()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
throw;
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
if (lastErrorCode == ERR_NONE) {
|
|
|
|
lastErrorCode = ERR_UNSPECIFIED;
|
|
|
|
strcpy(lastErrorMsg, ex.what());
|
|
|
|
}
|
|
|
|
LOG.error("%s", ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-26 22:16:03 +01:00
|
|
|
EvolutionSyncSource *EvolutionSyncSource::createSource(
|
|
|
|
const string &name,
|
2006-10-03 13:44:34 +02:00
|
|
|
SyncSourceConfig *sc,
|
2005-11-26 22:16:03 +01:00
|
|
|
const string &changeId,
|
|
|
|
const string &id,
|
|
|
|
const string &mimeType
|
|
|
|
)
|
|
|
|
{
|
2006-04-09 13:48:11 +02:00
|
|
|
// remove special characters from change ID
|
|
|
|
string strippedChangeId = changeId;
|
2006-06-11 22:07:26 +02:00
|
|
|
size_t offset = 0;
|
2006-04-09 13:48:11 +02:00
|
|
|
while (offset < strippedChangeId.size()) {
|
|
|
|
switch (strippedChangeId[offset]) {
|
|
|
|
case ':':
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
strippedChangeId.erase(offset, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
offset++;
|
2005-12-03 15:33:41 +01:00
|
|
|
}
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mimeType == "text/x-vcard") {
|
2006-07-23 12:27:21 +02:00
|
|
|
#ifdef ENABLE_EBOOK
|
2006-09-29 01:02:43 +02:00
|
|
|
return new EvolutionContactSource(name, sc, strippedChangeId, id, EVC_FORMAT_VCARD_21);
|
2006-07-23 12:27:21 +02:00
|
|
|
#else
|
|
|
|
LOG.error("access to addressbooks not compiled into this binary, text/x-vcard not supported");
|
|
|
|
#endif
|
2006-04-26 22:44:41 +02:00
|
|
|
} else if (mimeType == "text/vcard") {
|
2006-07-23 12:27:21 +02:00
|
|
|
#ifdef ENABLE_EBOOK
|
2006-09-29 01:02:43 +02:00
|
|
|
return new EvolutionContactSource(name, sc, strippedChangeId, id, EVC_FORMAT_VCARD_30);
|
2006-07-23 12:27:21 +02:00
|
|
|
#else
|
|
|
|
LOG.error("access to addressbooks not compiled into this binary, text/vcard not supported");
|
|
|
|
#endif
|
2006-04-09 13:48:11 +02:00
|
|
|
} else if (mimeType == "text/x-todo") {
|
2006-07-23 12:27:21 +02:00
|
|
|
#ifdef ENABLE_ECAL
|
2006-09-29 01:02:43 +02:00
|
|
|
return new EvolutionCalendarSource(E_CAL_SOURCE_TYPE_TODO, name, sc, strippedChangeId, id);
|
2006-07-23 12:27:21 +02:00
|
|
|
#else
|
|
|
|
LOG.error("access to calendars not compiled into this binary, text/x-todo not supported");
|
|
|
|
#endif
|
2006-04-09 13:48:11 +02:00
|
|
|
} else if (mimeType == "text/calendar" ||
|
|
|
|
mimeType == "text/x-vcalendar") {
|
2006-07-23 12:27:21 +02:00
|
|
|
#ifdef ENABLE_ECAL
|
2006-09-29 01:02:43 +02:00
|
|
|
return new EvolutionCalendarSource(E_CAL_SOURCE_TYPE_EVENT, name, sc, strippedChangeId, id);
|
2006-07-23 12:27:21 +02:00
|
|
|
#else
|
|
|
|
LOG.error("access to calendars not compiled into this binary, %s not supported", mimeType.c_str());
|
|
|
|
#endif
|
2005-11-26 22:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-09 13:48:11 +02:00
|
|
|
int EvolutionSyncSource::beginSync()
|
|
|
|
{
|
2006-05-25 19:32:07 +02:00
|
|
|
string buffer;
|
|
|
|
buffer += getName();
|
|
|
|
buffer += ": sync mode is ";
|
2006-04-09 13:48:11 +02:00
|
|
|
SyncMode mode = getSyncMode();
|
2006-05-25 19:32:07 +02:00
|
|
|
buffer += mode == SYNC_SLOW ? "'slow'" :
|
|
|
|
mode == SYNC_TWO_WAY ? "'two-way'" :
|
|
|
|
mode == SYNC_REFRESH_FROM_SERVER ? "'refresh from server'" :
|
|
|
|
mode == SYNC_REFRESH_FROM_CLIENT ? "'refresh from client'" :
|
2006-09-08 21:55:40 +02:00
|
|
|
mode == SYNC_ONE_WAY_FROM_SERVER ? "'one-way from server'" :
|
|
|
|
mode == SYNC_ONE_WAY_FROM_CLIENT ? "'one-way from client'" :
|
2006-05-25 19:32:07 +02:00
|
|
|
mode == SYNC_NONE ? "'none' (for debugging)" :
|
2006-04-09 13:48:11 +02:00
|
|
|
"???";
|
|
|
|
LOG.info( buffer.c_str() );
|
|
|
|
|
|
|
|
try {
|
|
|
|
const char *error = getenv("SYNCEVOLUTION_BEGIN_SYNC_ERROR");
|
|
|
|
if (error && strstr(error, getName())) {
|
2006-05-26 14:49:19 +02:00
|
|
|
throw runtime_error("artificial error in beginSync()");
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset state
|
|
|
|
m_isModified = false;
|
|
|
|
m_allItems.clear();
|
|
|
|
m_newItems.clear();
|
|
|
|
m_updatedItems.clear();
|
|
|
|
m_deletedItems.clear();
|
|
|
|
|
|
|
|
// determine what to do
|
|
|
|
bool needAll = false;
|
|
|
|
bool needPartial = false;
|
|
|
|
bool deleteLocal = false;
|
|
|
|
switch (mode) {
|
|
|
|
case SYNC_SLOW:
|
|
|
|
needAll = true;
|
|
|
|
m_isModified = true;
|
|
|
|
break;
|
2006-11-04 14:00:47 +01:00
|
|
|
case SYNC_ONE_WAY_FROM_CLIENT:
|
2006-04-09 13:48:11 +02:00
|
|
|
case SYNC_TWO_WAY:
|
|
|
|
needPartial = true;
|
|
|
|
break;
|
|
|
|
case SYNC_REFRESH_FROM_SERVER:
|
|
|
|
deleteLocal = true;
|
|
|
|
m_isModified = true;
|
|
|
|
break;
|
|
|
|
case SYNC_REFRESH_FROM_CLIENT:
|
|
|
|
needAll = true;
|
2006-08-17 23:39:29 +02:00
|
|
|
m_isModified = true;
|
2006-04-09 13:48:11 +02:00
|
|
|
break;
|
|
|
|
case SYNC_NONE:
|
|
|
|
// special mode for testing: prepare both all and partial lists
|
|
|
|
needAll = needPartial = true;
|
|
|
|
break;
|
2006-11-04 14:00:47 +01:00
|
|
|
case SYNC_ONE_WAY_FROM_SERVER:
|
|
|
|
// nothing to do, just wait for server's changes
|
|
|
|
break;
|
2006-04-09 13:48:11 +02:00
|
|
|
default:
|
2006-05-26 14:49:19 +02:00
|
|
|
throw runtime_error("unsupported sync mode, valid are only: slow, two-way, refresh");
|
2006-04-09 13:48:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginSyncThrow(needAll, needPartial, deleteLocal);
|
|
|
|
} catch( ... ) {
|
2006-05-26 14:49:19 +02:00
|
|
|
handleException();
|
2006-04-09 13:48:11 +02:00
|
|
|
m_hasFailed = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EvolutionSyncSource::endSync()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
endSyncThrow();
|
|
|
|
} catch ( ... ) {
|
2006-05-26 14:49:19 +02:00
|
|
|
handleException();
|
2006-04-09 13:48:11 +02:00
|
|
|
m_hasFailed = true;
|
|
|
|
}
|
2006-05-25 19:32:07 +02:00
|
|
|
|
|
|
|
// Do _not_ tell the caller (SyncManager) if an error occurred
|
|
|
|
// because that causes Sync4jClient to abort processing for all
|
|
|
|
// sync sources. Instead deal with failed sync sources in
|
|
|
|
// EvolutionSyncClient::sync().
|
|
|
|
return 0;
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
2005-11-26 22:16:03 +01:00
|
|
|
void EvolutionSyncSource::setItemStatus(const char *key, int status)
|
|
|
|
{
|
2006-04-09 13:48:11 +02:00
|
|
|
try {
|
|
|
|
// TODO: logging
|
|
|
|
setItemStatusThrow(key, status);
|
|
|
|
} catch (...) {
|
2006-05-26 14:49:19 +02:00
|
|
|
handleException();
|
|
|
|
m_hasFailed = true;
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int EvolutionSyncSource::addItem(SyncItem& item)
|
|
|
|
{
|
2006-06-15 17:12:11 +02:00
|
|
|
return processItem("add", &EvolutionSyncSource::addItemThrow, item, true);
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
2005-11-26 22:16:03 +01:00
|
|
|
|
2006-04-09 13:48:11 +02:00
|
|
|
int EvolutionSyncSource::updateItem(SyncItem& item)
|
|
|
|
{
|
2006-06-15 17:12:11 +02:00
|
|
|
return processItem("update", &EvolutionSyncSource::updateItemThrow, item, true);
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int EvolutionSyncSource::deleteItem(SyncItem& item)
|
|
|
|
{
|
2006-06-15 17:12:11 +02:00
|
|
|
return processItem("delete", &EvolutionSyncSource::deleteItemThrow, item, false);
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int EvolutionSyncSource::processItem(const char *action,
|
2006-04-17 11:41:37 +02:00
|
|
|
int (EvolutionSyncSource::*func)(SyncItem& item),
|
2006-06-15 17:12:11 +02:00
|
|
|
SyncItem& item,
|
|
|
|
bool needData)
|
2006-04-09 13:48:11 +02:00
|
|
|
{
|
2006-04-17 11:41:37 +02:00
|
|
|
int status = STC_COMMAND_FAILED;
|
|
|
|
|
2006-04-09 13:48:11 +02:00
|
|
|
try {
|
|
|
|
logItem(item, action);
|
2006-06-17 22:07:38 +02:00
|
|
|
if (needData && (item.getDataSize() < 0 || !item.getData())) {
|
2006-06-15 17:12:11 +02:00
|
|
|
// Something went wrong in the server: update or add without data.
|
|
|
|
// Shouldn't happen, but it did with one server and thus this
|
|
|
|
// security check was added to prevent segfaults.
|
|
|
|
logItem(item, "ignored due to missing data");
|
|
|
|
status = STC_OK;
|
|
|
|
} else {
|
|
|
|
status = (this->*func)(item);
|
|
|
|
}
|
2006-04-09 13:48:11 +02:00
|
|
|
m_isModified = true;
|
|
|
|
} catch (...) {
|
2006-05-26 14:49:19 +02:00
|
|
|
handleException();
|
2006-04-09 13:48:11 +02:00
|
|
|
m_hasFailed = true;
|
|
|
|
}
|
2006-04-17 11:41:37 +02:00
|
|
|
return status;
|
2006-04-09 13:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EvolutionSyncSource::setItemStatusThrow(const char *key, int status)
|
|
|
|
{
|
2006-05-25 12:35:36 +02:00
|
|
|
switch (status) {
|
|
|
|
case STC_ALREADY_EXISTS:
|
|
|
|
// found pair during slow sync, that's okay
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (status < 200 || status > 300) {
|
2006-05-25 19:32:07 +02:00
|
|
|
LOG.error("%s: unexpected SyncML status response %d for item %.80s\n",
|
|
|
|
getName(), status, key);
|
2006-05-25 12:35:36 +02:00
|
|
|
m_hasFailed = true;
|
|
|
|
}
|
2005-11-26 22:16:03 +01:00
|
|
|
}
|
|
|
|
}
|