Skip to content

Commit

Permalink
Implement papplClientGetLoc and use if from papplClientHTML* (Issue #58)
Browse files Browse the repository at this point in the history
Still need to implement positional parameter support and handle R-to-L
languages.
  • Loading branch information
michaelrsweet committed May 30, 2022
1 parent 7c69edc commit 507707e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
72 changes: 70 additions & 2 deletions pappl/client-loc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,74 @@ pappl_loc_t * // O - Localization data to use
papplClientGetLoc(
pappl_client_t *client) // I - Client
{
(void)client;
return (NULL);
// Range check input...
if (!client)
return (NULL);

if (!client->loc)
{
const char *language; // Language name
char temp[7], // Temporary language string
*ptr; // Pointer into temp string

// Look up language
if ((language = ippGetString(ippFindAttribute(client->request, "attributes-natural-language", IPP_TAG_LANGUAGE), 0, NULL)) != NULL)
{
// Use IPP language specification...
if ((client->loc = papplSystemFindLoc(client->system, language)) == NULL && language[2])
{
// Try the generic localization...
papplCopyString(temp, language, sizeof(temp));
temp[2] = '\0';
client->loc = papplSystemFindLoc(client->system, temp);
}
}
else if ((language = httpGetField(client->http, HTTP_FIELD_ACCEPT_LANGUAGE)) != NULL)
{
// Parse language string...
while (*language)
{
// Grab the next language code. The format (from RFC 7231) is:
//
// lang-code[;q=#][,...,land-code[;q=#]]
for (ptr = temp; *language && *language != ';' && *language != ','; language ++)
{
if (ptr < (temp + sizeof(temp) - 1))
*ptr++ = *language;
}
*ptr = '\0';

if (*language == ';')
{
// Skip "quality" parameter...
while (*language && *language != ',')
language ++;
}

if (*language == ',')
{
// Skip comma and whitespace...
language ++;
while (*language && isspace(*language & 255))
language ++;
}

// Look up the language...
if ((client->loc = papplSystemFindLoc(client->system, temp)) != NULL)
break;

// If the lookup failed and this is a regional language request, try
// the generic localization for that language...
if (temp[2])
{
// Truncate after the 2-digit language code...
temp[2] = '\0';
if ((client->loc = papplSystemFindLoc(client->system, temp)) != NULL)
break;
}
}
}
}

return (client->loc);
}
1 change: 1 addition & 0 deletions pappl/client-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct _pappl_client_s // Client data
pappl_printer_t *printer; // Printer, if any
pappl_scanner_t *scanner; // Scanner, if any
pappl_job_t *job; // Job, if any
pappl_loc_t *loc; // Localization, if any
int num_files; // Number of temporary files
char *files[10]; // Temporary files
};
Expand Down
20 changes: 14 additions & 6 deletions pappl/client-webif.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ papplClientHTMLHeader(
" <title>%s%s%s</title>\n"
" <link rel=\"shortcut icon\" href=\"/favicon.png\" type=\"image/png\">\n"
" <link rel=\"stylesheet\" href=\"/style.css\">\n"
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\">\n", title ? title : "", title ? " - " : "", name);
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\">\n", title ? papplLocGetString(papplClientGetLoc(client), title) : "", title ? " - " : "", name);
if (refresh > 0)
papplClientHTMLPrintf(client, "<meta http-equiv=\"refresh\" content=\"%d\">\n", refresh);
papplClientHTMLPuts(client,
Expand Down Expand Up @@ -1094,8 +1094,9 @@ papplClientHTMLPrintf(


// Loop through the format string, formatting as needed...
// TODO: Support positional parameters, e.g. "%2$s" to access the second string argument
va_start(ap, format);
start = format;
start = format = papplLocGetString(papplClientGetLoc(client), format);

while (*format)
{
Expand Down Expand Up @@ -1295,6 +1296,7 @@ _papplClientHTMLPutLinks(
size_t i, // Looping var
count; // Number of links
_pappl_link_t *l; // Current link
pappl_loc_t *loc; // Localization
const char *webscheme = (httpAddrLocalhost(httpGetAddress(client->http)) || !papplSystemGetTLSOnly(client->system)) ? "http" : "https";
// URL scheme for links

Expand All @@ -1303,6 +1305,7 @@ _papplClientHTMLPutLinks(
//
// Note: We use a loop and not cupsArrayGetFirst/Last because other threads may
// be enumerating the same array of links.
loc = papplClientGetLoc(client);

for (i = 0, count = cupsArrayGetCount(links); i < count; i ++)
{
Expand All @@ -1314,12 +1317,12 @@ _papplClientHTMLPutLinks(
if (strcmp(client->uri, l->path_or_url))
{
if (l->path_or_url[0] != '/' || !(l->options & PAPPL_LOPTIONS_HTTPS_REQUIRED) || (!client->system->auth_service && !client->system->auth_cb && !client->system->password_hash[0]))
papplClientHTMLPrintf(client, " <a class=\"btn\" href=\"%s\">%s</a>\n", l->path_or_url, l->label);
papplClientHTMLPrintf(client, " <a class=\"btn\" href=\"%s\">%s</a>\n", l->path_or_url, papplLocGetString(loc, l->label));
else
papplClientHTMLPrintf(client, " <a class=\"btn\" href=\"%s://%s:%d%s\">%s</a>\n", webscheme, client->host_field, client->host_port, l->path_or_url, l->label);
papplClientHTMLPrintf(client, " <a class=\"btn\" href=\"%s://%s:%d%s\">%s</a>\n", webscheme, client->host_field, client->host_port, l->path_or_url, papplLocGetString(loc, l->label));
}
else
papplClientHTMLPrintf(client, " <span class=\"active\">%s</span>\n", l->label);
papplClientHTMLPrintf(client, " <span class=\"active\">%s</span>\n", papplLocGetString(loc, l->label));
}
}

Expand All @@ -1337,7 +1340,12 @@ papplClientHTMLPuts(
const char *s) // I - String
{
if (client && s && *s)
httpWrite2(client->http, s, strlen(s));
{
s = papplLocGetString(papplClientGetLoc(client), s);

if (*s)
httpWrite2(client->http, s, strlen(s));
}
}


Expand Down
3 changes: 2 additions & 1 deletion pappl/client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Client processing code for the Printer Application Framework
//
// Copyright © 2019-2021 by Michael R Sweet.
// Copyright © 2019-2022 by Michael R Sweet.
// Copyright © 2010-2019 by Apple Inc.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
Expand Down Expand Up @@ -213,6 +213,7 @@ _papplClientProcessHTTP(
ippDelete(client->request);
ippDelete(client->response);

client->loc = NULL;
client->request = NULL;
client->response = NULL;
client->operation = HTTP_STATE_WAITING;
Expand Down

0 comments on commit 507707e

Please sign in to comment.