]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dest-localization.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / cups / dest-localization.c
index 1bd8324af3b335e3ba9705ab7aebda2a588fa589..67d01f496f6736fbbf95a89e9ffb50df758b1cd1 100644 (file)
@@ -1,17 +1,9 @@
 /*
- * "$Id$"
- *
  * Destination localization support for CUPS.
  *
- * Copyright 2012-2014 by Apple Inc.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Copyright 2012-2017 by Apple Inc.
  *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -19,6 +11,7 @@
  */
 
 #include "cups-private.h"
+#include "debug-internal.h"
 
 
 /*
  */
 
 static void    cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
-static int     cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize,
-                                 char **id, char **str);
-static char    *cups_scan_strings(char *buffer);
+
+
+/*
+ * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media
+ *                             size.
+ *
+ * The returned string is stored in the destination information and will become
+ * invalid if the destination information is deleted.
+ *
+ * @since CUPS 2.0/macOS 10.10@
+ */
+
+const char *                           /* O - Localized string */
+cupsLocalizeDestMedia(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    unsigned     flags,                        /* I - Media flags */
+    cups_size_t  *size)                        /* I - Media size */
+{
+  cups_lang_t          *lang;          /* Standard localizations */
+  _cups_message_t      key,            /* Search key */
+                       *match;         /* Matching entry */
+  pwg_media_t          *pwg;           /* PWG media information */
+  cups_array_t         *db;            /* Media database */
+  _cups_media_db_t     *mdb;           /* Media database entry */
+  char                 lstr[1024],     /* Localized size name */
+                       temp[256];      /* Temporary string */
+  const char           *lsize,         /* Localized media size */
+                       *lsource,       /* Localized media source */
+                       *ltype;         /* Localized media type */
+
+
+  DEBUG_printf(("cupsLocalizeDestMedia(http=%p, dest=%p, dinfo=%p, flags=%x, size=%p(\"%s\"))", (void *)http, (void *)dest, (void *)dinfo, flags, (void *)size, size ? size->media : "(null)"));
+
+ /*
+  * Range check input...
+  */
+
+  if (!http || !dest || !dinfo || !size)
+  {
+    DEBUG_puts("1cupsLocalizeDestMedia: Returning NULL.");
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (NULL);
+  }
+
+ /*
+  * Find the matching media database entry...
+  */
+
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+    db = dinfo->ready_db;
+  else
+    db = dinfo->media_db;
+
+  DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size->media));
+
+  for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
+  {
+    if (mdb->key && !strcmp(mdb->key, size->media))
+      break;
+    else if (mdb->size_name && !strcmp(mdb->size_name, size->media))
+      break;
+  }
+
+  if (!mdb)
+  {
+    for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
+    {
+      if (mdb->width == size->width && mdb->length == size->length && mdb->bottom == size->bottom && mdb->left == size->left && mdb->right == size->right && mdb->top == size->top)
+       break;
+    }
+  }
+
+ /*
+  * See if the localization is cached...
+  */
+
+  lang = cupsLangDefault();
+
+  if (!dinfo->localizations)
+    cups_create_localizations(http, dinfo);
+
+  snprintf(temp, sizeof(temp), "media.%s", size->media);
+  key.msg = temp;
+
+  if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
+  {
+    lsize = match->str;
+  }
+  else
+  {
+   /*
+    * Not a media name, try a media-key name...
+    */
+
+    snprintf(temp, sizeof(temp), "media-key.%s", size->media);
+    if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
+      lsize = match->str;
+    else
+      lsize = NULL;
+  }
+
+  if (!lsize && (pwg = pwgMediaForSize(size->width, size->length)) != NULL && pwg->ppd)
+  {
+   /*
+    * Get a standard localization...
+    */
+
+    snprintf(temp, sizeof(temp), "media.%s", pwg->pwg);
+    if ((lsize = _cupsLangString(lang, temp)) == temp)
+      lsize = NULL;
+  }
+
+  if (!lsize)
+  {
+   /*
+    * Make a dimensional localization...
+    */
+
+    if ((size->width % 635) == 0 && (size->length % 635) == 0)
+    {
+     /*
+      * Use inches since the size is a multiple of 1/4 inch.
+      */
+
+      snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g \"")), size->width / 2540.0, size->length / 2540.0);
+    }
+    else
+    {
+     /*
+      * Use millimeters since the size is not a multiple of 1/4 inch.
+      */
+
+      snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%d x %d mm")), (size->width + 50) / 100, (size->length + 50) / 100);
+    }
+
+    lsize = temp;
+  }
+
+  if (mdb)
+  {
+    DEBUG_printf(("1cupsLocalizeDestMedia: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb, mdb->key, mdb->size_name, mdb->source, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top));
+
+    if ((lsource = cupsLocalizeDestValue(http, dest, dinfo, "media-source", mdb->source)) == mdb->source && mdb->source)
+      lsource = _cupsLangString(lang, _("Other Tray"));
+    if ((ltype = cupsLocalizeDestValue(http, dest, dinfo, "media-type", mdb->type)) == mdb->type && mdb->type)
+      ltype = _cupsLangString(lang, _("Other Media"));
+  }
+  else
+  {
+    lsource = NULL;
+    ltype   = NULL;
+  }
+
+  if (!lsource && !ltype)
+  {
+    if (!size->bottom && !size->left && !size->right && !size->top)
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless)")), lsize);
+    else
+      strlcpy(lstr, lsize, sizeof(lstr));
+  }
+  else if (!lsource)
+  {
+    if (!size->bottom && !size->left && !size->right && !size->top)
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, ltype);
+    else
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, ltype);
+  }
+  else if (!ltype)
+  {
+    if (!size->bottom && !size->left && !size->right && !size->top)
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, lsource);
+    else
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, lsource);
+  }
+  else
+  {
+    if (!size->bottom && !size->left && !size->right && !size->top)
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s, %s)")), lsize, ltype, lsource);
+    else
+      snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s, %s)")), lsize, ltype, lsource);
+  }
+
+  if ((match = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL)
+    return (NULL);
+
+  match->msg = strdup(size->media);
+  match->str = strdup(lstr);
+
+  cupsArrayAdd(dinfo->localizations, match);
+
+  DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match->str));
+
+  return (match->str);
+}
 
 
 /*
@@ -38,7 +224,7 @@ static char  *cups_scan_strings(char *buffer);
  * The returned string is stored in the destination information and will become
  * invalid if the destination information is deleted.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 const char *                           /* O - Localized string */
