/* bookmarkfile-clean.c -- Clean obsolete entries from a bookmark file. Copyright (C) 2008 Claudio Saavedra This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include static gint bfile_clean_old_files (GBookmarkFile *bfile) { GError *error; GFile *file; gchar **uris; gchar **uri; gint removed = 0; uris = g_bookmark_file_get_uris (bfile, NULL); for (uri = uris; *uri != NULL; uri++) { file = g_file_new_for_uri (*uri); if (!g_file_query_exists (file, NULL)) { if (!g_bookmark_file_remove_item (bfile, *uri, &error)) { g_warning ("Could not remove item for file %s: %s", *uri, error->message); g_error_free (error); error = NULL; } else { removed ++; } g_object_unref (file); } } g_strfreev (uris); return removed; } int main (gint argc, gchar **argv) { const gchar *input_file; const gchar *output_file; GBookmarkFile *bfile; GError *error = NULL; gint removed; if (argc < 2) { g_message ("Usage: %s input.xbel [output.xbel]", argv[0]); return 1; } else if (argc == 2) { input_file = output_file = argv[1]; } else { input_file = argv[1]; output_file = argv[2]; } if (!g_file_test (input_file, G_FILE_TEST_EXISTS)) { g_message ("Input file %s doesn't exist", input_file); return 1; } g_type_init (); bfile = g_bookmark_file_new (); if (!g_bookmark_file_load_from_file (bfile, input_file, &error)) { g_message ("Could not load the input file %s: %s", input_file, error->message); g_error_free (error); g_bookmark_file_free (bfile); return 1; } removed = bfile_clean_old_files (bfile); if (!g_bookmark_file_to_file (bfile, output_file, &error)) { g_message ("Could not save to output file %s: %s", output_file, error->message); g_error_free (error); g_bookmark_file_free (bfile); return 1; } g_bookmark_file_free (bfile); g_print ("Removed %d obsolete entries from bookmarks file.\n", removed); return 0; }