modelines-plugin: Port to libpeas

Adapted from:
b43579beda
This commit is contained in:
JosephMcc 2016-12-26 13:29:33 -08:00
parent 4cad8ffa7f
commit decfeff888
2 changed files with 166 additions and 72 deletions

View File

@ -25,20 +25,23 @@
#include <glib/gi18n-lib.h> #include <glib/gi18n-lib.h>
#include <gmodule.h> #include <gmodule.h>
#include <libpeas/peas-activatable.h>
#include "xed-modeline-plugin.h" #include "xed-modeline-plugin.h"
#include "modeline-parser.h" #include "modeline-parser.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 "XedModelinePluginWindowData"
#define DOCUMENT_DATA_KEY "XedModelinePluginDocumentData" #define DOCUMENT_DATA_KEY "XedModelinePluginDocumentData"
typedef struct struct _XedModelinePluginPrivate
{ {
GtkWidget *window;
gulong tab_added_handler_id; gulong tab_added_handler_id;
gulong tab_removed_handler_id; gulong tab_removed_handler_id;
} WindowData; };
typedef struct typedef struct
{ {
@ -46,18 +49,20 @@ typedef struct
gulong document_saved_handler_id; gulong document_saved_handler_id;
} DocumentData; } DocumentData;
static void xed_modeline_plugin_activate (XedPlugin *plugin, XedWindow *window); enum
static void xed_modeline_plugin_deactivate (XedPlugin *plugin, XedWindow *window);
static GObject *xed_modeline_plugin_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_param);
static void xed_modeline_plugin_finalize (GObject *object);
XED_PLUGIN_REGISTER_TYPE(XedModelinePlugin, xed_modeline_plugin)
static void
window_data_free (WindowData *wdata)
{ {
g_slice_free (WindowData, wdata); PROP_0,
} PROP_OBJECT
};
static void peas_activatable_iface_init (PeasActivatableInterface *iface);
G_DEFINE_DYNAMIC_TYPE_EXTENDED (XedModelinePlugin,
xed_modeline_plugin,
PEAS_TYPE_EXTENSION_BASE,
0,
G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_TYPE_ACTIVATABLE,
peas_activatable_iface_init))
static void static void
document_data_free (DocumentData *ddata) document_data_free (DocumentData *ddata)
@ -66,43 +71,27 @@ document_data_free (DocumentData *ddata)
} }
static void static void
xed_modeline_plugin_class_init (XedModelinePluginClass *klass) xed_modeline_plugin_constructed (GObject *object)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass);
XedPluginClass *plugin_class = XED_PLUGIN_CLASS (klass);
object_class->constructor = xed_modeline_plugin_constructor;
object_class->finalize = xed_modeline_plugin_finalize;
plugin_class->activate = xed_modeline_plugin_activate;
plugin_class->deactivate = xed_modeline_plugin_deactivate;
}
static GObject *
xed_modeline_plugin_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_param)
{
GObject *object;
gchar *data_dir; gchar *data_dir;
object = G_OBJECT_CLASS (xed_modeline_plugin_parent_class)->constructor (type, data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (object));
n_construct_properties,
construct_param);
data_dir = xed_plugin_get_data_dir (XED_PLUGIN (object));
modeline_parser_init (data_dir); modeline_parser_init (data_dir);
g_free (data_dir); g_free (data_dir);
return object; G_OBJECT_CLASS (xed_modeline_plugin_parent_class)->constructed (object);
} }
static void static void
xed_modeline_plugin_init (XedModelinePlugin *plugin) xed_modeline_plugin_init (XedModelinePlugin *plugin)
{ {
xed_debug_message (DEBUG_PLUGINS, "XedModelinePlugin initializing"); xed_debug_message (DEBUG_PLUGINS, "XedModelinePlugin initializing");
plugin->priv = G_TYPE_INSTANCE_GET_PRIVATE (plugin,
XED_TYPE_MODELINE_PLUGIN,
XedModelinePluginPrivate);
} }
static void static void
@ -115,6 +104,60 @@ xed_modeline_plugin_finalize (GObject *object)
G_OBJECT_CLASS (xed_modeline_plugin_parent_class)->finalize (object); G_OBJECT_CLASS (xed_modeline_plugin_parent_class)->finalize (object);
} }
static void
xed_modeline_plugin_dispose (GObject *object)
{
XedModelinePlugin *plugin = XED_MODELINE_PLUGIN (object);
xed_debug_message (DEBUG_PLUGINS, "XedModelinePlugin disposing");
if (plugin->priv->window != NULL)
{
g_object_unref (plugin->priv->window);
plugin->priv->window = NULL;
}
G_OBJECT_CLASS (xed_modeline_plugin_parent_class)->dispose (object);
}
static void
xed_modeline_plugin_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
XedModelinePlugin *plugin = XED_MODELINE_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_modeline_plugin_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XedModelinePlugin *plugin = XED_MODELINE_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 static void
on_document_loaded_or_saved (XedDocument *document, on_document_loaded_or_saved (XedDocument *document,
const GError *error, const GError *error,
@ -186,15 +229,18 @@ on_window_tab_removed (XedWindow *window,
} }
static void static void
xed_modeline_plugin_activate (XedPlugin *plugin, xed_modeline_plugin_activate (PeasActivatable *activatable)
XedWindow *window)
{ {
WindowData *wdata; XedModelinePluginPrivate *data;
XedWindow *window;
GList *views; GList *views;
GList *l; GList *l;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
data = XED_MODELINE_PLUGIN (activatable)->priv;
window = XED_WINDOW (data->window);
views = xed_window_get_views (window); views = xed_window_get_views (window);
for (l = views; l != NULL; l = l->next) for (l = views; l != NULL; l = l->next)
{ {
@ -203,36 +249,30 @@ xed_modeline_plugin_activate (XedPlugin *plugin,
} }
g_list_free (views); g_list_free (views);
wdata = g_slice_new (WindowData); data->tab_added_handler_id =
wdata->tab_added_handler_id =
g_signal_connect (window, "tab-added", g_signal_connect (window, "tab-added",
G_CALLBACK (on_window_tab_added), NULL); G_CALLBACK (on_window_tab_added), NULL);
wdata->tab_removed_handler_id = data->tab_removed_handler_id =
g_signal_connect (window, "tab-removed", g_signal_connect (window, "tab-removed",
G_CALLBACK (on_window_tab_removed), NULL); G_CALLBACK (on_window_tab_removed), NULL);
g_object_set_data_full (G_OBJECT (window), WINDOW_DATA_KEY,
wdata, (GDestroyNotify) window_data_free);
} }
static void static void
xed_modeline_plugin_deactivate (XedPlugin *plugin, xed_modeline_plugin_deactivate (PeasActivatable *activatable)
XedWindow *window)
{ {
WindowData *wdata; XedModelinePluginPrivate *data;
XedWindow *window;
GList *views; GList *views;
GList *l; GList *l;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
wdata = g_object_steal_data (G_OBJECT (window), WINDOW_DATA_KEY); data = XED_MODELINE_PLUGIN (activatable)->priv;
window = XED_WINDOW (data->window);
g_signal_handler_disconnect (window, wdata->tab_added_handler_id); g_signal_handler_disconnect (window, data->tab_added_handler_id);
g_signal_handler_disconnect (window, wdata->tab_removed_handler_id); g_signal_handler_disconnect (window, data->tab_removed_handler_id);
window_data_free (wdata);
views = xed_window_get_views (window); views = xed_window_get_views (window);
@ -246,3 +286,41 @@ xed_modeline_plugin_deactivate (XedPlugin *plugin,
g_list_free (views); g_list_free (views);
} }
static void
xed_modeline_plugin_class_init (XedModelinePluginClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = xed_modeline_plugin_constructed;
object_class->finalize = xed_modeline_plugin_finalize;
object_class->dispose = xed_modeline_plugin_dispose;
object_class->set_property = xed_modeline_plugin_set_property;
object_class->get_property = xed_modeline_plugin_get_property;
g_object_class_override_property (object_class, PROP_OBJECT, "object");
g_type_class_add_private (klass, sizeof (XedModelinePluginPrivate));
}
static void
xed_modeline_plugin_class_finalize (XedModelinePluginClass *klass)
{
/* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */
}
static void
peas_activatable_iface_init (PeasActivatableInterface *iface)
{
iface->activate = xed_modeline_plugin_activate;
iface->deactivate = xed_modeline_plugin_deactivate;
}
G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
xed_modeline_plugin_register_type (G_TYPE_MODULE (module));
peas_object_module_register_extension_type (module,
PEAS_TYPE_ACTIVATABLE,
XED_TYPE_MODELINE_PLUGIN);
}

View File

@ -24,7 +24,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
@ -36,12 +37,27 @@ G_BEGIN_DECLS
#define XED_MODELINE_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_MODELINE_PLUGIN, XedModelinePluginClass)) #define XED_MODELINE_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XED_TYPE_MODELINE_PLUGIN, XedModelinePluginClass))
/* Private structure type */ /* Private structure type */
typedef XedPluginClass XedModelinePluginClass; typedef struct _XedModelinePlugin XedModelinePlugin;
typedef XedPlugin XedModelinePlugin; typedef struct _XedModelinePluginPrivate XedModelinePluginPrivate;
struct _XedModelinePlugin
{
PeasExtensionBase parent_instance;
/*< private >*/
XedModelinePluginPrivate *priv;
};
typedef struct _XedModelinePluginClass XedModelinePluginClass;
struct _XedModelinePluginClass
{
PeasExtensionBaseClass parent_class;
};
GType xed_modeline_plugin_get_type (void) G_GNUC_CONST; GType xed_modeline_plugin_get_type (void) G_GNUC_CONST;
G_MODULE_EXPORT GType register_xed_plugin (GTypeModule *module); G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
G_END_DECLS G_END_DECLS