00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <memory>
00020 using namespace std;
00021
00022 #include "config.h"
00023
00024 #ifdef ENABLE_ECAL
00025
00026 #include "EvolutionMemoSource.h"
00027 #include "EvolutionSmartPtr.h"
00028
00029 #include <common/base/Log.h>
00030
00031 SyncItem *EvolutionMemoSource::createItem(const string &luid)
00032 {
00033 logItem( luid, "extracting from EV" );
00034
00035 ItemID id(luid);
00036 eptr<icalcomponent> comp(retrieveItem(id));
00037 auto_ptr<SyncItem> item(new SyncItem(luid.c_str()));
00038
00039 item->setData("", 0);
00040 icalcomponent *cal = icalcomponent_get_first_component(comp, ICAL_VCALENDAR_COMPONENT);
00041 if (!cal) {
00042 cal = comp;
00043 }
00044 icalcomponent *journal = icalcomponent_get_first_component(cal, ICAL_VJOURNAL_COMPONENT);
00045 if (!journal) {
00046 journal = comp;
00047 }
00048 icalproperty *summaryprop = icalcomponent_get_first_property(journal, ICAL_SUMMARY_PROPERTY);
00049 string summary;
00050 if (summaryprop) {
00051 const char *summaryptr = icalproperty_get_summary(summaryprop);
00052 if (summaryptr) {
00053 summary = summaryptr;
00054 }
00055 }
00056 icalproperty *desc = icalcomponent_get_first_property(journal, ICAL_DESCRIPTION_PROPERTY);
00057 if (desc) {
00058 const char *text = icalproperty_get_description(desc);
00059 if (text) {
00060 size_t len = strlen(text);
00061 bool insertSummary = false;
00062 const char *eol;
00063
00064
00065
00066
00067
00068
00069
00070 eol = strchr(text, '\n');
00071 if (!eol) {
00072 eol = text + len;
00073 }
00074 if (summary.size() &&
00075 summary.compare(0, summary.size(), text, eol - text)) {
00076 insertSummary = true;
00077 }
00078
00079
00080
00081 eptr<char> dostext((char *)malloc(len * 2 + 1 +
00082 (insertSummary ? summary.size() + 2 : 0)));
00083 const char *from = text;
00084 char *to = dostext;
00085 if (insertSummary) {
00086 memcpy(to, summary.c_str(), summary.size());
00087 memcpy(to + summary.size(), "\r\n", 2);
00088 to += summary.size() + 2;
00089 }
00090 eol = strchr(from, '\n');
00091 while (eol) {
00092 size_t linelen = eol - from;
00093 memcpy(to, from, linelen);
00094 to += linelen;
00095 from += linelen;
00096 to[0] = '\r';
00097 to[1] = '\n';
00098 to += 2;
00099 from++;
00100
00101 eol = strchr(from, '\n');
00102 }
00103 memcpy(to, from, strlen(from) + 1);
00104 item->setData(dostext, strlen(dostext));
00105 }
00106 }
00107 item->setDataType("text/plain");
00108 item->setModificationTime(0);
00109
00110 return item.release();
00111 }
00112
00113 EvolutionCalendarSource::InsertItemResult EvolutionMemoSource::insertItem(const string &luid, const SyncItem &item)
00114 {
00115 const char *type = item.getDataType();
00116
00117
00118
00119
00120 if (!type[0] ||
00121 !strcasecmp(type, "raw") ||
00122 !strcasecmp(type, "text/x-vcalendar") ||
00123 !strcasecmp(type, "text/calendar")) {
00124 return EvolutionCalendarSource::insertItem(luid, item);
00125 }
00126
00127 bool update = !luid.empty();
00128 bool merged = false;
00129 string newluid = luid;
00130 string modTime;
00131
00132 eptr<char> text;
00133 text.set((char *)malloc(item.getDataSize() + 1), "copy of item");
00134 memcpy(text, item.getData(), item.getDataSize());
00135 text[item.getDataSize()] = 0;
00136
00137
00138 char *from = text, *to = text;
00139 const char *eol = strstr(from, "\r\n");
00140 while (eol) {
00141 size_t linelen = eol - from;
00142 if (to != from) {
00143 memmove(to, from, linelen);
00144 }
00145 to += linelen;
00146 from += linelen;
00147 *to = '\n';
00148 to++;
00149 from += 2;
00150
00151 eol = strstr(from, "\r\n");
00152 }
00153 if (to != from) {
00154 memmove(to, from, strlen(from) + 1);
00155 }
00156
00157 eol = strchr(text, '\n');
00158 string summary;
00159 if (eol) {
00160 summary.insert(0, (char *)text, eol - (char *)text);
00161 } else {
00162 summary = (char *)text;
00163 }
00164
00165 eptr<icalcomponent> subcomp(icalcomponent_vanew(
00166 ICAL_VJOURNAL_COMPONENT,
00167 icalproperty_new_summary(summary.c_str()),
00168 icalproperty_new_description(text),
00169 0));
00170
00171 if( !subcomp ) {
00172 throwError(string("failure creating vjournal " ) + summary);
00173 }
00174
00175 GError *gerror = NULL;
00176 char *uid = NULL;
00177
00178 if (!update) {
00179 if(!e_cal_create_object(m_calendar, subcomp, &uid, &gerror)) {
00180 if (gerror->domain == E_CALENDAR_ERROR &&
00181 gerror->code == E_CALENDAR_STATUS_OBJECT_ID_ALREADY_EXISTS) {
00182
00183
00184
00185 logItem(item, "exists already, updating instead");
00186 merged = true;
00187 g_clear_error(&gerror);
00188 } else {
00189 throwError( "storing new memo item", gerror );
00190 }
00191 } else {
00192 ItemID id(uid, "");
00193 newluid = id.getLUID();
00194 modTime = getItemModTime(id);
00195 }
00196 }
00197
00198 if (update || merged) {
00199 ItemID id(newluid);
00200
00201
00202 if (update && !id.m_uid.empty()) {
00203 icalcomponent_set_uid(subcomp, id.m_uid.c_str());
00204 }
00205
00206 if (!e_cal_modify_object(m_calendar, subcomp, CALOBJ_MOD_ALL, &gerror)) {
00207 throwError(string("updating memo item ") + item.getKey(), gerror);
00208 }
00209 ItemID newid = getItemID(subcomp);
00210 newluid = newid.getLUID();
00211 modTime = getItemModTime(newid);
00212 }
00213
00214 return InsertItemResult(newluid, modTime, merged);
00215 }
00216
00217 #endif