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
feed_panel_add_item_cb (KatzeArray* feed,
feed_panel_add_item_cb (KatzeArray* parent,
KatzeItem* child,
FeedPanel* panel)
{
@ -141,11 +141,13 @@ 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_item_get_parent (KATZE_ITEM (parent)))
{
if (KATZE_IS_ARRAY (child))
{
gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter,
@ -158,7 +160,7 @@ feed_panel_add_item_cb (KatzeArray* feed,
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))
if (item == KATZE_ITEM (parent))
{
gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), &child_iter,
&iter, 0, 0, child, -1);
@ -170,6 +172,7 @@ feed_panel_add_item_cb (KatzeArray* feed,
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 (
"<html><head><title>feed</title></head>"
"<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);
}
}

View file

@ -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);
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;
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);
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 = 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,11 +309,13 @@ secondary_icon_released_cb (GtkAction* action,
KatzeArray* feed;
feed = feed_add_item (priv->feeds, uri);
if (feed)
{
feed_save_items (priv->extension, priv->feeds);
feed_add_flags (feed, FEED_NEW);
update_feed (priv, KATZE_ITEM (feed));
}
}
}
static void
panel_add_feed_cb (FeedPanel* panel,
@ -324,20 +363,27 @@ panel_add_feed_cb (FeedPanel* panel,
KatzeArray* feed;
feed = feed_add_item (priv->feeds, uri);
if (feed)
{
feed_save_items (priv->extension, priv->feeds);
feed_add_flags (feed, FEED_NEW);
update_feed (priv, KATZE_ITEM (feed));
}
}
}
gtk_widget_destroy (dialog);
}
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,6 +440,7 @@ feed_app_add_browser_cb (MidoriApp* app,
if (sfeeds[i])
{
feed = feed_add_item (feeds, sfeeds[i]);
if (feed)
update_feed (priv, KATZE_ITEM (feed));
}
}