gboolean rssyl_subscribe(FolderItem *parent, const gchar *url, gboolean verbose) { gchar *myurl = NULL, *tmpname = NULL, *tmpname2 = NULL; RFetchCtx *ctx; FolderItem *new_item; RFolderItem *ritem; gint i = 1; RSubCtx *sctx; gboolean edit_properties = FALSE; gchar *official_title = NULL; g_return_val_if_fail(parent != NULL, FALSE); g_return_val_if_fail(url != NULL, FALSE); log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBING, url); myurl = my_normalize_url(url); /* Fetch the feed. */ ctx = rssyl_prep_fetchctx_from_url(myurl); g_free(myurl); g_return_val_if_fail(ctx != NULL, FALSE); rssyl_fetch_feed(ctx, verbose); debug_print("RSSyl: fetch success == %s\n", ctx->success ? "TRUE" : "FALSE"); if (!ctx->success) { /* User notification was already handled inside rssyl_fetch_feed(), * let's just return quietly. */ feed_free(ctx->feed); g_free(ctx->error); g_free(ctx); return FALSE; } if (verbose) { sctx = g_new0(RSubCtx, 1); sctx->feed = ctx->feed; sctx->edit_properties = FALSE; debug_print("RSSyl: Calling subscribe dialog routine...\n"); rssyl_subscribe_dialog(sctx); if (sctx->feed == NULL) { debug_print("RSSyl: User cancelled subscribe.\n"); g_free(sctx); return FALSE; } edit_properties = sctx->edit_properties; if (sctx->official_title != NULL) { debug_print("RSSyl: custom official title\n"); official_title = g_strdup(sctx->official_title); } if (sctx->edit_properties) debug_print("RSSyl: User wants to edit properties of the new feed.\n"); else debug_print("RSSyl: User does not want to edit properties of the new feed.\n"); g_free(sctx->official_title); g_free(sctx); } /* OK, feed is succesfully fetched and correct, let's add it to CM. */ /* Create a folder for it. */ tmpname = rssyl_format_string(ctx->feed->title, TRUE, TRUE); tmpname2 = g_strdup(tmpname); #ifdef G_OS_WIN32 /* Windows does not allow its filenames to start or end with a dot, * or to end with a space. */ if (tmpname2[0] == '.') tmpname2[0] = '_'; if (tmpname2[strlen(tmpname2) - 1] == '.') tmpname2[strlen(tmpname2) - 1] = '_'; if (tmpname2[strlen(tmpname2) - 1] == ' ') tmpname2[strlen(tmpname2) - 1] = '_'; #endif while (folder_find_child_item_by_name(parent, tmpname2) != 0 && i < 20) { debug_print("RSSyl: Folder '%s' already exists, trying another name\n", tmpname2); g_free(tmpname2); tmpname2 = g_strdup_printf("%s__%d", tmpname, ++i); } /* TODO: handle cases where i reaches 20 */ folder_item_update_freeze(); new_item = folder_create_folder(parent, tmpname2); g_free(tmpname); g_free(tmpname2); if (!new_item) { if (verbose) alertpanel_error(_("Couldn't create folder for new feed '%s'."), myurl); feed_free(ctx->feed); g_free(ctx->error); g_free(ctx); g_free(myurl); return FALSE; } debug_print("RSSyl: Adding '%s'\n", ctx->feed->url); ritem = (RFolderItem *)new_item; ritem->url = g_strdup(ctx->feed->url); if (official_title != NULL) { debug_print("RSSyl: storing official feed title '%s'\n", official_title); ritem->official_title = official_title; } if (feed_n_items(ctx->feed) > 0) feed_foreach_item(ctx->feed, rssyl_subscribe_foreach_func, (gpointer)ritem); folder_item_scan(new_item); folder_write_list(); if (edit_properties) rssyl_gtk_prop(ritem); folder_item_update_thaw(); return TRUE; }
gboolean rssyl_parse_feed(RFolderItem *ritem, Feed *feed) { gchar *tmp = NULL, *tmp2 = NULL; gint i = 1; g_return_val_if_fail(ritem != NULL, FALSE); g_return_val_if_fail(feed != NULL, FALSE); g_return_val_if_fail(feed->title != NULL, FALSE); debug_print("RSSyl: parse_feed\n"); /* Set the last_update timestamp here, so it is the same for all items */ ritem->last_update = time(NULL); /* If the upstream feed changed its title, change name of our folder * accordingly even if user has renamed it before. This makes sure that * user will be aware of the upstream title change. */ if( !ritem->ignore_title_rename && (ritem->official_title == NULL || strcmp(feed->title, ritem->official_title)) ) { g_free(ritem->official_title); ritem->official_title = g_strdup(feed->title); tmp = rssyl_format_string(feed->title, TRUE, TRUE); tmp2 = g_strdup(tmp); while (folder_item_rename(&ritem->item, tmp2) != 0 && i < 20) { g_free(tmp2); tmp2 = g_strdup_printf("%s__%d", tmp, ++i); debug_print("RSSyl: couldn't rename, trying '%s'\n", tmp2); } /* TODO: handle case when i reaches 20 */ g_free(tmp); g_free(tmp2); /* FIXME: update name in properties */ /* FIXME: store feed properties */ } folder_item_update_freeze(); /* Read contents of folder, so we can check for duplicates/updates */ rssyl_folder_read_existing(ritem); if( claws_is_exiting() ) { debug_print("RSSyl: Claws-Mail is exiting, bailing out\n"); log_print(LOG_PROTOCOL, RSSYL_LOG_ABORTED_EXITING, ritem->url); folder_item_update_thaw(); return TRUE; } /* Populate the ->deleted_items list so that we can check it when * adding each item. */ ritem->deleted_items = rssyl_deleted_update(ritem); /* Parse each item in the feed, adding or updating existing items if * necessary */ if( feed_n_items(feed) > 0 ) feed_foreach_item(feed, rssyl_foreach_parse_func, (gpointer)ritem); if( !ritem->keep_old && !ritem->fetching_comments ) { rssyl_folder_read_existing(ritem); rssyl_expire_items(ritem, feed); } rssyl_deleted_free(ritem->deleted_items); folder_item_scan(&ritem->item); folder_item_update_thaw(); if( !ritem->fetching_comments ) log_print(LOG_PROTOCOL, RSSYL_LOG_UPDATED, ritem->url); return TRUE; }