2 * Destination localization support for CUPS.
4 * Copyright 2012-2014 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
20 static void cups_create_localizations(http_t
*http
, cups_dinfo_t
*dinfo
);
21 static int cups_read_strings(cups_file_t
*fp
, char *buffer
, size_t bufsize
,
22 char **id
, char **str
);
23 static char *cups_scan_strings(char *buffer
);
27 * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media
30 * The returned string is stored in the destination information and will become
31 * invalid if the destination information is deleted.
33 * @since CUPS 2.0/macOS 10.10@
36 const char * /* O - Localized string */
37 cupsLocalizeDestMedia(
38 http_t
*http
, /* I - Connection to destination */
39 cups_dest_t
*dest
, /* I - Destination */
40 cups_dinfo_t
*dinfo
, /* I - Destination information */
41 unsigned flags
, /* I - Media flags */
42 cups_size_t
*size
) /* I - Media size */
44 cups_lang_t
*lang
; /* Standard localizations */
45 _cups_message_t key
, /* Search key */
46 *match
; /* Matching entry */
47 pwg_media_t
*pwg
; /* PWG media information */
48 cups_array_t
*db
; /* Media database */
49 _cups_media_db_t
*mdb
; /* Media database entry */
50 char name
[1024], /* Size name */
51 temp
[256]; /* Temporary string */
52 const char *lsize
, /* Localized media size */
53 *lsource
, /* Localized media source */
54 *ltype
; /* Localized media type */
57 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)"));
60 * Range check input...
63 if (!http
|| !dest
|| !dinfo
|| !size
)
65 DEBUG_puts("1cupsLocalizeDestMedia: Returning NULL.");
66 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
71 * See if the localization is cached...
74 if (!dinfo
->localizations
)
75 cups_create_localizations(http
, dinfo
);
78 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
, &key
)) != NULL
)
80 DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match
->str
));
85 * If not, get the localized size, source, and type strings...
88 lang
= cupsLangDefault();
90 snprintf(temp
, sizeof(temp
), "media.%s", size
->media
);
91 if ((lsize
= _cupsLangString(lang
, temp
)) != NULL
&& strcmp(lsize
, temp
))
93 DEBUG_printf(("1cupsLocalizeDestMedia: Returning standard localization \"%s\".", lsize
));
97 pwg
= pwgMediaForSize(size
->width
, size
->length
);
100 lsize
= _cupsLangString(lang
, pwg
->ppd
);
106 if ((size
->width
% 635) == 0 && (size
->length
% 635) == 0)
109 * Use inches since the size is a multiple of 1/4 inch.
112 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%g x %g \"")), size
->width
/ 2540.0, size
->length
/ 2540.0);
117 * Use millimeters since the size is not a multiple of 1/4 inch.
120 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%d x %d mm")), (size
->width
+ 50) / 100, (size
->length
+ 50) / 100);
126 if (flags
& CUPS_MEDIA_FLAGS_READY
)
127 db
= dinfo
->ready_db
;
129 db
= dinfo
->media_db
;
131 DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size
->media
));
133 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
135 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
137 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
143 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
145 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
)
152 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
));
154 lsource
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-source", mdb
->source
);
155 ltype
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-type", mdb
->type
);
163 if (!lsource
&& !ltype
)
165 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
166 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless)")), lsize
);
168 strlcpy(name
, lsize
, sizeof(name
));
172 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
173 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, ltype
);
175 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, ltype
);
179 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
180 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, lsource
);
182 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, lsource
);
186 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
187 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s, %s)")), lsize
, ltype
, lsource
);
189 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s, %s)")), lsize
, ltype
, lsource
);
192 if ((match
= (_cups_message_t
*)calloc(1, sizeof(_cups_message_t
))) == NULL
)
195 match
->id
= strdup(size
->media
);
196 match
->str
= strdup(name
);
198 cupsArrayAdd(dinfo
->localizations
, match
);
200 DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match
->str
));
207 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
210 * The returned string is stored in the destination information and will become
211 * invalid if the destination information is deleted.
213 * @since CUPS 1.6/macOS 10.8@
216 const char * /* O - Localized string */
217 cupsLocalizeDestOption(
218 http_t
*http
, /* I - Connection to destination */
219 cups_dest_t
*dest
, /* I - Destination */
220 cups_dinfo_t
*dinfo
, /* I - Destination information */
221 const char *option
) /* I - Option to localize */
223 _cups_message_t key
, /* Search key */
224 *match
; /* Matching entry */
225 const char *localized
; /* Localized string */
228 DEBUG_printf(("cupsLocalizeDestOption(http=%p, dest=%p, dinfo=%p, option=\"%s\")", (void *)http
, (void *)dest
, (void *)dinfo
, option
));
230 if (!http
|| !dest
|| !dinfo
)
233 if (!dinfo
->localizations
)
234 cups_create_localizations(http
, dinfo
);
236 key
.id
= (char *)option
;
237 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
240 else if ((localized
= _cupsLangString(cupsLangDefault(), option
)) != NULL
)
248 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
251 * The returned string is stored in the destination information and will become
252 * invalid if the destination information is deleted.
254 * @since CUPS 1.6/macOS 10.8@
257 const char * /* O - Localized string */
258 cupsLocalizeDestValue(
259 http_t
*http
, /* I - Connection to destination */
260 cups_dest_t
*dest
, /* I - Destination */
261 cups_dinfo_t
*dinfo
, /* I - Destination information */
262 const char *option
, /* I - Option to localize */
263 const char *value
) /* I - Value to localize */
265 _cups_message_t key
, /* Search key */
266 *match
; /* Matching entry */
267 char pair
[256]; /* option.value pair */
268 const char *localized
; /* Localized string */
271 DEBUG_printf(("cupsLocalizeDestValue(http=%p, dest=%p, dinfo=%p, option=\"%s\", value=\"%s\")", (void *)http
, (void *)dest
, (void *)dinfo
, option
, value
));
273 if (!http
|| !dest
|| !dinfo
)
276 if (!strcmp(option
, "media"))
278 pwg_media_t
*media
= pwgMediaForPWG(value
);
281 strlcpy(size
.media
, value
, sizeof(size
.media
));
282 size
.width
= media
? media
->width
: 0;
283 size
.length
= media
? media
->length
: 0;
289 return (cupsLocalizeDestMedia(http
, dest
, dinfo
, CUPS_MEDIA_FLAGS_DEFAULT
, &size
));
292 if (!dinfo
->localizations
)
293 cups_create_localizations(http
, dinfo
);
295 snprintf(pair
, sizeof(pair
), "%s.%s", option
, value
);
297 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
300 else if ((localized
= _cupsLangString(cupsLangDefault(), pair
)) != NULL
&& strcmp(localized
, pair
))
308 * 'cups_create_localizations()' - Create the localizations array for a
313 cups_create_localizations(
314 http_t
*http
, /* I - Connection to destination */
315 cups_dinfo_t
*dinfo
) /* I - Destination informations */
317 http_t
*http2
; /* Connection for strings file */
318 http_status_t status
; /* Request status */
319 ipp_attribute_t
*attr
; /* "printer-strings-uri" attribute */
320 char scheme
[32], /* URI scheme */
321 userpass
[256], /* Username/password info */
322 hostname
[256], /* Hostname */
323 resource
[1024], /* Resource */
325 /* Hostname of connection */
326 tempfile
[1024]; /* Temporary filename */
327 int port
; /* Port number */
328 http_encryption_t encryption
; /* Encryption to use */
329 cups_file_t
*temp
; /* Temporary file */
333 * Create an empty message catalog...
336 dinfo
->localizations
= _cupsMessageNew(NULL
);
339 * See if there are any localizations...
342 if ((attr
= ippFindAttribute(dinfo
->attrs
, "printer-strings-uri",
343 IPP_TAG_URI
)) == NULL
)
349 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
355 * Pull apart the URI and determine whether we need to try a different
359 if (httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
360 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
361 hostname
, sizeof(hostname
), &port
, resource
,
362 sizeof(resource
)) < HTTP_URI_STATUS_OK
)
364 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
365 "\"%s\".", attr
->values
[0].string
.text
));
369 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
371 if (!_cups_strcasecmp(http_hostname
, hostname
) &&
372 port
== httpAddrPort(http
->hostaddr
))
375 * Use the same connection...
383 * Connect to the alternate host...
386 if (!strcmp(scheme
, "https"))
387 encryption
= HTTP_ENCRYPTION_ALWAYS
;
389 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
391 if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1,
392 30000, NULL
)) == NULL
)
394 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
395 "%s:%d: %s", hostname
, port
, cupsLastErrorString()));
401 * Get a temporary file...
404 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
406 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
407 "file: %s", cupsLastErrorString()));
413 status
= cupsGetFd(http2
, resource
, cupsFileNumber(temp
));
415 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource
,
416 httpStatus(status
)));
418 if (status
== HTTP_STATUS_OK
)
421 * Got the file, read it...
424 char buffer
[8192], /* Message buffer */
426 *str
; /* Translated message */
427 _cups_message_t
*m
; /* Current message */
429 lseek(cupsFileNumber(temp
), 0, SEEK_SET
);
431 while (cups_read_strings(temp
, buffer
, sizeof(buffer
), &id
, &str
))
433 if ((m
= malloc(sizeof(_cups_message_t
))) == NULL
)
437 m
->str
= strdup(str
);
440 cupsArrayAdd(dinfo
->localizations
, m
);
455 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
456 cupsArrayCount(dinfo
->localizations
)));
471 * 'cups_read_strings()' - Read a pair of strings from a .strings file.
474 static int /* O - 1 on success, 0 on failure */
475 cups_read_strings(cups_file_t
*strings
, /* I - .strings file */
476 char *buffer
, /* I - Line buffer */
477 size_t bufsize
, /* I - Size of line buffer */
478 char **id
, /* O - Pointer to ID string */
479 char **str
) /* O - Pointer to translation string */
481 char *bufptr
; /* Pointer into buffer */
484 while (cupsFileGets(strings
, buffer
, bufsize
))
486 if (buffer
[0] != '\"')
490 bufptr
= cups_scan_strings(buffer
);
497 while (*bufptr
&& *bufptr
!= '\"')
504 bufptr
= cups_scan_strings(bufptr
);
519 * 'cups_scan_strings()' - Scan a quoted string.
522 static char * /* O - End of string */
523 cups_scan_strings(char *buffer
) /* I - Start of string */
525 char *bufptr
; /* Pointer into string */
528 for (bufptr
= buffer
+ 1; *bufptr
&& *bufptr
!= '\"'; bufptr
++)
532 if (bufptr
[1] >= '0' && bufptr
[1] <= '3' &&
533 bufptr
[2] >= '0' && bufptr
[2] <= '7' &&
534 bufptr
[3] >= '0' && bufptr
[3] <= '7')
537 * Decode \nnn octal escape...
540 *bufptr
= (char)(((((bufptr
[1] - '0') << 3) | (bufptr
[2] - '0')) << 3) | (bufptr
[3] - '0'));
541 _cups_strcpy(bufptr
+ 1, bufptr
+ 4);
546 * Decode \C escape...
549 _cups_strcpy(bufptr
, bufptr
+ 1);
552 else if (*bufptr
== 'r')
554 else if (*bufptr
== 't')