Commit bf30024d authored by Matthew Barnes's avatar Matthew Barnes
Browse files

Teach EMailExtensionRegistry to find extensions.

Now we have the following extension points in the GType hierarchy:

   E_TYPE_MAIL_FORMATTER_EXTENSION
   E_TYPE_MAIL_FORMATTER_PRINT_EXTENSION
   E_TYPE_MAIL_FORMATTER_QUOTE_EXTENSION
   E_TYPE_MAIL_PARSER_EXTENSION

A registry just needs to be given one of these extension points, and it
can use g_type_children() to find and load all registered extensions.

This eliminates e-mail-format-extensions.[ch] as well as the dynamic
loaders I added a few commits back.  Dynamically loaded extensions are
now easier to register, at the cost of internal extensions being a tad
more cumbersome to register.  Fair tradeoff, imo.

This also makes e_mail_extension_registry_add_extension() a private
function used only by e_mail_formatter_extension_registry_load() and
e_mail_parser_extension_registry_load().
parent 7c656866
......@@ -40,8 +40,6 @@ libemformat_la_SOURCES = \
$(emformatinclude_HEADERS) \
e-mail-extension-registry.c \
e-mail-inline-filter.c \
e-mail-format-extensions.c \
e-mail-format-extensions.h \
e-mail-formatter.c \
e-mail-formatter-print.c \
e-mail-formatter-quote.c \
......
......@@ -19,7 +19,8 @@
#include <glib-object.h>
#include "e-mail-extension-registry.h"
#include "e-mail-format-extensions.h"
#include "e-mail-formatter-extension.h"
#include "e-mail-parser-extension.h"
#include <libebackend/libebackend.h>
#include <camel/camel.h>
......@@ -58,6 +59,49 @@ destroy_queue (GQueue *queue)
g_queue_free_full (queue, g_object_unref);
}
static void
mail_extension_registry_add_extension (EMailExtensionRegistry *registry,
const gchar **mime_types,
GType extension_type)
{
GObject *extension;
gint ii;
if (mime_types == NULL) {
g_critical (
"%s does not define any MIME types",
g_type_name (extension_type));
return;
}
extension = g_object_new (extension_type, NULL);
for (ii = 0; mime_types[ii] != NULL; ii++) {
GQueue *queue;
queue = g_hash_table_lookup (
registry->priv->table, mime_types[ii]);
if (queue == NULL) {
queue = g_queue_new ();
g_hash_table_insert (
registry->priv->table,
(gpointer) mime_types[ii],
queue);
}
g_queue_push_head (queue, g_object_ref (extension));
if (camel_debug ("emformat:registry")) {
printf (
"Added extension '%s' for type '%s'\n",
g_type_name (extension_type),
mime_types[ii]);
}
}
g_object_unref (extension);
}
static void
mail_extension_registry_finalize (GObject *object)
{
......@@ -96,55 +140,6 @@ e_mail_extension_registry_init (EMailExtensionRegistry *registry)
(GDestroyNotify) destroy_queue);
}
/**
* e_mail_extension_registry_add_extension:
* @registry: an #EMailExtensionRegistry
* @mime_types: a %NULL-terminated array of MIME types
* @extension_type: the #GType of the extension being added
*
* Creates an instance of @extension_type and registers the instance for
* all provided MIME types.
*/
void
e_mail_extension_registry_add_extension (EMailExtensionRegistry *registry,
const gchar **mime_types,
GType extension_type)
{
GObject *extension;
gint ii;
g_return_if_fail (E_IS_MAIL_EXTENSION_REGISTRY (registry));
g_return_if_fail (mime_types != NULL);
g_return_if_fail (extension_type != G_TYPE_INVALID);
extension = g_object_new (extension_type, NULL);
for (ii = 0; mime_types[ii] != NULL; ii++) {
GQueue *queue;
queue = g_hash_table_lookup (
registry->priv->table, mime_types[ii]);
if (queue == NULL) {
queue = g_queue_new ();
g_hash_table_insert (
registry->priv->table,
(gpointer) mime_types[ii],
queue);
}
g_queue_push_head (queue, g_object_ref (extension));
if (camel_debug ("emformat:registry")) {
printf (
"Added extension '%s' for type '%s'\n",
g_type_name (extension_type),
mime_types[ii]);
}
}
g_object_unref (extension);
}
/**
* e_mail_extension_registry_get_for_mime_type:
* @regstry: An #EMailExtensionRegistry
......@@ -225,6 +220,36 @@ e_mail_parser_extension_registry_init (EMailParserExtensionRegistry *registry)
{
}
void
e_mail_parser_extension_registry_load (EMailParserExtensionRegistry *registry)
{
GType *children;
GType base_extension_type;
guint ii, n_children;
g_return_if_fail (E_IS_MAIL_PARSER_EXTENSION_REGISTRY (registry));
base_extension_type = E_TYPE_MAIL_PARSER_EXTENSION;
children = g_type_children (base_extension_type, &n_children);
for (ii = 0; ii < n_children; ii++) {
EMailParserExtensionClass *class;
if (G_TYPE_IS_ABSTRACT (children[ii]))
continue;
class = g_type_class_ref (children[ii]);
mail_extension_registry_add_extension (
E_MAIL_EXTENSION_REGISTRY (registry),
class->mime_types, children[ii]);
g_type_class_unref (class);
}
g_free (children);
}
/******************************************************************************/
G_DEFINE_TYPE_WITH_CODE (
......@@ -243,3 +268,32 @@ e_mail_formatter_extension_registry_init (EMailFormatterExtensionRegistry *regis
{
}
void
e_mail_formatter_extension_registry_load (EMailFormatterExtensionRegistry *registry,
GType base_extension_type)
{
GType *children;
guint ii, n_children;
g_return_if_fail (E_IS_MAIL_FORMATTER_EXTENSION_REGISTRY (registry));
children = g_type_children (base_extension_type, &n_children);
for (ii = 0; ii < n_children; ii++) {
EMailFormatterExtensionClass *class;
if (G_TYPE_IS_ABSTRACT (children[ii]))
continue;
class = g_type_class_ref (children[ii]);
mail_extension_registry_add_extension (
E_MAIL_EXTENSION_REGISTRY (registry),
class->mime_types, children[ii]);
g_type_class_unref (class);
}
g_free (children);
}
......@@ -57,10 +57,6 @@ struct _EMailExtensionRegistryClass {
GType e_mail_extension_registry_get_type
(void) G_GNUC_CONST;
void e_mail_extension_registry_add_extension
(EMailExtensionRegistry *registry,
const gchar **mime_types,
GType extension_type);
GQueue * e_mail_extension_registry_get_for_mime_type
(EMailExtensionRegistry *registry,
const gchar *mime_type);
......@@ -108,6 +104,8 @@ struct _EMailParserExtensionRegistryClass {
GType e_mail_parser_extension_registry_get_type
(void) G_GNUC_CONST;
void e_mail_parser_extension_registry_load
(EMailParserExtensionRegistry *registry);
G_END_DECLS
......@@ -149,6 +147,9 @@ struct _EMailFormatterExtensionRegistryClass {
GType e_mail_formatter_extension_registry_get_type
(void) G_GNUC_CONST;
void e_mail_formatter_extension_registry_load
(EMailFormatterExtensionRegistry *registry,
GType base_extension_type);
G_END_DECLS
......
/*
* e-mail-format-extensions.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*/
#include "e-mail-format-extensions.h"
#include "em-format/e-mail-parser-extension.h"
#include "em-format/e-mail-formatter-extension.h"
typedef GType (*TypeFunc) (void);
TypeFunc parser_funcs[] = {
e_mail_parser_application_mbox_get_type,
e_mail_parser_attachment_bar_get_type,
e_mail_parser_headers_get_type,
e_mail_parser_message_get_type,
e_mail_parser_secure_button_get_type,
e_mail_parser_source_get_type,
e_mail_parser_image_get_type,
e_mail_parser_inline_pgp_encrypted_get_type,
e_mail_parser_inline_pgp_signed_get_type,
e_mail_parser_message_delivery_status_get_type,
e_mail_parser_message_external_get_type,
e_mail_parser_message_rfc822_get_type,
e_mail_parser_multipart_alternative_get_type,
e_mail_parser_multipart_apple_double_get_type,
e_mail_parser_multipart_digest_get_type,
e_mail_parser_multipart_encrypted_get_type,
e_mail_parser_multipart_mixed_get_type,
e_mail_parser_multipart_related_get_type,
e_mail_parser_multipart_signed_get_type,
e_mail_parser_text_enriched_get_type,
e_mail_parser_text_html_get_type,
e_mail_parser_text_plain_get_type,
#ifdef ENABLE_SMIME
e_mail_parser_application_smime_get_type,
#endif
NULL
};
TypeFunc formatter_funcs[] = {
e_mail_formatter_attachment_get_type,
e_mail_formatter_attachment_bar_get_type,
e_mail_formatter_error_get_type,
e_mail_formatter_headers_get_type,
e_mail_formatter_secure_button_get_type,
e_mail_formatter_source_get_type,
e_mail_formatter_image_get_type,
e_mail_formatter_message_rfc822_get_type,
e_mail_formatter_text_enriched_get_type,
e_mail_formatter_text_html_get_type,
e_mail_formatter_text_plain_get_type,
NULL
};
TypeFunc quote_formatter_funcs[] = {
e_mail_formatter_quote_attachment_get_type,
e_mail_formatter_quote_headers_get_type,
e_mail_formatter_quote_message_rfc822_get_type,
e_mail_formatter_quote_text_enriched_get_type,
e_mail_formatter_quote_text_html_get_type,
e_mail_formatter_quote_text_plain_get_type,
NULL
};
TypeFunc print_formatter_funcs[] = {
e_mail_formatter_print_headers_get_type,
NULL
};
static void
load (EMailExtensionRegistry *ereg,
TypeFunc *func_array)
{
gint ii;
for (ii = 0; func_array[ii] != NULL; ii++) {
GType extension_type;
GType interface_type;
gpointer extension_class;
const gchar **mime_types = NULL;
extension_type = func_array[ii]();
extension_class = g_type_class_ref (extension_type);
interface_type = E_TYPE_MAIL_FORMATTER_EXTENSION;
if (g_type_is_a (extension_type, interface_type))
mime_types = ((EMailFormatterExtensionClass *)
extension_class)->mime_types;
interface_type = E_TYPE_MAIL_PARSER_EXTENSION;
if (g_type_is_a (extension_type, interface_type))
mime_types = ((EMailParserExtensionClass *)
extension_class)->mime_types;
if (mime_types != NULL)
e_mail_extension_registry_add_extension (
ereg, mime_types, extension_type);
else
g_critical (
"%s does not define any MIME types",
g_type_name (extension_type));
g_type_class_unref (extension_class);
}
}
void
e_mail_parser_internal_extensions_load (EMailExtensionRegistry *ereg)
{
load (ereg, parser_funcs);
}
void
e_mail_formatter_internal_extensions_load (EMailExtensionRegistry *ereg)
{
load (ereg, formatter_funcs);
}
void
e_mail_formatter_quote_internal_extensions_load (EMailExtensionRegistry *ereg)
{
load (ereg, quote_formatter_funcs);
}
void
e_mail_formatter_print_internal_extensions_load (EMailExtensionRegistry *ereg)
{
load (ereg, print_formatter_funcs);
}
/*
* extensions.h
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*/
#ifndef EXTENSIONS_H_
#define EXTENSIONS_H_
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <em-format/e-mail-extension-registry.h>
#include <em-format/e-mail-part.h>
#include <widgets/misc/e-attachment-view.h>
G_BEGIN_DECLS
void e_mail_formatter_internal_extensions_load (EMailExtensionRegistry *ereg);
void e_mail_parser_internal_extensions_load (EMailExtensionRegistry *ereg);
void e_mail_formatter_quote_internal_extensions_load (EMailExtensionRegistry *ereg);
void e_mail_formatter_print_internal_extensions_load (EMailExtensionRegistry *ereg);
GType e_mail_formatter_attachment_get_type (void);
GType e_mail_formatter_attachment_bar_get_type
(void);
GType e_mail_formatter_error_get_type (void);
GType e_mail_formatter_headers_get_type (void);
GType e_mail_formatter_secure_button_get_type
(void);
GType e_mail_formatter_source_get_type (void);
GType e_mail_formatter_image_get_type (void);
GType e_mail_formatter_message_rfc822_get_type (void);
GType e_mail_formatter_text_enriched_get_type (void);
GType e_mail_formatter_text_html_get_type (void);
GType e_mail_formatter_text_plain_get_type (void);
GType e_mail_parser_application_mbox_get_type (void);
GType e_mail_parser_attachment_bar_get_type (void);
GType e_mail_parser_headers_get_type (void);
GType e_mail_parser_message_get_type (void);
GType e_mail_parser_secure_button_get_type (void);
GType e_mail_parser_source_get_type (void);
GType e_mail_parser_image_get_type (void);
GType e_mail_parser_inline_pgp_encrypted_get_type (void);
GType e_mail_parser_inline_pgp_signed_get_type (void);
GType e_mail_parser_message_delivery_status_get_type (void);
GType e_mail_parser_message_external_get_type (void);
GType e_mail_parser_message_rfc822_get_type (void);
GType e_mail_parser_multipart_alternative_get_type (void);
GType e_mail_parser_multipart_apple_double_get_type (void);
GType e_mail_parser_multipart_digest_get_type (void);
GType e_mail_parser_multipart_encrypted_get_type (void);
GType e_mail_parser_multipart_mixed_get_type (void);
GType e_mail_parser_multipart_related_get_type (void);
GType e_mail_parser_multipart_signed_get_type (void);
GType e_mail_parser_text_enriched_get_type (void);
GType e_mail_parser_text_html_get_type (void);
GType e_mail_parser_text_plain_get_type (void);
#ifdef ENABLE_SMIME
GType e_mail_parser_application_smime_get_type (void);
#endif
GType e_mail_formatter_quote_attachment_get_type (void);
GType e_mail_formatter_quote_headers_get_type (void);
GType e_mail_formatter_quote_message_rfc822_get_type (void);
GType e_mail_formatter_quote_text_enriched_get_type (void);
GType e_mail_formatter_quote_text_html_get_type (void);
GType e_mail_formatter_quote_text_plain_get_type (void);
GType e_mail_formatter_print_headers_get_type (void);
G_END_DECLS
#endif /* EXTENSIONS_H_ */
......@@ -19,7 +19,6 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include "e-mail-part-attachment-bar.h"
#include <misc/e-attachment-bar.h>
......@@ -31,6 +30,8 @@
typedef EMailFormatterExtension EMailFormatterAttachmentBar;
typedef EMailFormatterExtensionClass EMailFormatterAttachmentBarClass;
GType e_mail_formatter_attachment_bar_get_type (void);
G_DEFINE_TYPE (
EMailFormatterAttachmentBar,
e_mail_formatter_attachment_bar,
......
......@@ -20,7 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include "e-mail-part-attachment.h"
#include "e-mail-part-attachment-bar.h"
......@@ -43,6 +42,8 @@
typedef EMailFormatterExtension EMailFormatterAttachment;
typedef EMailFormatterExtensionClass EMailFormatterAttachmentClass;
GType e_mail_formatter_attachment_get_type (void);
G_DEFINE_TYPE (
EMailFormatterAttachment,
e_mail_formatter_attachment,
......
......@@ -19,7 +19,6 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <glib/gi18n-lib.h>
......@@ -32,6 +31,8 @@
typedef EMailFormatterExtension EMailFormatterError;
typedef EMailFormatterExtensionClass EMailFormatterErrorClass;
GType e_mail_formatter_error_get_type (void);
G_DEFINE_TYPE (
EMailFormatterError,
e_mail_formatter_error,
......
......@@ -20,8 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <glib/gi18n-lib.h>
#include <em-format/e-mail-formatter-extension.h>
......@@ -40,6 +38,8 @@
typedef EMailFormatterExtension EMailFormatterHeaders;
typedef EMailFormatterExtensionClass EMailFormatterHeadersClass;
GType e_mail_formatter_headers_get_type (void);
G_DEFINE_TYPE (
EMailFormatterHeaders,
e_mail_formatter_headers,
......
......@@ -20,8 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <em-format/e-mail-parser-extension.h>
#include <em-format/e-mail-formatter-extension.h>
#include <em-format/e-mail-part-utils.h>
......@@ -36,6 +34,8 @@
typedef EMailFormatterExtension EMailFormatterImage;
typedef EMailFormatterExtensionClass EMailFormatterImageClass;
GType e_mail_formatter_image_get_type (void);
G_DEFINE_TYPE (
EMailFormatterImage,
e_mail_formatter_image,
......
......@@ -20,8 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <glib/gi18n-lib.h>
#include <glib-object.h>
......@@ -38,6 +36,8 @@
typedef EMailFormatterExtension EMailFormatterMessageRFC822;
typedef EMailFormatterExtensionClass EMailFormatterMessageRFC822Class;
GType e_mail_formatter_message_rfc822_get_type (void);
G_DEFINE_TYPE (
EMailFormatterMessageRFC822,
e_mail_formatter_message_rfc822,
......
......@@ -20,8 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <glib/gi18n-lib.h>
#include <em-format/e-mail-formatter-print.h>
......@@ -37,6 +35,8 @@
typedef EMailFormatterExtension EMailFormatterPrintHeaders;
typedef EMailFormatterExtensionClass EMailFormatterPrintHeadersClass;
GType e_mail_formatter_print_headers_get_type (void);
G_DEFINE_TYPE (
EMailFormatterPrintHeaders,
e_mail_formatter_print_headers,
......
......@@ -239,9 +239,9 @@ e_mail_formatter_print_base_init (EMailFormatterPrintClass *class)
/* Register internal extensions. */
g_type_ensure (e_mail_formatter_print_headers_get_type ());
e_mail_formatter_print_internal_extensions_load (
E_MAIL_EXTENSION_REGISTRY (
E_MAIL_FORMATTER_CLASS (class)->extension_registry));
e_mail_formatter_extension_registry_load (
E_MAIL_FORMATTER_CLASS (class)->extension_registry,
E_TYPE_MAIL_FORMATTER_PRINT_EXTENSION);
E_MAIL_FORMATTER_CLASS (class)->text_html_flags =
CAMEL_MIME_FILTER_TOHTML_CONVERT_NL |
......
......@@ -20,7 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include "e-mail-part-attachment.h"
#include <em-format/e-mail-formatter-quote.h>
......@@ -35,6 +34,8 @@
typedef EMailFormatterExtension EMailFormatterQuoteAttachment;
typedef EMailFormatterExtensionClass EMailFormatterQuoteAttachmentClass;
GType e_mail_formatter_quote_attachment_get_type (void);
G_DEFINE_TYPE (
EMailFormatterQuoteAttachment,
e_mail_formatter_quote_attachment,
......
......@@ -20,8 +20,6 @@
#include <config.h>
#endif
#include "e-mail-format-extensions.h"
#include <glib/gi18n-lib.h>
#include <em-format/e-mail-formatter-quote.h>
......@@ -37,6 +35,8 @@
typedef EMailFormatterExtension EMailFormatterQuoteHeaders;
typedef EMailFormatterExtensionClass EMailFormatterQuoteHeadersClass;
</