void KCalTodoDataSource::get_changes(OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx, osync_bool slow_sync) { osync_trace(TRACE_ENTRY, "%s(%p, %p)", __PRETTY_FUNCTION__, info, ctx); OSyncError *error = NULL; OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); OSyncObjFormat *objformat = osync_format_env_find_objformat(formatenv, "vtodo20"); OSyncHashTable *hashtable = osync_objtype_sink_get_hashtable(sink); if (slow_sync) { osync_trace(TRACE_INTERNAL, "Got slow-sync"); if (!osync_hashtable_slowsync(hashtable, &error)) { osync_context_report_osyncerror(ctx, error); osync_trace(TRACE_EXIT_ERROR, "%s: %s", __PRETTY_FUNCTION__, osync_error_print(&error)); return; } } if (!kcal->get_todo_changes(this, sink, info, ctx)) { osync_trace(TRACE_EXIT_ERROR, "%s: error in get_todo_changes", __PRETTY_FUNCTION__); osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Error while detecting latest changes."); return; } if (!report_deleted(sink, info, ctx, objformat)) { osync_trace(TRACE_EXIT_ERROR, "%s", __PRETTY_FUNCTION__); osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Error while detecting deleted entries."); return; } osync_context_report_success(ctx); osync_trace(TRACE_EXIT, "%s", __PRETTY_FUNCTION__); }
bool KCalSharedResource::get_event_changes(OSyncDataSource *dsobj, OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx) { OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); OSyncObjFormat *objformat = osync_format_env_find_objformat(formatenv, "vevent20"); KCal::Event::List events = calendar->events(); for (KCal::Event::List::ConstIterator i = events.begin(); i != events.end(); i++) { if ( ! dsobj->has_category((*i)->categories()) ) continue; /* Skip entries from birthday resource. This is just a workaround. * patch by rhuitl * FIXME: todo: add a list of resources to kdepim-sync.conf */ if ( (*i)->uid().contains("KABC_Birthday") || (*i)->uid().contains("KABC_Anniversary") ) continue; if (!report_incidence(dsobj, sink, info, ctx, *i, objformat)) return false; } return true; }
bool KCalSharedResource::get_todo_changes(OSyncDataSource *dsobj, OSyncObjTypeSink *sink, OSyncPluginInfo *info, OSyncContext *ctx) { OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); OSyncObjFormat *objformat = osync_format_env_find_objformat(formatenv, "vtodo20"); KCal::Todo::List todos = calendar->todos(); for (KCal::Todo::List::ConstIterator i = todos.begin(); i != todos.end(); i++) { if ( ! dsobj->has_category((*i)->categories()) ) continue; if (!report_incidence(dsobj, sink, info, ctx, *i, objformat)) return false; } return true; }
static void get_changes(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx) { osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, userdata, info, ctx); //plugin_environment *env = (plugin_environment *)userdata; OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); sink_environment *sinkenv = osync_objtype_sink_get_userdata(sink); OSyncError *error = NULL; //If you use opensync hashtables you can detect if you need //to do a slow-sync and set this on the hastable directly //otherwise you have to make 2 function like "get_changes" and //"get_all" and decide which to use using //osync_objtype_sink_get_slow_sync if (osync_objtype_sink_get_slowsync(sinkenv->sink)) { osync_trace(TRACE_INTERNAL, "Slow sync requested"); if (osync_hashtable_slowsync(sinkenv->hashtable, &error)) { osync_context_report_osyncerror(ctx, error); osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); osync_error_unref(&error); return; } } /* * Now you can get the changes. * Loop over all changes you get and do the following: */ do { char *hash = osync_strdup("<the calculated hash of the object>"); char *uid = osync_strdup("<some uid>"); //Now get the data of this change char *data = NULL; //Make the new change to report OSyncChange *change = osync_change_new(&error); if (!change) { osync_context_report_osyncwarning(ctx, error); osync_error_unref(&error); continue; } //Now set the uid of the object osync_change_set_uid(change, uid); osync_change_set_hash(change, hash); OSyncChangeType changetype = osync_hashtable_get_changetype(sinkenv->hashtable, change); osync_change_set_changetype(change, changetype); // Update entry. // Set the hash of the object (optional, only required if you use hashtabled) osync_hashtable_update_change(sinkenv->hashtable, change); if (changetype == OSYNC_CHANGE_TYPE_UNMODIFIED) { osync_free(hash); osync_free(uid); osync_change_unref(change); continue; } osync_free(hash); osync_free(uid); OSyncObjFormat *format = osync_format_env_find_objformat(formatenv, "<objformat>"); OSyncData *odata = osync_data_new(data, 0, format, &error); if (!odata) { osync_change_unref(change); osync_context_report_osyncwarning(ctx, error); osync_error_unref(&error); continue; } osync_data_set_objtype(odata, osync_objtype_sink_get_name(sinkenv->sink)); //Now you can set the data for the object osync_change_set_data(change, odata); osync_data_unref(odata); // just report the change via osync_context_report_change(ctx, change); osync_change_unref(change); osync_free(uid); } while(0); //When you are done looping and if you are using hashtables //check for deleted entries ... via hashtable OSyncList *u, *uids = osync_hashtable_get_deleted(sinkenv->hashtable); for (u = uids; u; u = u->next) { OSyncChange *change = osync_change_new(&error); if (!change) { osync_context_report_osyncwarning(ctx, error); osync_error_unref(&error); continue; } const char *uid = u->data; osync_change_set_uid(change, uid); osync_change_set_changetype(change, OSYNC_CHANGE_TYPE_DELETED); OSyncObjFormat *format = osync_format_env_find_objformat(formatenv, "<objformat>"); OSyncData *odata = osync_data_new(NULL, 0, format, &error); if (!odata) { osync_change_unref(change); osync_context_report_osyncwarning(ctx, error); osync_error_unref(&error); continue; } osync_data_set_objtype(odata, osync_objtype_sink_get_name(sinkenv->sink)); osync_change_set_data(change, odata); osync_data_unref(odata); osync_context_report_change(ctx, change); osync_hashtable_update_change(sinkenv->hashtable, change); osync_change_unref(change); } osync_list_free(uids); //Now we need to answer the call osync_context_report_success(ctx); osync_trace(TRACE_EXIT, "%s", __func__); }
/* In initialize, we get the config for the plugin. Here we also must register * all _possible_ objtype sinks. */ static void *mock_initialize(OSyncPlugin *plugin, OSyncPluginInfo *info, OSyncError **error) { osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, plugin, info, error); if (mock_get_error(info->memberid, "INIT_NULL_NOERROR")) { osync_trace(TRACE_EXIT, "%s: %s", __func__, "Everything is fine. I don't need plugin userdata."); return NULL; } if (mock_get_error(info->memberid, "INIT_NULL")) { osync_error_set(error, OSYNC_ERROR_EXPECTED, "Triggering INIT_NULL error"); osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); return NULL; } mock_env *env = osync_try_malloc0(sizeof(mock_env), error); osync_assert(env); OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); osync_assert(formatenv); OSyncPluginConfig *config = osync_plugin_info_get_config(info); osync_assert(config); if (mock_get_error(info->memberid, "MAINSINK_CONNECT")) { env->mainsink = osync_objtype_main_sink_new(error); osync_objtype_sink_set_connect_func(env->mainsink, mock_connect); osync_objtype_sink_set_disconnect_func(env->mainsink, mock_mainsink_disconnect); osync_objtype_sink_set_userdata(env->mainsink, env); osync_plugin_info_set_main_sink(info, env->mainsink); } /* Now we register the objtypes that we can sync. This plugin is special. It can * synchronize any objtype we configure it to sync and where a conversion * path to the file format can be found */ OSyncList *objtypesinks = osync_plugin_info_get_objtype_sinks(info); OSyncList *list = NULL; for (list = objtypesinks;list; list = list->next) { MockDir *dir = osync_try_malloc0(sizeof(MockDir), error); osync_assert(dir); dir->committed_all = TRUE; OSyncObjTypeSink *sink = (OSyncObjTypeSink*)list->data; osync_assert(sink); const char *objtype = osync_objtype_sink_get_name(sink); dir->res = osync_plugin_config_find_active_resource(config, objtype); osync_plugin_resource_ref(dir->res); dir->path = osync_plugin_resource_get_path(dir->res); osync_assert(dir->path); OSyncList *format_sinks = osync_plugin_resource_get_objformat_sinks(dir->res); osync_assert(osync_list_length(format_sinks) == 1); OSyncObjFormatSink *format_sink = osync_list_nth_data(format_sinks, 0); const char *objformat_str = osync_objformat_sink_get_objformat(format_sink); osync_assert(objformat_str); dir->objformat = osync_format_env_find_objformat(formatenv, objformat_str); osync_assert(dir->objformat); osync_objformat_ref(dir->objformat); osync_list_free(format_sinks); /* const char *objformat = osync_objformat_get_name(dir->objformat); OSyncObjFormatSink *format_sink = osync_objformat_sink_new(objformat, error); if (!format_sink) return NULL; osync_objtype_sink_add_objformat_sink(sink, format_sink); */ /* Sanity check for connect_done */ dir->connect_done = TRUE; if (!mock_get_error(info->memberid, "MAINSINK_CONNECT")) { osync_objtype_sink_set_connect_func(sink, mock_connect); osync_objtype_sink_set_connect_done_func(sink, mock_connect_done); osync_objtype_sink_set_disconnect_func(sink, mock_disconnect); } osync_objtype_sink_set_get_changes_func(sink, mock_get_changes); osync_objtype_sink_set_committed_all_func(sink, mock_committed_all); osync_objtype_sink_set_commit_func(sink, mock_commit_change); osync_objtype_sink_set_read_func(sink, mock_read); osync_objtype_sink_set_sync_done_func(sink, mock_sync_done); /* We pass the MockDir object to the sink, so we dont have to look it up * again once the functions are called */ osync_objtype_sink_set_userdata(sink, dir); /* Request an Anchor */ osync_objtype_sink_enable_state_db(sink, TRUE); /* Request an Hashtable */ osync_objtype_sink_enable_hashtable(sink, TRUE); //Lets reduce the timeouts a bit so the checks work faster osync_objtype_sink_set_connect_timeout(sink, 2); osync_objtype_sink_set_getchanges_timeout(sink, 2); osync_objtype_sink_set_commit_timeout(sink, 4); osync_objtype_sink_set_committedall_timeout(sink, 4); osync_objtype_sink_set_syncdone_timeout(sink, 2); osync_objtype_sink_set_disconnect_timeout(sink, 2); osync_objtype_sink_set_read_timeout(sink, 2); /* XXX No testcase is currently using this at all! */ #if 0 if (g_getenv("NO_TIMEOUTS")) { /* XXX Timeout value of wouldn't work out, since the Sink object would fallback to the default timeout value: sink->timeout.connect ? sink->timeout.connect : OSYNC_SINK_TIMEOUT_CONNECT; Really needed?! */ osync_objtype_sink_set_connect_timeout(sink, 0); osync_objtype_sink_set_getchanges_timeout(sink, 0); osync_objtype_sink_set_commit_timeout(sink, 0); osync_objtype_sink_set_committedall_timeout(sink, 0); osync_objtype_sink_set_syncdone_timeout(sink, 0); osync_objtype_sink_set_disconnect_timeout(sink, 0); osync_objtype_sink_set_read_timeout(sink, 0); } /* What is meant by this?! Maybe OSyncPlugin.useable?! Not used at all... if (g_getenv("IS_AVAILABLE")) info->functions.is_available = mock_is_available; */ #endif env->directories = g_list_append(env->directories, dir); } osync_list_free(objtypesinks); osync_trace(TRACE_EXIT, "%s: %p", __func__, env); return (void *)env; }
/** Report files on a directory * * NOTE: If 'dir' is non-empty it MUST start it a slash. This is just * to make easier concatenation of the paths, and we can just concatenate * fsinfo->path and subdir to get the complete path. * * @param dir The fsinfo->path subdirectory that should be reported. Use * an empty string to report files on fsinfo->path. Should * start with a slash. See note above. * */ static void mock_report_dir(MockDir *directory, const char *subdir, OSyncContext *ctx, OSyncPluginInfo *info, OSyncObjTypeSink *sink) { GError *gerror = NULL; const char *de = NULL; char *path = NULL; GDir *dir = NULL; OSyncError *error = NULL; OSyncList *sorted_dir_list = NULL; osync_trace(TRACE_ENTRY, "%s(%p, %s, %p, %p)", __func__, directory, subdir, ctx, sink); OSyncHashTable *hashtable = osync_objtype_sink_get_hashtable(sink); OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); osync_assert(formatenv); path = g_build_filename(directory->path, subdir, NULL); osync_trace(TRACE_INTERNAL, "path %s", path); dir = g_dir_open(path, 0, &gerror); osync_assert(dir); while((de = g_dir_read_name(dir))) { sorted_dir_list = osync_list_insert_sorted(sorted_dir_list, g_strdup(de), (OSyncCompareFunc)strcmp); } g_dir_close(dir); while(sorted_dir_list) { de = sorted_dir_list->data; char *filename = g_build_filename(path, de, NULL); char *relative_filename = NULL; if (!subdir) relative_filename = g_strdup(de); else relative_filename = g_build_filename(subdir, de, NULL); g_free(sorted_dir_list->data); sorted_dir_list = osync_list_remove(sorted_dir_list, sorted_dir_list->data); osync_trace(TRACE_INTERNAL, "path2 %s %s", filename, relative_filename); if (g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { struct stat buf; stat(filename, &buf); char *hash = mock_generate_hash(&buf); /* Report normal files */ OSyncChange *change = osync_change_new(&error); osync_assert(change); osync_change_set_uid(change, relative_filename); osync_change_set_hash(change, hash); g_free(hash); OSyncChangeType type = osync_hashtable_get_changetype(hashtable, change); osync_change_set_changetype(change, type); osync_hashtable_update_change(hashtable, change); if (type == OSYNC_CHANGE_TYPE_UNMODIFIED) { g_free(filename); g_free(relative_filename); osync_change_unref(change); continue; } OSyncFileFormat *file = osync_try_malloc0(sizeof(OSyncFileFormat), &error); osync_assert(file); file->path = g_strdup(relative_filename); OSyncData *odata = NULL; if (!mock_get_error(info->memberid, "ONLY_INFO")) { osync_assert(osync_file_read(filename, &(file->data), &(file->size), &error)); if (mock_get_error(info->memberid, "SLOW_REPORT")) g_usleep(1*G_USEC_PER_SEC); odata = osync_data_new((char *)file, sizeof(OSyncFileFormat), directory->objformat, &error); osync_assert(odata); osync_change_set_data(change, odata); } osync_data_set_objtype(odata, osync_objtype_sink_get_name(sink)); osync_data_unref(odata); osync_context_report_change(ctx, change); osync_change_unref(change); } g_free(filename); g_free(relative_filename); } g_free(path); osync_trace(TRACE_EXIT, "%s", __func__); }