1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-07-17 15:14:35 -04:00
elinks/src/mime/dialogs.c

192 lines
4.4 KiB
C
Raw Normal View History

/* Internal MIME types implementation dialogs */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "elinks.h"
#include "bfu/dialog.h"
#include "bfu/menu.h"
#include "config/options.h"
#include "mime/dialogs.h"
#include "intl/libintl.h"
#include "terminal/terminal.h"
#include "util/color.h"
#include "util/conv.h"
#include "util/memory.h"
#include "util/string.h"
static struct option *
2022-02-17 13:54:37 -05:00
get_real_opt(const char *base, char *id)
{
struct option *opt = get_opt_rec_real(config_options, base);
struct string translated;
assert(opt);
if (init_string(&translated)
&& add_optname_to_string(&translated, id, strlen(id)))
opt = get_opt_rec_real(opt, translated.source);
done_string(&translated);
return opt;
}
static void
really_del_ext(void *fcp)
{
struct option *opt = get_real_opt("mime.extension",
(char *) fcp);
if (opt) delete_option(opt);
}
void
menu_del_ext(struct terminal *term, void *fcp, void *xxx2)
{
Here is a framework that detects cases where a PO file assigns the same accelerator key to multiple buttons in a dialog box or to multiple items in a menu. ELinks already has some support for this but it requires the translator to run ELinks and manually scan through all menus and dialogs. The attached changes make it possible to quickly detect and list any conflicts, including ones that can only occur on operating systems or configurations that the translator is not currently using. The changes have no immediate effect on the elinks executable or the MO files. PO files become larger, however. The scheme works like this: - Like before, accelerator keys in translatable strings are tagged with the tilde (~) character. - Whenever a C source file defines an accelerator key, it must assign one or more named "contexts" to it. The translations in the PO files inherit these contexts. If multiple strings use the same accelerator (case insensitive) in the same context, that's a conflict and can be detected automatically. - The contexts are defined with "gettext_accelerator_context" comments in source files. These comments delimit regions where all translatable strings containing tildes are given the same contexts. There must be one special comment at the top of the region; it lists the contexts assigned to that region. The region automatically ends at the end of the function (found with regexp /^\}/), but it can also be closed explicitly with another special comment. The comments are formatted like this: /* [gettext_accelerator_context(foo, bar, baz)] begins a region that uses the contexts "foo", "bar", and "baz". The comma is the delimiter; whitespace is optional. [gettext_accelerator_context()] ends the region. */ The scripts don't currently check whether this syntax occurs inside or outside comments. - The names of contexts consist of C identifiers delimited with periods. I typically used the name of a function that sets up a dialog, or the name of an array where the items of a menu are listed. There is a special feature for static functions: if the name begins with a period, then the period will be replaced with the name of the source file and a colon. - If a menu is programmatically generated from multiple parts, of which some are never used together, so that it is safe to use the same accelerators in them, then it is necessary to define multiple contexts for the same menu. link_menu() in src/viewer/text/link.c is the most complex example of this. - During make update-po: - A Perl script (po/gather-accelerator-contexts.pl) reads po/elinks.pot, scans the source files listed in it for "gettext_accelerator_context" comments, and rewrites po/elinks.pot with "accelerator_context" comments that indicate the contexts of each msgid: the union of all contexts of all of its uses in the source files. It also removes any "gettext_accelerator_context" comments that xgettext --add-comments has copied to elinks.pot. - If po/gather-accelerator-contexts.pl does not find any contexts for some use of an msgid that seems to contain an accelerator (because it contains a tilde), it warns. If the tilde refers to e.g. "~/.elinks" and does not actually mark an accelerator, the warning can be silenced by specifying the special context "IGNORE", which the script otherwise ignores. - msgmerge copies the "accelerator_context" comments from po/elinks.pot to po/*.po. Translators do not edit those comments. - During make check-po: - Another Perl script (po/check-accelerator-contexts.pl) reads po/*.po and keeps track of which accelerators have been bound in each context. It warns about any conflicts it finds. This script does not access the C source files; thus it does not matter if the line numbers in "#:" lines are out of date. This implementation is not perfect and I am not proposing to add it to the main source tree at this time. Specifically: - It introduces compile-time dependencies on Perl and Locale::PO. There should be a configure-time or compile-time check so that the new features are skipped if the prerequisites are missing. - When the scripts include msgstr strings in warnings, they should transcode them from the charset of the PO file to the one specified by the user's locale. - It is not adequately documented (well, except perhaps here). - po/check-accelerator-contexts.pl reports the same conflict multiple times if it occurs in multiple contexts. - The warning messages should include line numbers, so that users of Emacs could conveniently edit the conflicting part of the PO file. This is not feasible with the current version of Locale::PO. - Locale::PO does not understand #~ lines and spews warnings about them. There is an ugly hack to hide these warnings. - Jonas Fonseca suggested the script could propose accelerators that are still available. This has not been implemented. There are three files attached: - po/gather-accelerator-contexts.pl: Augments elinks.pot with context information. - po/check-accelerator-contexts.pl: Checks conflicts. - accelerator-contexts.diff: Makes po/Makefile run the scripts, and adds special comments to source files.
2005-12-04 18:38:29 -05:00
/* [gettext_accelerator_context(menu_del_ext)] */
struct option *opt = NULL;
2022-01-25 12:25:58 -05:00
char *extension = (char *)fcp;
if (!extension) return;
opt = get_real_opt("mime.extension", extension);
if (!opt) {
mem_free(extension);
return;
}
msg_box(term, getml(extension, (void *) NULL), MSGBOX_FREE_TEXT,
N_("Delete extension"), ALIGN_CENTER,
msg_text(term, N_("Delete extension %s -> %s?"),
extension, opt->value.string),
extension, 2,
MSG_BOX_BUTTON(N_("~Yes"), really_del_ext, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), NULL, B_ESC));
}
struct extension {
char ext_orig[MAX_STR_LEN];
char ext[MAX_STR_LEN];
char ct[MAX_STR_LEN];
};
static void
add_mime_extension(void *data)
{
2022-01-25 12:25:58 -05:00
struct extension *ext = (struct extension *)data;
struct string name;
if (!ext || !init_string(&name)) return;
add_to_string(&name, "mime.extension.");
add_optname_to_string(&name, ext->ext, strlen(ext->ext));
really_del_ext(ext->ext_orig); /* ..or rename ;) */
safe_strncpy(get_opt_str(name.source, NULL), ext->ct, MAX_STR_LEN);
option_changed(NULL, get_opt_rec(config_options, name.source));
done_string(&name);
}
void
menu_add_ext(struct terminal *term, void *fcp, void *xxx2)
{
Here is a framework that detects cases where a PO file assigns the same accelerator key to multiple buttons in a dialog box or to multiple items in a menu. ELinks already has some support for this but it requires the translator to run ELinks and manually scan through all menus and dialogs. The attached changes make it possible to quickly detect and list any conflicts, including ones that can only occur on operating systems or configurations that the translator is not currently using. The changes have no immediate effect on the elinks executable or the MO files. PO files become larger, however. The scheme works like this: - Like before, accelerator keys in translatable strings are tagged with the tilde (~) character. - Whenever a C source file defines an accelerator key, it must assign one or more named "contexts" to it. The translations in the PO files inherit these contexts. If multiple strings use the same accelerator (case insensitive) in the same context, that's a conflict and can be detected automatically. - The contexts are defined with "gettext_accelerator_context" comments in source files. These comments delimit regions where all translatable strings containing tildes are given the same contexts. There must be one special comment at the top of the region; it lists the contexts assigned to that region. The region automatically ends at the end of the function (found with regexp /^\}/), but it can also be closed explicitly with another special comment. The comments are formatted like this: /* [gettext_accelerator_context(foo, bar, baz)] begins a region that uses the contexts "foo", "bar", and "baz". The comma is the delimiter; whitespace is optional. [gettext_accelerator_context()] ends the region. */ The scripts don't currently check whether this syntax occurs inside or outside comments. - The names of contexts consist of C identifiers delimited with periods. I typically used the name of a function that sets up a dialog, or the name of an array where the items of a menu are listed. There is a special feature for static functions: if the name begins with a period, then the period will be replaced with the name of the source file and a colon. - If a menu is programmatically generated from multiple parts, of which some are never used together, so that it is safe to use the same accelerators in them, then it is necessary to define multiple contexts for the same menu. link_menu() in src/viewer/text/link.c is the most complex example of this. - During make update-po: - A Perl script (po/gather-accelerator-contexts.pl) reads po/elinks.pot, scans the source files listed in it for "gettext_accelerator_context" comments, and rewrites po/elinks.pot with "accelerator_context" comments that indicate the contexts of each msgid: the union of all contexts of all of its uses in the source files. It also removes any "gettext_accelerator_context" comments that xgettext --add-comments has copied to elinks.pot. - If po/gather-accelerator-contexts.pl does not find any contexts for some use of an msgid that seems to contain an accelerator (because it contains a tilde), it warns. If the tilde refers to e.g. "~/.elinks" and does not actually mark an accelerator, the warning can be silenced by specifying the special context "IGNORE", which the script otherwise ignores. - msgmerge copies the "accelerator_context" comments from po/elinks.pot to po/*.po. Translators do not edit those comments. - During make check-po: - Another Perl script (po/check-accelerator-contexts.pl) reads po/*.po and keeps track of which accelerators have been bound in each context. It warns about any conflicts it finds. This script does not access the C source files; thus it does not matter if the line numbers in "#:" lines are out of date. This implementation is not perfect and I am not proposing to add it to the main source tree at this time. Specifically: - It introduces compile-time dependencies on Perl and Locale::PO. There should be a configure-time or compile-time check so that the new features are skipped if the prerequisites are missing. - When the scripts include msgstr strings in warnings, they should transcode them from the charset of the PO file to the one specified by the user's locale. - It is not adequately documented (well, except perhaps here). - po/check-accelerator-contexts.pl reports the same conflict multiple times if it occurs in multiple contexts. - The warning messages should include line numbers, so that users of Emacs could conveniently edit the conflicting part of the PO file. This is not feasible with the current version of Locale::PO. - Locale::PO does not understand #~ lines and spews warnings about them. There is an ugly hack to hide these warnings. - Jonas Fonseca suggested the script could propose accelerators that are still available. This has not been implemented. There are three files attached: - po/gather-accelerator-contexts.pl: Augments elinks.pot with context information. - po/check-accelerator-contexts.pl: Checks conflicts. - accelerator-contexts.diff: Makes po/Makefile run the scripts, and adds special comments to source files.
2005-12-04 18:38:29 -05:00
/* [gettext_accelerator_context(menu_add_ext)] */
2016-04-20 12:42:22 -04:00
struct extension *new_;
struct dialog *dlg;
#define MIME_WIDGETS_COUNT 4
2016-04-20 12:42:22 -04:00
dlg = calloc_dialog(MIME_WIDGETS_COUNT, sizeof(*new_));
if (!dlg) {
mem_free_if(fcp);
return;
}
2016-04-20 12:42:22 -04:00
new_ = (struct extension *) get_dialog_offset(dlg, MIME_WIDGETS_COUNT);
if (fcp) {
2022-01-25 12:25:58 -05:00
struct option *opt = get_real_opt("mime.extension", (char *)fcp);
if (opt) {
2022-01-25 12:25:58 -05:00
safe_strncpy(new_->ext, (const char *)fcp, MAX_STR_LEN);
2016-04-20 12:42:22 -04:00
safe_strncpy(new_->ct, opt->value.string, MAX_STR_LEN);
2022-01-25 12:25:58 -05:00
safe_strncpy(new_->ext_orig, (const char *)fcp, MAX_STR_LEN);
}
mem_free(fcp);
}
dlg->title = _("Extension", term);
dlg->layouter = generic_dialog_layouter;
2016-04-20 12:42:22 -04:00
add_dlg_field(dlg, _("Extension(s)", term), 0, 0, check_nonempty, MAX_STR_LEN, new_->ext, NULL);
add_dlg_field(dlg, _("Content-Type", term), 0, 0, check_nonempty, MAX_STR_LEN, new_->ct, NULL);
2016-04-20 12:42:22 -04:00
add_dlg_ok_button(dlg, _("~OK", term), B_ENTER, add_mime_extension, new_);
add_dlg_button(dlg, _("~Cancel", term), B_ESC, cancel_dialog, NULL);
add_dlg_end(dlg, MIME_WIDGETS_COUNT);
do_dialog(term, dlg, getml(dlg, (void *) NULL));
}
static struct menu_item mi_no_ext[] = {
INIT_MENU_ITEM(N_("No extensions"), NULL, ACT_MAIN_NONE, NULL, NULL, NO_SELECT),
NULL_MENU_ITEM
};
void
menu_list_ext(struct terminal *term, void *fn_, void *xxx)
{
2022-01-25 12:25:58 -05:00
menu_func_T fn = (menu_func_T)fn_;
LIST_OF(struct option) *opt_tree = get_opt_tree("mime.extension", NULL);
struct option *opt;
struct menu_item *mi = NULL;
foreachback (opt, *opt_tree) {
struct string translated;
char *translated2;
char *optptr2;
if (!strcmp(opt->name, "_template_")) continue;
if (!init_string(&translated)
|| !add_real_optname_to_string(&translated, opt->aname,
strlen(opt->aname))) {
done_string(&translated);
continue;
}
if (!mi) {
mi = new_menu(FREE_ANY | NO_INTL);
if (!mi) {
done_string(&translated);
return;
}
}
translated2 = memacpy(translated.source, translated.length);
optptr2 = stracpy(opt->value.string);
if (translated2 && optptr2) {
add_to_menu(&mi, translated.source, optptr2, ACT_MAIN_NONE,
fn, translated2, 0);
} else {
mem_free_if(optptr2);
mem_free_if(translated2);
done_string(&translated);
}
}
if (!mi) mi = mi_no_ext;
do_menu(term, mi, NULL, 0);
}