Beispiel #1
0
PRIVATE int parseFeed(rss_feeds *feeds, const char* feedstr) {
  rss_feed* feed = NULL;
  int32_t result = SUCCESS; /* be optimistic */
  simple_list option_list = NULL;
  NODE * current = NULL;
  suboption_t *opt_item = NULL;

  option_list = parseMultiOption(feedstr);
  current = option_list;

  while (current != NULL) {
    opt_item = (suboption_t*)current->data;

    if(opt_item != NULL) {
      if(!feed) {
        feed = feed_new();
        assert(feed && "feed_new() failed!");
      }

      if(!strncmp(opt_item->option, "url_pattern", 11)) {
        feed->url_pattern = trim(opt_item->value);
      } else if(!strncmp(opt_item->option, "url_replace", 11)) {
        feed->url_replace = trim(opt_item->value);
      } else if(!strncmp(opt_item->option, "url", 3)) {
        feed->url = trim(opt_item->value);
      } else if(!strncmp(opt_item->option, "cookies", 6)) {
        feed->cookies = trim(opt_item->value);
      } else if(!strncmp(opt_item->option, "id", 2)) {
        feed->id = trim(opt_item->value);
      } else {
        dbg_printf(P_ERROR, "Unknown suboption '%s'!", opt_item->option);
      }
    } else {
      assert(0 && "opt_item == NULL");
    }

    current = current->next;
  }

  if(feed && feed->url) {
    /* Maybe the cookies are encoded within the URL */
    if(feed->cookies == NULL) {
      parseCookiesFromURL(feed);
    }

    feed_add(feed, feeds);
  } else {
    dbg_printf(P_ERROR, "Invalid feed: '%s'", feedstr);
    feed_free(feed);
    result = FAILURE;
  }

  if(option_list != NULL) {
    freeList(&option_list, freeOptionItem);
  }

  return result;
}
Beispiel #2
0
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;
}