1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/* GXPSFile
*
* Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <string.h>
#include "gxps-file.h"
#include "gxps-archive.h"
#include "gxps-private.h"
#include "gxps-error.h"
#include "gxps-debug.h"
/**
* SECTION:gxps-file
* @Short_description: XPS Files
* @Title: GXPSFile
* @See_also: #GXPSDocument, #GXPSLinkTarget
*
* #GXPSFile represents a XPS file. A #GXPSFile is a set of one or more
* documents, you can get the amount of documents contained in the set
* with gxps_file_get_n_documents(). Documents can be retrieved by their
* index in the set with gxps_file_get_document().
*/
enum {
PROP_0,
PROP_FILE
};
struct _GXPSFilePrivate {
GFile *file;
GXPSArchive *zip;
GList *docs;
gboolean initialized;
GError *init_error;
gchar *fixed_repr;
gchar *thumbnail;
gchar *core_props;
};
static void initable_iface_init (GInitableIface *initable_iface);
G_DEFINE_TYPE_WITH_CODE (GXPSFile, gxps_file, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
GQuark
gxps_file_error_quark (void)
{
return g_quark_from_static_string ("gxps-file-error-quark");
}
#define REL_METATADA_CORE_PROPS "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
#define REL_METATADA_THUMBNAIL "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
#define REL_FIXED_REPRESENTATION "http://schemas.microsoft.com/xps/2005/06/fixedrepresentation"
/* Relationship parser */
static void
rels_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
{
GXPSFile *xps = GXPS_FILE (user_data);
if (strcmp (element_name, "Relationship") == 0) {
const gchar *type = NULL;
const gchar *target = NULL;
gint i;
for (i = 0; names[i]; i++) {
if (strcmp (names[i], "Type") == 0) {
type = values[i];
} else if (strcmp (names[i], "Target") == 0) {
target = values[i];
} else if (strcmp (names[i], "Id") == 0) {
/* Ignore ids for now */
}
}
if (!type || !target) {
gxps_parse_error (context,
"_rels/.rels",
G_MARKUP_ERROR_MISSING_ATTRIBUTE,
element_name,
!type ? "Type" : "Target",
NULL, error);
return;
}
if (strcmp (type, REL_FIXED_REPRESENTATION) == 0) {
xps->priv->fixed_repr = g_strdup (target);
} else if (strcmp (type, REL_METATADA_THUMBNAIL) == 0) {
xps->priv->thumbnail = g_strdup (target);
} else if (strcmp (type, REL_METATADA_CORE_PROPS) == 0) {
xps->priv->core_props = g_strdup (target);
} else {
GXPS_DEBUG (g_debug ("Unsupported attribute of %s, %s=%s",
element_name, type, target));
}
} else if (strcmp (element_name, "Relationships") == 0) {
/* Nothing to do */
} else {
gxps_parse_error (context,
"_rels/.rels",
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
element_name, NULL, NULL, error);
}
}
static const GMarkupParser rels_parser = {
rels_start_element,
NULL,
NULL,
NULL,
NULL
};
static gboolean
gxps_file_parse_rels (GXPSFile *xps,
GError **error)
{
GInputStream *stream;
GMarkupParseContext *ctx;
stream = gxps_archive_open (xps->priv->zip, "_rels/.rels");
if (!stream) {
g_set_error_literal (error,
GXPS_ERROR,
GXPS_ERROR_SOURCE_NOT_FOUND,
"Source _rels/.rels not found in archive");
return FALSE;
}
ctx = g_markup_parse_context_new (&rels_parser, 0, xps, NULL);
gxps_parse_stream (ctx, stream, error);
g_object_unref (stream);
g_markup_parse_context_free (ctx);
return (*error != NULL) ? FALSE : TRUE;
}
/* FixedRepresentation parser */
static void
fixed_repr_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
{
GXPSFile *xps = GXPS_FILE (user_data);
if (strcmp (element_name, "DocumentReference") == 0) {
gint i;
for (i = 0; names[i]; i++) {
if (strcmp (names[i], "Source") == 0) {
xps->priv->docs = g_list_prepend (xps->priv->docs,
gxps_resolve_relative_path (xps->priv->fixed_repr, values[i]));
}
}
xps->priv->docs = g_list_reverse (xps->priv->docs);
} else if (strcmp (element_name, "FixedDocumentSequence") == 0) {
/* Nothing to do */
} else {
gxps_parse_error (context,
xps->priv->fixed_repr,
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
element_name, NULL, NULL, error);
}
}
static const GMarkupParser fixed_repr_parser = {
fixed_repr_start_element,
NULL,
NULL,
NULL,
NULL
};
static gboolean
gxps_file_parse_fixed_repr (GXPSFile *xps,
GError **error)
{
GInputStream *stream;
GMarkupParseContext *ctx;
stream = gxps_archive_open (xps->priv->zip,
xps->priv->fixed_repr);
if (!stream) {
g_set_error_literal (error,
GXPS_FILE_ERROR,
GXPS_FILE_ERROR_INVALID,
"Invalid XPS File: cannot open fixedrepresentation");
return FALSE;
}
ctx = g_markup_parse_context_new (&fixed_repr_parser, 0, xps, NULL);
gxps_parse_stream (ctx, stream, error);
g_object_unref (stream);
g_markup_parse_context_free (ctx);
return (*error != NULL) ? FALSE : TRUE;
}
static void
gxps_file_finalize (GObject *object)
{
GXPSFile *xps = GXPS_FILE (object);
if (xps->priv->zip) {
g_object_unref (xps->priv->zip);
xps->priv->zip = NULL;
}
if (xps->priv->file) {
g_object_unref (xps->priv->file);
xps->priv->file = NULL;
}
if (xps->priv->docs) {
g_list_foreach (xps->priv->docs, (GFunc)g_free, NULL);
g_list_free (xps->priv->docs);
xps->priv->docs = NULL;
}
if (xps->priv->fixed_repr) {
g_free (xps->priv->fixed_repr);
xps->priv->fixed_repr = NULL;
}
if (xps->priv->thumbnail) {
g_free (xps->priv->thumbnail);
xps->priv->thumbnail = NULL;
}
if (xps->priv->core_props) {
g_free (xps->priv->core_props);
xps->priv->core_props = NULL;
}
g_clear_error (&xps->priv->init_error);
G_OBJECT_CLASS (gxps_file_parent_class)->finalize (object);
}
static void
gxps_file_init (GXPSFile *xps)
{
xps->priv = G_TYPE_INSTANCE_GET_PRIVATE (xps,
GXPS_TYPE_FILE,
GXPSFilePrivate);
}
static void
gxps_file_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GXPSFile *xps = GXPS_FILE (object);
switch (prop_id) {
case PROP_FILE:
xps->priv->file = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gxps_file_class_init (GXPSFileClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = gxps_file_set_property;
object_class->finalize = gxps_file_finalize;
g_object_class_install_property (object_class,
PROP_FILE,
g_param_spec_object ("file",
"File",
"The file file",
G_TYPE_FILE,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_type_class_add_private (klass, sizeof (GXPSFilePrivate));
}
static gboolean
gxps_file_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GXPSFile *xps = GXPS_FILE (initable);
if (xps->priv->initialized) {
if (xps->priv->init_error) {
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
return TRUE;
}
xps->priv->initialized = TRUE;
xps->priv->zip = gxps_archive_new (xps->priv->file, &xps->priv->init_error);
if (!xps->priv->zip) {
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
if (!gxps_file_parse_rels (xps, &xps->priv->init_error)) {
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
if (!xps->priv->fixed_repr) {
g_set_error_literal (&xps->priv->init_error,
GXPS_FILE_ERROR,
GXPS_FILE_ERROR_INVALID,
"Invalid XPS File: fixedrepresentation not found");
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
if (!gxps_file_parse_fixed_repr (xps, &xps->priv->init_error)) {
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
if (!xps->priv->docs) {
g_set_error_literal (&xps->priv->init_error,
GXPS_FILE_ERROR,
GXPS_FILE_ERROR_INVALID,
"Invalid XPS File: no documents found");
g_propagate_error (error, g_error_copy (xps->priv->init_error));
return FALSE;
}
return TRUE;
}
static void
initable_iface_init (GInitableIface *initable_iface)
{
initable_iface->init = gxps_file_initable_init;
}
/**
* gxps_file_new:
* @filename: a #GFile
* @error: #GError for error reporting, or %NULL to ignore
*
* Creates a new #GXPSFile for the given #GFile.
*
* Returns: a #GXPSFile or %NULL on error.
*/
GXPSFile *
gxps_file_new (GFile *filename,
GError **error)
{
g_return_val_if_fail (G_IS_FILE (filename), NULL);
return g_initable_new (GXPS_TYPE_FILE,
NULL, error,
"file", filename,
NULL);
}
/**
* gxps_file_get_n_documents:
* @xps: a #GXPSFile
*
* Gets the number of documents in @xps.
*
* Returns: the number of documents.
*/
guint
gxps_file_get_n_documents (GXPSFile *xps)
{
g_return_val_if_fail (GXPS_IS_FILE (xps), 0);
return g_list_length (xps->priv->docs);
}
/**
* gxps_file_get_document:
* @xps: a #GXPSFile
* @n_doc: the index of the document to get
* @error: #GError for error reporting, or %NULL to ignore
*
* Creates a new #GXPSDocument representing the document at
* index @n_doc in @xps file.
*
* Returns: (transfer full): a new #GXPSDocument or %NULL on error.
* Free the returned object with g_object_unref().
*/
GXPSDocument *
gxps_file_get_document (GXPSFile *xps,
guint n_doc,
GError **error)
{
const gchar *source;
g_return_val_if_fail (GXPS_IS_FILE (xps), NULL);
source = g_list_nth_data (xps->priv->docs, n_doc);
g_assert (source != NULL);
return _gxps_document_new (xps->priv->zip, source, error);
}
/**
* gxps_file_get_document_for_link_target:
* @xps: a #GXPSFile
* @target: a #GXPSLinkTarget
*
* Gets the index of the document in @xps pointed by @target.
* If the #GXPSLinkTarget does not reference a document, or
* referenced document is not found in @xps file -1 will be
* returned. In this case you can look for the page pointed by
* the link target by calling gxps_document_get_page_for_anchor()
* with the anchor of the #GXPSLinkTarget for every document in
* @xps.
*
* Returns: the index of the document pointed by the given
* #GXPSLinkTarget or -1.
*/
gint
gxps_file_get_document_for_link_target (GXPSFile *xps,
GXPSLinkTarget *target)
{
GList *l;
guint n_doc = 0;
const gchar *uri;
g_return_val_if_fail (GXPS_IS_FILE (xps), -1);
g_return_val_if_fail (target != NULL, -1);
uri = gxps_link_target_get_uri (target);
for (l = xps->priv->docs; l; l = g_list_next (l)) {
if (g_ascii_strcasecmp (uri, (gchar *)l->data) == 0)
return n_doc;
n_doc++;
}
return -1;
}
/**
* gxps_file_get_core_properties:
* @xps: a #GXPSFile
* @error: #GError for error reporting, or %NULL to ignore
*
* Create a #GXPSCoreProperties object containing the metadata
* of @xpsm, or %NULL in case of error or if the #GXPSFile
* doesn't contain core properties.
*
* Returns: (transfer full): a new #GXPSCoreProperties or %NULL.
* Free the returned object with g_object_unref().
*/
GXPSCoreProperties *
gxps_file_get_core_properties (GXPSFile *xps,
GError **error)
{
g_return_val_if_fail (GXPS_IS_FILE (xps), NULL);
if (!xps->priv->core_props)
return NULL;
return _gxps_core_properties_new (xps->priv->zip,
xps->priv->core_props,
error);
}
|