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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2009 Carlos Garcia Campos
* Copyright (C) 2004 Marco Pesenti Gritti
* Copyright © 2021 Christian Persch
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "pps-document-info.h"
#include "pps-xmp.h"
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
#include <langinfo.h>
#endif
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
G_DEFINE_BOXED_TYPE (PpsDocumentInfo, pps_document_info, pps_document_info_copy, pps_document_info_free)
/**
* pps_document_info_new:
*
* Returns: (transfer full): a new, empty #PpsDocumentInfo
*/
PpsDocumentInfo *
pps_document_info_new (void)
{
return g_new0 (PpsDocumentInfo, 1);
}
/**
* pps_document_info_copy:
* @info: a #PpsDocumentInfo
*
* Returns: (transfer full): a copy of @info
*/
PpsDocumentInfo *
pps_document_info_copy (const PpsDocumentInfo *info)
{
PpsDocumentInfo *copy;
g_return_val_if_fail (info != NULL, NULL);
copy = pps_document_info_new ();
copy->title = g_strdup (info->title);
copy->format = g_strdup (info->format);
copy->author = g_strdup (info->author);
copy->subject = g_strdup (info->subject);
copy->keywords = g_strdup (info->keywords);
copy->security = g_strdup (info->security);
copy->creator = g_strdup (info->creator);
copy->producer = g_strdup (info->producer);
copy->linearized = g_strdup (info->linearized);
copy->creation_datetime = g_date_time_ref (info->creation_datetime);
copy->modified_datetime = g_date_time_ref (info->modified_datetime);
copy->layout = info->layout;
copy->mode = info->mode;
copy->ui_hints = info->ui_hints;
copy->permissions = info->permissions;
copy->n_pages = info->n_pages;
copy->license = pps_document_license_copy (info->license);
copy->fields_mask = info->fields_mask;
return copy;
}
/**
* pps_document_info_free:
* @info: (transfer full): a #PpsDocumentInfo
*
* Frees @info.
*/
void
pps_document_info_free (PpsDocumentInfo *info)
{
if (info == NULL)
return;
g_free (info->title);
g_free (info->format);
g_free (info->author);
g_free (info->subject);
g_free (info->keywords);
g_free (info->creator);
g_free (info->producer);
g_free (info->linearized);
g_free (info->security);
pps_document_license_free (info->license);
g_clear_pointer (&info->creation_datetime, g_date_time_unref);
g_clear_pointer (&info->modified_datetime, g_date_time_unref);
g_free (info);
}
/*
* pps_document_info_take_created_datetime:
* @info: a #PpsDocumentInfo
* @datetime: (transfer full): a #GDateTime
*
* Sets the #GDateTime for when the document was created.
*/
void
pps_document_info_take_created_datetime (PpsDocumentInfo *info,
GDateTime *datetime)
{
g_return_if_fail (info != NULL);
g_clear_pointer (&info->creation_datetime, g_date_time_unref);
info->creation_datetime = datetime;
info->fields_mask |= PPS_DOCUMENT_INFO_CREATION_DATETIME;
}
/**
* pps_document_info_get_created_datetime:
* @info: a #PpsDocumentInfo
*
* Returns: (transfer none) (nullable): a #GDateTime for when the document was created
*/
GDateTime *
pps_document_info_get_created_datetime (const PpsDocumentInfo *info)
{
g_return_val_if_fail (info != NULL, NULL);
g_return_val_if_fail (info->fields_mask & PPS_DOCUMENT_INFO_CREATION_DATETIME, NULL);
return info->creation_datetime;
}
/*
* pps_document_info_take_modified_datetime:
* @info: a #PpsDocumentInfo
* @datetime: (transfer full): a #GDateTime
*
* Sets the #GDateTime for when the document was last modified.
*/
void
pps_document_info_take_modified_datetime (PpsDocumentInfo *info,
GDateTime *datetime)
{
g_return_if_fail (info != NULL);
g_clear_pointer (&info->modified_datetime, g_date_time_unref);
info->modified_datetime = datetime;
info->fields_mask |= PPS_DOCUMENT_INFO_MOD_DATETIME;
}
/**
* pps_document_info_get_modified_datetime:
* @info: a #PpsDocumentInfo
*
* Returns: (transfer none) (nullable): a #GDateTime for when the document was last modified
*/
GDateTime *
pps_document_info_get_modified_datetime (const PpsDocumentInfo *info)
{
g_return_val_if_fail (info != NULL, NULL);
g_return_val_if_fail (info->fields_mask & PPS_DOCUMENT_INFO_MOD_DATETIME, NULL);
return info->modified_datetime;
}
/*
* pps_document_info_set_from_xmp:
* @info: a #PpsDocumentInfo
* @xmp: a XMP document
* @size: the size of @xmp in bytes, or -1 if @xmp is a NUL-terminated string
*
* Parses the XMP document and sets @info from it.
*
* Returns: %TRUE iff @xmp could be successfully parsed as a XMP document
*/
gboolean
pps_document_info_set_from_xmp (PpsDocumentInfo *info,
const char *xmp,
gssize size)
{
return pps_xmp_parse (xmp, size != -1 ? size : strlen (xmp), info);
}
static GtkUnit
get_default_user_units (void)
{
/* Translate to the default units to use for presenting
* lengths to the user. Translate to default:inch if you
* want inches, otherwise translate to default:mm.
* Do *not* translate it to "predefinito:mm", if it
* it isn't default:mm or default:inch it will not work
*/
gchar *e = _ ("default:mm");
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
gchar *imperial = NULL;
imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if (imperial && imperial[0] == 2)
return GTK_UNIT_INCH; /* imperial */
if (imperial && imperial[0] == 1)
return GTK_UNIT_MM; /* metric */
#endif
if (strcmp (e, "default:mm") == 0)
return GTK_UNIT_MM;
if (strcmp (e, "default:inch") == 0)
return GTK_UNIT_INCH;
g_warning ("Whoever translated default:mm did so wrongly.\n");
return GTK_UNIT_MM;
}
static gdouble
get_tolerance (gdouble size)
{
if (size < 150.0f)
return 1.5f;
else if (size >= 150.0f && size <= 600.0f)
return 2.0f;
else
return 3.0f;
}
/**
* pps_document_info_regular_pager_size:
* @info: a #PpsDocumentInfo
*
* Returns: (transfer full) (nullable): A string represent the regular paper size
*/
char *
pps_document_info_regular_paper_size (const PpsDocumentInfo *info)
{
GList *paper_sizes, *l;
gchar *exact_size;
gchar *str = NULL;
GtkUnit units;
g_return_val_if_fail (info->fields_mask & PPS_DOCUMENT_INFO_PAPER_SIZE, NULL);
units = get_default_user_units ();
if (units == GTK_UNIT_MM) {
exact_size = g_strdup_printf (_ ("%.0f × %.0f mm"),
info->paper_width,
info->paper_height);
} else {
exact_size = g_strdup_printf (_ ("%.2f × %.2f inch"),
info->paper_width / 25.4f,
info->paper_height / 25.4f);
}
paper_sizes = gtk_paper_size_get_paper_sizes (FALSE);
for (l = paper_sizes; l && l->data; l = g_list_next (l)) {
GtkPaperSize *size = (GtkPaperSize *) l->data;
gdouble paper_width;
gdouble paper_height;
gdouble width_tolerance;
gdouble height_tolerance;
paper_width = gtk_paper_size_get_width (size, GTK_UNIT_MM);
paper_height = gtk_paper_size_get_height (size, GTK_UNIT_MM);
width_tolerance = get_tolerance (paper_width);
height_tolerance = get_tolerance (paper_height);
if (ABS (info->paper_height - paper_height) <= height_tolerance &&
ABS (info->paper_width - paper_width) <= width_tolerance) {
/* Note to translators: first placeholder is the paper name (eg.
* A4), second placeholder is the paper size (eg. 297x210 mm) */
str = g_strdup_printf (_ ("%s, Portrait (%s)"),
gtk_paper_size_get_display_name (size),
exact_size);
} else if (ABS (info->paper_width - paper_height) <= height_tolerance &&
ABS (info->paper_height - paper_width) <= width_tolerance) {
/* Note to translators: first placeholder is the paper name (eg.
* A4), second placeholder is the paper size (eg. 297x210 mm) */
str = g_strdup_printf (_ ("%s, Landscape (%s)"),
gtk_paper_size_get_display_name (size),
exact_size);
}
}
g_clear_list (&paper_sizes, (GDestroyNotify) gtk_paper_size_free);
if (str != NULL) {
g_free (exact_size);
return str;
}
return exact_size;
}
/**
* pps_document_info_pages:
* @info: a #PpsDocumentInfo
* @pages: (out) (optional):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_pages (const PpsDocumentInfo *info, gint *pages)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_PAPER_SIZE;
if (has_value && pages)
*pages = info->n_pages;
return has_value;
}
/**
* pps_document_info_permissions:
* @info: a #PpsDocumentInfo
* @permissions: (out) (optional):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_permissions (const PpsDocumentInfo *info, PpsDocumentPermissions *permissions)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_PERMISSIONS;
if (has_value && permissions)
*permissions = info->permissions;
return has_value;
}
/**
* pps_document_info_start_mode:
* @info: a #PpsDocumentInfo
* @mode: (out) (optional):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_start_mode (const PpsDocumentInfo *info, PpsDocumentMode *mode)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_START_MODE;
if (has_value && mode)
*mode = info->mode;
return has_value;
}
/**
* pps_document_info_license:
* @info: a #PpsDocumentInfo
* @license: (out) (optional):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_license (const PpsDocumentInfo *info, PpsDocumentLicense **license)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_LICENSE;
if (has_value && license)
*license = pps_document_license_copy (info->license);
return has_value;
}
/**
* pps_document_info_contains_js:
* @info: a #PpsDocumentInfo
* @contains_js: (out) (optional):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_contains_js (const PpsDocumentInfo *info, PpsDocumentContainsJS *contains_js)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_CONTAINS_JS;
if (has_value && contains_js)
*contains_js = info->contains_js;
return has_value;
}
/**
* pps_document_info_title:
* @info: a #PpsDocumentInfo
* @title: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_title (const PpsDocumentInfo *info, gchar **title)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_TITLE;
if (has_value && title)
*title = g_strdup (info->title);
return has_value;
}
/**
* pps_document_info_format:
* @info: a #PpsDocumentInfo
* @format: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_format (const PpsDocumentInfo *info, gchar **format)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_FORMAT;
if (has_value && format)
*format = g_strdup (info->format);
return has_value;
}
/**
* pps_document_info_author:
* @info: a #PpsDocumentInfo
* @author: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_author (const PpsDocumentInfo *info, gchar **author)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_AUTHOR;
if (has_value && author)
*author = g_strdup (info->author);
return has_value;
}
/**
* pps_document_info_subject:
* @info: a #PpsDocumentInfo
* @subject: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_subject (const PpsDocumentInfo *info, gchar **subject)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_SUBJECT;
if (has_value && subject)
*subject = g_strdup (info->subject);
return has_value;
}
/**
* pps_document_info_keywords:
* @info: a #PpsDocumentInfo
* @keywords: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_keywords (const PpsDocumentInfo *info, gchar **keywords)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_KEYWORDS;
if (has_value && keywords)
*keywords = g_strdup (info->keywords);
return has_value;
}
/**
* pps_document_info_creator:
* @info: a #PpsDocumentInfo
* @creator: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_creator (const PpsDocumentInfo *info, gchar **creator)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_CREATOR;
if (has_value && creator)
*creator = g_strdup (info->creator);
return has_value;
}
/**
* pps_document_info_producer:
* @info: a #PpsDocumentInfo
* @producer: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_producer (const PpsDocumentInfo *info, gchar **producer)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_PRODUCER;
if (has_value && producer)
*producer = g_strdup (info->producer);
return has_value;
}
/**
* pps_document_info_linearized:
* @info: a #PpsDocumentInfo
* @linearized: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_linearized (const PpsDocumentInfo *info, gchar **linearized)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_LINEARIZED;
if (has_value && linearized)
*linearized = g_strdup (info->linearized);
return has_value;
}
/**
* pps_document_info_security:
* @info: a #PpsDocumentInfo
* @security: (out) (optional) (transfer full):
*
* Returns: %TRUE iff info has this field
*/
gboolean
pps_document_info_security (const PpsDocumentInfo *info, gchar **security)
{
gboolean has_value = info->fields_mask & PPS_DOCUMENT_INFO_SECURITY;
if (has_value && security)
*security = g_strdup (info->security);
return has_value;
}
/* PpsDocumentLicense */
G_DEFINE_BOXED_TYPE (PpsDocumentLicense, pps_document_license, pps_document_license_copy, pps_document_license_free)
/**
* pps_document_license_new:
*
* Returns: (transfer full): a new, empty #PpsDocumentLicense
*/
PpsDocumentLicense *
pps_document_license_new (void)
{
return g_new0 (PpsDocumentLicense, 1);
}
/**
* pps_document_license_copy:
* @license: (nullable): a #PpsDocumentLicense
*
* Returns: (transfer full): a copy of @license, or %NULL
*/
PpsDocumentLicense *
pps_document_license_copy (PpsDocumentLicense *license)
{
PpsDocumentLicense *new_license;
if (!license)
return NULL;
new_license = pps_document_license_new ();
if (license->text)
new_license->text = g_strdup (license->text);
if (license->uri)
new_license->uri = g_strdup (license->uri);
if (license->web_statement)
new_license->web_statement = g_strdup (license->web_statement);
return new_license;
}
/**
* pps_document_license_free:
* @license: (transfer full): a #PpsDocumentLicense
*
* Frees @license.
*/
void
pps_document_license_free (PpsDocumentLicense *license)
{
if (!license)
return;
g_free (license->text);
g_free (license->uri);
g_free (license->web_statement);
g_free (license);
}
/**
* pps_document_license_get_text:
* @license: (transfer full): a #PpsDocumentLicense
*
* Returns: (transfer none) (nullable): the license text
*/
const gchar *
pps_document_license_get_text (PpsDocumentLicense *license)
{
return license->text;
}
/**
* pps_document_license_get_uri:
* @license: (transfer full): a #PpsDocumentLicense
*
* Returns: (transfer none) (nullable): the license URI
*/
const gchar *
pps_document_license_get_uri (PpsDocumentLicense *license)
{
return license->uri;
}
/**
* pps_document_license_get_web_statement
* @license: (transfer full): a #PpsDocumentLicense
*
* Returns: (transfer none) (nullable): the license web statement
*/
const gchar *
pps_document_license_get_web_statement (PpsDocumentLicense *license)
{
return license->web_statement;
}
|