@@ -50,21 +236,22 @@ cupsLocalizeDestOption(
 {
   _cups_message_t      key,            /* Search key */
                        *match;         /* Matching entry */
+  const char            *localized;     /* Localized string */
 
 
+  DEBUG_printf(("cupsLocalizeDestOption(http=%p, dest=%p, dinfo=%p, option=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option));
+
   if (!http || !dest || !dinfo)
     return (option);
 
   if (!dinfo->localizations)
     cups_create_localizations(http, dinfo);
 
-  if (cupsArrayCount(dinfo->localizations) == 0)
-    return (option);
-
-  key.id = (char *)option;
-  if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
-                                                &key)) != NULL)
+  key.msg = (char *)option;
+  if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
     return (match->str);
+  else if ((localized = _cupsLangString(cupsLangDefault(), option)) != NULL)
+    return (localized);
   else
     return (option);
 }
@@ -77,7 +264,7 @@ cupsLocalizeDestOption(
  * The returned string is stored in the destination information and will become
  * invalid if the destination information is deleted.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 const char *                           /* O - Localized string */
@@ -91,22 +278,39 @@ cupsLocalizeDestValue(
   _cups_message_t      key,            /* Search key */
                        *match;         /* Matching entry */
   char                 pair[256];      /* option.value pair */
+  const char            *localized;     /* Localized string */
 
 
+  DEBUG_printf(("cupsLocalizeDestValue(http=%p, dest=%p, dinfo=%p, option=\"%s\", value=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option, value));
+
   if (!http || !dest || !dinfo)
     return (value);
 
+  if (!strcmp(option, "media"))
+  {
+    pwg_media_t *media = pwgMediaForPWG(value);
+    cups_size_t size;
+
+    strlcpy(size.media, value, sizeof(size.media));
+    size.width  = media ? media->width : 0;
+    size.length = media ? media->length : 0;
+    size.left   = 0;
+    size.right  = 0;
+    size.bottom = 0;
+    size.top    = 0;
+
+    return (cupsLocalizeDestMedia(http, dest, dinfo, CUPS_MEDIA_FLAGS_DEFAULT, &size));
+  }
+
   if (!dinfo->localizations)
     cups_create_localizations(http, dinfo);
 
-  if (cupsArrayCount(dinfo->localizations) == 0)
-    return (value);
-
   snprintf(pair, sizeof(pair), "%s.%s", option, value);
-  key.id = pair;
-  if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
-                                                &key)) != NULL)
+  key.msg = pair;
+  if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
     return (match->str);
+  else if ((localized = _cupsLangString(cupsLangDefault(), pair)) != NULL && strcmp(localized, pair))
+    return (localized);
   else
     return (value);
 }
