GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-help-utils.c
1 /*
2  * gnc-help-utils.c
3  *
4  * Copyright (C) 2007 Andreas Koehler <[email protected]>
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 [email protected]
22  */
23 
24 #include "config.h"
25 #include <glib.h>
26 
27 #ifdef HAVE_HTMLHELPW
28 # include <windows.h>
29 # include <htmlhelp.h>
30 #endif
31 
32 #include "gnc-help-utils.h"
33 
34 #ifdef HAVE_HTMLHELPW
35 
36 static GHashTable *
37 parse_hhmap_file(const gchar *chmfile)
38 {
39  gchar *mapfile = NULL, *dot;
40  GKeyFile *keyfile = NULL;
41  GError *error = NULL;
42  gchar **keys = NULL, **key;
43  gint value;
44  GHashTable *ctxtmap = NULL;
45 
46  g_return_val_if_fail(chmfile, NULL);
47 
48  mapfile = g_new(gchar, strlen(chmfile) + 7);
49  strcpy(mapfile, chmfile);
50  dot = strrchr(mapfile, '.');
51  if (dot)
52  strcpy(dot, ".hhmap");
53  else
54  strcat(mapfile, ".hhmap");
55 
56  keyfile = g_key_file_new();
57  if (!g_key_file_load_from_file(keyfile, mapfile, G_KEY_FILE_NONE, &error))
58  goto cleanup_parse;
59 
60  if (NULL == (keys = g_key_file_get_keys(keyfile, "Map", NULL, &error)))
61  goto cleanup_parse;
62 
63  ctxtmap = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
64  for (key = keys; *key; key++)
65  {
66  value = g_key_file_get_integer(keyfile, "Map", *key, &error);
67  if (error)
68  goto cleanup_parse;
69  else
70  g_hash_table_insert(ctxtmap, g_strdup(*key), GINT_TO_POINTER(value));
71  }
72 
73 cleanup_parse:
74  if (error)
75  {
76  g_warning("Could not load help map file: %s", error->message);
77  g_error_free(error);
78  if (ctxtmap)
79  g_hash_table_destroy(ctxtmap);
80  ctxtmap = NULL;
81  }
82  if (keys)
83  g_strfreev(keys);
84  if (keyfile)
85  g_key_file_free (keyfile);
86  if (mapfile)
87  g_free(mapfile);
88 
89  return ctxtmap;
90 }
91 
92 void
93 gnc_show_htmlhelp(const gchar *chmfile, const gchar *anchor)
94 {
95  static GHashTable *chmfile_ctxtmap_map;
96  G_LOCK_DEFINE_STATIC(chmfile_ctxtmap_map);
97  GHashTable *ctxtmap;
98  gboolean create_map = FALSE;
99  wchar_t *wpath;
100  gint id = 0;
101 
102  g_return_if_fail(chmfile);
103 
104  if (anchor)
105  {
106  G_LOCK(chmfile_ctxtmap_map);
107  if (!chmfile_ctxtmap_map)
108  {
109  chmfile_ctxtmap_map = g_hash_table_new(g_str_hash, g_str_equal);
110  create_map = TRUE;
111  }
112  else
113  {
114  create_map = !g_hash_table_lookup_extended(
115  chmfile_ctxtmap_map, chmfile, NULL, (gpointer) & ctxtmap);
116  }
117 
118  if (create_map)
119  {
120  ctxtmap = parse_hhmap_file(chmfile);
121  g_hash_table_insert(chmfile_ctxtmap_map, g_strdup(chmfile), ctxtmap);
122  }
123 
124  if (ctxtmap)
125  {
126  gpointer ptr = g_hash_table_lookup(ctxtmap, anchor);
127  if (ptr)
128  id = GPOINTER_TO_INT(ptr);
129  }
130  G_UNLOCK(chmfile_ctxtmap_map);
131 
132  if (!id)
133  g_warning("Could not find anchor '%s'", anchor);
134  }
135 
136  wpath = g_utf8_to_utf16(chmfile, -1, NULL, NULL, NULL);
137  HtmlHelpW(GetDesktopWindow(), wpath, HH_HELP_CONTEXT, id);
138  g_free(wpath);
139 }
140 
141 #else /* !HAVE_HTMLHELPW */
142 void
143 gnc_show_htmlhelp(const gchar *chmfile, const gchar *anchor)
144 {
145  gchar *argv[3];
146 
147  g_return_if_fail(chmfile);
148 
149  argv[0] = "hh";
150  argv[1] = g_strdup(chmfile);
151  argv[2] = NULL;
152 
153  if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
154  NULL, NULL, NULL, NULL))
155  if (g_file_test(chmfile, G_FILE_TEST_IS_REGULAR))
156  g_warning("Found CHM help file, but could not spawn hh to open it");
157 
158  g_free(argv[1]);
159 }
160 #endif /* HAVE_HTMLHELPW */