spell-plugin: Port to libpeas

Adapted from:
67a0f36183
This commit is contained in:
JosephMcc 2016-12-26 13:44:07 -08:00
parent 7e6b54b760
commit e389368a91
2 changed files with 268 additions and 287 deletions

View File

@ -31,9 +31,11 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gmodule.h> #include <gmodule.h>
#include <libpeas/peas-activatable.h>
#include <libpeas-gtk/peas-gtk-configurable.h>
#include <xed/xed-window.h>
#include <xed/xed-debug.h> #include <xed/xed-debug.h>
#include <xed/xed-help.h>
#include <xed/xed-prefs-manager.h> #include <xed/xed-prefs-manager.h>
#include <xed/xed-statusbar.h> #include <xed/xed-statusbar.h>
#include <xed/xed-utils.h> #include <xed/xed-utils.h>
@ -46,7 +48,6 @@
#define XED_METADATA_ATTRIBUTE_SPELL_LANGUAGE "metadata::xed-spell-language" #define XED_METADATA_ATTRIBUTE_SPELL_LANGUAGE "metadata::xed-spell-language"
#define XED_METADATA_ATTRIBUTE_SPELL_ENABLED "metadata::xed-spell-enabled" #define XED_METADATA_ATTRIBUTE_SPELL_ENABLED "metadata::xed-spell-enabled"
#define WINDOW_DATA_KEY "XedSpellPluginWindowData"
#define MENU_PATH "/MenuBar/ToolsMenu/ToolsOps_1" #define MENU_PATH "/MenuBar/ToolsMenu/ToolsOps_1"
#define XED_SPELL_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \ #define XED_SPELL_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
@ -57,32 +58,40 @@
#define SPELL_SCHEMA "org.x.editor.plugins.spell" #define SPELL_SCHEMA "org.x.editor.plugins.spell"
#define AUTOCHECK_TYPE_KEY "autocheck-type" #define AUTOCHECK_TYPE_KEY "autocheck-type"
XED_PLUGIN_REGISTER_TYPE(XedSpellPlugin, xed_spell_plugin) static void peas_activatable_iface_init (PeasActivatableInterface *iface);
static void peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface);
typedef struct G_DEFINE_DYNAMIC_TYPE_EXTENDED (XedSpellPlugin,
xed_spell_plugin,
PEAS_TYPE_EXTENSION_BASE,
0,
G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_TYPE_ACTIVATABLE,
peas_activatable_iface_init)
G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_GTK_TYPE_CONFIGURABLE,
peas_gtk_configurable_iface_init))
enum
{ {
PROP_0,
PROP_OBJECT
};
struct _XedSpellPluginPrivate
{
GtkWidget *window;
GtkActionGroup *action_group; GtkActionGroup *action_group;
guint ui_id; guint ui_id;
guint message_cid; guint message_cid;
gulong tab_added_id; gulong tab_added_id;
gulong tab_removed_id; gulong tab_removed_id;
XedSpellPlugin *plugin;
} WindowData;
typedef struct
{
XedPlugin *plugin;
XedWindow *window;
} ActionData;
struct _XedSpellPluginPrivate
{
GSettings *settings; GSettings *settings;
}; };
static void spell_cb (GtkAction *action, ActionData *action_data); static void spell_cb (GtkAction *action, XedSpellPlugin *plugin);
static void set_language_cb (GtkAction *action, ActionData *action_data); static void set_language_cb (GtkAction *action, XedSpellPlugin *plugin);
static void auto_spell_cb (GtkAction *action, XedWindow *window); static void auto_spell_cb (GtkAction *action, XedSpellPlugin *plugin);
/* UI actions. */ /* UI actions. */
static const GtkActionEntry action_entries[] = static const GtkActionEntry action_entries[] =
@ -120,13 +129,13 @@ typedef struct _SpellConfigureDialog SpellConfigureDialog;
struct _SpellConfigureDialog struct _SpellConfigureDialog
{ {
GtkWidget *dialog; GtkWidget *content;
GtkWidget *never; GtkWidget *never;
GtkWidget *always; GtkWidget *always;
GtkWidget *document; GtkWidget *document;
XedSpellPlugin *plugin; GSettings *settings;
}; };
typedef enum typedef enum
@ -163,15 +172,27 @@ xed_spell_plugin_init (XedSpellPlugin *plugin)
} }
static void static void
xed_spell_plugin_finalize (GObject *object) xed_spell_plugin_dispose (GObject *object)
{ {
XedSpellPlugin *plugin = XED_SPELL_PLUGIN (object); XedSpellPlugin *plugin = XED_SPELL_PLUGIN (object);
xed_debug_message (DEBUG_PLUGINS, "XedSpellPlugin finalizing"); xed_debug_message (DEBUG_PLUGINS, "XedSpellPlugin disposing");
if (plugin->priv->window != NULL)
{
g_object_unref (plugin->priv->window);
plugin->priv->window = NULL;
}
if (plugin->priv->action_group)
{
g_object_unref (plugin->priv->action_group);
plugin->priv->action_group = NULL;
}
g_object_unref (G_OBJECT (plugin->priv->settings)); g_object_unref (G_OBJECT (plugin->priv->settings));
G_OBJECT_CLASS (xed_spell_plugin_parent_class)->finalize (object); G_OBJECT_CLASS (xed_spell_plugin_parent_class)->dispose (object);
} }
static void static void
@ -226,16 +247,16 @@ get_autocheck_type (XedSpellPlugin *plugin)
} }
static void static void
set_autocheck_type (XedSpellPlugin *plugin, set_autocheck_type (GSettings *settings,
XedSpellPluginAutocheckType autocheck_type) XedSpellPluginAutocheckType autocheck_type)
{ {
if (!g_settings_is_writable (plugin->priv->settings, if (!g_settings_is_writable (settings,
AUTOCHECK_TYPE_KEY)) AUTOCHECK_TYPE_KEY))
{ {
return; return;
} }
g_settings_set_enum (plugin->priv->settings, g_settings_set_enum (settings,
AUTOCHECK_TYPE_KEY, AUTOCHECK_TYPE_KEY,
autocheck_type); autocheck_type);
} }
@ -739,7 +760,6 @@ get_configure_dialog (XedSpellPlugin *plugin)
SpellConfigureDialog *dialog = NULL; SpellConfigureDialog *dialog = NULL;
gchar *data_dir; gchar *data_dir;
gchar *ui_file; gchar *ui_file;
GtkWidget *content;
XedSpellPluginAutocheckType autocheck_type; XedSpellPluginAutocheckType autocheck_type;
GtkWidget *error_widget; GtkWidget *error_widget;
gboolean ret; gboolean ret;
@ -750,37 +770,15 @@ get_configure_dialog (XedSpellPlugin *plugin)
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
GtkWidget *dlg = gtk_dialog_new_with_buttons (_("Configure Spell Checker plugin..."), dialog = g_slice_new (SpellConfigureDialog);
NULL, dialog->settings = g_object_ref (plugin->priv->settings);
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
GTK_STOCK_HELP,
GTK_RESPONSE_HELP,
NULL);
g_return_val_if_fail (dlg != NULL, NULL); data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
dialog = g_new0 (SpellConfigureDialog, 1);
dialog->dialog = dlg;
/* HIG defaults */
gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog->dialog)), 5);
gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog->dialog))),
2); /* 2 * 5 + 2 = 12 */
gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (dialog->dialog))),
5);
gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog->dialog))), 6);
data_dir = xed_plugin_get_data_dir (XED_PLUGIN (plugin));
ui_file = g_build_filename (data_dir, "xed-spell-setup-dialog.ui", NULL); ui_file = g_build_filename (data_dir, "xed-spell-setup-dialog.ui", NULL);
ret = xed_utils_get_ui_objects (ui_file, ret = xed_utils_get_ui_objects (ui_file,
root_objects, root_objects,
&error_widget, &error_widget,
"spell_dialog_content", &content, "spell_dialog_content", &dialog->content,
"autocheck_never", &dialog->never, "autocheck_never", &dialog->never,
"autocheck_document", &dialog->document, "autocheck_document", &dialog->document,
"autocheck_always", &dialog->always, "autocheck_always", &dialog->always,
@ -791,18 +789,9 @@ get_configure_dialog (XedSpellPlugin *plugin)
if (!ret) if (!ret)
{ {
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog->dialog))), return NULL;
error_widget, TRUE, TRUE, 0);
gtk_container_set_border_width (GTK_CONTAINER (error_widget), 5);
gtk_widget_show (error_widget);
return dialog;
} }
gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE);
autocheck_type = get_autocheck_type (plugin); autocheck_type = get_autocheck_type (plugin);
if (autocheck_type == AUTOCHECK_ALWAYS) if (autocheck_type == AUTOCHECK_ALWAYS)
@ -818,75 +807,46 @@ get_configure_dialog (XedSpellPlugin *plugin)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->never), TRUE); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->never), TRUE);
} }
gtk_window_set_default_size (GTK_WIDGET (content), 15, 120);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog->dialog))),
content, FALSE, FALSE, 0);
g_object_unref (content);
gtk_container_set_border_width (GTK_CONTAINER (content), 5);
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
GTK_RESPONSE_OK);
return dialog; return dialog;
} }
static void static void
ok_button_pressed (SpellConfigureDialog *dialog) configure_dialog_button_toggled (GtkToggleButton *button,
SpellConfigureDialog *dialog)
{ {
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->always))) if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->always)))
{ {
set_autocheck_type (dialog->plugin, AUTOCHECK_ALWAYS); set_autocheck_type (dialog->settings, AUTOCHECK_ALWAYS);
} }
else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->document))) else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->document)))
{ {
set_autocheck_type (dialog->plugin, AUTOCHECK_DOCUMENT); set_autocheck_type (dialog->settings, AUTOCHECK_DOCUMENT);
} }
else else
{ {
set_autocheck_type (dialog->plugin, AUTOCHECK_NEVER); set_autocheck_type (dialog->settings, AUTOCHECK_NEVER);
} }
} }
static void static void
configure_dialog_response_cb (GtkWidget *widget, configure_dialog_destroyed (GtkWidget *widget,
gint response, gpointer data)
SpellConfigureDialog *dialog)
{ {
switch (response) SpellConfigureDialog *dialog = (SpellConfigureDialog *) data;
{
case GTK_RESPONSE_HELP:
{
xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_HELP");
xed_help_display (GTK_WINDOW (widget), xed_debug (DEBUG_PLUGINS);
NULL,
"xed-spell-checker-plugin");
break;
}
case GTK_RESPONSE_OK:
{
xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_OK");
ok_button_pressed (dialog); g_object_unref (dialog->settings);
g_slice_free (SpellConfigureDialog, data);
gtk_widget_destroy (dialog->dialog);
break;
}
case GTK_RESPONSE_CANCEL:
{
xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_CANCEL");
gtk_widget_destroy (dialog->dialog);
}
}
} }
static void static void
set_language_cb (GtkAction *action, set_language_cb (GtkAction *action,
ActionData *action_data) XedSpellPlugin *plugin)
{ {
XedWindow *window;
XedDocument *doc; XedDocument *doc;
XedSpellChecker *spell; XedSpellChecker *spell;
const XedSpellCheckerLanguage *lang; const XedSpellCheckerLanguage *lang;
@ -896,7 +856,8 @@ set_language_cb (GtkAction *action,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
doc = xed_window_get_active_document (action_data->window); window = XED_WINDOW (plugin->priv->window);
doc = xed_window_get_active_document (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
spell = get_spell_checker_from_document (doc); spell = get_spell_checker_from_document (doc);
@ -904,13 +865,13 @@ set_language_cb (GtkAction *action,
lang = xed_spell_checker_get_language (spell); lang = xed_spell_checker_get_language (spell);
data_dir = xed_plugin_get_data_dir (action_data->plugin); data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
dlg = xed_spell_language_dialog_new (GTK_WINDOW (action_data->window), dlg = xed_spell_language_dialog_new (GTK_WINDOW (window),
lang, lang,
data_dir); data_dir);
g_free (data_dir); g_free (data_dir);
wg = xed_window_get_group (action_data->window); wg = xed_window_get_group (window);
gtk_window_group_add_window (wg, GTK_WINDOW (dlg)); gtk_window_group_add_window (wg, GTK_WINDOW (dlg));
@ -926,8 +887,10 @@ set_language_cb (GtkAction *action,
static void static void
spell_cb (GtkAction *action, spell_cb (GtkAction *action,
ActionData *action_data) XedSpellPlugin *plugin)
{ {
XedSpellPluginPrivate *data;
XedWindow *window;
XedView *view; XedView *view;
XedDocument *doc; XedDocument *doc;
XedSpellChecker *spell; XedSpellChecker *spell;
@ -938,7 +901,9 @@ spell_cb (GtkAction *action,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
view = xed_window_get_active_view (action_data->window); data = plugin->priv;
window = XED_WINDOW (data->window);
view = xed_window_get_active_view (window);
g_return_if_fail (view != NULL); g_return_if_fail (view != NULL);
doc = XED_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view))); doc = XED_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
@ -949,14 +914,9 @@ spell_cb (GtkAction *action,
if (gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (doc)) <= 0) if (gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (doc)) <= 0)
{ {
WindowData *data;
GtkWidget *statusbar; GtkWidget *statusbar;
data = (WindowData *) g_object_get_data (G_OBJECT (action_data->window), statusbar = xed_window_get_statusbar (window);
WINDOW_DATA_KEY);
g_return_if_fail (data != NULL);
statusbar = xed_window_get_statusbar (action_data->window);
xed_statusbar_flash_message (XED_STATUSBAR (statusbar), xed_statusbar_flash_message (XED_STATUSBAR (statusbar),
data->message_cid, data->message_cid,
_("The document is empty.")); _("The document is empty."));
@ -979,14 +939,9 @@ spell_cb (GtkAction *action,
word = get_next_misspelled_word (view); word = get_next_misspelled_word (view);
if (word == NULL) if (word == NULL)
{ {
WindowData *data;
GtkWidget *statusbar; GtkWidget *statusbar;
data = (WindowData *) g_object_get_data (G_OBJECT (action_data->window), statusbar = xed_window_get_statusbar (window);
WINDOW_DATA_KEY);
g_return_if_fail (data != NULL);
statusbar = xed_window_get_statusbar (action_data->window);
xed_statusbar_flash_message (XED_STATUSBAR (statusbar), xed_statusbar_flash_message (XED_STATUSBAR (statusbar),
data->message_cid, data->message_cid,
_("No misspelled words")); _("No misspelled words"));
@ -994,12 +949,13 @@ spell_cb (GtkAction *action,
return; return;
} }
data_dir = xed_plugin_get_data_dir (action_data->plugin); data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
dlg = xed_spell_checker_dialog_new_from_spell_checker (spell, data_dir); dlg = xed_spell_checker_dialog_new_from_spell_checker (spell, data_dir);
g_free (data_dir); g_free (data_dir);
gtk_window_set_modal (GTK_WINDOW (dlg), TRUE); gtk_window_set_modal (GTK_WINDOW (dlg), TRUE);
gtk_window_set_transient_for (GTK_WINDOW (dlg), gtk_window_set_transient_for (GTK_WINDOW (dlg),
GTK_WINDOW (action_data->window)); GTK_WINDOW (window));
g_signal_connect (dlg, "ignore", G_CALLBACK (ignore_cb), view); g_signal_connect (dlg, "ignore", G_CALLBACK (ignore_cb), view);
g_signal_connect (dlg, "ignore_all", G_CALLBACK (ignore_cb), view); g_signal_connect (dlg, "ignore_all", G_CALLBACK (ignore_cb), view);
@ -1054,15 +1010,16 @@ set_auto_spell (XedWindow *window,
static void static void
auto_spell_cb (GtkAction *action, auto_spell_cb (GtkAction *action,
XedWindow *window) XedSpellPlugin *plugin)
{ {
XedWindow *window;
XedDocument *doc; XedDocument *doc;
gboolean active; gboolean active;
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
window = XED_WINDOW (plugin->priv->window);
active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)); active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
xed_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated"); xed_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated");
@ -1071,12 +1028,8 @@ auto_spell_cb (GtkAction *action,
if (doc == NULL) if (doc == NULL)
return; return;
data = g_object_get_data (G_OBJECT (window), if (get_autocheck_type (plugin) == AUTOCHECK_DOCUMENT)
WINDOW_DATA_KEY);
if (get_autocheck_type(data->plugin) == AUTOCHECK_DOCUMENT)
{ {
xed_document_set_metadata (doc, xed_document_set_metadata (doc,
XED_METADATA_ATTRIBUTE_SPELL_ENABLED, XED_METADATA_ATTRIBUTE_SPELL_ENABLED,
active ? "1" : NULL, NULL); active ? "1" : NULL, NULL);
@ -1086,26 +1039,10 @@ auto_spell_cb (GtkAction *action,
} }
static void static void
free_window_data (WindowData *data) update_ui (XedSpellPlugin *plugin)
{
g_return_if_fail (data != NULL);
g_object_unref (data->action_group);
g_slice_free (WindowData, data);
}
static void
free_action_data (gpointer data)
{
g_return_if_fail (data != NULL);
g_slice_free (ActionData, data);
}
static void
update_ui_real (XedWindow *window,
WindowData *data)
{ {
XedSpellPluginPrivate *data;
XedWindow *window;
XedDocument *doc; XedDocument *doc;
XedView *view; XedView *view;
gboolean autospell; gboolean autospell;
@ -1113,6 +1050,8 @@ update_ui_real (XedWindow *window,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = plugin->priv;
window = XED_WINDOW (data->window);
doc = xed_window_get_active_document (window); doc = xed_window_get_active_document (window);
view = xed_window_get_active_view (window); view = xed_window_get_active_view (window);
@ -1135,12 +1074,12 @@ update_ui_real (XedWindow *window,
"AutoSpell"); "AutoSpell");
g_signal_handlers_block_by_func (action, auto_spell_cb, g_signal_handlers_block_by_func (action, auto_spell_cb,
window); plugin);
set_auto_spell (window, doc, autospell); set_auto_spell (window, doc, autospell);
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
autospell); autospell);
g_signal_handlers_unblock_by_func (action, auto_spell_cb, g_signal_handlers_unblock_by_func (action, auto_spell_cb,
window); plugin);
} }
} }
@ -1150,20 +1089,16 @@ update_ui_real (XedWindow *window,
} }
static void static void
set_auto_spell_from_metadata (XedWindow *window, set_auto_spell_from_metadata (XedSpellPlugin *plugin,
XedDocument *doc, XedDocument *doc,
GtkActionGroup *action_group) GtkActionGroup *action_group)
{ {
gboolean active = FALSE; gboolean active = FALSE;
gchar *active_str = NULL; gchar *active_str = NULL;
XedWindow *window;
XedDocument *active_doc; XedDocument *active_doc;
XedSpellPluginAutocheckType autocheck_type; XedSpellPluginAutocheckType autocheck_type;
WindowData *data; autocheck_type = get_autocheck_type (plugin);
data = g_object_get_data (G_OBJECT (window),
WINDOW_DATA_KEY);
autocheck_type = get_autocheck_type(data->plugin);
switch (autocheck_type) switch (autocheck_type)
{ {
@ -1191,6 +1126,8 @@ set_auto_spell_from_metadata (XedWindow *window,
g_free (active_str); g_free (active_str);
} }
window = XED_WINDOW (plugin->priv->window);
set_auto_spell (window, doc, active); set_auto_spell (window, doc, active);
/* In case that the doc is the active one we mark the spell action */ /* In case that the doc is the active one we mark the spell action */
@ -1204,22 +1141,21 @@ set_auto_spell_from_metadata (XedWindow *window,
"AutoSpell"); "AutoSpell");
g_signal_handlers_block_by_func (action, auto_spell_cb, g_signal_handlers_block_by_func (action, auto_spell_cb,
window); plugin);
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
active); active);
g_signal_handlers_unblock_by_func (action, auto_spell_cb, g_signal_handlers_unblock_by_func (action, auto_spell_cb,
window); plugin);
} }
} }
static void static void
on_document_loaded (XedDocument *doc, on_document_loaded (XedDocument *doc,
const GError *error, const GError *error,
XedWindow *window) XedSpellPlugin *plugin)
{ {
if (error == NULL) if (error == NULL)
{ {
WindowData *data;
XedSpellChecker *spell; XedSpellChecker *spell;
spell = XED_SPELL_CHECKER (g_object_get_qdata (G_OBJECT (doc), spell = XED_SPELL_CHECKER (g_object_get_qdata (G_OBJECT (doc),
@ -1229,22 +1165,18 @@ on_document_loaded (XedDocument *doc,
set_language_from_metadata (spell, doc); set_language_from_metadata (spell, doc);
} }
data = g_object_get_data (G_OBJECT (window), set_auto_spell_from_metadata (plugin, doc, plugin->priv->action_group);
WINDOW_DATA_KEY);
set_auto_spell_from_metadata (window, doc, data->action_group);
} }
} }
static void static void
on_document_saved (XedDocument *doc, on_document_saved (XedDocument *doc,
const GError *error, const GError *error,
XedWindow *window) XedSpellPlugin *plugin)
{ {
XedAutomaticSpellChecker *autospell; XedAutomaticSpellChecker *autospell;
XedSpellChecker *spell; XedSpellChecker *spell;
const gchar *key; const gchar *key;
WindowData *data;
if (error != NULL) if (error != NULL)
{ {
@ -1264,10 +1196,7 @@ on_document_saved (XedDocument *doc,
key = NULL; key = NULL;
} }
data = g_object_get_data (G_OBJECT (window), if (get_autocheck_type (plugin) == AUTOCHECK_DOCUMENT)
WINDOW_DATA_KEY);
if (get_autocheck_type(data->plugin) == AUTOCHECK_DOCUMENT)
{ {
xed_document_set_metadata (doc, xed_document_set_metadata (doc,
@ -1289,7 +1218,7 @@ on_document_saved (XedDocument *doc,
static void static void
tab_added_cb (XedWindow *window, tab_added_cb (XedWindow *window,
XedTab *tab, XedTab *tab,
gpointer useless) XedSpellPlugin *plugin)
{ {
XedDocument *doc; XedDocument *doc;
@ -1297,57 +1226,54 @@ tab_added_cb (XedWindow *window,
g_signal_connect (doc, "loaded", g_signal_connect (doc, "loaded",
G_CALLBACK (on_document_loaded), G_CALLBACK (on_document_loaded),
window); plugin);
g_signal_connect (doc, "saved", g_signal_connect (doc, "saved",
G_CALLBACK (on_document_saved), G_CALLBACK (on_document_saved),
window); plugin);
} }
static void static void
tab_removed_cb (XedWindow *window, tab_removed_cb (XedWindow *window,
XedTab *tab, XedTab *tab,
gpointer useless) XedSpellPlugin *plugin)
{ {
XedDocument *doc; XedDocument *doc;
doc = xed_tab_get_document (tab); doc = xed_tab_get_document (tab);
g_signal_handlers_disconnect_by_func (doc, on_document_loaded, window); g_signal_handlers_disconnect_by_func (doc, on_document_loaded, plugin);
g_signal_handlers_disconnect_by_func (doc, on_document_saved, window); g_signal_handlers_disconnect_by_func (doc, on_document_saved, plugin);
} }
static void static void
impl_activate (XedPlugin *plugin, xed_spell_plugin_activate (PeasActivatable *activatable)
XedWindow *window)
{ {
XedSpellPlugin *plugin;
XedSpellPluginPrivate *data;
XedWindow *window;
GtkUIManager *manager; GtkUIManager *manager;
WindowData *data;
ActionData *action_data;
GList *docs, *l; GList *docs, *l;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = g_slice_new (WindowData); plugin = XED_SPELL_PLUGIN (activatable);
data->plugin = XED_SPELL_PLUGIN (plugin); data = plugin->priv;
action_data = g_slice_new (ActionData); window = XED_WINDOW (data->window);
action_data->plugin = plugin;
action_data->window = window;
manager = xed_window_get_ui_manager (window); manager = xed_window_get_ui_manager (window);
data->action_group = gtk_action_group_new ("XedSpellPluginActions"); data->action_group = gtk_action_group_new ("XedSpellPluginActions");
gtk_action_group_set_translation_domain (data->action_group, gtk_action_group_set_translation_domain (data->action_group,
GETTEXT_PACKAGE); GETTEXT_PACKAGE);
gtk_action_group_add_actions_full (data->action_group, gtk_action_group_add_actions (data->action_group,
action_entries, action_entries,
G_N_ELEMENTS (action_entries), G_N_ELEMENTS (action_entries),
action_data, plugin);
(GDestroyNotify) free_action_data);
gtk_action_group_add_toggle_actions (data->action_group, gtk_action_group_add_toggle_actions (data->action_group,
toggle_action_entries, toggle_action_entries,
G_N_ELEMENTS (toggle_action_entries), G_N_ELEMENTS (toggle_action_entries),
window); plugin);
gtk_ui_manager_insert_action_group (manager, data->action_group, -1); gtk_ui_manager_insert_action_group (manager, data->action_group, -1);
@ -1357,11 +1283,6 @@ impl_activate (XedPlugin *plugin,
(GTK_STATUSBAR (xed_window_get_statusbar (window)), (GTK_STATUSBAR (xed_window_get_statusbar (window)),
"spell_plugin_message"); "spell_plugin_message");
g_object_set_data_full (G_OBJECT (window),
WINDOW_DATA_KEY,
data,
(GDestroyNotify) free_window_data);
gtk_ui_manager_add_ui (manager, gtk_ui_manager_add_ui (manager,
data->ui_id, data->ui_id,
MENU_PATH, MENU_PATH,
@ -1386,100 +1307,125 @@ impl_activate (XedPlugin *plugin,
GTK_UI_MANAGER_MENUITEM, GTK_UI_MANAGER_MENUITEM,
FALSE); FALSE);
update_ui_real (window, data); update_ui (plugin);
docs = xed_window_get_documents (window); docs = xed_window_get_documents (window);
for (l = docs; l != NULL; l = g_list_next (l)) for (l = docs; l != NULL; l = g_list_next (l))
{ {
XedDocument *doc = XED_DOCUMENT (l->data); XedDocument *doc = XED_DOCUMENT (l->data);
set_auto_spell_from_metadata (window, doc, set_auto_spell_from_metadata (plugin, doc,
data->action_group); data->action_group);
g_signal_handlers_disconnect_by_func (doc, g_signal_handlers_disconnect_by_func (doc,
on_document_loaded, on_document_loaded,
window); plugin);
g_signal_handlers_disconnect_by_func (doc, g_signal_handlers_disconnect_by_func (doc,
on_document_saved, on_document_saved,
window); plugin);
} }
data->tab_added_id = data->tab_added_id =
g_signal_connect (window, "tab-added", g_signal_connect (window, "tab-added",
G_CALLBACK (tab_added_cb), NULL); G_CALLBACK (tab_added_cb), plugin);
data->tab_removed_id = data->tab_removed_id =
g_signal_connect (window, "tab-removed", g_signal_connect (window, "tab-removed",
G_CALLBACK (tab_removed_cb), NULL); G_CALLBACK (tab_removed_cb), plugin);
} }
static void static void
impl_deactivate (XedPlugin *plugin, xed_spell_plugin_deactivate (PeasActivatable *activatable)
XedWindow *window)
{ {
XedSpellPluginPrivate *data;
XedWindow *window;
GtkUIManager *manager; GtkUIManager *manager;
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
manager = xed_window_get_ui_manager (window); data = XED_SPELL_PLUGIN (activatable)->priv;
window = XED_WINDOW (data->window);
data = (WindowData *) g_object_get_data (G_OBJECT (window), WINDOW_DATA_KEY); manager = xed_window_get_ui_manager (window);
g_return_if_fail (data != NULL);
gtk_ui_manager_remove_ui (manager, data->ui_id); gtk_ui_manager_remove_ui (manager, data->ui_id);
gtk_ui_manager_remove_action_group (manager, data->action_group); gtk_ui_manager_remove_action_group (manager, data->action_group);
g_signal_handler_disconnect (window, data->tab_added_id); g_signal_handler_disconnect (window, data->tab_added_id);
g_signal_handler_disconnect (window, data->tab_removed_id); g_signal_handler_disconnect (window, data->tab_removed_id);
g_object_set_data (G_OBJECT (window), WINDOW_DATA_KEY, NULL);
} }
static void static void
impl_update_ui (XedPlugin *plugin, xed_spell_plugin_update_state (PeasActivatable *activatable)
XedWindow *window)
{ {
WindowData *data; xed_debug (DEBUG_PLUGINS);
xed_debug (DEBUG_PLUGINS); update_ui (XED_SPELL_PLUGIN (activatable));
}
data = (WindowData *) g_object_get_data (G_OBJECT (window), WINDOW_DATA_KEY); static void
g_return_if_fail (data != NULL); xed_spell_plugin_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
XedSpellPlugin *plugin = XED_SPELL_PLUGIN (object);
update_ui_real (window, data); switch (prop_id)
{
case PROP_OBJECT:
plugin->priv->window = GTK_WIDGET (g_value_dup_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
xed_spell_plugin_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XedSpellPlugin *plugin = XED_SPELL_PLUGIN (object);
switch (prop_id)
{
case PROP_OBJECT:
g_value_set_object (value, plugin->priv->window);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
} }
static GtkWidget * static GtkWidget *
impl_create_configure_dialog (XedPlugin *plugin) xed_spell_plugin_create_configure_widget (PeasGtkConfigurable *configurable)
{ {
SpellConfigureDialog *dialog; SpellConfigureDialog *dialog;
dialog = get_configure_dialog(XED_SPELL_PLUGIN (plugin)); dialog = get_configure_dialog (XED_SPELL_PLUGIN (configurable));
dialog->plugin = XED_SPELL_PLUGIN (plugin); g_signal_connect (dialog->always, "toggled", G_CALLBACK (configure_dialog_button_toggled), dialog);
g_signal_connect (dialog->document, "toggled", G_CALLBACK (configure_dialog_button_toggled), dialog);
g_signal_connect (dialog->never, "toggled", G_CALLBACK (configure_dialog_button_toggled), dialog);
g_signal_connect (dialog->content, "destroy", G_CALLBACK (configure_dialog_destroyed), dialog);
g_signal_connect (dialog->dialog, return dialog->content;
"response",
G_CALLBACK (configure_dialog_response_cb),
dialog);
return GTK_WIDGET (dialog->dialog);
} }
static void static void
xed_spell_plugin_class_init (XedSpellPluginClass *klass) xed_spell_plugin_class_init (XedSpellPluginClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
XedPluginClass *plugin_class = XED_PLUGIN_CLASS (klass);
object_class->finalize = xed_spell_plugin_finalize; object_class->dispose = xed_spell_plugin_dispose;
object_class->set_property = xed_spell_plugin_set_property;
object_class->get_property = xed_spell_plugin_get_property;
plugin_class->activate = impl_activate; g_object_class_override_property (object_class, PROP_OBJECT, "object");
plugin_class->deactivate = impl_deactivate;
plugin_class->update_ui = impl_update_ui;
plugin_class->create_configure_dialog = impl_create_configure_dialog;
if (spell_checker_id == 0) if (spell_checker_id == 0)
spell_checker_id = g_quark_from_string ("XedSpellCheckerID"); spell_checker_id = g_quark_from_string ("XedSpellCheckerID");
@ -1489,3 +1435,37 @@ xed_spell_plugin_class_init (XedSpellPluginClass *klass)
g_type_class_add_private (object_class, sizeof (XedSpellPluginPrivate)); g_type_class_add_private (object_class, sizeof (XedSpellPluginPrivate));
} }
static void
xed_spell_plugin_class_finalize (XedSpellPluginClass *klass)
{
/* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */
}
static void
peas_activatable_iface_init (PeasActivatableInterface *iface)
{
iface->activate = xed_spell_plugin_activate;
iface->deactivate = xed_spell_plugin_deactivate;
iface->update_state = xed_spell_plugin_update_state;
}
static void
peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface)
{
iface->create_configure_widget = xed_spell_plugin_create_configure_widget;
}
G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
xed_spell_plugin_register_type (G_TYPE_MODULE (module));
peas_object_module_register_extension_type (module,
PEAS_TYPE_ACTIVATABLE,
XED_TYPE_SPELL_PLUGIN);
peas_object_module_register_extension_type (module,
PEAS_GTK_TYPE_CONFIGURABLE,
XED_TYPE_SPELL_PLUGIN);
}

View File

@ -25,52 +25,53 @@
#include <glib.h> #include <glib.h>
#include <glib-object.h> #include <glib-object.h>
#include <xed/xed-plugin.h> #include <libpeas/peas-extension-base.h>
#include <libpeas/peas-object-module.h>
G_BEGIN_DECLS G_BEGIN_DECLS
/* /*
* Type checking and casting macros * Type checking and casting macros
*/ */
#define XED_TYPE_SPELL_PLUGIN (xed_spell_plugin_get_type ()) #define XED_TYPE_SPELL_PLUGIN (xed_spell_plugin_get_type ())
#define XED_SPELL_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XED_TYPE_SPELL_PLUGIN, XedSpellPlugin)) #define XED_SPELL_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XED_TYPE_SPELL_PLUGIN, XedSpellPlugin))
#define XED_SPELL_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), XED_TYPE_SPELL_PLUGIN, XedSpellPluginClass)) #define XED_SPELL_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), XED_TYPE_SPELL_PLUGIN, XedSpellPluginClass))
#define XED_IS_SPELL_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XED_TYPE_SPELL_PLUGIN)) #define XED_IS_SPELL_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XED_TYPE_SPELL_PLUGIN))
#define XED_IS_SPELL_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), XED_TYPE_SPELL_PLUGIN)) #define XED_IS_SPELL_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), XED_TYPE_SPELL_PLUGIN))
#define XED_SPELL_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_SPELL_PLUGIN, XedSpellPluginClass)) #define XED_SPELL_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_SPELL_PLUGIN, XedSpellPluginClass))
/* Private structure type */ /* Private structure type */
typedef struct _XedSpellPluginPrivate XedSpellPluginPrivate; typedef struct _XedSpellPluginPrivate XedSpellPluginPrivate;
/* /*
* Main object structure * Main object structure
*/ */
typedef struct _XedSpellPlugin XedSpellPlugin; typedef struct _XedSpellPlugin XedSpellPlugin;
struct _XedSpellPlugin struct _XedSpellPlugin
{ {
XedPlugin parent_instance; PeasExtensionBase parent_instance;
XedSpellPluginPrivate *priv; XedSpellPluginPrivate *priv;
}; };
/* /*
* Class definition * Class definition
*/ */
typedef struct _XedSpellPluginClass XedSpellPluginClass; typedef struct _XedSpellPluginClass XedSpellPluginClass;
struct _XedSpellPluginClass struct _XedSpellPluginClass
{ {
XedPluginClass parent_class; PeasExtensionBaseClass parent_class;
}; };
/* /*
* Public methods * Public methods
*/ */
GType xed_spell_plugin_get_type (void) G_GNUC_CONST; GType xed_spell_plugin_get_type (void) G_GNUC_CONST;
/* All the plugins must implement this function */ /* All the plugins must implement this function */
G_MODULE_EXPORT GType register_xed_plugin (GTypeModule *module); G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
G_END_DECLS G_END_DECLS