From: <svn...@op...> - 2009-11-01 17:12:58
|
Author: henrik Date: Sun Nov 1 18:12:45 2009 New Revision: 5901 URL: http://www.opensync.org/changeset/5901 Log: Until now, the mozilla-sync plugin has been running in the OpenSync process, and accessing the mozilla data in a non-thread-safe way. The mozilla-sync plugin is now replaced by a simple stub, running with OSYNC_START_TYPE_EXTERNAL. See http://old.nabble.com/-trunk--OSYNC_START_TYPE_EXTERNAL-example---OSyncClient-API-changes-ts26137387.html This means, that the bulk of the work is now done in the blueZync plugin, by calling thunderbird --mozilla-sync /path/to/pipe The implementation is still not thread-safe, but with the threading capabilities of Thunderbird 3, it will be possible to make a thread-safe implementation for Thunderbird 3. The blueZync plugin can be found here: http://bluezync.kaarposoft.dk/ Deleted: plugins/mozilla-sync/trunk/CTestConfig.cmake plugins/mozilla-sync/trunk/src/calendar-event.cpp plugins/mozilla-sync/trunk/src/calendar-event.h plugins/mozilla-sync/trunk/src/mozilla-calendar.cpp plugins/mozilla-sync/trunk/src/mozilla-calendar.h plugins/mozilla-sync/trunk/src/mozilla-config.cpp plugins/mozilla-sync/trunk/src/mozilla-config.h plugins/mozilla-sync/trunk/src/mozilla-connect-test.cpp plugins/mozilla-sync/trunk/src/mozilla-headers.h plugins/mozilla-sync/trunk/src/mozilla-sync-utils.h plugins/mozilla-sync/trunk/src/mozilla-sync.h plugins/mozilla-sync/trunk/src/mozilla-utils.cpp plugins/mozilla-sync/trunk/src/mozilla-utils.h plugins/mozilla-sync/trunk/src/mozilla-xpcom.cpp plugins/mozilla-sync/trunk/src/mozilla-xpcom.h plugins/mozilla-sync/trunk/src/nsProfileDirServiceProvider.cpp plugins/mozilla-sync/trunk/src/nsProfileDirServiceProvider.h plugins/mozilla-sync/trunk/src/thunderbird-addressbook.cpp plugins/mozilla-sync/trunk/src/thunderbird-addressbook.h plugins/mozilla-sync/trunk/src/thunderbird-card.cpp plugins/mozilla-sync/trunk/src/thunderbird-card.h plugins/mozilla-sync/trunk/tests/ Modified: plugins/mozilla-sync/trunk/CMakeLists.txt plugins/mozilla-sync/trunk/src/CMakeLists.txt plugins/mozilla-sync/trunk/src/mozilla-sync plugins/mozilla-sync/trunk/src/mozilla-sync.cpp Modified: plugins/mozilla-sync/trunk/CMakeLists.txt ============================================================================== --- plugins/mozilla-sync/trunk/CMakeLists.txt Sun Nov 1 17:26:59 2009 (r5900) +++ plugins/mozilla-sync/trunk/CMakeLists.txt Sun Nov 1 18:12:45 2009 (r5901) @@ -40,23 +40,8 @@ SET( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${DESTDIR}${CMAKE_INSTALL_PREFIX}/share/libopensync1/cmake/modules/" "${CMAKE_SOURCE_DIR}/cmake/modules" ) FIND_PACKAGE( GLIB2 2.14 REQUIRED) -FIND_PACKAGE( LibXml2 REQUIRED ) FIND_PACKAGE( OpenSync REQUIRED ) -FIND_PACKAGE( ThunderbirdXpcom REQUIRED ) -STRING (REGEX REPLACE "^([0-9]+)\\.([0-9]+)$" "\\1\\2" _CALENDAR_VERSION "${SUNBIRD_VERSION}" ) -STRING (REGEX REPLACE "^0*([0-9]+)$" "\\1" CALENDAR_VERSION "${_CALENDAR_VERSION}" ) -MESSAGE ( "CALENDAR_VERSION=[${CALENDAR_VERSION}]" ) - -### SEEMS THAT WE CANNOT LINK TO GLUE SINCE IT IS NOT THREAD SAFE, AND WE WILL SEGFAULT -SET(XPCOM_GLUE FALSE) - -IF (XPCOM_GLUE) - ADD_DEFINITIONS( -DXPCOM_GLUE ) - SET( XPCOM_LIBRARIES /home/henrik/my/software/gecko-sdk/lib/libxpcomglue.a ) -ELSE (XPCOM_GLUE) -# SET( XPCOM_LIBRARIES ${LIBTBXPCOM_XPCOM} ) -ENDIF (XPCOM_GLUE) INCLUDE( OpenSyncInternal ) @@ -65,15 +50,6 @@ ADD_SUBDIRECTORY( src ) -MESSAGE ( "ENABLE_TESTING [${ENABLE_TESTING}]" ) -IF( ENABLE_TESTING ) - MESSAGE( "TESTING ENABLED" ) - ENABLE_TESTING() - INCLUDE( Dart ) - FIND_PACKAGE( Check REQUIRED ) - ADD_SUBDIRECTORY( tests ) -ENDIF ( ENABLE_TESTING ) - # cmake is great. but four back-slashes to escape a dot? Go figure... SET ( CPACK_SOURCE_IGNORE_FILES "\\\\.svn/;.*~" ) @@ -88,3 +64,4 @@ # For debugging... #INCLUDE ( CMakePrintSystemInformation ) + Modified: plugins/mozilla-sync/trunk/src/CMakeLists.txt ============================================================================== --- plugins/mozilla-sync/trunk/src/CMakeLists.txt Sun Nov 1 17:26:59 2009 (r5900) +++ plugins/mozilla-sync/trunk/src/CMakeLists.txt Sun Nov 1 18:12:45 2009 (r5901) @@ -22,84 +22,26 @@ # ================================================================ -MESSAGE("LIBTBXPCOM_INCLUDE_DIR ${LIBTBXPCOM_INCLUDE_DIR}") -MESSAGE("XPCOM_LIBRARIES ${XPCOM_LIBRARIES}") - -#ADD_DEFINITIONS( -Wall ) -#ADD_DEFINITIONS( -fdiagnostics-show-option ) - -ADD_DEFINITIONS ( -DCALENDAR_VERSION="${CALENDAR_VERSION}" ) - INCLUDE_DIRECTORIES( ${OPENSYNC_INCLUDE_DIRS} - ${XPCOM_INCLUDE_DIRS} - ${LIBXML2_INCLUDE_DIRS} - ${GLIB2_INCLUDE_DIRS} - ${ICAL_INCLUDE_DIRS} ) + ${GLIB2_INCLUDE_DIRS} +) LINK_DIRECTORIES( ${OPENSYNC_LIBRARY_DIRS} - ${XPCOM_LIBRARY_DIRS} - ${LIBXML2_LIBRARY_DIRS} ${GLIB2_LIBRARY_DIRS} - ${ICAL_LIBRARY_DIRS} ) - -SET( COMMON_SOURCE_FILES - mozilla-config.cpp - mozilla-utils.cpp - mozilla-xpcom.cpp - nsProfileDirServiceProvider.cpp - thunderbird-card.cpp - thunderbird-addressbook.cpp - mozilla-calendar.cpp - calendar-event.cpp -) -###### PLUGIN ################### - - -OPENSYNC_PLUGIN_ADD( mozilla-sync ${COMMON_SOURCE_FILES} mozilla-sync.cpp ) + ) -IF (XPCOM_GLUE) - SET_TARGET_PROPERTIES(mozilla-sync PROPERTIES SKIP_BUILD_RPATH yes) -ENDIF (XPCOM_GLUE) - -MESSAGE ( STATUS "XPCOM_LIBRARIES=[${XPCOM_LIBRARIES}]" ) -TARGET_LINK_LIBRARIES( mozilla-sync - ${OPENSYNC_LIBRARIES} ${XPCOM_LIBRARIES} - ${ICAL_LIBRARIES} ${LIBXML2_LIBRARIES} ${GLIB2_LIBRARIES} ) -#SET_TARGET_PROPERTIES(mozilla-sync PROPERTIES LINK_FLAGS "-rdynamic -Wl,--export-dynamic") -#SET_TARGET_PROPERTIES(mozilla-sync PROPERTIES LINKER_LANGUAGE C) -#SET_TARGET_PROPERTIES(mozilla-sync PROPERTIES LINK_FLAGS "-lstdc++") +OPENSYNC_PLUGIN_ADD( mozilla-sync mozilla-sync.cpp ) -###### PLUGIN ################### - OPENSYNC_PLUGIN_DESCRIPTIONS( mozilla-sync-desc.xml ) OPENSYNC_PLUGIN_CAPABILITIES( mozilla-sync-caps.xml ) OPENSYNC_PLUGIN_CONFIG( mozilla-sync ) OPENSYNC_PLUGIN_INSTALL( mozilla-sync ) - -###### CONNECTION TEST ################### - -# ### TODO ### -# mozilla-connect-test does not handle the new configuration format - -# With this we get some logging on stdout from nsProfileDirServiceProvider -#ADD_DEFINITIONS( -DNSPROFILEDIRSERVICEPROVIDER_DEBUG ) - -#ADD_EXECUTABLE (mozilla-connect-test ${COMMON_SOURCE_FILES} mozilla-connect-test.cpp) - -### WE MUST NOT HAVE -rdynamic OR EVERYTING WILL FAIL WITH SEGMENTATION FAULT -#SET( CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") - -#IF (XPCOM_GLUE) -# SET_TARGET_PROPERTIES(mozilla-connect-test PROPERTIES SKIP_BUILD_RPATH yes) -#ENDIF (XPCOM_GLUE) - -#TARGET_LINK_LIBRARIES( mozilla-connect-test -# ${XPCOM_LIBRARIES} -# ${ICAL_LIBRARIES} ${LIBXML2_LIBRARIES} ${GLIB2_LIBRARIES} ) +TARGET_LINK_LIBRARIES( mozilla-sync + ${OPENSYNC_LIBRARIES} ${GLIB2_LIBRARIES} ) Modified: plugins/mozilla-sync/trunk/src/mozilla-sync ============================================================================== --- plugins/mozilla-sync/trunk/src/mozilla-sync Sun Nov 1 17:26:59 2009 (r5900) +++ plugins/mozilla-sync/trunk/src/mozilla-sync Sun Nov 1 18:12:45 2009 (r5901) @@ -8,36 +8,16 @@ <config> -<AdvancedOptions> - - <AdvancedOption> - <!-- profile-directory is where the user's thunderbird profile lives. - Use the full pathname. Tilde ~ is *not* recognized. - Remove or leave blank to have mozilla-sync try to find - default profile directory automatically. --> - - <DisplayName>Profile directory</DisplayName> - <Name>profile-directory</Name> - <Type>string</Type> - <Value>/home/someone/.thunderbird/ab1cde23</Value> - </AdvancedOption> - -</AdvancedOptions> - - <Resources> <Resource> - <!-- addressbook-file is the name of the addressbook file. - Should have .mab extension. - Relative to profile-directory --> <Enabled>1</Enabled> <Formats> <Format> <Name>xmlformat-contact</Name> </Format> </Formats> - <Name>abook.mab</Name> + <Name>MyAddressBook</Name> <ObjType>contact</ObjType> </Resource> @@ -63,5 +43,8 @@ </Resources> +<ExternalPlugin> + <ExternalCommand>thunderbird -mozilla-sync %s</ExternalCommand> +</ExternalPlugin> </config> Modified: plugins/mozilla-sync/trunk/src/mozilla-sync.cpp ============================================================================== --- plugins/mozilla-sync/trunk/src/mozilla-sync.cpp Sun Nov 1 17:26:59 2009 (r5900) +++ plugins/mozilla-sync/trunk/src/mozilla-sync.cpp Sun Nov 1 18:12:45 2009 (r5901) @@ -30,61 +30,17 @@ ================================================================ */ -/** @mainpage - -Clickable map of main parts: - -@dot -digraph G -{ - rankdir = BT; - - edge [color="midnightblue"]; - node [fontname="FreeSans.ttf",fontsize=10,shape=record]; - - n1 [label="mozilla-sync", URL="\ref mozilla-sync.cpp"]; - n11 [label="mozilla-config", URL="\ref mozilla-config.cpp"]; - n11 -> n1; - n12 [label="mozilla-xpcom", URL="\ref mozilla-xpcom.cpp"]; - n12 -> n1; - n121 [label="nsProfileDirServiceProvider", URL="\ref nsProfileDirServiceProvider"]; - n121 -> n12; - n13 [label="ThunderbirdAddressbook",URL="\ref ThunderbirdAddressbook"]; - n13 -> n1; - n131 [label="ThunderbirdCard", URL="\ref ThunderbirdCard"]; - n131 -> n13; - n14 [label="MozillaCalendar",URL="\ref MozillaCalendar"]; - n14 -> n1; - n141 [label="calendar-event", URL="\ref calendar-event.cpp"]; - n141 -> n14; -} -@enddot - -**/ - #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "mozilla-headers.h" +#include <glib.h> #include <opensync/opensync.h> -#include <opensync/opensync-format.h> #include <opensync/opensync-plugin.h> #include <opensync/opensync-version.h> -#include <opensync/opensync-data.h> -#include <opensync/opensync-helper.h> -#include <opensync/opensync-capabilities.h> -#include <opensync/opensync-xmlformat.h> - -#include "thunderbird-card.h" -#include "calendar-event.h" -#include "thunderbird-addressbook.h" -#include "mozilla-calendar.h" -#include "mozilla-xpcom.h" -#include "mozilla-sync.h" #ifdef _WIN32 #ifndef __func__ @@ -123,16 +79,6 @@ osync_trace(TRACE_EXIT, "%s(%d)%s: %s", g_basename(__FILE__), __LINE__, __func__, __szMsg); \ g_free(__szMsg); } } -#define LOG_EXIT_ERROR_SZ(level, message) { \ - osync_trace(TRACE_EXIT_ERROR, "%s(%d)%s: %s", g_basename(__FILE__), __LINE__, __func__, message); \ - osync_error_set(ppOSyncError, OSYNC_ERROR_GENERIC, message); \ - ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, message); } - -#define LOG_EXIT_ERROR_OSYNC(level, ppOSyncError) { \ - const char *__szOErr=""; \ - if (ppOSyncError) __szOErr=osync_error_print_stack(ppOSyncError); \ - osync_trace(TRACE_EXIT_ERROR, "%s(%d)%s: %s", g_basename(__FILE__), __LINE__, __func__, __szOErr); \ - ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, __szOErr); } #define LOG_EXIT_ERROR_OSYNC_SZ(level, ppOSyncError, message) { \ const char *__szOErr=""; \ @@ -140,992 +86,18 @@ osync_trace(TRACE_EXIT_ERROR, "%s(%d)%s: %s: %s", g_basename(__FILE__), __LINE__, __func__, message, __szOErr); \ ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, __szOErr); } -#define LOG_EXIT_REPORT_ERROR_SZ(level, context, message) { \ - ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, message); \ - OSyncError *__pError = NULL; \ - osync_error_set(&__pError, OSYNC_ERROR_GENERIC, message); \ - osync_context_report_osyncerror(context, __pError); \ - osync_trace(TRACE_EXIT_ERROR, "%s(%d)%s: %s", g_basename(__FILE__), __LINE__, __func__, message); } - -#define LOG_EXIT_REPORT_ERROR_MSG(level, context, message, ...) { \ - if (level<=LOGLEVEL) { \ - char *__szMsg=g_strdup_printf(message,## __VA_ARGS__); \ - ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, __szMsg); \ - OSyncError *__pError = NULL; \ - osync_error_set(&__pError, OSYNC_ERROR_GENERIC, __szMsg); \ - osync_context_report_osyncerror(context, __pError); \ - osync_trace(TRACE_EXIT_ERROR, __szMsg); \ - g_free(__szMsg); } } - - -#define LOG_EXIT_REPORT_ERROR_OSYNC(level, context, pError) { \ - const char *__szOErr=""; \ - if (pError) __szOErr=osync_error_print_stack(&pError); \ - ERR_PRINTF("%s(%d)%s: ****** %s\n", g_basename(__FILE__), __LINE__, __func__, __szOErr); \ - osync_context_report_osyncerror(context, pError); \ - osync_trace(TRACE_EXIT_ERROR, "%s(%d)%s: %s", g_basename(__FILE__), __LINE__, __func__, __szOErr); } - -#define GFREE(p) { if (p) g_free(p); } - -// TODO: osync_error_unref(ppOSyncError); - - -// ------------------------------------------------------------ -/** Function to log output from child functions called - @param level Log level - @param sensitive True if output could contain personal info - @param szFile Name of file we are logging from - @param iLine Line number in szFile - @param szFunc Name of function we are logging from - @param szFormat format specifier and arguments for vprintf -**/ -void logFunction(int level, bool sensitive, const char* szFile, int iLine, const char *szFunc, const char* szFormat, ...) { - if (level>LOGLEVEL) return; - va_list ap; - va_start(ap, szFormat); - char *__szMsg=g_strdup_vprintf(szFormat, ap); - OSyncTraceType t=TRACE_INTERNAL; - if (sensitive) t=TRACE_SENSITIVE; - -#ifdef WIN32 - char *_szFile=g_strdup(szFile); - osync_trace(TRACE_INTERNAL, "%s(%d)%s: %s", g_basename(_szFile), iLine, szFunc, __szMsg); - g_free(_szFile); -#else - osync_trace(TRACE_INTERNAL, "%s(%d)%s: %s", g_basename(szFile), iLine, szFunc, __szMsg); -#endif - - g_free(__szMsg); - va_end(ap); -} - extern "C" { -// ---------------------------------------- -/** @brief Report address book changes since last sync into the hash-table provided - - @param pOSyncMozillaDatabase The database we have to report on - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) - @param pHashTable The hash table to report changes into - @return TRUE on success; FALSE on error -**/ -gboolean mozilla_sync_report_addressbook_changes_to_hash( - OSyncMozillaDatabase *pOSyncMozillaDatabase, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - OSyncHashTable *pHashTable) { - - LOG_ENTRY(10, "(%p, %p, %p)", pOSyncMozillaDatabase, pOSyncPluginInfo, pOSyncContext); - - if (pOSyncMozillaDatabase->iMozillaDatabaseType!=MOZILLA_DATABASE_TYPE_ADDRESSBOOK) { - LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, - g_strdup_printf("Expected MOZILLA_DATABASE_TYPE_ADDRESSBOOK; got [%d]", pOSyncMozillaDatabase->iMozillaDatabaseType)); - return FALSE; - } - - OSyncError *pError = NULL; - ThunderbirdAddressbook *pAB = pOSyncMozillaDatabase->pThunderbirdAddressbook; - - char *szError=NULL; - if (!pAB->CardFirst(&szError, &logFunction)) - { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return FALSE; } - - if (pAB->CardDone()) { - LOG(10, "No address cards in address book"); - } else { - while(!pAB->CardDone()) { - - // Get information on the current card - ThunderbirdCard *pCard=pAB->CardCurrent(); - char *szKey=pCard->GetKey(pAB->myAddrDatabase, &szError, &logFunction); - if (!szKey) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - char *szHash=pCard->GetHash(&szError, &logFunction); - if (!szHash) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - // Now get ready to describe the change - OSyncChange *pChange = osync_change_new(&pError); - if (!pChange) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - - // Identify the change - osync_change_set_uid(pChange, szKey); - osync_change_set_hash(pChange, szHash); - OSyncChangeType type = osync_hashtable_get_changetype(pHashTable, pChange); - osync_change_set_changetype(pChange, type); - osync_hashtable_update_change(pHashTable, pChange); - g_free(szHash); - g_free(szKey); - - if (type != OSYNC_CHANGE_TYPE_UNMODIFIED) { - - // Let's be sure we have the right format - const char *szExpectedFormat="xmlformat-contact"; - if (strcmp(pOSyncMozillaDatabase->szObjformat, szExpectedFormat)!=0) { - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, - g_strdup_printf("Wrong object format. OSyncMozillaDatabase says [%s], we wanted [%s]", - pOSyncMozillaDatabase->szObjformat,szExpectedFormat)); - return FALSE; } - - // Get the format - OSyncObjFormat *pOSyncObjFormat = - osync_format_env_find_objformat(pOSyncMozillaDatabase->pOSyncMozillaEnv->pOSyncFormatEnv, - pOSyncMozillaDatabase->szObjformat); - if (!pOSyncObjFormat) { - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, "Could not get OSyncObjFormat"); - return FALSE; } - - - char *szXML=pCard->GetXML(&szError, &logFunction); - LOG(20, "The xml of the card"); - LOG_SENSITIVE(20, "%s", szXML); - if (!szXML) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - //OSyncXMLFormat *pXMLFormat = osync_xmlformat_new(pOSyncMozillaDatabase->szObjformat, &pError); - - OSyncXMLFormat *pXMLFormat = osync_xmlformat_parse(szXML, strlen(szXML), &pError); - if (!pXMLFormat) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - if (!osync_xmlformat_sort(pXMLFormat, &pError)) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - GFREE(szXML); - OSyncData *odata = NULL; - - odata = osync_data_new((char*)pXMLFormat, osync_xmlformat_size(), pOSyncObjFormat, &pError); - - if (!odata) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - //if (!odata) { - //osync_change_unref(change); - //osync_context_report_osyncwarning(ctx, error); - //osync_error_unref(&error); - //g_free(data); - // continue; - //} - - osync_change_set_objtype(pChange, osync_objtype_sink_get_name(pOSyncMozillaDatabase->pOSyncObjTypeSink)); - osync_data_set_objtype(odata, osync_objtype_sink_get_name(pOSyncMozillaDatabase->pOSyncObjTypeSink)); - - - - // Report the change - osync_change_set_data(pChange, odata); - osync_data_unref(odata); - osync_context_report_change(pOSyncContext, pChange); - } - - osync_change_unref(pChange); - if (!pAB->CardNext(&szError, &logFunction)) - { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return FALSE; } - } - } - - LOG_EXIT(10, "TRUE"); - return TRUE; - -} - - -// ---------------------------------------- -/** @brief Report calendar changes since last sync into the provided hash table - - @todo mozilla_sync_report_calendar_changes_to_hash and mozilla_sync_report_addressbook_changes_to_hash - are quite similar. Could we merge them? - - @param pOSyncMozillaDatabase The database we have to report on - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) - @param pHashTable The hash table to report changes into - @return TRUE on success; FALSE on error -**/ -gboolean mozilla_sync_report_calendar_changes_to_hash( - OSyncMozillaDatabase *pOSyncMozillaDatabase, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - OSyncHashTable *pHashTable) { - - LOG_ENTRY(10, "(%p, %p, %p)", pOSyncMozillaDatabase, pOSyncPluginInfo, pOSyncContext); - - if (pOSyncMozillaDatabase->iMozillaDatabaseType!=MOZILLA_DATABASE_TYPE_CALENDAR) { - LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, - g_strdup_printf("Expected MOZILLA_DATABASE_TYPE_CALENDAR; got [%d]", pOSyncMozillaDatabase->iMozillaDatabaseType)); - return FALSE; - } - OSyncError *pError = NULL; - MozillaCalendar *pCal = pOSyncMozillaDatabase->pMozillaCalendar; - - char *szError=NULL; - if (!pCal->EventFirst(&szError, &logFunction)) - { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return FALSE; } - - if (pCal->EventDone()) { - LOG(10, "No events in calendar"); - } else { - while(!pCal->EventDone()) { - - // Get information on the current event - - char *szKey; - char *szHash; - char *szTitle; - - gboolean f; - f=pCal->CurrentKeyHashTitle(&szKey, &szHash, &szTitle, &szError, &logFunction); - if (!f) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - - // Now get ready to describe the change - OSyncChange *pChange = osync_change_new(&pError); - if (!pChange) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - - // Identify the change - osync_change_set_uid(pChange, szKey); - osync_change_set_hash(pChange, szHash); - OSyncChangeType type = osync_hashtable_get_changetype(pHashTable, pChange); - osync_change_set_changetype(pChange, type); - osync_hashtable_update_change(pHashTable, pChange); - g_free(szHash); - - // Bail out if no changes - if (type == OSYNC_CHANGE_TYPE_UNMODIFIED) { - LOG(20, "key=[%s] hash=[%s]", szKey, szHash); - LOG_SENSITIVE(20, "key=[%s] hash=[%s] title=[%s] UNMODIFIED", szKey, szHash, szTitle); - if (!pCal->EventNext(&szError, &logFunction)) - { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return FALSE; } - continue; - } - - // Let's be sure we have the right format - const char *szExpectedFormat="vevent20"; - if (strcmp(pOSyncMozillaDatabase->szObjformat, szExpectedFormat)!=0) { - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, - g_strdup_printf("Wrong object format. OSyncMozillaDatabase says [%s], we wanted [%s]", - pOSyncMozillaDatabase->szObjformat,szExpectedFormat)); - return FALSE; } - - // Get the format - OSyncObjFormat *pOSyncObjFormat = - osync_format_env_find_objformat(pOSyncMozillaDatabase->pOSyncMozillaEnv->pOSyncFormatEnv, - pOSyncMozillaDatabase->szObjformat); - if (!pOSyncObjFormat) { - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, "Could not get OSyncObjFormat"); - return FALSE; } - - - char *szICS=pCal->CurrentICS(&szError, &logFunction); - LOG_SENSITIVE(30, "%s", szICS); - if (!szICS) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - OSyncData *odata = NULL; - odata = osync_data_new(szICS, strlen(szICS), pOSyncObjFormat, &pError); - - if (!odata) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return FALSE; - } - - osync_change_set_objtype(pChange, osync_objtype_sink_get_name(pOSyncMozillaDatabase->pOSyncObjTypeSink)); - osync_data_set_objtype(odata, osync_objtype_sink_get_name(pOSyncMozillaDatabase->pOSyncObjTypeSink)); - - - // Report the change - osync_change_set_data(pChange, odata); - osync_data_unref(odata); - osync_context_report_change(pOSyncContext, pChange); - osync_change_unref(pChange); - if (!pCal->EventNext(&szError, &logFunction)) - { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return FALSE; } - } - } - - LOG_EXIT(10, "TRUE"); - return TRUE; - -} - - -// ---------------------------------------- -/** @brief Get changes since last sync (or all changes on slow sync) - - @param data The OSyncMozillaEnv - @param pOSyncObjTypeSink The ObjectType sink we shall get changes for. - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) - @param fSlow True if we are doing a slow sync, meaning we want to get *all* changes -**/ -static void mozilla_sync_get_changes( - OSyncObjTypeSink* pOSyncObjTypeSink, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - osync_bool fSlow, - void *data) -{ - LOG_ENTRY(10, "(%p, %p, %p, %d, %p)", pOSyncObjTypeSink, pOSyncPluginInfo, pOSyncContext, fSlow, data); - OSyncMozillaDatabase *pOSyncMozillaDatabase = (OSyncMozillaDatabase*) data; - int i = 0; - OSyncError *pError = NULL; - - OSyncHashTable *pHashTable = osync_objtype_sink_get_hashtable(pOSyncObjTypeSink); - // Thank you Michael Bell for corrections - if (fSlow) { - osync_trace(TRACE_INTERNAL, "Slow sync requested"); - if (!osync_hashtable_slowsync(pHashTable, &pError)) - { - osync_context_report_osyncerror(pOSyncContext, pError); - osync_trace(TRACE_EXIT_ERROR, "%s - %s", __func__, osync_error_print_stack(&pError)); - osync_error_unref(&pError); - return; - } - } - - LOG(10, "Getting changes for [%s]", osync_objtype_sink_get_name(pOSyncObjTypeSink)); - - gboolean f; - - if (pOSyncMozillaDatabase->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_ADDRESSBOOK) { - f=mozilla_sync_report_addressbook_changes_to_hash(pOSyncMozillaDatabase, pOSyncPluginInfo, pOSyncContext, pHashTable); - } else if (pOSyncMozillaDatabase->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_CALENDAR) { - f=mozilla_sync_report_calendar_changes_to_hash(pOSyncMozillaDatabase, pOSyncPluginInfo, pOSyncContext, pHashTable); - } else { - LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, "Unknown iMozillaDatabaseType"); return; - } - - if (!f) return; - - OSyncObjFormat *pOSyncObjFormat = - osync_format_env_find_objformat(pOSyncMozillaDatabase->pOSyncMozillaEnv->pOSyncFormatEnv, - pOSyncMozillaDatabase->szObjformat); - if (!pOSyncObjFormat) { - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, "Could not get OSyncObjFormat"); - return; } - - - LOG(10, "Checking for deleted [%s]", osync_objtype_sink_get_name(pOSyncObjTypeSink)); - - OSyncList *u; - OSyncList *uids = osync_hashtable_get_deleted(pHashTable); - for (u = uids; u; u = u->next) { - OSyncChange *change = osync_change_new(&pError); - if (!change) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - osync_error_unref(&pError); - continue; - } - const char *uid = (const char*) u->data; - - osync_change_set_uid(change, uid); - osync_change_set_changetype(change, OSYNC_CHANGE_TYPE_DELETED); - - OSyncData *odata = osync_data_new(NULL, 0, pOSyncObjFormat, &pError); - if (!odata) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - osync_change_unref(change); - osync_error_unref(&pError); - continue; - } - - osync_data_set_objtype(odata, osync_objtype_sink_get_name(pOSyncMozillaDatabase->pOSyncObjTypeSink)); - osync_change_set_data(change, odata); - osync_data_unref(odata); - - osync_context_report_change(pOSyncContext, change); - - osync_hashtable_update_change(pHashTable, change); - - osync_change_unref(change); - } - osync_list_free(uids); - - osync_context_report_success(pOSyncContext); - LOG(10, "Done getting changes for [%s]", osync_objtype_sink_get_name(pOSyncObjTypeSink)); - LOG_EXIT(10, "void"); -} - - -// ---------------------------------------- -/** @brief Commit change into Mozilla calendar - - @param pCalendar The calendar to commit the change into - @param changeType The type of change as reported by OpenSync - @param szKey The key of the mozilla addressbook card - @param szXML The full XML representation of the mozilla addressbook card - @param pHT Pointer to hash table to update - @param pOSyncContext The context of the connect (e.g. to report error) - @param pChange The change we shall commit - -**/ -static gboolean mozilla_sync_commit_calendar_change( - MozillaCalendar *pCalendar, - OSyncChangeType changeType, - const char *szKey, - const char *szXML, - OSyncHashTable *pHT, - OSyncContext *pOSyncContext, - OSyncChange *pChange) -{ - - LOG_ENTRY(10, "(%p, %d, %s, %p, %p, %p)", pCalendar, changeType, szKey, pHT, pOSyncContext, pChange); - LOG_SENSITIVE(10, "szXML=[%s]", szXML); - gboolean f; - char *szError=NULL; - char *szNewKey=NULL; - char *szNewHash=NULL; - if (changeType==OSYNC_CHANGE_TYPE_ADDED) { - f=pCalendar->AddEvent(szXML, &szNewKey, &szNewHash, &szError, &logFunction); - LOG(10, "Adding key=[%s] hash=[%s] to hash table", szNewKey, szNewHash); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - /* Inform OpenSync about the new ID */ - - osync_change_set_uid(pChange, szNewKey); - osync_change_set_hash(pChange, szNewHash); - osync_hashtable_update_change(pHT, pChange); - - GFREE(szNewKey); GFREE(szNewHash); - } - - else if (changeType==OSYNC_CHANGE_TYPE_MODIFIED) { - - f=pCalendar->ModifyEvent(szKey, szXML, &szNewHash, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - LOG(10, "Modifying key=[%s] hash=[%s] in hash table", szKey, szNewHash); - // %%%%%%%%%% %%%%%%%%%% - osync_change_set_hash(pChange, szNewHash); - osync_hashtable_update_change(pHT, pChange); - - } - - else if (changeType==OSYNC_CHANGE_TYPE_DELETED) { - f=pCalendar->DeleteEvent(szKey, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - osync_hashtable_update_change(pHT, pChange); - } - - LOG_EXIT(10, "TRUE"); - return TRUE; -} - -// ---------------------------------------- -/** @brief Commit change into thunderbird - - @param pThunderbirdAddressbook The address book to commit the change into - @param changeType The type of change as reported by OpenSync - @param szKey The key of the mozilla addressbook card - @param szXML The full XML representation of the mozilla addressbook card - @param pHT Pointer to hash table to update - @param pOSyncContext The context of the connect (e.g. to report error) - @param pChange The change we shall commit -**/ -static gboolean mozilla_sync_commit_addressbook_change( - ThunderbirdAddressbook *pThunderbirdAddressbook, - OSyncChangeType changeType, - const char *szKey, - const char *szXML, - OSyncHashTable *pHT, - OSyncContext *pOSyncContext, - OSyncChange *pChange) -{ - LOG_ENTRY(10, "(%p, %d, %s, %p, %p, %p)", pThunderbirdAddressbook, changeType, szKey, pHT, pOSyncContext, pChange); - LOG_SENSITIVE(10, "szXML=[%s]", szXML); - - gboolean f; - char *szError=NULL; - if (changeType==OSYNC_CHANGE_TYPE_ADDED) { - ThunderbirdCard *pCard = new ThunderbirdCard(); - f=pCard->CreateFromXML(szXML, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - f=pThunderbirdAddressbook->AddCard(pCard, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - char *szKey=pCard->GetKey(pThunderbirdAddressbook->myAddrDatabase, &szError, &logFunction); - if (!szKey) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - char *szHash=pCard->GetHash(&szError, &logFunction); - if (!szHash) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - osync_change_set_uid(pChange, szKey); - osync_change_set_hash(pChange, szHash); - osync_hashtable_update_change(pHT, pChange); - - GFREE(szKey); GFREE(szHash); - delete pCard; - - } - else if (changeType==OSYNC_CHANGE_TYPE_MODIFIED) { - // %%%%%%%%%% %%%%%%%%%% - char *szHash; - f=pThunderbirdAddressbook->ModifyCardFromXML(szKey, szXML, &szHash, &szError, &logFunction); - osync_change_set_hash(pChange, szHash); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - GFREE(szHash); - } - else if (changeType==OSYNC_CHANGE_TYPE_DELETED) { - f=pThunderbirdAddressbook->DeleteCard(szKey, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, szError); return FALSE; } - - osync_hashtable_update_change(pHT, pChange); - - } - - - LOG_EXIT(10, "TRUE"); - return TRUE; -} - -// ---------------------------------------- -/** @brief Commit change - - @param data The OSyncMozillaEnv - @param pOSyncObjTypeSink The ObjectType sink we shall commit the change into. - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) - @param pChange The change we shall commit -**/ -static void mozilla_sync_commit_change( - OSyncObjTypeSink* pOSyncObjTypeSink, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - OSyncChange *pChange, void *data) -{ - LOG_ENTRY(10, "(%p, %p, %p, %p)", pOSyncObjTypeSink, pOSyncPluginInfo, pOSyncContext, data); - - - // Log what we are about to do - OSyncChangeType changeType = osync_change_get_changetype(pChange); - const char *szKey = osync_change_get_uid(pChange); - switch (changeType) { - case OSYNC_CHANGE_TYPE_ADDED: - LOG(10, "Adding new"); - break; - case OSYNC_CHANGE_TYPE_MODIFIED: - LOG(10, "Modifying [%s]", szKey); - break; - case OSYNC_CHANGE_TYPE_DELETED: - LOG(10, "Deleting [%s]", szKey); - break; - default: - LOG_EXIT_REPORT_ERROR_SZ(0, pOSyncContext, "Unknown change type"); - return; - break; - } - - - OSyncMozillaDatabase *pOSyncMozillaDatabase = (OSyncMozillaDatabase*) data; - OSyncHashTable *pHT = osync_objtype_sink_get_hashtable(pOSyncObjTypeSink); - - gboolean f; - OSyncError *pError = NULL; - - OSyncData *pOSyncData=osync_change_get_data(pChange); - if (!pOSyncData) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, "Change has no data"); return; } - void *pVoidBuffer; - unsigned int uiSize; - osync_data_get_data(pOSyncData, (char**) &pVoidBuffer, &uiSize); - - OSyncObjFormat *pOSyncObjFormat=osync_change_get_objformat(pChange); - const char *szObjFormat=osync_objformat_get_name(pOSyncObjFormat); - LOG(10, "ObjFormat [%s]", szObjFormat); - - if (pOSyncMozillaDatabase->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_ADDRESSBOOK) { - if (changeType != OSYNC_CHANGE_TYPE_DELETED) - if (strcmp(szObjFormat, "xmlformat-contact")) - { LOG_EXIT_REPORT_ERROR_MSG(10, pOSyncContext, - "Invalid object format [%s]; expected [xmlformat-contact]", szObjFormat); return; } - char *szXML=NULL; - if ( (changeType==OSYNC_CHANGE_TYPE_ADDED) || (changeType==OSYNC_CHANGE_TYPE_MODIFIED) ) { - OSyncXMLFormat *pXMLFormat = (OSyncXMLFormat *)pVoidBuffer; - unsigned int size; - if (!osync_xmlformat_assemble(pXMLFormat, &szXML, &size, &pError)) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return; - } - if (!osync_xmlformat_sort(pXMLFormat, &pError)) { - LOG_EXIT_REPORT_ERROR_OSYNC(0, pOSyncContext, pError); - return; - } - LOG_SENSITIVE(1000, "%s", szXML); - } - ThunderbirdAddressbook *pAB = pOSyncMozillaDatabase->pThunderbirdAddressbook; - f=mozilla_sync_commit_addressbook_change(pAB, changeType, szKey, szXML, pHT, pOSyncContext, pChange); - } else if (pOSyncMozillaDatabase->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_CALENDAR) { - if (changeType != OSYNC_CHANGE_TYPE_DELETED) - if (strcmp(szObjFormat, "vevent20")) - { LOG_EXIT_REPORT_ERROR_MSG(10, pOSyncContext, - "Invalid object format [%s]; expected [vevent20]", szObjFormat); return; } - MozillaCalendar *pCal = pOSyncMozillaDatabase->pMozillaCalendar; - f=mozilla_sync_commit_calendar_change(pCal, changeType, szKey, (const char*) pVoidBuffer, pHT, pOSyncContext, pChange); - } else { - LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, "Unknown iMozillaDatabaseType"); return; - } - - if (f) osync_context_report_success(pOSyncContext); - LOG_EXIT(10, "void"); -} - -// ---------------------------------------- -/** @brief Called when sync is complete - - @param data The OSyncMozillaEnv - @param pOSyncObjTypeSink The ObjectType sink for which the sync is complete. - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context (e.g. to report error) -**/ -static void mozilla_sync_done(OSyncObjTypeSink* pOSyncObjTypeSink, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - void *data) -{ - LOG_ENTRY(10, "(%p, %p, %p)", pOSyncPluginInfo, pOSyncContext, data); - - OSyncMozillaDatabase *db = (OSyncMozillaDatabase*) data; - - OSyncError *error = NULL; - - LOG(10, "Updating anchor to value [%s]", db->szAnchorValue); - OSyncSinkStateDB *pStateDB = osync_objtype_sink_get_state_db(pOSyncObjTypeSink); - if (!osync_sink_state_set(pStateDB, db->szAnchorKey, db->szAnchorValue, &error)) { - osync_context_report_osyncerror(pOSyncContext, error); - LOG_EXIT_ERROR_OSYNC_SZ(10, &error, "Could not do osync_anchor_update"); - return; } - - osync_context_report_success(pOSyncContext); - LOG_EXIT(10, "void"); -} - - -// ---------------------------------------- -/** @brief Connect to sink - - @param data The OSyncMozillaEnv - @param pOSyncObjTypeSink The ObjectType sink we shall try to connect to. - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) -**/ -static void mozilla_sync_connect( - OSyncObjTypeSink* pOSyncObjTypeSink, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - void *data) -{ - - LOG_ENTRY(10, "(%p, %p, %p, %p)", pOSyncObjTypeSink, pOSyncPluginInfo, pOSyncContext, data); - - OSyncError *error = NULL; - - OSyncMozillaDatabase *db = (OSyncMozillaDatabase*) data; - OSyncMozillaEnv *env = db->pOSyncMozillaEnv; - - // TODO: Check the exact meaning of each timeout, and ajust accordingly - osync_objtype_sink_set_getchanges_timeout(pOSyncObjTypeSink, 4*60); // 4 minutes - osync_objtype_sink_set_commit_timeout(pOSyncObjTypeSink, 4*60); // 4 minutes - osync_objtype_sink_set_committedall_timeout(pOSyncObjTypeSink, 4*60); // 4 minutes - osync_objtype_sink_set_syncdone_timeout(pOSyncObjTypeSink, 4*60); // 4 minutes - osync_objtype_sink_set_read_timeout(pOSyncObjTypeSink, 4*60); // 4 minutes - - - osync_objtype_sink_enable_state_db(pOSyncObjTypeSink, TRUE); - OSyncSinkStateDB *pStateDB = osync_objtype_sink_get_state_db(pOSyncObjTypeSink); - osync_bool same; - if (!osync_sink_state_equal(pStateDB, db->szAnchorKey, db->szAnchorValue, &same, &error)) { - osync_context_report_osyncerror(pOSyncContext, error); - LOG_EXIT_ERROR_OSYNC_SZ(10, &error, "Could not do osync_anchor_compare"); - return; } - - if (same) { - LOG(10, "Anchor matches; so do not force slow sync"); - } else { - LOG(10, "Anchor does not match; so force slow sync"); - osync_context_report_slowsync(pOSyncContext); - } - - LOG(10, "Initializing Mozilla"); - gboolean f; - char *szError=NULL; - - f=mozilla_startup( - env->mozillaConfig.szProfileDirectory, - &(env->xpcomWasStarted), - &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return; } - - if (db->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_ADDRESSBOOK) { - LOG(10, "Initializing Thunderbird address book"); - db->pThunderbirdAddressbook=new ThunderbirdAddressbook; - f=db->pThunderbirdAddressbook->Init(env->mozillaConfig.szAddressbookFile, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return; } - } else if (db->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_CALENDAR) { - LOG(10, "Initializing Mozilla calendar"); - db->pMozillaCalendar=new MozillaCalendar; - f=db->pMozillaCalendar->Init(env->mozillaConfig.szCalendarId, &szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return; } - } else { - LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, "Unknown iMozillaDatabaseType"); return; - } - - osync_context_report_success(pOSyncContext); - LOG_EXIT(10, "void"); -} - -// ---------------------------------------- -/** @brief Disconnect from sink - - @param data The OSyncMozillaEnv - @param pOSyncObjTypeSink The ObjectType sink we shall disconnect from. - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param pOSyncContext The context of the connect (e.g. to report error) -**/ -static void mozilla_sync_disconnect( - OSyncObjTypeSink* pOSyncObjTypeSink, - OSyncPluginInfo *pOSyncPluginInfo, - OSyncContext *pOSyncContext, - void *data) -{ - LOG_ENTRY(10, "(%p, %p, %p, %p)", pOSyncObjTypeSink, pOSyncPluginInfo, pOSyncContext, data); - - OSyncMozillaDatabase *db = (OSyncMozillaDatabase*) data; - OSyncMozillaEnv *env = db->pOSyncMozillaEnv; - - LOG(10, "Finalizing Thunderbird"); - gboolean f; - char *szError=NULL; - f=db->pThunderbirdAddressbook->Close(&szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return; } - - if (env->xpcomWasStarted) { - LOG(10, "NOT finalizing Mozilla"); - } else { - LOG(10, "Finalizing Mozilla"); - f=mozilla_shutdown(&szError, &logFunction); - if (!f) { LOG_EXIT_REPORT_ERROR_SZ(10, pOSyncContext, szError); return; } - } - - osync_context_report_success(pOSyncContext); - - LOG_EXIT(10, "void"); - -} - - -// ---------------------------------------- -/** @brief Discover sinks - - Here we actually tell opensync which sinks are available. - For this plugin, we go through the list of databases and enable all, since all have been configured. - - @param data The OSyncMozillaEnv - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. - @param ppOSyncError Fill out in case of error -**/ -static osync_bool mozilla_sync_discover(OSyncPluginInfo *pOSyncPluginInfo, void *data, OSyncError **ppOSyncError) -{ - LOG_ENTRY(10, "(%p, %p, %p)", data, pOSyncPluginInfo, ppOSyncError); - - if (!data) { - LOG_EXIT(10, "We where called with data=NULL, so we can do nothing"); - return TRUE; - } - - OSyncMozillaEnv *env = (OSyncMozillaEnv *)data; - GList *o = env->pGListMozillaDatabases; - for (; o; o = o->next) { - OSyncMozillaDatabase *db = (OSyncMozillaDatabase*) o->data; - if (db->pOSyncObjTypeSink) { - const char *szSinkName = osync_objtype_sink_get_name(db->pOSyncObjTypeSink); - osync_objtype_sink_set_available(db->pOSyncObjTypeSink, TRUE); - } - } - - OSyncVersion *version = osync_version_new(ppOSyncError); - if (!version) - { LOG_EXIT_ERROR_OSYNC_SZ(10, ppOSyncError, "Could not get osync_version_new"); return FALSE; } - osync_version_set_plugin(version, "mozilla-sync"); - osync_version_set_vendor(version, "KaarPoSoft"); - //osync_version_set_modelversion(version, "version"); - //osync_version_set_firmwareversion(version, "firmwareversion"); - //osync_version_set_softwareversion(version, "softwareversion"); - //osync_version_set_hardwareversion(version, "hardwareversion"); - osync_plugin_info_set_version(pOSyncPluginInfo, version); - osync_version_unref(version); - - LOG_EXIT(10, "TRUE"); - return TRUE; -} - - -// ---------------------------------------- -/** @brief initialize use of the mozilla-sync plugin - - In initialize, we get and parse the config for the plugin. - Here we also must register all _possible_ objtype sinks - - @param pOSyncPlugin Not used - @param pOSyncPluginInfo On entry we have what we gave in get_sync_info. Now TODO - @param ppOSyncError Fill out in case of error -**/ static void *mozilla_sync_initialize(OSyncPlugin *pOSyncPlugin, OSyncPluginInfo *pOSyncPluginInfo, OSyncError **ppOSyncError) { - LOG_ENTRY(10, "(%p, %p, %p)",pOSyncPlugin, pOSyncPluginInfo, ppOSyncError); - - LOG(10, "Initial"); - - OSyncPluginConfig *pOSyncPluginConfig = NULL; - - LOG(10, "Initializing OSyncMozillaEnv"); - OSyncMozillaEnv *pOSyncMozillaEnv = (OSyncMozillaEnv*)osync_try_malloc0(sizeof(OSyncMozillaEnv), ppOSyncError); - if (!pOSyncMozillaEnv) - { LOG_EXIT_ERROR_OSYNC_SZ(10, ppOSyncError, "Could not create OSyncMozillaEnv"); return NULL; } - memset(pOSyncMozillaEnv, 0, sizeof(OSyncMozillaEnv)); - pOSyncMozillaEnv->pOSyncFormatEnv = osync_plugin_info_get_format_env(pOSyncPluginInfo); - - LOG(10, "Reading configuration"); - - pOSyncPluginConfig=osync_plugin_info_get_config(pOSyncPluginInfo); - gboolean f; - char *szError=NULL; - f=mozilla_config_extract(pOSyncPluginConfig, &(pOSyncMozillaEnv->mozillaConfig), &szError, &logFunction); - if (!f) { LOG_EXIT_ERROR_SZ(10, szError); return NULL; } - f=mozilla_config_check( &(pOSyncMozillaEnv->mozillaConfig), &szError, &logFunction); - if (!f) { LOG_EXIT_ERROR_SZ(10, szError); return NULL; } - - - osync_plugin_set_initialize_timeout(pOSyncPlugin, 3*60); // 3 minutes - osync_plugin_set_finalize_timeout(pOSyncPlugin, 3*60); // 3 minutes - - OSyncMozillaDatabase *pOSyncMozillaDatabase; - - if (pOSyncMozillaEnv->mozillaConfig.szAddressbookFile) { - LOG(10, "Creating OSyncMozillaDatabase for Thunderbird addressbook"); - pOSyncMozillaDatabase=(OSyncMozillaDatabase*)osync_try_malloc0(sizeof(OSyncMozillaDatabase), ppOSyncError); - memset(pOSyncMozillaDatabase, 0, sizeof(OSyncMozillaDatabase)); - if (!pOSyncMozillaDatabase) - { LOG_EXIT_ERROR_OSYNC_SZ(10, ppOSyncError, "Could not create OSyncMozillaDatabase"); return NULL; } - - - pOSyncMozillaDatabase->iMozillaDatabaseType=MOZILLA_DATABASE_TYPE_ADDRESSBOOK; - pOSyncMozillaDatabase->pOSyncMozillaEnv=pOSyncMozillaEnv; - pOSyncMozillaDatabase->szAnchorKey="ThunderbirdAddressbook"; - pOSyncMozillaDatabase->szAnchorValue= - g_strjoin(G_DIR_SEPARATOR_S, pOSyncMozillaDatabase->pOSyncMozillaEnv->mozillaConfig.szProfileDirectory, - pOSyncMozillaDatabase->pOSyncMozillaEnv->mozillaConfig.szAddressbookFile, NULL); - pOSyncMozillaDatabase->szObjtype=g_strdup("contact"); - pOSyncMozillaDatabase->szObjformat=g_strdup("xmlformat-contact"); - - pOSyncMozillaEnv->pGListMozillaDatabases=NULL; - pOSyncMozillaEnv->pGListMozillaDatabases=g_list_append(pOSyncMozillaEnv->pGListMozillaDatabases, pOSyncMozillaDatabase); - } - - - if (pOSyncMozillaEnv->mozillaConfig.szCalendarId) { - LOG(10, "Creating OSyncMozillaDatabase for Sunbird/Lightning calendar"); - pOSyncMozillaDatabase=(OSyncMozillaDatabase*)osync_try_malloc0(sizeof(OSyncMozillaDatabase), ppOSyncError); - memset(pOSyncMozillaDatabase, 0, sizeof(OSyncMozillaDatabase)); - if (!pOSyncMozillaDatabase) - { LOG_EXIT_ERROR_OSYNC_SZ(10, ppOSyncError, "Could not create OSyncMozillaDatabase"); return NULL; } - - pOSyncMozillaDatabase->iMozillaDatabaseType=MOZILLA_DATABASE_TYPE_CALENDAR; - pOSyncMozillaDatabase->pOSyncMozillaEnv=pOSyncMozillaEnv; - pOSyncMozillaDatabase->szAnchorKey="MozillaCalendar"; - pOSyncMozillaDatabase->szAnchorValue= - g_strjoin(G_DIR_SEPARATOR_S, pOSyncMozillaDatabase->pOSyncMozillaEnv->mozillaConfig.szProfileDirectory, - pOSyncMozillaDatabase->pOSyncMozillaEnv->mozillaConfig.szCalendarId, NULL); - pOSyncMozillaDatabase->szObjtype=g_strdup("event"); - pOSyncMozillaDatabase->szObjformat=g_strdup("vevent20"); - - pOSyncMozillaEnv->pGListMozillaDatabases=g_list_append(pOSyncMozillaEnv->pGListMozillaDatabases, pOSyncMozillaDatabase); - } - - - /* Now we register the objtypes that we can sync. */ - LOG(10, "Configuring formats for OSyncMozillaDatabases"); - int i=0; - GList *o = pOSyncMozillaEnv->pGListMozillaDatabases; - for (; o; o = o->next, i++) { - LOG(10, "Configuring formats for OSyncMozillaDatabase #%d", i); - OSyncMozillaDatabase *db = (OSyncMozillaDatabase*) o->data; - - OSyncObjTypeSink *sink = osync_plugin_info_find_objtype(pOSyncPluginInfo, db->szObjtype); - if (!sink) { - LOG(0, "Group is not configured to handle object type [%s], although mozilla-sync is!", db->szObjtype); - osync_objtype_sink_unref(sink); - continue; - } - - // Request state database functionality from OpenSync - osync_objtype_sink_enable_state_db(sink, TRUE); - - // Request a hashtable from OpenSync - osync_objtype_sink_enable_hashtable(sink, TRUE); - - // TODO: Check the exact meaning of each timeout, and ajust accordingly - osync_objtype_sink_set_connect_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_disconnect_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_getchanges_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_commit_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_committedall_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_syncdone_timeout(sink, 5*60); // 4 minutes - osync_objtype_sink_set_read_timeout(sink, 5*60); // 4 minutes - - db->pOSyncObjTypeSink = sink; - - osync_objtype_sink_set_connect_func(sink, mozilla_sync_connect); - osync_objtype_sink_set_get_changes_func(sink, mozilla_sync_get_changes); - osync_objtype_sink_set_commit_func(sink, mozilla_sync_commit_change); - osync_objtype_sink_set_sync_done_func(sink, mozilla_sync_done); - - osync_objtype_sink_set_userdata(sink, db); - - } - - LOG_EXIT(10, "(%p)", pOSyncMozillaEnv); - return (void*) pOSyncMozillaEnv; - return NULL; } - -void freeMozillaDatabase(void* voidOSyncMozillaDatabase, void* notused) { - OSyncMozillaDatabase *pDB=(OSyncMozillaDatabase*) voidOSyncMozillaDatabase; - - if (pDB->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_ADDRESSBOOK) { - if (pDB->pThunderbirdAddressbook) delete pDB->pThunderbirdAddressbook; - } else if (pDB->iMozillaDatabaseType==MOZILLA_DATABASE_TYPE_CALENDAR) { - if (pDB->pMozillaCalendar) delete pDB->pMozillaCalendar; - } -} - - -// ---------------------------------------- -/** @brief finalize use of the mozilla-sync plugin - - Simply free the mozilla-sync environment - - @param data The OSyncMozillaEnv -**/ static void mozilla_sync_finalize(void *data) { - LOG_ENTRY(10, "(%p)", data); - OSyncMozillaEnv *pOSyncMozillaEnv = (OSyncMozillaEnv*) data; - if (pOSyncMozillaEnv) { - // Hope OpenSync is freeing pOSyncFormatEnv - g_list_foreach(pOSyncMozillaEnv->pGListMozillaDatabases, freeMozillaDatabase, NULL); - free(pOSyncMozillaEnv); - } - LOG_EXIT(10, ""); } - // ---------------------------------------- /** @brief register the mozilla-sync plugin @@ -1144,14 +116,11 @@ osync_plugin_set_name(pOSyncPlugin, "mozilla-sync"); osync_plugin_set_longname(pOSyncPlugin, "Mozilla Synchronization Plugin"); osync_plugin_set_description(pOSyncPlugin, "Synchronize with Mozilla (moz-abdirectory)"); + osync_plugin_set_start_type(pOSyncPlugin, OSYNC_START_TYPE_EXTERNAL); osync_plugin_set_config_type(pOSyncPlugin, OSYNC_PLUGIN_NEEDS_CONFIGURATION); osync_plugin_set_initialize(pOSyncPlugin, mozilla_sync_initialize); osync_plugin_set_finalize(pOSyncPlugin, mozilla_sync_finalize); - osync_plugin_set_discover(pOSyncPlugin, mozilla_sync_discover); - - osync_plugin_set_initialize_timeout(pOSyncPlugin, 5*60); // 3 minutes - osync_plugin_set_finalize_timeout(pOSyncPlugin, 5*60); // 3 minutes osync_bool f; f=osync_plugin_env_register_plugin(pOSyncPluginEnv, pOSyncPlugin, ppOSyncError); @@ -1177,3 +146,4 @@ } } // extern "C" + |