]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/dest-localization.c
Last of the media improvements (Issue #5167)
[thirdparty/cups.git] / cups / dest-localization.c
CommitLineData
dcb445bc 1/*
7e86f2f6 2 * Destination localization support for CUPS.
dcb445bc 3 *
89550f3f 4 * Copyright 2012-2017 by Apple Inc.
dcb445bc 5 *
e3101897 6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
dcb445bc
MS
7 */
8
9/*
10 * Include necessary headers...
11 */
12
13#include "cups-private.h"
14
15
a29fd7dd
MS
16/*
17 * Local functions...
18 */
19
20static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
a29fd7dd
MS
21
22
104fd4ae
MS
23/*
24 * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media
25 * size.
26 *
27 * The returned string is stored in the destination information and will become
28 * invalid if the destination information is deleted.
29 *
8072030b 30 * @since CUPS 2.0/macOS 10.10@
104fd4ae
MS
31 */
32
33const char * /* O - Localized string */
34cupsLocalizeDestMedia(
35 http_t *http, /* I - Connection to destination */
36 cups_dest_t *dest, /* I - Destination */
37 cups_dinfo_t *dinfo, /* I - Destination information */
38 unsigned flags, /* I - Media flags */
39 cups_size_t *size) /* I - Media size */
40{
41 cups_lang_t *lang; /* Standard localizations */
42 _cups_message_t key, /* Search key */
43 *match; /* Matching entry */
44 pwg_media_t *pwg; /* PWG media information */
45 cups_array_t *db; /* Media database */
46 _cups_media_db_t *mdb; /* Media database entry */
71dc43af 47 char lstr[1024], /* Localized size name */
104fd4ae
MS
48 temp[256]; /* Temporary string */
49 const char *lsize, /* Localized media size */
50 *lsource, /* Localized media source */
51 *ltype; /* Localized media type */
52
53
a9357c9d
MS
54 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)"));
55
104fd4ae
MS
56 /*
57 * Range check input...
58 */
59
60 if (!http || !dest || !dinfo || !size)
61 {
a9357c9d 62 DEBUG_puts("1cupsLocalizeDestMedia: Returning NULL.");
104fd4ae
MS
63 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
64 return (NULL);
65 }
66
67 /*
71dc43af 68 * Find the matching media database entry...
104fd4ae
MS
69 */
70
71dc43af
MS
71 if (flags & CUPS_MEDIA_FLAGS_READY)
72 db = dinfo->ready_db;
73 else
74 db = dinfo->media_db;
104fd4ae 75
71dc43af
MS
76 DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size->media));
77
78 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
79 {
80 if (mdb->key && !strcmp(mdb->key, size->media))
81 break;
82 else if (mdb->size_name && !strcmp(mdb->size_name, size->media))
83 break;
84 }
85
86 if (!mdb)
a9357c9d 87 {
71dc43af
MS
88 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
89 {
90 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)
91 break;
92 }
a9357c9d 93 }
104fd4ae
MS
94
95 /*
71dc43af 96 * See if the localization is cached...
104fd4ae
MS
97 */
98
99 lang = cupsLangDefault();
a9357c9d 100
71dc43af
MS
101 if (!dinfo->localizations)
102 cups_create_localizations(http, dinfo);
103
a9357c9d 104 snprintf(temp, sizeof(temp), "media.%s", size->media);
71dc43af
MS
105 key.msg = temp;
106
107 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
a9357c9d 108 {
71dc43af 109 lsize = match->str;
a9357c9d 110 }
71dc43af
MS
111 else
112 {
113 /*
114 * Not a media name, try a media-key name...
115 */
a9357c9d 116
71dc43af
MS
117 snprintf(temp, sizeof(temp), "media-key.%s", size->media);
118 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
119 lsize = match->str;
120 else
121 lsize = NULL;
122 }
104fd4ae 123
71dc43af
MS
124 if (!lsize && (pwg = pwgMediaForSize(size->width, size->length)) != NULL && pwg->ppd)
125 {
126 /*
127 * Get a standard localization...
128 */
129
130 snprintf(temp, sizeof(temp), "media.%s", pwg->pwg);
131 if ((lsize = _cupsLangString(lang, temp)) == temp)
132 lsize = NULL;
133 }
104fd4ae
MS
134
135 if (!lsize)
136 {
71dc43af
MS
137 /*
138 * Make a dimensional localization...
139 */
140
104fd4ae
MS
141 if ((size->width % 635) == 0 && (size->length % 635) == 0)
142 {
143 /*
144 * Use inches since the size is a multiple of 1/4 inch.
145 */
146
a9357c9d 147 snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g \"")), size->width / 2540.0, size->length / 2540.0);
104fd4ae
MS
148 }
149 else
150 {
151 /*
152 * Use millimeters since the size is not a multiple of 1/4 inch.
153 */
154
155 snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%d x %d mm")), (size->width + 50) / 100, (size->length + 50) / 100);
156 }
157
158 lsize = temp;
159 }
160
104fd4ae
MS
161 if (mdb)
162 {
807315e6 163 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));
85cbdbc7 164
84de5e92
MS
165 if ((lsource = cupsLocalizeDestValue(http, dest, dinfo, "media-source", mdb->source)) == mdb->source && mdb->source)
166 lsource = _cupsLangString(lang, _("Other Tray"));
167 if ((ltype = cupsLocalizeDestValue(http, dest, dinfo, "media-type", mdb->type)) == mdb->type && mdb->type)
168 ltype = _cupsLangString(lang, _("Other Media"));
104fd4ae
MS
169 }
170 else
171 {
172 lsource = NULL;
173 ltype = NULL;
174 }
175
176 if (!lsource && !ltype)
177 {
982be458 178 if (!size->bottom && !size->left && !size->right && !size->top)
71dc43af 179 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless)")), lsize);
104fd4ae 180 else
71dc43af 181 strlcpy(lstr, lsize, sizeof(lstr));
104fd4ae
MS
182 }
183 else if (!lsource)
184 {
982be458 185 if (!size->bottom && !size->left && !size->right && !size->top)
71dc43af 186 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, ltype);
104fd4ae 187 else
71dc43af 188 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, ltype);
104fd4ae
MS
189 }
190 else if (!ltype)
191 {
982be458 192 if (!size->bottom && !size->left && !size->right && !size->top)
71dc43af 193 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, lsource);
104fd4ae 194 else
71dc43af 195 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, lsource);
104fd4ae
MS
196 }
197 else
198 {
982be458 199 if (!size->bottom && !size->left && !size->right && !size->top)
71dc43af 200 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s, %s)")), lsize, ltype, lsource);
104fd4ae 201 else
71dc43af 202 snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s, %s)")), lsize, ltype, lsource);
104fd4ae
MS
203 }
204
205 if ((match = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL)
206 return (NULL);
207
89550f3f 208 match->msg = strdup(size->media);
71dc43af 209 match->str = strdup(lstr);
104fd4ae
MS
210
211 cupsArrayAdd(dinfo->localizations, match);
212
a9357c9d
MS
213 DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match->str));
214
104fd4ae
MS
215 return (match->str);
216}
217
218
dcb445bc
MS
219/*
220 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
221 * option.
222 *
a29fd7dd
MS
223 * The returned string is stored in the destination information and will become
224 * invalid if the destination information is deleted.
dcb445bc 225 *
8072030b 226 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
227 */
228
229const char * /* O - Localized string */
230cupsLocalizeDestOption(
231 http_t *http, /* I - Connection to destination */
232 cups_dest_t *dest, /* I - Destination */
233 cups_dinfo_t *dinfo, /* I - Destination information */
234 const char *option) /* I - Option to localize */
235{
a29fd7dd
MS
236 _cups_message_t key, /* Search key */
237 *match; /* Matching entry */
a9357c9d
MS
238 const char *localized; /* Localized string */
239
a29fd7dd 240
a9357c9d 241 DEBUG_printf(("cupsLocalizeDestOption(http=%p, dest=%p, dinfo=%p, option=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option));
a29fd7dd
MS
242
243 if (!http || !dest || !dinfo)
244 return (option);
245
246 if (!dinfo->localizations)
247 cups_create_localizations(http, dinfo);
248
89550f3f
MS
249 key.msg = (char *)option;
250 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
a29fd7dd 251 return (match->str);
a9357c9d
MS
252 else if ((localized = _cupsLangString(cupsLangDefault(), option)) != NULL)
253 return (localized);
a29fd7dd
MS
254 else
255 return (option);
dcb445bc
MS
256}
257
258
259/*
260 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
261 * option+value pair.
262 *
a29fd7dd
MS
263 * The returned string is stored in the destination information and will become
264 * invalid if the destination information is deleted.
dcb445bc 265 *
8072030b 266 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
267 */
268
269const char * /* O - Localized string */
270cupsLocalizeDestValue(
271 http_t *http, /* I - Connection to destination */
272 cups_dest_t *dest, /* I - Destination */
273 cups_dinfo_t *dinfo, /* I - Destination information */
274 const char *option, /* I - Option to localize */
275 const char *value) /* I - Value to localize */
276{
a29fd7dd
MS
277 _cups_message_t key, /* Search key */
278 *match; /* Matching entry */
279 char pair[256]; /* option.value pair */
a9357c9d 280 const char *localized; /* Localized string */
a29fd7dd
MS
281
282
a9357c9d
MS
283 DEBUG_printf(("cupsLocalizeDestValue(http=%p, dest=%p, dinfo=%p, option=\"%s\", value=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option, value));
284
a29fd7dd
MS
285 if (!http || !dest || !dinfo)
286 return (value);
287
a9357c9d
MS
288 if (!strcmp(option, "media"))
289 {
290 pwg_media_t *media = pwgMediaForPWG(value);
291 cups_size_t size;
292
293 strlcpy(size.media, value, sizeof(size.media));
294 size.width = media ? media->width : 0;
295 size.length = media ? media->length : 0;
296 size.left = 0;
297 size.right = 0;
298 size.bottom = 0;
299 size.top = 0;
300
301 return (cupsLocalizeDestMedia(http, dest, dinfo, CUPS_MEDIA_FLAGS_DEFAULT, &size));
302 }
303
a29fd7dd
MS
304 if (!dinfo->localizations)
305 cups_create_localizations(http, dinfo);
306
a29fd7dd 307 snprintf(pair, sizeof(pair), "%s.%s", option, value);
89550f3f
MS
308 key.msg = pair;
309 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
a29fd7dd 310 return (match->str);
a9357c9d
MS
311 else if ((localized = _cupsLangString(cupsLangDefault(), pair)) != NULL && strcmp(localized, pair))
312 return (localized);
a29fd7dd
MS
313 else
314 return (value);
315}
316
317
318/*
319 * 'cups_create_localizations()' - Create the localizations array for a
320 * destination.
321 */
322
323static void
324cups_create_localizations(
325 http_t *http, /* I - Connection to destination */
326 cups_dinfo_t *dinfo) /* I - Destination informations */
327{
328 http_t *http2; /* Connection for strings file */
329 http_status_t status; /* Request status */
330 ipp_attribute_t *attr; /* "printer-strings-uri" attribute */
331 char scheme[32], /* URI scheme */
332 userpass[256], /* Username/password info */
333 hostname[256], /* Hostname */
334 resource[1024], /* Resource */
335 http_hostname[256],
336 /* Hostname of connection */
337 tempfile[1024]; /* Temporary filename */
338 int port; /* Port number */
339 http_encryption_t encryption; /* Encryption to use */
340 cups_file_t *temp; /* Temporary file */
341
342
a29fd7dd
MS
343 /*
344 * See if there are any localizations...
345 */
346
347 if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
348 IPP_TAG_URI)) == NULL)
349 {
350 /*
89550f3f 351 * Nope, create an empty message catalog...
a29fd7dd
MS
352 */
353
89550f3f
MS
354 dinfo->localizations = _cupsMessageNew(NULL);
355 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) value.");
356 return;
a29fd7dd
MS
357 }
358
359 /*
360 * Pull apart the URI and determine whether we need to try a different
361 * server...
362 */
363
364 if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
365 scheme, sizeof(scheme), userpass, sizeof(userpass),
366 hostname, sizeof(hostname), &port, resource,
cb7f98ee 367 sizeof(resource)) < HTTP_URI_STATUS_OK)
a29fd7dd 368 {
89550f3f
MS
369 dinfo->localizations = _cupsMessageNew(NULL);
370 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value \"%s\".", attr->values[0].string.text));
a29fd7dd
MS
371 return;
372 }
373
374 httpGetHostname(http, http_hostname, sizeof(http_hostname));
375
376 if (!_cups_strcasecmp(http_hostname, hostname) &&
a469f8a5 377 port == httpAddrPort(http->hostaddr))
a29fd7dd
MS
378 {
379 /*
380 * Use the same connection...
381 */
382
383 http2 = http;
384 }
385 else
386 {
387 /*
388 * Connect to the alternate host...
389 */
390
391 if (!strcmp(scheme, "https"))
cb7f98ee 392 encryption = HTTP_ENCRYPTION_ALWAYS;
a29fd7dd 393 else
cb7f98ee 394 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
a29fd7dd 395
cb7f98ee
MS
396 if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
397 30000, NULL)) == NULL)
a29fd7dd
MS
398 {
399 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
400 "%s:%d: %s", hostname, port, cupsLastErrorString()));
401 return;
402 }
403 }
404
405 /*
406 * Get a temporary file...
407 */
408
409 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
410 {
411 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
412 "file: %s", cupsLastErrorString()));
413 if (http2 != http)
414 httpClose(http2);
415 return;
416 }
417
418 status = cupsGetFd(http2, resource, cupsFileNumber(temp));
89550f3f 419 cupsFileClose(temp);
a29fd7dd 420
89550f3f 421 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, httpStatus(status)));
a29fd7dd 422
cb7f98ee 423 if (status == HTTP_STATUS_OK)
a29fd7dd
MS
424 {
425 /*
426 * Got the file, read it...
427 */
428
89550f3f 429 dinfo->localizations = _cupsMessageLoad(tempfile, _CUPS_MESSAGE_STRINGS);
a29fd7dd
MS
430 }
431
432 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
433 cupsArrayCount(dinfo->localizations)));
434
435 /*
436 * Cleanup...
437 */
438
439 unlink(tempfile);
a29fd7dd
MS
440
441 if (http2 != http)
442 httpClose(http2);
443}
444