plug-ins/help/Makefile.am plug-ins/help/gimphelpdomain.[ch]

2008-04-02  Sven Neumann  <sven@gimp.org>

	* plug-ins/help/Makefile.am
	* plug-ins/help/gimphelpdomain.[ch]
	* plug-ins/help/gimphelplocale.[ch]
	* plug-ins/help/gimphelp.[ch]
	* plug-ins/help/gimp-help-lookup.c: use GIO to access the help index.

svn path=/trunk/; revision=25331
This commit is contained in:
Sven Neumann 2008-04-02 10:30:58 +00:00 committed by Sven Neumann
parent 3a3e1b90d9
commit 4ecea2402d
9 changed files with 73 additions and 125 deletions

View File

@ -1,3 +1,11 @@
2008-04-02 Sven Neumann <sven@gimp.org>
* plug-ins/help/Makefile.am
* plug-ins/help/gimphelpdomain.[ch]
* plug-ins/help/gimphelplocale.[ch]
* plug-ins/help/gimphelp.[ch]
* plug-ins/help/gimp-help-lookup.c: use GIO to access the help index.
2008-04-01 Sven Neumann <sven@gimp.org>
* app/display/gimpdisplayshell-title.c

View File

@ -35,7 +35,7 @@ help_SOURCES = help.c
INCLUDES = \
-I$(top_srcdir) \
$(GLIB_CFLAGS) \
$(GIO_CFLAGS) \
-I$(includedir)
LDADD = \
@ -44,7 +44,7 @@ LDADD = \
$(libgimpcolor) \
$(libgimpbase) \
$(libgimpmath) \
$(GLIB_LIBS) \
$(GIO_LIBS) \
$(RT_LIBS) \
$(INTLLIBS)
@ -56,4 +56,4 @@ gimp_help_lookup_SOURCES = gimp-help-lookup.c
gimp_help_lookup_LDADD = \
$(libgimphelp) \
$(libgimpbase) \
$(GLIB_LIBS)
$(GIO_LIBS)

View File

