Changeset 3301

Show
Ignore:
Timestamp:
04/28/08 13:56:10 (5 months ago)
Author:
paule
Message:

Fix for changes to hashtable API and simplify some of the change handling code as a result

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/opie-sync/src/opie_sync.c

    r3166 r3301  
    295295        if (!env->hashtable) 
    296296                goto error; 
     297        if(!osync_hashtable_load(env->hashtable, &error)) 
     298                goto error; 
    297299 
    298300        if(env->objtype == OPIE_OBJECT_TYPE_NOTE) { 
     
    324326        OSyncError *error = NULL; 
    325327 
    326         /* Reset internal list of hashtable report, so we can detect 
    327            deleted entries */ 
    328         osync_hashtable_reset_reports(env->hashtable); 
    329  
    330328        if (osync_objtype_sink_get_slowsync(sink)) { 
    331329                osync_trace(TRACE_INTERNAL, "Slow sync requested"); 
     
    341339        while(item_node) 
    342340        { 
    343                 char *uid = opie_xml_get_tagged_uid(item_node); 
    344                 unsigned char *hash = hash_xml_node(env->doc, item_node); 
     341                /* Create the new change to report */ 
     342                OSyncChange *change = osync_change_new(&error); 
     343                if (!change) { 
     344                        osync_context_report_osyncwarning(ctx, error); 
     345                        osync_error_unref(&error); 
     346                } 
     347                else { 
     348 
     349                        /* Retrieve and set the uid of the object */ 
     350                        char *uid = opie_xml_get_tagged_uid(item_node); 
     351                        osync_change_set_uid(change, uid); 
     352                        g_free(uid); 
     353                         
     354                        /* Calculate and set the hash of the object */ 
     355                        unsigned char *hash = hash_xml_node(env->doc, item_node); 
     356                        osync_change_set_hash(change, hash); 
     357                        g_free(hash); 
     358                         
     359                        /* Detect and set the type of change */ 
     360                        OSyncChangeType changetype = osync_hashtable_get_changetype(env->hashtable, change); 
     361                        osync_change_set_changetype(change, changetype); 
     362                         
     363                        /* Update the hashtable with the hash of the object */ 
     364                        osync_hashtable_update_change(env->hashtable, change); 
     365         
     366                        if (changetype != OSYNC_CHANGE_TYPE_UNMODIFIED) { 
     367                                /* Convert category IDs to names that other systems can use */ 
     368                                g_mutex_lock(env->plugin_env->plugin_mutex); 
     369                                char *categories_bkup = opie_xml_get_categories(item_node); 
     370                                if(env->plugin_env->categories_doc && categories_bkup) 
     371                                        opie_xml_category_ids_to_names(env->plugin_env->categories_doc, item_node); 
     372                                g_mutex_unlock(env->plugin_env->plugin_mutex); 
     373                                 
     374                                char *data = xml_node_to_text(env->doc, item_node); 
    345375                 
    346                 /* Convert category IDs to names that other systems can use */ 
    347                 g_mutex_lock(env->plugin_env->plugin_mutex); 
    348                 char *categories_bkup = opie_xml_get_categories(item_node); 
    349                 if(env->plugin_env->categories_doc && categories_bkup) 
    350                         opie_xml_category_ids_to_names(env->plugin_env->categories_doc, item_node); 
    351                 g_mutex_unlock(env->plugin_env->plugin_mutex); 
    352                  
    353                 char *data = xml_node_to_text(env->doc, item_node); 
    354  
    355                 /* Restore old categories value as we don't want to save this back to our XML file */ 
    356                 if(categories_bkup) { 
    357                         opie_xml_set_categories(item_node, categories_bkup); 
    358                         g_free(categories_bkup); 
    359                 } 
    360                  
    361                 // Report every entry .. every unreported entry got deleted. 
    362                 osync_hashtable_report(env->hashtable, uid); 
    363  
    364                 OSyncChangeType changetype = osync_hashtable_get_changetype(env->hashtable, uid, hash); 
    365  
    366                 if (changetype != OSYNC_CHANGE_TYPE_UNMODIFIED) { 
    367                         //Set the hash of the object (optional, only required if you use hashtabled) 
    368                         osync_hashtable_update_hash(env->hashtable, changetype, uid, hash); 
    369          
    370                         //Make the new change to report 
    371                         OSyncChange *change = osync_change_new(&error); 
    372                         if (!change) { 
    373                                 osync_context_report_osyncwarning(ctx, error); 
    374                                 osync_error_unref(&error); 
    375                         } 
    376                         else { 
    377          
    378                                 //Now set the uid of the object 
    379                                 osync_change_set_uid(change, uid); 
    380                                 osync_change_set_hash(change, hash); 
    381                                 osync_change_set_changetype(change, changetype); 
    382                  
     376                                /* Restore old categories value as we don't want to save this back to our XML file */ 
     377                                if(categories_bkup) { 
     378                                        opie_xml_set_categories(item_node, categories_bkup); 
     379                                        g_free(categories_bkup); 
     380                                } 
     381                                 
    383382                                OSyncData *odata = osync_data_new(data, strlen(data) + 1, env->objformat, &error); 
    384383                                if (!odata) { 
    385                                         osync_change_unref(change); 
    386384                                        osync_context_report_osyncwarning(ctx, error); 
    387385                                        osync_error_unref(&error); 
     
    390388                                        osync_data_set_objtype(odata, osync_objtype_sink_get_name(sink)); 
    391389                         
    392                                         //Now you can set the data for the object 
     390                                        /* Set the data for the object */ 
    393391                                        osync_change_set_data(change, odata); 
    394392                                        osync_data_unref(odata); 
    395393                         
    396                                         // just report the change via 
     394                                        /* Report the change */ 
    397395                                        osync_context_report_change(ctx, change); 
    398                          
    399                                         osync_change_unref(change); 
    400396                                } 
     397                                g_free(data); 
    401398                        } 
    402                 } 
    403  
    404                 g_free(data); 
    405                 g_free(hash); 
    406                 g_free(uid); 
     399                         
     400                        osync_change_unref(change); 
     401                } 
    407402                 
    408403                item_node = opie_xml_get_next(item_node); 
    409404        } 
    410405 
    411         //When you are done looping and if you are using hashtables 
    412         //check for deleted entries ... via hashtable 
    413         int i; 
    414         char **uids = osync_hashtable_get_deleted(env->hashtable)
    415         for (i=0; uids[i]; i++) { 
     406        /* Check for deleted entries using the hashtable */ 
     407        OSyncList *u, *uids = osync_hashtable_get_deleted(env->hashtable); 
     408        for (u = uids; u; u = u->next) { 
     409               const char *uid = u->data
     410                
    416411                OSyncChange *change = osync_change_new(&error); 
    417412                if (!change) { 
    418                         g_free(uids[i]); 
    419413                        osync_context_report_osyncwarning(ctx, error); 
    420414                        osync_error_unref(&error); 
     
    422416                } 
    423417 
    424                 osync_change_set_uid(change, uids[i]); 
     418                osync_change_set_uid(change, uid); 
    425419                osync_change_set_changetype(change, OSYNC_CHANGE_TYPE_DELETED); 
    426420 
    427421                OSyncData *odata = osync_data_new(NULL, 0, env->objformat, &error); 
    428422                if (!odata) { 
    429                         g_free(uids[i]); 
    430423                        osync_change_unref(change); 
    431424                        osync_context_report_osyncwarning(ctx, error); 
     
    440433                osync_context_report_change(ctx, change); 
    441434 
    442                 osync_hashtable_update_hash(env->hashtable, osync_change_get_changetype(change), osync_change_get_uid(change), NULL); 
     435                osync_hashtable_update_change(env->hashtable, change); 
    443436 
    444437                osync_change_unref(change); 
    445                 g_free(uids[i]); 
    446         } 
    447         g_free(uids); 
     438        } 
     439        osync_list_free(uids); 
    448440 
    449441        //Now we need to answer the call 
     
    543535                xmlFreeDoc(change_doc); 
    544536 
    545         osync_hashtable_update_hash(env->hashtable, osync_change_get_changetype(change), osync_change_get_uid(change), hash); 
     537        osync_hashtable_update_change(env->hashtable, change); 
    546538 
    547539        //Answer the call 
     
    580572        } 
    581573         
     574        if (!osync_hashtable_save(env->hashtable, &error)) 
     575                goto error; 
     576         
    582577        osync_context_report_success(ctx); 
    583578        osync_trace(TRACE_EXIT, "%s", __func__); 
     
    596591         
    597592        /* Close the hashtable */ 
    598         osync_hashtable_free(env->hashtable); 
     593        osync_hashtable_unref(env->hashtable); 
    599594        env->hashtable = NULL; 
    600595