1 /* ev-convert-metadata.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <glib/gi18n.h>
28 #include <libxml/tree.h>
30 #define EV_METADATA_NAMESPACE "metadata::evince"
48 free_doc_item (DocItem *item)
55 convert_finish (ConvertData *data)
57 g_list_foreach (data->items, (GFunc)free_doc_item, NULL);
58 g_list_free (data->items);
59 xmlFreeDoc (data->doc);
66 convert_file (ConvertData *data)
79 item = (DocItem *) data->current->data;
80 uri = (const gchar *)item->uri;
82 data->current = g_list_next (data->current);
84 /* Update progress information */
85 total = g_list_length (data->items);
86 current = ++(data->n_item);
88 text = g_strdup_printf (_("Converting %s"), uri);
89 gtk_label_set_text (GTK_LABEL (data->label), text);
92 text = g_strdup_printf (_("%d of %d documents converted"), current, total);
93 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (data->progress), text);
95 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (data->progress),
96 (gdouble)(current - 1) / total);
98 file = g_file_new_for_uri (uri);
99 if (!g_file_query_exists (file, NULL)) {
100 g_printerr ("Uri %s does not exist\n", uri);
101 g_object_unref (file);
103 return data->current != NULL;
106 for (cur = node->xmlChildrenNode; cur != NULL; cur = cur->next) {
110 if (xmlStrcmp (cur->name, (const xmlChar *)"entry") != 0)
113 key = xmlGetProp (cur, (const xmlChar *)"key");
114 value = xmlGetProp (cur, (const xmlChar *)"value");
118 GError *error = NULL;
120 info = g_file_info_new ();
122 gio_key = g_strconcat (EV_METADATA_NAMESPACE"::", key, NULL);
123 g_file_info_set_attribute_string (info, gio_key, (const gchar *)value);
126 if (!g_file_set_attributes_from_info (file, info, 0, NULL, &error)) {
127 g_printerr ("Error setting metadata for %s: %s\n",
128 uri, error->message);
129 g_error_free (error);
132 g_object_unref (info);
141 g_object_unref (file);
143 return data->current != NULL;
147 convert_metadata_cancel (GtkDialog *dialog,
151 convert_finish (data);
156 show_progress_dialog (ConvertData *data)
159 GtkWidget *action_area;
160 GtkWidget *vbox, *pbox;
165 dialog = gtk_dialog_new_with_buttons (_("Converting metadata"),
167 #if GTK_CHECK_VERSION (2, 90, 8)
170 GTK_DIALOG_NO_SEPARATOR,
172 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
174 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
175 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
176 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
177 gtk_box_set_spacing (GTK_BOX (action_area), 6);
179 vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
180 gtk_box_set_spacing (GTK_BOX (vbox), 12);
182 label = gtk_label_new (NULL);
183 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
184 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
185 text = g_strdup_printf ("<b>%s</b>", _("Converting metadata"));
186 gtk_label_set_markup (GTK_LABEL (label), text);
188 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
189 gtk_widget_show (label);
191 label = gtk_label_new (_("The metadata format used by Evince "
192 "has changed, and hence it needs to be migrated. "
193 "If the migration is cancelled the metadata "
194 "storage will not work."));
195 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
196 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
197 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
198 gtk_widget_show (label);
200 pbox = gtk_vbox_new (FALSE, 6);
201 progress = gtk_progress_bar_new ();
202 data->progress = progress;
203 gtk_box_pack_start (GTK_BOX (pbox), progress, TRUE, TRUE, 0);
204 gtk_widget_show (progress);
206 label = gtk_label_new (NULL);
208 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
209 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
210 gtk_box_pack_start (GTK_BOX (pbox), label, FALSE, FALSE, 0);
211 gtk_widget_show (label);
213 gtk_box_pack_start (GTK_BOX (vbox), pbox, TRUE, TRUE, 0);
214 gtk_widget_show (pbox);
216 g_signal_connect (dialog, "response",
217 G_CALLBACK (convert_metadata_cancel),
220 gtk_widget_show (dialog);
224 convert_metadata_file (const gchar *filename)
230 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
233 doc = xmlParseFile (filename);
235 g_printerr ("Error loading metadata file %s\n", filename);
239 cur = xmlDocGetRootElement (doc);
241 g_printerr ("Metadata file %s is empty\n", filename);
246 if (xmlStrcmp (cur->name, (const xmlChar *) "metadata")) {
247 g_printerr ("File %s is not a valid evince metadata file\n", filename);
252 data = g_new0 (ConvertData, 1);
255 for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
259 if (xmlStrcmp (cur->name, (const xmlChar *)"document") != 0)
262 uri = xmlGetProp (cur, (const xmlChar *)"uri");
266 item = g_new (DocItem, 1);
269 data->items = g_list_prepend (data->items, item);
273 xmlFreeDoc (data->doc);
279 show_progress_dialog (data);
281 data->current = data->items;
282 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
283 (GSourceFunc)convert_file,
285 (GDestroyNotify)convert_finish);
291 main (gint argc, gchar **argv)
294 g_printerr ("%s\n", "Usage: evince-convert-metadata FILE");
298 gtk_init (&argc, &argv);
300 if (!convert_metadata_file (argv[1]))