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

Signed-off-by: Federico Mena Quintero <federico@gnome.org>
This commit is contained in:
Federico Mena Quintero 2012-09-11 15:02:01 -05:00
parent caf659e791
commit 8b1eb6db99
3 changed files with 59 additions and 62 deletions

View File

@ -276,38 +276,39 @@ _gtk_bookmarks_manager_list_bookmarks (GtkBookmarksManager *manager)
return g_slist_reverse (files); 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 gboolean
_gtk_bookmarks_manager_insert_bookmark (GtkBookmarksManager *manager, _gtk_bookmarks_manager_insert_bookmark (GtkBookmarksManager *manager,
GFile *file, GFile *file,
gint position, gint position,
GError **error) GError **error)
{ {
GSList *bookmarks; GSList *link;
GtkBookmark *bookmark; GtkBookmark *bookmark;
gboolean result = TRUE;
GFile *bookmarks_file; GFile *bookmarks_file;
g_return_val_if_fail (manager != NULL, FALSE); g_return_val_if_fail (manager != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
bookmarks = manager->bookmarks; link = find_bookmark_link_for_file (manager->bookmarks, file);
while (bookmarks) if (link)
{ {
bookmark = bookmarks->data; bookmark = link->data;
bookmarks = bookmarks->next; gchar *uri = g_file_get_uri (bookmark->file);
if (g_file_equal (bookmark->file, file))
{
/* File is already in bookmarks */
result = FALSE;
break;
}
}
if (!result)
{
gchar *uri = g_file_get_uri (file);
g_set_error (error, g_set_error (error,
GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR,
@ -339,9 +340,7 @@ _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
GFile *file, GFile *file,
GError **error) GError **error)
{ {
GtkBookmark *bookmark; GSList *link;
GSList *bookmarks;
gboolean result = FALSE;
GFile *bookmarks_file; GFile *bookmarks_file;
g_return_val_if_fail (manager != NULL, FALSE); g_return_val_if_fail (manager != NULL, FALSE);
@ -350,25 +349,16 @@ _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
if (!manager->bookmarks) if (!manager->bookmarks)
return FALSE; return FALSE;
bookmarks = manager->bookmarks; link = find_bookmark_link_for_file (manager->bookmarks, file);
if (link)
while (bookmarks)
{ {
bookmark = bookmarks->data; GtkBookmark *bookmark = link->data;
if (g_file_equal (bookmark->file, file)) manager->bookmarks = g_slist_remove_link (manager->bookmarks, link);
{
result = TRUE;
manager->bookmarks = g_slist_remove_link (manager->bookmarks, bookmarks);
_gtk_bookmark_free (bookmark); _gtk_bookmark_free (bookmark);
g_slist_free_1 (bookmarks); g_slist_free_1 (link);
break;
} }
else
bookmarks = bookmarks->next;
}
if (!result)
{ {
gchar *uri = g_file_get_uri (file); gchar *uri = g_file_get_uri (file);
@ -421,40 +411,46 @@ _gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
return label; return label;
} }
void gboolean
_gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager, _gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
GFile *file, GFile *file,
const gchar *label) const gchar *label,
GError **error)
{ {
gboolean changed = FALSE;
GFile *bookmarks_file; GFile *bookmarks_file;
GSList *bookmarks; GSList *link;
g_return_if_fail (manager != NULL); g_return_val_if_fail (manager != NULL, FALSE);
g_return_if_fail (file != NULL); g_return_val_if_fail (file != NULL, FALSE);
bookmarks = manager->bookmarks; link = find_bookmark_link_for_file (manager->bookmarks, file);
if (link)
while (bookmarks)
{ {
GtkBookmark *bookmark; GtkBookmark *bookmark = link->data;
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
if (g_file_equal (file, bookmark->file))
{
g_free (bookmark->label); g_free (bookmark->label);
bookmark->label = g_strdup (label); bookmark->label = g_strdup (label);
changed = TRUE;
break;
} }
else
{
gchar *uri = g_file_get_uri (file);
g_set_error (error,
GTK_FILE_CHOOSER_ERROR,
GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
"%s does not exist in the bookmarks list",
uri);
g_free (uri);
return FALSE;
} }
bookmarks_file = get_bookmarks_file (); bookmarks_file = get_bookmarks_file ();
save_bookmarks (bookmarks_file, manager->bookmarks); save_bookmarks (bookmarks_file, manager->bookmarks);
g_object_unref (bookmarks_file); g_object_unref (bookmarks_file);
if (changed)
notify_changed (manager); notify_changed (manager);
return TRUE;
} }

View File

@ -67,8 +67,9 @@ gboolean _gtk_bookmarks_manager_remove_bookmark (GtkBookmarksManager *manager,
gchar * _gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager, gchar * _gtk_bookmarks_manager_get_bookmark_label (GtkBookmarksManager *manager,
GFile *file); GFile *file);
void _gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager, gboolean _gtk_bookmarks_manager_set_bookmark_label (GtkBookmarksManager *manager,
GFile *file, GFile *file,
const gchar *label); const gchar *label,
GError **error);
#endif /* __GTK_BOOKMARKS_MANAGER_H__ */ #endif /* __GTK_BOOKMARKS_MANAGER_H__ */

View File

@ -3016,7 +3016,7 @@ bookmarks_edited (GtkCellRenderer *cell,
gtk_tree_path_free (path); gtk_tree_path_free (path);
file = g_file_new_for_uri (uri); 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_object_unref (file);
g_free (uri); g_free (uri);