Prevent attempts to add duplicate feeds

This commit is contained in:
Dale Whittaker 2009-04-28 23:31:37 +02:00 committed by Christian Dywan
parent 9c6322b6fc
commit 522b3ac678
2 changed files with 114 additions and 48 deletions

View file

@ -131,7 +131,7 @@ feed_panel_treeview_render_text_cb (GtkTreeViewColumn* column,
} }
static void static void
feed_panel_add_item_cb (KatzeArray* feed, feed_panel_add_item_cb (KatzeArray* parent,
KatzeItem* child, KatzeItem* child,
FeedPanel* panel) FeedPanel* panel)
{ {
@ -141,33 +141,36 @@ feed_panel_add_item_cb (KatzeArray* feed,
KatzeItem* item; KatzeItem* item;
gint i; 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)); g_return_if_fail (KATZE_IS_ITEM (child));
model = gtk_tree_view_get_model (GTK_TREE_VIEW (panel->treeview)); model = gtk_tree_view_get_model (GTK_TREE_VIEW (panel->treeview));
if (KATZE_IS_ARRAY (child)) if (katze_item_get_parent (KATZE_ITEM (parent)))
{ {
gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter, if (KATZE_IS_ARRAY (child))
NULL, G_MAXINT, 0, child, -1); {
} gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter,
else NULL, G_MAXINT, 0, child, -1);
{ }
else
i = 0;
while (gtk_tree_model_iter_nth_child (model, &iter, NULL, i++))
{ {
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,
&iter, 0, 0, child, -1);
g_object_unref (child); i = 0;
while (gtk_tree_model_iter_nth_child (model, &iter, NULL, i++))
{
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 (item);
break;
}
g_object_unref (item); g_object_unref (item);
break;
} }
g_object_unref (item);
} }
} }
feed_panel_insert_item (panel, GTK_TREE_STORE (model), &child_iter, child); feed_panel_insert_item (panel, GTK_TREE_STORE (model), &child_iter, child);
@ -199,21 +202,33 @@ feed_panel_remove_iter (GtkTreeModel* model,
} }
static void static void
feed_panel_remove_item_cb (KatzeArray* feed, feed_panel_remove_item_cb (KatzeArray* item,
KatzeItem* child, KatzeItem* child,
FeedPanel* panel) FeedPanel* panel)
{ {
GtkTreeModel* model; GtkTreeModel* model;
KatzeItem* pitem;
g_assert (KATZE_IS_ARRAY (feed)); g_assert (KATZE_IS_ARRAY (item));
g_assert (KATZE_IS_ITEM (child)); g_assert (KATZE_IS_ITEM (child));
if (KATZE_IS_ARRAY (child)) if (KATZE_IS_ARRAY (child))
feed_panel_disconnect_feed (panel, KATZE_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)); model = gtk_tree_view_get_model (GTK_TREE_VIEW (panel->treeview));
feed_panel_remove_iter (model, child); feed_panel_remove_iter (model, pitem);
g_object_unref (child); g_object_unref (pitem);
} }
static void static void
@ -362,13 +377,17 @@ feed_panel_cursor_or_row_changed_cb (GtkTreeView* treeview,
if (date) if (date)
{ {
SoupDate* sdate; SoupDate* sdate;
const gchar* suri; const gchar* puri;
KatzeItem* parent;
sdate = soup_date_new_from_time_t ((time_t) date); 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 ( text = g_strdup_printf (
"<html><head><title>feed</title></head>" "<html><head><title>feed</title></head>"
"<body><h3>%s</h3><p />Last updated %s.</body></html>", "<body><h3>%s</h3><p />Last updated %s.</body></html>",
suri, soup_date_to_string (sdate, SOUP_DATE_HTTP)); puri, soup_date_to_string (sdate, SOUP_DATE_HTTP));
soup_date_free (sdate); soup_date_free (sdate);
} }
} }

View file

