4 * Destination localization support for CUPS.
6 * Copyright 2012-2014 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * Include necessary headers...
21 #include "cups-private.h"
28 static void cups_create_localizations(http_t
*http
, cups_dinfo_t
*dinfo
);
29 static int cups_read_strings(cups_file_t
*fp
, char *buffer
, size_t bufsize
,
30 char **id
, char **str
);
31 static char *cups_scan_strings(char *buffer
);
35 * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media
38 * The returned string is stored in the destination information and will become
39 * invalid if the destination information is deleted.
41 * @since CUPS 2.0/OS X 10.10@
44 const char * /* O - Localized string */
45 cupsLocalizeDestMedia(
46 http_t
*http
, /* I - Connection to destination */
47 cups_dest_t
*dest
, /* I - Destination */
48 cups_dinfo_t
*dinfo
, /* I - Destination information */
49 unsigned flags
, /* I - Media flags */
50 cups_size_t
*size
) /* I - Media size */
52 cups_lang_t
*lang
; /* Standard localizations */
53 _cups_message_t key
, /* Search key */
54 *match
; /* Matching entry */
55 pwg_media_t
*pwg
; /* PWG media information */
56 cups_array_t
*db
; /* Media database */
57 _cups_media_db_t
*mdb
; /* Media database entry */
58 char name
[1024], /* Size name */
59 temp
[256]; /* Temporary string */
60 const char *lsize
, /* Localized media size */
61 *lsource
, /* Localized media source */
62 *ltype
; /* Localized media type */
66 * Range check input...
69 if (!http
|| !dest
|| !dinfo
|| !size
)
71 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
76 * See if the localization is cached...
79 if (!dinfo
->localizations
)
80 cups_create_localizations(http
, dinfo
);
83 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
, &key
)) != NULL
)
87 * If not, get the localized size, source, and type strings...
90 lang
= cupsLangDefault();
91 pwg
= pwgMediaForSize(size
->width
, size
->length
);
94 lsize
= _cupsLangString(lang
, pwg
->ppd
);
100 if ((size
->width
% 635) == 0 && (size
->length
% 635) == 0)
103 * Use inches since the size is a multiple of 1/4 inch.
106 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%g x %g")), size
->width
/ 2540.0, size
->length
/ 2540.0);
111 * Use millimeters since the size is not a multiple of 1/4 inch.
114 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%d x %d mm")), (size
->width
+ 50) / 100, (size
->length
+ 50) / 100);
120 if (flags
& CUPS_MEDIA_FLAGS_READY
)
121 db
= dinfo
->ready_db
;
123 db
= dinfo
->media_db
;
125 DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size
->media
));
127 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
129 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
131 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
137 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
139 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
)
146 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]", mdb
, mdb
->key
, mdb
->size_name
, mdb
->source
, mdb
->type
, mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
));
148 lsource
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-source", mdb
->source
);
149 ltype
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-type", mdb
->type
);
157 if (!lsource
&& !ltype
)
159 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
160 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless)")), lsize
);
162 strlcpy(name
, lsize
, sizeof(name
));
166 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
167 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, ltype
);
169 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, ltype
);
173 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
174 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, lsource
);
176 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, lsource
);
180 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
181 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s, %s)")), lsize
, ltype
, lsource
);
183 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s, %s)")), lsize
, ltype
, lsource
);
186 if ((match
= (_cups_message_t
*)calloc(1, sizeof(_cups_message_t
))) == NULL
)
189 match
->id
= strdup(size
->media
);
190 match
->str
= strdup(name
);
192 cupsArrayAdd(dinfo
->localizations
, match
);
199 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
202 * The returned string is stored in the destination information and will become
203 * invalid if the destination information is deleted.
205 * @since CUPS 1.6/OS X 10.8@
208 const char * /* O - Localized string */
209 cupsLocalizeDestOption(
210 http_t
*http
, /* I - Connection to destination */
211 cups_dest_t
*dest
, /* I - Destination */
212 cups_dinfo_t
*dinfo
, /* I - Destination information */
213 const char *option
) /* I - Option to localize */
215 _cups_message_t key
, /* Search key */
216 *match
; /* Matching entry */
219 if (!http
|| !dest
|| !dinfo
)
222 if (!dinfo
->localizations
)
223 cups_create_localizations(http
, dinfo
);
225 if (cupsArrayCount(dinfo
->localizations
) == 0)
228 key
.id
= (char *)option
;
229 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
238 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
241 * The returned string is stored in the destination information and will become
242 * invalid if the destination information is deleted.
244 * @since CUPS 1.6/OS X 10.8@
247 const char * /* O - Localized string */
248 cupsLocalizeDestValue(
249 http_t
*http
, /* I - Connection to destination */
250 cups_dest_t
*dest
, /* I - Destination */
251 cups_dinfo_t
*dinfo
, /* I - Destination information */
252 const char *option
, /* I - Option to localize */
253 const char *value
) /* I - Value to localize */
255 _cups_message_t key
, /* Search key */
256 *match
; /* Matching entry */
257 char pair
[256]; /* option.value pair */
260 if (!http
|| !dest
|| !dinfo
)
263 if (!dinfo
->localizations
)
264 cups_create_localizations(http
, dinfo
);
266 if (cupsArrayCount(dinfo
->localizations
) == 0)
269 snprintf(pair
, sizeof(pair
), "%s.%s", option
, value
);
271 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
280 * 'cups_create_localizations()' - Create the localizations array for a
285 cups_create_localizations(
286 http_t
*http
, /* I - Connection to destination */
287 cups_dinfo_t
*dinfo
) /* I - Destination informations */
289 http_t
*http2
; /* Connection for strings file */
290 http_status_t status
; /* Request status */
291 ipp_attribute_t
*attr
; /* "printer-strings-uri" attribute */
292 char scheme
[32], /* URI scheme */
293 userpass
[256], /* Username/password info */
294 hostname
[256], /* Hostname */
295 resource
[1024], /* Resource */
297 /* Hostname of connection */
298 tempfile
[1024]; /* Temporary filename */
299 int port
; /* Port number */
300 http_encryption_t encryption
; /* Encryption to use */
301 cups_file_t
*temp
; /* Temporary file */
305 * Create an empty message catalog...
308 dinfo
->localizations
= _cupsMessageNew(NULL
);
311 * See if there are any localizations...
314 if ((attr
= ippFindAttribute(dinfo
->attrs
, "printer-strings-uri",
315 IPP_TAG_URI
)) == NULL
)
321 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
327 * Pull apart the URI and determine whether we need to try a different
331 if (httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
332 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
333 hostname
, sizeof(hostname
), &port
, resource
,
334 sizeof(resource
)) < HTTP_URI_STATUS_OK
)
336 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
337 "\"%s\".", attr
->values
[0].string
.text
));
341 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
343 if (!_cups_strcasecmp(http_hostname
, hostname
) &&
344 port
== httpAddrPort(http
->hostaddr
))
347 * Use the same connection...
355 * Connect to the alternate host...
358 if (!strcmp(scheme
, "https"))
359 encryption
= HTTP_ENCRYPTION_ALWAYS
;
361 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
363 if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1,
364 30000, NULL
)) == NULL
)
366 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
367 "%s:%d: %s", hostname
, port
, cupsLastErrorString()));
373 * Get a temporary file...
376 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
378 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
379 "file: %s", cupsLastErrorString()));
385 status
= cupsGetFd(http2
, resource
, cupsFileNumber(temp
));
387 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource
,
388 httpStatus(status
)));
390 if (status
== HTTP_STATUS_OK
)
393 * Got the file, read it...
396 char buffer
[8192], /* Message buffer */
398 *str
; /* Translated message */
399 _cups_message_t
*m
; /* Current message */
401 lseek(cupsFileNumber(temp
), 0, SEEK_SET
);
403 while (cups_read_strings(temp
, buffer
, sizeof(buffer
), &id
, &str
))
405 if ((m
= malloc(sizeof(_cups_message_t
))) == NULL
)
409 m
->str
= strdup(str
);
412 cupsArrayAdd(dinfo
->localizations
, m
);
427 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
428 cupsArrayCount(dinfo
->localizations
)));
443 * 'cups_read_strings()' - Read a pair of strings from a .strings file.
446 static int /* O - 1 on success, 0 on failure */
447 cups_read_strings(cups_file_t
*strings
, /* I - .strings file */
448 char *buffer
, /* I - Line buffer */
449 size_t bufsize
, /* I - Size of line buffer */
450 char **id
, /* O - Pointer to ID string */
451 char **str
) /* O - Pointer to translation string */
453 char *bufptr
; /* Pointer into buffer */
456 while (cupsFileGets(strings
, buffer
, bufsize
))
458 if (buffer
[0] != '\"')
462 bufptr
= cups_scan_strings(buffer
);
469 while (*bufptr
&& *bufptr
!= '\"')
476 bufptr
= cups_scan_strings(bufptr
);
491 * 'cups_scan_strings()' - Scan a quoted string.
494 static char * /* O - End of string */
495 cups_scan_strings(char *buffer
) /* I - Start of string */
497 char *bufptr
; /* Pointer into string */
500 for (bufptr
= buffer
+ 1; *bufptr
&& *bufptr
!= '\"'; bufptr
++)
504 if (bufptr
[1] >= '0' && bufptr
[1] <= '3' &&
505 bufptr
[2] >= '0' && bufptr
[2] <= '7' &&
506 bufptr
[3] >= '0' && bufptr
[3] <= '7')
509 * Decode \nnn octal escape...
512 *bufptr
= (char)(((((bufptr
[1] - '0') << 3) | (bufptr
[2] - '0')) << 3) | (bufptr
[3] - '0'));
513 _cups_strcpy(bufptr
+ 1, bufptr
+ 4);
518 * Decode \C escape...
521 _cups_strcpy(bufptr
, bufptr
+ 1);
524 else if (*bufptr
== 'r')
526 else if (*bufptr
== 't')