@ -80,7 +80,7 @@ main (gint argc,
GError *error = NULL;
help_base = g_getenv (GIMP_HELP_ENV_URI);
help_root = g_build_path (G_DIR_SEPARATOR_S, gimp_data_directory (), GIMP_HELP_PREFIX, NULL);
help_root = g_build_filename (gimp_data_directory (), GIMP_HELP_PREFIX, NULL);
context = g_option_context_new ("HELP-ID");
g_option_context_add_main_entries (context, entries, NULL);
@ -91,12 +91,14 @@ main (gint argc,
return EXIT_FAILURE;
}
g_type_init ();
if (help_base)
uri = g_strdup (help_base);
else
uri = g_filename_to_uri (help_root, NULL, NULL);
gimp_help_register_domain (GIMP_HELP_DEFAULT_DOMAIN, uri, help_root);
gimp_help_register_domain (GIMP_HELP_DEFAULT_DOMAIN, uri);
g_free (uri);
uri = lookup (GIMP_HELP_DEFAULT_DOMAIN,

View File

@ -84,13 +84,10 @@ gimp_help_init (gint num_domain_names,
g_free (help_root);
}
gimp_help_register_domain (GIMP_HELP_DEFAULT_DOMAIN,
default_domain_uri, NULL);
gimp_help_register_domain (GIMP_HELP_DEFAULT_DOMAIN, default_domain_uri);
for (i = 0; i < num_domain_names; i++)
{
gimp_help_register_domain (domain_names[i], domain_uris[i], NULL);
}
gimp_help_register_domain (domain_names[i], domain_uris[i]);
g_free (default_domain_uri);
@ -109,8 +106,7 @@ gimp_help_exit (void)
void
gimp_help_register_domain (const gchar *domain_name,
const gchar *domain_uri,
const gchar *domain_root)
const gchar *domain_uri)
{
g_return_if_fail (domain_name != NULL);
g_return_if_fail (domain_uri != NULL);
@ -127,8 +123,7 @@ gimp_help_register_domain (const gchar *domain_name,
g_hash_table_insert (domain_hash,
g_strdup (domain_name),
gimp_help_domain_new (domain_name,
domain_uri, domain_root));
gimp_help_domain_new (domain_name, domain_uri));
}
GimpHelpDomain *

View File

@ -49,8 +49,7 @@ gboolean gimp_help_init (gint n_domain_names,
void gimp_help_exit (void);
void gimp_help_register_domain (const gchar *domain_name,
const gchar *domain_uri,
const gchar *domain_root);
const gchar *domain_uri);
GimpHelpDomain * gimp_help_lookup_domain (const gchar *domain_name);
GList * gimp_help_parse_locales (const gchar *help_locales);

View File

@ -44,25 +44,21 @@
/* local function prototypes */
static gboolean domain_locale_parse (GimpHelpDomain *domain,
GimpHelpLocale *locale,
GError **error);
static gchar * domain_filename_from_uri (const gchar *uri);
static gboolean domain_locale_parse (GimpHelpDomain *domain,
GimpHelpLocale *locale,
GError **error);
/* public functions */
GimpHelpDomain *
gimp_help_domain_new (const gchar *domain_name,
const gchar *domain_uri,
const gchar *domain_root)
const gchar *domain_uri)
{
GimpHelpDomain *domain = g_slice_new0 (GimpHelpDomain);
domain->help_domain = g_strdup (domain_name);
domain->help_uri = g_strdup (domain_uri);
domain->help_root = g_strdup (domain_root);
if (domain_uri)
{
@ -84,7 +80,6 @@ gimp_help_domain_free (GimpHelpDomain *domain)
g_free (domain->help_domain);
g_free (domain->help_uri);
g_free (domain->help_root);
g_slice_free (GimpHelpDomain, domain);
}
@ -209,70 +204,19 @@ domain_locale_parse (GimpHelpDomain *domain,
GimpHelpLocale *locale,
GError **error)
{
gchar *filename;
gchar *uri;
gboolean success;
g_return_val_if_fail (domain != NULL, FALSE);
g_return_val_if_fail (locale != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (! domain->help_root)
domain->help_root = domain_filename_from_uri (domain->help_uri);
uri = g_strdup_printf ("%s/%s/gimp-help.xml",
domain->help_uri, locale->locale_id);
if (! domain->help_root)
{
g_set_error (error, 0, 0,
"Cannot determine location of gimp-help.xml from '%s'",
domain->help_uri);
return FALSE;
}
success = gimp_help_locale_parse (locale, uri, domain->help_domain, error);
filename = g_build_filename (domain->help_root,
locale->locale_id,
"gimp-help.xml",
NULL);
success = gimp_help_locale_parse (locale,
filename,
domain->help_domain,
error);
g_free (filename);
g_free (uri);
return success;
}
static gchar *
domain_filename_from_uri (const gchar *uri)
{
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;
}

View File

@ -29,14 +29,12 @@ struct _GimpHelpDomain
{
gchar *help_domain;
gchar *help_uri;
gchar *help_root;
GHashTable *help_locales;
};
GimpHelpDomain * gimp_help_domain_new (const gchar *domain_name,
const gchar *domain_uri,
const gchar *domain_root);
const gchar *domain_uri);
void gimp_help_domain_free (GimpHelpDomain *domain);
GimpHelpLocale * gimp_help_domain_lookup_locale (GimpHelpDomain *domain,

View File

@ -29,7 +29,8 @@
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include "gimphelp.h"
@ -44,7 +45,7 @@
static void locale_set_error (GError **error,
const gchar *format,
const gchar *filename);
GFile *file);
/* public functions */
@ -103,7 +104,7 @@ typedef enum
typedef struct
{
const gchar *filename;
GFile *file;
LocaleParserState state;
LocaleParserState last_known_state;
gint markup_depth;
@ -116,7 +117,7 @@ typedef struct
} LocaleParser;
static gboolean locale_parser_parse (GMarkupParseContext *context,
GIOChannel *io,
GInputStream *stream,
GError **error);
static void locale_parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
@ -154,17 +155,18 @@ static const GMarkupParser markup_parser =
gboolean
gimp_help_locale_parse (GimpHelpLocale *locale,
const gchar *filename,
const gchar *uri,
const gchar *help_domain,
GError **error)
{
GMarkupParseContext *context;
GIOChannel *io;
GFile *file;
GFileInputStream *stream;
LocaleParser parser = { NULL, };
gboolean success;
g_return_val_if_fail (locale != NULL, FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (uri != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (locale->help_id_mapping)
@ -181,21 +183,22 @@ gimp_help_locale_parse (GimpHelpLocale *locale,
#ifdef GIMP_HELP_DEBUG
g_printerr ("help (%s): parsing '%s' for locale \"%s\"\n",
locale->locale_id,
filename,
help_domain);
locale->locale_id, uri, help_domain);
#endif
io = g_io_channel_new_file (filename, "r", error);
if (! io)
file = g_file_new_for_uri (uri);
stream = g_file_read (file, NULL, error);
if (! stream)
{
locale_set_error (error,
_("Could not open '%s' for reading: %s"),
filename);
locale_set_error (error, _("Could not open '%s' for reading: %s"), file);
g_object_unref (file);
return FALSE;
}
parser.filename = filename;
parser.file = file;
parser.value = g_string_new (NULL);
parser.locale = locale;
parser.help_domain = help_domain;
@ -203,50 +206,45 @@ gimp_help_locale_parse (GimpHelpLocale *locale,
context = g_markup_parse_context_new (&markup_parser, 0, &parser, NULL);
success = locale_parser_parse (context, io, error);
success = locale_parser_parse (context, G_INPUT_STREAM (stream), error);
g_markup_parse_context_free (context);
g_io_channel_unref (io);
g_object_unref (stream);
g_string_free (parser.value, TRUE);
g_free (parser.id_attr_name);
if (! success)
locale_set_error (error, _("Parse error in '%s':\n%s"), filename);
locale_set_error (error, _("Parse error in '%s':\n%s"), file);
g_object_unref (file);
return success;
}
static gboolean
locale_parser_parse (GMarkupParseContext *context,
GIOChannel *io,
GInputStream *stream,
GError **error)
{
GIOStatus status;
gsize len;
gchar buffer[4096];
gssize len;
gchar buffer[4096];
while (TRUE)
while ((len = g_input_stream_read (stream, buffer, sizeof (buffer),
NULL, error)) != -1)
{
status = g_io_channel_read_chars (io,
buffer, sizeof (buffer), &len, error);
switch (status)
switch (len)
{
case G_IO_STATUS_ERROR:
return FALSE;
case G_IO_STATUS_EOF:
case 0:
return g_markup_parse_context_end_parse (context, error);
case G_IO_STATUS_NORMAL:
default:
if (! g_markup_parse_context_parse (context, buffer, len, error))
return FALSE;
break;
case G_IO_STATUS_AGAIN:
break;
}
}
return TRUE;
return FALSE;
}
static void
@ -332,8 +330,11 @@ locale_parser_error (GMarkupParseContext *context,
gpointer user_data)
{
LocaleParser *parser = (LocaleParser *) user_data;
gchar *name = g_file_get_parse_name (parser->file);
g_printerr ("help (parsing %s): %s", parser->filename, error->message);
g_printerr ("help (parsing %s): %s", name, error->message);
g_free (name);
}
static void
@ -453,13 +454,14 @@ locale_parser_parse_missing (LocaleParser *parser,
static void
locale_set_error (GError **error,
const gchar *format,
const gchar *filename)
GFile *file)
{
if (error && *error)
{
gchar *name = g_filename_display_name (filename);
gchar *msg = g_strdup_printf (format, name, (*error)->message);
gchar *name = g_file_get_parse_name (file);
gchar *msg;
msg = g_strdup_printf (format, name, (*error)->message);
g_free (name);
g_free ((*error)->message);

View File

@ -43,7 +43,7 @@ const gchar * gimp_help_locale_map (GimpHelpLocale *locale,
const gchar *help_id);
gboolean gimp_help_locale_parse (GimpHelpLocale *locale,
const gchar *filename,
const gchar *uri,
const gchar *help_domain,
GError **error);