Changeset 2183

Show
Ignore:
Timestamp:
06/22/07 20:46:02 (1 year ago)
Author:
dgollub
Message:

Support for event and todo object type in evo2-sync.
Patch by prahal

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/evolution2/src/Makefile.am

    r1264 r2183  
    2525        evolution2_sync.c \ 
    2626        evolution2_ebook.c \ 
     27        evolution2_ecal.c \ 
     28        evolution2_etodo.c \ 
    2729        evolution2_xml.c 
    2830 
    2931 
    30 #       evolution2_ecal.c 
    31 #       evolution2_etodo.c 
    3232 
    3333evo2_sync_la_LDFLAGS = -avoid-version -export-dynamic -module 
  • plugins/evolution2/src/evolution2_ecal.c

    r1147 r2183  
    2121#include "evolution2_sync.h" 
    2222 
    23 osync_bool evo2_calendar_open(evo_environment *env, OSyncError **error) 
    24 
    25         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); 
    26         ESourceList *sources = NULL; 
    27         ESource *source = NULL; 
    28         GError *gerror = NULL; 
    29          
    30          
    31         if (strcmp(env->calendar_path, "default")) { 
    32                 if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) { 
    33                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get sources for calendar: %s", gerror ? gerror->message : "None"); 
    34                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    35                         g_clear_error(&gerror); 
    36                         return FALSE; 
     23static void evo2_ecal_connect(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     24
     25        OSyncError *error = NULL; 
     26        GError *gerror = NULL; 
     27        ESourceList *sources = NULL; 
     28        ESource *source = NULL; 
     29 
     30        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     31        OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); 
     32        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     33 
     34        if (!env->calendar_path) { 
     35                osync_error_set(&error, OSYNC_ERROR_GENERIC, "no calendar path set"); 
     36                goto error; 
     37        } 
     38 
     39        if (strcmp(env->calendar_path, "default")) { 
     40                if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) { 
     41                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to get sources for calendar: %s", gerror ? gerror->message : "None"); 
     42                        goto error; 
     43                } 
     44                 
     45                if (!(source = evo2_find_source(sources, env->calendar_path))) { 
     46                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Error finding source \"%s\"", env->calendar_path); 
     47                        goto error; 
     48                } 
     49  
     50                if (!(env->calendar = e_cal_new(source, E_CAL_SOURCE_TYPE_EVENT))) { 
     51                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to create new calendar"); 
     52                        goto error; 
    3753                } 
    38                  
    39                 source = evo2_find_source(sources, env->calendar_path); 
    40                 if (!source) { 
    41                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find source for calendar"); 
    42                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    43                         return FALSE; 
    44                 } 
    45                  
    46                 env->calendar = e_cal_new(source, E_CAL_SOURCE_TYPE_EVENT); 
    47                 if(!env->calendar) { 
    48                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to create new calendar"); 
    49                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    50                         return FALSE; 
    51                 } 
    52                  
     54 
    5355                if(!e_cal_open(env->calendar, FALSE, &gerror)) { 
    54                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open calendar: %s", gerror ? gerror->message : "None"); 
    55                         g_object_unref(env->calendar); 
    56                         env->calendar = NULL; 
    57                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    58                         g_clear_error(&gerror); 
    59                         return FALSE; 
    60                 } 
    61         } else { 
    62                 if (!e_cal_open_default (&env->calendar, E_CAL_SOURCE_TYPE_EVENT, NULL, NULL, &gerror)) { 
    63                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open default calendar: %s", gerror ? gerror->message : "None"); 
    64                         env->calendar = NULL; 
    65                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    66                         g_clear_error(&gerror); 
    67                         return FALSE; 
    68                 } 
    69         } 
    70          
    71         if (!osync_anchor_compare(env->member, "event", env->calendar_path)) 
    72                 osync_member_set_slow_sync(env->member, "event", TRUE); 
    73          
    74         osync_trace(TRACE_EXIT, "%s", __func__); 
    75         return TRUE; 
    76 
    77  
    78 void evo2_calendar_get_changes(OSyncContext *ctx) 
    79 
    80         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, ctx); 
    81         evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx); 
    82          
    83         GList *changes = NULL; 
    84         GList *l = NULL; 
    85         char *data = NULL; 
    86         const char *uid = NULL; 
    87         int datasize = 0; 
    88         GError *gerror = NULL; 
    89          
    90         if (osync_member_get_slow_sync(env->member, "event") == FALSE) { 
    91                 osync_debug("EVO2-SYNC", 4, "No slow_sync for event"); 
    92                 if (!e_cal_get_changes(env->calendar, env->change_id, &changes, &gerror)) { 
    93                         osync_context_send_log(ctx, "Unable to open changed calendar entries"); 
    94                         osync_trace(TRACE_EXIT_ERROR, "%s: Unable to open changed calendar entries: %s", __func__, gerror ? gerror->message : "None"); 
    95                         g_clear_error(&gerror); 
    96                         return; 
    97                 } 
    98                  
    99                 for (l = changes; l; l = l->next) { 
    100                         ECalChange *ecc = (ECalChange *)l->data; 
     56                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open calendar: %s", gerror ? gerror->message : "None"); 
     57                        goto error_free_event; 
     58                } 
     59        } else { 
     60                osync_trace(TRACE_INTERNAL, "Opening default calendar\n"); 
     61                if (!e_cal_open_default(&env->calendar, E_CAL_SOURCE_TYPE_EVENT, NULL, NULL, &gerror)) { 
     62                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open default calendar: %s", gerror ? gerror->message : "None"); 
     63                        goto error_free_event; 
     64                } 
     65        } 
     66 
     67        char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     68        if (!osync_anchor_compare(anchorpath, "event", env->calendar_path)) 
     69                osync_objtype_sink_set_slowsync(sink, TRUE); 
     70        g_free(anchorpath); 
     71 
     72 
     73        osync_context_report_success(ctx); 
     74 
     75        osync_trace(TRACE_EXIT, "%s", __func__); 
     76        return; 
     77 
     78error_free_event: 
     79                g_object_unref(env->calendar); 
     80                env->calendar = NULL; 
     81error: 
     82        if (gerror) 
     83                g_clear_error(&gerror); 
     84        osync_context_report_osyncerror(ctx, error); 
     85        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); 
     86        osync_error_unref(&error); 
     87
     88 
     89static void evo2_ecal_disconnect(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     90
     91        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     92        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     93 
     94        if (env->calendar) { 
     95                g_object_unref(env->calendar); 
     96                env->calendar = NULL; 
     97        } 
     98 
     99        osync_context_report_success(ctx); 
     100 
     101        osync_trace(TRACE_EXIT, "%s", __func__); 
     102
     103 
     104static void evo2_ecal_sync_done(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     105
     106        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     107        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     108 
     109        char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     110        osync_anchor_update(anchorpath, "event", env->calendar_path); 
     111        g_free(anchorpath); 
     112 
     113 
     114        GList *changes = NULL; 
     115        e_cal_get_changes(env->calendar, env->change_id, &changes, NULL); 
     116  
     117        osync_context_report_success(ctx); 
     118         
     119        osync_trace(TRACE_EXIT, "%s", __func__); 
     120
     121 
     122void evo2_ecal_report_change(OSyncContext *ctx, OSyncObjFormat *format, char *data, unsigned int size, const char *uid, OSyncChangeType changetype) 
     123
     124        OSyncError *error = NULL; 
     125 
     126        OSyncChange *change = osync_change_new(&error); 
     127        if (!change) { 
     128                osync_context_report_osyncwarning(ctx, error); 
     129                osync_error_unref(&error); 
     130                return; 
     131        } 
     132 
     133        osync_change_set_uid(change, uid); 
     134        osync_change_set_changetype(change, changetype); 
     135 
     136        OSyncData *odata = osync_data_new(data, size, format, &error); 
     137        if (!odata) { 
     138                osync_change_unref(change); 
     139                osync_context_report_osyncwarning(ctx, error); 
     140                osync_error_unref(&error); 
     141                return; 
     142        } 
     143 
     144        osync_change_set_data(change, odata); 
     145        osync_data_unref(odata); 
     146 
     147        osync_context_report_change(ctx, change); 
     148 
     149        osync_change_unref(change); 
     150
     151 
     152 
     153static void evo2_ecal_get_changes(void *indata, OSyncPluginInfo *info, OSyncContext *ctx) 
     154
     155        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, indata, info, ctx); 
     156        OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); 
     157        OSyncEvoEnv *env = (OSyncEvoEnv *)indata; 
     158        OSyncError *error = NULL; 
     159 
     160        GList *changes = NULL; 
     161        ECalChange *ecc = NULL; 
     162        GList *l = NULL; 
     163        char *data = NULL; 
     164        const char *uid = NULL; 
     165        int datasize = 0; 
     166        GError *gerror = NULL; 
     167 
     168        if (osync_objtype_sink_get_slowsync(sink) == FALSE) { 
     169                osync_trace(TRACE_INTERNAL, "No slow_sync for calendar"); 
     170                if (!e_cal_get_changes(env->calendar, env->change_id, &changes, &gerror)) { 
     171                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open changed calendar entries: %s", gerror ? gerror->message : "None"); 
     172                        goto error; 
     173                } 
     174                osync_trace(TRACE_INTERNAL, "Found %i changes for change-ID %s", g_list_length(changes), env->change_id); 
     175 
     176                for (l = changes; l; l = l->next) { 
     177                        ecc = (ECalChange *)l->data; 
    101178                        e_cal_component_get_uid(ecc->comp, &uid); 
    102179                        e_cal_component_commit_sequence (ecc->comp); 
     
    106183                                        data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp)); 
    107184                                        datasize = strlen(data) + 1; 
    108                                         evo2_report_change(ctx, "event", "vevent20", data, datasize, uid, CHANGE_ADDED); 
     185                                        evo2_ecal_report_change(ctx, env->calendar_format, data, datasize, uid, OSYNC_CHANGE_TYPE_ADDED); 
    109186                                        break; 
    110187                                case E_CAL_CHANGE_MODIFIED: 
    111188                                        data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp)); 
    112189                                        datasize = strlen(data) + 1; 
    113                                         evo2_report_change(ctx, "event", "vevent20", data, datasize, uid, CHANGE_MODIFIED); 
     190                                        evo2_ecal_report_change(ctx, env->calendar_format, data, datasize, uid, OSYNC_CHANGE_TYPE_MODIFIED); 
    114191                                        break; 
    115192                                case E_CAL_CHANGE_DELETED: 
    116                                         evo2_report_change(ctx, "event", "vevent20", NULL, 0, uid, CHANGE_DELETED); 
     193                                        evo2_ecal_report_change(ctx, env->calendar_format, NULL, 0, uid, OSYNC_CHANGE_TYPE_DELETED); 
    117194                                        break; 
    118195                        } 
    119                 } 
    120         } else { 
    121                 osync_debug("EVO2-SYNC", 4, "slow_sync for event"); 
    122         if (!e_cal_get_object_list_as_comp (env->calendar, "(contains? \"any\" \"\")", &changes, &gerror)) { 
    123                         osync_context_send_log(ctx, "Unable to get all events"); 
    124                         osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get all events: %s", __func__, gerror ? gerror->message : "None"); 
    125                         g_clear_error(&gerror); 
    126                         return; 
    127         } 
     196                } 
     197        } else { 
     198                osync_trace(TRACE_INTERNAL, "slow_sync for calendar"); 
     199                if (!e_cal_get_object_list_as_comp (env->calendar, "(contains? \"any\" \"\")", &changes, &gerror)) { 
     200                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to get changes from calendar: %s", gerror ? gerror->message : "None"); 
     201                        goto error; 
     202                } 
    128203                for (l = changes; l; l = l->next) { 
    129204                        ECalComponent *comp = E_CAL_COMPONENT (l->data); 
     
    132207                        e_cal_component_get_uid(comp, &uid); 
    133208                        int datasize = strlen(data) + 1; 
    134                         evo2_report_change(ctx, "event", "vevent20", data, datasize, uid, CHANGE_ADDED); 
     209                        evo2_ecal_report_change(ctx, env->calendar_format, data, datasize, uid, OSYNC_CHANGE_TYPE_ADDED); 
    135210                        g_object_unref (comp); 
    136211                } 
    137212        } 
    138         osync_trace(TRACE_EXIT, "%s", __func__); 
    139 
    140  
    141 static osync_bool evo2_calendar_modify(OSyncContext *ctx, OSyncChange *change) 
    142 
    143         osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, ctx, change); 
    144         evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx); 
    145          
    146         const char *uid = osync_change_get_uid(change); 
    147         char *data = osync_change_get_data(change); 
     213 
     214        osync_context_report_success(ctx); 
     215 
     216        osync_trace(TRACE_EXIT, "%s", __func__); 
     217        return; 
     218 
     219error: 
     220        if (gerror) 
     221                g_clear_error(&gerror); 
     222        osync_context_report_osyncerror(ctx, error); 
     223        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); 
     224        osync_error_unref(&error); 
     225
     226 
     227static void evo2_ecal_modify(void *data, OSyncPluginInfo *info, OSyncContext *ctx, OSyncChange *change) 
     228
     229        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, data, info, ctx, change); 
     230        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     231 
     232        const char *uid = osync_change_get_uid(change); 
    148233        icalcomponent *icomp = NULL; 
    149234        char *returnuid = NULL; 
    150         GError *gerror = NULL; 
    151          
    152         switch (osync_change_get_changetype(change)) { 
    153                 case CHANGE_DELETED: 
    154                         if (!e_cal_remove_object(env->calendar, uid, &gerror)) { 
    155                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to delete event: %s", gerror ? gerror->message : "None"); 
    156                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to delete event: %s", __func__, gerror ? gerror->message : "None"); 
    157                                 g_clear_error(&gerror); 
    158                                 return FALSE; 
    159                         } 
    160                         break; 
    161                 case CHANGE_ADDED: 
    162                         icomp = icalcomponent_new_from_string(data); 
     235        GError *gerror = NULL; 
     236        OSyncError *error = NULL; 
     237        OSyncData *odata = NULL; 
     238        char *plain = NULL; 
     239 
     240        switch (osync_change_get_changetype(change)) { 
     241                case OSYNC_CHANGE_TYPE_DELETED: 
     242                        if (!e_cal_remove_object(env->calendar, uid, &gerror)) { 
     243                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to delete event: %s", gerror ? gerror->message : "None"); 
     244                                goto error; 
     245                        } 
     246                        break; 
     247                case OSYNC_CHANGE_TYPE_ADDED: 
     248                        odata = osync_change_get_data(change); 
     249                        osync_data_get_data(odata, &plain, NULL); 
     250                        icomp = icalcomponent_new_from_string(plain); 
    163251                        if (!icomp) { 
    164252                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to convert event"); 
    165                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to convert event", __func__); 
    166                                 return FALSE; 
     253                                goto error; 
    167254                        } 
    168255                         
    169256                        icomp = icalcomponent_get_first_component (icomp, ICAL_VEVENT_COMPONENT); 
    170257                        if (!icomp) { 
    171                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to get vevent"); 
    172                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get vevent", __func__); 
    173                                 return FALSE; 
     258                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to get event"); 
     259                                goto error; 
    174260                        } 
    175261                         
    176262                        if (!e_cal_create_object(env->calendar, icomp, &returnuid, &gerror)) { 
    177                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to create event: %s", gerror ? gerror->message : "None"); 
    178                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to create event: %s", __func__, gerror ? gerror->message : "None"); 
    179                                 g_clear_error(&gerror); 
    180                                 return FALSE; 
     263                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to create event: %s", gerror ? gerror->message : "None"); 
     264                                goto error; 
    181265                        } 
    182266                        osync_change_set_uid(change, returnuid); 
    183                         break; 
    184                 case CHANGE_MODIFIED: 
    185                         icomp = icalcomponent_new_from_string(data); 
     267                        break; 
     268                case OSYNC_CHANGE_TYPE_MODIFIED: 
     269                        odata = osync_change_get_data(change); 
     270                        osync_data_get_data(odata, &plain, NULL); 
     271 
     272                        icomp = icalcomponent_new_from_string(plain); 
    186273                        if (!icomp) { 
    187                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to convert event2"); 
    188                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to convert event2", __func__); 
    189                                 return FALSE; 
     274                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to convert event2"); 
     275                                goto error; 
    190276                        } 
    191277                         
    192278                        icomp = icalcomponent_get_first_component (icomp, ICAL_VEVENT_COMPONENT); 
    193279                        if (!icomp) { 
    194                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to get vevent2"); 
    195                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get vevent2", __func__); 
    196                                 return FALSE; 
    197                         } 
    198  
     280                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to get event2"); 
     281                                goto error; 
     282                        } 
     283                         
    199284                        icalcomponent_set_uid (icomp, uid); 
    200285                        if (!e_cal_modify_object(env->calendar, icomp, CALOBJ_MOD_ALL, &gerror)) { 
    201                                 /* try to add */ 
    202286                                osync_trace(TRACE_INTERNAL, "unable to mod event: %s", gerror ? gerror->message : "None"); 
    203287                                g_clear_error(&gerror); 
    204288                                if (!e_cal_create_object(env->calendar, icomp, &returnuid, &gerror)) { 
    205                                         osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to create or modify event: %s", gerror ? gerror->message : "None"); 
    206                                         osync_trace(TRACE_EXIT_ERROR, "%s: Unable to create event: %s", __func__, gerror ? gerror->message : "None"); 
    207                                         g_clear_error(&gerror); 
    208                                         return FALSE; 
     289                                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to create event: %s", gerror ? gerror->message : "None"); 
     290                                        goto error; 
    209291                                } 
    210292                        } 
    211                         break; 
    212                 default: 
    213                         printf("Error\n"); 
     293                        break; 
     294                default: 
     295                        printf("Error\n"); 
     296        } 
     297 
     298        osync_context_report_success(ctx); 
     299 
     300        osync_trace(TRACE_EXIT, "%s", __func__); 
     301        return; 
     302 
     303error: 
     304        if (gerror) 
     305                g_clear_error(&gerror); 
     306        osync_context_report_osyncerror(ctx, error); 
     307        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); 
     308        osync_error_unref(&error); 
     309
     310 
     311osync_bool evo2_ecal_initialize(OSyncEvoEnv *env, OSyncPluginInfo *info, OSyncError **error) 
     312
     313        OSyncFormatEnv *formatenv = osync_plugin_info_get_format_env(info); 
     314        env->calendar_format = osync_format_env_find_objformat(formatenv, "vevent20"); 
     315 
     316        if (!env->calendar_format) { 
     317                osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find vevent20 object format. vformat plugin installed?"); 
     318                return FALSE; 
     319        } 
     320 
     321 
     322        env->calendar_sink = osync_objtype_sink_new("event", error); 
     323        if (!env->calendar_sink){ 
     324                printf("calendar sink failed to initialize\n"); 
     325                return FALSE; 
    214326        } 
    215          
    216         osync_context_report_success(ctx); 
    217         osync_trace(TRACE_EXIT, "%s", __func__); 
    218         return TRUE; 
    219 
    220  
    221 void evo2_calendar_setup(OSyncPluginInfo *info) 
    222 
    223         osync_plugin_accept_objtype(info, "event"); 
    224         osync_plugin_accept_objformat(info, "event", "vevent20", NULL); 
    225         osync_plugin_set_commit_objformat(info, "event", "vevent20", evo2_calendar_modify); 
    226         osync_plugin_set_access_objformat(info, "event", "vevent20", evo2_calendar_modify); 
    227 
     327 
     328        osync_objtype_sink_add_objformat(env->calendar_sink, "vevent20"); 
     329 
     330        /* All sinks have the same functions of course */ 
     331        OSyncObjTypeSinkFunctions functions; 
     332        memset(&functions, 0, sizeof(functions)); 
     333        functions.connect = evo2_ecal_connect; 
     334        functions.disconnect = evo2_ecal_disconnect; 
     335        functions.get_changes = evo2_ecal_get_changes; 
     336        functions.commit = evo2_ecal_modify; 
     337        functions.sync_done = evo2_ecal_sync_done; 
     338 
     339        /* We pass the OSyncFileDir object to the sink, so we dont have to look it up 
     340         * again once the functions are called */ 
     341        osync_objtype_sink_set_functions(env->calendar_sink, functions, NULL); 
     342        osync_plugin_info_add_objtype(info, env->calendar_sink); 
     343
     344 
  • plugins/evolution2/src/evolution2_ecal.h

    r1264 r2183  
    22#define ECAL_H 
    33 
     4osync_bool evo2_ecal_initialize(OSyncEvoEnv *env, OSyncPluginInfo *info, OSyncError **error); 
     5 
    46#endif /*  ECAL_H */ 
  • plugins/evolution2/src/evolution2_etodo.c

    r766 r2183  
    2121#include "evolution2_sync.h" 
    2222 
    23 osync_bool evo2_todo_open(evo_environment *env, OSyncError **error) 
    24 
    25         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); 
    26         ESourceList *sources = NULL; 
    27         ESource *source = NULL; 
    28         GError *gerror = NULL; 
    29          
    30         if (strcmp(env->tasks_path, "default")) { 
    31                 if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_TODO, &gerror)) { 
    32                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get sources for tasks: %s", gerror ? gerror->message : "None"); 
    33                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    34                         g_clear_error(&gerror); 
    35                         return FALSE; 
     23static void evo2_etodo_connect(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     24
     25        OSyncError *error = NULL; 
     26        GError *gerror = NULL; 
     27        ESourceList *sources = NULL; 
     28        ESource *source = NULL; 
     29 
     30        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     31        OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); 
     32        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     33 
     34        if (!env->tasks_path) { 
     35                osync_error_set(&error, OSYNC_ERROR_GENERIC, "no tasks path set"); 
     36                goto error; 
     37        } 
     38 
     39        if (strcmp(env->tasks_path, "default")) { 
     40                if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_TODO, &gerror)) { 
     41                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to get sources for tasks: %s", gerror ? gerror->message : "None"); 
     42                        goto error; 
     43                } 
     44                 
     45                if (!(source = evo2_find_source(sources, env->tasks_path))) { 
     46                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Error finding source \"%s\"", env->tasks_path); 
     47                        goto error; 
     48                } 
     49  
     50                if (!(env->tasks = e_cal_new(source, E_CAL_SOURCE_TYPE_TODO))) { 
     51                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to create new tasks"); 
     52                        goto error; 
    3653                } 
    37                  
    38                 source = evo2_find_source(sources, env->tasks_path); 
    39                 if (!source) { 
    40                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find source for tasks"); 
    41                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    42                         return FALSE; 
    43                 } 
    44                  
    45                 env->tasks = e_cal_new(source, E_CAL_SOURCE_TYPE_TODO); 
    46                 if(!env->tasks) { 
    47                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to create new tasks"); 
    48                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    49                         return FALSE; 
    50                 } 
    51                  
     54 
    5255                if(!e_cal_open(env->tasks, FALSE, &gerror)) { 
    53                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open tasks: %s", gerror ? gerror->message : "None"); 
    54                         g_object_unref(env->tasks); 
    55                         env->tasks = NULL; 
    56                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    57                         g_clear_error(&gerror); 
    58                         return FALSE; 
    59                 } 
    60         } else { 
    61                 if (!e_cal_open_default (&env->tasks, E_CAL_SOURCE_TYPE_TODO, NULL, NULL, &gerror)) { 
    62                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open default tasks: %s", gerror ? gerror->message : "None"); 
    63                         env->calendar = NULL; 
    64                         osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
    65                         g_clear_error(&gerror); 
    66                         return FALSE; 
    67                 } 
    68         } 
    69          
    70         if (!osync_anchor_compare(env->member, "todo", env->tasks_path)) 
    71                 osync_member_set_slow_sync(env->member, "todo", TRUE); 
    72          
    73         osync_trace(TRACE_EXIT, "%s", __func__); 
    74         return TRUE; 
    75 
    76  
    77 void evo2_todo_get_changes(OSyncContext *ctx) 
    78 
    79         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, ctx); 
    80         evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx); 
    81          
    82         GList *changes = NULL; 
    83         GList *l = NULL; 
    84         char *data = NULL; 
    85         const char *uid = NULL; 
    86         int datasize = 0; 
    87         GError *gerror = NULL; 
    88          
    89         if (osync_member_get_slow_sync(env->member, "todo") == FALSE) { 
    90                 osync_debug("EVO2-SYNC", 4, "No slow_sync for todo"); 
    91                 if (!e_cal_get_changes(env->tasks, env->change_id, &changes, &gerror)) { 
    92                         osync_context_send_log(ctx, "Unable to open changed tasks entries"); 
    93                         osync_trace(TRACE_EXIT_ERROR, "%s: Unable to open changed tasks entries: %s", __func__, gerror ? gerror->message : "None"); 
    94                         g_clear_error(&gerror); 
    95                         return; 
    96                 } 
    97                  
    98                 for (l = changes; l; l = l->next) { 
    99                         ECalChange *ecc = (ECalChange *)l->data; 
     56                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open tasks: %s", gerror ? gerror->message : "None"); 
     57                        goto error_free_todo; 
     58                } 
     59        } else { 
     60                osync_trace(TRACE_INTERNAL, "Opening default tasks\n"); 
     61                if (!e_cal_open_default(&env->tasks, E_CAL_SOURCE_TYPE_TODO, NULL, NULL, &gerror)) { 
     62                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open default tasks: %s", gerror ? gerror->message : "None"); 
     63                        goto error_free_todo; 
     64                } 
     65        } 
     66 
     67        char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     68        if (!osync_anchor_compare(anchorpath, "todo", env->tasks_path)) 
     69                osync_objtype_sink_set_slowsync(sink, TRUE); 
     70        g_free(anchorpath); 
     71 
     72 
     73        osync_context_report_success(ctx); 
     74 
     75        osync_trace(TRACE_EXIT, "%s", __func__); 
     76        return; 
     77 
     78error_free_todo: 
     79                g_object_unref(env->tasks); 
     80                env->tasks = NULL; 
     81error: 
     82        if (gerror) 
     83                g_clear_error(&gerror); 
     84        osync_context_report_osyncerror(ctx, error); 
     85        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); 
     86        osync_error_unref(&error); 
     87
     88 
     89static void evo2_etodo_disconnect(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     90
     91        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     92        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     93 
     94        if (env->tasks) { 
     95                g_object_unref(env->tasks); 
     96                env->tasks = NULL; 
     97        } 
     98 
     99        osync_context_report_success(ctx); 
     100 
     101        osync_trace(TRACE_EXIT, "%s", __func__); 
     102
     103 
     104static void evo2_etodo_sync_done(void *data, OSyncPluginInfo *info, OSyncContext *ctx) 
     105
     106        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, info, ctx); 
     107        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     108 
     109        char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     110        osync_anchor_update(anchorpath, "todo", env->tasks_path); 
     111        g_free(anchorpath); 
     112 
     113 
     114        GList *changes = NULL; 
     115        e_cal_get_changes(env->tasks, env->change_id, &changes, NULL); 
     116  
     117        osync_context_report_success(ctx); 
     118         
     119        osync_trace(TRACE_EXIT, "%s", __func__); 
     120
     121 
     122void evo2_etodo_report_change(OSyncContext *ctx, OSyncObjFormat *format, char *data, unsigned int size, const char *uid, OSyncChangeType changetype) 
     123
     124        OSyncError *error = NULL; 
     125 
     126        OSyncChange *change = osync_change_new(&error); 
     127        if (!change) { 
     128                osync_context_report_osyncwarning(ctx, error); 
     129                osync_error_unref(&error); 
     130                return; 
     131        } 
     132 
     133        osync_change_set_uid(change, uid); 
     134        osync_change_set_changetype(change, changetype); 
     135 
     136        OSyncData *odata = osync_data_new(data, size, format, &error); 
     137        if (!odata) { 
     138                osync_change_unref(change); 
     139                osync_context_report_osyncwarning(ctx, error); 
     140                osync_error_unref(&error); 
     141                return; 
     142        } 
     143 
     144        osync_change_set_data(change, odata); 
     145        osync_data_unref(odata); 
     146 
     147        osync_context_report_change(ctx, change); 
     148 
     149        osync_change_unref(change); 
     150
     151 
     152 
     153static void evo2_etodo_get_changes(void *indata, OSyncPluginInfo *info, OSyncContext *ctx) 
     154
     155        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, indata, info, ctx); 
     156        OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); 
     157        OSyncEvoEnv *env = (OSyncEvoEnv *)indata; 
     158        OSyncError *error = NULL; 
     159 
     160        GList *changes = NULL; 
     161        ECalChange *ecc = NULL; 
     162        GList *l = NULL; 
     163        char *data = NULL; 
     164        const char *uid = NULL; 
     165        int datasize = 0; 
     166        GError *gerror = NULL; 
     167 
     168        if (osync_objtype_sink_get_slowsync(sink) == FALSE) { 
     169                osync_trace(TRACE_INTERNAL, "No slow_sync for tasks"); 
     170                if (!e_cal_get_changes(env->tasks, env->change_id, &changes, &gerror)) { 
     171                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to open changed tasks entries: %s", gerror ? gerror->message : "None"); 
     172                        goto error; 
     173                } 
     174                osync_trace(TRACE_INTERNAL, "Found %i changes for change-ID %s", g_list_length(changes), env->change_id); 
     175 
     176                for (l = changes; l; l = l->next) { 
     177                        ecc = (ECalChange *)l->data; 
    100178                        e_cal_component_get_uid(ecc->comp, &uid); 
    101179                        e_cal_component_commit_sequence (ecc->comp); 
     
    103181                        switch (ecc->type) { 
    104182                                case E_CAL_CHANGE_ADDED: 
    105                                         data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp)); 
     183                                        data = e_cal_get_component_as_string(env->tasks, e_cal_component_get_icalcomponent(ecc->comp)); 
    106184                                        datasize = strlen(data) + 1; 
    107                                         evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_ADDED); 
     185                                        evo2_etodo_report_change(ctx, env->tasks_format, data, datasize, uid, OSYNC_CHANGE_TYPE_ADDED); 
    108186                                        break; 
    109187                                case E_CAL_CHANGE_MODIFIED: 
    110                                         data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp)); 
     188                                        data = e_cal_get_component_as_string(env->tasks, e_cal_component_get_icalcomponent(ecc->comp)); 
    111189                                        datasize = strlen(data) + 1; 
    112                                         evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_MODIFIED); 
     190                                        evo2_etodo_report_change(ctx, env->tasks_format, data, datasize, uid, OSYNC_CHANGE_TYPE_MODIFIED); 
    113191                                        break; 
    114192                                case E_CAL_CHANGE_DELETED: 
    115                                         evo2_report_change(ctx, "todo", "vtodo20", NULL, 0, uid, CHANGE_DELETED); 
     193                                        evo2_etodo_report_change(ctx, env->tasks_format, NULL, 0, uid, OSYNC_CHANGE_TYPE_DELETED); 
    116194                                        break; 
    117195                        } 
    118                 } 
    119         } else { 
    120                 osync_debug("EVO2-SYNC", 4, "slow_sync for todo"); 
    121         if (!e_cal_get_object_list_as_comp (env->tasks, "(contains? \"any\" \"\")", &changes, &gerror)) { 
    122                         osync_context_send_log(ctx, "Unable to get all todos"); 
    123                         osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get all todos: %s", __func__, gerror ? gerror->message : "None"); 
    124                         g_clear_error(&gerror); 
    125                         return; 
    126         } 
     196                } 
     197        } else { 
     198                osync_trace(TRACE_INTERNAL, "slow_sync for tasks"); 
     199                if (!e_cal_get_object_list_as_comp (env->tasks, "(contains? \"any\" \"\")", &changes, &gerror)) { 
     200                        osync_error_set(&error, OSYNC_ERROR_GENERIC, "Failed to get changes from tasks: %s", gerror ? gerror->message : "None"); 
     201                        goto error; 
     202                } 
    127203                for (l = changes; l; l = l->next) { 
    128204                        ECalComponent *comp = E_CAL_COMPONENT (l->data); 
     
    131207                        e_cal_component_get_uid(comp, &uid); 
    132208                        int datasize = strlen(data) + 1; 
    133                         evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_ADDED); 
     209                        evo2_etodo_report_change(ctx, env->tasks_format, data, datasize, uid, OSYNC_CHANGE_TYPE_ADDED); 
    134210                        g_object_unref (comp); 
    135211                } 
    136212        } 
    137         osync_trace(TRACE_EXIT, "%s", __func__); 
    138 
    139  
    140  
    141 static osync_bool evo2_todo_modify(OSyncContext *ctx, OSyncChange *change) 
    142 
    143         osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, ctx, change); 
    144         evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx); 
    145          
    146         const char *uid = osync_change_get_uid(change); 
    147         char *data = osync_change_get_data(change); 
     213 
     214        osync_context_report_success(ctx); 
     215 
     216        osync_trace(TRACE_EXIT, "%s", __func__); 
     217        return; 
     218 
     219error: 
     220        if (gerror) 
     221                g_clear_error(&gerror); 
     222        osync_context_report_osyncerror(ctx, error); 
     223        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); 
     224        osync_error_unref(&error); 
     225
     226 
     227static void evo2_etodo_modify(void *data, OSyncPluginInfo *info, OSyncContext *ctx, OSyncChange *change) 
     228
     229        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, data, info, ctx, change); 
     230        OSyncEvoEnv *env = (OSyncEvoEnv *)data; 
     231 
     232        const char *uid = osync_change_get_uid(change); 
    148233        icalcomponent *icomp = NULL; 
    149234        char *returnuid = NULL; 
    150         GError *gerror = NULL; 
    151          
    152         switch (osync_change_get_changetype(change)) { 
    153                 case CHANGE_DELETED: 
    154                         if (!e_cal_remove_object(env->tasks, uid, &gerror)) { 
    155                                 osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to delete todo: %s", gerror ? gerror->message : "None"); 
    156                                 osync_trace(TRACE_EXIT_ERROR, "%s: Unable to delete todo: %s", __func__, gerror ? gerror->message : "None"); 
    157                                 g_clear_error(&gerror); 
    158                                 return FALSE; 
    159                         } 
    160                         break; 
    161                 case CHANGE_ADDED: 
    162                         icomp = icalcomponent_new_from_string(data); 
     235        GError *gerror = NULL; 
     236        OSyncError *error = NULL; 
     237        OSyncData *odata = NULL; 
     238        char *plain = NULL; 
     239 
     240        switch (osync_change_get_changetype(change)) { 
     241                case OSYNC_CHANGE_TYPE_DELETED: 
     242                        if (!e_cal_remove_object(env->tasks, uid, &gerror)) { 
     243                                osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unable to delete todo: %s", gerror ? gerror->message : "None"); 
     244