});
-void
-djvu_handle_events (DjvuDocument *djvu_document, int wait)
+#define EV_DJVU_ERROR ev_djvu_error_quark ()
+
+static GQuark
+ev_djvu_error_quark (void)
+{
+ static GQuark q = 0;
+ if (q == 0)
+ q = g_quark_from_static_string ("ev-djvu-quark");
+
+ return q;
+}
+
+static void
+handle_message (const ddjvu_message_t *msg, GError **error)
+{
+ switch (msg->m_any.tag) {
+ case DDJVU_ERROR: {
+ gchar *error_str;
+
+ if (msg->m_error.filename) {
+ error_str = g_strdup_printf ("DjvuLibre error: %s:%d",
+ msg->m_error.filename,
+ msg->m_error.lineno);
+ } else {
+ error_str = g_strdup_printf ("DjvuLibre error: %s",
+ msg->m_error.message);
+ }
+
+ if (error) {
+ g_set_error (error, EV_DJVU_ERROR, 0, error_str);
+ } else {
+ g_warning (error_str);
+ }
+
+ g_free (error_str);
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void
+djvu_handle_events (DjvuDocument *djvu_document, int wait, GError **error)
{
ddjvu_context_t *ctx = djvu_document->d_context;
const ddjvu_message_t *msg;
return;
if (wait)
- msg = ddjvu_message_wait (ctx);
+ ddjvu_message_wait (ctx);
while ((msg = ddjvu_message_peek (ctx))) {
- switch (msg->m_any.tag) {
- case DDJVU_ERROR:
- g_warning ("DjvuLibre error: %s",
- msg->m_error.message);
- if (msg->m_error.filename)
- g_warning ("DjvuLibre error: %s:%d",
- msg->m_error.filename,
- msg->m_error.lineno);
- break;
- default:
- break;
- }
+ handle_message (msg, error);
+ ddjvu_message_pop (ctx);
+ if (error && *error)
+ return;
+ }
+}
+
+static void
+djvu_wait_for_message (DjvuDocument *djvu_document, ddjvu_message_tag_t message, GError **error)
+{
+ ddjvu_context_t *ctx = djvu_document->d_context;
+ const ddjvu_message_t *msg;
+
+ ddjvu_message_wait (ctx);
+ while ((msg = ddjvu_message_peek (ctx)) && (msg->m_any.tag != message)) {
+ handle_message (msg, error);
ddjvu_message_pop (ctx);
+ if (error && *error)
+ return;
}
+ if (msg && msg->m_any.tag == message)
+ ddjvu_message_pop (ctx);
}
static gboolean
ddjvu_document_t *doc;
gchar *filename;
gboolean missing_files = FALSE;
+ GError *djvu_error = NULL;
/* FIXME: We could actually load uris */
filename = g_filename_from_uri (uri, NULL, error);
djvu_document->d_document = doc;
- while (!ddjvu_document_decoding_done (djvu_document->d_document))
- djvu_handle_events (djvu_document, TRUE);
+ djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
+ if (djvu_error) {
+ g_set_error (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ djvu_error->message);
+ g_error_free (djvu_error);
+ g_free (filename);
+ ddjvu_document_release (djvu_document->d_document);
+ djvu_document->d_document = NULL;
+
+ return FALSE;
+ }
+
+ if (ddjvu_document_decoding_error (djvu_document->d_document))
+ djvu_handle_events (djvu_document, TRUE, &djvu_error);
+
+ if (djvu_error) {
+ g_set_error (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ djvu_error->message);
+ g_error_free (djvu_error);
+ g_free (filename);
+ ddjvu_document_release (djvu_document->d_document);
+ djvu_document->d_document = NULL;
+
+ return FALSE;
+ }
+
g_free (djvu_document->uri);
djvu_document->uri = g_strdup (uri);
ddjvu_status_t r;
while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
- djvu_handle_events(djvu_document, TRUE);
+ djvu_handle_events(djvu_document, TRUE, NULL);
if (r >= DDJVU_JOB_FAILED)
- djvu_handle_events(djvu_document, TRUE);
+ djvu_handle_events(djvu_document, TRUE, NULL);
*width = info.width * SCALE_FACTOR;
*height = info.height * SCALE_FACTOR;
d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page->index);
while (!ddjvu_page_decoding_done (d_page))
- djvu_handle_events(djvu_document, TRUE);
+ djvu_handle_events(djvu_document, TRUE, NULL);
page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
pixels = gdk_pixbuf_get_pixels (pixbuf);
while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page->index, 1) < DDJVU_JOB_OK)
- djvu_handle_events(djvu_document, TRUE);
+ djvu_handle_events(djvu_document, TRUE, NULL);
ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index,
&thumb_width, &thumb_height,
ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
while (!ddjvu_job_done(job)) {
- djvu_handle_events (djvu_document, TRUE);
+ djvu_handle_events (djvu_document, TRUE, NULL);
}
fclose(fn);
miniexp_t outline;
while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
- djvu_handle_events (djvu_document, TRUE);
+ djvu_handle_events (djvu_document, TRUE, NULL);
if (outline) {
ddjvu_miniexp_release (djvu_document->d_document, outline);
ddjvu_pageinfo_t page_info;
while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
- djvu_handle_events (djvu_document, TRUE);
+ djvu_handle_events (djvu_document, TRUE, NULL);
while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
- djvu_handle_events(djvu_document, TRUE);
+ djvu_handle_events(djvu_document, TRUE, NULL);
if (page_annotations) {
hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
miniexp_t outline = miniexp_nil;
while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
- djvu_handle_events (djvu_document, TRUE);
+ djvu_handle_events (djvu_document, TRUE, NULL);
if (outline) {
model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,