@ -58,7 +58,6 @@ typedef struct
enum enum
{ {
FEED_NEW,
FEED_READ, FEED_READ,
FEED_REMOVE FEED_REMOVE
}; };
@ -104,10 +103,33 @@ feed_add_item (KatzeArray* feeds,
if (uri) if (uri)
{ {
feed = katze_array_new (KATZE_TYPE_ITEM); if (katze_array_find_token (feeds, uri))
g_object_set_data_full (G_OBJECT (feed), "feeduri", {
(gpointer) g_strdup ((uri)), g_free); GtkWidget* dialog;
katze_array_add_item (feeds, feed);
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; return feed;
} }
@ -129,7 +151,7 @@ feed_save_items (MidoriExtension* extension,
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
item = katze_array_get_nth_item (feed, 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; sfeeds[n] = NULL;
@ -141,12 +163,19 @@ static void
feed_handle_net_error (FeedNetPrivate* netpriv, feed_handle_net_error (FeedNetPrivate* netpriv,
const gchar* msg) const gchar* msg)
{ {
KatzeItem* child;
const gchar* uri; const gchar* uri;
gint n;
uri = (gchar*) g_object_get_data (G_OBJECT (netpriv->feed), "feeduri"); n = katze_array_get_length (netpriv->feed);
katze_item_set_name (KATZE_ITEM (netpriv->feed), uri); g_assert (n == 1);
katze_item_set_text (KATZE_ITEM (netpriv->feed), msg); child = katze_array_get_nth_item (netpriv->feed, 0);
katze_item_set_uri (KATZE_ITEM (netpriv->feed), NULL); g_assert (KATZE_IS_ARRAY (child));
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); feed_remove_flags (netpriv->feed, FEED_READ);
} }
@ -184,8 +213,19 @@ feed_transfer_cb (KatzeNetRequest* request,
if (request->data) 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, if (!parse_feed (request->data, request->length,
netpriv->parsers, netpriv->feed, &error)) netpriv->parsers, item, &error))
{ {
feed_handle_net_error (netpriv, error->message); feed_handle_net_error (netpriv, error->message);
g_error_free (error); g_error_free (error);
@ -216,11 +256,8 @@ update_feed (FeedPrivate* priv,
if (!(feed_has_flags (feed, FEED_READ))) if (!(feed_has_flags (feed, FEED_READ)))
{ {
FeedNetPrivate* netpriv; FeedNetPrivate* netpriv;
gchar* uri;
uri = (gchar*) g_object_get_data (G_OBJECT (feed), "feeduri");
feed_add_flags (feed, FEED_READ); feed_add_flags (feed, FEED_READ);
katze_item_set_uri (KATZE_ITEM (feed), uri);
netpriv = g_new0 (FeedNetPrivate, 1); netpriv = g_new0 (FeedNetPrivate, 1);
netpriv->parsers = priv->parsers; netpriv->parsers = priv->parsers;
netpriv->extension = priv->extension; netpriv->extension = priv->extension;
@ -272,9 +309,11 @@ secondary_icon_released_cb (GtkAction* action,
KatzeArray* feed; KatzeArray* feed;
feed = feed_add_item (priv->feeds, uri); feed = feed_add_item (priv->feeds, uri);
feed_save_items (priv->extension, priv->feeds); if (feed)
feed_add_flags (feed, FEED_NEW); {
update_feed (priv, KATZE_ITEM (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; KatzeArray* feed;
feed = feed_add_item (priv->feeds, uri); feed = feed_add_item (priv->feeds, uri);
feed_save_items (priv->extension, priv->feeds); if (feed)
feed_add_flags (feed, FEED_NEW); {
update_feed (priv, KATZE_ITEM (feed)); feed_save_items (priv->extension, priv->feeds);
update_feed (priv, KATZE_ITEM (feed));
}
} }
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
@ -334,10 +375,15 @@ panel_add_feed_cb (FeedPanel* panel,
static void static void
panel_remove_feed_cb (FeedPanel* panel, panel_remove_feed_cb (FeedPanel* panel,
KatzeArray* feed, KatzeItem* item,
FeedPrivate* priv) FeedPrivate* priv)
{ {
KatzeArray* feed;
feed = katze_item_get_parent (item);
g_assert (KATZE_IS_ARRAY (priv->feeds)); g_assert (KATZE_IS_ARRAY (priv->feeds));
g_assert (KATZE_IS_ARRAY (feed));
if (feed_has_flags (feed, FEED_READ)) if (feed_has_flags (feed, FEED_READ))
feed_add_flags (feed, FEED_REMOVE); feed_add_flags (feed, FEED_REMOVE);
@ -394,7 +440,8 @@ feed_app_add_browser_cb (MidoriApp* app,
if (sfeeds[i]) if (sfeeds[i])
{ {
feed = feed_add_item (feeds, 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); g_strdupv (sfeeds);