char * get_random_hint (int force) { char *data, *result = NULL, *eol; int len; int start; static int last_sec; static struct timeval tv; GIConv conv; GString *buffer; /* Do not change hints more often than one minute */ gettimeofday (&tv, NULL); if (!force && !(tv.tv_sec > last_sec + 60)) return g_strdup (""); last_sec = tv.tv_sec; data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL); if (data == NULL) return NULL; /* get a random entry */ srand (tv.tv_sec); len = strlen (data); start = rand () % len; for (; start != 0; start--) if (data[start] == '\n') { start++; break; } eol = strchr (data + start, '\n'); if (eol != NULL) *eol = '\0'; /* hint files are stored in utf-8 */ /* try convert hint file from utf-8 to terminal encoding */ conv = str_crt_conv_from ("UTF-8"); if (conv != INVALID_CONV) { buffer = g_string_new (""); if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) result = g_strdup (buffer->str); g_string_free (buffer, TRUE); str_close_conv (conv); } g_free (data); return result; }
/* * Return a random hint. If force is not 0, ignore the timeout. */ char * get_random_hint (int force) { char *data, *result, *eol; int len; int start; static int last_sec; static struct timeval tv; /* Do not change hints more often than one minute */ gettimeofday (&tv, NULL); if (!force && !(tv.tv_sec > last_sec + 60)) return g_strdup (""); last_sec = tv.tv_sec; data = load_mc_home_file (MC_HINT, NULL); if (!data) return 0; /* get a random entry */ srand (tv.tv_sec); len = strlen (data); start = rand () % len; for (; start; start--) { if (data[start] == '\n') { start++; break; } } eol = strchr (&data[start], '\n'); if (eol) *eol = 0; result = g_strdup (&data[start]); g_free (data); return result; }
/* event callback */ gboolean help_interactive_display (const gchar * event_group_name, const gchar * event_name, gpointer init_data, gpointer data) { const dlg_colors_t help_colors = { HELP_NORMAL_COLOR, /* common text color */ 0, /* unused in help */ HELP_BOLD_COLOR, /* bold text color */ 0, /* unused in help */ HELP_TITLE_COLOR /* title color */ }; WButtonBar *help_bar; Widget *md; char *hlpfile = NULL; char *filedata; ev_help_t *event_data = (ev_help_t *) data; (void) event_group_name; (void) event_name; (void) init_data; if (event_data->filename != NULL) g_file_get_contents (event_data->filename, &filedata, NULL, NULL); else filedata = load_mc_home_file (mc_global.share_data_dir, MC_HELP, &hlpfile); if (filedata == NULL) message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), event_data->filename ? event_data->filename : hlpfile, unix_error_string (errno)); g_free (hlpfile); if (filedata == NULL) return TRUE; translate_file (filedata); g_free (filedata); if (fdata == NULL) return TRUE; if ((event_data->node == NULL) || (*event_data->node == '\0')) event_data->node = "[main]"; main_node = search_string (fdata, event_data->node); if (main_node == NULL) { message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), event_data->node); /* Fallback to [main], return if it also cannot be found */ main_node = search_string (fdata, "[main]"); if (main_node == NULL) { interactive_display_finish (); return TRUE; } } help_lines = min (LINES - 4, max (2 * LINES / 3, 18)); whelp = dlg_create (TRUE, 0, 0, help_lines + 4, HELP_WINDOW_WIDTH + 4, help_colors, help_callback, NULL, "[Help]", _("Help"), DLG_TRYUP | DLG_CENTER | DLG_WANT_TAB); selected_item = search_string_node (main_node, STRING_LINK_START) - 1; currentpoint = main_node + 1; /* Skip the newline following the start of the node */ for (history_ptr = HISTORY_SIZE; history_ptr;) { history_ptr--; history[history_ptr].page = currentpoint; history[history_ptr].link = selected_item; } help_bar = buttonbar_new (TRUE); WIDGET (help_bar)->y -= WIDGET (whelp)->y; WIDGET (help_bar)->x -= WIDGET (whelp)->x; md = mousedispatch_new (1, 1, help_lines, HELP_WINDOW_WIDTH - 2); add_widget (whelp, md); add_widget (whelp, help_bar); buttonbar_set_label (help_bar, 1, Q_ ("ButtonBar|Help"), help_map, NULL); buttonbar_set_label (help_bar, 2, Q_ ("ButtonBar|Index"), help_map, NULL); buttonbar_set_label (help_bar, 3, Q_ ("ButtonBar|Prev"), help_map, NULL); buttonbar_set_label (help_bar, 4, "", help_map, NULL); buttonbar_set_label (help_bar, 5, "", help_map, NULL); buttonbar_set_label (help_bar, 6, "", help_map, NULL); buttonbar_set_label (help_bar, 7, "", help_map, NULL); buttonbar_set_label (help_bar, 8, "", help_map, NULL); buttonbar_set_label (help_bar, 9, "", help_map, NULL); buttonbar_set_label (help_bar, 10, Q_ ("ButtonBar|Quit"), help_map, NULL); dlg_run (whelp); interactive_display_finish (); dlg_destroy (whelp); return TRUE; }
char * get_random_hint (int force) { char *data, *result = NULL, *eop; int len; int start; static int last_sec; static struct timeval tv; GIConv conv; /* Do not change hints more often than one minute */ gettimeofday (&tv, NULL); if (!force && !(tv.tv_sec > last_sec + 60)) return g_strdup (""); last_sec = tv.tv_sec; data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL); if (data == NULL) return NULL; /* get a random entry */ srand (tv.tv_sec); len = strlen (data); start = rand () % (len - 1); /* Search the start of paragraph */ for (; start != 0; start--) if (data[start] == '\n' && data[start + 1] == '\n') { start += 2; break; } /* Search the end of paragraph */ for (eop = data + start; *eop != '\0'; eop++) { if (*eop == '\n' && *(eop + 1) == '\n') { *eop = '\0'; break; } if (*eop == '\n') *eop = ' '; } /* hint files are stored in utf-8 */ /* try convert hint file from utf-8 to terminal encoding */ conv = str_crt_conv_from ("UTF-8"); if (conv != INVALID_CONV) { GString *buffer; buffer = g_string_new (""); if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) result = g_string_free (buffer, FALSE); else g_string_free (buffer, TRUE); str_close_conv (conv); } else result = g_strdup (&data[start]); g_free (data); return result; }