xed/xedit/xedit-plugin-info.c

397 lines
8.6 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* xedit-plugin-info.c
* This file is part of xedit
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2002-2005 - Paolo Maggi
* Copyright (C) 2007 - Paolo Maggi, Steve Frécinaux
*
* 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 of the License, 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
/*
2016-01-25 08:13:49 -06:00
* Modified by the xedit Team, 2002-2007. See the AUTHORS file for a
* list of people on the xedit Team.
2011-11-07 13:46:58 -06:00
* See the ChangeLog files for a list of changes.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <glib/gi18n.h>
#include <glib.h>
2016-01-25 08:13:49 -06:00
#include "xedit-plugin-info.h"
#include "xedit-plugin-info-priv.h"
#include "xedit-debug.h"
#include "xedit-plugin.h"
2011-11-07 13:46:58 -06:00
void
2016-01-25 08:13:49 -06:00
_xedit_plugin_info_ref (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_atomic_int_inc (&info->refcount);
}
2016-01-25 08:13:49 -06:00
static XeditPluginInfo *
xedit_plugin_info_copy (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
_xedit_plugin_info_ref (info);
2011-11-07 13:46:58 -06:00
return info;
}
void
2016-01-25 08:13:49 -06:00
_xedit_plugin_info_unref (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
if (!g_atomic_int_dec_and_test (&info->refcount))
return;
if (info->plugin != NULL)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Unref plugin %s", info->name);
2011-11-07 13:46:58 -06:00
g_object_unref (info->plugin);
}
g_free (info->file);
g_free (info->module_name);
g_strfreev (info->dependencies);
g_free (info->name);
g_free (info->desc);
g_free (info->icon_name);
g_free (info->website);
g_free (info->copyright);
g_free (info->loader);
g_free (info->version);
g_strfreev (info->authors);
g_free (info);
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_info_get_type:
2011-11-07 13:46:58 -06:00
*
2016-01-25 08:13:49 -06:00
* Retrieves the #GType object which is associated with the #XeditPluginInfo
2011-11-07 13:46:58 -06:00
* class.
*
2016-01-25 08:13:49 -06:00
* Return value: the GType associated with #XeditPluginInfo.
2011-11-07 13:46:58 -06:00
**/
GType
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_type (void)
2011-11-07 13:46:58 -06:00
{
static GType the_type = 0;
if (G_UNLIKELY (!the_type))
the_type = g_boxed_type_register_static (
2016-01-25 08:13:49 -06:00
"XeditPluginInfo",
(GBoxedCopyFunc) xedit_plugin_info_copy,
(GBoxedFreeFunc) _xedit_plugin_info_unref);
2011-11-07 13:46:58 -06:00
return the_type;
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_info_new:
2011-11-07 13:46:58 -06:00
* @filename: the filename where to read the plugin information
*
2016-01-25 08:13:49 -06:00
* Creates a new #XeditPluginInfo from a file on the disk.
2011-11-07 13:46:58 -06:00
*
2016-01-25 08:13:49 -06:00
* Return value: a newly created #XeditPluginInfo.
2011-11-07 13:46:58 -06:00
*/
2016-01-25 08:13:49 -06:00
XeditPluginInfo *
_xedit_plugin_info_new (const gchar *file)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditPluginInfo *info;
2011-11-07 13:46:58 -06:00
GKeyFile *plugin_file = NULL;
gchar *str;
g_return_val_if_fail (file != NULL, NULL);
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Loading plugin: %s", file);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
info = g_new0 (XeditPluginInfo, 1);
2011-11-07 13:46:58 -06:00
info->refcount = 1;
info->file = g_strdup (file);
plugin_file = g_key_file_new ();
if (!g_key_file_load_from_file (plugin_file, file, G_KEY_FILE_NONE, NULL))
{
g_warning ("Bad plugin file: %s", file);
goto error;
}
if (!g_key_file_has_key (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"IAge",
NULL))
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS,
2011-11-07 13:46:58 -06:00
"IAge key does not exist in file: %s", file);
goto error;
}
/* Check IAge=2 */
if (g_key_file_get_integer (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"IAge",
NULL) != 2)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS,
2011-11-07 13:46:58 -06:00
"Wrong IAge in file: %s", file);
goto error;
}
/* Get module name */
str = g_key_file_get_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Module",
NULL);
if ((str != NULL) && (*str != '\0'))
{
info->module_name = str;
}
else
{
g_warning ("Could not find 'Module' in %s", file);
2014-11-03 04:58:45 -06:00
g_free (str);
2011-11-07 13:46:58 -06:00
goto error;
}
/* Get the dependency list */
info->dependencies = g_key_file_get_string_list (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Depends",
NULL,
NULL);
if (info->dependencies == NULL)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Depends' in %s", file);
2011-11-07 13:46:58 -06:00
info->dependencies = g_new0 (gchar *, 1);
}
/* Get the loader for this plugin */
str = g_key_file_get_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Loader",
NULL);
if ((str != NULL) && (*str != '\0'))
{
info->loader = str;
}
else
{
/* default to the C loader */
info->loader = g_strdup("c");
2014-11-03 04:58:45 -06:00
g_free (str);
2011-11-07 13:46:58 -06:00
}
/* Get Name */
str = g_key_file_get_locale_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Name",
NULL, NULL);
if (str)
info->name = str;
else
{
g_warning ("Could not find 'Name' in %s", file);
goto error;
}
/* Get Description */
str = g_key_file_get_locale_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Description",
NULL, NULL);
if (str)
info->desc = str;
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Description' in %s", file);
2011-11-07 13:46:58 -06:00
/* Get Icon */
str = g_key_file_get_locale_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Icon",
NULL, NULL);
if (str)
info->icon_name = str;
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Icon' in %s, using 'xedit-plugin'", file);
2011-11-07 13:46:58 -06:00
/* Get Authors */
info->authors = g_key_file_get_string_list (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Authors",
NULL,
NULL);
if (info->authors == NULL)
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Authors' in %s", file);
2011-11-07 13:46:58 -06:00
/* Get Copyright */
str = g_key_file_get_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Copyright",
NULL);
if (str)
info->copyright = str;
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Copyright' in %s", file);
2011-11-07 13:46:58 -06:00
/* Get Website */
str = g_key_file_get_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Website",
NULL);
if (str)
info->website = str;
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Website' in %s", file);
2011-11-07 13:46:58 -06:00
/* Get Version */
str = g_key_file_get_string (plugin_file,
2016-01-25 08:13:49 -06:00
"Xedit Plugin",
2011-11-07 13:46:58 -06:00
"Version",
NULL);
if (str)
info->version = str;
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Could not find 'Version' in %s", file);
2011-11-07 13:46:58 -06:00
g_key_file_free (plugin_file);
/* If we know nothing about the availability of the plugin,
set it as available */
info->available = TRUE;
return info;
error:
g_free (info->file);
g_free (info->module_name);
g_free (info->name);
g_free (info->loader);
g_free (info);
g_key_file_free (plugin_file);
return NULL;
}
gboolean
2016-01-25 08:13:49 -06:00
xedit_plugin_info_is_active (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, FALSE);
return info->available && info->plugin != NULL;
}
gboolean
2016-01-25 08:13:49 -06:00
xedit_plugin_info_is_available (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, FALSE);
return info->available != FALSE;
}
gboolean
2016-01-25 08:13:49 -06:00
xedit_plugin_info_is_configurable (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "Is '%s' configurable?", info->name);
2011-11-07 13:46:58 -06:00
g_return_val_if_fail (info != NULL, FALSE);
if (info->plugin == NULL || !info->available)
return FALSE;
2016-01-25 08:13:49 -06:00
return xedit_plugin_is_configurable (info->plugin);
2011-11-07 13:46:58 -06:00
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_module_name (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->module_name;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_name (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->name;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_description (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->desc;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_icon_name (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
2016-01-25 08:13:49 -06:00
/* use the xedit-plugin icon as a default if the plugin does not
2011-11-07 13:46:58 -06:00
have its own */
if (info->icon_name != NULL &&
gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
info->icon_name))
return info->icon_name;
else
2016-01-25 08:13:49 -06:00
return "xedit-plugin";
2011-11-07 13:46:58 -06:00
}
const gchar **
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_authors (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, (const gchar **)NULL);
return (const gchar **) info->authors;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_website (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->website;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_copyright (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->copyright;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_info_get_version (XeditPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
g_return_val_if_fail (info != NULL, NULL);
return info->version;
}