From 5e77a5c4612262f1495551ba5f6001093c0a04b3 Mon Sep 17 00:00:00 2001 From: cdfrey Date: Sat, 9 Jun 2007 03:12:48 +0000 Subject: [PATCH] - opensync compiles again, with 0.30! (not functional yet) - moving closer to more modular and reusable plugin functions --- opensync-plugin/src/barry_sync.cc | 191 ++++++++++++++++++++----------------- opensync-plugin/src/environment.cc | 22 ++++- opensync-plugin/src/environment.h | 24 ++++- opensync-plugin/src/trace.h | 5 + 4 files changed, 149 insertions(+), 93 deletions(-) diff --git a/opensync-plugin/src/barry_sync.cc b/opensync-plugin/src/barry_sync.cc index c940c8b4..a09dc8f6 100644 --- a/opensync-plugin/src/barry_sync.cc +++ b/opensync-plugin/src/barry_sync.cc @@ -1,12 +1,12 @@ -// -// \file barry_sync.cc -// Opensync module for the USB Blackberry handheld -// -// A lot of this code is based heavily on the example -// plugin from the OpenSync project: -// http://www.opensync.org/ -// OpenSync is distributed under LGPLv2.1 -// +/// +/// \file barry_sync.cc +/// Opensync module for the USB Blackberry handheld +/// +/// A lot of this code is based heavily on the example +/// plugin from the OpenSync project: +/// http://www.opensync.org/ +/// OpenSync is distributed under LGPLv2.1 +/// /* Copyright (C) 2006-2007, Net Direct Inc. (http://www.netdirect.ca/) @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include "barry_sync.h" #include "environment.h" @@ -56,12 +58,11 @@ extern "C" { // Support functions and classes // -void GetChanges(OSyncContext *ctx, BarryEnvironment *env, - DatabaseSyncState::cache_type &cache, - const char *DBDBName, const char *FormatName, +void GetChanges(OSyncContext *ctx, OSyncObjTypeSink *sink, + BarryEnvironment *env, DatabaseSyncState *sync, GetData_t getdata) { - Trace trace("GetChanges"); + Trace trace("GetChanges", sync->GetDesc().c_str()); OSyncError *error = NULL; @@ -69,9 +70,10 @@ void GetChanges(OSyncContext *ctx, BarryEnvironment *env, using namespace Barry; using Barry::RecordStateTable; Controller &con = *env->m_pCon; + DatabaseSyncState::cache_type &cache = sync->m_Cache; // fetch state table - unsigned int dbId = con.GetDBID(DBDBName); + unsigned int dbId = con.GetDBID(sync->GetDBName()); RecordStateTable table; con.GetRecordStateTable(dbId, table); @@ -129,9 +131,8 @@ void GetChanges(OSyncContext *ctx, BarryEnvironment *env, // Set the last argument to FALSE if the real data // should be queried later in a "get_data" function char *data = (*getdata)(env, dbId, index); - osync_change_set_data(change, data, strlen(data), TRUE); - OSyncData *odata = osync_data_new(data, 0, env->objformat, &error); + OSyncData *odata = osync_data_new(NULL, 0, sync->m_pObjFormat, &error); if( !odata ) { osync_change_unref(change); osync_context_report_osyncwarning(ctx, error); @@ -140,6 +141,10 @@ void GetChanges(OSyncContext *ctx, BarryEnvironment *env, } osync_data_set_objtype(odata, osync_objtype_sink_get_name(sink)); + osync_data_set_data(odata, data, strlen(data)); + + osync_change_set_data(change, odata); + osync_data_unref(odata); // just report the change via osync_context_report_change(ctx, change); @@ -166,17 +171,35 @@ void GetChanges(OSyncContext *ctx, BarryEnvironment *env, // register a DELETE, no data trace.log("found DELETE change"); -FIXME - this needs to match the same calls as above... - OSyncChange *change = osync_change_new(); + OSyncChange *change = osync_change_new(&error); + if( !change ) { + trace.error("osync_change_new() failed"); + osync_context_report_osyncwarning(ctx, error); + osync_error_unref(&error); + continue; + } + osync_change_set_changetype(change, OSYNC_CHANGE_TYPE_DELETED); - osync_change_set_member(change, env->member); - osync_change_set_objformat_string(change, FormatName); + + OSyncData *odata = osync_data_new(NULL, 0, sync->m_pObjFormat, &error); + if( !odata ) { + osync_change_unref(change); + osync_context_report_osyncwarning(ctx, error); + osync_error_unref(&error); + continue; + } + osync_change_set_data(change, odata); + + osync_data_set_objtype(odata, osync_objtype_sink_get_name(sink)); char *uid = g_strdup_printf("%u", recordId); osync_change_set_uid(change, uid); trace.log(uid); g_free(uid); + osync_change_set_data(change, odata); + osync_data_unref(odata); + // report the change osync_context_report_change(ctx, change); } @@ -340,6 +363,13 @@ static void connect(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx) // initialize via this call: BarryEnvironment *env = (BarryEnvironment *)userdata; + if( env->IsConnected() ) { + // we've been here before.... possibly another "sink" + // was run ahead of us + trace.log("Already connected in connect()"); + return; + } + // Probe for available devices Barry::Probe probe; int nIndex = probe.FindActive(env->m_pin); @@ -371,22 +401,22 @@ static void get_changes(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx { Trace trace("get_changes"); - try { + BarryEnvironment *env = (BarryEnvironment*) userdata; + OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); + DatabaseSyncState *sync = (DatabaseSyncState*) osync_objtype_sink_get_userdata(sink); - BarryEnvironment *env = (BarryEnvironment *)userdata; + if( !sync ) { + osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, + "Unable to retrieve database sync state."); + return; + } - if( env->m_CalendarSync.m_Sync ) { - GetChanges(ctx, env, env->m_CalendarSync.m_Cache, - "Calendar", "vevent20", - &VEventConverter::GetRecordData); - } + trace.logf("get_chagnes for %s", sync->GetDesc().c_str()); - if( env->m_ContactsSync.m_Sync ) { - // FIXME - not yet implemented -// GetChanges(ctx, env, env->m_ContactsSync.m_Cache -// "Address Book", "vcard30", -// &ContactToVCard::GetRecordData); - } + try { + + GetChanges(ctx, sink, env, sync, + &VEventConverter::GetRecordData); // FIXME ack, hard coded // Success! osync_context_report_success(ctx); @@ -399,7 +429,7 @@ static void get_changes(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx // this function will be called exactly once with all objects to write // gathered in an array -static void batch_commit_vevent20(void *userdata, +static void batch_commit_calendar(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx, OSyncContext **contexts, @@ -463,55 +493,47 @@ static void batch_commit_vevent20(void *userdata, static void sync_done(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx) { // - // This function will only be called if the sync was successfull + // This function will only be called if the sync was successful // Trace trace("sync_done"); - try { +// BarryEnvironment *env = (BarryEnvironment*) userdata; + OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); + DatabaseSyncState *sync = (DatabaseSyncState*) osync_objtype_sink_get_userdata(sink); + bool error = false; - BarryEnvironment *env = (BarryEnvironment *)userdata; - bool error = false; + if( !sync ) { + osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, + "Unable to retrieve database sync state."); + return; + } - if( env->m_CalendarSync.m_Sync ) { - // update the cache - if( !env->m_CalendarSync.SaveCache() ) { - error = true; - osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, - "Error saving calendar cache"); - } + trace.logf("sync_done for %s", sync->GetDesc().c_str()); - // save the id map - env->m_CalendarSync.CleanupMap(); - if( !env->m_CalendarSync.SaveMap() ) { - error = true; - osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, - "Error saving calendar id map"); - } + try { - // clear all dirty flags - env->ClearCalendarDirtyFlags(); - } - if( env->m_ContactsSync.m_Sync ) { - // update the cache - if( !env->m_ContactsSync.SaveCache() ) { - error = true; - osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, - "Error saving contacts cache"); - } + // update the cache + if( !sync->SaveCache() ) { + error = true; + trace.errorf("Error saving %s cache", sync->GetDesc().c_str()); + osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, + trace.get_last_msg()); + } - // save the id map - env->m_ContactsSync.CleanupMap(); - if( !env->m_ContactsSync.SaveMap() ) { - error = true; - osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, - "Error saving contacts id map"); - } - // clear all dirty flags - env->ClearContactsDirtyFlags(); + // save the id map + sync->CleanupMap(); + if( !sync->SaveMap() ) { + error = true; + trace.errorf("Error saving %s id map", sync->GetDesc().c_str()); + osync_context_report_error(ctx, OSYNC_ERROR_IO_ERROR, + trace.get_last_msg()); } + // clear all dirty flags + sync->ClearDirtyFlags(); + // Success if( !error ) osync_context_report_success(ctx); @@ -540,14 +562,6 @@ static void disconnect(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx) // OpenSync plugin callbacks // -static void finalize(void *userdata) -{ - Trace trace("finalize"); - - BarryEnvironment *env = (BarryEnvironment *)userdata; - delete env; -} - static void *initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError **error) { Trace trace("initialize"); @@ -588,7 +602,7 @@ static void *initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError * // and it appears to be sink-related... there's a lot we could // do here to organize this into classes, per-sink, etc. OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); -env->objformat = osync_format_env_find_objformat(formatenv, "vevent20"); +env->m_CalendarSync.m_pObjFormat = osync_format_env_find_objformat(formatenv, "vevent20"); // Setup the Calendar sink, using batch commit OSyncObjTypeSink *sink = osync_objtype_sink_new("event", error); @@ -605,13 +619,12 @@ env->objformat = osync_format_env_find_objformat(formatenv, "vevent20"); functions.connect = connect; functions.disconnect = disconnect; functions.get_changes = get_changes; - functions.batch_commit = batch_commit_vevent20; + functions.batch_commit = batch_commit_calendar; functions.sync_done = sync_done; - // We pass the OSyncFileDir object to the sink, - // so we dont have to look it up again once - // the functions are called - osync_objtype_sink_set_functions(sink, functions, env.get()); + // store pointer to database object in sink, + // for functions to use later + osync_objtype_sink_set_functions(sink, functions, &env->m_CalendarSync); osync_plugin_info_add_objtype(info, sink); @@ -638,6 +651,14 @@ env->objformat = osync_format_env_find_objformat(formatenv, "vevent20"); } } +static void finalize(void *userdata) +{ + Trace trace("finalize"); + + BarryEnvironment *env = (BarryEnvironment *)userdata; + delete env; +} + // Tell opensync which sinks are available static osync_bool discover(void *userdata, OSyncPluginInfo *info, OSyncError **error) { diff --git a/opensync-plugin/src/environment.cc b/opensync-plugin/src/environment.cc index 5ba119a5..6e2697a5 100644 --- a/opensync-plugin/src/environment.cc +++ b/opensync-plugin/src/environment.cc @@ -32,9 +32,15 @@ ////////////////////////////////////////////////////////////////////////////// // DatabaseSyncState -DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo *pi, const char *description) - : m_Sync(false), - m_Desc(description) +DatabaseSyncState::DatabaseSyncState(BarryEnvironment *pEnv, + OSyncPluginInfo *pi, + const char *DBName, + const char *description) + : m_pEnv(pEnv), + m_Sync(false), + m_pObjFormat(0), + m_Desc(description), + m_DBName(DBName) { m_CacheFilename = m_MapFilename = osync_plugin_info_get_configdir(pi); @@ -110,6 +116,12 @@ void DatabaseSyncState::CleanupMap() } } +void DatabaseSyncState::ClearDirtyFlags() +{ + Trace trace("DatabaseSyncState::ClearDirtyFlags()", m_Desc.c_str()); + m_pEnv->ClearDirtyFlags(m_Table, GetDBName()); +} + unsigned long DatabaseSyncState::GetMappedRecordId(const std::string &uid) { Trace trace("DatabaseSyncState::GetMappedRecordId()", m_Desc.c_str()); @@ -149,8 +161,8 @@ unsigned long DatabaseSyncState::GetMappedRecordId(const std::string &uid) BarryEnvironment::BarryEnvironment(OSyncPluginInfo *pi) : m_pin(0), m_pCon(0), - m_CalendarSync(pi, "calendar"), - m_ContactsSync(pi, "contacts") + m_CalendarSync(this, pi, Barry::Calendar::GetDBName(), "calendar"), + m_ContactsSync(this, pi, Barry::Contact::GetDBName(), "contacts") { } diff --git a/opensync-plugin/src/environment.h b/opensync-plugin/src/environment.h index f45ce2e3..d97b5bc4 100644 --- a/opensync-plugin/src/environment.h +++ b/opensync-plugin/src/environment.h @@ -29,8 +29,13 @@ #include "idmap.h" -struct DatabaseSyncState +class BarryEnvironment; + +class DatabaseSyncState { +private: + BarryEnvironment *m_pEnv; + public: // cache is a map of record ID to bool... the bool doesn't mean // anything... the mere existence of the ID means it belongs @@ -51,11 +56,16 @@ public: bool m_Sync; + // OpenSync data + OSyncObjFormat *m_pObjFormat; + private: std::string m_Desc; + const char *m_DBName; public: - DatabaseSyncState(OSyncPluginInfo *pi, const char *description); + DatabaseSyncState(BarryEnvironment *pEnv, OSyncPluginInfo *pi, + const char *DBName, const char *description); ~DatabaseSyncState(); bool LoadCache(); @@ -68,10 +78,13 @@ public: void ClearDirtyFlags(); unsigned long GetMappedRecordId(const std::string &uid); + + const std::string& GetDesc() const { return m_Desc; } + const char* GetDBName() const { return m_DBName; } }; -struct BarryEnvironment +class BarryEnvironment { public: // user config data @@ -89,6 +102,11 @@ public: BarryEnvironment(OSyncPluginInfo *pi); ~BarryEnvironment(); + // meta data + bool IsConnected() { return m_pCon; } + + // operations + void OpenDesktop(Barry::ProbeResult &result); void Disconnect(); diff --git a/opensync-plugin/src/trace.h b/opensync-plugin/src/trace.h index 0b2870db..27dde8ec 100644 --- a/opensync-plugin/src/trace.h +++ b/opensync-plugin/src/trace.h @@ -29,6 +29,7 @@ class Trace { const char *text, *tag; bool m_error; + char m_buffer[2048]; public: explicit Trace(const char *t) : text(t), tag(0), m_error(false) { @@ -50,6 +51,8 @@ public: "barry_sync: %s", text); } + const char* get_last_msg() const { return m_buffer; } + void log(const char *t) { osync_trace(TRACE_INTERNAL, "barry_sync: %s", t); @@ -72,6 +75,7 @@ public: osync_trace(TRACE_INTERNAL, "barry_sync: %s", buffer); else osync_trace(TRACE_INTERNAL, "barry_sync: (trace error, output too long for buffer: %s)", t); + strcpy(m_buffer, buffer); } void errorf(const char *t, ...) @@ -87,6 +91,7 @@ public: osync_trace(TRACE_ERROR, "barry_sync: %s", buffer); else osync_trace(TRACE_ERROR, "barry_sync: (trace error, output too long for buffer: %s)", t); + strcpy(m_buffer, buffer); } }; -- 2.11.4.GIT