dest-localization.c [plain text]
#include "cups-private.h"
#include "debug-internal.h"
static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
const char *
cupsLocalizeDestMedia(
http_t *http,
cups_dest_t *dest,
cups_dinfo_t *dinfo,
unsigned flags,
cups_size_t *size)
{
cups_lang_t *lang;
_cups_message_t key,
*match;
pwg_media_t *pwg;
cups_array_t *db;
_cups_media_db_t *mdb;
char lstr[1024],
temp[256];
const char *lsize,
*lsource,
*ltype;
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)"));
if (!http || !dest || !dinfo || !size)
{
DEBUG_puts("1cupsLocalizeDestMedia: Returning NULL.");
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
return (NULL);
}
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;
}
}
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
{
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)
{
snprintf(temp, sizeof(temp), "media.%s", pwg->pwg);
if ((lsize = _cupsLangString(lang, temp)) == temp)
lsize = NULL;
}
if (!lsize)
{
if ((size->width % 635) == 0 && (size->length % 635) == 0)
{
snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g \"")), size->width / 2540.0, size->length / 2540.0);
}
else
{
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);
}
const char *
cupsLocalizeDestOption(
http_t *http,
cups_dest_t *dest,
cups_dinfo_t *dinfo,
const char *option)
{
_cups_message_t key,
*match;
const char *localized;
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);
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);
}
const char *
cupsLocalizeDestValue(
http_t *http,
cups_dest_t *dest,
cups_dinfo_t *dinfo,
const char *option,
const char *value)
{
_cups_message_t key,
*match;
char pair[256];
const char *localized;
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);
snprintf(pair, sizeof(pair), "%s.%s", option, value);
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);
}
static void
cups_create_localizations(
http_t *http,
cups_dinfo_t *dinfo)
{
http_t *http2;
http_status_t status;
ipp_attribute_t *attr;
char scheme[32],
userpass[256],
hostname[256],
resource[1024],
http_hostname[256],
tempfile[1024];
int port;
http_encryption_t encryption;
cups_file_t *temp;
if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
IPP_TAG_URI)) == NULL)
{
dinfo->localizations = _cupsMessageNew(NULL);
DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) value.");
return;
}
if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
scheme, sizeof(scheme), userpass, sizeof(userpass),
hostname, sizeof(hostname), &port, resource,
sizeof(resource)) < HTTP_URI_STATUS_OK)
{
dinfo->localizations = _cupsMessageNew(NULL);
DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value \"%s\".", attr->values[0].string.text));
return;
}
httpGetHostname(http, http_hostname, sizeof(http_hostname));
if (!_cups_strcasecmp(http_hostname, hostname) &&
port == httpAddrPort(http->hostaddr))
{
http2 = http;
}
else
{
if (!strcmp(scheme, "https"))
encryption = HTTP_ENCRYPTION_ALWAYS;
else
encryption = HTTP_ENCRYPTION_IF_REQUESTED;
if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
30000, NULL)) == NULL)
{
DEBUG_printf(("4cups_create_localizations: Unable to connect to "
"%s:%d: %s", hostname, port, cupsLastErrorString()));
return;
}
}
if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
{
DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
"file: %s", cupsLastErrorString()));
if (http2 != http)
httpClose(http2);
return;
}
status = cupsGetFd(http2, resource, cupsFileNumber(temp));
cupsFileClose(temp);
DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, httpStatus(status)));
if (status == HTTP_STATUS_OK)
{
dinfo->localizations = _cupsMessageLoad(tempfile, _CUPS_MESSAGE_STRINGS);
}
DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
cupsArrayCount(dinfo->localizations)));
unlink(tempfile);
if (http2 != http)
httpClose(http2);
}