docinfo-plugin: Port to libpeas

Adapted from:
9ca3dda491
This commit is contained in:
JosephMcc 2016-12-26 13:13:01 -08:00
parent 7220312ac6
commit fdc2da63a9
2 changed files with 460 additions and 422 deletions

View File

@ -31,14 +31,26 @@
#include <glib/gi18n-lib.h> #include <glib/gi18n-lib.h>
#include <pango/pango-break.h> #include <pango/pango-break.h>
#include <gmodule.h> #include <gmodule.h>
#include <libpeas/peas-activatable.h>
#include <xed/xed-window.h>
#include <xed/xed-debug.h> #include <xed/xed-debug.h>
#include <xed/xed-utils.h> #include <xed/xed-utils.h>
#define WINDOW_DATA_KEY "XedDocInfoWindowData"
#define MENU_PATH "/MenuBar/ToolsMenu/ToolsOps_2" #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 typedef struct
{ {
@ -57,23 +69,29 @@ typedef struct
GtkWidget *selected_bytes_label; GtkWidget *selected_bytes_label;
} DocInfoDialog; } DocInfoDialog;
typedef struct struct _XedDocInfoPluginPrivate
{ {
XedPlugin *plugin; GtkWidget *window;
GtkActionGroup *ui_action_group; GtkActionGroup *ui_action_group;
guint ui_id; guint ui_id;
DocInfoDialog *dialog; DocInfoDialog *dialog;
} WindowData; };
enum
{
PROP_0,
PROP_OBJECT
};
static void docinfo_dialog_response_cb (GtkDialog *widget, static void docinfo_dialog_response_cb (GtkDialog *widget,
gint res_id, gint res_id,
XedWindow *window); XedDocInfoPluginPrivate *data);
static void static void
docinfo_dialog_destroy_cb (GObject *obj, docinfo_dialog_destroy_cb (GObject *obj,
WindowData *data) XedDocInfoPluginPrivate *data)
{ {
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
@ -85,9 +103,10 @@ docinfo_dialog_destroy_cb (GObject *obj,
} }
static DocInfoDialog * static DocInfoDialog *
get_docinfo_dialog (XedWindow *window, get_docinfo_dialog (XedDocInfoPlugin *plugin)
WindowData *data)
{ {
XedDocInfoPluginPrivate *data;
XedWindow *window;
DocInfoDialog *dialog; DocInfoDialog *dialog;
gchar *data_dir; gchar *data_dir;
gchar *ui_file; gchar *ui_file;
@ -97,9 +116,12 @@ get_docinfo_dialog (XedWindow *window,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = plugin->priv;
window = XED_WINDOW (data->window);
dialog = g_new (DocInfoDialog, 1); dialog = g_new (DocInfoDialog, 1);
data_dir = xed_plugin_get_data_dir (data->plugin); data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
ui_file = g_build_filename (data_dir, "docinfo.ui", NULL); ui_file = g_build_filename (data_dir, "docinfo.ui", NULL);
ret = xed_utils_get_ui_objects (ui_file, ret = xed_utils_get_ui_objects (ui_file,
NULL, NULL,
@ -136,20 +158,11 @@ get_docinfo_dialog (XedWindow *window,
return NULL; return NULL;
} }
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog), gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog), GTK_RESPONSE_OK);
GTK_RESPONSE_OK); gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), GTK_WINDOW (window));
gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
GTK_WINDOW (window));
g_signal_connect (dialog->dialog, g_signal_connect (dialog->dialog, "destroy", G_CALLBACK (docinfo_dialog_destroy_cb), data);
"destroy", g_signal_connect (dialog->dialog, "response", G_CALLBACK (docinfo_dialog_response_cb), data);
G_CALLBACK (docinfo_dialog_destroy_cb),
data);
g_signal_connect (dialog->dialog,
"response",
G_CALLBACK (docinfo_dialog_response_cb),
window);
return dialog; return dialog;
} }
@ -167,10 +180,7 @@ calculate_info (XedDocument *doc,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), start, end, TRUE);
start,
end,
TRUE);
*chars = g_utf8_strlen (text, -1); *chars = g_utf8_strlen (text, -1);
*bytes = strlen (text); *bytes = strlen (text);
@ -182,21 +192,20 @@ calculate_info (XedDocument *doc,
attrs = g_new0 (PangoLogAttr, *chars + 1); attrs = g_new0 (PangoLogAttr, *chars + 1);
pango_get_log_attrs (text, pango_get_log_attrs (text, -1, 0, pango_language_from_string ("C"), attrs, *chars + 1);
-1,
0,
pango_language_from_string ("C"),
attrs,
*chars + 1);
for (i = 0; i < (*chars); i++) for (i = 0; i < (*chars); i++)
{ {
if (attrs[i].is_white) if (attrs[i].is_white)
{
++(*white_chars); ++(*white_chars);
}
if (attrs[i].is_word_start) if (attrs[i].is_word_start)
{
++(*words); ++(*words);
} }
}
g_free (attrs); g_free (attrs);
} }
@ -224,18 +233,16 @@ docinfo_real (XedDocument *doc,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), &start, &end);
&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, calculate_info (doc, &start, &end, &chars, &words, &white_chars, &bytes);
&start, &end,
&chars, &words, &white_chars, &bytes);
if (chars == 0) if (chars == 0)
{
lines = 0; lines = 0;
}
xed_debug_message (DEBUG_PLUGINS, "Chars: %d", chars); xed_debug_message (DEBUG_PLUGINS, "Chars: %d", chars);
xed_debug_message (DEBUG_PLUGINS, "Lines: %d", lines); xed_debug_message (DEBUG_PLUGINS, "Lines: %d", lines);
@ -285,17 +292,13 @@ selectioninfo_real (XedDocument *doc,
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
sel = gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), sel = gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), &start, &end);
&start,
&end);
if (sel) if (sel)
{ {
lines = gtk_text_iter_get_line (&end) - gtk_text_iter_get_line (&start) + 1; lines = gtk_text_iter_get_line (&end) - gtk_text_iter_get_line (&start) + 1;
calculate_info (doc, calculate_info (doc, &start, &end, &chars, &words, &white_chars, &bytes);
&start, &end,
&chars, &words, &white_chars, &bytes);
xed_debug_message (DEBUG_PLUGINS, "Selected chars: %d", chars); xed_debug_message (DEBUG_PLUGINS, "Selected chars: %d", chars);
xed_debug_message (DEBUG_PLUGINS, "Selected lines: %d", lines); xed_debug_message (DEBUG_PLUGINS, "Selected lines: %d", lines);
@ -313,7 +316,9 @@ selectioninfo_real (XedDocument *doc,
} }
if (chars == 0) if (chars == 0)
{
lines = 0; lines = 0;
}
tmp_str = g_strdup_printf("%d", lines); tmp_str = g_strdup_printf("%d", lines);
gtk_label_set_text (GTK_LABEL (dialog->selected_lines_label), tmp_str); gtk_label_set_text (GTK_LABEL (dialog->selected_lines_label), tmp_str);
@ -338,16 +343,16 @@ selectioninfo_real (XedDocument *doc,
static void static void
docinfo_cb (GtkAction *action, docinfo_cb (GtkAction *action,
XedWindow *window) XedDocInfoPlugin *plugin)
{ {
XedDocInfoPluginPrivate *data;
XedWindow *window;
XedDocument *doc; XedDocument *doc;
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = (WindowData *) g_object_get_data (G_OBJECT (window), data = plugin->priv;
WINDOW_DATA_KEY); window = XED_WINDOW (data->window);
doc = xed_window_get_active_document (window); doc = xed_window_get_active_document (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
@ -360,7 +365,7 @@ docinfo_cb (GtkAction *action,
{ {
DocInfoDialog *dialog; DocInfoDialog *dialog;
dialog = get_docinfo_dialog (window, data); dialog = get_docinfo_dialog (plugin);
g_return_if_fail (dialog != NULL); g_return_if_fail (dialog != NULL);
data->dialog = dialog; data->dialog = dialog;
@ -377,14 +382,13 @@ docinfo_cb (GtkAction *action,
static void static void
docinfo_dialog_response_cb (GtkDialog *widget, docinfo_dialog_response_cb (GtkDialog *widget,
gint res_id, gint res_id,
XedWindow *window) XedDocInfoPluginPrivate *data)
{ {
WindowData *data; XedWindow *window;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = (WindowData *) g_object_get_data (G_OBJECT (window), window = XED_WINDOW (data->window);
WINDOW_DATA_KEY);
switch (res_id) switch (res_id)
{ {
@ -405,11 +409,8 @@ docinfo_dialog_response_cb (GtkDialog *widget,
doc = xed_window_get_active_document (window); doc = xed_window_get_active_document (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
docinfo_real (doc, docinfo_real (doc, data->dialog);
data->dialog); selectioninfo_real (doc, data->dialog);
selectioninfo_real (doc,
data->dialog);
break; break;
} }
@ -427,42 +428,21 @@ static const GtkActionEntry action_entries[] =
}; };
static void static void
free_window_data (WindowData *data) update_ui (XedDocInfoPluginPrivate *data)
{
g_return_if_fail (data != NULL);
xed_debug (DEBUG_PLUGINS);
g_object_unref (data->plugin);
g_object_unref (data->ui_action_group);
if (data->dialog != NULL)
{
gtk_widget_destroy (data->dialog->dialog);
}
g_free (data);
}
static void
update_ui_real (XedWindow *window,
WindowData *data)
{ {
XedWindow *window;
XedView *view; XedView *view;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
window = XED_WINDOW (data->window);
view = xed_window_get_active_view (window); view = xed_window_get_active_view (window);
gtk_action_group_set_sensitive (data->ui_action_group, gtk_action_group_set_sensitive (data->ui_action_group, (view != NULL));
(view != NULL));
if (data->dialog != NULL) if (data->dialog != NULL)
{ {
gtk_dialog_set_response_sensitive (GTK_DIALOG (data->dialog->dialog), gtk_dialog_set_response_sensitive (GTK_DIALOG (data->dialog->dialog), GTK_RESPONSE_OK, (view != NULL));
GTK_RESPONSE_OK,
(view != NULL));
} }
} }
@ -470,50 +450,95 @@ static void
xed_docinfo_plugin_init (XedDocInfoPlugin *plugin) 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 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 static void
impl_activate (XedPlugin *plugin, xed_docinfo_plugin_set_property (GObject *object,
XedWindow *window) guint prop_id,
const GValue *value,
GParamSpec *pspec)
{ {
XedDocInfoPlugin *plugin = XED_DOCINFO_PLUGIN (object);
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_docinfo_plugin_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XedDocInfoPlugin *plugin = XED_DOCINFO_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 void
xed_docinfo_plugin_activate (PeasActivatable *activatable)
{
XedDocInfoPlugin *plugin;
XedDocInfoPluginPrivate *data;
XedWindow *window;
GtkUIManager *manager; GtkUIManager *manager;
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = g_new (WindowData, 1); plugin = XED_DOCINFO_PLUGIN (activatable);
data = plugin->priv;
window = XED_WINDOW (data->window);
data->plugin = g_object_ref (plugin);
data->dialog = NULL; data->dialog = NULL;
data->ui_action_group = gtk_action_group_new ("XedDocInfoPluginActions"); data->ui_action_group = gtk_action_group_new ("XedDocInfoPluginActions");
gtk_action_group_set_translation_domain (data->ui_action_group, gtk_action_group_set_translation_domain (data->ui_action_group, GETTEXT_PACKAGE);
GETTEXT_PACKAGE); gtk_action_group_add_actions (data->ui_action_group, action_entries,
gtk_action_group_add_actions (data->ui_action_group, G_N_ELEMENTS (action_entries), plugin);
action_entries,
G_N_ELEMENTS (action_entries),
window);
manager = xed_window_get_ui_manager (window); manager = xed_window_get_ui_manager (window);
gtk_ui_manager_insert_action_group (manager, gtk_ui_manager_insert_action_group (manager, data->ui_action_group, -1);
data->ui_action_group,
-1);
data->ui_id = gtk_ui_manager_new_merge_id (manager); 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, gtk_ui_manager_add_ui (manager,
data->ui_id, data->ui_id,
MENU_PATH, MENU_PATH,
@ -522,60 +547,69 @@ impl_activate (XedPlugin *plugin,
GTK_UI_MANAGER_MENUITEM, GTK_UI_MANAGER_MENUITEM,
FALSE); FALSE);
update_ui_real (window, update_ui (data);
data);
} }
static void static void
impl_deactivate (XedPlugin *plugin, xed_docinfo_plugin_deactivate (PeasActivatable *activatable)
XedWindow *window)
{ {
XedDocInfoPluginPrivate *data;
XedWindow *window;
GtkUIManager *manager; GtkUIManager *manager;
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = XED_DOCINFO_PLUGIN (activatable)->priv;
window = XED_WINDOW (data->window);
manager = xed_window_get_ui_manager (window); manager = xed_window_get_ui_manager (window);
data = (WindowData *) g_object_get_data (G_OBJECT (window), gtk_ui_manager_remove_ui (manager, data->ui_id);
WINDOW_DATA_KEY); gtk_ui_manager_remove_action_group (manager, data->ui_action_group);
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);
} }
static void static void
impl_update_ui (XedPlugin *plugin, xed_docinfo_plugin_update_state (PeasActivatable *activatable)
XedWindow *window)
{ {
WindowData *data;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = (WindowData *) g_object_get_data (G_OBJECT (window), update_ui (XED_DOCINFO_PLUGIN (activatable)->priv);
WINDOW_DATA_KEY);
g_return_if_fail (data != NULL);
update_ui_real (window,
data);
} }
static void static void
xed_docinfo_plugin_class_init (XedDocInfoPluginClass *klass) xed_docinfo_plugin_class_init (XedDocInfoPluginClass *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_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; g_object_class_override_property (object_class, PROP_OBJECT, "object");
plugin_class->deactivate = impl_deactivate;
plugin_class->update_ui = impl_update_ui; 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);
} }

View File

@ -25,7 +25,8 @@
#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
@ -49,7 +50,10 @@ typedef struct _XedDocInfoPlugin XedDocInfoPlugin;
struct _XedDocInfoPlugin struct _XedDocInfoPlugin
{ {
XedPlugin parent_instance; PeasExtensionBase parent_instance;
/*< private >*/
XedDocInfoPluginPrivate *priv;
}; };
/* /*
@ -59,7 +63,7 @@ typedef struct _XedDocInfoPluginClass XedDocInfoPluginClass;
struct _XedDocInfoPluginClass struct _XedDocInfoPluginClass
{ {
XedPluginClass parent_class; PeasExtensionBaseClass parent_class;
}; };
/* /*
@ -68,7 +72,7 @@ struct _XedDocInfoPluginClass
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 */ /* 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