More fixes for parsing dates in vcalendar on Windows.
[claws.git] / src / plugins / vcalendar / vcal_manager.c
index 7a4cef28f4f24746f4cb377d3bd0057179baa444..017aa8c444bebb8052d812aada1f2b9c06ba451c 100644 (file)
@@ -347,7 +347,7 @@ gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_
                                 "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
                            icalproperty_new_calscale("GREGORIAN"),
                            icalproperty_new_method(is_reply ? ICAL_METHOD_REPLY:event->method),
-                           0
+                           (void*)0
                            );  
 
        if (!calendar) {
@@ -380,7 +380,7 @@ gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_
 
        ievent = 
            icalcomponent_vanew(
-                ICAL_VEVENT_COMPONENT, 0);
+                ICAL_VEVENT_COMPONENT, (void*)0);
 
        if (!ievent) {
                g_warning ("can't generate event");
@@ -519,24 +519,25 @@ gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_
 
 static void get_rfc822_date_from_time_t(gchar *buf, gint len, time_t t)
 {
+#ifndef G_OS_WIN32
        struct tm *lt;
        gchar day[4], mon[4];
        gint dd, hh, mm, ss, yyyy;
        gchar buft1[512];
        struct tm buft2;
 
-#ifndef G_OS_WIN32
        lt = localtime_r(&t, &buft2);
-#else
-       if (t < 0)
-               t = 1;
-       lt = localtime(&t);
-#endif
-
        sscanf(asctime_r(lt, buft1), "%3s %3s %d %d:%d:%d %d\n",
               day, mon, &dd, &hh, &mm, &ss, &yyyy);
        g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
                   day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
+#else
+       GDateTime *dt = g_date_time_new_from_unix_local(t);
+       gchar *buf2 = g_date_time_format(dt, "%a, %e %b %Y %H:%M:%S %z");
+       g_date_time_unref(dt);
+       strncpy(buf, buf2, len);
+       g_free(buf2);
+#endif
 }
 
 static gchar *write_headers_date(const gchar *uid)
@@ -546,7 +547,6 @@ static gchar *write_headers_date(const gchar *uid)
        gchar date[128];
        time_t t;
        struct tm lt;
-       struct tm buft;
 
        memset(subject, 0, sizeof(subject));
        memset(date, 0, sizeof(date));
@@ -567,11 +567,12 @@ static gchar *write_headers_date(const gchar *uid)
                t = time(NULL) + (86400*7);
                t_subject = _("Later");
        }  else {
-               g_warning("unknown spec date\n");
+               g_warning("unknown spec date");
                return NULL;
        } 
        
 #ifndef G_OS_WIN32
+       struct tm buft;
        lt = *localtime_r(&t, &buft);
 #else
        if (t < 0)