@@ -137,12 +341,6 @@ cups_create_localizations(
   cups_file_t          *temp;          /* Temporary file */
 
 
- /*
-  * Create an empty message catalog...
-  */
-
-  dinfo->localizations = _cupsMessageNew(NULL);
-
  /*
   * See if there are any localizations...
   */
@@ -151,12 +349,12 @@ cups_create_localizations(
                                IPP_TAG_URI)) == NULL)
   {
    /*
-    * Nope...
+    * Nope, create an empty message catalog...
     */
 
-    DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
-               "value.");
-    return;                            /* Nope */
+    dinfo->localizations = _cupsMessageNew(NULL);
+    DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) value.");
+    return;
   }
 
  /*
@@ -169,8 +367,8 @@ cups_create_localizations(
                       hostname, sizeof(hostname), &port, resource,
                       sizeof(resource)) < HTTP_URI_STATUS_OK)
   {
-    DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
-                  "\"%s\".", attr->values[0].string.text));
+    dinfo->localizations = _cupsMessageNew(NULL);
+    DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value \"%s\".", attr->values[0].string.text));
     return;
   }
 
@@ -219,9 +417,9 @@ cups_create_localizations(
   }
 
   status = cupsGetFd(http2, resource, cupsFileNumber(temp));
+  cupsFileClose(temp);
 
-  DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource,
-                httpStatus(status)));
+  DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, httpStatus(status)));
 
   if (status == HTTP_STATUS_OK)
   {
@@ -229,35 +427,7 @@ cups_create_localizations(
     * Got the file, read it...
     */
 
-    char               buffer[8192],   /* Message buffer */
-                       *id,            /* ID string */
-                       *str;           /* Translated message */
-    _cups_message_t    *m;             /* Current message */
-
-    lseek(cupsFileNumber(temp), 0, SEEK_SET);
-
-    while (cups_read_strings(temp, buffer, sizeof(buffer), &id, &str))
-    {
-      if ((m = malloc(sizeof(_cups_message_t))) == NULL)
-        break;
-
-      m->id  = strdup(id);
-      m->str = strdup(str);
-
-      if (m->id && m->str)
-        cupsArrayAdd(dinfo->localizations, m);
-      else
-      {
-        if (m->id)
-          free(m->id);
-
-        if (m->str)
-          free(m->str);
-
-        free(m);
-        break;
-      }
-    }
+    dinfo->localizations = _cupsMessageLoad(tempfile, _CUPS_MESSAGE_STRINGS);
   }
 
   DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
@@ -268,108 +438,8 @@ cups_create_localizations(
   */
 
   unlink(tempfile);
-  cupsFileClose(temp);
 
   if (http2 != http)
     httpClose(http2);
 }
 
-
-/*
- * 'cups_read_strings()' - Read a pair of strings from a .strings file.
- */
-
-static int                             /* O - 1 on success, 0 on failure */
-cups_read_strings(cups_file_t *strings,        /* I - .strings file */
-                  char        *buffer, /* I - Line buffer */
-                  size_t      bufsize, /* I - Size of line buffer */
-                 char        **id,     /* O - Pointer to ID string */
-                 char        **str)    /* O - Pointer to translation string */
-{
-  char *bufptr;                        /* Pointer into buffer */
-
-
-  while (cupsFileGets(strings, buffer, bufsize))
-  {
-    if (buffer[0] != '\"')
-      continue;
-
-    *id    = buffer + 1;
-    bufptr = cups_scan_strings(buffer);
-
-    if (*bufptr != '\"')
-      continue;
-
-    *bufptr++ = '\0';
-
-    while (*bufptr && *bufptr != '\"')
-      bufptr ++;
-
-    if (!*bufptr)
-      continue;
-
-    *str   = bufptr + 1;
-    bufptr = cups_scan_strings(bufptr);
-
-    if (*bufptr != '\"')
-      continue;
-
-    *bufptr = '\0';
-
-    return (1);
-  }
-
-  return (0);
-}
-
-
-/*
- * 'cups_scan_strings()' - Scan a quoted string.
- */
-
-static char *                          /* O - End of string */
-cups_scan_strings(char *buffer)                /* I - Start of string */
-{
-  char *bufptr;                        /* Pointer into string */
-
-
-  for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
-  {
-    if (*bufptr == '\\')
-    {
-      if (bufptr[1] >= '0' && bufptr[1] <= '3' &&
-         bufptr[2] >= '0' && bufptr[2] <= '7' &&
-         bufptr[3] >= '0' && bufptr[3] <= '7')
-      {
-       /*
-       * Decode \nnn octal escape...
-       */
-
-       *bufptr = (char)(((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) | (bufptr[3] - '0'));
-       _cups_strcpy(bufptr + 1, bufptr + 4);
-      }
-      else
-      {
-       /*
-       * Decode \C escape...
-       */
-
-       _cups_strcpy(bufptr, bufptr + 1);
-       if (*bufptr == 'n')
-         *bufptr = '\n';
-       else if (*bufptr == 'r')
-         *bufptr = '\r';
-       else if (*bufptr == 't')
-         *bufptr = '\t';
-      }
-    }
-  }
-
-  return (bufptr);
-}
-
-
-
-/*
- * End of "$Id$".
- */