From: <svn...@op...> - 2010-11-03 08:11:20
|
Author: deloptes Date: Wed Nov 3 09:11:10 2010 New Revision: 6151 URL: http://www.opensync.org/changeset/6151 Log: - some progress on syncing multiple objects KNOWN ISSUE: 1. it behaves strange when rejecting forecast 2. when synced it suggests modification to the member on the next sync TODO: implement search or some kind of check on item and/or UID (remoteId) instead only on remoteId Modified: plugins/akonadi-sync/trunk/src/akonadi-sync plugins/akonadi-sync/trunk/src/akonadi_opensync.cpp plugins/akonadi-sync/trunk/src/akonadisink.cpp plugins/akonadi-sync/trunk/src/akonadisink.h plugins/akonadi-sync/trunk/src/datasink.cpp plugins/akonadi-sync/trunk/src/datasink.h plugins/akonadi-sync/trunk/src/sinkbase.cpp plugins/akonadi-sync/trunk/src/sinkbase.h Modified: plugins/akonadi-sync/trunk/src/akonadi-sync ============================================================================== --- plugins/akonadi-sync/trunk/src/akonadi-sync Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/akonadi-sync Wed Nov 3 09:11:10 2010 (r6151) @@ -41,20 +41,13 @@ <Url>default</Url> </Resource> <Resource> - <Enabled>0</Enabled> + <Enabled>1</Enabled> <Formats> <Format> - <Name>vjournal</Name> + <Name>vnote11</Name> </Format> - </Formats> - <ObjType>note</ObjType> - <Url>default</Url> - </Resource> - <Resource> - <Enabled>0</Enabled> - <Formats> <Format> - <Name>vnote11</Name> + <Name>vjournal</Name> </Format> </Formats> <ObjType>note</ObjType> Modified: plugins/akonadi-sync/trunk/src/akonadi_opensync.cpp ============================================================================== --- plugins/akonadi-sync/trunk/src/akonadi_opensync.cpp Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/akonadi_opensync.cpp Wed Nov 3 09:11:10 2010 (r6151) @@ -55,21 +55,22 @@ static void* akonadi_initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError **error) { - kDebug(); osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, plugin, info, error); if ( !app ) app = new QCoreApplication( fakeArgc, fakeArgv ); if ( !kcd ) - kcd = new KComponentData( "akonadi_opensync" ); + kcd = new KComponentData( "akonadi-sync" ); + kDebug(); // main sink AkonadiSink *mainSink = new AkonadiSink(); - if ( !mainSink->initialize( plugin, info, error ) ) { + if ( !mainSink->initialize(plugin, info, error ) ) { delete mainSink; osync_trace(TRACE_EXIT_ERROR, "%s: NULL", __func__); return 0; } +// QList<DataSink*> sinkList; // object type sinks // http://www.opensync.org/wiki/devel/pluginPortingGuide-0.40 @@ -77,7 +78,8 @@ // --------------------------------- // osync_plugin_get_config_type(); - unsigned int objects_supported = 4; +// unsigned int objects_supported = 4; + QList<DataSink*> sinkList; OSyncList *s = NULL, *list = osync_plugin_info_get_objtype_sinks(info); for ( s = list; s; s = s->next ) { @@ -99,17 +101,21 @@ continue; // there might be something more intelligent to check how to return below - if ( !ds->initialize( plugin, info, sink, error ) ) { - osync_objtype_sink_set_enabled(sink, false); + if ( !ds->initialize(plugin, info, sink, error ) ) { +// osync_objtype_sink_set_enabled(sink, false); osync_objtype_sink_set_available(sink, false); delete ds; - objects_supported--; - } +// objects_supported--; + continue; + } +// osync_objtype_sink_set_enabled(sink, true); +// mainSink->addSink(ds); + sinkList.append(ds); + osync_objtype_sink_set_available(sink, true); } - // if we support at least one object return the mainSink - if ( objects_supported >= 1 ) { + if ( ! sinkList.isEmpty() ) { osync_trace(TRACE_EXIT, " %s: %p", __func__, mainSink); return mainSink; } @@ -282,12 +288,8 @@ osync_objtype_sink_set_available(sink, true); } else if ( ! strcmp(myType,"note") ) { - if ( ! testSupport(sink, config, "application/x-vnd.akonadi.calendar.journal", error) ) - osync_objtype_sink_set_available(sink, false); - else - osync_objtype_sink_set_available(sink, true); - - if ( ! testSupport(sink, config, "application/x-vnd.kde.notes", error) ) + if ( ! testSupport(sink, config, "application/x-vnd.akonadi.calendar.journal", error) + && ! testSupport(sink, config, "application/x-vnd.kde.notes", error) ) osync_objtype_sink_set_available(sink, false); else osync_objtype_sink_set_available(sink, true); @@ -304,7 +306,7 @@ // set information about the peer (KDE itself) { OSyncVersion *version = osync_version_new(error); - osync_version_set_plugin(version, "Akonadi-sync"); + osync_version_set_plugin(version, "akonadi-sync"); osync_version_set_softwareversion(version, "0.40"); osync_version_set_identifier(version, "akonadi-sync"); osync_plugin_info_set_version(info, version); @@ -319,8 +321,8 @@ { osync_trace(TRACE_ENTRY, "%s(%p)", __func__, userdata); kDebug(); - AkonadiSink *sink = reinterpret_cast<AkonadiSink*>( userdata ); - sink->disconnect(); + AkonadiSink *mainSink = reinterpret_cast<AkonadiSink*>( userdata ); + mainSink->disconnect(); delete kcd; kcd = 0; delete app; @@ -340,13 +342,14 @@ } osync_plugin_set_name(plugin, "akonadi-sync"); - osync_plugin_set_longname(plugin, "Akonadi"); + osync_plugin_set_longname(plugin, "Akonadi OpenSync Plugin"); osync_plugin_set_description(plugin, "Plugin to synchronize with Akonadi"); osync_plugin_set_config_type(plugin, OSYNC_PLUGIN_OPTIONAL_CONFIGURATION); osync_plugin_set_initialize(plugin, akonadi_initialize); - osync_plugin_set_finalize_timeout(plugin, 5); osync_plugin_set_finalize(plugin, akonadi_finalize); + osync_plugin_set_finalize_timeout(plugin, 5); osync_plugin_set_discover(plugin, akonadi_discover); + osync_plugin_set_discover_timeout(plugin, 5); osync_plugin_set_start_type(plugin, OSYNC_START_TYPE_PROCESS); if ( ! osync_plugin_env_register_plugin(env, plugin, error) ) { Modified: plugins/akonadi-sync/trunk/src/akonadisink.cpp ============================================================================== --- plugins/akonadi-sync/trunk/src/akonadisink.cpp Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/akonadisink.cpp Wed Nov 3 09:11:10 2010 (r6151) @@ -1,8 +1,5 @@ /* Copyright (c) 2008 Volker Krause <vk...@kd...> - Copyright (c) 2010 Emanoil Kotsev <del...@ya...> - - $Id$ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by @@ -26,53 +23,57 @@ #include <KDebug> -#include <opensync/opensync-plugin.h> -#include <opensync/opensync-data.h> -#include <opensync/opensync-format.h> - AkonadiSink::AkonadiSink() : - SinkBase( Connect ) + SinkBase( Connect ) { } AkonadiSink::~AkonadiSink() { +// Q_FOREACH (DataSink *ds, m_SinkList) { +// delete ds; +// } +// m_SinkList.clear(); } bool AkonadiSink::initialize(OSyncPlugin * plugin, OSyncPluginInfo * info, OSyncError ** error) { - Q_UNUSED( plugin ); - kDebug(); - - if ( !Akonadi::Control::start() ) - return false; - - OSyncObjTypeSink *sink = osync_objtype_main_sink_new( error ); - if (!sink) { - - kDebug() << "No sink "; - return false; - } - osync_plugin_info_set_main_sink( info, sink ); - wrapSink( sink ); - osync_objtype_sink_set_userdata(sink, this); - - return true; + Q_UNUSED(plugin) + kDebug(); + OSyncObjTypeSink *sink = osync_objtype_main_sink_new( error ); + wrapSink( sink ); + osync_plugin_info_set_main_sink( info, sink ); + osync_objtype_sink_unref(sink); + return true; } +// void AkonadiSink::setSink(OSyncObjTypeSink *sink) +// { +// +// } + void AkonadiSink::connect() { - osync_trace(TRACE_ENTRY, "%s(%p, %p)", __PRETTY_FUNCTION__, pluginInfo(), context()); - kDebug(); + osync_trace(TRACE_ENTRY, "%s(%p, %p)", __PRETTY_FUNCTION__, pluginInfo(), context()); + kDebug(); +// OSyncError *oerror = 0; + if ( !Akonadi::Control::start() ) { +// osync_error_set_type(&oerror,OSYNC_ERROR_NO_CONNECTION); + kDebug() << "Could not start Akonadi." ; + error( OSYNC_ERROR_NO_CONNECTION, "Could not start Akonadi." ); + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __PRETTY_FUNCTION__, "Could not start Akonadi."); + return; + } - if ( !Akonadi::Control::start() ) { - error( OSYNC_ERROR_NO_CONNECTION, "Could not start Akonadi." ); - osync_trace(TRACE_EXIT_ERROR, "%s: %s", __PRETTY_FUNCTION__, "Could not start Akonadi."); - return; - } - success(); - osync_trace(TRACE_EXIT, "%s", __PRETTY_FUNCTION__); + success(); + osync_trace(TRACE_EXIT, "%s", __PRETTY_FUNCTION__); } +// void AkonadiSink::addSink(DataSink* ds) +// { +// Q_ASSERT(ds == 0 ); +// m_SinkList.append(ds); +// } + #include "akonadisink.moc" Modified: plugins/akonadi-sync/trunk/src/akonadisink.h ============================================================================== --- plugins/akonadi-sync/trunk/src/akonadisink.h Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/akonadisink.h Wed Nov 3 09:11:10 2010 (r6151) @@ -1,8 +1,5 @@ /* Copyright (c) 2008 Volker Krause <vk...@kd...> - Copyright (c) 2010 Emanoil Kotsev <del...@ya...> - - $Id$ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by @@ -22,7 +19,7 @@ #ifndef AKONADISINK_H #define AKONADISINK_H - +// #include "datasink.h" #include "sinkbase.h" /** @@ -30,16 +27,20 @@ */ class AkonadiSink : public SinkBase { - Q_OBJECT + Q_OBJECT -public: + public: AkonadiSink(); ~AkonadiSink(); bool initialize( OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError **error ); void connect(); - + +// void addSink(DataSink* ds); + +// private: +// QList<DataSink*> m_SinkList; }; #endif Modified: plugins/akonadi-sync/trunk/src/datasink.cpp ============================================================================== --- plugins/akonadi-sync/trunk/src/datasink.cpp Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/datasink.cpp Wed Nov 3 09:11:10 2010 (r6151) @@ -25,6 +25,11 @@ #include <akonadi/collectionfetchjob.h> #include <akonadi/collectionfetchscope.h> #include <akonadi/itemdeletejob.h> +#include <akonadi/itemmodifyjob.h> +#include <akonadi/itemcreatejob.h> +#include <akonadi/itemfetchscope.h> +#include <akonadi/mimetypechecker.h> + // calendar includes #include <kcal/incidence.h> @@ -53,14 +58,7 @@ m_Format("default"), m_Url("default") { - kDebug() << "Constr.objtype:" << type; m_type = type; - - m_isEvent = ( type == DataSink::Calendars ) ? true : false; - m_isContact = ( type == DataSink::Contacts ) ? true : false; - m_isNote = ( type == DataSink::Notes ) ? true : false; - m_isTodo = ( type == DataSink::Todos ) ? true : false; - } DataSink::~DataSink() @@ -70,7 +68,8 @@ bool DataSink::initialize ( OSyncPlugin * plugin, OSyncPluginInfo * info, OSyncObjTypeSink *sink, OSyncError ** error ) { - kDebug() << "initializing" << osync_objtype_sink_get_name ( sink ); + m_Name = osync_objtype_sink_get_name ( sink ); + kDebug() << "initializing" << m_Name; Q_UNUSED ( plugin ); // Q_UNUSED ( info ); Q_UNUSED ( error ); @@ -79,7 +78,7 @@ OSyncPluginConfig *config = osync_plugin_info_get_config ( info ); if ( !config ) { - osync_error_set ( error, OSYNC_ERROR_GENERIC, "Unable to get config." ); +// osync_error_set ( error, OSYNC_ERROR_GENERIC, "Unable to get config." ); return false; } @@ -99,6 +98,8 @@ OSyncObjFormatSink *objformatsink = ( OSyncObjFormatSink * ) r->data; const char* tobjformat = osync_objformat_sink_get_objformat ( objformatsink ); + // NOTE "application/x-vnd.kde.contactgroup" is used for contact groups ... we can use it probably later + // TODO how can I negotiate format ... is this here enough? switch ( m_type ) { @@ -109,6 +110,7 @@ // always prefer newer format if ( !strcmp ( "vcard30", tobjformat ) ) m_Format = "vcard30"; + m_MimeType = "text/directory"; break; } case Calendars: @@ -118,14 +120,19 @@ // always prefer newer format if ( !strcmp ( "vevent20", tobjformat ) ) m_Format = "vevent20"; + m_MimeType = "application/x-vnd.akonadi.calendar.event"; break; } case Notes: { - if ( !strcmp ( "vnote11", tobjformat ) ) + if ( !strcmp ( "vnote11", tobjformat ) ) { m_Format = "vnote11"; - if ( !strcmp ( "vjournal", tobjformat ) ) + m_MimeType = "application/x-vnd.kde.notes"; + } + if ( !strcmp ( "vjournal", tobjformat ) ) { m_Format = "vjournal"; + m_MimeType = "application/x-vnd.akonadi.calendar.journal"; + } break; } case Todos: @@ -135,6 +142,7 @@ // always prefer newer format if ( !strcmp ( "vtodo20", tobjformat ) ) m_Format = "vtodo20"; + m_MimeType = "application/x-vnd.akonadi.calendar.todo"; break; } @@ -151,14 +159,7 @@ osync_objtype_sink_set_userdata ( sink, this ); wrapSink ( sink ); -// osync_objtype_sink_set_enabled ( sink, true ); -// osync_objtype_sink_set_available ( sink, true ); -// set hashtable - OSyncHashTable *hashtable = NULL; - if ( osync_objtype_sink_load_hashtable ( sink , info, error ) ) - hashtable = osync_objtype_sink_get_hashtable(sink); - if ( ! hashtable ) - osync_objtype_sink_enable_hashtable ( sink , true ); + osync_objtype_sink_enable_hashtable ( sink , true ); return true; } @@ -189,6 +190,7 @@ if ( !hashtable ) { kDebug() << "No hashtable"; + error ( OSYNC_ERROR_FILE_NOT_FOUND, "No hashtable"); osync_trace ( TRACE_EXIT_ERROR, "%s: %s", __PRETTY_FUNCTION__, osync_error_print ( &oerror ) ); return; } @@ -235,16 +237,15 @@ { kDebug(); kDebug() << "retrieved" << items.count() << "items"; - OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env ( pluginInfo() ); - OSyncObjFormat *format = osync_format_env_find_objformat ( formatenv, m_Format.toLatin1().data() ); - + Akonadi::MimeTypeChecker checker; + checker.addWantedMimeType( m_MimeType ); Q_FOREACH ( const Item& item, items ) { - if ( getMimeWithFormat(format) != item.mimeType() ) { + // report only items of given mimeType + if ( checker.isWantedItem( item ) ) + reportChange ( item ); + else kDebug() << item.mimeType() << "skipped!"; - continue; - } - reportChange ( item ); } kDebug() << "slotItemsReceived done"; } @@ -258,7 +259,11 @@ kDebug() << "Revision:" << item.revision() << "\n"; kDebug() << "StorageCollectionId:" << item.storageCollectionId() << "\n"; kDebug() << "Url:" << item.url() << "\n"; - kDebug() << "item.payloadData().data()" << "\n" << item.payloadData().data() ; + + QString hash = getHash( item.id(), item.revision() ) ; + + kDebug() << hash; + kDebug() << "item.payloadData().data()" << "\n" << item.payloadData().data(); OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env ( pluginInfo() ); @@ -274,14 +279,9 @@ } osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); - osync_change_set_hash ( change, QString::number( item.revision() ).toLatin1() ); +// osync_change_set_uid ( change, QString::number( item.id() ).toLatin1() ); + osync_change_set_hash ( change, getHash( item.id(), item.revision() ).toLatin1().data() ); OSyncChangeType changetype = osync_hashtable_get_changetype(hashtable, change); - if ( ! changetype ) { - kDebug() << "No change type"; - osync_change_unref ( change ); - warning ( error ); - return; - } osync_change_set_changetype(change, changetype); osync_hashtable_update_change ( hashtable, change ); @@ -296,23 +296,33 @@ // should be queried later in a "get_data" function OSyncObjFormat *format = osync_format_env_find_objformat ( formatenv, m_Format.toLatin1().data() ); - char *cvtToString = item.payloadData().data(); - OSyncData *odata = osync_data_new ( cvtToString , strlen(cvtToString), format, &error ); + int newDataSize = item.payloadData().size(); + char newData[newDataSize]; + memcpy(newData, item.payloadData().data(), newDataSize); +// OSyncData *odata = osync_data_new ( item.payloadData().data() , item.payloadData().size(), format, &error ); + OSyncData *odata = osync_data_new ( newData, newDataSize, format, &error ); if ( !odata ) { - osync_data_unref ( odata ); osync_change_unref(change); warning(error); return; } - osync_data_set_objtype( odata, osync_objtype_sink_get_name( sink() ) ); - osync_change_set_data ( change, odata ); + + osync_error_unref(&error); + + kDebug()<< "change" << change; + kDebug()<< "data " << odata; + kDebug()<< "context" << context(); - osync_data_unref ( odata ); +// do I need this here + osync_data_set_objtype( odata, m_Name.toLatin1().data() ); + osync_change_set_data ( change, odata ); +// not sure but it gets probably delete together with data +// osync_data_unref ( odata ); osync_context_report_change ( context(), change ); - osync_error_unref(&error); + osync_change_unref ( change ); } @@ -328,10 +338,7 @@ { QString uid ( ( char * ) u->data ); kDebug() << "going to delete with uid:" << uid; -// if ( getMimeWithFormat(format) != ) { -// kDebug() << item.mimeType() << "skipped!"; -// return; -// } + OSyncChange *change = osync_change_new ( &error ); if ( !change ) { @@ -353,43 +360,22 @@ continue; } - osync_data_set_objtype( data, osync_objtype_sink_get_name( sink() ) ); + osync_data_set_objtype( data, m_Name.toLatin1().data() ); osync_change_set_data( change, data ); osync_data_unref(data); + osync_hashtable_update_change ( hashtable, change ); osync_context_report_change ( context(), change ); - osync_hashtable_update_change ( hashtable, change ); +// osync_hashtable_update_change ( hashtable, change ); osync_change_unref ( change ); } - osync_list_free ( u ); osync_list_free ( uids ); osync_error_unref(&error); - success(); kDebug() << "got all changes success()."; -} - -// NOTE "application/x-vnd.kde.contactgroup" is used for contact groups ... we can use it probably later - -QString DataSink::getMimeWithFormat ( OSyncObjFormat * format ) { - const char* name = osync_objformat_get_name (format); - if (!strcmp(name,"vcard21") || !strcmp(name,"vcard30") ) - return "text/directory"; - else if (!strcmp(name,"vevent10") || !strcmp(name,"vevent20") ) -// return "text/calendar"; - return "application/x-vnd.akonadi.calendar.event"; - else if (!strcmp(name,"vtodo10") || !strcmp(name,"vtodo20")) -// return "text/calendar"; - return "application/x-vnd.akonadi.calendar.todo"; - else if (!strcmp(name,"vnote11") ) - return "application/x-vnd.kde.notes"; - else if (!strcmp(name,"vjournal")) -// return "text/calendar"; - return "application/x-vnd.akonadi.calendar.journal"; - else - return NULL; + success(); } void DataSink::commit ( OSyncChange *change ) @@ -397,10 +383,16 @@ kDebug(); OSyncHashTable *hashtable = osync_objtype_sink_get_hashtable ( sink() ); - QString mimeType = getMimeWithFormat(osync_change_get_objformat(change)); - QString id = QString::fromLatin1 ( osync_change_get_uid ( change ) ); - kDebug() << "change uid:" << id; +// kDebug() << "change hash:" << osync_change_get_hash ( change ) ; + + QString remoteId = QString::fromLatin1 ( osync_change_get_uid ( change ) ); + QString hash = QString::fromLatin1 ( osync_change_get_hash ( change ) ); + + int id = idFromHash(hash); + kDebug() << "change id:" << id; + kDebug() << "change uid:" << remoteId; + kDebug() << "change hash:" << hash; kDebug() << "objform:" << osync_objformat_get_name ( osync_change_get_objformat ( change ) ); kDebug(); @@ -418,25 +410,27 @@ Item item; char *plain = 0; osync_data_get_data ( osync_change_get_data ( change ), &plain, /*size*/0 ); - QString str = QString::fromLatin1 ( plain ); - setPayload ( &item, mimeType, str ); - item.setRemoteId( id ); + QString str = QString::fromUtf8( plain ); +// QString str = QString::fromLatin1( plain ); +// QString str = QString::fromLocal8Bit( plain ); + kDebug() << "data: " << str; + setPayload ( &item, str ); + item.setRemoteId( remoteId ); ItemCreateJob *job = new Akonadi::ItemCreateJob ( item, col ); if ( ! job->exec() ) { error( OSYNC_ERROR_GENERIC, "Unable to create job for item."); return; - } - - item = job->item(); // handle !job->exec in return too.. - if ( ! item.isValid() ) { + } else { + item = job->item(); // handle !job->exec in return too.. + if ( ! item.isValid() ) { error( OSYNC_ERROR_GENERIC, "Unable to fetch item."); return; - } - osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); - osync_change_set_hash ( change, QString::number ( item.revision() ).toLatin1().data() ); -// osync_change_set_hash(change, "0"); - kDebug() << "data" << str; + } + osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); + osync_change_set_hash ( change, getHash( item.id(), item.revision() ).toLatin1().data() ); + + } break; } @@ -444,46 +438,48 @@ { char *plain = 0; osync_data_get_data ( osync_change_get_data ( change ), &plain, /*size*/0 ); - QString str = QString::fromLatin1 ( plain ); - Item item = fetchItem ( id ); + QString str = QString::fromUtf8( plain ); + kDebug() << "data" << str; + Item item = fetchItem ( remoteId ); if ( ! item.isValid() ) { error( OSYNC_ERROR_GENERIC, "Unable to fetch item."); return; } - setPayload ( &item, mimeType, str ); + setPayload ( &item, str ); ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob ( item ); if ( ! modifyJob->exec() ) { error ( OSYNC_ERROR_GENERIC, "Unable to fetch item."); return; - } - - item = modifyJob->item(); - if ( ! item.isValid() ) { + } else { + item = modifyJob->item(); + if ( ! item.isValid() ) { error( OSYNC_ERROR_GENERIC, "Unable to fetch item."); return; - } - osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); - osync_change_set_hash ( change, QString::number ( item.revision() ).toLatin1().data() ); -// osync_change_set_hash(change, "0"); - kDebug() << "data" << str; + } + osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); + osync_change_set_hash ( change, getHash( item.id(), item.revision() ).toLatin1().data() ); + + } break; } case OSYNC_CHANGE_TYPE_DELETED: { - Item item = fetchItem ( id ); + Item item = fetchItem ( remoteId ); if ( ! item.isValid() ) { - error( OSYNC_ERROR_GENERIC, "Unable to fetch item"); - return; // FIXME break or return? +// error( OSYNC_ERROR_GENERIC, "Unable to fetch item"); + break; // FIXME break or return? } ItemDeleteJob *job = new ItemDeleteJob( item ); if ( ! job->exec() ) { - error( OSYNC_ERROR_GENERIC, "Unable to delete item"); - return; +// error( OSYNC_ERROR_GENERIC, "Unable to delete item"); + break; } + osync_change_set_uid ( change, item.remoteId().toLatin1().data() ); + kDebug() << "deleted: (testing skipped)" << remoteId; break; } @@ -500,14 +496,15 @@ } osync_hashtable_update_change ( hashtable, change ); + success(); } -bool DataSink::setPayload ( Item *item, const QString mimeType, const QString &str ) +bool DataSink::setPayload ( Item *item, const QString &str ) { kDebug(); - item->setMimeType ( mimeType ); - kDebug()<< "To mimetype: " << mimeType; + item->setMimeType ( m_MimeType ); + kDebug()<< "To mimetype: " << m_MimeType; switch ( m_type ) { case Contacts: @@ -554,7 +551,26 @@ return true; } -const Item DataSink::fetchItem ( const QString& id ) +const Item DataSink::fetchItem ( int id ) +{ + kDebug(); + ItemFetchJob *fetchJob = new ItemFetchJob( Item( id ) ); + fetchJob->fetchScope().fetchFullPayload(); + + if( fetchJob->exec() ) { + foreach ( const Item &item, fetchJob->items() ) { + if( item.id() == id ) { + kDebug() << "got item"; + return item; + } + } + } + + // no such item found? + return Item(); +} + +const Item DataSink::fetchItem ( const QString& remoteId ) { kDebug(); @@ -562,13 +578,12 @@ fetchJob->fetchScope().fetchFullPayload(); if ( fetchJob->exec() ) foreach ( const Item &item, fetchJob->items() ) - if ( !strcmp(item.remoteId().toLatin1().data(), id.toLatin1().data() )) + if ( item.remoteId() == remoteId ) return item; // no such item found? // this will be handled as invalid item return Item(); } - void DataSink::syncDone() { kDebug() << "sync for sink member done"; @@ -581,4 +596,14 @@ success(); } +QString DataSink::getHash( int id, int rev ) { + return QString::number(id) + "-" + QString::number( rev ) ; +} + +int DataSink::idFromHash( const QString hash) { + QString str = hash; + str.remove(QRegExp("-.*")); + kDebug() << str; + return str.toInt(); +} #include "datasink.moc" Modified: plugins/akonadi-sync/trunk/src/datasink.h ============================================================================== --- plugins/akonadi-sync/trunk/src/datasink.h Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/datasink.h Wed Nov 3 09:11:10 2010 (r6151) @@ -1,8 +1,5 @@ /* Copyright (c) 2008 Volker Krause <vk...@kd...> - Copyright (c) 2010 Emanoil Kotsev <del...@ya...> - - $Id$ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by @@ -27,17 +24,12 @@ #include <akonadi/collection.h> #include <akonadi/itemfetchjob.h> -#include <akonadi/itemcreatejob.h> -#include <akonadi/itemmodifyjob.h> -#include <akonadi/itemfetchscope.h> #include <opensync/opensync.h> #include <opensync/opensync-plugin.h> #include <opensync/opensync-data.h> #include <opensync/opensync-format.h> -#include <KUrl> - #include <boost/shared_ptr.hpp> using namespace Akonadi; @@ -50,12 +42,12 @@ Q_OBJECT public: - enum Type { Calendars = 0, Contacts, Todos, Notes }; + enum Type { Calendars = 0, Contacts, Notes, Todos }; DataSink( int type ); ~DataSink(); - bool initialize( OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncObjTypeSink *sink, OSyncError **error ); + bool initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncObjTypeSink *sink, OSyncError **error ); void getChanges(); void commit( OSyncChange *change ); @@ -66,7 +58,6 @@ void slotItemsReceived( const Akonadi::Item::List & ); protected: - /** * Returns the collection we are supposed to sync with. */ @@ -78,26 +69,42 @@ void reportChange( const Item& item, const QString& format ); /** - * Reimplement in subclass to provide data about changes to opensync. - * Note, that you must call DataSink::reportChange( Item, QString, int ) - * after you have organized data to be send to opensync. + * Reimplement in subclass to provide data about changes to opensync. Note, that you must call DataSink::reportChange( Item, QString, int ) after you have organized data to be send to opensync. */ void reportChange( const Item & item ); /** + * Creates a new item based on the data given by opensync. + */ +// const Item createItem( OSyncChange *change ); + /** + * Modified an item based on the data given by opensync. + */ +// const Item modifyItem( OSyncChange *change ); + /** * Deletes an item based on the data given by opensync. */ - void deleteItem( OSyncChange *change ); +// void deleteItem( OSyncChange *change ); + private: + const Item createAkonadiItem( OSyncChange *change ); const Item fetchItem( const QString& id ); - bool setPayload( Item *item, const QString mimeType, const QString &str ); - QString getMimeWithFormat(OSyncObjFormat* format); + const Item fetchItem( int id ); + const QString formatName(); + bool setPayload( Item *item, const QString &str ); + QString getHash(int id, int rev); + int idFromHash(QString hash); + private: + int m_type; + QString m_Name; QString m_Format; + QString m_MimeType; QString m_Url; + }; #endif Modified: plugins/akonadi-sync/trunk/src/sinkbase.cpp ============================================================================== --- plugins/akonadi-sync/trunk/src/sinkbase.cpp Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/sinkbase.cpp Wed Nov 3 09:11:10 2010 (r6151) @@ -25,9 +25,11 @@ #define WRAP() \ osync_trace( TRACE_ENTRY, "%s(%p,%p, %p, %p)", __PRETTY_FUNCTION__, sink, info, ctx, userdata); \ - SinkBase *sb = reinterpret_cast<SinkBase*>(userdata ); \ - sb->setContext( ctx ); \ - sb->setPluginInfo( info ); + kDebug() << osync_objtype_sink_get_name( sink );\ + SinkBase *sb = reinterpret_cast<SinkBase*>(userdata); \ + sb->setSink(sink);\ + sb->setPluginInfo( info );\ + sb->setContext( ctx ); extern "C" { @@ -40,38 +42,38 @@ } static void disconnect_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, void *userdata) { - WRAP( ) + WRAP( ) sb->disconnect(); + osync_objtype_sink_unref(sink); osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); } static void get_changes_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, osync_bool slow_sync, void *userdata) { - WRAP ( ) - if ( slow_sync ) - sb->setSlowSink(slow_sync); + WRAP ( ) + sb->setSlowSink(slow_sync); sb->getChanges(); osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); } static void sync_done_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, void *userdata) { - WRAP( ) + WRAP( ) sb->syncDone(); osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); } static void commit_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, OSyncChange *change, void *userdata) { - WRAP( ) + WRAP( ) sb->commit(change); osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); } - static void commitAll_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, void *userdata) { - WRAP( ) - sb->commitAll(); - osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); - } - +// static void commitAll_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, void *userdata) { +// WRAP( ) +// sb->commitAll(); +// osync_trace( TRACE_EXIT, "%s", __PRETTY_FUNCTION__ ); +// } +// // static void read_wrapper(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, OSyncChange *change, void *userdata) { // WRAP( ) // sb->commit(change); @@ -88,9 +90,9 @@ SinkBase::SinkBase( int features ) : - mContext( 0 ), mSink( 0 ), mPluginInfo( 0 ), + mContext( 0 ), m_canConnect(false), m_canDisconnect(false), m_canCommit (false), @@ -117,12 +119,12 @@ SinkBase::~SinkBase() { - if ( mContext) - osync_context_unref(mContext); +// if ( mContext) +// osync_context_unref(mContext); if ( mSink ) osync_objtype_sink_unref( mSink ); - if (mPluginInfo) - osync_plugin_info_unref(mPluginInfo); +// if (mPluginInfo) +// osync_plugin_info_unref(mPluginInfo); } void SinkBase::connect() @@ -132,50 +134,54 @@ void SinkBase::setSlowSink(osync_bool s) { - Q_ASSERT( m_SlowSync ); + kDebug(); m_SlowSync = s; } osync_bool SinkBase::getSlowSink() { - Q_ASSERT( m_SlowSync ); + kDebug(); return m_SlowSync; } void SinkBase::disconnect() { + kDebug(); Q_ASSERT( false ); } void SinkBase::getChanges() { + kDebug(); Q_ASSERT( false ); } void SinkBase::commit(OSyncChange * chg) { + kDebug(); Q_UNUSED( chg ); Q_ASSERT( false ); } -void SinkBase::commitAll() -{ - Q_ASSERT( false ); -} - -void SinkBase::write() -{ - Q_ASSERT( false ); -} - -void SinkBase::read() -{ - Q_ASSERT( false ); -} +// void SinkBase::commitAll() +// { +// Q_ASSERT( false ); +// } +// +// void SinkBase::write() +// { +// Q_ASSERT( false ); +// } +// +// void SinkBase::read() +// { +// Q_ASSERT( false ); +// } void SinkBase::syncDone() { + kDebug(); Q_ASSERT( false ); } @@ -184,13 +190,18 @@ kDebug(); Q_ASSERT( mContext ); osync_context_report_success( mContext ); + mContext = 0; } -void SinkBase::error(OSyncErrorType type, const QString &msg) const +void SinkBase::error(OSyncErrorType type, QString msg) const { kDebug(); Q_ASSERT( mContext ); - osync_context_report_error(mContext, type, "%s", msg.toLatin1().data() ); + OSyncError *oerror; + osync_error_set(&oerror, type, "%s", msg.toUtf8().data() ); + osync_context_report_osyncerror(mContext, oerror ); + osync_error_unref( &oerror ); + mContext = 0; } void SinkBase::warning(OSyncError * error) const @@ -199,6 +210,7 @@ Q_ASSERT( mContext ); osync_context_report_osyncwarning( mContext, error ); osync_error_unref( &error ); + mContext = 0; } void SinkBase::wrapSink(OSyncObjTypeSink* sink) @@ -212,10 +224,10 @@ kDebug() << ">> m_canDisconnect:" << m_canDisconnect; kDebug() << ">> m_canCommit:" << m_canCommit; kDebug() << ">> m_canGetChanges:" << m_canGetChanges; - kDebug() << ">> m_canWrite:" << m_canConnect; - kDebug() << ">> m_canCommitAll:" << m_canGetChanges; - kDebug() << ">> m_canRead:" << m_canConnect; - kDebug() << ">> m_canSyncDone:" << m_canGetChanges; + kDebug() << ">> m_canWrite:" << m_canWrite; + kDebug() << ">> m_canCommitAll:" << m_canCommitAll; + kDebug() << ">> m_canRead:" << m_canRead; + kDebug() << ">> m_canSyncDone:" << m_canSyncDone; if ( m_canConnect ) { osync_objtype_sink_set_connect_func(sink, connect_wrapper); @@ -233,10 +245,10 @@ osync_objtype_sink_set_commit_func(sink, commit_wrapper); osync_objtype_sink_set_commit_timeout(sink, 15); } - if ( m_canCommitAll ) { - osync_objtype_sink_set_committed_all_func(sink, commitAll_wrapper); - osync_objtype_sink_set_committedall_timeout(sink, 15); - } +// if ( m_canCommitAll ) { +// osync_objtype_sink_set_committed_all_func(sink, commitAll_wrapper); +// osync_objtype_sink_set_committedall_timeout(sink, 15); +// } if ( m_canSyncDone ) { osync_objtype_sink_set_sync_done_func(sink, sync_done_wrapper); osync_objtype_sink_set_syncdone_timeout(sink, 15); @@ -249,16 +261,27 @@ // osync_objtype_sink_set_read_timeout(sink, 5); // } + osync_objtype_sink_set_userdata ( sink, this ); + +} + +void SinkBase::setSink(OSyncObjTypeSink *sink) +{ + kDebug(); + Q_ASSERT( sink == mSink ); + mSink = sink; } void SinkBase::setPluginInfo(OSyncPluginInfo * info) { + kDebug(); Q_ASSERT( mPluginInfo == 0 || mPluginInfo == info ); mPluginInfo = info; } void SinkBase::setContext(OSyncContext * context) { + kDebug(); Q_ASSERT( mContext == 0 || context == mContext ); // ### do I need to ref() that here? and then probably deref() the old one somewhere above? mContext = context; Modified: plugins/akonadi-sync/trunk/src/sinkbase.h ============================================================================== --- plugins/akonadi-sync/trunk/src/sinkbase.h Thu Oct 21 15:55:57 2010 (r6150) +++ plugins/akonadi-sync/trunk/src/sinkbase.h Wed Nov 3 09:11:10 2010 (r6151) @@ -56,38 +56,40 @@ virtual void disconnect(); virtual void getChanges(); virtual void commit( OSyncChange *chg ); - virtual void write(); - virtual void read(); - virtual void commitAll(); +// virtual void write(); +// virtual void read(); +// virtual void commitAll(); virtual void syncDone(); OSyncContext* context() const { return mContext; } - void setContext( OSyncContext *context ); OSyncPluginInfo *pluginInfo() const { return mPluginInfo; } + osync_bool getSlowSink (); + void setPluginInfo( OSyncPluginInfo *info ); + void setContext( OSyncContext *context ); + void setSink( OSyncObjTypeSink *sink); void setSlowSink (osync_bool); - osync_bool getSlowSink (); protected: void success() const; - void error( OSyncErrorType type, const QString &msg ) const; + void error(OSyncErrorType type, QString msg) const; void warning( OSyncError *error ) const; - void wrapSink( OSyncObjTypeSink* sink ); + void wrapSink(OSyncObjTypeSink* sink ); OSyncObjTypeSink* sink() const { return mSink; } - bool m_isContact, m_isEvent, m_isTodo, m_isNote, m_isJournal; //TODO intended to be private +// bool m_isContact, m_isEvent, m_isTodo, m_isNote, m_isJournal; //TODO intended to be private private: - mutable OSyncContext *mContext; OSyncObjTypeSink *mSink; OSyncPluginInfo *mPluginInfo; + mutable OSyncContext *mContext; // what do we have and what can we do bool m_canConnect, m_canDisconnect, m_canCommit, m_canCommitAll, m_canGetChanges, m_canWrite, m_canRead, m_canSyncDone; |