From fdc2da63a90308b6d6f07c603d61c06edba61051 Mon Sep 17 00:00:00 2001 From: JosephMcc Date: Mon, 26 Dec 2016 13:13:01 -0800 Subject: [PATCH] docinfo-plugin: Port to libpeas Adapted from: https://github.com/mate-desktop/pluma/commit/9ca3dda49113bcc40b726ebf51db7f07d69e96ad --- plugins/docinfo/xed-docinfo-plugin.c | 846 ++++++++++++++------------- plugins/docinfo/xed-docinfo-plugin.h | 36 +- 2 files changed, 460 insertions(+), 422 deletions(-) diff --git a/plugins/docinfo/xed-docinfo-plugin.c b/plugins/docinfo/xed-docinfo-plugin.c index fc7b8df..a8337bd 100644 --- a/plugins/docinfo/xed-docinfo-plugin.c +++ b/plugins/docinfo/xed-docinfo-plugin.c @@ -1,13 +1,13 @@ /* * xed-docinfo-plugin.c - * - * Copyright (C) 2002-2005 Paolo Maggi + * + * Copyright (C) 2002-2005 Paolo Maggi * * 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, 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 @@ -31,551 +31,585 @@ #include #include #include +#include +#include #include #include -#define WINDOW_DATA_KEY "XedDocInfoWindowData" #define MENU_PATH "/MenuBar/ToolsMenu/ToolsOps_2" -XED_PLUGIN_REGISTER_TYPE(XedDocInfoPlugin, xed_docinfo_plugin) +#define XED_DOCINFO_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \ + XED_TYPE_DOCINFO_PLUGIN, \ + XedDocInfoPluginPrivate)) + +static void peas_activatable_iface_init (PeasActivatableInterface *iface); + +G_DEFINE_DYNAMIC_TYPE_EXTENDED (XedDocInfoPlugin, + xed_docinfo_plugin, + PEAS_TYPE_EXTENSION_BASE, + 0, + G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_TYPE_ACTIVATABLE, + peas_activatable_iface_init)) typedef struct { - GtkWidget *dialog; - GtkWidget *file_name_label; - GtkWidget *lines_label; - GtkWidget *words_label; - GtkWidget *chars_label; - GtkWidget *chars_ns_label; - GtkWidget *bytes_label; - GtkWidget *selection_vbox; - GtkWidget *selected_lines_label; - GtkWidget *selected_words_label; - GtkWidget *selected_chars_label; - GtkWidget *selected_chars_ns_label; - GtkWidget *selected_bytes_label; + GtkWidget *dialog; + GtkWidget *file_name_label; + GtkWidget *lines_label; + GtkWidget *words_label; + GtkWidget *chars_label; + GtkWidget *chars_ns_label; + GtkWidget *bytes_label; + GtkWidget *selection_vbox; + GtkWidget *selected_lines_label; + GtkWidget *selected_words_label; + GtkWidget *selected_chars_label; + GtkWidget *selected_chars_ns_label; + GtkWidget *selected_bytes_label; } DocInfoDialog; -typedef struct +struct _XedDocInfoPluginPrivate { - XedPlugin *plugin; + GtkWidget *window; - GtkActionGroup *ui_action_group; - guint ui_id; + GtkActionGroup *ui_action_group; + guint ui_id; - DocInfoDialog *dialog; -} WindowData; + DocInfoDialog *dialog; +}; -static void docinfo_dialog_response_cb (GtkDialog *widget, - gint res_id, - XedWindow *window); +enum +{ + PROP_0, + PROP_OBJECT +}; + +static void docinfo_dialog_response_cb (GtkDialog *widget, + gint res_id, + XedDocInfoPluginPrivate *data); static void -docinfo_dialog_destroy_cb (GObject *obj, - WindowData *data) +docinfo_dialog_destroy_cb (GObject *obj, + XedDocInfoPluginPrivate *data) { - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - if (data != NULL) - { - g_free (data->dialog); - data->dialog = NULL; - } + if (data != NULL) + { + g_free (data->dialog); + data->dialog = NULL; + } } static DocInfoDialog * -get_docinfo_dialog (XedWindow *window, - WindowData *data) +get_docinfo_dialog (XedDocInfoPlugin *plugin) { - DocInfoDialog *dialog; - gchar *data_dir; - gchar *ui_file; - GtkWidget *content; - GtkWidget *error_widget; - gboolean ret; + XedDocInfoPluginPrivate *data; + XedWindow *window; + DocInfoDialog *dialog; + gchar *data_dir; + gchar *ui_file; + GtkWidget *content; + GtkWidget *error_widget; + gboolean ret; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - dialog = g_new (DocInfoDialog, 1); + data = plugin->priv; + window = XED_WINDOW (data->window); - data_dir = xed_plugin_get_data_dir (data->plugin); - ui_file = g_build_filename (data_dir, "docinfo.ui", NULL); - ret = xed_utils_get_ui_objects (ui_file, - NULL, - &error_widget, - "dialog", &dialog->dialog, - "docinfo_dialog_content", &content, - "file_name_label", &dialog->file_name_label, - "words_label", &dialog->words_label, - "bytes_label", &dialog->bytes_label, - "lines_label", &dialog->lines_label, - "chars_label", &dialog->chars_label, - "chars_ns_label", &dialog->chars_ns_label, - "selection_vbox", &dialog->selection_vbox, - "selected_words_label", &dialog->selected_words_label, - "selected_bytes_label", &dialog->selected_bytes_label, - "selected_lines_label", &dialog->selected_lines_label, - "selected_chars_label", &dialog->selected_chars_label, - "selected_chars_ns_label", &dialog->selected_chars_ns_label, - NULL); + dialog = g_new (DocInfoDialog, 1); - g_free (data_dir); - g_free (ui_file); + data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin)); + ui_file = g_build_filename (data_dir, "docinfo.ui", NULL); + ret = xed_utils_get_ui_objects (ui_file, + NULL, + &error_widget, + "dialog", &dialog->dialog, + "docinfo_dialog_content", &content, + "file_name_label", &dialog->file_name_label, + "words_label", &dialog->words_label, + "bytes_label", &dialog->bytes_label, + "lines_label", &dialog->lines_label, + "chars_label", &dialog->chars_label, + "chars_ns_label", &dialog->chars_ns_label, + "selection_vbox", &dialog->selection_vbox, + "selected_words_label", &dialog->selected_words_label, + "selected_bytes_label", &dialog->selected_bytes_label, + "selected_lines_label", &dialog->selected_lines_label, + "selected_chars_label", &dialog->selected_chars_label, + "selected_chars_ns_label", &dialog->selected_chars_ns_label, + NULL); - if (!ret) - { - const gchar *err_message; + g_free (data_dir); + g_free (ui_file); - err_message = gtk_label_get_label (GTK_LABEL (error_widget)); - xed_warning (GTK_WINDOW (window), "%s", err_message); + if (!ret) + { + const gchar *err_message; - g_free (dialog); - gtk_widget_destroy (error_widget); + err_message = gtk_label_get_label (GTK_LABEL (error_widget)); + xed_warning (GTK_WINDOW (window), "%s", err_message); - return NULL; - } + g_free (dialog); + gtk_widget_destroy (error_widget); - gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog), - GTK_RESPONSE_OK); - gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), - GTK_WINDOW (window)); - - g_signal_connect (dialog->dialog, - "destroy", - G_CALLBACK (docinfo_dialog_destroy_cb), - data); + return NULL; + } - g_signal_connect (dialog->dialog, - "response", - G_CALLBACK (docinfo_dialog_response_cb), - window); + gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog), GTK_RESPONSE_OK); + gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), GTK_WINDOW (window)); - return dialog; + g_signal_connect (dialog->dialog, "destroy", G_CALLBACK (docinfo_dialog_destroy_cb), data); + g_signal_connect (dialog->dialog, "response", G_CALLBACK (docinfo_dialog_response_cb), data); + + return dialog; } -static void +static void calculate_info (XedDocument *doc, - GtkTextIter *start, - GtkTextIter *end, - gint *chars, - gint *words, - gint *white_chars, - gint *bytes) -{ - gchar *text; + GtkTextIter *start, + GtkTextIter *end, + gint *chars, + gint *words, + gint *white_chars, + gint *bytes) +{ + gchar *text; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), - start, - end, - TRUE); + text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), start, end, TRUE); - *chars = g_utf8_strlen (text, -1); - *bytes = strlen (text); + *chars = g_utf8_strlen (text, -1); + *bytes = strlen (text); - if (*chars > 0) - { - PangoLogAttr *attrs; - gint i; + if (*chars > 0) + { + PangoLogAttr *attrs; + gint i; - attrs = g_new0 (PangoLogAttr, *chars + 1); + attrs = g_new0 (PangoLogAttr, *chars + 1); - pango_get_log_attrs (text, - -1, - 0, - pango_language_from_string ("C"), - attrs, - *chars + 1); + pango_get_log_attrs (text, -1, 0, pango_language_from_string ("C"), attrs, *chars + 1); - for (i = 0; i < (*chars); i++) - { - if (attrs[i].is_white) - ++(*white_chars); + for (i = 0; i < (*chars); i++) + { + if (attrs[i].is_white) + { + ++(*white_chars); + } - if (attrs[i].is_word_start) - ++(*words); - } + if (attrs[i].is_word_start) + { + ++(*words); + } + } - g_free (attrs); - } - else - { - *white_chars = 0; - *words = 0; - } + g_free (attrs); + } + else + { + *white_chars = 0; + *words = 0; + } - g_free (text); + g_free (text); } static void -docinfo_real (XedDocument *doc, - DocInfoDialog *dialog) +docinfo_real (XedDocument *doc, + DocInfoDialog *dialog) { - GtkTextIter start, end; - gint words = 0; - gint chars = 0; - gint white_chars = 0; - gint lines = 0; - gint bytes = 0; - gchar *tmp_str; - gchar *doc_name; + GtkTextIter start, end; + gint words = 0; + gint chars = 0; + gint white_chars = 0; + gint lines = 0; + gint bytes = 0; + gchar *tmp_str; + gchar *doc_name; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), - &start, - &end); + gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), &start, &end); - lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (doc)); + lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (doc)); - calculate_info (doc, - &start, &end, - &chars, &words, &white_chars, &bytes); + calculate_info (doc, &start, &end, &chars, &words, &white_chars, &bytes); - if (chars == 0) - lines = 0; + if (chars == 0) + { + lines = 0; + } - xed_debug_message (DEBUG_PLUGINS, "Chars: %d", chars); - xed_debug_message (DEBUG_PLUGINS, "Lines: %d", lines); - xed_debug_message (DEBUG_PLUGINS, "Words: %d", words); - xed_debug_message (DEBUG_PLUGINS, "Chars non-space: %d", chars - white_chars); - xed_debug_message (DEBUG_PLUGINS, "Bytes: %d", bytes); + xed_debug_message (DEBUG_PLUGINS, "Chars: %d", chars); + xed_debug_message (DEBUG_PLUGINS, "Lines: %d", lines); + xed_debug_message (DEBUG_PLUGINS, "Words: %d", words); + xed_debug_message (DEBUG_PLUGINS, "Chars non-space: %d", chars - white_chars); + xed_debug_message (DEBUG_PLUGINS, "Bytes: %d", bytes); - doc_name = xed_document_get_short_name_for_display (doc); - tmp_str = g_strdup_printf ("%s", doc_name); - gtk_label_set_markup (GTK_LABEL (dialog->file_name_label), tmp_str); - g_free (doc_name); - g_free (tmp_str); + doc_name = xed_document_get_short_name_for_display (doc); + tmp_str = g_strdup_printf ("%s", doc_name); + gtk_label_set_markup (GTK_LABEL (dialog->file_name_label), tmp_str); + g_free (doc_name); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", lines); - gtk_label_set_text (GTK_LABEL (dialog->lines_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", lines); + gtk_label_set_text (GTK_LABEL (dialog->lines_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", words); - gtk_label_set_text (GTK_LABEL (dialog->words_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", words); + gtk_label_set_text (GTK_LABEL (dialog->words_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", chars); - gtk_label_set_text (GTK_LABEL (dialog->chars_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", chars); + gtk_label_set_text (GTK_LABEL (dialog->chars_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", chars - white_chars); - gtk_label_set_text (GTK_LABEL (dialog->chars_ns_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", chars - white_chars); + gtk_label_set_text (GTK_LABEL (dialog->chars_ns_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", bytes); - gtk_label_set_text (GTK_LABEL (dialog->bytes_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", bytes); + gtk_label_set_text (GTK_LABEL (dialog->bytes_label), tmp_str); + g_free (tmp_str); } static void -selectioninfo_real (XedDocument *doc, - DocInfoDialog *dialog) +selectioninfo_real (XedDocument *doc, + DocInfoDialog *dialog) { - gboolean sel; - GtkTextIter start, end; - gint words = 0; - gint chars = 0; - gint white_chars = 0; - gint lines = 0; - gint bytes = 0; - gchar *tmp_str; + gboolean sel; + GtkTextIter start, end; + gint words = 0; + gint chars = 0; + gint white_chars = 0; + gint lines = 0; + gint bytes = 0; + gchar *tmp_str; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - sel = gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), - &start, - &end); + sel = gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), &start, &end); - if (sel) - { - lines = gtk_text_iter_get_line (&end) - gtk_text_iter_get_line (&start) + 1; - - calculate_info (doc, - &start, &end, - &chars, &words, &white_chars, &bytes); + if (sel) + { + lines = gtk_text_iter_get_line (&end) - gtk_text_iter_get_line (&start) + 1; - xed_debug_message (DEBUG_PLUGINS, "Selected chars: %d", chars); - xed_debug_message (DEBUG_PLUGINS, "Selected lines: %d", lines); - xed_debug_message (DEBUG_PLUGINS, "Selected words: %d", words); - xed_debug_message (DEBUG_PLUGINS, "Selected chars non-space: %d", chars - white_chars); - xed_debug_message (DEBUG_PLUGINS, "Selected bytes: %d", bytes); + calculate_info (doc, &start, &end, &chars, &words, &white_chars, &bytes); - gtk_widget_set_sensitive (dialog->selection_vbox, TRUE); - } - else - { - gtk_widget_set_sensitive (dialog->selection_vbox, FALSE); + xed_debug_message (DEBUG_PLUGINS, "Selected chars: %d", chars); + xed_debug_message (DEBUG_PLUGINS, "Selected lines: %d", lines); + xed_debug_message (DEBUG_PLUGINS, "Selected words: %d", words); + xed_debug_message (DEBUG_PLUGINS, "Selected chars non-space: %d", chars - white_chars); + xed_debug_message (DEBUG_PLUGINS, "Selected bytes: %d", bytes); - xed_debug_message (DEBUG_PLUGINS, "Selection empty"); - } + gtk_widget_set_sensitive (dialog->selection_vbox, TRUE); + } + else + { + gtk_widget_set_sensitive (dialog->selection_vbox, FALSE); - if (chars == 0) - lines = 0; + xed_debug_message (DEBUG_PLUGINS, "Selection empty"); + } - tmp_str = g_strdup_printf("%d", lines); - gtk_label_set_text (GTK_LABEL (dialog->selected_lines_label), tmp_str); - g_free (tmp_str); + if (chars == 0) + { + lines = 0; + } - tmp_str = g_strdup_printf("%d", words); - gtk_label_set_text (GTK_LABEL (dialog->selected_words_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", lines); + gtk_label_set_text (GTK_LABEL (dialog->selected_lines_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", chars); - gtk_label_set_text (GTK_LABEL (dialog->selected_chars_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", words); + gtk_label_set_text (GTK_LABEL (dialog->selected_words_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", chars - white_chars); - gtk_label_set_text (GTK_LABEL (dialog->selected_chars_ns_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", chars); + gtk_label_set_text (GTK_LABEL (dialog->selected_chars_label), tmp_str); + g_free (tmp_str); - tmp_str = g_strdup_printf("%d", bytes); - gtk_label_set_text (GTK_LABEL (dialog->selected_bytes_label), tmp_str); - g_free (tmp_str); + tmp_str = g_strdup_printf("%d", chars - white_chars); + gtk_label_set_text (GTK_LABEL (dialog->selected_chars_ns_label), tmp_str); + g_free (tmp_str); + + tmp_str = g_strdup_printf("%d", bytes); + gtk_label_set_text (GTK_LABEL (dialog->selected_bytes_label), tmp_str); + g_free (tmp_str); } static void -docinfo_cb (GtkAction *action, - XedWindow *window) +docinfo_cb (GtkAction *action, + XedDocInfoPlugin *plugin) { - XedDocument *doc; - WindowData *data; + XedDocInfoPluginPrivate *data; + XedWindow *window; + XedDocument *doc; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - data = (WindowData *) g_object_get_data (G_OBJECT (window), - WINDOW_DATA_KEY); + data = plugin->priv; + window = XED_WINDOW (data->window); + doc = xed_window_get_active_document (window); + g_return_if_fail (doc != NULL); - doc = xed_window_get_active_document (window); - g_return_if_fail (doc != NULL); + if (data->dialog != NULL) + { + gtk_window_present (GTK_WINDOW (data->dialog->dialog)); + gtk_widget_grab_focus (GTK_WIDGET (data->dialog->dialog)); + } + else + { + DocInfoDialog *dialog; - if (data->dialog != NULL) - { - gtk_window_present (GTK_WINDOW (data->dialog->dialog)); - gtk_widget_grab_focus (GTK_WIDGET (data->dialog->dialog)); - } - else - { - DocInfoDialog *dialog; + dialog = get_docinfo_dialog (plugin); + g_return_if_fail (dialog != NULL); - dialog = get_docinfo_dialog (window, data); - g_return_if_fail (dialog != NULL); - - data->dialog = dialog; + data->dialog = dialog; - gtk_widget_show (GTK_WIDGET (dialog->dialog)); - } - - docinfo_real (doc, - data->dialog); - selectioninfo_real (doc, - data->dialog); + gtk_widget_show (GTK_WIDGET (dialog->dialog)); + } + + docinfo_real (doc, + data->dialog); + selectioninfo_real (doc, + data->dialog); } static void -docinfo_dialog_response_cb (GtkDialog *widget, - gint res_id, - XedWindow *window) +docinfo_dialog_response_cb (GtkDialog *widget, + gint res_id, + XedDocInfoPluginPrivate *data) { - WindowData *data; + XedWindow *window; - xed_debug (DEBUG_PLUGINS); - - data = (WindowData *) g_object_get_data (G_OBJECT (window), - WINDOW_DATA_KEY); + xed_debug (DEBUG_PLUGINS); - switch (res_id) - { - case GTK_RESPONSE_CLOSE: - { - xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_CLOSE"); - gtk_widget_destroy (data->dialog->dialog); + window = XED_WINDOW (data->window); - break; - } + switch (res_id) + { + case GTK_RESPONSE_CLOSE: + { + xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_CLOSE"); + gtk_widget_destroy (data->dialog->dialog); - case GTK_RESPONSE_OK: - { - XedDocument *doc; - - xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_OK"); - - doc = xed_window_get_active_document (window); - g_return_if_fail (doc != NULL); - - docinfo_real (doc, - data->dialog); + break; + } - selectioninfo_real (doc, - data->dialog); - - break; - } - } + case GTK_RESPONSE_OK: + { + XedDocument *doc; + + xed_debug_message (DEBUG_PLUGINS, "GTK_RESPONSE_OK"); + + doc = xed_window_get_active_document (window); + g_return_if_fail (doc != NULL); + + docinfo_real (doc, data->dialog); + selectioninfo_real (doc, data->dialog); + + break; + } + } } static const GtkActionEntry action_entries[] = { - { "DocumentStatistics", - NULL, - N_("_Document Statistics"), - NULL, - N_("Get statistical information on the current document"), - G_CALLBACK (docinfo_cb) } + { "DocumentStatistics", + NULL, + N_("_Document Statistics"), + NULL, + N_("Get statistical information on the current document"), + G_CALLBACK (docinfo_cb) } }; static void -free_window_data (WindowData *data) +update_ui (XedDocInfoPluginPrivate *data) { - g_return_if_fail (data != NULL); - - xed_debug (DEBUG_PLUGINS); + XedWindow *window; + XedView *view; - g_object_unref (data->plugin); + xed_debug (DEBUG_PLUGINS); - g_object_unref (data->ui_action_group); - - if (data->dialog != NULL) - { - gtk_widget_destroy (data->dialog->dialog); - } - - g_free (data); -} + window = XED_WINDOW (data->window); + view = xed_window_get_active_view (window); -static void -update_ui_real (XedWindow *window, - WindowData *data) -{ - XedView *view; + gtk_action_group_set_sensitive (data->ui_action_group, (view != NULL)); - xed_debug (DEBUG_PLUGINS); - - view = xed_window_get_active_view (window); - - gtk_action_group_set_sensitive (data->ui_action_group, - (view != NULL)); - - if (data->dialog != NULL) - { - gtk_dialog_set_response_sensitive (GTK_DIALOG (data->dialog->dialog), - GTK_RESPONSE_OK, - (view != NULL)); - } + if (data->dialog != NULL) + { + gtk_dialog_set_response_sensitive (GTK_DIALOG (data->dialog->dialog), GTK_RESPONSE_OK, (view != NULL)); + } } static void xed_docinfo_plugin_init (XedDocInfoPlugin *plugin) { - xed_debug_message (DEBUG_PLUGINS, "XedDocInfoPlugin initializing"); + xed_debug_message (DEBUG_PLUGINS, "XedDocInfoPlugin initializing"); + plugin->priv = XED_DOCINFO_PLUGIN_GET_PRIVATE (plugin); } static void -xed_docinfo_plugin_finalize (GObject *object) +xed_docinfo_plugin_dispose (GObject *object) { - xed_debug_message (DEBUG_PLUGINS, "XedDocInfoPlugin finalizing"); + XedDocInfoPlugin *plugin = XED_DOCINFO_PLUGIN (object); - G_OBJECT_CLASS (xed_docinfo_plugin_parent_class)->finalize (object); + xed_debug_message (DEBUG_PLUGINS, "XedDocInfoPlugin disposing"); + + if (plugin->priv->window != NULL) + { + g_object_unref (plugin->priv->window); + plugin->priv->window = NULL; + } + + if (plugin->priv->ui_action_group != NULL) + { + g_object_unref (plugin->priv->ui_action_group); + plugin->priv->ui_action_group = NULL; + } + + G_OBJECT_CLASS (xed_docinfo_plugin_parent_class)->dispose (object); } static void -impl_activate (XedPlugin *plugin, - XedWindow *window) +xed_docinfo_plugin_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) { - GtkUIManager *manager; - WindowData *data; - - xed_debug (DEBUG_PLUGINS); + XedDocInfoPlugin *plugin = XED_DOCINFO_PLUGIN (object); - data = g_new (WindowData, 1); - - data->plugin = g_object_ref (plugin); - data->dialog = NULL; - data->ui_action_group = gtk_action_group_new ("XedDocInfoPluginActions"); - - gtk_action_group_set_translation_domain (data->ui_action_group, - GETTEXT_PACKAGE); - gtk_action_group_add_actions (data->ui_action_group, - action_entries, - G_N_ELEMENTS (action_entries), - window); - - manager = xed_window_get_ui_manager (window); - gtk_ui_manager_insert_action_group (manager, - data->ui_action_group, - -1); - - data->ui_id = gtk_ui_manager_new_merge_id (manager); - - g_object_set_data_full (G_OBJECT (window), - WINDOW_DATA_KEY, - data, - (GDestroyNotify) free_window_data); - - gtk_ui_manager_add_ui (manager, - data->ui_id, - MENU_PATH, - "DocumentStatistics", - "DocumentStatistics", - GTK_UI_MANAGER_MENUITEM, - FALSE); - - 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 -impl_deactivate (XedPlugin *plugin, - XedWindow *window) +xed_docinfo_plugin_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) { - GtkUIManager *manager; - WindowData *data; + XedDocInfoPlugin *plugin = XED_DOCINFO_PLUGIN (object); - xed_debug (DEBUG_PLUGINS); - - manager = xed_window_get_ui_manager (window); - - data = (WindowData *) g_object_get_data (G_OBJECT (window), - WINDOW_DATA_KEY); - g_return_if_fail (data != NULL); - - gtk_ui_manager_remove_ui (manager, - data->ui_id); - gtk_ui_manager_remove_action_group (manager, - data->ui_action_group); - - g_object_set_data (G_OBJECT (window), - WINDOW_DATA_KEY, - NULL); + 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 void -impl_update_ui (XedPlugin *plugin, - XedWindow *window) +xed_docinfo_plugin_activate (PeasActivatable *activatable) { - WindowData *data; + XedDocInfoPlugin *plugin; + XedDocInfoPluginPrivate *data; + XedWindow *window; + GtkUIManager *manager; - xed_debug (DEBUG_PLUGINS); + xed_debug (DEBUG_PLUGINS); - data = (WindowData *) g_object_get_data (G_OBJECT (window), - WINDOW_DATA_KEY); - g_return_if_fail (data != NULL); + plugin = XED_DOCINFO_PLUGIN (activatable); + data = plugin->priv; + window = XED_WINDOW (data->window); - update_ui_real (window, - data); + data->dialog = NULL; + data->ui_action_group = gtk_action_group_new ("XedDocInfoPluginActions"); + + gtk_action_group_set_translation_domain (data->ui_action_group, GETTEXT_PACKAGE); + gtk_action_group_add_actions (data->ui_action_group, action_entries, + G_N_ELEMENTS (action_entries), plugin); + + manager = xed_window_get_ui_manager (window); + gtk_ui_manager_insert_action_group (manager, data->ui_action_group, -1); + + data->ui_id = gtk_ui_manager_new_merge_id (manager); + + gtk_ui_manager_add_ui (manager, + data->ui_id, + MENU_PATH, + "DocumentStatistics", + "DocumentStatistics", + GTK_UI_MANAGER_MENUITEM, + FALSE); + + update_ui (data); +} + +static void +xed_docinfo_plugin_deactivate (PeasActivatable *activatable) +{ + XedDocInfoPluginPrivate *data; + XedWindow *window; + GtkUIManager *manager; + + xed_debug (DEBUG_PLUGINS); + + data = XED_DOCINFO_PLUGIN (activatable)->priv; + window = XED_WINDOW (data->window); + + manager = xed_window_get_ui_manager (window); + + gtk_ui_manager_remove_ui (manager, data->ui_id); + gtk_ui_manager_remove_action_group (manager, data->ui_action_group); +} + +static void +xed_docinfo_plugin_update_state (PeasActivatable *activatable) +{ + xed_debug (DEBUG_PLUGINS); + + update_ui (XED_DOCINFO_PLUGIN (activatable)->priv); } static void xed_docinfo_plugin_class_init (XedDocInfoPluginClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - XedPluginClass *plugin_class = XED_PLUGIN_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); - object_class->finalize = xed_docinfo_plugin_finalize; + object_class->dispose = xed_docinfo_plugin_dispose; + object_class->set_property = xed_docinfo_plugin_set_property; + object_class->get_property = xed_docinfo_plugin_get_property; - plugin_class->activate = impl_activate; - plugin_class->deactivate = impl_deactivate; - plugin_class->update_ui = impl_update_ui; + g_object_class_override_property (object_class, PROP_OBJECT, "object"); + + g_type_class_add_private (klass, sizeof (XedDocInfoPluginPrivate)); +} + +static void +xed_docinfo_plugin_class_finalize (XedDocInfoPluginClass *klass) +{ + /* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */ +} + +static void +peas_activatable_iface_init (PeasActivatableInterface *iface) +{ + iface->activate = xed_docinfo_plugin_activate; + iface->deactivate = xed_docinfo_plugin_deactivate; + iface->update_state = xed_docinfo_plugin_update_state; +} + +G_MODULE_EXPORT void +peas_register_types (PeasObjectModule *module) +{ + xed_docinfo_plugin_register_type (G_TYPE_MODULE (module)); + + peas_object_module_register_extension_type (module, + PEAS_TYPE_ACTIVATABLE, + XED_TYPE_DOCINFO_PLUGIN); } diff --git a/plugins/docinfo/xed-docinfo-plugin.h b/plugins/docinfo/xed-docinfo-plugin.h index dcb3856..b6c5cd8 100644 --- a/plugins/docinfo/xed-docinfo-plugin.h +++ b/plugins/docinfo/xed-docinfo-plugin.h @@ -1,7 +1,7 @@ /* * xed-docinfo-plugin.h - * - * Copyright (C) 2002-2005 Paolo Maggi + * + * Copyright (C) 2002-2005 Paolo Maggi * * 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 @@ -25,50 +25,54 @@ #include #include -#include +#include +#include G_BEGIN_DECLS /* * Type checking and casting macros */ -#define XED_TYPE_DOCINFO_PLUGIN (xed_docinfo_plugin_get_type ()) -#define XED_DOCINFO_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPlugin)) -#define XED_DOCINFO_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPluginClass)) -#define XED_IS_DOCINFO_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XED_TYPE_DOCINFO_PLUGIN)) -#define XED_IS_DOCINFO_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), XED_TYPE_DOCINFO_PLUGIN)) -#define XED_DOCINFO_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPluginClass)) +#define XED_TYPE_DOCINFO_PLUGIN (xed_docinfo_plugin_get_type ()) +#define XED_DOCINFO_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPlugin)) +#define XED_DOCINFO_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPluginClass)) +#define XED_IS_DOCINFO_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XED_TYPE_DOCINFO_PLUGIN)) +#define XED_IS_DOCINFO_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), XED_TYPE_DOCINFO_PLUGIN)) +#define XED_DOCINFO_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_DOCINFO_PLUGIN, XedDocInfoPluginClass)) /* Private structure type */ -typedef struct _XedDocInfoPluginPrivate XedDocInfoPluginPrivate; +typedef struct _XedDocInfoPluginPrivate XedDocInfoPluginPrivate; /* * Main object structure */ -typedef struct _XedDocInfoPlugin XedDocInfoPlugin; +typedef struct _XedDocInfoPlugin XedDocInfoPlugin; struct _XedDocInfoPlugin { - XedPlugin parent_instance; + PeasExtensionBase parent_instance; + + /*< private >*/ + XedDocInfoPluginPrivate *priv; }; /* * Class definition */ -typedef struct _XedDocInfoPluginClass XedDocInfoPluginClass; +typedef struct _XedDocInfoPluginClass XedDocInfoPluginClass; struct _XedDocInfoPluginClass { - XedPluginClass parent_class; + PeasExtensionBaseClass parent_class; }; /* * Public methods */ -GType xed_docinfo_plugin_get_type (void) G_GNUC_CONST; +GType xed_docinfo_plugin_get_type (void) G_GNUC_CONST; /* 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