| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* AbiSource Application Framework |
| 4 |
* Copyright (C) 2001 Dom Lachowicz <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "ut_assert.h" |
| 25 |
|
| 26 |
#include "xap_Module.h" |
| 27 |
#include "xap_ModuleManager.h" |
| 28 |
|
| 29 |
/*! |
| 30 |
* Protected constructor |
| 31 |
*/ |
| 32 |
XAP_Module::XAP_Module () : |
| 33 |
m_fnRegister(0), |
| 34 |
m_fnDeregister(0), |
| 35 |
m_fnSupportsVersion(0), |
| 36 |
m_creator (0), |
| 37 |
m_bLoaded(false), |
| 38 |
m_bRegistered(false), |
| 39 |
m_iStatus(0), |
| 40 |
m_szSPI(0) |
| 41 |
{ |
| 42 |
// zero this out |
| 43 |
memset (&m_info, 0, sizeof (m_info)); |
| 44 |
} |
| 45 |
|
| 46 |
/*! |
| 47 |
* Protected destructor |
| 48 |
*/ |
| 49 |
XAP_Module::~XAP_Module () |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
/*! |
| 54 |
* marks the module as loaded; returns false if module is already loaded |
| 55 |
*/ |
| 56 |
bool XAP_Module::setSymbols (XAP_Plugin_Registration fnRegister, |
| 57 |
XAP_Plugin_Registration fnDeregister, |
| 58 |
XAP_Plugin_VersionCheck fnSupportsVersion) |
| 59 |
{ |
| 60 |
UT_ASSERT (!m_bLoaded && fnRegister && fnDeregister && fnSupportsVersion); |
| 61 |
|
| 62 |
if (m_bLoaded || !(fnRegister && fnDeregister && fnSupportsVersion)) return false; |
| 63 |
|
| 64 |
m_fnRegister = fnRegister; |
| 65 |
m_fnDeregister = fnDeregister; |
| 66 |
m_fnSupportsVersion = fnSupportsVersion; |
| 67 |
|
| 68 |
m_bLoaded = true; |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
/*! |
| 74 |
* After loading a module, call this function to register |
| 75 |
* the plugin with AbiWord |
| 76 |
* |
| 77 |
* \return true on success, false on failure |
| 78 |
*/ |
| 79 |
bool XAP_Module::registerThySelf () |
| 80 |
{ |
| 81 |
UT_ASSERT (m_bLoaded && !m_bRegistered); |
| 82 |
|
| 83 |
if (!m_bLoaded) return false; |
| 84 |
if ( m_bRegistered) return false; |
| 85 |
|
| 86 |
m_bRegistered = true; // i.e., don't try to register again |
| 87 |
|
| 88 |
typedef int (*plugin_init_func_t) (XAP_ModuleInfo *) ; |
| 89 |
plugin_init_func_t plugin_init_func; |
| 90 |
plugin_init_func_t *pfunc = &plugin_init_func; |
| 91 |
|
| 92 |
m_iStatus = 0; |
| 93 |
|
| 94 |
if (m_fnRegister) |
| 95 |
{ |
| 96 |
memset (&m_info, 0, sizeof (m_info)); // ensure that this is null |
| 97 |
m_iStatus = m_fnRegister (&m_info); |
| 98 |
} |
| 99 |
else if (resolveSymbol ("abi_plugin_register", reinterpret_cast<void **>(pfunc))) |
| 100 |
{ |
| 101 |
if (!plugin_init_func) |
| 102 |
{ |
| 103 |
// damn this sucks. probably not an abiword plugin |
| 104 |
return false; |
| 105 |
} |
| 106 |
memset (&m_info, 0, sizeof (m_info)); // ensure that this is null |
| 107 |
m_iStatus = plugin_init_func (&m_info); |
| 108 |
} |
| 109 |
return (m_iStatus ? true : false); |
| 110 |
} |
| 111 |
|
| 112 |
/*! |
| 113 |
* Whether the plugin is registered |
| 114 |
* |
| 115 |
* \return true if registered, false otherwise |
| 116 |
*/ |
| 117 |
bool XAP_Module::registered () |
| 118 |
{ |
| 119 |
if (!m_bLoaded) return false; |
| 120 |
|
| 121 |
if (!m_bRegistered) return false; |
| 122 |
return (m_iStatus ? true : false); |
| 123 |
} |
| 124 |
|
| 125 |
/*! |
| 126 |
* Before unloading a module, call this function to unregister |
| 127 |
* the plugin, so that it might g_free resources, de-init itself, |
| 128 |
* etc... |
| 129 |
* |
| 130 |
* \return true on success, false on failure |
| 131 |
*/ |
| 132 |
bool XAP_Module::unregisterThySelf () |
| 133 |
{ |
| 134 |
UT_ASSERT (m_bLoaded && m_bRegistered); |
| 135 |
|
| 136 |
bool result = true; |
| 137 |
|
| 138 |
if (registered ()) |
| 139 |
{ |
| 140 |
typedef int (*plugin_cleanup_func_t) (XAP_ModuleInfo *); |
| 141 |
plugin_cleanup_func_t plugin_cleanup_func; |
| 142 |
plugin_cleanup_func_t *pfunc = &plugin_cleanup_func; |
| 143 |
|
| 144 |
if (m_fnDeregister) |
| 145 |
{ |
| 146 |
if (m_fnDeregister (&m_info) == 0) result = false; |
| 147 |
} |
| 148 |
else if (resolveSymbol ("abi_plugin_unregister", reinterpret_cast<void **>(pfunc))) |
| 149 |
{ |
| 150 |
if (plugin_cleanup_func) |
| 151 |
{ |
| 152 |
if (plugin_cleanup_func (&m_info) == 0) result = false; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// reset this to 0 |
| 158 |
memset (&m_info, 0, sizeof (m_info)); |
| 159 |
|
| 160 |
m_bRegistered = false; |
| 161 |
m_iStatus = 0; |
| 162 |
m_szSPI = 0; |
| 163 |
|
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
/*! |
| 168 |
* Query if this plugin supports the requested AbiWord version |
| 169 |
* |
| 170 |
* \param major - "1" |
| 171 |
* \param minor - "9" |
| 172 |
* \param release - "4" |
| 173 |
* \return true if it supports the requested version, false otherwise |
| 174 |
*/ |
| 175 |
bool XAP_Module::supportsAbiVersion (UT_uint32 major, UT_uint32 minor, UT_uint32 release) |
| 176 |
{ |
| 177 |
UT_ASSERT (m_bLoaded && m_bRegistered); |
| 178 |
|
| 179 |
typedef int (*plugin_supports_ver_t) (UT_uint32, UT_uint32, UT_uint32); |
| 180 |
plugin_supports_ver_t plugin_supports_ver; |
| 181 |
plugin_supports_ver_t *pfunc = &plugin_supports_ver; |
| 182 |
int result = 0; |
| 183 |
|
| 184 |
if (m_fnSupportsVersion) |
| 185 |
{ |
| 186 |
result = m_fnSupportsVersion (major, minor, release); |
| 187 |
} |
| 188 |
else if (resolveSymbol ("abi_plugin_supports_version", reinterpret_cast<void **>(pfunc))) |
| 189 |
{ |
| 190 |
if (!plugin_supports_ver) |
| 191 |
{ |
| 192 |
// damn this sucks. probably not an abiword plugin |
| 193 |
return false; |
| 194 |
} |
| 195 |
result = plugin_supports_ver (major, minor, release); |
| 196 |
} |
| 197 |
return (result ? true : false); |
| 198 |
} |