2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2003-08-29 02:49:11 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2004-03-09 09:45:21 +08:00
|
|
|
* The GIMP Help plug-in
|
|
|
|
* Copyright (C) 1999-2004 Sven Neumann <sven@gimp.org>
|
2003-08-29 02:49:11 +08:00
|
|
|
* Michael Natterer <mitch@gimp.org>
|
2004-03-09 09:45:21 +08:00
|
|
|
* Henrik Brix Andersen <brix@gimp.org>
|
2003-08-29 02:49:11 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2004-08-14 23:47:22 +08:00
|
|
|
/* This code is written so that it can also be compiled standalone.
|
|
|
|
* It shouldn't depend on libgimp.
|
|
|
|
*/
|
|
|
|
|
2003-08-29 02:49:11 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
#include <glib-object.h>
|
2004-01-19 11:06:04 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
|
|
|
#include "gimphelp.h"
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2004-08-14 23:47:22 +08:00
|
|
|
#ifdef DISABLE_NLS
|
|
|
|
#define _(String) (String)
|
|
|
|
#else
|
2004-01-16 04:07:51 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
2004-08-14 23:47:22 +08:00
|
|
|
#endif
|
2004-01-16 04:07:51 +08:00
|
|
|
|
2003-08-29 02:49:11 +08:00
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
static gboolean domain_locale_parse (GimpHelpDomain *domain,
|
|
|
|
GimpHelpLocale *locale,
|
|
|
|
GError **error);
|
2004-08-14 23:47:22 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
static gchar * domain_filename_from_uri (const gchar *uri);
|
2004-03-18 10:45:52 +08:00
|
|
|
|
2005-02-08 02:06:17 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
/* public functions */
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
GimpHelpDomain *
|
|
|
|
gimp_help_domain_new (const gchar *domain_name,
|
|
|
|
const gchar *domain_uri,
|
|
|
|
const gchar *domain_root)
|
|
|
|
{
|
2007-05-23 21:59:51 +08:00
|
|
|
GimpHelpDomain *domain = g_slice_new0 (GimpHelpDomain);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
domain->help_domain = g_strdup (domain_name);
|
|
|
|
domain->help_uri = g_strdup (domain_uri);
|
|
|
|
domain->help_root = g_strdup (domain_root);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
if (domain_uri)
|
|
|
|
{
|
|
|
|
/* strip trailing slash */
|
|
|
|
if (g_str_has_suffix (domain->help_uri, "/"))
|
|
|
|
domain->help_uri[strlen (domain->help_uri) - 1] = '\0';
|
|
|
|
}
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
return domain;
|
|
|
|
}
|
2003-08-29 02:49:11 +08:00
|
|
|
|
|
|
|
void
|
2006-11-21 05:46:21 +08:00
|
|
|
gimp_help_domain_free (GimpHelpDomain *domain)
|
2003-08-29 02:49:11 +08:00
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
g_return_if_fail (domain != NULL);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
if (domain->help_locales)
|
|
|
|
g_hash_table_destroy (domain->help_locales);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
g_free (domain->help_domain);
|
|
|
|
g_free (domain->help_uri);
|
|
|
|
g_free (domain->help_root);
|
2007-05-23 21:59:51 +08:00
|
|
|
|
|
|
|
g_slice_free (GimpHelpDomain, domain);
|
2003-08-29 02:49:11 +08:00
|
|
|
}
|
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
GimpHelpLocale *
|
|
|
|
gimp_help_domain_lookup_locale (GimpHelpDomain *domain,
|
|
|
|
const gchar *locale_id)
|
2003-08-29 02:49:11 +08:00
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
GimpHelpLocale *locale = NULL;
|
|
|
|
|
|
|
|
if (domain->help_locales)
|
|
|
|
locale = g_hash_table_lookup (domain->help_locales, locale_id);
|
|
|
|
else
|
|
|
|
domain->help_locales =
|
|
|
|
g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
g_free,
|
|
|
|
(GDestroyNotify) gimp_help_locale_free);
|
|
|
|
|
|
|
|
if (locale)
|
|
|
|
return locale;
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
locale = gimp_help_locale_new (locale_id);
|
|
|
|
g_hash_table_insert (domain->help_locales, g_strdup (locale_id), locale);
|
|
|
|
|
|
|
|
domain_locale_parse (domain, locale, NULL);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
return locale;
|
2003-08-29 02:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2006-11-24 04:36:17 +08:00
|
|
|
gimp_help_domain_map (GimpHelpDomain *domain,
|
|
|
|
GList *help_locales,
|
|
|
|
const gchar *help_id,
|
|
|
|
GimpHelpLocale **ret_locale,
|
|
|
|
gboolean *fatal_error)
|
2003-08-29 02:49:11 +08:00
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
GimpHelpLocale *locale = NULL;
|
|
|
|
const gchar *ref = NULL;
|
|
|
|
GList *list;
|
2003-08-29 02:49:11 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (domain != NULL, NULL);
|
2004-03-18 08:11:03 +08:00
|
|
|
g_return_val_if_fail (help_locales != NULL, NULL);
|
2003-08-29 02:49:11 +08:00
|
|
|
g_return_val_if_fail (help_id != NULL, NULL);
|
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
if (fatal_error)
|
|
|
|
*fatal_error = FALSE;
|
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
/* first pass: look for a reference matching the help_id */
|
2004-03-18 10:45:52 +08:00
|
|
|
for (list = help_locales; list && !ref; list = list->next)
|
2003-08-29 02:49:11 +08:00
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
locale = gimp_help_domain_lookup_locale (domain,
|
|
|
|
(const gchar *) list->data);
|
|
|
|
ref = gimp_help_locale_map (locale, help_id);
|
2004-03-18 10:45:52 +08:00
|
|
|
}
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
/* second pass: look for a fallback */
|
|
|
|
for (list = help_locales; list && !ref; list = list->next)
|
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
locale = gimp_help_domain_lookup_locale (domain,
|
|
|
|
(const gchar *) list->data);
|
2004-03-18 20:18:12 +08:00
|
|
|
ref = locale->help_missing;
|
|
|
|
}
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-24 04:36:17 +08:00
|
|
|
if (ret_locale)
|
|
|
|
*ret_locale = locale;
|
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
if (ref)
|
|
|
|
{
|
|
|
|
return g_strconcat (domain->help_uri, "/",
|
|
|
|
locale->locale_id, "/",
|
|
|
|
ref,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else /* try to assemble a useful error message */
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2004-03-18 10:45:52 +08:00
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
#ifdef GIMP_HELP_DEBUG
|
|
|
|
g_printerr ("help: help_id lookup and all fallbacks failed for '%s'\n",
|
|
|
|
help_id);
|
2004-03-18 10:45:52 +08:00
|
|
|
#endif
|
2004-03-18 08:11:03 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
locale = gimp_help_domain_lookup_locale (domain,
|
|
|
|
GIMP_HELP_DEFAULT_LOCALE);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
if (! domain_locale_parse (domain, locale, &error))
|
|
|
|
{
|
|
|
|
if (error->code == G_FILE_ERROR_NOENT)
|
2005-10-06 06:27:50 +08:00
|
|
|
{
|
|
|
|
g_message ("%s\n\n%s",
|
|
|
|
_("The GIMP help files are not found."),
|
|
|
|
_("Please install the additional help package or use "
|
|
|
|
"the online user manual at http://docs.gimp.org/."));
|
|
|
|
}
|
2004-03-18 20:18:12 +08:00
|
|
|
else
|
2005-10-06 06:27:50 +08:00
|
|
|
{
|
|
|
|
g_message ("%s\n\n%s\n\n%s",
|
|
|
|
_("There is a problem with the GIMP help files."),
|
|
|
|
error->message,
|
|
|
|
_("Please check your installation."));
|
|
|
|
}
|
2004-03-18 10:45:52 +08:00
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
g_error_free (error);
|
2004-12-06 03:50:37 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
if (fatal_error)
|
|
|
|
*fatal_error = TRUE;
|
2004-03-18 20:18:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_message (_("Help ID '%s' unknown"), help_id);
|
|
|
|
}
|
2004-03-18 10:45:52 +08:00
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-03-18 10:45:52 +08:00
|
|
|
|
2003-08-29 02:49:11 +08:00
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2004-03-18 08:11:03 +08:00
|
|
|
static gboolean
|
2006-11-21 05:46:21 +08:00
|
|
|
domain_locale_parse (GimpHelpDomain *domain,
|
|
|
|
GimpHelpLocale *locale,
|
|
|
|
GError **error)
|
2003-08-29 02:49:11 +08:00
|
|
|
{
|
2006-11-21 05:46:21 +08:00
|
|
|
gchar *filename;
|
|
|
|
gboolean success;
|
2003-08-29 02:49:11 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (domain != NULL, FALSE);
|
2004-03-18 10:45:52 +08:00
|
|
|
g_return_val_if_fail (locale != NULL, FALSE);
|
2004-03-18 08:11:03 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2004-08-15 01:53:40 +08:00
|
|
|
if (! domain->help_root)
|
2006-11-21 05:46:21 +08:00
|
|
|
domain->help_root = domain_filename_from_uri (domain->help_uri);
|
2004-08-15 01:53:40 +08:00
|
|
|
|
|
|
|
if (! domain->help_root)
|
2004-08-14 23:47:22 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, 0, 0,
|
|
|
|
"Cannot determine location of gimp-help.xml from '%s'",
|
|
|
|
domain->help_uri);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-08-15 01:53:40 +08:00
|
|
|
filename = g_build_filename (domain->help_root,
|
2004-03-18 10:45:52 +08:00
|
|
|
locale->locale_id,
|
|
|
|
"gimp-help.xml",
|
|
|
|
NULL);
|
2003-08-29 02:49:11 +08:00
|
|
|
|
2006-11-21 05:46:21 +08:00
|
|
|
success = gimp_help_locale_parse (locale,
|
|
|
|
filename,
|
|
|
|
domain->help_domain,
|
|
|
|
error);
|
2004-03-18 20:18:12 +08:00
|
|
|
|
2003-08-29 02:49:11 +08:00
|
|
|
g_free (filename);
|
|
|
|
|
2004-03-18 20:18:12 +08:00
|
|
|
return success;
|
2003-08-29 02:49:11 +08:00
|
|
|
}
|
|
|
|
|
2005-02-08 02:30:13 +08:00
|
|
|
static gchar *
|
2006-11-21 05:46:21 +08:00
|
|
|
domain_filename_from_uri (const gchar *uri)
|
2005-02-08 02:06:17 +08:00
|
|
|
{
|
|
|
|
gchar *filename;
|
|
|
|
gchar *hostname;
|
|
|
|
|
|
|
|
g_return_val_if_fail (uri != NULL, NULL);
|
|
|
|
|
|
|
|
filename = g_filename_from_uri (uri, &hostname, NULL);
|
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (hostname)
|
|
|
|
{
|
|
|
|
/* we have a file: URI with a hostname */
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* on Win32, create a valid UNC path and use it as the filename */
|
|
|
|
|
|
|
|
gchar *tmp = g_build_filename ("//", hostname, filename, NULL);
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
filename = tmp;
|
|
|
|
#else
|
|
|
|
/* otherwise return NULL, caller should use URI then */
|
|
|
|
g_free (filename);
|
|
|
|
filename = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
g_free (hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|