annotate mcabber/src/help.c @ 1650:cd81806b5947

Try fallback language (en) when help file with current language is not found Patch by Nixtrian, slightly modified by Mikael
author Nixtrian
date Tue, 17 Nov 2009 23:09:06 +0100
parents dcd5d4c75199
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
1 /*
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
2 * help.c -- Help command
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
3 *
1599
dcd5d4c75199 Update/Add headers
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
4 * Copyright (C) 2006-2009 Mikael Berthe <mikael@lilotux.net>
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
5 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or (at
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
9 * your option) any later version.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
10 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
14 * General Public License for more details.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
15 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
19 * USA
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
20 */
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
21
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
22 #include <stdio.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
23 #include <stdlib.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
24 #include <string.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
25 #include <ctype.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
26 #include <glib.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
27
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
28 #include "settings.h"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
29 #include "logprint.h"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
30 #include "utils.h"
892
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
31 #include "screen.h"
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
32
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
33 #define DEFAULT_LANG "en"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
34
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
35 // get_lang()
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
36 // Return the language code string (a 2-letters string).
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
37 static const char *get_lang(void) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
38 static const char *lang_str = DEFAULT_LANG;
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
39 #ifdef DATA_DIR
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
40 static char lang[3];
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
41 const char *opt_l;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
42 opt_l = settings_opt_get("lang");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
43 if (opt_l && strlen(opt_l) == 2 && isalpha(opt_l[0]) && isalpha(opt_l[1])) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
44 strncpy(lang, opt_l, sizeof(lang));
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
45 mc_strtolower(lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
46 lang_str = lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
47 }
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
48 #endif /* DATA_DIR */
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
49 return lang_str;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
50 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
51
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
52 // help_process(string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
53 // Display help about the "string" command.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
54 // If string is null, display general help.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
55 // Return 0 in case of success.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
56 int help_process(char *string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
57 {
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
58 #ifndef DATA_DIR
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
59 scr_LogPrint(LPRINT_NORMAL, "Help isn't available.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
60 return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
61 #else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
62 const char *lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
63 FILE *fp;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
64 char *helpfiles_dir, *filename;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
65 char *data;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
66 const int datasize = 4096;
892
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
67 int linecount = 0;
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
68 char *p;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
69
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
70 // Check string is ok
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
71 for (p = string; p && *p; p++) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
72 if (!isalnum(*p) && *p != '_' && *p != '-') {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
73 scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword).");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
74 return 1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
75 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
76 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
77
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
78 // Look for help file
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
79 lang = get_lang();
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
80 helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR);
1650
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
81 p = NULL;
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
82
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
83 if (string && *string) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
84 p = g_strdup(string);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
85 mc_strtolower(p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
86 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
87 } else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
88 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
89
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
90 fp = fopen(filename, "r");
1650
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
91
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
92 if (!(fp) && (g_strcmp0(lang, DEFAULT_LANG)) ) {
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
93 g_free(filename);
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
94 if (p)
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
95 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, DEFAULT_LANG, p);
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
96 else
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
97 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, DEFAULT_LANG);
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
98
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
99 fp = fopen(filename, "r");
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
100 }
cd81806b5947 Try fallback language (en) when help file with current language is not found
Nixtrian
parents: 1599
diff changeset
101 g_free(p);
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
102 g_free(filename);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
103 g_free(helpfiles_dir);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
104
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
105 if (!fp) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
106 scr_LogPrint(LPRINT_NORMAL, "No help found.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
107 return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
108 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
109
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
110 data = g_new(char, datasize);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
111 while (!feof(fp)) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
112 if (fgets(data, datasize, fp) == NULL) break;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
113 // Strip trailing newline
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
114 for (p = data; *p; p++) ;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
115 if (p > data)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
116 p--;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
117 if (*p == '\n' || *p == '\r')
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
118 *p = '\0';
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
119 // Displaty the help line
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
120 scr_LogPrint(LPRINT_NORMAL, "%s", data);
892
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
121 linecount++;
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
122 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
123 fclose(fp);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
124 g_free(data);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
125
892
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
126 if (linecount) {
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
127 scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE);
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
128 update_roster = TRUE;
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
129 }
94bb9e40e40b Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents: 882
diff changeset
130
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
131 return 0;
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
132 #endif /* DATA_DIR */
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
133 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
134
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
135 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */