Real fix for Coverity 1372362.
[claws.git] / src / plugins / vcalendar / vcal_folder.c
index 76928f52b21b7f6860c5567de97f90c70afd6c18..6ed463dce0b0e98a0b08501849cb3b56cc3a9162 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and 
+ * Copyright (C) 1999-2015 Colin Leroy <colin@colino.net> and
  * the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -61,6 +60,7 @@
 #include "mainwindow.h"
 #include "statusbar.h"
 #include "msgcache.h"
+#include "passwordstore.h"
 #include "timing.h"
 #include "messageview.h"
 
@@ -164,7 +164,7 @@ static char *vcal_popup_labels[] =
 {
        N_("_New meeting..."),
        N_("_Export calendar..."),
-       N_("_Subscribe to webCal..."),
+       N_("_Subscribe to Webcal..."),
        N_("_Unsubscribe..."),
        N_("_Rename..."),
        N_("U_pdate subscriptions"),
@@ -706,9 +706,10 @@ add_new:
 
 GSList *vcal_get_events_list(FolderItem *item)
 {
-       DIR *dp;
-       struct dirent *d;
+       GDir *dp;
+       const gchar *d;
        GSList *events = NULL;
+       GError *error = NULL;
 
        if (item != item->folder->inbox) {
                GSList *subs = vcal_folder_get_webcal_events_for_folder(item);
@@ -725,27 +726,29 @@ GSList *vcal_get_events_list(FolderItem *item)
                return events;
        }
 
-       dp = opendir(vcal_manager_get_event_path());
+       dp = g_dir_open(vcal_manager_get_event_path(), 0, &error);
        
        if (!dp) {
-               FILE_OP_ERROR(vcal_manager_get_event_path(), "opendir");
+               debug_print("couldn't open dir '%s': %s (%d)\n",
+                               vcal_manager_get_event_path(), error->message, error->code);
+               g_error_free(error);
                return 0;
        }
 
-       while ((d = readdir(dp)) != NULL) {
+       while ((d = g_dir_read_name(dp)) != NULL) {
                VCalEvent *event = NULL;
-               if (d->d_name[0] == '.' || strstr(d->d_name, ".bak")
-               ||  !strcmp(d->d_name, "internal.ics")
-               ||  !strcmp(d->d_name, "internal.ifb")
-               ||  !strcmp(d->d_name, "multisync")) 
+               if (d[0] == '.' || strstr(d, ".bak")
+               ||  !strcmp(d, "internal.ics")
+               ||  !strcmp(d, "internal.ifb")
+               ||  !strcmp(d, "multisync")) 
                        continue;
 
-               event = vcal_manager_load_event(d->d_name);
+               event = vcal_manager_load_event(d);
                if (!event)
                        continue;
-               if (event->rec_occurence) {
+               if (event->rec_occurrence) {
                        vcal_manager_free_event(event);
-                       claws_unlink(d->d_name);
+                       claws_unlink(d);
                        continue;
                }
 
@@ -771,7 +774,7 @@ GSList *vcal_get_events_list(FolderItem *item)
                                struct icaldurationtype ical_dur;
                                int i = 0;
 
-                               debug_print("dumping recurring events from main event %s\n", d->d_name);
+                               debug_print("dumping recurring events from main event %s\n", d);
                                recur = icalrecurrencetype_from_string(event->recur);
                                dtstart = icaltime_from_string(event->dtstart);
 
@@ -801,7 +804,7 @@ GSList *vcal_get_events_list(FolderItem *item)
                                                                event->sequence, event->type);
                                        g_free(uid);
                                        vcal_manager_copy_attendees(event, nevent);
-                                       nevent->rec_occurence = TRUE;
+                                       nevent->rec_occurrence = TRUE;
                                        vcal_manager_save_event(nevent, FALSE);
                                        account = vcal_manager_get_account_from_event(event);
                                        status =
@@ -822,7 +825,7 @@ GSList *vcal_get_events_list(FolderItem *item)
                        vcal_manager_free_event(event);
                }
        }
-       closedir(dp);
+       g_dir_close(dp);
        return g_slist_reverse(events);
 }
 
@@ -860,7 +863,7 @@ static gint vcal_get_num_list(Folder *folder, FolderItem *item,
                        continue;
                g_hash_table_insert(hash_uids, GINT_TO_POINTER(n_msg), g_strdup(event->uid));
                
-               if (event->rec_occurence) {
+               if (event->rec_occurrence) {
                        vcal_manager_free_event(event);
                        continue;
                }
@@ -1051,7 +1054,7 @@ static gint vcal_remove_msg(Folder *folder, FolderItem *_item, gint num)
        if (_item == folder->inbox)
                vcal_remove_event(folder, msginfo);
 
-       procmsg_msginfo_free(msginfo);
+       procmsg_msginfo_free(&msginfo);
        return 0;
 }
 
@@ -1124,7 +1127,7 @@ static gint vcal_remove_folder(Folder *folder, FolderItem *fitem)
 
 static gboolean vcal_scan_required(Folder *folder, FolderItem *item)
 {
-       struct stat s;
+       GStatBuf s;
        VCalFolderItem *vitem = (VCalFolderItem *)item;
 
        g_return_val_if_fail(item != NULL, FALSE);
@@ -1144,7 +1147,7 @@ static gint vcal_folder_lock_count = 0;
 
 static void vcal_set_mtime(Folder *folder, FolderItem *item)
 {
-       struct stat s;
+       GStatBuf s;
        gchar *path = folder_item_get_path(item);
 
        if (folder->inbox != item)
@@ -1159,7 +1162,8 @@ static void vcal_set_mtime(Folder *folder, FolderItem *item)
        }
 
        item->mtime = s.st_mtime;
-       debug_print("VCAL: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);
+       debug_print("VCAL: forced mtime of %s to %lld\n",
+                       item->name?item->name:"(null)", (long long)item->mtime);
        g_free(path);
 }
 
@@ -1167,31 +1171,45 @@ void vcal_folder_export(Folder *folder)
 {      
        FolderItem *item = folder?folder->inbox:NULL;
        gboolean need_scan = folder?vcal_scan_required(folder, item):TRUE;
+       gchar *export_pass = NULL;
+       gchar *export_freebusy_pass = NULL;
 
        if (vcal_folder_lock_count) /* blocked */
                return;
        vcal_folder_lock_count++;
+       
+       export_pass = vcal_passwd_get("export");
+       export_freebusy_pass = vcal_passwd_get("export_freebusy");
+
        if (vcal_meeting_export_calendar(vcalprefs.export_path, 
                        vcalprefs.export_user, 
-                       vcalprefs.export_pass,
+                       export_pass,
                        TRUE)) {
                debug_print("exporting calendar\n");
                if (vcalprefs.export_enable &&
                    vcalprefs.export_command &&
                    strlen(vcalprefs.export_command))
                        execute_command_line(
-                               vcalprefs.export_command, TRUE);
+                               vcalprefs.export_command, TRUE, NULL);
        }
+       if (export_pass != NULL) {
+               memset(export_pass, 0, strlen(export_pass));
+       }
+       g_free(export_pass);
        if (vcal_meeting_export_freebusy(vcalprefs.export_freebusy_path,
                        vcalprefs.export_freebusy_user,
-                       vcalprefs.export_freebusy_pass)) {
+                       export_freebusy_pass)) {
                debug_print("exporting freebusy\n");
                if (vcalprefs.export_freebusy_enable &&
                    vcalprefs.export_freebusy_command &&
                    strlen(vcalprefs.export_freebusy_command))
                        execute_command_line(
-                               vcalprefs.export_freebusy_command, TRUE);
+                               vcalprefs.export_freebusy_command, TRUE, NULL);
+       }
+       if (export_freebusy_pass != NULL) {
+               memset(export_freebusy_pass, 0, strlen(export_freebusy_pass));
        }
+       g_free(export_freebusy_pass);
        vcal_folder_lock_count--;
        if (!need_scan && folder) {
                vcal_set_mtime(folder, folder->inbox);
@@ -1702,7 +1720,7 @@ gboolean vcal_curl_put(gchar *url, FILE *fp, gint filesize, const gchar *user, c
 #endif
        curl_easy_setopt(curl_ctx, CURLOPT_USERAGENT, 
                "Claws Mail vCalendar plugin "
-               "(http://www.claws-mail.org/plugins.php)");
+               "(" PLUGINS_URI ")");
        curl_easy_setopt(curl_ctx, CURLOPT_INFILESIZE, filesize);
        res = curl_easy_perform(curl_ctx);
        g_free(userpwd);
@@ -1715,7 +1733,7 @@ gboolean vcal_curl_put(gchar *url, FILE *fp, gint filesize, const gchar *user, c
 
        curl_easy_getinfo(curl_ctx, CURLINFO_RESPONSE_CODE, &response_code);
        if (response_code < 200 || response_code >= 300) {
-               g_warning("Can't export calendar, got code %ld\n", response_code);
+               g_warning("Can't export calendar, got code %ld", response_code);
                res = FALSE;
        }
        curl_easy_cleanup(curl_ctx);
@@ -1780,7 +1798,7 @@ static void update_subscription_finish(const gchar *uri, gchar *feed, gboolean v
        icalcomponent *cal = NULL;
        
        if (root == NULL) {
-               g_warning("can't get root folder\n");
+               g_warning("can't get root folder");
                g_free(feed);
                if (error)
                        g_free(error);
@@ -1814,10 +1832,10 @@ static void update_subscription_finish(const gchar *uri, gchar *feed, gboolean v
        }
        if (strncmp(feed, "BEGIN:VCALENDAR", strlen("BEGIN:VCALENDAR"))) {
                if (verbose && manual_update) {
-                       alertpanel_error(_("This URL does not look like a WebCal URL:\n%s\n%s"),
+                       alertpanel_error(_("This URL does not look like a Webcal URL:\n%s\n%s"),
                                        uri, error ? error:_("Unknown error"));
                } else  {
-                       log_error(LOG_PROTOCOL, _("This URL does not look like a WebCal URL:\n%s\n%s\n"),
+                       log_error(LOG_PROTOCOL, _("This URL does not look like a Webcal URL:\n%s\n%s\n"),
                                        uri, error ? error:_("Unknown error"));
                }
                g_free(feed);
@@ -1834,14 +1852,10 @@ static void update_subscription_finish(const gchar *uri, gchar *feed, gboolean v
                gchar *title = feed_get_title(feed);
                if (title == NULL) {
                        if (strstr(uri, "://"))
-                               title = g_strdup(strstr(uri,"://")+3);
+                               title = g_path_get_basename(strstr(uri,"://")+3);
                        else
                                title = g_strdup(uri);
                        subst_for_filename(title);
-                       if (strlen(title) > 32) {
-                               title[29]=title[30]=title[31]='.';
-                               title[32]='\0';
-                       }
                }
                item = folder_create_folder(root->node->data, title);
                if (!item) {
@@ -1922,7 +1936,7 @@ static void subscribe_cal_cb(GtkAction *action, gpointer data)
        gchar *uri = NULL;
        gchar *tmp = NULL;
 
-       tmp = input_dialog(_("Subscribe to WebCal"), _("Enter the WebCal URL:"), NULL);
+       tmp = input_dialog(_("Subscribe to Webcal"), _("Enter the WebCal URL:"), NULL);
        if (tmp == NULL)
                return;
        
@@ -1948,22 +1962,22 @@ static void subscribe_cal_cb(GtkAction *action, gpointer data)
 static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message;
        AlertValue avalue;
        gchar *old_id;
 
        if (!folderview->selected) return;
 
-       item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
+       item = folderview_get_selected_item(folderview);
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
+       opened = folderview_get_opened_item(folderview);
 
        message = g_strdup_printf
                (_("Do you really want to unsubscribe?"));
-       avalue = alertpanel_full(_("Delete folder"), message,
+       avalue = alertpanel_full(_("Delete subscription"), message,
                                 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, 
                                 FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
        g_free(message);
@@ -1973,12 +1987,10 @@ static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
 
        vcal_item_closed(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
@@ -2063,14 +2075,13 @@ static void set_view_cb(GtkAction *gaction, GtkRadioAction *current, gpointer da
 {
        FolderView *folderview = (FolderView *)data;
        gint action = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
        FolderItem *item = NULL, *oitem = NULL;
 
        if (!folderview->selected) return;
        if (setting_sensitivity) return;
 
-       oitem = gtk_cmctree_node_get_row_data(ctree, folderview->opened);
-       item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
+       oitem = folderview_get_opened_item(folderview);
+       item = folderview_get_selected_item(folderview);
 
        if (!item)
                return;
@@ -2096,7 +2107,7 @@ gchar *vcal_get_event_as_ical_str(VCalEvent *event)
             icalproperty_new_prodid(
                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
            icalproperty_new_calscale("GREGORIAN"),
-            0);
+            (void*)0);
        vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
        ical = g_strdup(icalcomponent_as_ical_string(calendar));
        icalcomponent_free(calendar);
@@ -2134,6 +2145,43 @@ static gchar *get_email_from_property(icalproperty *p)
        return email;
 }
 
+static void adjust_for_local_time_zone(icalproperty *eventtime, icalproperty *tzoffsetto, int dtstart)
+{
+       int tzoffset;
+       int loctzoffset;
+       time_t loctime, gmttime, evttime;
+       struct icaltimetype icaltime;
+
+       /* calculate local UTC offset */
+       loctime = time(NULL);
+       loctime = mktime(localtime(&loctime));
+       gmttime = mktime(gmtime(&loctime));
+       loctzoffset = loctime - gmttime;
+
+       if (eventtime && tzoffsetto) {
+               tzoffset = icalproperty_get_tzoffsetto(tzoffsetto);
+               if (dtstart) {
+                       evttime = icaltime_as_timet(icalproperty_get_dtstart(eventtime));
+               }
+               else {
+                       evttime = icaltime_as_timet(icalproperty_get_dtend(eventtime));
+               }
+
+               /* convert to UTC */
+               evttime -= tzoffset;
+               /* and adjust for local time zone */
+               evttime += loctzoffset;
+               icaltime = icaltime_from_timet(evttime, 0);
+
+               if (dtstart) {
+                       icalproperty_set_dtstart(eventtime, icaltime);
+               }
+               else {
+                       icalproperty_set_dtend(eventtime, icaltime);
+               } 
+       }
+}
+
 #define GET_PROP(comp,prop,kind) {                                             \
        prop = NULL;                                                            \
        if (!(prop = icalcomponent_get_first_property(comp, kind))) {           \
@@ -2173,6 +2221,7 @@ VCalEvent *vcal_get_event_from_ical(const gchar *ical, const gchar *charset)
        gchar *int_ical = g_strdup(ical);
        icalcomponent *comp = icalcomponent_new_from_string(int_ical);
        icalcomponent *inner = NULL;
+       icalcomponent *tzcomp = NULL;
        icalproperty *prop = NULL;
        GSList *list = NULL, *cur = NULL;
        gchar *uid = NULL;
@@ -2216,6 +2265,31 @@ VCalEvent *vcal_get_event_from_ical(const gchar *ical, const gchar *charset)
                TO_UTF8(summary);
                icalproperty_free(prop);
        }
+       tzcomp = icalcomponent_get_first_component(comp, ICAL_VTIMEZONE_COMPONENT);
+       if (tzcomp) {
+               icalproperty *evtstart = NULL;
+               icalproperty *evtend = NULL;
+               icalproperty *tzoffsetto = NULL;
+               icalcomponent *tzstd = NULL;
+
+               tzstd = icalcomponent_get_first_component(tzcomp, ICAL_XSTANDARD_COMPONENT);
+               tzoffsetto = icalcomponent_get_first_property(tzstd, ICAL_TZOFFSETTO_PROPERTY);
+
+               GET_PROP(comp, evtstart, ICAL_DTSTART_PROPERTY);
+               adjust_for_local_time_zone(evtstart, tzoffsetto, TRUE);
+
+               GET_PROP(comp, evtend, ICAL_DTEND_PROPERTY);
+               adjust_for_local_time_zone(evtend, tzoffsetto, FALSE);
+
+               if (tzoffsetto)
+                       icalproperty_free(tzoffsetto);
+               if (evtstart)
+                       icalproperty_free(evtstart);
+               if (evtend)
+                       icalproperty_free(evtend);
+               if (tzstd)
+                       icalcomponent_free(tzstd);
+       }
        GET_PROP(comp, prop, ICAL_DTSTART_PROPERTY);
        if (prop) {
                dtstart = g_strdup(icaltime_as_ical_string(icalproperty_get_dtstart(prop)));
@@ -2352,7 +2426,7 @@ gboolean vcal_event_exists(const gchar *id)
 
        info = folder_item_get_msginfo_by_msgid(folder->inbox, id);
        if (info != NULL) {
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                return TRUE;
        }
        return FALSE;
@@ -2390,7 +2464,7 @@ gboolean vcal_delete_event(const gchar *id)
        if (info != NULL) {
                debug_print("removing event %s\n", id);
                vcal_remove_event(folder, info);
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                folder_item_scan(folder->inbox);
                return TRUE;
        }