From 522b3ac678824f06ce68e8c42a2da2ca854c909a Mon Sep 17 00:00:00 2001 From: Dale Whittaker Date: Tue, 28 Apr 2009 23:31:37 +0200 Subject: [PATCH] Prevent attempts to add duplicate feeds --- extensions/feed-panel/feed-panel.c | 69 ++++++++++++++-------- extensions/feed-panel/main.c | 91 ++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 47 deletions(-) diff --git a/extensions/feed-panel/feed-panel.c b/extensions/feed-panel/feed-panel.c index 1389e969..afa7f66f 100644 --- a/extensions/feed-panel/feed-panel.c +++ b/extensions/feed-panel/feed-panel.c @@ -131,7 +131,7 @@ feed_panel_treeview_render_text_cb (GtkTreeViewColumn* column, } static void -feed_panel_add_item_cb (KatzeArray* feed, +feed_panel_add_item_cb (KatzeArray* parent, KatzeItem* child, FeedPanel* panel) { @@ -141,33 +141,36 @@ feed_panel_add_item_cb (KatzeArray* feed, KatzeItem* item; gint i; - g_return_if_fail (KATZE_IS_ARRAY (feed)); + g_return_if_fail (KATZE_IS_ARRAY (parent)); g_return_if_fail (KATZE_IS_ITEM (child)); model = gtk_tree_view_get_model (GTK_TREE_VIEW (panel->treeview)); - if (KATZE_IS_ARRAY (child)) - { - gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter, - NULL, G_MAXINT, 0, child, -1); - } - else + if (katze_item_get_parent (KATZE_ITEM (parent))) { - - i = 0; - while (gtk_tree_model_iter_nth_child (model, &iter, NULL, i++)) + if (KATZE_IS_ARRAY (child)) { - gtk_tree_model_get (model, &iter, 0, &item, -1); - if (item == KATZE_ITEM (feed)) + gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter, + NULL, G_MAXINT, 0, child, -1); + } + else + { + + i = 0; + while (gtk_tree_model_iter_nth_child (model, &iter, NULL, i++)) { - gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter, - &iter, 0, 0, child, -1); + gtk_tree_model_get (model, &iter, 0, &item, -1); + if (item == KATZE_ITEM (parent)) + { + gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter, + &iter, 0, 0, child, -1); - g_object_unref (child); + g_object_unref (child); + g_object_unref (item); + break; + } g_object_unref (item); - break; } - g_object_unref (item); } } feed_panel_insert_item (panel, GTK_TREE_STORE (model), &child_iter, child); @@ -199,21 +202,33 @@ feed_panel_remove_iter (GtkTreeModel* model, } static void -feed_panel_remove_item_cb (KatzeArray* feed, +feed_panel_remove_item_cb (KatzeArray* item, KatzeItem* child, FeedPanel* panel) { GtkTreeModel* model; + KatzeItem* pitem; - g_assert (KATZE_IS_ARRAY (feed)); + g_assert (KATZE_IS_ARRAY (item)); g_assert (KATZE_IS_ITEM (child)); if (KATZE_IS_ARRAY (child)) feed_panel_disconnect_feed (panel, KATZE_ARRAY (child)); + if (!katze_item_get_parent (KATZE_ITEM (item))) + { + gint n; + + n = katze_array_get_length (KATZE_ARRAY (child)); + g_assert (n == 1); + pitem = katze_array_get_nth_item (KATZE_ARRAY (child), 0); + } + else + pitem = child; + model = gtk_tree_view_get_model (GTK_TREE_VIEW (panel->treeview)); - feed_panel_remove_iter (model, child); - g_object_unref (child); + feed_panel_remove_iter (model, pitem); + g_object_unref (pitem); } static void @@ -362,13 +377,17 @@ feed_panel_cursor_or_row_changed_cb (GtkTreeView* treeview, if (date) { SoupDate* sdate; - const gchar* suri; + const gchar* puri; + KatzeItem* parent; sdate = soup_date_new_from_time_t ((time_t) date); - suri = (const gchar*)g_object_get_data (G_OBJECT (item), "feeduri"); + + parent = katze_item_get_parent (item); + g_assert (KATZE_IS_ARRAY (parent)); + puri = katze_item_get_uri (parent); text = g_strdup_printf ( "feed" "

%s

Last updated %s.", - suri, soup_date_to_string (sdate, SOUP_DATE_HTTP)); + puri, soup_date_to_string (sdate, SOUP_DATE_HTTP)); soup_date_free (sdate); } } diff --git a/extensions/feed-panel/main.c b/extensions/feed-panel/main.c index 9f72973c..af360060 100644 --- a/extensions/feed-panel/main.c +++ b/extensions/feed-panel/main.c @@ -58,7 +58,6 @@ typedef struct enum { - FEED_NEW, FEED_READ, FEED_REMOVE }; @@ -104,10 +103,33 @@ feed_add_item (KatzeArray* feeds, if (uri) { - feed = katze_array_new (KATZE_TYPE_ITEM); - g_object_set_data_full (G_OBJECT (feed), "feeduri", - (gpointer) g_strdup ((uri)), g_free); - katze_array_add_item (feeds, feed); + if (katze_array_find_token (feeds, uri)) + { + GtkWidget* dialog; + + dialog = gtk_message_dialog_new ( + NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, + _("Error")); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), + _("Feed %s already exists"), uri); + gtk_window_set_title (GTK_WINDOW (dialog), EXTENSION_NAME); + gtk_widget_show (dialog); + g_signal_connect_swapped (dialog, "response", + G_CALLBACK (gtk_widget_destroy), dialog); + + } + else + { + KatzeArray* child; + + feed = katze_array_new (KATZE_TYPE_ARRAY); + child = katze_array_new (KATZE_TYPE_ITEM); + katze_item_set_uri (KATZE_ITEM (feed), uri); + katze_item_set_token (KATZE_ITEM (feed), uri); + katze_item_set_uri (KATZE_ITEM (child), uri); + katze_array_add_item (feeds, feed); + katze_array_add_item (feed, child); + } } return feed; } @@ -129,7 +151,7 @@ feed_save_items (MidoriExtension* extension, for (i = 0; i < n; i++) { item = katze_array_get_nth_item (feed, i); - sfeeds[i] = (gchar*) g_object_get_data (G_OBJECT (item), "feeduri"); + sfeeds[i] = (gchar*) katze_item_get_uri (KATZE_ITEM (item)); } sfeeds[n] = NULL; @@ -141,12 +163,19 @@ static void feed_handle_net_error (FeedNetPrivate* netpriv, const gchar* msg) { + KatzeItem* child; const gchar* uri; + gint n; + + n = katze_array_get_length (netpriv->feed); + g_assert (n == 1); + child = katze_array_get_nth_item (netpriv->feed, 0); + g_assert (KATZE_IS_ARRAY (child)); - uri = (gchar*) g_object_get_data (G_OBJECT (netpriv->feed), "feeduri"); - katze_item_set_name (KATZE_ITEM (netpriv->feed), uri); - katze_item_set_text (KATZE_ITEM (netpriv->feed), msg); - katze_item_set_uri (KATZE_ITEM (netpriv->feed), NULL); + uri = katze_item_get_uri (KATZE_ITEM (netpriv->feed)); + katze_item_set_name (child, uri); + katze_item_set_text (child, msg); + katze_item_set_uri (child, NULL); feed_remove_flags (netpriv->feed, FEED_READ); } @@ -184,8 +213,19 @@ feed_transfer_cb (KatzeNetRequest* request, if (request->data) { + KatzeArray* item; + const gchar* uri; + gint n; + + n = katze_array_get_length (netpriv->feed); + g_assert (n == 1); + item = katze_array_get_nth_item (netpriv->feed, 0); + g_assert (KATZE_IS_ARRAY (item)); + uri = katze_item_get_uri (KATZE_ITEM (netpriv->feed)); + katze_item_set_uri (KATZE_ITEM (item), uri); + if (!parse_feed (request->data, request->length, - netpriv->parsers, netpriv->feed, &error)) + netpriv->parsers, item, &error)) { feed_handle_net_error (netpriv, error->message); g_error_free (error); @@ -216,11 +256,8 @@ update_feed (FeedPrivate* priv, if (!(feed_has_flags (feed, FEED_READ))) { FeedNetPrivate* netpriv; - gchar* uri; - uri = (gchar*) g_object_get_data (G_OBJECT (feed), "feeduri"); feed_add_flags (feed, FEED_READ); - katze_item_set_uri (KATZE_ITEM (feed), uri); netpriv = g_new0 (FeedNetPrivate, 1); netpriv->parsers = priv->parsers; netpriv->extension = priv->extension; @@ -272,9 +309,11 @@ secondary_icon_released_cb (GtkAction* action, KatzeArray* feed; feed = feed_add_item (priv->feeds, uri); - feed_save_items (priv->extension, priv->feeds); - feed_add_flags (feed, FEED_NEW); - update_feed (priv, KATZE_ITEM (feed)); + if (feed) + { + feed_save_items (priv->extension, priv->feeds); + update_feed (priv, KATZE_ITEM (feed)); + } } } @@ -324,9 +363,11 @@ panel_add_feed_cb (FeedPanel* panel, KatzeArray* feed; feed = feed_add_item (priv->feeds, uri); - feed_save_items (priv->extension, priv->feeds); - feed_add_flags (feed, FEED_NEW); - update_feed (priv, KATZE_ITEM (feed)); + if (feed) + { + feed_save_items (priv->extension, priv->feeds); + update_feed (priv, KATZE_ITEM (feed)); + } } } gtk_widget_destroy (dialog); @@ -334,10 +375,15 @@ panel_add_feed_cb (FeedPanel* panel, static void panel_remove_feed_cb (FeedPanel* panel, - KatzeArray* feed, + KatzeItem* item, FeedPrivate* priv) { + KatzeArray* feed; + + feed = katze_item_get_parent (item); + g_assert (KATZE_IS_ARRAY (priv->feeds)); + g_assert (KATZE_IS_ARRAY (feed)); if (feed_has_flags (feed, FEED_READ)) feed_add_flags (feed, FEED_REMOVE); @@ -394,7 +440,8 @@ feed_app_add_browser_cb (MidoriApp* app, if (sfeeds[i]) { feed = feed_add_item (feeds, sfeeds[i]); - update_feed (priv, KATZE_ITEM (feed)); + if (feed) + update_feed (priv, KATZE_ITEM (feed)); } } g_strdupv (sfeeds); -- 2.39.5