Factor out code to find a bookmark in the list of bookmarks

Signed-off-by: Federico Mena Quintero's avatarFederico Mena Quintero <federico@gnome.org>
parent caf659e7
......@@ -276,38 +276,39 @@ _gtk_bookmarks_manager_list_bookmarks (GtkBookmarksManager *manager)
return g_slist_reverse (files);
}
GSList *
find_bookmark_link_for_file (GSList *bookmarks, GFile *file)
{
for (; bookmarks; bookmarks = bookmarks->next)
{
GtkBookmark *bookmark = bookmarks->data;
if (g_file_equal (file, bookmark->file))
return bookmarks;
}
return NULL;
}
gboolean
_gtk_bookmarks_manager_insert_bookmark (GtkBookmarksManager *manager,
GFile *file,
gint position,
GError **error)
{
GSList *bookmarks;
GSList *link;
GtkBookmark *bookmark;
gboolean result = TRUE;
GFile *bookmarks_file;
g_return_val_if_fail (manager != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
bookmarks = manager->bookmarks;
while (bookmarks)
{
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
if (g_file_equal (bookmark->file, file))
{
/* File is already in bookmarks */
result = FALSE;
break;
}
}
link = find_bookmark_link_for_file (manager->bookmarks, file);
if (!result)
if (link)
{
gchar *uri = g_file_get_uri (file);
bookmark = link->data;
gchar *uri = g_file_get_uri (bookmark->file);
g_set_error (error,
GTK_FILE_CHOOSER_ERROR,
......@@ -339,9 +340,7 @@ _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
GFile *file,
GError **error)
{
GtkBookmark *bookmark;
GSList *bookmarks;
gboolean result = FALSE;
GSList *link;
GFile *bookmarks_file;
g_return_val_if_fail (manager != NULL, FALSE);
......@@ -350,25 +349,16 @@ _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
if (!manager->bookmarks)
return FALSE;
bookmarks = manager->bookmarks;
while (bookmarks)
link = find_bookmark_link_for_file (manager->bookmarks, file);
if (link)
{
bookmark = bookmarks->data;
if (g_file_equal (bookmark->file, file))
{
result = TRUE;
manager->bookmarks = g_slist_remove_link (manager->bookmarks, bookmarks);
_gtk_bookmark_free (bookmark);
g_slist_free_1 (bookmarks);
break;
}
GtkBookmark *bookmark = link->data;
bookmarks = bookmarks->next;
manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
_gtk_bookmark_free (bookmark);
g_slist_free_1 (link);
}
if (!result)
else
{
gchar *uri = g_file_get_uri (file);
......@@ -421,40 +411,46 @@ _gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
return label;
}
void
gboolean
_gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
GFile *file,
const gchar *label)
const gchar *label,
GError **error)
{
gboolean changed = FALSE;
GFile *bookmarks_file;
GSList *bookmarks;
GSList *link;
g_return_if_fail (manager != NULL);
g_return_if_fail (file != NULL);
g_return_val_if_fail (manager != NULL, FALSE);
g_return_val_if_fail (file != NULL, FALSE);
bookmarks = manager->bookmarks;
link = find_bookmark_link_for_file (manager->bookmarks, file);
if (link)
{
GtkBookmark *bookmark = link->data;
while (bookmarks)
g_free (bookmark->label);
bookmark->label = g_strdup (label);
}
else
{
GtkBookmark *bookmark;
gchar *uri = g_file_get_uri (file);
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
g_set_error (error,
GTK_FILE_CHOOSER_ERROR,
GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
"%s does not exist in the bookmarks list",
uri);
if (g_file_equal (file, bookmark->file))
{
g_free (bookmark->label);
bookmark->label = g_strdup (label);
changed = TRUE;
break;
}
g_free (uri);
return FALSE;
}
bookmarks_file = get_bookmarks_file ();
save_bookmarks (bookmarks_file, manager->bookmarks);
g_object_unref (bookmarks_file);
if (changed)
notify_changed (manager);
notify_changed (manager);
return TRUE;
}
......@@ -67,8 +67,9 @@ gboolean _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
gchar * _gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
GFile *file);
void _gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
GFile *file,
const gchar *label);
gboolean _gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
GFile *file,
const gchar *label,
GError **error);
#endif /* __GTK_BOOKMARKS_MANAGER_H__ */
......@@ -3016,7 +3016,7 @@ bookmarks_edited (GtkCellRenderer *cell,
gtk_tree_path_free (path);
file = g_file_new_for_uri (uri);
_gtk_bookmarks_manager_set_bookmark_label (sidebar->bookmarks_manager, file, new_text);
_gtk_bookmarks_manager_set_bookmark_label (sidebar->bookmarks_manager, file, new_text, NULL); /* NULL-GError */
g_object_unref (file);
g_free (uri);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment