RSSyl: Remove unnecessary use of link() in rssyl_add_msgs().
[claws.git] / src / plugins / rssyl / rssyl.c
index c87fde95f137bf15d1ad225fe62c8e7724023b70..6e2590aadaf9a6bb031ef474d5fa4b8ec5b7f5a4 100644 (file)
@@ -39,6 +39,7 @@
 #include <xml.h>
 #include <toolbar.h>
 #include <prefs_toolbar.h>
+#include <utils.h>
 
 /* Local includes */
 #include "libfeed/feeditem.h"
@@ -271,6 +272,22 @@ static void rssyl_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
                        g_free(ritem->url);
                        ritem->url = g_strdup(attr->value);
                }
+               /* (int) URL auth */
+               if (!strcmp(attr->name, "auth")) {
+                       ritem->auth->type = atoi(attr->value);
+               }
+               /* (str) Auth user */
+               if (!strcmp(attr->name, "auth_user")) {
+                       g_free(ritem->auth->username);
+                       ritem->auth->username = g_strdup(attr->value);
+               }
+               /* (str) Auth pass */
+               if (!strcmp(attr->name, "auth_pass")) {
+                       gsize len = 0;
+                       guchar *pwd = g_base64_decode(attr->value, &len);
+                       g_free(ritem->auth->password);
+                       ritem->auth->password = (gchar *)pwd;
+               }
                /* (str) Official title */
                if( !strcmp(attr->name, "official_title")) {
                        g_free(ritem->official_title);
@@ -317,6 +334,19 @@ static XMLTag *rssyl_item_get_xml(Folder *folder, FolderItem *item)
        /* (str) URL */
        if( ri->url != NULL )
                xml_tag_add_attr(tag, xml_attr_new("uri", ri->url));
+       /* (int) Auth */
+       tmp = g_strdup_printf("%d", ri->auth->type);
+       xml_tag_add_attr(tag, xml_attr_new("auth", tmp));
+       g_free(tmp);
+       /* (str) Auth user */
+       if (ri->auth->username != NULL)
+               xml_tag_add_attr(tag, xml_attr_new("auth_user", ri->auth->username));
+       /* (str) Auth pass */
+       if (ri->auth->password != NULL) {
+               gchar *pwd = g_base64_encode(ri->auth->password, strlen(ri->auth->password));
+               xml_tag_add_attr(tag, xml_attr_new("auth_pass", pwd));
+               g_free(pwd);
+       }
        /* (str) Official title */
        if( ri->official_title != NULL )
                xml_tag_add_attr(tag, xml_attr_new("official_title", ri->official_title));
@@ -395,6 +425,10 @@ static FolderItem *rssyl_item_new(Folder *folder)
        RFolderItem *ritem = g_new0(RFolderItem, 1);
 
        ritem->url = NULL;
+       ritem->auth = g_new0(FeedAuth, 1);
+       ritem->auth->type = FEED_AUTH_NONE;
+       ritem->auth->username = NULL;
+       ritem->auth->password = NULL;
        ritem->official_title = NULL;
        ritem->source_id = NULL;
        ritem->items = NULL;
@@ -419,6 +453,11 @@ static void rssyl_item_destroy(Folder *folder, FolderItem *item)
        g_return_if_fail(ritem != NULL);
 
        g_free(ritem->url);
+       if (ritem->auth->username)
+               g_free(ritem->auth->username);
+       if (ritem->auth->password)
+               g_free(ritem->auth->password);
+       g_free(ritem->auth);
        g_free(ritem->official_title);
        g_slist_free(ritem->items);
 
@@ -646,9 +685,30 @@ static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
        return nummsgs;
 }
 
-static gboolean rssyl_scan_required(Folder *folder, FolderItem *item)
+static gboolean rssyl_is_msg_changed(Folder *folder, FolderItem *item,
+               MsgInfo *msginfo)
 {
-       return TRUE;
+       struct stat s;
+       gchar *path = NULL;
+
+       g_return_val_if_fail(folder != NULL, FALSE);
+       g_return_val_if_fail(item != NULL, FALSE);
+       g_return_val_if_fail(msginfo != NULL, FALSE);
+
+       path = g_strconcat(folder_item_get_path(item), G_DIR_SEPARATOR_S,
+                       itos(msginfo->msgnum), NULL);
+
+       if (g_stat(path, &s) < 0 ||
+               msginfo->size != s.st_size || (
+                               (msginfo->mtime - s.st_mtime != 0) &&
+                               (msginfo->mtime - s.st_mtime != 3600) &&
+                               (msginfo->mtime - s.st_mtime != -3600))) {
+               g_free(path);
+               return TRUE;
+       }
+
+       g_free(path);
+       return FALSE;
 }
 
 static gchar *rssyl_fetch_msg(Folder *folder, FolderItem *item, gint num)
@@ -722,12 +782,10 @@ static gint rssyl_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                g_return_val_if_fail(destfile != NULL, -1);
                debug_print("RSSyl: add_msgs: new filename is '%s'\n", destfile);
 