@@ -700,7 +701,7 @@ gchar *vcal_manager_icalevent_dump(icalcomponent *event, gchar *orga, icalcompon
                                 "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
                            icalproperty_new_calscale("GREGORIAN"),
                            icalproperty_new_method(ICAL_METHOD_PUBLISH),
-                           0
+                           (void*)0
                            );  
 
        if (!calendar) {
@@ -729,18 +730,19 @@ gchar *vcal_manager_icalevent_dump(icalcomponent *event, gchar *orga, icalcompon
        
        /* encode to quoted-printable */
        while (lines[i]) {
-               gchar buf[256];
                gint e_len = strlen(qpbody), n_len = 0;
                gchar *outline = conv_codeset_strdup(lines[i], CS_UTF_8, conv_get_outgoing_charset_str());
+               gchar *qpoutline = g_malloc(strlen(outline)*8 + 1);
                
-               qp_encode_line(buf, (guchar *)outline);
-               n_len = strlen(buf);
+               qp_encode_line(qpoutline, (guchar *)outline);
+               n_len = strlen(qpoutline);
                
                qpbody = g_realloc(qpbody, e_len + n_len + 1);
-               strcpy(qpbody+e_len, buf);
+               strcpy(qpbody+e_len, qpoutline);
                *(qpbody+n_len+e_len) = '\0';
                
                g_free(outline);
+               g_free(qpoutline);
                i++;
        }
        
@@ -786,16 +788,28 @@ VCalEvent * vcal_manager_new_event        (const gchar    *uid,
 
        if (dtend && *(dtend)) {
                time_t tmp = icaltime_as_timet((icaltime_from_string(dtend)));
+#ifdef G_OS_WIN32
+               GDateTime *dt = g_date_time_new_from_unix_local(tmp);
+               event->end = g_date_time_format(dt, "%a, %e %b %Y %H:%M:%S %z");
+               g_date_time_unref(dt);
+#else
                gchar buft[512];
                tzset();
                event->end      = g_strdup(ctime_r(&tmp, buft));
+#endif
        }
        
        if (dtstart && *(dtstart)) {
                time_t tmp = icaltime_as_timet((icaltime_from_string(dtstart)));
+#ifdef G_OS_WIN32
+               GDateTime *dt = g_date_time_new_from_unix_local(tmp);
+               event->start = g_date_time_format(dt, "%a, %e %b %Y %H:%M:%S %z");
+               g_date_time_unref(dt);
+#else
                gchar buft[512];
                tzset();
                event->start    = g_strdup(ctime_r(&tmp, buft));
+#endif
        }
        event->dtstart          = g_strdup(dtstart?dtstart:"");
        event->dtend            = g_strdup(dtend?dtend:"");
@@ -920,7 +934,7 @@ void vcal_manager_save_event (VCalEvent *event, gboolean export_after)
        xml_tag_add_attr(tag, xml_attr_new("type", tmp));
        g_free(tmp);
        
-       tmp = g_strdup_printf("%lu", event->postponed);
+       tmp = g_strdup_printf("%lld", (long long)event->postponed);
        xml_tag_add_attr(tag, xml_attr_new("postponed", tmp));
        g_free(tmp);
        
@@ -972,7 +986,7 @@ void vcal_manager_save_event (VCalEvent *event, gboolean export_after)
        xml_free_tree(rootnode);
 
        if (prefs_file_close(pfile) < 0) {
-               g_warning("failed to write event.\n");
+               g_warning("failed to write event.");
                return;
        }
  
@@ -989,7 +1003,7 @@ static VCalEvent *event_get_from_xml (const gchar *uid, GNode *node)
        gchar *description = NULL, *url = NULL, *recur = NULL;
        VCalEvent *event = NULL;
        enum icalproperty_method method = ICAL_METHOD_REQUEST;
-       enum icalproperty_kind type = ICAL_VEVENT_COMPONENT;
+       enum icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
        gint sequence = 0, rec_occurence = 0;
        time_t postponed = (time_t)0;
        
@@ -997,7 +1011,7 @@ static VCalEvent *event_get_from_xml (const gchar *uid, GNode *node)
 
        xmlnode = node->data;
        if (strcmp2(xmlnode->tag->tag, "event") != 0) {
-               g_warning("tag name != \"event\"\n");
+               g_warning("tag name != \"event\"");
                return NULL;
        }
        
@@ -1065,7 +1079,7 @@ static VCalEvent *event_get_from_xml (const gchar *uid, GNode *node)
                
                xmlnode = node->data;
                if (strcmp2(xmlnode->tag->tag, "answer") != 0) {
-                       g_warning("tag name != \"answer\"\n");
+                       g_warning("tag name != \"answer\"");
                        return event;
                }
                list = xmlnode->tag->attr;
@@ -1111,7 +1125,7 @@ VCalEvent *vcal_manager_load_event (const gchar *uid)
        g_free(path);
        
        if (!node) {
-               g_warning("no node\n");
+               g_warning("no node");
                return NULL;
        }
        
@@ -1245,11 +1259,14 @@ static gchar *write_headers(PrefsAccount        *account,
                else if (status == ICAL_PARTSTAT_TENTATIVE)
                        prefix = _("Tentatively Accepted: ");
                else 
-                       prefix = "Re: ";
-       } else if (event->method == ICAL_METHOD_PUBLISH)
+                       prefix = "Re: "; 
+       } else if (event->method == ICAL_METHOD_PUBLISH) {
                method_str = "PUBLISH";
-       else
-               method_str = "REQUEST";         
+       } else if (event->method == ICAL_METHOD_CANCEL) {
+               method_str = "CANCEL";
+       } else {
+               method_str = "REQUEST";
+       }
        
        subject = g_strdup_printf("%s%s", prefix, event->summary);
 
@@ -1420,7 +1437,7 @@ static gboolean vcal_manager_send (PrefsAccount   *account,
 
        folderitem = account_get_special_folder(account, F_QUEUE);
        if (!folderitem) {
-               g_warning("can't find queue folder for %s\n", account->address);
+               g_warning("can't find queue folder for %s", account->address);
                g_unlink(tmpfile);
                g_free(tmpfile);
                return FALSE;
@@ -1428,7 +1445,7 @@ static gboolean vcal_manager_send (PrefsAccount   *account,
        folder_item_scan(folderitem);
        
        if ((msgnum = folder_item_add_msg(folderitem, tmpfile, NULL, TRUE)) < 0) {
-               g_warning("can't queue the message\n");
+               g_warning("can't queue the message");
                g_unlink(tmpfile);
                g_free(tmpfile);
                return FALSE;
@@ -1478,7 +1495,6 @@ EventTime event_to_today(VCalEvent *event, time_t t)
        struct tm evtstart, today;
        time_t evtstart_t, today_t;
        struct icaltimetype itt;
-       struct tm buft;
 
        tzset();
        
@@ -1491,6 +1507,7 @@ EventTime event_to_today(VCalEvent *event, time_t t)
        }
        
 #ifndef G_OS_WIN32
+       struct tm buft;
        today = *localtime_r(&today_t, &buft);
        localtime_r(&evtstart_t, &evtstart);
 #else