GnuCash  5.6-150-g038405b370+
gnc-help-utils.c
1 /*
2  * gnc-help-utils.c
3  *
4  * Copyright (C) 2007 Andreas Koehler <andi5.py@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA gnu@gnu.org
22  */
23 
24 #include <config.h>
25 #include <glib.h>
26 
27 #include <windows.h>
28 #include <htmlhelp.h>
29 
30 #include "gnc-help-utils.h"
31 
32 static GHashTable *
33 parse_hhmap_file(const gchar *chmfile)
34 {
35  gchar *mapfile = NULL, *dot;
36  GKeyFile *keyfile = NULL;
37  GError *error = NULL;
38  gchar **keys = NULL, **key;
39  gint value;
40  GHashTable *ctxtmap = NULL;
41 
42  g_return_val_if_fail(chmfile, NULL);
43 
44  mapfile = g_new(gchar, strlen(chmfile) + 7);
45  strcpy(mapfile, chmfile);
46  dot = strrchr(mapfile, '.');
47  if (dot)
48  strcpy(dot, ".hhmap");
49  else
50  strcat(mapfile, ".hhmap");
51 
52  keyfile = g_key_file_new();
53  if (!g_key_file_load_from_file(keyfile, mapfile, G_KEY_FILE_NONE, &error))
54  goto cleanup_parse;
55 
56  if (NULL == (keys = g_key_file_get_keys(keyfile, "MAP", NULL, &error)))
57  goto cleanup_parse;
58 
59  ctxtmap = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
60  for (key = keys; *key; key++)
61  {
62  value = g_key_file_get_integer(keyfile, "MAP", *key, &error);
63  if (error)
64  goto cleanup_parse;
65  else
66  g_hash_table_insert(ctxtmap, g_strdup(*key), GINT_TO_POINTER(value));
67  }
68 
69 cleanup_parse:
70  if (error)
71  {
72  g_warning("Could not load help map file: %s", error->message);
73  g_error_free(error);
74  if (ctxtmap)
75  g_hash_table_destroy(ctxtmap);
76  ctxtmap = NULL;
77  }
78  if (keys)
79  g_strfreev(keys);
80  if (keyfile)
81  g_key_file_free (keyfile);
82  if (mapfile)
83  g_free(mapfile);
84 
85  return ctxtmap;
86 }
87 
88 void
89 gnc_show_htmlhelp(const gchar *chmfile, const gchar *anchor)
90 {
91  static GHashTable *chmfile_ctxtmap_map;
92  G_LOCK_DEFINE_STATIC(chmfile_ctxtmap_map);
93  GHashTable *ctxtmap;
94  gboolean create_map = FALSE;
95  wchar_t *wpath;
96  gint id = 0;
97 
98  g_return_if_fail(chmfile);
99 
100  if (anchor)
101  {
102  G_LOCK(chmfile_ctxtmap_map);
103  if (!chmfile_ctxtmap_map)
104  {
105  chmfile_ctxtmap_map = g_hash_table_new(g_str_hash, g_str_equal);
106  create_map = TRUE;
107  }
108  else
109  {
110  create_map = !g_hash_table_lookup_extended(
111  chmfile_ctxtmap_map, chmfile, NULL, (gpointer) & ctxtmap);
112  }
113 
114  if (create_map)
115  {
116  ctxtmap = parse_hhmap_file(chmfile);
117  g_hash_table_insert(chmfile_ctxtmap_map, g_strdup(chmfile), ctxtmap);
118  }
119 
120  if (ctxtmap)
121  {
122  gpointer ptr = g_hash_table_lookup(ctxtmap, anchor);
123  if (ptr)
124  id = GPOINTER_TO_INT(ptr);
125  }
126  G_UNLOCK(chmfile_ctxtmap_map);
127 
128  if (!id)
129  g_warning("Could not find anchor '%s'", anchor);
130  }
131 
132  wpath = g_utf8_to_utf16(chmfile, -1, NULL, NULL, NULL);
133  HtmlHelpW(GetDesktopWindow(), wpath, HH_HELP_CONTEXT, id);
134  g_free(wpath);
135 }