-               if( link(fileinfo->file, destfile) < 0 ) {
-                       if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
-                               g_warning("can't copy message %s to %s\n", fileinfo->file, destfile);
-                               g_free(destfile);
-                               return -1;
-                       }
+               if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
+                       g_warning("can't copy message %s to %s\n", fileinfo->file, destfile);
+                       g_free(destfile);
+                       return -1;
                }
 
                if( relation != NULL )
@@ -769,7 +827,7 @@ static gint rssyl_remove_msg(Folder *folder, FolderItem *item, gint num)
        file = rssyl_fetch_msg(folder, item, num);
        g_return_val_if_fail(file != NULL, -1);
 
-       need_scan = rssyl_scan_required(folder, item);
+       need_scan = mh_get_class()->scan_required(folder, item);
 
        /* are we doing a folder move ? */
        tmp = g_strdup_printf("%s.tmp", file);
@@ -807,6 +865,7 @@ static gboolean rssyl_subscribe_uri(Folder *folder, const gchar *uri)
 static void rssyl_copy_private_data(Folder *folder, FolderItem *oldi,
                FolderItem *newi)
 {
+       gchar *dpathold, *dpathnew;
        RFolderItem *olditem = (RFolderItem *)oldi,
                                                                        *newitem = (RFolderItem *)newi;
 
@@ -814,15 +873,62 @@ static void rssyl_copy_private_data(Folder *folder, FolderItem *oldi,
        g_return_if_fail(olditem != NULL);
        g_return_if_fail(newitem != NULL);
 
-       if( olditem->url != NULL ) {
+       if (olditem->url != NULL) {
                g_free(newitem->url);
                newitem->url = g_strdup(olditem->url);
        }
 
-       if( olditem->official_title != NULL ) {
+       if (olditem->auth != NULL) {
+               if (newitem->auth != NULL) {
+                       if (newitem->auth->username != NULL) {
+                               g_free(newitem->auth->username);
+                               newitem->auth->username = NULL;
+                       }
+                       if (newitem->auth->password != NULL) {
+                               g_free(newitem->auth->password);
+                               newitem->auth->password = NULL;
+                       }
+                       g_free(newitem->auth);
+               }
+               newitem->auth = g_new0(FeedAuth, 1);
+               newitem->auth->type = olditem->auth->type;
+               if (olditem->auth->username != NULL)
+                       newitem->auth->username = g_strdup(olditem->auth->username);
+               if (olditem->auth->password != NULL)
+                       newitem->auth->password = g_strdup(olditem->auth->password);
+       }
+
+       if (olditem->official_title != NULL) {
                g_free(newitem->official_title);
                newitem->official_title = g_strdup(olditem->official_title);
        }
+
+       if (olditem->source_id != NULL) {
+               g_free(newitem->source_id);
+               newitem->source_id = g_strdup(olditem->source_id);
+       }
+
+       newitem->keep_old = olditem->keep_old;
+       newitem->default_refresh_interval = olditem->default_refresh_interval;
+       newitem->refresh_interval = olditem->refresh_interval;
+       newitem->fetch_comments = olditem->fetch_comments;
+       newitem->fetch_comments_max_age = olditem->fetch_comments_max_age;
+       newitem->silent_update = olditem->silent_update;
+       newitem->write_heading = olditem->write_heading;
+       newitem->ignore_title_rename = olditem->ignore_title_rename;
+       newitem->ssl_verify_peer = olditem->ssl_verify_peer;
+       newitem->refresh_id = olditem->refresh_id;
+       newitem->fetching_comments = olditem->fetching_comments;
+       newitem->last_update = olditem->last_update;
+
+       dpathold = g_strconcat(rssyl_item_get_path(oldi->folder, oldi),
+                       G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
+       dpathnew = g_strconcat(rssyl_item_get_path(newi->folder, newi),
+                       G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
+       move_file(dpathold, dpathnew, TRUE);
+       g_free(dpathold);
+       g_free(dpathnew);
+
 }
 
 /************************************************************************/
@@ -850,7 +956,7 @@ FolderClass *rssyl_folder_get_class()
                rssyl_class.rename_folder = rssyl_rename_folder;
                rssyl_class.remove_folder = rssyl_remove_folder;
                rssyl_class.get_num_list = rssyl_get_num_list;
-               rssyl_class.scan_required = rssyl_scan_required;
+               rssyl_class.scan_required = mh_get_class()->scan_required;
                rssyl_class.item_set_xml = rssyl_item_set_xml;
                rssyl_class.item_get_xml = rssyl_item_get_xml;
 
@@ -863,6 +969,7 @@ FolderClass *rssyl_folder_get_class()
                rssyl_class.add_msgs = rssyl_add_msgs;
                rssyl_class.remove_msg = rssyl_remove_msg;
                rssyl_class.remove_msgs = NULL;
+               rssyl_class.is_msg_changed = rssyl_is_msg_changed;
 //             rssyl_class.change_flags = rssyl_change_flags;
                rssyl_class.change_flags = NULL;
                rssyl_class.subscribe = rssyl_subscribe_uri;